2018-09-21 17:53:33 +02:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
2019-02-05 13:44:35 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
import com.minelittlepony.unicopia.Predicates;
|
2019-02-12 15:33:51 +01:00
|
|
|
import com.minelittlepony.unicopia.Race;
|
2019-02-17 00:08:19 +01:00
|
|
|
import com.minelittlepony.unicopia.init.UItems;
|
2018-09-24 21:37:16 +02:00
|
|
|
import com.minelittlepony.unicopia.item.ICastable;
|
2018-09-21 17:53:33 +02:00
|
|
|
import com.minelittlepony.unicopia.network.EffectSync;
|
|
|
|
import com.minelittlepony.unicopia.spell.ICaster;
|
|
|
|
import com.minelittlepony.unicopia.spell.IMagicEffect;
|
2019-01-22 17:39:30 +01:00
|
|
|
import com.minelittlepony.unicopia.spell.SpellAffinity;
|
2018-09-21 17:53:33 +02:00
|
|
|
import com.minelittlepony.unicopia.spell.SpellRegistry;
|
|
|
|
|
|
|
|
import net.minecraft.block.SoundType;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.Entity;
|
2019-02-14 21:59:11 +01:00
|
|
|
import net.minecraft.entity.EntityCreature;
|
2018-09-21 17:53:33 +02:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.SoundEvents;
|
2019-02-04 21:09:36 +01:00
|
|
|
import net.minecraft.item.Item;
|
2018-09-21 17:53:33 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.datasync.DataParameter;
|
|
|
|
import net.minecraft.network.datasync.DataSerializers;
|
|
|
|
import net.minecraft.network.datasync.EntityDataManager;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.DamageSource;
|
|
|
|
import net.minecraft.util.EnumActionResult;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.SoundCategory;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2019-02-14 21:59:11 +01:00
|
|
|
public class EntitySpell extends EntityCreature implements IMagicals, ICaster<EntityLivingBase>, IInAnimate {
|
2018-09-21 17:53:33 +02:00
|
|
|
|
|
|
|
private EntityLivingBase owner = null;
|
|
|
|
|
|
|
|
public float hoverStart;
|
|
|
|
|
|
|
|
private static final DataParameter<Integer> LEVEL = EntityDataManager
|
|
|
|
.createKey(EntitySpell.class, DataSerializers.VARINT);
|
|
|
|
|
|
|
|
private static final DataParameter<String> OWNER = EntityDataManager
|
|
|
|
.createKey(EntitySpell.class, DataSerializers.STRING);
|
|
|
|
|
|
|
|
private static final DataParameter<NBTTagCompound> EFFECT = EntityDataManager
|
|
|
|
.createKey(EntitySpell.class, DataSerializers.COMPOUND_TAG);
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
private static final DataParameter<Integer> AFFINITY = EntityDataManager
|
|
|
|
.createKey(EntitySpell.class, DataSerializers.VARINT);
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
private final EffectSync<EntityLivingBase> effectDelegate = new EffectSync<>(this, EFFECT);
|
|
|
|
|
|
|
|
public EntitySpell(World w) {
|
|
|
|
super(w);
|
|
|
|
setSize(0.6f, 0.25f);
|
|
|
|
hoverStart = (float)(Math.random() * Math.PI * 2.0D);
|
|
|
|
setRenderDistanceWeight(getRenderDistanceWeight() + 1);
|
|
|
|
preventEntitySpawning = false;
|
|
|
|
enablePersistence();
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2018-09-21 17:53:33 +02:00
|
|
|
public boolean isInRangeToRenderDist(double distance) {
|
2018-09-26 17:53:12 +02:00
|
|
|
if (getCurrentLevel() > 0) {
|
|
|
|
distance /= getCurrentLevel();
|
|
|
|
}
|
2018-09-21 17:53:33 +02:00
|
|
|
return super.isInRangeToRenderDist(distance);
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
@Override
|
|
|
|
public SpellAffinity getAffinity() {
|
|
|
|
return SpellAffinity.values()[dataManager.get(AFFINITY)];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAffinity(SpellAffinity affinity) {
|
|
|
|
dataManager.set(AFFINITY, affinity.ordinal());
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2019-02-05 13:44:35 +01:00
|
|
|
public void setEffect(@Nullable IMagicEffect effect) {
|
2018-09-21 17:53:33 +02:00
|
|
|
effectDelegate.set(effect);
|
2019-01-26 22:20:35 +01:00
|
|
|
|
2019-02-05 13:44:35 +01:00
|
|
|
if (effect != null) {
|
|
|
|
effect.onPlaced(this);
|
|
|
|
}
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:33:51 +01:00
|
|
|
@Override
|
|
|
|
public boolean canInteract(Race race) {
|
|
|
|
return race.canCast();
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:44:35 +01:00
|
|
|
@Nullable
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2019-02-13 21:43:08 +01:00
|
|
|
public <T extends IMagicEffect> T getEffect(@Nullable Class<T> type, boolean update) {
|
|
|
|
return effectDelegate.get(type, update);
|
|
|
|
}
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2019-02-06 11:33:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasEffect() {
|
|
|
|
return effectDelegate.has();
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
@Override
|
|
|
|
protected void entityInit() {
|
|
|
|
super.entityInit();
|
|
|
|
dataManager.register(LEVEL, 0);
|
|
|
|
dataManager.register(EFFECT, new NBTTagCompound());
|
|
|
|
dataManager.register(OWNER, "");
|
2019-01-22 17:39:30 +01:00
|
|
|
dataManager.register(AFFINITY, SpellAffinity.NEUTRAL.ordinal());
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack onPlayerMiddleClick(EntityPlayer player) {
|
2019-02-04 21:09:36 +01:00
|
|
|
ItemStack stack = new ItemStack(getItem(), 1);
|
2018-09-21 17:53:33 +02:00
|
|
|
SpellRegistry.instance().enchantStack(stack, getEffect().getName());
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:09:36 +01:00
|
|
|
protected Item getItem() {
|
|
|
|
return getAffinity() == SpellAffinity.BAD ? UItems.curse : UItems.spell;
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
@Override
|
|
|
|
protected boolean canTriggerWalking() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPushedByWater() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canRenderOnFire() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOwner(EntityLivingBase owner) {
|
|
|
|
this.owner = owner;
|
|
|
|
setOwner(owner.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setOwner(String ownerName) {
|
2018-09-24 21:37:16 +02:00
|
|
|
if (!StringUtils.isEmpty(ownerName)) {
|
2018-09-21 17:53:33 +02:00
|
|
|
dataManager.set(OWNER, ownerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getOwnerName() {
|
|
|
|
String ownerName = dataManager.get(OWNER);
|
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
if (!StringUtils.isEmpty(ownerName)) {
|
2018-09-21 17:53:33 +02:00
|
|
|
if (owner instanceof EntityPlayer) {
|
|
|
|
return owner.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ownerName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EntityLivingBase getOwner() {
|
|
|
|
if (owner == null) {
|
|
|
|
String ownerName = dataManager.get(OWNER);
|
|
|
|
if (ownerName != null && ownerName.length() > 0) {
|
|
|
|
owner = world.getPlayerEntityByName(ownerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void displayTick() {
|
|
|
|
if (hasEffect()) {
|
2019-02-06 09:31:31 +01:00
|
|
|
getEffect().render(this);
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdate() {
|
|
|
|
if (world.isRemote) {
|
|
|
|
displayTick();
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:44:35 +01:00
|
|
|
if (!hasEffect()) {
|
2018-09-21 17:53:33 +02:00
|
|
|
setDead();
|
|
|
|
} else {
|
|
|
|
if (getEffect().getDead()) {
|
|
|
|
setDead();
|
|
|
|
onDeath();
|
|
|
|
} else {
|
2019-02-06 09:31:31 +01:00
|
|
|
getEffect().update(this);
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getEffect().allowAI()) {
|
|
|
|
super.onUpdate();
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:53:12 +02:00
|
|
|
|
|
|
|
if (overLevelCap()) {
|
|
|
|
if (world.rand.nextInt(10) == 0) {
|
|
|
|
spawnExplosionParticle();
|
|
|
|
}
|
2019-02-06 09:31:31 +01:00
|
|
|
|
|
|
|
if (!world.isRemote && hasEffect()) {
|
|
|
|
float exhaustionChance = getEffect().getExhaustion(this);
|
|
|
|
|
|
|
|
if (exhaustionChance == 0 || world.rand.nextInt((int)(exhaustionChance / 500)) == 0) {
|
|
|
|
addLevels(-1);
|
|
|
|
} else if (world.rand.nextInt((int)(exhaustionChance * 500)) == 0) {
|
|
|
|
setEffect(null);
|
|
|
|
} else if (world.rand.nextInt((int)(exhaustionChance * 3500)) == 0) {
|
|
|
|
world.createExplosion(this, posX, posY, posZ, getCurrentLevel()/2, true);
|
|
|
|
setDead();
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:53:12 +02:00
|
|
|
}
|
2019-02-06 09:31:31 +01:00
|
|
|
|
|
|
|
if (getCurrentLevel() < 0) {
|
|
|
|
setDead();
|
|
|
|
}
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fall(float distance, float damageMultiplier) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:31:31 +01:00
|
|
|
public boolean overLevelCap() {
|
|
|
|
return getCurrentLevel() > getMaxLevel();
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
@Override
|
|
|
|
protected void updateFallState(double y, boolean onGround, IBlockState state, BlockPos pos) {
|
|
|
|
this.onGround = true;
|
|
|
|
//super.updateFallState(y, onGround = this.onGround = true, state, pos);
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2018-09-21 17:53:33 +02:00
|
|
|
public boolean attackEntityFrom(DamageSource source, float amount) {
|
|
|
|
if (!world.isRemote) {
|
|
|
|
setDead();
|
|
|
|
onDeath();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onDeath() {
|
|
|
|
SoundType sound = SoundType.STONE;
|
|
|
|
|
|
|
|
world.playSound(posX, posY, posZ, sound.getBreakSound(), SoundCategory.NEUTRAL, sound.getVolume(), sound.getPitch(), true);
|
|
|
|
|
|
|
|
if (world.getGameRules().getBoolean("doTileDrops")) {
|
2018-09-24 21:37:16 +02:00
|
|
|
int level = getCurrentLevel();
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2019-02-04 21:09:36 +01:00
|
|
|
ItemStack stack = new ItemStack(getItem(), level + 1);
|
2018-09-21 17:53:33 +02:00
|
|
|
if (hasEffect()) {
|
|
|
|
SpellRegistry.instance().enchantStack(stack, getEffect().getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
entityDropItem(stack, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2018-09-21 17:53:33 +02:00
|
|
|
public void setDead() {
|
|
|
|
if (hasEffect()) {
|
|
|
|
getEffect().setDead();
|
|
|
|
}
|
|
|
|
super.setDead();
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:53:12 +02:00
|
|
|
@Override
|
2018-09-21 17:53:33 +02:00
|
|
|
public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand) {
|
|
|
|
if (Predicates.MAGI.test(player)) {
|
|
|
|
ItemStack currentItem = player.getHeldItem(EnumHand.MAIN_HAND);
|
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
if (currentItem != null
|
|
|
|
&& currentItem.getItem() instanceof ICastable
|
|
|
|
&& ((ICastable)currentItem.getItem()).canFeed(this, currentItem)
|
|
|
|
&& tryLevelUp(currentItem)) {
|
2018-09-21 17:53:33 +02:00
|
|
|
|
|
|
|
if (!player.capabilities.isCreativeMode) {
|
|
|
|
currentItem.shrink(1);
|
|
|
|
|
|
|
|
if (currentItem.isEmpty()) {
|
|
|
|
player.renderBrokenItemStack(currentItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
public boolean tryLevelUp(ItemStack stack) {
|
2019-02-05 13:44:35 +01:00
|
|
|
if (hasEffect() && SpellRegistry.stackHasEnchantment(stack)) {
|
|
|
|
if (!getEffect().getName().contentEquals(SpellRegistry.getKeyFromStack(stack))) {
|
2018-09-24 21:37:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
addLevels(1);
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
playSound(SoundEvents.ENTITY_ZOMBIE_VILLAGER_CURE, 0.1f, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxLevel() {
|
2019-02-06 09:31:31 +01:00
|
|
|
return hasEffect() ? getEffect().getMaxLevelCutOff(this) : 0;
|
2018-09-21 17:53:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:37:16 +02:00
|
|
|
@Override
|
|
|
|
public int getCurrentLevel() {
|
|
|
|
return dataManager.get(LEVEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setCurrentLevel(int level) {
|
2019-02-21 18:18:38 +01:00
|
|
|
dataManager.set(LEVEL, Math.max(level, 0));
|
2018-09-24 21:37:16 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
@Override
|
|
|
|
public Entity getEntity() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readEntityFromNBT(NBTTagCompound compound) {
|
|
|
|
super.readEntityFromNBT(compound);
|
2019-02-17 18:21:22 +01:00
|
|
|
if (compound.hasKey("affinity")) {
|
|
|
|
setAffinity(SpellAffinity.of(compound.getString("affinity")));
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
setOwner(compound.getString("ownerName"));
|
2018-09-24 21:37:16 +02:00
|
|
|
setCurrentLevel(compound.getInteger("level"));
|
2018-09-21 17:53:33 +02:00
|
|
|
|
|
|
|
if (compound.hasKey("effect")) {
|
|
|
|
setEffect(SpellRegistry.instance().createEffectFromNBT(compound.getCompoundTag("effect")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeEntityToNBT(NBTTagCompound compound) {
|
|
|
|
super.writeEntityToNBT(compound);
|
|
|
|
|
2019-02-17 18:21:22 +01:00
|
|
|
compound.setString("affinity", getAffinity().name());
|
2018-09-21 17:53:33 +02:00
|
|
|
compound.setString("ownerName", getOwnerName());
|
2018-09-24 21:37:16 +02:00
|
|
|
compound.setInteger("level", getCurrentLevel());
|
2018-09-21 17:53:33 +02:00
|
|
|
|
|
|
|
if (hasEffect()) {
|
|
|
|
compound.setTag("effect", SpellRegistry.instance().serializeEffectToNBT(getEffect()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|