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

121 lines
3.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.ability;
2020-01-16 12:35:46 +01:00
2023-08-16 00:18:41 +02:00
import java.util.Optional;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.data.Hit;
import com.minelittlepony.unicopia.ability.data.Pos;
import com.minelittlepony.unicopia.block.UBlocks;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.particle.MagicParticleEffect;
2022-10-01 18:20:53 +02:00
import com.minelittlepony.unicopia.util.TraceHelper;
2020-01-16 12:35:46 +01:00
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
2024-01-03 21:51:24 +01:00
import net.minecraft.block.FarmlandBlock;
2020-01-16 12:35:46 +01:00
import net.minecraft.item.BoneMealItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
/**
* Earth Pony ability to grow crops
*/
public class EarthPonyGrowAbility implements Ability<Pos> {
2020-01-16 12:35:46 +01:00
@Override
2020-04-15 18:12:00 +02:00
public int getWarmupTime(Pony player) {
2020-01-16 12:35:46 +01:00
return 10;
}
@Override
2020-04-15 18:12:00 +02:00
public int getCooldownTime(Pony player) {
2020-01-16 12:35:46 +01:00
return 50;
}
@Override
public boolean canUse(Race race) {
return race.canUseEarth();
2020-01-16 12:35:46 +01:00
}
@Override
2023-08-16 00:18:41 +02:00
public Optional<Pos> prepare(Pony player) {
return TraceHelper.findBlock(player.asEntity(), 3, 1).map(Pos::new);
2020-01-16 12:35:46 +01:00
}
@Override
public Hit.Serializer<Pos> getSerializer() {
return Pos.SERIALIZER;
2020-01-16 12:35:46 +01:00
}
@Override
public double getCostEstimate(Pony player) {
return 10;
}
2020-01-16 12:35:46 +01:00
@Override
2023-08-16 00:18:41 +02:00
public boolean apply(Pony player, Pos data) {
2020-01-16 12:35:46 +01:00
int count = 0;
for (BlockPos pos : BlockPos.iterate(
data.pos().add(-2, -2, -2),
data.pos().add( 2, 2, 2))) {
count += applySingle(player.asWorld(), player.asWorld().getBlockState(pos), pos);
2020-01-16 12:35:46 +01:00
}
if (count > 0) {
player.subtractEnergyCost(count / 5D);
2020-01-16 12:35:46 +01:00
}
2023-08-16 00:18:41 +02:00
return true;
2020-01-16 12:35:46 +01:00
}
protected int applySingle(World w, BlockState state, BlockPos pos) {
ItemStack stack = new ItemStack(Items.BONE_MEAL);
if (state.getBlock() instanceof Growable growable) {
return growable.grow(w, state, pos) ? 1 : 0;
}
if (BoneMealItem.useOnFertilizable(stack, w, pos)) {
if (w.random.nextInt(350) == 0) {
if (w.getBlockState(pos.down()).isOf(Blocks.FARMLAND)) {
w.setBlockState(pos.down(), Blocks.DIRT.getDefaultState());
}
w.setBlockState(pos, UBlocks.PLUNDER_VINE_BUD.getDefaultState());
} else if (w.random.nextInt(5000) == 0) {
2024-01-02 23:03:41 +01:00
if (w.getBlockState(pos.down()).isOf(Blocks.FARMLAND)) {
2024-01-03 21:51:24 +01:00
FarmlandBlock.setToDirt(null, state, w, pos.down());
2024-01-02 23:03:41 +01:00
}
UBlocks.CURING_JOKE.grow(w, state, pos);
}
return 1;
}
if (BoneMealItem.useOnGround(stack, w, pos, Direction.UP)) {
2020-01-16 12:35:46 +01:00
return 1;
}
return 0;
}
@Override
2023-08-16 00:18:41 +02:00
public void warmUp(Pony player, AbilitySlot slot) {
player.getMagicalReserves().getExertion().addPercent(30);
2020-01-16 12:35:46 +01:00
if (player.asWorld().isClient()) {
2020-04-22 16:28:20 +02:00
player.spawnParticles(MagicParticleEffect.UNICORN, 1);
2020-01-16 12:35:46 +01:00
}
}
@Override
2023-08-16 00:18:41 +02:00
public void coolDown(Pony player, AbilitySlot slot) {
2020-01-16 12:35:46 +01:00
}
public interface Growable {
boolean grow(World world, BlockState state, BlockPos pos);
}
2020-01-16 12:35:46 +01:00
}