From b10203db01a01f3dae66f657c03f6988fdf8d03d Mon Sep 17 00:00:00 2001 From: grimmauld Date: Wed, 7 Apr 2021 15:13:07 +0200 Subject: [PATCH] Extract block breaking in TreeCutter.Tree to AbstractBlockBreakQueue for better reusability and option of saw passing a different item --- .../utility/AbstractBlockBreakQueue.java | 32 +++++++++++++++++++ .../create/foundation/utility/TreeCutter.java | 22 ++----------- 2 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/utility/AbstractBlockBreakQueue.java diff --git a/src/main/java/com/simibubi/create/foundation/utility/AbstractBlockBreakQueue.java b/src/main/java/com/simibubi/create/foundation/utility/AbstractBlockBreakQueue.java new file mode 100644 index 000000000..69886577e --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/AbstractBlockBreakQueue.java @@ -0,0 +1,32 @@ +package com.simibubi.create.foundation.utility; + +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.event.ForgeEventFactory; + +import javax.annotation.Nullable; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +public abstract class AbstractBlockBreakQueue { + protected Consumer makeCallbackFor(World world, float effectChance, ItemStack toDamage, @Nullable PlayerEntity playerEntity, BiConsumer drop) { + return pos -> { + ItemStack usedTool = toDamage.copy(); + BlockHelper.destroyBlockAs(world, pos, playerEntity, toDamage, effectChance, stack -> drop.accept(pos, stack)); + if (toDamage.isEmpty() && !usedTool.isEmpty()) + ForgeEventFactory.onPlayerDestroyItem(playerEntity, usedTool, Hand.MAIN_HAND); + }; + } + + public void destroyBlocks(World world, @Nullable LivingEntity entity, BiConsumer drop) { + PlayerEntity playerEntity = entity instanceof PlayerEntity ? ((PlayerEntity) entity) : null; + ItemStack toDamage = playerEntity != null && !playerEntity.isCreative() ? playerEntity.getHeldItemMainhand() : ItemStack.EMPTY; + destroyBlocks(world, toDamage, playerEntity, drop); + } + + public abstract void destroyBlocks(World world, ItemStack toDamage, @Nullable PlayerEntity playerEntity, BiConsumer drop); +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/TreeCutter.java b/src/main/java/com/simibubi/create/foundation/utility/TreeCutter.java index cd4b9db41..9b50aaa64 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/TreeCutter.java +++ b/src/main/java/com/simibubi/create/foundation/utility/TreeCutter.java @@ -2,22 +2,18 @@ package com.simibubi.create.foundation.utility; import com.simibubi.create.AllTags; import net.minecraft.block.*; -import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.tags.BlockTags; import net.minecraft.util.Direction; -import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; -import net.minecraftforge.event.ForgeEventFactory; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.*; import java.util.function.BiConsumer; -import java.util.function.Consumer; import java.util.function.Predicate; public class TreeCutter { @@ -209,7 +205,7 @@ public class TreeCutter { return state.contains(LeavesBlock.DISTANCE); } - public static class Tree { + public static class Tree extends AbstractBlockBreakQueue{ private final List logs; private final List leaves; @@ -218,22 +214,10 @@ public class TreeCutter { this.leaves = leaves; } - public void destroyBlocks(World world, @Nullable LivingEntity entity, BiConsumer drop) { - PlayerEntity playerEntity = entity instanceof PlayerEntity ? ((PlayerEntity) entity) : null; - ItemStack toDamage = playerEntity != null && !playerEntity.isCreative() ? playerEntity.getHeldItemMainhand() : ItemStack.EMPTY; - + @Override + public void destroyBlocks(World world, ItemStack toDamage, @Nullable PlayerEntity playerEntity, BiConsumer drop) { logs.forEach(makeCallbackFor(world, 1/2f, toDamage, playerEntity, drop)); leaves.forEach(makeCallbackFor(world, 1/8f, toDamage, playerEntity, drop)); } - - private Consumer makeCallbackFor(World world, float effectChance, ItemStack toDamage, @Nullable PlayerEntity playerEntity, BiConsumer drop) { - return pos -> { - ItemStack usedTool = toDamage.copy(); - BlockHelper.destroyBlockAs(world, pos, playerEntity, toDamage, effectChance, stack -> drop.accept(pos, stack)); - if (toDamage.isEmpty() && !usedTool.isEmpty()) - ForgeEventFactory.onPlayerDestroyItem(playerEntity, usedTool, Hand.MAIN_HAND); - }; - } } - }