From 10f0789af09f4e459333d181d53b3655d0bffb8b Mon Sep 17 00:00:00 2001 From: cnlimiter Date: Fri, 26 Jul 2024 22:22:31 +0800 Subject: [PATCH] refactor(tool-utils): optimize arrow interaction and tree destruction logic - Refactor arrow hit logic by using instance of LivingEntity and ServerPlayer to improve code clarity and readability. - Implement capture and cluster spawning for dropped items during tree destruction, ensuring more controlled item handling and better player feedback. - Expand the isLog method to include leaves, optimizing the tree destruction logic for a more comprehensive handling of wood types. These changes enhance the overall functionality and performance of the tool utilities, providing a more streamlined player experience during arrow interactions and tree destruction processes. --- .../nova/mods/avaritia/util/ToolUtils.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/committee/nova/mods/avaritia/util/ToolUtils.java b/src/main/java/committee/nova/mods/avaritia/util/ToolUtils.java index 1b807407..99054778 100644 --- a/src/main/java/committee/nova/mods/avaritia/util/ToolUtils.java +++ b/src/main/java/committee/nova/mods/avaritia/util/ToolUtils.java @@ -294,14 +294,14 @@ public static void infinityArrowDamage(@NotNull EntityHitResult result, Arrow ar } } - if (!arrow.level().isClientSide && owner instanceof LivingEntity) { - EnchantmentHelper.doPostHurtEffects(livingentity, owner); - EnchantmentHelper.doPostDamageEffects((LivingEntity)owner, livingentity); + if (!arrow.level().isClientSide && owner instanceof LivingEntity livingOwner) { + EnchantmentHelper.doPostHurtEffects(livingentity, livingOwner); + EnchantmentHelper.doPostDamageEffects(livingOwner, livingentity); } arrow.doPostHurtEffects(livingentity); - if (livingentity != owner && livingentity instanceof Player && owner instanceof ServerPlayer && !arrow.isSilent()) { - ((ServerPlayer)owner).connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.ARROW_HIT_PLAYER, 0.0F)); + if (livingentity != owner && livingentity instanceof Player && owner instanceof ServerPlayer serverPlayer && !arrow.isSilent()) { + serverPlayer.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.ARROW_HIT_PLAYER, 0.0F)); } if (!entity.isAlive() && arrow.piercedAndKilledEntities != null) { @@ -391,7 +391,7 @@ public static void aoeAttack(Player player, float range, float damage, boolean h * Axe * ***/ public static boolean canHarvest(BlockPos pos, Level world) { - if (!isLog(world, pos)) { + if (!isLogOrLeaves(world, pos)) { return false; } @@ -407,9 +407,16 @@ public static boolean canHarvest(BlockPos pos, Level world) { public static void destroyTree(Player player, Level world, BlockPos pos, ItemStack heldItem) { List connectedLogs = getConnectedLogs(world, pos); + ItemCaptureHandler.enableItemCapture(true); + for (BlockPos logPos : connectedLogs) { destroy(world, player, logPos, heldItem); } + + ItemCaptureHandler.enableItemCapture(false); + + Set drops = ItemCaptureHandler.getCapturedDrops(); + ClustersUtils.spawnClusters(world, player, drops); } private static void destroy(Level world, Player player, BlockPos pos, ItemStack heldItem) { @@ -431,7 +438,7 @@ private static void collectLogs(Level world, BlockPos pos, BlockPosList position for (int y = -1; y <= 1; y++) { for (int z = -1; z <= 1; z++) { BlockPos p = pos.offset(x, y, z); - if (isLog(world, p)) { + if (isLogOrLeaves(world, p)) { if (positions.add(p)) { posList.add(p); } @@ -446,9 +453,9 @@ private static void collectLogs(Level world, BlockPos pos, BlockPosList position } } - private static boolean isLog(Level world, BlockPos pos) { + private static boolean isLogOrLeaves(Level world, BlockPos pos) { BlockState b = world.getBlockState(pos); - return b.is(BlockTags.LOGS); + return b.is(BlockTags.LOGS) || b.is(BlockTags.LEAVES); } private static class BlockPosList extends ArrayList {