diff --git a/build.gradle b/build.gradle index e1c581324..409a3d12e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'idea' } -version = "1.9.4-beta1" +version = "1.9.4-beta2.2" group= "kamkeel.customnpc-plus" archivesBaseName = "CustomNPC-Plus" diff --git a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java index e9afafd53..df59cb7ee 100644 --- a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java +++ b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java @@ -23,6 +23,7 @@ import net.minecraftforge.event.entity.player.*; import net.minecraftforge.event.world.BlockEvent; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.constants.EnumQuestType; +import noppes.npcs.controllers.CustomEffectController; import noppes.npcs.controllers.PartyController; import noppes.npcs.controllers.PlayerDataController; import noppes.npcs.controllers.ScriptController; @@ -71,6 +72,13 @@ public class ScriptPlayerEventHandler { playerData.timers.update(); } + // TODO: Fix Config + // player.ticksExisted % ConfigDBCGameplay.CheckEffectsTick == 0 + if (player.ticksExisted % 10 == 0) + CustomEffectController.Instance.runEffects(player); + + if (player.ticksExisted % 20 == 0) + CustomEffectController.Instance.decrementEffects(player); Party party = playerData.getPlayerParty(); boolean trackingPartyQuest = playerData.questData.getTrackedQuest() != null && party != null && party.getQuest() != null && party.getQuest().getId() == playerData.questData.getTrackedQuest().getId(); @@ -422,6 +430,8 @@ public class ScriptPlayerEventHandler { PlayerDataScript handler = ScriptController.Instance.playerScripts; if(event.entityLiving instanceof EntityPlayer) { try { + CustomEffectController.getInstance().killEffects((EntityPlayer) event.entityLiving); + IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityLiving); EventHooks.onPlayerDeath(handler,scriptPlayer, event.source, source); } catch (Exception ignored) {} diff --git a/src/main/java/noppes/npcs/controllers/CustomEffectController.java b/src/main/java/noppes/npcs/controllers/CustomEffectController.java index 045bace4b..f68b73a52 100644 --- a/src/main/java/noppes/npcs/controllers/CustomEffectController.java +++ b/src/main/java/noppes/npcs/controllers/CustomEffectController.java @@ -92,14 +92,6 @@ public class CustomEffectController implements ICustomEffectHandler { } } - public boolean has(String name) { - return get(name) != null; - } - - public boolean has(int id) { - return get(id) != null; - } - @Override public ICustomEffect createEffect(String name) { if (has(name)) @@ -176,17 +168,47 @@ public class CustomEffectController implements ICustomEffectHandler { return lastUsedID; } - public CustomEffect get(String name) { - for (CustomEffect effect : customEffects.values()) { - if (effect.getName().equalsIgnoreCase(name)) { - return effect; + + public CustomEffect get(int id, int index) { + HashMap effectMap = indexMapper.get(index); + return effectMap != null ? effectMap.get(id) : null; + } + + public CustomEffect get(int id) { + return get(id, 0); + } + + public boolean has(int id, int index) { + HashMap effectMap = indexMapper.get(index); + return effectMap != null && effectMap.containsKey(id); + } + + public boolean has(int id) { + return has(id, 0); + } + + public CustomEffect get(String name, int index) { + HashMap effectMap = indexMapper.get(index); + if (effectMap != null) { + for (CustomEffect effect : effectMap.values()) { + if (effect.getName().equalsIgnoreCase(name)) { + return effect; + } } } return null; } - public CustomEffect get(int id) { - return customEffects.get(id); + public CustomEffect get(String name) { + return get(name, 0); + } + + public boolean has(String name, int index) { + return get(name, index) != null; + } + + public boolean has(String name) { + return has(name, 0); } public Map getPlayerEffects(EntityPlayer player) { diff --git a/src/main/java/noppes/npcs/controllers/data/PlayerData.java b/src/main/java/noppes/npcs/controllers/data/PlayerData.java index 3852fffea..92e32e654 100644 --- a/src/main/java/noppes/npcs/controllers/data/PlayerData.java +++ b/src/main/java/noppes/npcs/controllers/data/PlayerData.java @@ -43,7 +43,7 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData { public PlayerItemGiverData itemgiverData = new PlayerItemGiverData(this); public PlayerMailData mailData = new PlayerMailData(this); public AnimationData animationData = new AnimationData(this); - + public PlayerEffectData effectData = new PlayerEffectData(this); public DataTimers timers = new DataTimers(this); public DataSkinOverlays skinOverlays = new DataSkinOverlays(this); @@ -96,6 +96,7 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData { timers.readFromNBT(data); skinOverlays.readFromNBT(data); animationData.readFromNBT(data); + effectData.readFromNBT(data); if(player != null){ playername = player.getCommandSenderName(); @@ -135,6 +136,7 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData { timers.writeToNBT(compound); skinOverlays.writeToNBT(compound); animationData.writeToNBT(compound); + effectData.writeToNBT(compound); compound.setString("PlayerName", playername); compound.setString("UUID", uuid); @@ -164,6 +166,16 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData { factionData.loadNBTData(data); } + public NBTTagCompound getPlayerEffects(){ + NBTTagCompound compound = new NBTTagCompound(); + effectData.writeToNBT(compound); + return compound; + } + + public void setPlayerEffects(NBTTagCompound data){ + effectData.readFromNBT(data); + } + public NBTTagCompound getSyncNBTFull() { if(player != null){ playername = player.getCommandSenderName(); diff --git a/src/main/java/noppes/npcs/controllers/data/PlayerEffectData.java b/src/main/java/noppes/npcs/controllers/data/PlayerEffectData.java new file mode 100644 index 000000000..7cde61002 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/PlayerEffectData.java @@ -0,0 +1,87 @@ +package noppes.npcs.controllers.data; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class PlayerEffectData { + private final PlayerData parent; + private ConcurrentHashMap effects = new ConcurrentHashMap<>(); + + public PlayerEffectData(PlayerData playerData){ + this.parent = playerData; + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + NBTTagList list = new NBTTagList(); + for (Map.Entry entry : effects.entrySet()) { + NBTTagCompound effectCompound = new NBTTagCompound(); + effectCompound.setInteger("id", entry.getKey().getId()); + effectCompound.setInteger("index", entry.getKey().getIndex()); + effectCompound.setInteger("duration", entry.getValue().duration); + effectCompound.setByte("level", entry.getValue().level); + list.appendTag(effectCompound); + } + compound.setTag("Effects", list); + return compound; + } + + public void readFromNBT(NBTTagCompound compound) { + HashSet newKeys = new HashSet<>(); + if (compound.hasKey("Effects")) { + NBTTagList list = compound.getTagList("Effects", 10); + for (int i = 0; i < list.tagCount(); i++) { + NBTTagCompound effectCompound = list.getCompoundTagAt(i); + int id = effectCompound.getInteger("id"); + int index = effectCompound.getInteger("index"); + int duration = effectCompound.getInteger("duration"); + byte level = effectCompound.getByte("level"); + EffectKey key = new EffectKey(id, index); + newKeys.add(key); + PlayerEffect effect = effects.get(key); + if (effect != null) { + effect.duration = duration; + effect.level = level; + } else { + effect = new PlayerEffect(id, duration, level); + effect.index = index; + effects.put(key, effect); + } + } + } + Iterator it = effects.keySet().iterator(); + while (it.hasNext()) { + EffectKey key = it.next(); + if (!newKeys.contains(key)) { + it.remove(); + } + } + } + + public ConcurrentHashMap getEffects() { + return effects; + } + + public void setEffects(ConcurrentHashMap effects) { + this.effects = effects; + } + + public PlayerEffect getPlayerEffect(int id, int index) { + return effects.get(new EffectKey(id, index)); + } + + public PlayerEffect getPlayerEffect(int id) { + return getPlayerEffect(id, 0); + } + + public boolean hasPlayerEffect(int id, int index) { + return effects.containsKey(new EffectKey(id, index)); + } + + public boolean hasPlayerEffect(int id) { + return hasPlayerEffect(id, 0); + } +}