From 8b525252a545ccdb6d6eb4c3b65af03d4c9e9601 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sat, 28 Nov 2020 22:00:52 +0000 Subject: [PATCH] Added leaf blower logic :D --- .../common/items/ActuallyItems.java | 5 +- .../common/items/useables/LeafBlowerItem.java | 89 +++++++++++++++++++ .../items/useables/TeleportStaffItem.java | 2 +- 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/items/useables/LeafBlowerItem.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java index 142b526f5..3b15b02d9 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/ActuallyItems.java @@ -3,6 +3,7 @@ package de.ellpeck.actuallyadditions.common.items; import com.google.common.collect.ImmutableSet; import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.items.useables.AllInOneTool; +import de.ellpeck.actuallyadditions.common.items.useables.LeafBlowerItem; import de.ellpeck.actuallyadditions.common.items.useables.ManualItem; import de.ellpeck.actuallyadditions.common.items.useables.TeleportStaffItem; import de.ellpeck.actuallyadditions.common.materials.ArmorMaterials; @@ -171,8 +172,8 @@ public final class ActuallyItems { public static final RegistryObject CRUSHED_COAL = ITEMS.register("crushed_coal", basicItem()); public static final RegistryObject CRUSHED_BLACK_QUARTZ = ITEMS.register("crushed_black_quartz", basicItem()); public static final RegistryObject SOLIDIFIED_EXPERIENCE = ITEMS.register("solidified_experience", basicItem()); - public static final RegistryObject LEAF_BLOWER = ITEMS.register("leaf_blower", basicItem()); - public static final RegistryObject ADVANCED_LEAF_BLOWER = ITEMS.register("advanced_leaf_blower", basicItem()); + public static final RegistryObject LEAF_BLOWER = ITEMS.register("leaf_blower", () -> new LeafBlowerItem(false)); + public static final RegistryObject ADVANCED_LEAF_BLOWER = ITEMS.register("advanced_leaf_blower", () -> new LeafBlowerItem(true)); public static final RegistryObject RING_OF_GROWTH = ITEMS.register("ring_of_growth", basicItem()); public static final RegistryObject RING_OF_MAGNETIZING = ITEMS.register("ring_of_magnetizing", basicItem()); public static final RegistryObject RING_OF_SPEED = ITEMS.register("ring_of_speed", basicItem()); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/LeafBlowerItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/LeafBlowerItem.java new file mode 100644 index 000000000..88b51ba17 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/LeafBlowerItem.java @@ -0,0 +1,89 @@ +package de.ellpeck.actuallyadditions.common.items.useables; + +import de.ellpeck.actuallyadditions.common.items.ActuallyItem; +import de.ellpeck.actuallyadditions.common.items.IUseItem; +import net.minecraft.block.Block; +import net.minecraft.block.BushBlock; +import net.minecraft.block.LeavesBlock; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.SoundEvents; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.common.IForgeShearable; + +import java.util.ArrayList; +import java.util.Collections; + +public class LeafBlowerItem extends ActuallyItem implements IUseItem { + private final boolean isAdvanced; + + public LeafBlowerItem(boolean isAdvanced) { + super(baseProps().maxStackSize(0)); + this.isAdvanced = isAdvanced; + } + + @Override + public ActionResult onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { + playerIn.setActiveHand(handIn); + return ActionResult.resultSuccess(playerIn.getHeldItem(handIn)); + } + + @Override + public void onUsingTick(ItemStack stack, LivingEntity player, int count) { + World world = player.world; + BlockPos pos = player.getPosition(); + + if (!world.isRemote) { + if (count <= this.getUseDuration(stack) && (this.isAdvanced || count % 3 == 0)) { + // Breaks the Blocks + this.findAndDestroy(world, pos); + + // Plays a Minecart sounds (It really sounds like a Leaf Blower!) + player.playSound(SoundEvents.ENTITY_MINECART_RIDING, 0.3F, 0.001F); + } + } + } + + private void findAndDestroy(World world, BlockPos pos) { + ArrayList breakPositions = new ArrayList<>(); + + int rangeSides = 5; + int rangeUp = 1; + for (int reachX = -rangeSides; reachX < rangeSides + 1; reachX++) { + for (int reachZ = -rangeSides; reachZ < rangeSides + 1; reachZ++) { + for (int reachY = this.isAdvanced ? -rangeSides : -rangeUp; reachY < (this.isAdvanced ? rangeSides : rangeUp) + 1; reachY++) { + //The current Block to break + BlockPos currentPos = new BlockPos(pos.getX() + reachX, pos.getY() + reachY, pos.getZ() + reachZ); + Block block = world.getBlockState(currentPos).getBlock(); + + if ((block instanceof BushBlock || block instanceof IForgeShearable) && (this.isAdvanced || !(world.getBlockState(currentPos).getBlock() instanceof LeavesBlock))) { + breakPositions.add(currentPos); + } + } + } + } + + if (breakPositions.isEmpty()) { + return; + } + + Collections.shuffle(breakPositions); + BlockPos theCoord = breakPositions.get(0); + + world.destroyBlock(theCoord, true); + } + + @Override + public int getUseDuration(ItemStack stack) { + return Integer.MAX_VALUE; + } + + @Override + public boolean canUse(ItemStack stack) { + return true; // todo: add energy logic + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java index 882f728ed..112c135eb 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/useables/TeleportStaffItem.java @@ -74,6 +74,6 @@ public class TeleportStaffItem extends ActuallyItem implements IUseItem { @Override public boolean canUse(ItemStack stack) { - return true; + return true; // todo: add energy logic } }