NaturesAura/src/main/java/de/ellpeck/naturesaura/entities/EntityEffectInhibitor.java

209 lines
7.7 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.entities;
import de.ellpeck.naturesaura.Helper;
2019-02-22 19:06:47 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2020-01-26 01:41:49 +01:00
import de.ellpeck.naturesaura.items.ItemEffectPowder;
import de.ellpeck.naturesaura.items.ModItems;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.misc.LevelData;
2021-12-06 14:38:12 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-06 14:38:12 +01:00
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tuple;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
2021-12-19 16:26:31 +01:00
import net.minecraft.world.entity.player.Player;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
2024-02-03 14:56:07 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2021-12-19 16:26:31 +01:00
import org.jetbrains.annotations.Nullable;
public class EntityEffectInhibitor extends Entity implements IVisualizable {
2021-12-06 14:38:12 +01:00
private static final EntityDataAccessor<String> INHIBITED_EFFECT = SynchedEntityData.defineId(EntityEffectInhibitor.class, EntityDataSerializers.STRING);
private static final EntityDataAccessor<Integer> COLOR = SynchedEntityData.defineId(EntityEffectInhibitor.class, EntityDataSerializers.INT);
private static final EntityDataAccessor<Integer> AMOUNT = SynchedEntityData.defineId(EntityEffectInhibitor.class, EntityDataSerializers.INT);
private ResourceLocation lastEffect;
private boolean powderListDirty;
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2019-02-21 12:42:45 +01:00
public int renderTicks;
2021-12-04 15:40:09 +01:00
public EntityEffectInhibitor(EntityType<?> entityTypeIn, Level levelIn) {
super(entityTypeIn, levelIn);
}
2021-12-04 15:40:09 +01:00
public static void place(Level level, ItemStack stack, double posX, double posY, double posZ) {
2021-12-06 14:38:12 +01:00
var effect = ItemEffectPowder.getEffect(stack);
var entity = new EntityEffectInhibitor(ModEntities.EFFECT_INHIBITOR, level);
2019-02-22 19:06:47 +01:00
entity.setInhibitedEffect(effect);
entity.setColor(NaturesAuraAPI.EFFECT_POWDERS.get(effect));
entity.setAmount(stack.getCount());
2021-12-06 14:38:12 +01:00
entity.setPos(posX, posY, posZ);
level.addFreshEntity(entity);
2019-02-22 19:06:47 +01:00
}
@Override
2021-12-06 14:38:12 +01:00
public void onAddedToWorld() {
super.onAddedToWorld();
this.powderListDirty = true;
}
@Override
2021-12-06 14:38:12 +01:00
public void onRemovedFromWorld() {
super.onRemovedFromWorld();
// we pass a null effect because we want to remove our effect from the world
this.updatePowderListStatus(null);
}
2020-01-21 23:02:39 +01:00
@Override
2021-12-06 14:38:12 +01:00
protected void defineSynchedData() {
this.entityData.define(EntityEffectInhibitor.INHIBITED_EFFECT, "");
2022-06-27 15:24:04 +02:00
this.entityData.define(EntityEffectInhibitor.COLOR, 0);
this.entityData.define(EntityEffectInhibitor.AMOUNT, 0);
2020-01-21 23:02:39 +01:00
}
@Override
2021-12-06 14:38:12 +01:00
public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
super.onSyncedDataUpdated(key);
2022-06-27 15:24:04 +02:00
if (EntityEffectInhibitor.INHIBITED_EFFECT.equals(key) || EntityEffectInhibitor.AMOUNT.equals(key))
this.powderListDirty = true;
}
@Override
2021-12-06 14:38:12 +01:00
public void setPos(double x, double y, double z) {
if (x != this.getX() || y != this.getY() || z != this.getZ())
this.powderListDirty = true;
2021-12-06 14:38:12 +01:00
super.setPos(x, y, z);
}
@Override
2020-01-21 23:02:39 +01:00
public void tick() {
super.tick();
if (this.powderListDirty)
this.updatePowderListStatus(this.getInhibitedEffect());
2023-07-08 12:32:27 +02:00
if (this.level().isClientSide) {
if (this.level().getGameTime() % 5 == 0) {
2020-01-23 20:57:56 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2023-07-08 12:32:27 +02:00
this.getX() + this.level().random.nextGaussian() * 0.1F,
2021-12-06 14:38:12 +01:00
this.getY(),
2023-07-08 12:32:27 +02:00
this.getZ() + this.level().random.nextGaussian() * 0.1F,
this.level().random.nextGaussian() * 0.005F,
this.level().random.nextFloat() * 0.03F,
this.level().random.nextGaussian() * 0.005F,
this.getColor(), this.level().random.nextFloat() * 3F + 1F, 120, 0F, true, true);
}
2019-02-21 12:42:45 +01:00
this.renderTicks++;
}
}
2020-01-21 23:02:39 +01:00
@Override
2021-12-06 14:38:12 +01:00
protected void readAdditionalSaveData(CompoundTag compound) {
2020-01-21 23:02:39 +01:00
this.setInhibitedEffect(new ResourceLocation(compound.getString("effect")));
this.setColor(compound.getInt("color"));
this.setAmount(compound.contains("amount") ? compound.getInt("amount") : 24);
}
@Override
2021-12-06 14:38:12 +01:00
protected void addAdditionalSaveData(CompoundTag compound) {
2020-01-21 23:02:39 +01:00
compound.putString("effect", this.getInhibitedEffect().toString());
compound.putInt("color", this.getColor());
compound.putInt("amount", this.getAmount());
}
2021-12-19 16:26:31 +01:00
@Override
public boolean skipAttackInteraction(Entity entity) {
2023-07-08 12:32:27 +02:00
return entity instanceof Player player && (!this.level().mayInteract(player, this.blockPosition()) || this.hurt(this.damageSources().playerAttack(player), 0.0F));
2021-12-19 16:26:31 +01:00
}
@Override
2021-12-06 14:38:12 +01:00
public boolean hurt(DamageSource source, float amount) {
2023-07-08 12:32:27 +02:00
if (source.getEntity() != null && !this.level().isClientSide) {
2021-12-06 14:38:12 +01:00
this.spawnAtLocation(this.getDrop(), 0F);
this.kill();
return true;
2021-12-19 16:26:31 +01:00
} else {
2021-12-06 14:38:12 +01:00
return super.hurt(source, amount);
2021-12-19 16:26:31 +01:00
}
}
@Override
public boolean isPickable() {
// SOMEHOW, this actually means "is raytraceable" IN GENERAL even though getPickResult is about middle-clicking, jesus fucking christ mojang
return true;
}
@Nullable
@Override
public ItemStack getPickResult() {
return this.getDrop();
}
2019-02-22 19:06:47 +01:00
public ItemStack getDrop() {
2020-01-26 01:41:49 +01:00
return ItemEffectPowder.setEffect(new ItemStack(ModItems.EFFECT_POWDER, this.getAmount()), this.getInhibitedEffect());
2019-02-22 19:06:47 +01:00
}
2020-02-07 15:22:30 +01:00
public ResourceLocation getInhibitedEffect() {
2022-06-27 15:24:04 +02:00
var effect = this.entityData.get(EntityEffectInhibitor.INHIBITED_EFFECT);
2020-02-07 15:22:30 +01:00
if (effect == null || effect.isEmpty())
return null;
return new ResourceLocation(effect);
}
public void setInhibitedEffect(ResourceLocation effect) {
this.entityData.set(EntityEffectInhibitor.INHIBITED_EFFECT, effect != null ? effect.toString() : "");
}
2020-02-07 15:22:30 +01:00
public int getColor() {
2022-06-27 15:24:04 +02:00
return this.entityData.get(EntityEffectInhibitor.COLOR);
}
public void setColor(int color) {
2022-06-27 15:24:04 +02:00
this.entityData.set(EntityEffectInhibitor.COLOR, color);
}
2020-02-07 15:22:30 +01:00
public int getAmount() {
2022-06-27 15:24:04 +02:00
return this.entityData.get(EntityEffectInhibitor.AMOUNT);
}
public void setAmount(int amount) {
2022-06-27 15:24:04 +02:00
this.entityData.set(EntityEffectInhibitor.AMOUNT, amount);
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-06 14:38:12 +01:00
public AABB getVisualizationBounds(Level level, BlockPos pos) {
return Helper.aabb(this.getEyePosition()).inflate(this.getAmount());
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-04 15:40:09 +01:00
public int getVisualizationColor(Level level, BlockPos pos) {
return this.getColor();
}
private void updatePowderListStatus(ResourceLocation inhibitedEffect) {
2023-07-08 12:32:27 +02:00
var powders = ((LevelData) ILevelData.getLevelData(this.level())).effectPowders;
if (this.lastEffect != null) {
2021-12-06 14:38:12 +01:00
var oldList = powders.get(this.lastEffect);
oldList.removeIf(t -> this.getEyePosition().equals(t.getA()));
}
if (inhibitedEffect != null) {
var newList = powders.get(inhibitedEffect);
2021-12-06 14:38:12 +01:00
newList.add(new Tuple<>(this.getEyePosition(), this.getAmount()));
}
this.powderListDirty = false;
this.lastEffect = inhibitedEffect;
}
}