Unicopia/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java

293 lines
11 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.ability;
import java.util.*;
import java.util.stream.Stream;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.data.Hit;
import com.minelittlepony.unicopia.ability.data.Pos;
2021-08-20 22:25:46 +02:00
import com.minelittlepony.unicopia.ability.data.tree.TreeType;
2022-09-20 23:50:15 +02:00
import com.minelittlepony.unicopia.client.minelittlepony.MineLPDelegate;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.entity.Living;
2023-06-02 21:20:30 +02:00
import com.minelittlepony.unicopia.entity.damage.UDamageTypes;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles;
2023-04-30 11:46:33 +02:00
import com.minelittlepony.unicopia.server.world.BlockDestructionManager;
import com.minelittlepony.unicopia.util.*;
import net.minecraft.block.BeehiveBlock;
2021-02-13 12:50:24 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BeehiveBlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.BeeEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
/**
* Earth Pony kicking ability
*/
public class EarthPonyKickAbility implements Ability<Pos> {
@Override
public int getWarmupTime(Pony player) {
return 3;
}
@Override
public int getCooldownTime(Pony player) {
return 50;
}
@Override
public boolean canUse(Race race) {
return race.canUseEarth();
}
@Override
public Identifier getIcon(Pony player) {
Identifier id = Abilities.REGISTRY.getId(this);
return new Identifier(id.getNamespace(), "textures/gui/ability/" + id.getPath()
+ "_" + player.getObservedSpecies().getId().getPath()
+ ".png");
}
@Override
public double getCostEstimate(Pony player) {
return TraceHelper.findBlock(player.asEntity(), getKickDirection(player) * 6, 1)
.filter(pos -> TreeType.at(pos, player.asWorld()) != TreeType.NONE)
.isPresent() ? 3 : 1;
}
@Override
public Optional<Pos> prepareQuickAction(Pony player, ActivationType type) {
return Optional.of(getDefaultKickLocation(player));
}
@Override
public boolean onQuickAction(Pony player, ActivationType type, Optional<Pos> data) {
if (type == ActivationType.TAP) {
if (!player.isClient()) {
data.ifPresent(kickLocation -> {
Vec3d origin = player.getOriginVector();
World w = player.asWorld();
for (var e : VecHelper.findInRange(player.asEntity(), w, kickLocation.vec(), 2, EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR)) {
if (e instanceof LivingEntity entity) {
float calculatedStrength = 0.5F * (1 + player.getLevel().getScaled(9));
2023-06-02 21:20:30 +02:00
entity.damage(player.damageOf(UDamageTypes.KICK, player), player.asWorld().random.nextBetween(2, 10) + calculatedStrength);
entity.takeKnockback(calculatedStrength, origin.x - entity.getX(), origin.z - entity.getZ());
Living.updateVelocity(entity);
player.subtractEnergyCost(3);
player.setAnimation(Animation.KICK, Animation.Recipient.ANYONE);
return;
}
}
BlockPos pos = kickLocation.pos();
EarthPonyStompAbility.stompBlock(w, pos, 10 * (1 + player.getLevel().getScaled(5)) * w.getBlockState(pos).calcBlockBreakingDelta(player.asEntity(), w, pos));
player.setAnimation(Animation.KICK, Animation.Recipient.ANYONE);
});
}
return true;
}
if (type == ActivationType.DOUBLE_TAP && player.asEntity().isOnGround() && player.getMagicalReserves().getMana().get() > 40) {
player.getPhysics().dashForward((float)player.asWorld().random.nextTriangular(3.5F, 0.3F));
player.subtractEnergyCost(4);
return true;
}
return false;
}
@Nullable
@Override
2023-08-16 00:18:41 +02:00
public Optional<Pos> prepare(Pony player) {
return TraceHelper.findBlock(player.asEntity(), 6 * getKickDirection(player), 1)
.filter(pos -> TreeType.at(pos, player.asWorld()) != TreeType.NONE)
.map(Pos::new)
2023-08-16 00:18:41 +02:00
.or(() -> Optional.of(getDefaultKickLocation(player)));
}
private int getKickDirection(Pony player) {
return MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isEquine() ? -1 : 1;
}
private Pos getDefaultKickLocation(Pony player) {
Vec3d kickVector = player.asEntity().getRotationVector().multiply(1, 0, 1);
if (MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isEquine()) {
kickVector = kickVector.rotateY((float)Math.PI);
}
2023-06-02 21:20:30 +02:00
return new Pos(BlockPos.ofFloored(player.getOriginVector().add(kickVector)));
2021-08-20 22:22:28 +02:00
}
@Override
public Hit.Serializer<Pos> getSerializer() {
return Pos.SERIALIZER;
}
@Override
2023-08-16 00:18:41 +02:00
public boolean apply(Pony iplayer, Pos data) {
BlockPos pos = data.pos();
TreeType tree = TreeType.at(pos, iplayer.asWorld());
2023-08-16 00:18:41 +02:00
if (tree == TreeType.NONE || tree.findBase(iplayer.asWorld(), pos)
.map(base -> tree.countBlocks(iplayer.asWorld(), pos) > 0)
.orElse(false)) {
return false;
}
iplayer.setAnimation(Animation.KICK, Animation.Recipient.ANYONE);
iplayer.subtractEnergyCost(tree == TreeType.NONE ? 1 : 3);
ParticleUtils.spawnParticle(iplayer.asWorld(), UParticles.GROUND_POUND, data.vec(), Vec3d.ZERO);
PlayerEntity player = iplayer.asEntity();
2023-06-03 13:40:54 +02:00
if (BlockDestructionManager.of(player.getWorld()).getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) {
if (player.getWorld().random.nextInt(30) == 0) {
tree.traverse(player.getWorld(), pos, (w, state, p, recurseLevel) -> {
2021-02-13 12:50:24 +01:00
if (recurseLevel < 5) {
w.breakBlock(p, true);
} else {
Block.dropStacks(w.getBlockState(p), w, p);
w.setBlockState(p, Blocks.AIR.getDefaultState(), 3);
}
});
}
iplayer.subtractEnergyCost(3);
} else {
2021-02-13 12:50:24 +01:00
int cost = dropApples(player, pos);
if (cost > 0) {
iplayer.subtractEnergyCost(cost / 7F);
}
}
2023-08-16 00:18:41 +02:00
return true;
}
@Override
2023-08-16 00:18:41 +02:00
public void warmUp(Pony player, AbilitySlot slot) {
player.getMagicalReserves().getExertion().addPercent(40);
}
@Override
2023-08-16 00:18:41 +02:00
public void coolDown(Pony player, AbilitySlot slot) {
}
2021-02-13 12:50:24 +01:00
private int dropApples(PlayerEntity player, BlockPos pos) {
2023-06-03 13:40:54 +02:00
TreeType tree = TreeType.at(pos, player.getWorld());
2023-06-03 13:40:54 +02:00
if (tree.countBlocks(player.getWorld(), pos) > 0) {
List<ItemEntity> capturedDrops = new ArrayList<>();
2023-06-03 13:40:54 +02:00
tree.traverse(player.getWorld(), pos, (world, state, position, recurse) -> {
2021-02-13 12:50:24 +01:00
affectBlockChange(player, position);
}, (world, state, position, recurse) -> {
affectBlockChange(player, position);
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);
2021-02-13 12:50:24 +01:00
}
});
2023-06-03 13:40:54 +02:00
capturedDrops.forEach(player.getWorld()::spawnEntity);
return capturedDrops.size() / 3;
}
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()) {
2022-10-07 00:30:51 +02:00
return Stream.of(tree.pickRandomStack(world.random, treeState));
}
if (below.getBlock() instanceof Buckable buckable) {
return buckable.onBucked((ServerWorld)world, below, down).stream();
}
return Stream.empty();
}
2021-02-13 12:50:24 +01:00
private void affectBlockChange(PlayerEntity player, BlockPos position) {
2023-06-03 13:40:54 +02:00
BlockDestructionManager.of(player.getWorld()).damageBlock(position, 4);
PosHelper.all(position, p -> {
2023-06-03 13:40:54 +02:00
BlockState s = player.getWorld().getBlockState(p);
if (s.getBlock() instanceof BeehiveBlock) {
2023-06-03 13:40:54 +02:00
if (player.getWorld().getBlockEntity(p) instanceof BeehiveBlockEntity hive) {
hive.angerBees(player, s, BeehiveBlockEntity.BeeState.EMERGENCY);
}
2023-06-03 13:40:54 +02:00
player.getWorld().updateComparators(position, s.getBlock());
Box area = new Box(position).expand(8, 6, 8);
2023-06-03 13:40:54 +02:00
List<BeeEntity> nearbyBees = player.getWorld().getNonSpectatingEntities(BeeEntity.class, area);
if (!nearbyBees.isEmpty()) {
2023-06-03 13:40:54 +02:00
List<PlayerEntity> nearbyPlayers = player.getWorld().getNonSpectatingEntities(PlayerEntity.class, area);
int i = nearbyPlayers.size();
for (BeeEntity bee : nearbyBees) {
if (bee.getTarget() == null) {
2023-06-03 13:40:54 +02:00
bee.setTarget(nearbyPlayers.get(player.getWorld().random.nextInt(i)));
}
}
}
}
}, PosHelper.HORIZONTAL);
}
public interface Buckable {
List<ItemStack> onBucked(ServerWorld world, BlockState state, BlockPos pos);
}
}