2018-12-14 00:47:01 +01:00
|
|
|
package de.ellpeck.naturesaura.entities;
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
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;
|
2019-01-28 15:43:21 +01:00
|
|
|
import de.ellpeck.naturesaura.api.render.IVisualizable;
|
2020-01-26 01:41:49 +01:00
|
|
|
import de.ellpeck.naturesaura.items.ItemEffectPowder;
|
2018-12-14 00:47:01 +01:00
|
|
|
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.protocol.Packet;
|
|
|
|
import net.minecraft.network.syncher.EntityDataAccessor;
|
|
|
|
import net.minecraft.network.syncher.EntityDataSerializers;
|
|
|
|
import net.minecraft.network.syncher.SynchedEntityData;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2019-02-21 12:27:54 +01:00
|
|
|
import net.minecraft.util.Tuple;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.world.damagesource.DamageSource;
|
|
|
|
import net.minecraft.world.damagesource.EntityDamageSource;
|
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.phys.AABB;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraftforge.network.NetworkHooks;
|
2019-02-21 12:27:54 +01:00
|
|
|
|
2019-01-28 15:43:21 +01:00
|
|
|
public class EntityEffectInhibitor extends Entity implements IVisualizable {
|
2018-12-14 00:47:01 +01:00
|
|
|
|
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);
|
2021-04-03 23:16:18 +02:00
|
|
|
private ResourceLocation lastEffect;
|
|
|
|
private boolean powderListDirty;
|
2018-12-14 00:47:01 +01:00
|
|
|
|
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);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
public void onAddedToWorld() {
|
|
|
|
super.onAddedToWorld();
|
2021-04-03 23:16:18 +02:00
|
|
|
this.powderListDirty = true;
|
2019-02-21 12:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
public void onRemovedFromWorld() {
|
|
|
|
super.onRemovedFromWorld();
|
2021-04-03 23:16:18 +02:00
|
|
|
this.setInhibitedEffect(null);
|
|
|
|
this.updatePowderListStatus();
|
2019-02-21 12:27:54 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 23:02:39 +01:00
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
protected void defineSynchedData() {
|
|
|
|
this.entityData.define(INHIBITED_EFFECT, null);
|
|
|
|
this.entityData.define(COLOR, 0);
|
|
|
|
this.entityData.define(AMOUNT, 0);
|
2020-01-21 23:02:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
|
|
|
|
super.onSyncedDataUpdated(key);
|
2021-04-03 23:16:18 +02:00
|
|
|
if (INHIBITED_EFFECT.equals(key) || AMOUNT.equals(key))
|
|
|
|
this.powderListDirty = true;
|
2019-02-21 12:27:54 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 23:16:18 +02:00
|
|
|
@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())
|
2021-04-03 23:16:18 +02:00
|
|
|
this.powderListDirty = true;
|
2021-12-06 14:38:12 +01:00
|
|
|
super.setPos(x, y, z);
|
2019-02-21 12:27:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-14 00:47:01 +01:00
|
|
|
@Override
|
2020-01-21 23:02:39 +01:00
|
|
|
public void tick() {
|
|
|
|
super.tick();
|
2021-04-03 23:16:18 +02:00
|
|
|
|
|
|
|
if (this.powderListDirty)
|
|
|
|
this.updatePowderListStatus();
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.isClientSide) {
|
|
|
|
if (this.level.getGameTime() % 5 == 0) {
|
2020-01-23 20:57:56 +01:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
2021-12-06 14:38:12 +01:00
|
|
|
this.getX() + this.level.random.nextGaussian() * 0.1F,
|
|
|
|
this.getY(),
|
|
|
|
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);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
2019-02-21 12:42:45 +01:00
|
|
|
this.renderTicks++;
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canBeCollidedWith() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
public Packet<?> getAddEntityPacket() {
|
2020-01-23 19:20:47 +01:00
|
|
|
return NetworkHooks.getEntitySpawningPacket(this);
|
2020-01-21 23:02:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-14 00:47:01 +01:00
|
|
|
@Override
|
2021-12-06 14:38:12 +01:00
|
|
|
public boolean hurt(DamageSource source, float amount) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (source instanceof EntityDamageSource && !this.level.isClientSide) {
|
2021-12-06 14:38:12 +01:00
|
|
|
this.kill();
|
|
|
|
this.spawnAtLocation(this.getDrop(), 0F);
|
2018-12-14 00:47:01 +01:00
|
|
|
return true;
|
|
|
|
} else
|
2021-12-06 14:38:12 +01:00
|
|
|
return super.hurt(source, amount);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-12-06 14:38:12 +01:00
|
|
|
var effect = this.entityData.get(INHIBITED_EFFECT);
|
2020-02-07 15:22:30 +01:00
|
|
|
if (effect == null || effect.isEmpty())
|
|
|
|
return null;
|
|
|
|
return new ResourceLocation(effect);
|
|
|
|
}
|
|
|
|
|
2018-12-14 00:47:01 +01:00
|
|
|
public void setInhibitedEffect(ResourceLocation effect) {
|
2021-12-06 14:38:12 +01:00
|
|
|
this.entityData.set(INHIBITED_EFFECT, effect != null ? effect.toString() : null);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
public int getColor() {
|
2021-12-06 14:38:12 +01:00
|
|
|
return this.entityData.get(COLOR);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setColor(int color) {
|
2021-12-06 14:38:12 +01:00
|
|
|
this.entityData.set(COLOR, color);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
public int getAmount() {
|
2021-12-06 14:38:12 +01:00
|
|
|
return this.entityData.get(AMOUNT);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
2019-01-28 15:43:21 +01:00
|
|
|
|
|
|
|
public void setAmount(int amount) {
|
2021-12-06 14:38:12 +01:00
|
|
|
this.entityData.set(AMOUNT, amount);
|
2019-01-28 15:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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());
|
2019-01-28 15:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2019-01-28 15:43:21 +01:00
|
|
|
return this.getColor();
|
|
|
|
}
|
2021-04-03 23:16:18 +02:00
|
|
|
|
|
|
|
private void updatePowderListStatus() {
|
2021-12-06 14:38:12 +01:00
|
|
|
var powders = ((LevelData) ILevelData.getLevelData(this.level)).effectPowders;
|
2021-04-03 23:16:18 +02:00
|
|
|
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()));
|
2021-04-03 23:16:18 +02:00
|
|
|
}
|
2021-12-06 14:38:12 +01:00
|
|
|
var effect = this.getInhibitedEffect();
|
2021-04-03 23:16:18 +02:00
|
|
|
if (effect != null) {
|
2021-12-06 14:38:12 +01:00
|
|
|
var newList = powders.get(effect);
|
|
|
|
newList.add(new Tuple<>(this.getEyePosition(), this.getAmount()));
|
2021-04-03 23:16:18 +02:00
|
|
|
}
|
|
|
|
this.powderListDirty = false;
|
|
|
|
this.lastEffect = effect;
|
|
|
|
}
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|