Skip to content

Commit

Permalink
refactor(tool-utils): optimize arrow interaction and tree destruction…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
cnlimiter committed Jul 26, 2024
1 parent 566c28a commit 10f0789
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/main/java/committee/nova/mods/avaritia/util/ToolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<BlockPos> connectedLogs = getConnectedLogs(world, pos);

ItemCaptureHandler.enableItemCapture(true);

for (BlockPos logPos : connectedLogs) {
destroy(world, player, logPos, heldItem);
}

ItemCaptureHandler.enableItemCapture(false);

Set<ItemStack> drops = ItemCaptureHandler.getCapturedDrops();
ClustersUtils.spawnClusters(world, player, drops);
}

private static void destroy(Level world, Player player, BlockPos pos, ItemStack heldItem) {
Expand All @@ -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);
}
Expand All @@ -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<BlockPos> {
Expand Down

0 comments on commit 10f0789

Please sign in to comment.