Fixed kicking apple trees not dropping the fruit

This commit is contained in:
Sollace 2022-09-25 13:58:10 +02:00
parent 9e0e03379e
commit 1cd3390b1e
2 changed files with 64 additions and 50 deletions

View file

@ -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<Pos> {
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<Pos> {
TreeType tree = TreeType.at(pos, player.world);
if (tree.countBlocks(player.world, pos) > 0) {
List<ItemEntity> capturedDrops = Lists.newArrayList();
List<ItemEntity> 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<ItemStack> 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<ItemEntity> 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<Pos> {
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<ItemStack> 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<Pos> {
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<BeeEntity> nearbyBees = player.world.getNonSpectatingEntities(BeeEntity.class, area);
if (!nearbyBees.isEmpty()) {
List<PlayerEntity> nearbyPlayers = player.world.getNonSpectatingEntities(PlayerEntity.class, area);
int i = nearbyPlayers.size();
List<PlayerEntity> 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);

View file

@ -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> STAGE = EnumProperty.of("stage", Stage.class);
@ -113,6 +114,18 @@ public class FruitBearingBlock extends LeavesBlock implements TintedBlock {
return newState;
}
@Override
public List<ItemStack> 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);