ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemLeafBlower.java

136 lines
4.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemLeafBlower.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2015-03-29 15:29:05 +02:00
import de.ellpeck.actuallyadditions.api.misc.IDisplayStandItem;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import net.minecraft.block.*;
import net.minecraft.entity.LivingEntity;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2015-03-29 15:29:05 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;
import net.minecraft.tileentity.TileEntity;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.ActionResult;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-05-01 22:26:26 +02:00
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2015-03-29 15:29:05 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.IForgeShearable;
2015-03-29 15:29:05 +02:00
2021-02-26 22:15:48 +01:00
import java.util.ArrayList;
import java.util.Collections;
2019-05-02 09:10:29 +02:00
public class ItemLeafBlower extends ItemBase implements IDisplayStandItem {
2015-03-29 15:29:05 +02:00
private final boolean isAdvanced;
public ItemLeafBlower(boolean isAdvanced) {
super(ActuallyItems.defaultProps().maxStackSize(1));
2015-03-29 15:29:05 +02:00
this.isAdvanced = isAdvanced;
}
2015-10-03 10:19:40 +02:00
@Override
2021-02-26 22:15:48 +01:00
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
2016-03-18 23:47:22 +01:00
player.setActiveHand(hand);
return ActionResult.resultSuccess(player.getHeldItem(hand));
2015-10-03 10:19:40 +02:00
}
@Override
public UseAction getUseAction(ItemStack stack) {
return UseAction.BOW;
2015-10-03 10:19:40 +02:00
}
@Override
public int getUseDuration(ItemStack stack) {
2015-10-03 10:19:40 +02:00
return Integer.MAX_VALUE;
}
@Override
public void onUsingTick(ItemStack stack, LivingEntity player, int count) {
this.doUpdate(player.world, MathHelper.floor(player.getPosX()), MathHelper.floor(player.getPosY()), MathHelper.floor(player.getPosZ()), count, stack);
}
2019-05-02 09:10:29 +02:00
private boolean doUpdate(World world, int x, int y, int z, int time, ItemStack stack) {
if (!world.isRemote) {
if (time <= this.getUseDuration(stack) && (this.isAdvanced || time % 3 == 0)) {
//Breaks the Blocks
boolean broke = this.breakStuff(world, x, y, z);
//Plays a Minecart sounds (It really sounds like a Leaf Blower!)
world.playSound(null, x, y, z, SoundEvents.ENTITY_MINECART_RIDING, SoundCategory.PLAYERS, 0.3F, 0.001F);
return broke;
2015-03-29 15:29:05 +02:00
}
}
return false;
2015-03-29 15:29:05 +02:00
}
/**
* Breaks (harvests) Grass and Leaves around
2015-10-02 16:48:01 +02:00
*
* @param world The World
2015-10-02 16:48:01 +02:00
* @param x The X Position of the Player
* @param y The Y Position of the Player
* @param z The Z Position of the Player
*/
2019-05-02 09:10:29 +02:00
public boolean breakStuff(World world, int x, int y, int z) {
2019-02-27 19:53:05 +01:00
ArrayList<BlockPos> breakPositions = new ArrayList<>();
int rangeSides = 5;
int rangeUp = 1;
2019-05-02 09:10:29 +02:00
for (int reachX = -rangeSides; reachX < rangeSides + 1; reachX++) {
for (int reachZ = -rangeSides; reachZ < rangeSides + 1; reachZ++) {
2021-02-26 22:15:48 +01:00
for (int reachY = this.isAdvanced
? -rangeSides
: -rangeUp; reachY < (this.isAdvanced
? rangeSides
: rangeUp) + 1; reachY++) {
//The current Block to break
2019-05-02 09:10:29 +02:00
BlockPos pos = new BlockPos(x + reachX, y + reachY, z + reachZ);
2016-07-04 20:15:41 +02:00
Block block = world.getBlockState(pos).getBlock();
if ((block instanceof BushBlock || block instanceof IForgeShearable) && (this.isAdvanced || block instanceof LeavesBlock)) {
breakPositions.add(pos);
}
}
}
}
2019-05-02 09:10:29 +02:00
if (!breakPositions.isEmpty()) {
Collections.shuffle(breakPositions);
2016-01-08 13:31:58 +01:00
BlockPos theCoord = breakPositions.get(0);
2021-02-26 22:15:48 +01:00
BlockState theState = world.getBlockState(theCoord);
world.destroyBlock(theCoord, true);
// theState.getBlock().dropBlockAsItem(world, theCoord, theState, 0);
//Plays the Breaking Sound
world.playEvent(2001, theCoord, Block.getStateId(theState));
2016-01-23 23:29:19 +01:00
//Deletes the Block
2021-02-27 16:33:00 +01:00
world.setBlockState(theCoord, Blocks.AIR.getDefaultState());
2016-01-23 23:29:19 +01:00
return true;
2015-03-29 15:29:05 +02:00
}
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean update(ItemStack stack, TileEntity tile, int elapsedTicks) {
return this.doUpdate(tile.getWorld(), tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), elapsedTicks, stack);
}
@Override
2019-05-02 09:10:29 +02:00
public int getUsePerTick(ItemStack stack, TileEntity tile, int elapsedTicks) {
return 60;
2015-03-29 15:29:05 +02:00
}
}