2016-05-30 18:20:31 -07:00
|
|
|
package WayofTime.bloodmagic.util.handler.event;
|
|
|
|
|
2016-06-12 12:42:32 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
2017-08-15 20:21:54 -07:00
|
|
|
import WayofTime.bloodmagic.core.RegistrarBloodMagic;
|
2016-05-30 18:20:31 -07:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.monster.EntityMob;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.potion.PotionEffect;
|
|
|
|
import net.minecraft.util.DamageSource;
|
2016-06-12 12:42:32 -04:00
|
|
|
import net.minecraft.util.EntityDamageSourceIndirect;
|
2016-05-30 18:20:31 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.ChunkPos;
|
|
|
|
import net.minecraft.world.EnumDifficulty;
|
2016-06-12 12:42:32 -04:00
|
|
|
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
2016-05-30 18:20:31 -07:00
|
|
|
import net.minecraftforge.event.entity.living.LivingDropsEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
|
|
|
|
import net.minecraftforge.event.world.ChunkDataEvent;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.Event;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
2016-06-12 12:42:32 -04:00
|
|
|
import WayofTime.bloodmagic.annot.Handler;
|
|
|
|
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
|
|
|
|
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
|
|
|
|
import WayofTime.bloodmagic.api.soul.IDemonWill;
|
|
|
|
import WayofTime.bloodmagic.api.soul.IDemonWillWeapon;
|
|
|
|
import WayofTime.bloodmagic.api.soul.PlayerDemonWillHandler;
|
|
|
|
import WayofTime.bloodmagic.demonAura.PosXY;
|
|
|
|
import WayofTime.bloodmagic.demonAura.WillChunk;
|
|
|
|
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
|
|
|
|
import WayofTime.bloodmagic.entity.projectile.EntitySentientArrow;
|
2017-08-15 18:14:28 -07:00
|
|
|
import WayofTime.bloodmagic.core.RegistrarBloodMagicItems;
|
2016-05-30 18:20:31 -07:00
|
|
|
|
|
|
|
@Handler
|
2016-06-12 12:42:32 -04:00
|
|
|
public class WillHandler
|
|
|
|
{
|
2016-05-30 18:20:31 -07:00
|
|
|
|
|
|
|
private final HashMap<Integer, Integer> serverTicks = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
// Adds Will to player
|
|
|
|
@SubscribeEvent
|
2016-06-12 12:42:32 -04:00
|
|
|
public void onItemPickup(EntityItemPickupEvent event)
|
|
|
|
{
|
2017-08-14 20:53:42 -07:00
|
|
|
ItemStack stack = event.getItem().getItem();
|
2017-02-19 16:06:29 -08:00
|
|
|
if (stack.getItem() instanceof IDemonWill)
|
2016-06-12 12:42:32 -04:00
|
|
|
{
|
2016-05-30 18:20:31 -07:00
|
|
|
EntityPlayer player = event.getEntityPlayer();
|
2016-11-02 12:16:24 -04:00
|
|
|
EnumDemonWillType pickupType = ((IDemonWill) stack.getItem()).getType(stack);
|
2016-05-30 18:20:31 -07:00
|
|
|
ItemStack remainder = PlayerDemonWillHandler.addDemonWill(player, stack);
|
|
|
|
|
2016-11-02 12:16:24 -04:00
|
|
|
if (remainder == null || ((IDemonWill) stack.getItem()).getWill(pickupType, stack) < 0.0001 || PlayerDemonWillHandler.isDemonWillFull(pickupType, player))
|
2016-06-12 12:42:32 -04:00
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
stack.setCount(0);
|
2016-05-30 18:20:31 -07:00
|
|
|
event.setResult(Event.Result.ALLOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2016-06-12 12:42:32 -04:00
|
|
|
public void onEntityAttacked(LivingDeathEvent event)
|
2016-05-30 18:20:31 -07:00
|
|
|
{
|
2016-06-12 12:42:32 -04:00
|
|
|
if (event.getSource() instanceof EntityDamageSourceIndirect)
|
|
|
|
{
|
2017-08-14 20:53:42 -07:00
|
|
|
Entity sourceEntity = event.getSource().getImmediateSource();
|
2016-05-30 18:20:31 -07:00
|
|
|
|
2016-06-12 12:42:32 -04:00
|
|
|
if (sourceEntity instanceof EntitySentientArrow)
|
|
|
|
{
|
|
|
|
((EntitySentientArrow) sourceEntity).reimbursePlayer(event.getEntityLiving(), event.getEntityLiving().getMaxHealth());
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 18:20:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add/Drop Demon Will for Player
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onLivingDrops(LivingDropsEvent event)
|
|
|
|
{
|
|
|
|
EntityLivingBase attackedEntity = event.getEntityLiving();
|
|
|
|
DamageSource source = event.getSource();
|
2017-08-14 20:53:42 -07:00
|
|
|
Entity entity = source.getTrueSource();
|
2016-05-30 18:20:31 -07:00
|
|
|
|
2017-08-15 20:21:54 -07:00
|
|
|
if (attackedEntity.isPotionActive(RegistrarBloodMagic.SOUL_SNARE) && (attackedEntity instanceof EntityMob || attackedEntity.getEntityWorld().getDifficulty() == EnumDifficulty.PEACEFUL))
|
2016-05-30 18:20:31 -07:00
|
|
|
{
|
2017-08-15 20:21:54 -07:00
|
|
|
PotionEffect eff = attackedEntity.getActivePotionEffect(RegistrarBloodMagic.SOUL_SNARE);
|
2016-05-30 18:20:31 -07:00
|
|
|
int lvl = eff.getAmplifier();
|
|
|
|
|
|
|
|
double amountOfSouls = attackedEntity.getEntityWorld().rand.nextDouble() * (lvl + 1) * (lvl + 1) * 5;
|
2017-08-14 20:53:42 -07:00
|
|
|
ItemStack soulStack = ((IDemonWill) RegistrarBloodMagicItems.MONSTER_SOUL).createWill(0, amountOfSouls);
|
2016-12-12 19:56:36 -08:00
|
|
|
event.getDrops().add(new EntityItem(attackedEntity.getEntityWorld(), attackedEntity.posX, attackedEntity.posY, attackedEntity.posZ, soulStack));
|
2016-05-30 18:20:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entity != null && entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
EntityPlayer player = (EntityPlayer) entity;
|
|
|
|
ItemStack heldStack = player.getHeldItemMainhand();
|
2017-02-19 16:06:29 -08:00
|
|
|
if (heldStack.getItem() instanceof IDemonWillWeapon && !player.getEntityWorld().isRemote)
|
2016-05-30 18:20:31 -07:00
|
|
|
{
|
|
|
|
IDemonWillWeapon demonWillWeapon = (IDemonWillWeapon) heldStack.getItem();
|
|
|
|
List<ItemStack> droppedSouls = demonWillWeapon.getRandomDemonWillDrop(attackedEntity, player, heldStack, event.getLootingLevel());
|
|
|
|
if (!droppedSouls.isEmpty())
|
|
|
|
{
|
|
|
|
ItemStack remainder;
|
|
|
|
for (ItemStack willStack : droppedSouls)
|
|
|
|
{
|
|
|
|
remainder = PlayerDemonWillHandler.addDemonWill(player, willStack);
|
2016-11-02 12:16:24 -04:00
|
|
|
|
2017-03-11 15:53:43 -08:00
|
|
|
if (!remainder.isEmpty())
|
2016-11-02 12:16:24 -04:00
|
|
|
{
|
|
|
|
EnumDemonWillType pickupType = ((IDemonWill) remainder.getItem()).getType(remainder);
|
|
|
|
if (((IDemonWill) remainder.getItem()).getWill(pickupType, remainder) >= 0.0001)
|
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
event.getDrops().add(new EntityItem(attackedEntity.getEntityWorld(), attackedEntity.posX, attackedEntity.posY, attackedEntity.posZ, remainder));
|
2016-11-02 12:16:24 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-30 18:20:31 -07:00
|
|
|
}
|
|
|
|
player.inventoryContainer.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onServerWorldTick(TickEvent.WorldTickEvent event)
|
|
|
|
{
|
|
|
|
if (event.world.isRemote)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int dim = event.world.provider.getDimension();
|
|
|
|
if (event.phase == TickEvent.Phase.END)
|
|
|
|
{
|
|
|
|
if (!this.serverTicks.containsKey(dim))
|
|
|
|
this.serverTicks.put(dim, 0);
|
|
|
|
|
|
|
|
int ticks = (this.serverTicks.get(dim));
|
|
|
|
|
|
|
|
if (ticks % 20 == 0)
|
|
|
|
{
|
|
|
|
CopyOnWriteArrayList<PosXY> dirtyChunks = WorldDemonWillHandler.dirtyChunks.get(dim);
|
|
|
|
if ((dirtyChunks != null) && (dirtyChunks.size() > 0))
|
|
|
|
{
|
|
|
|
for (PosXY pos : dirtyChunks)
|
|
|
|
event.world.markChunkDirty(new BlockPos(pos.x * 16, 5, pos.y * 16), null);
|
|
|
|
|
|
|
|
dirtyChunks.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.serverTicks.put(dim, ticks + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void chunkSave(ChunkDataEvent.Save event)
|
|
|
|
{
|
|
|
|
int dim = event.getWorld().provider.getDimension();
|
2017-08-14 20:53:42 -07:00
|
|
|
ChunkPos loc = event.getChunk().getPos();
|
2016-05-30 18:20:31 -07:00
|
|
|
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
event.getData().setTag("BloodMagic", nbt);
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
WillChunk ac = WorldDemonWillHandler.getWillChunk(dim, loc.x, loc.z);
|
2016-05-30 18:20:31 -07:00
|
|
|
if (ac != null)
|
|
|
|
{
|
|
|
|
nbt.setShort("base", ac.getBase());
|
|
|
|
ac.getCurrentWill().writeToNBT(nbt, "current");
|
|
|
|
if (!event.getChunk().isLoaded())
|
2017-08-14 20:53:42 -07:00
|
|
|
WorldDemonWillHandler.removeWillChunk(dim, loc.x, loc.z);
|
2016-05-30 18:20:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void chunkLoad(ChunkDataEvent.Load event)
|
|
|
|
{
|
|
|
|
int dim = event.getWorld().provider.getDimension();
|
|
|
|
if (event.getData().getCompoundTag("BloodMagic").hasKey("base"))
|
|
|
|
{
|
|
|
|
NBTTagCompound nbt = event.getData().getCompoundTag("BloodMagic");
|
|
|
|
short base = nbt.getShort("base");
|
|
|
|
DemonWillHolder current = new DemonWillHolder();
|
|
|
|
current.readFromNBT(nbt, "current");
|
|
|
|
WorldDemonWillHandler.addWillChunk(dim, event.getChunk(), base, current);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
WorldDemonWillHandler.generateWill(event.getChunk());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|