2018-11-13 11:39:28 +01:00
|
|
|
package de.ellpeck.naturesaura.chunk.effect;
|
2018-10-30 18:18:56 +01:00
|
|
|
|
2018-11-13 18:21:41 +01:00
|
|
|
import de.ellpeck.naturesaura.ModConfig;
|
2018-11-13 11:39:28 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-11-12 01:29:33 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
|
2018-11-12 01:29:33 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-12-15 16:24:53 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.Items;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.*;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.chunk.LevelChunk;
|
|
|
|
import net.minecraft.world.level.levelgen.Heightmap;
|
2021-06-28 14:31:37 +02:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2018-10-30 18:18:56 +01:00
|
|
|
|
|
|
|
public class PlantBoostEffect implements IDrainSpotEffect {
|
2018-11-13 11:39:28 +01:00
|
|
|
|
|
|
|
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "plant_boost");
|
|
|
|
|
2019-02-09 21:55:40 +01:00
|
|
|
private int amount;
|
|
|
|
private int dist;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
|
2018-12-02 01:36:41 +01:00
|
|
|
if (spot <= 0)
|
2019-02-09 21:55:40 +01:00
|
|
|
return false;
|
2021-12-15 16:30:22 +01:00
|
|
|
var auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 30);
|
2021-06-28 14:31:37 +02:00
|
|
|
int aura = auraAndSpots.getLeft();
|
2019-01-29 11:46:38 +01:00
|
|
|
if (aura < 1500000)
|
2019-02-09 21:55:40 +01:00
|
|
|
return false;
|
2021-12-04 19:17:21 +01:00
|
|
|
this.amount = Math.min(45, Mth.ceil(Math.abs(aura) / 100000F / auraAndSpots.getRight()));
|
2019-02-09 21:55:40 +01:00
|
|
|
if (this.amount <= 1)
|
|
|
|
return false;
|
2021-12-04 19:17:21 +01:00
|
|
|
this.dist = Mth.clamp(Math.abs(aura) / 150000, 5, 35);
|
2019-02-09 21:55:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-10-30 18:18:56 +01:00
|
|
|
|
2019-02-09 21:55:40 +01:00
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public ActiveType isActiveHere(Player player, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(player.level, pos, spot))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ()) > this.dist * this.dist)
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (NaturesAuraAPI.instance().isEffectPowderActive(player.level, player.blockPosition(), NAME))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INHIBITED;
|
|
|
|
return ActiveType.ACTIVE;
|
2019-02-09 21:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getDisplayIcon() {
|
|
|
|
return new ItemStack(Items.WHEAT_SEEDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public void update(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(level, pos, spot))
|
2019-02-09 21:55:40 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = this.amount / 2 + level.random.nextInt(this.amount / 2); i >= 0; i--) {
|
|
|
|
var x = Mth.floor(pos.getX() + (2 * level.random.nextFloat() - 1) * this.dist);
|
|
|
|
var z = Mth.floor(pos.getZ() + (2 * level.random.nextFloat() - 1) * this.dist);
|
|
|
|
var plantPos = new BlockPos(x, level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z), z).below();
|
2021-12-15 16:24:53 +01:00
|
|
|
if (plantPos.distSqr(pos) <= this.dist * this.dist && level.isLoaded(plantPos)) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (NaturesAuraAPI.instance().isEffectPowderActive(level, plantPos, NAME))
|
2018-12-14 00:47:01 +01:00
|
|
|
continue;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var state = level.getBlockState(plantPos);
|
|
|
|
var block = state.getBlock();
|
2021-12-15 16:24:53 +01:00
|
|
|
if (block instanceof BonemealableBlock growable && !(block instanceof DoublePlantBlock) && !(block instanceof TallGrassBlock) && block != Blocks.GRASS_BLOCK) {
|
|
|
|
if (growable.isValidBonemealTarget(level, plantPos, state, false)) {
|
2020-03-14 00:47:54 +01:00
|
|
|
try {
|
2021-12-15 16:24:53 +01:00
|
|
|
growable.performBonemeal((ServerLevel) level, level.random, plantPos, state);
|
2020-03-14 00:47:54 +01:00
|
|
|
} catch (Exception e) {
|
2021-12-04 15:40:09 +01:00
|
|
|
// a lot of stuff throws here (double plants where generation only caused half of it to exist, bamboo at level height...)
|
2020-10-05 22:48:32 +02:00
|
|
|
// so just catch all, bleh
|
2020-03-14 00:47:54 +01:00
|
|
|
}
|
2021-12-15 16:30:22 +01:00
|
|
|
var closestSpot = IAuraChunk.getHighestSpot(level, plantPos, 25, pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(level, closestSpot).drainAura(closestSpot, 3500);
|
2018-10-30 18:18:56 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(level, plantPos, 32,
|
2020-01-26 00:16:06 +01:00
|
|
|
new PacketParticles(plantPos.getX(), plantPos.getY(), plantPos.getZ(), PacketParticles.Type.PLANT_BOOST));
|
2018-10-30 18:18:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 23:42:13 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
|
2020-01-24 17:05:41 +01:00
|
|
|
return ModConfig.instance.plantBoostEffect.get() && type.isSimilar(NaturesAuraAPI.TYPE_OVERWORLD);
|
2018-11-07 23:42:13 +01:00
|
|
|
}
|
2018-11-13 11:39:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
|
|
|
return NAME;
|
|
|
|
}
|
2018-10-30 18:18:56 +01:00
|
|
|
}
|