From 1cd3390b1ece7c24249f5aa778a6c4816c307145 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 25 Sep 2022 13:58:10 +0200 Subject: [PATCH] Fixed kicking apple trees not dropping the fruit --- .../ability/EarthPonyKickAbility.java | 99 ++++++++++--------- .../unicopia/block/FruitBearingBlock.java | 15 ++- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java index 8254d64c..10c48cee 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java @@ -1,11 +1,10 @@ package com.minelittlepony.unicopia.ability; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.stream.Stream; import org.jetbrains.annotations.Nullable; -import com.google.common.collect.Lists; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.ability.data.Hit; import com.minelittlepony.unicopia.ability.data.Pos; @@ -157,10 +156,8 @@ public class EarthPonyKickAbility implements Ability { PlayerEntity player = iplayer.getMaster(); - boolean harmed = player.getHealth() < player.getMaxHealth(); - if (BlockDestructionManager.of(player.world).getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) { - if (!harmed || player.world.random.nextInt(30) == 0) { + if (player.world.random.nextInt(30) == 0) { tree.traverse(player.world, pos, (w, state, p, recurseLevel) -> { if (recurseLevel < 5) { w.breakBlock(p, true); @@ -194,47 +191,23 @@ public class EarthPonyKickAbility implements Ability { TreeType tree = TreeType.at(pos, player.world); if (tree.countBlocks(player.world, pos) > 0) { - List capturedDrops = Lists.newArrayList(); + List capturedDrops = new ArrayList<>(); tree.traverse(player.world, pos, (world, state, position, recurse) -> { affectBlockChange(player, position); }, (world, state, position, recurse) -> { affectBlockChange(player, position); - - BlockState below = world.getBlockState(position.down()); - - if (below.isAir()) { - ItemStack stack = tree.pickRandomStack(state); - if (!stack.isEmpty()) { - world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, position, Block.getRawIdFromState(state)); - - capturedDrops.add(new ItemEntity(world, - position.getX() + world.random.nextFloat(), - position.getY() - 0.5, - position.getZ() + world.random.nextFloat(), - stack - )); - } - } else if (below.getBlock() instanceof Buckable buckable) { - List stacks = buckable.onBucked((ServerWorld)world, state, pos); - if (!stacks.isEmpty()) { - world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, position, Block.getRawIdFromState(state)); - stacks.forEach(stack -> { - capturedDrops.add(new ItemEntity(world, - position.getX() + world.random.nextFloat(), - position.getY() - 0.5, - position.getZ() + world.random.nextFloat(), - stack - )); - }); - } + List drops = buckBlock(tree, state, world, position) + .filter(i -> !i.isEmpty()) + .map(stack -> createDrop(stack, position, world)) + .toList(); + if (!drops.isEmpty()) { + world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, position, Block.getRawIdFromState(state)); + capturedDrops.addAll(drops); } }); - capturedDrops.forEach(item -> { - item.setToDefaultPickupDelay(); - player.world.spawnEntity(item); - }); + capturedDrops.forEach(player.world::spawnEntity); return capturedDrops.size() / 3; } @@ -242,6 +215,33 @@ public class EarthPonyKickAbility implements Ability { return 0; } + private ItemEntity createDrop(ItemStack stack, BlockPos pos, World world) { + ItemEntity entity = new ItemEntity(world, + pos.getX() + world.random.nextFloat(), + pos.getY() - 0.5, + pos.getZ() + world.random.nextFloat(), + stack + ); + entity.setToDefaultPickupDelay(); + return entity; + } + + private Stream buckBlock(TreeType tree, BlockState treeState, World world, BlockPos position) { + + if (treeState.getBlock() instanceof Buckable buckable) { + return buckable.onBucked((ServerWorld)world, treeState, position).stream(); + } + + BlockPos down = position.down(); + BlockState below = world.getBlockState(down); + + if (below.isAir()) { + return Stream.of(tree.pickRandomStack(treeState)); + } + + return Stream.empty(); + } + private void affectBlockChange(PlayerEntity player, BlockPos position) { BlockDestructionManager.of(player.world).damageBlock(position, 4); @@ -249,23 +249,24 @@ public class EarthPonyKickAbility implements Ability { BlockState s = player.world.getBlockState(p); if (s.getBlock() instanceof BeehiveBlock) { - BeehiveBlockEntity hive = (BeehiveBlockEntity)player.world.getBlockEntity(p); - if (hive != null) { - hive.angerBees(player, s, BeehiveBlockEntity.BeeState.BEE_RELEASED); + if (player.world.getBlockEntity(p) instanceof BeehiveBlockEntity hive) { + hive.angerBees(player, s, BeehiveBlockEntity.BeeState.EMERGENCY); } + player.world.updateComparators(position, s.getBlock()); + Box area = new Box(position).expand(8, 6, 8); List nearbyBees = player.world.getNonSpectatingEntities(BeeEntity.class, area); if (!nearbyBees.isEmpty()) { - List nearbyPlayers = player.world.getNonSpectatingEntities(PlayerEntity.class, area); - int i = nearbyPlayers.size(); + List nearbyPlayers = player.world.getNonSpectatingEntities(PlayerEntity.class, area); + int i = nearbyPlayers.size(); - for (BeeEntity bee : nearbyBees) { - if (bee.getTarget() == null) { - bee.setTarget(nearbyPlayers.get(player.world.random.nextInt(i))); - } - } + for (BeeEntity bee : nearbyBees) { + if (bee.getTarget() == null) { + bee.setTarget(nearbyPlayers.get(player.world.random.nextInt(i))); + } + } } } }, PosHelper.HORIZONTAL); diff --git a/src/main/java/com/minelittlepony/unicopia/block/FruitBearingBlock.java b/src/main/java/com/minelittlepony/unicopia/block/FruitBearingBlock.java index 8a9d6135..34236f23 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/FruitBearingBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/FruitBearingBlock.java @@ -6,6 +6,7 @@ import java.util.function.Supplier; import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.USounds; +import com.minelittlepony.unicopia.ability.EarthPonyKickAbility.Buckable; import net.minecraft.block.*; import net.minecraft.item.ItemStack; @@ -21,7 +22,7 @@ import net.minecraft.util.math.random.Random; import net.minecraft.world.*; import net.minecraft.world.event.GameEvent; -public class FruitBearingBlock extends LeavesBlock implements TintedBlock { +public class FruitBearingBlock extends LeavesBlock implements TintedBlock, Buckable { public static final IntProperty AGE = Properties.AGE_25; public static final int WITHER_AGE = 15; public static final EnumProperty STAGE = EnumProperty.of("stage", Stage.class); @@ -113,6 +114,18 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock { return newState; } + @Override + public List onBucked(ServerWorld world, BlockState state, BlockPos pos) { + world.setBlockState(pos, state.with(STAGE, Stage.IDLE).with(AGE, 0)); + + pos = pos.down(); + state = world.getBlockState(pos); + if (state.isOf(fruit.get()) && state.getBlock() instanceof Buckable buckable) { + return buckable.onBucked(world, state, pos); + } + return List.of(); + } + @Override public int getTint(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int foliageColor) { return TintedBlock.blend(foliageColor, overlay);