2018-12-14 00:47:01 +01:00
|
|
|
package de.ellpeck.naturesaura.entities;
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
import com.google.common.collect.ListMultimap;
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2019-02-22 19:06:47 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2019-02-21 12:27:54 +01:00
|
|
|
import de.ellpeck.naturesaura.api.misc.IWorldData;
|
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;
|
2019-02-21 12:27:54 +01:00
|
|
|
import de.ellpeck.naturesaura.misc.WorldData;
|
2018-12-14 00:47:01 +01:00
|
|
|
import net.minecraft.entity.Entity;
|
2020-01-21 23:02:39 +01:00
|
|
|
import net.minecraft.entity.EntityType;
|
2018-12-14 00:47:01 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2020-01-21 23:02:39 +01:00
|
|
|
import net.minecraft.network.IPacket;
|
2018-12-14 00:47:01 +01:00
|
|
|
import net.minecraft.network.datasync.DataParameter;
|
|
|
|
import net.minecraft.network.datasync.DataSerializers;
|
|
|
|
import net.minecraft.network.datasync.EntityDataManager;
|
|
|
|
import net.minecraft.util.DamageSource;
|
|
|
|
import net.minecraft.util.EntityDamageSource;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2019-02-21 12:27:54 +01:00
|
|
|
import net.minecraft.util.Tuple;
|
2019-01-28 15:43:21 +01:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-02-21 12:27:54 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2018-12-14 00:47:01 +01:00
|
|
|
import net.minecraft.world.World;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2020-01-23 19:20:47 +01:00
|
|
|
import net.minecraftforge.fml.network.NetworkHooks;
|
2018-12-14 00:47:01 +01:00
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-01-28 15:43:21 +01:00
|
|
|
public class EntityEffectInhibitor extends Entity implements IVisualizable {
|
2018-12-14 00:47:01 +01:00
|
|
|
|
|
|
|
private static final DataParameter<String> INHIBITED_EFFECT = EntityDataManager.createKey(EntityEffectInhibitor.class, DataSerializers.STRING);
|
|
|
|
private static final DataParameter<Integer> COLOR = EntityDataManager.createKey(EntityEffectInhibitor.class, DataSerializers.VARINT);
|
2019-01-28 15:43:21 +01:00
|
|
|
private static final DataParameter<Integer> AMOUNT = EntityDataManager.createKey(EntityEffectInhibitor.class, DataSerializers.VARINT);
|
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;
|
|
|
|
|
2020-01-21 23:02:39 +01:00
|
|
|
public EntityEffectInhibitor(EntityType<?> entityTypeIn, World worldIn) {
|
|
|
|
super(entityTypeIn, worldIn);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:06:47 +01:00
|
|
|
public static void place(World world, ItemStack stack, double posX, double posY, double posZ) {
|
2020-01-26 01:41:49 +01:00
|
|
|
ResourceLocation effect = ItemEffectPowder.getEffect(stack);
|
2020-01-21 23:02:39 +01:00
|
|
|
EntityEffectInhibitor entity = new EntityEffectInhibitor(ModEntities.EFFECT_INHIBITOR, world);
|
2019-02-22 19:06:47 +01:00
|
|
|
entity.setInhibitedEffect(effect);
|
|
|
|
entity.setColor(NaturesAuraAPI.EFFECT_POWDERS.get(effect));
|
|
|
|
entity.setAmount(stack.getCount());
|
|
|
|
entity.setPosition(posX, posY, posZ);
|
2020-01-21 23:02:39 +01:00
|
|
|
world.addEntity(entity);
|
2019-02-22 19:06:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
@Override
|
|
|
|
public void onAddedToWorld() {
|
|
|
|
super.onAddedToWorld();
|
|
|
|
this.addToPowderList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemovedFromWorld() {
|
|
|
|
this.removeFromPowderList();
|
|
|
|
super.onRemovedFromWorld();
|
|
|
|
}
|
|
|
|
|
2020-01-21 23:02:39 +01:00
|
|
|
@Override
|
|
|
|
protected void registerData() {
|
|
|
|
this.dataManager.register(INHIBITED_EFFECT, null);
|
|
|
|
this.dataManager.register(COLOR, 0);
|
|
|
|
this.dataManager.register(AMOUNT, 0);
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
@Override
|
|
|
|
public void setPosition(double x, double y, double z) {
|
2020-01-28 18:08:56 +01:00
|
|
|
boolean should = x != this.getPosX() || y != this.getPosY() || z != this.getPosZ();
|
2019-02-21 12:27:54 +01:00
|
|
|
if (should)
|
|
|
|
this.removeFromPowderList();
|
|
|
|
super.setPosition(x, y, z);
|
|
|
|
if (should)
|
|
|
|
this.addToPowderList();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addToPowderList() {
|
2020-01-23 19:20:47 +01:00
|
|
|
if (!this.isAddedToWorld() || this.getInhibitedEffect() == null)
|
2019-02-21 12:27:54 +01:00
|
|
|
return;
|
|
|
|
List<Tuple<Vec3d, Integer>> powders = this.getPowderList();
|
|
|
|
powders.add(new Tuple<>(this.getPositionVector(), this.getAmount()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeFromPowderList() {
|
2020-01-23 19:20:47 +01:00
|
|
|
if (!this.isAddedToWorld() || this.getInhibitedEffect() == null)
|
2019-02-21 12:27:54 +01:00
|
|
|
return;
|
|
|
|
List<Tuple<Vec3d, Integer>> powders = this.getPowderList();
|
|
|
|
Vec3d pos = this.getPositionVector();
|
|
|
|
for (int i = 0; i < powders.size(); i++)
|
2020-01-21 23:02:39 +01:00
|
|
|
if (pos.equals(powders.get(i).getA())) {
|
2019-02-21 12:27:54 +01:00
|
|
|
powders.remove(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Tuple<Vec3d, Integer>> getPowderList() {
|
|
|
|
ListMultimap<ResourceLocation, Tuple<Vec3d, Integer>> powders = ((WorldData) IWorldData.getWorldData(this.world)).effectPowders;
|
|
|
|
return powders.get(this.getInhibitedEffect());
|
|
|
|
}
|
|
|
|
|
2018-12-14 00:47:01 +01:00
|
|
|
@Override
|
2020-01-21 23:02:39 +01:00
|
|
|
public void tick() {
|
|
|
|
super.tick();
|
2018-12-14 00:47:01 +01:00
|
|
|
if (this.world.isRemote) {
|
2020-01-21 23:02:39 +01:00
|
|
|
if (this.world.getGameTime() % 5 == 0) {
|
2020-01-23 20:57:56 +01:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
2020-01-28 18:08:56 +01:00
|
|
|
this.getPosX() + this.world.rand.nextGaussian() * 0.1F,
|
|
|
|
this.getPosY(),
|
|
|
|
this.getPosZ() + this.world.rand.nextGaussian() * 0.1F,
|
2018-12-14 00:47:01 +01:00
|
|
|
this.world.rand.nextGaussian() * 0.005F,
|
|
|
|
this.world.rand.nextFloat() * 0.03F,
|
|
|
|
this.world.rand.nextGaussian() * 0.005F,
|
|
|
|
this.getColor(), this.world.rand.nextFloat() * 3F + 1F, 120, 0F, true, true);
|
|
|
|
}
|
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
|
|
|
|
protected void readAdditional(CompoundNBT compound) {
|
|
|
|
this.setInhibitedEffect(new ResourceLocation(compound.getString("effect")));
|
|
|
|
this.setColor(compound.getInt("color"));
|
|
|
|
this.setAmount(compound.contains("amount") ? compound.getInt("amount") : 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void writeAdditional(CompoundNBT compound) {
|
|
|
|
compound.putString("effect", this.getInhibitedEffect().toString());
|
|
|
|
compound.putInt("color", this.getColor());
|
|
|
|
compound.putInt("amount", this.getAmount());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPacket<?> createSpawnPacket() {
|
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
|
|
|
|
public boolean attackEntityFrom(DamageSource source, float amount) {
|
|
|
|
if (source instanceof EntityDamageSource && !this.world.isRemote) {
|
2020-01-21 23:02:39 +01:00
|
|
|
this.remove();
|
2019-02-22 19:06:47 +01:00
|
|
|
this.entityDropItem(this.getDrop(), 0F);
|
2018-12-14 00:47:01 +01:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return super.attackEntityFrom(source, amount);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
String effect = this.dataManager.get(INHIBITED_EFFECT);
|
|
|
|
if (effect == null || effect.isEmpty())
|
|
|
|
return null;
|
|
|
|
return new ResourceLocation(effect);
|
|
|
|
}
|
|
|
|
|
2018-12-14 00:47:01 +01:00
|
|
|
public void setInhibitedEffect(ResourceLocation effect) {
|
2019-02-21 12:27:54 +01:00
|
|
|
this.removeFromPowderList();
|
2018-12-14 00:47:01 +01:00
|
|
|
this.dataManager.set(INHIBITED_EFFECT, effect.toString());
|
2019-02-21 12:27:54 +01:00
|
|
|
this.addToPowderList();
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
public int getColor() {
|
|
|
|
return this.dataManager.get(COLOR);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setColor(int color) {
|
|
|
|
this.dataManager.set(COLOR, color);
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
public int getAmount() {
|
|
|
|
return this.dataManager.get(AMOUNT);
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|
2019-01-28 15:43:21 +01:00
|
|
|
|
|
|
|
public void setAmount(int amount) {
|
2019-02-21 12:27:54 +01:00
|
|
|
this.removeFromPowderList();
|
2019-01-28 15:43:21 +01:00
|
|
|
this.dataManager.set(AMOUNT, amount);
|
2019-02-21 12:27:54 +01:00
|
|
|
this.addToPowderList();
|
2019-01-28 15:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-01-28 15:43:21 +01:00
|
|
|
public AxisAlignedBB getVisualizationBounds(World world, BlockPos pos) {
|
2019-02-21 12:27:54 +01:00
|
|
|
return Helper.aabb(this.getPositionVector()).grow(this.getAmount());
|
2019-01-28 15:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-01-28 15:43:21 +01:00
|
|
|
public int getVisualizationColor(World world, BlockPos pos) {
|
|
|
|
return this.getColor();
|
|
|
|
}
|
2018-12-14 00:47:01 +01:00
|
|
|
}
|