mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-19 03:49:10 +00:00
Custom Effect [WIP]
This commit is contained in:
1 parent
65ac349c08
commit
20a46e6007
31 files changed
+1331
-65
No files matched your search
@@ -7,7 +7,6 @@ public class MarkovDictionarySPA extends MarkovDictionary {
|
||||
|
||||
public MarkovDictionarySPA(String dictionary, int seqlen, Random rng) {
|
||||
super(dictionary, seqlen, rng);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -108,16 +108,16 @@ public class CommonProxy implements IGuiHandler {
|
||||
|
||||
@Override
|
||||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void openGui(EntityNPCInterface npc, EnumGuiType gui) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void openGui(EntityNPCInterface npc, EnumGuiType gui, int x, int y, int z) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -126,12 +126,12 @@ public class CommonProxy implements IGuiHandler {
|
||||
}
|
||||
|
||||
public void openGui(EntityPlayer player, Object guiscreen) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void spawnParticle(EntityLivingBase player, String string, Object... ob) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public class CommonProxy implements IGuiHandler {
|
||||
}
|
||||
|
||||
public void registerItem(Item item) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ public class DataInventory implements IInventory {
|
||||
public HashMap<Integer,ItemStack> weapons = new HashMap<Integer, ItemStack>();
|
||||
public HashMap<Integer,ItemStack> prevWeapons = new HashMap<Integer, ItemStack>();
|
||||
public HashMap<Integer,ItemStack> armor = new HashMap<Integer, ItemStack>();
|
||||
|
||||
|
||||
public int minExp = 0;
|
||||
public int maxExp = 0;
|
||||
|
||||
|
||||
public int lootMode = 0;
|
||||
|
||||
|
||||
private EntityNPCInterface npc;
|
||||
|
||||
|
||||
public DataInventory(EntityNPCInterface npc){
|
||||
this.npc = npc;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class DataInventory implements IInventory {
|
||||
nbttagcompound.setTag("Weapons", NBTTags.nbtItemStackList(getWeapons()));
|
||||
nbttagcompound.setTag("DoubleDropChance", NBTTags.nbtIntegerDoubleMap(dropchance));
|
||||
nbttagcompound.setInteger("LootMode", lootMode);
|
||||
|
||||
|
||||
return nbttagcompound;
|
||||
}
|
||||
public void readEntityFromNBT(NBTTagCompound nbttagcompound){
|
||||
@@ -187,7 +187,7 @@ public class DataInventory implements IInventory {
|
||||
npc.worldObj.spawnEntityInWorld(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public EntityItem getEntityItem(ItemStack itemstack) {
|
||||
if (itemstack == null) {
|
||||
return null;
|
||||
@@ -205,13 +205,13 @@ public class DataInventory implements IInventory {
|
||||
|
||||
return entityitem;
|
||||
}
|
||||
|
||||
|
||||
public ItemStack armorItemInSlot(int i) {
|
||||
return getArmor().get(i);
|
||||
}
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return 15;
|
||||
}
|
||||
@Override
|
||||
@@ -243,7 +243,7 @@ public class DataInventory implements IInventory {
|
||||
var3 = getArmor();
|
||||
i = 2;
|
||||
}
|
||||
|
||||
|
||||
ItemStack var4 = null;
|
||||
if (var3.get(par1) != null)
|
||||
{
|
||||
@@ -355,17 +355,17 @@ public class DataInventory implements IInventory {
|
||||
}
|
||||
@Override
|
||||
public void markDirty() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
public void openInventory() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
public void closeInventory() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -81,10 +81,10 @@ public class LogWriter {
|
||||
logger.setLevel(Level.ALL);
|
||||
info(new Date().toString());
|
||||
} catch (SecurityException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -223,4 +223,4 @@ class NPCStamp
|
||||
public Date makeDate = new Date();
|
||||
public Date recentDate = new Date();
|
||||
public int counter = 1;
|
||||
};
|
||||
};
|
||||
@@ -65,6 +65,14 @@ import static kamkeel.npcs.network.packets.data.large.ScrollGroupPacket.sendScro
|
||||
public class NoppesUtilServer {
|
||||
private static HashMap<String,Quest> editingQuests = new HashMap<String,Quest>();
|
||||
|
||||
public static IPlayer getIPlayer(EntityPlayer p) {
|
||||
return (IPlayer) NpcAPI.Instance().getIEntity(p);
|
||||
}
|
||||
|
||||
public static UUID getUUID(Entity entity) {
|
||||
return entity instanceof EntityPlayer ? ((EntityPlayer) entity).getGameProfile().getId() : entity.getUniqueID();
|
||||
}
|
||||
|
||||
public static void setEditingNpc(EntityPlayer player, EntityNPCInterface npc){
|
||||
PlayerData data = PlayerDataController.Instance.getPlayerData(player);
|
||||
data.editingNpc = npc;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class TranslateUtil {
|
||||
private static final String AudioUrl = "http://translate.google.com/translate_tts?q=%s&tl=%s";
|
||||
|
||||
public static String Translate(String text){
|
||||
|
||||
|
||||
try {
|
||||
String urlStr = String.format(TranslateUrl, URLEncoder.encode(text,"utf8"), "auto", "nl");
|
||||
URL url = new URL(urlStr);
|
||||
@@ -45,10 +45,10 @@ public class TranslateUtil {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
// public static void PlayAudio(String text){
|
||||
// try {
|
||||
// String urlStr = String.format(AudioUrl, URLEncoder.encode(text,"utf8"), "en");
|
||||
@@ -59,12 +59,12 @@ public class TranslateUtil {
|
||||
// //connection.setDoOutput(true);
|
||||
// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
|
||||
// connection.setRequestProperty("X-HTTP-Method-Override", "GET");
|
||||
//
|
||||
//
|
||||
// InputStream audioSrc = new BufferedInputStream(connection.getInputStream());
|
||||
// //DataInputStream read = new DataInputStream(audioSrc);
|
||||
// AudioInputStream audioIn = AudioSystem.getAudioInputStream(audioSrc);
|
||||
// Clip clip = AudioSystem.getClip();
|
||||
//
|
||||
//
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (MalformedURLException e) {
|
||||
@@ -72,10 +72,10 @@ public class TranslateUtil {
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (UnsupportedAudioFileException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
//
|
||||
// e.printStackTrace();
|
||||
// } catch (LineUnavailableException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
//
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -80,7 +80,7 @@ public class GuiNpcDimension extends GuiNPCInterface implements IScrollData{
|
||||
}
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -466,7 +466,7 @@ public class GuiNpcMobSpawner extends GuiNPCInterface implements IGuiData {
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -217,7 +217,7 @@ public class GuiNpcMobSpawnerMounter extends GuiNPCInterface implements IGuiData
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -144,7 +144,7 @@ public class GuiNpcMobSpawnerSelector extends SubGuiInterface implements IGuiDat
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SubGuiNpcResistanceProperties extends SubGuiInterface implements IS
|
||||
|
||||
@Override
|
||||
public void mousePressed(GuiNpcSlider slider) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ public class GuiNPCManageTransporters extends GuiNPCInterface implements IScroll
|
||||
|
||||
@Override
|
||||
public void setSelected(String selected) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ public class GuiEntitySelection extends GuiNPCInterface
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class GuiPresetSave extends GuiNPCInterface{
|
||||
}
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -155,7 +155,7 @@ public class GuiPresetSelection extends GuiNPCInterface
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ public class GuiMailbox extends GuiNPCInterface implements IGuiData, ICustomScro
|
||||
}
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -94,7 +94,7 @@ public class GuiTransportSelection extends GuiNPCInterface implements ITopButton
|
||||
}
|
||||
@Override
|
||||
public void setSelected(String selected) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -114,13 +114,13 @@ public class GuiNpcCompanion extends GuiNPCInterface2 implements ITextfieldListe
|
||||
|
||||
@Override
|
||||
public void mousePressed(GuiNpcSlider slider) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(GuiNpcSlider slider) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -308,7 +308,7 @@ public class GuiCustomScroll extends GuiScreen
|
||||
drawTexturedModalRect(i, k, xSize, 11, 5, 1);
|
||||
}
|
||||
public boolean hasSelected() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return selected >= 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -196,7 +196,7 @@ public class GuiModelInterface extends GuiNPCInterface {
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -155,8 +155,8 @@ public class GuiModelInterface2 extends GuiNPCInterface2{
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setSave(boolean saveNPC){
|
||||
|
||||
@@ -92,7 +92,7 @@ public class BlockCarpentryBenchRenderer extends TileEntitySpecialRenderer imple
|
||||
@Override
|
||||
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z,
|
||||
Block block, int modelId, RenderBlocks renderer) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class LegacyLoader {
|
||||
private String fileName;
|
||||
private Class<?> configClass;
|
||||
private LinkedList<Field> configFields;
|
||||
|
||||
|
||||
public LegacyLoader(Class<?> clss, File dir, String fileName){
|
||||
if(!dir.exists())
|
||||
dir.mkdir();
|
||||
@@ -131,16 +131,16 @@ public class LegacyLoader {
|
||||
out.write(name + "=" + field.get(null).toString() + System.getProperty( "line.separator" ));
|
||||
out.write(System.getProperty( "line.separator" ));
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,601 @@
|
||||
package noppes.npcs.controllers;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.NoppesUtilServer;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.handler.IStatusEffectHandler;
|
||||
import noppes.npcs.api.handler.data.ICustomEffect;
|
||||
import noppes.npcs.controllers.data.CustomEffect;
|
||||
import noppes.npcs.controllers.data.EffectScriptHandler;
|
||||
import noppes.npcs.controllers.data.PlayerEffect;
|
||||
import noppes.npcs.scripted.event.PlayerEvent;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import static noppes.npcs.scripted.event.PlayerEvent.EffectEvent.ExpirationType;
|
||||
|
||||
public class StatusEffectController implements IStatusEffectHandler {
|
||||
|
||||
public static StatusEffectController Instance = new StatusEffectController();
|
||||
public HashMap<Integer, HashMap<Integer, CustomEffect>> indexMapper = new HashMap<>();
|
||||
|
||||
public HashMap<Integer, CustomEffect> customEffectsSync = new HashMap<>();
|
||||
public HashMap<Integer, CustomEffect> customEffects = new HashMap<>();
|
||||
|
||||
public HashMap<Integer, EffectScriptHandler> customEffectScriptHandlers = new HashMap<>();
|
||||
private HashMap<Integer, String> bootOrder;
|
||||
|
||||
private int lastUsedID = 0;
|
||||
|
||||
public ConcurrentHashMap<UUID, Map<Integer, PlayerEffect>> playerEffects = new ConcurrentHashMap<>();
|
||||
|
||||
public StatusEffectController() {}
|
||||
|
||||
public static StatusEffectController getInstance() {
|
||||
return Instance;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
lastUsedID = 0;
|
||||
playerEffects.clear();
|
||||
bootOrder = new HashMap<>();
|
||||
LogWriter.info("Loading custom effects...");
|
||||
readCustomEffectMap();
|
||||
loadCustomEffects();
|
||||
LogWriter.info("Done loading custom effects.");
|
||||
}
|
||||
|
||||
|
||||
public void runEffects(EntityPlayer player) {
|
||||
Map<Integer, PlayerEffect> current = getPlayerEffects(player);
|
||||
for (int active : current.keySet()) {
|
||||
CustomEffect effect = get(active);
|
||||
if (effect != null) {
|
||||
effect.runEffect(player, current.get(active));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void killEffects(EntityPlayer player) {
|
||||
Map<Integer, PlayerEffect> current = getPlayerEffects(player);
|
||||
Iterator<Map.Entry<Integer, PlayerEffect>> iterator = current.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<Integer, PlayerEffect> entry = iterator.next();
|
||||
CustomEffect effect = get(entry.getKey());
|
||||
if (effect != null) {
|
||||
if (effect.lossOnDeath) {
|
||||
effect.onRemoved(player, entry.getValue(), PlayerEvent.EffectEvent.ExpirationType.DEATH);
|
||||
iterator.remove();
|
||||
}
|
||||
} else {
|
||||
iterator.remove(); // Use iterator to remove the current element
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
return get(name);
|
||||
|
||||
CustomEffect effect = new CustomEffect();
|
||||
effect.name = name;
|
||||
|
||||
if (effect.id == -1) {
|
||||
int id = getUnusedId();
|
||||
while (customEffects.containsKey(id)) {
|
||||
id = getUnusedId();
|
||||
}
|
||||
effect.id = id;
|
||||
}
|
||||
customEffects.put(effect.id, effect);
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICustomEffect getEffect(String name) {
|
||||
return get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEffect(String name) {
|
||||
ICustomEffect effect = getEffect(name);
|
||||
if (effect != null) {
|
||||
CustomEffect foundEffect = customEffects.remove(effect.getID());
|
||||
customEffectScriptHandlers.remove(effect.getID());
|
||||
if (foundEffect != null && foundEffect.name != null) {
|
||||
File dir = this.getDir();
|
||||
for (File file : dir.listFiles()) {
|
||||
if (!file.isFile() || !file.getName().endsWith(".json"))
|
||||
continue;
|
||||
if (file.getName().equals(foundEffect.name + ".json")) {
|
||||
file.delete();
|
||||
|
||||
// TODO: Fix Packets
|
||||
// DBCPacketHandler.Instance.sendToAll(new DBCInfoSyncPacket(DBCSyncType.CUSTOM_EFFECT, EnumSyncAction.REMOVE, foundEffect.getID(), new NBTTagCompound()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
saveEffectLoadMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(int id) {
|
||||
ICustomEffect effect = get(id);
|
||||
if (effect != null) {
|
||||
CustomEffect foundEffect = customEffects.remove(effect.getID());
|
||||
customEffectScriptHandlers.remove(effect.getID());
|
||||
if (foundEffect != null && foundEffect.name != null) {
|
||||
File dir = this.getDir();
|
||||
for (File file : dir.listFiles()) {
|
||||
if (!file.isFile() || !file.getName().endsWith(".json"))
|
||||
continue;
|
||||
if (file.getName().equals(foundEffect.name + ".json")) {
|
||||
file.delete();
|
||||
|
||||
// TODO: Fix Packets
|
||||
// DBCPacketHandler.Instance.sendToAll(new DBCInfoSyncPacket(DBCSyncType.CUSTOM_EFFECT, EnumSyncAction.REMOVE, foundEffect.getID(), new NBTTagCompound()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
saveEffectLoadMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int getUnusedId() {
|
||||
for (int catid : customEffects.keySet()) {
|
||||
if (catid > lastUsedID)
|
||||
lastUsedID = catid;
|
||||
}
|
||||
lastUsedID++;
|
||||
return lastUsedID;
|
||||
}
|
||||
|
||||
public CustomEffect get(String name) {
|
||||
for (CustomEffect effect : customEffects.values()) {
|
||||
if (effect.getName().equalsIgnoreCase(name)) {
|
||||
return effect;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public CustomEffect get(int id) {
|
||||
CustomEffect statusEffect;
|
||||
statusEffect = customEffects.get(id);
|
||||
return statusEffect;
|
||||
}
|
||||
|
||||
public Map<Integer, PlayerEffect> getPlayerEffects(EntityPlayer player) {
|
||||
UUID playerId = NoppesUtilServer.getUUID(player);
|
||||
if (!playerEffects.containsKey(playerId))
|
||||
playerEffects.put(playerId, new ConcurrentHashMap<>());
|
||||
return playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
}
|
||||
|
||||
public void applyEffect(EntityPlayer player, int id) {
|
||||
CustomEffect parent = get(id);
|
||||
if (parent != null) {
|
||||
PlayerEffect playerEffect = new PlayerEffect(id, parent.length, (byte) 1);
|
||||
applyEffect(player, playerEffect);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyEffect(EntityPlayer player, int id, int duration) {
|
||||
CustomEffect parent = get(id);
|
||||
if (parent != null) {
|
||||
PlayerEffect playerEffect = new PlayerEffect(id, duration, (byte) 1);
|
||||
applyEffect(player, playerEffect);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyEffect(EntityPlayer player, PlayerEffect effect) {
|
||||
if (effect == null)
|
||||
return;
|
||||
|
||||
CustomEffect parent = get(effect.id);
|
||||
if (parent != null) {
|
||||
Map<Integer, PlayerEffect> currentEffects = new ConcurrentHashMap<>();
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
else
|
||||
playerEffects.put(uuid, currentEffects);
|
||||
|
||||
currentEffects.put(effect.id, effect);
|
||||
parent.onAdded(player, effect);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeEffect(EntityPlayer player, PlayerEffect effect, ExpirationType type) {
|
||||
if (effect == null)
|
||||
return;
|
||||
|
||||
Map<Integer, PlayerEffect> currentEffects = new ConcurrentHashMap<>();
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
else
|
||||
playerEffects.put(uuid, currentEffects);
|
||||
|
||||
if (currentEffects.containsKey(effect.id)) {
|
||||
CustomEffect parent = get(effect.id);
|
||||
if (parent != null) {
|
||||
parent.onRemoved(player, effect, type);
|
||||
}
|
||||
currentEffects.remove(effect.id);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEffect(IPlayer player, int id) {
|
||||
if (player == null || player.getMCEntity() == null)
|
||||
return false;
|
||||
return hasEffect((EntityPlayer) player.getMCEntity(), id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEffect(IPlayer player, ICustomEffect effect) {
|
||||
return hasEffect(player, effect.getID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEffectDuration(IPlayer player, int id) {
|
||||
if (player == null || player.getMCEntity() == null)
|
||||
return -1;
|
||||
return getEffectDuration((EntityPlayer) player.getMCEntity(), id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEffectDuration(IPlayer player, ICustomEffect effect) {
|
||||
return getEffectDuration(player, effect.getID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyEffect(IPlayer player, int id, int duration, byte level) {
|
||||
if (player == null || player.getMCEntity() == null)
|
||||
return;
|
||||
applyEffect((EntityPlayer) player.getMCEntity(), id, duration, level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyEffect(IPlayer player, ICustomEffect effect, int duration, byte level) {
|
||||
applyEffect(player, effect.getID(), duration, level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEffect(IPlayer player, int id) {
|
||||
if (player == null || player.getMCEntity() == null)
|
||||
return;
|
||||
removeEffect((EntityPlayer) player.getMCEntity(), id, ExpirationType.REMOVED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeEffect(IPlayer player, ICustomEffect effect) {
|
||||
removeEffect((EntityPlayer) player.getMCEntity(), (PlayerEffect) effect, ExpirationType.REMOVED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearEffects(IPlayer player) {
|
||||
if (player == null || player.getMCEntity() == null)
|
||||
return;
|
||||
clearEffects(player.getMCEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICustomEffect saveEffect(ICustomEffect customEffect) {
|
||||
if (customEffect.getID() < 0) {
|
||||
customEffect.setID(getUnusedId());
|
||||
while (has(customEffect.getName()))
|
||||
customEffect.setName(customEffect.getName() + "_");
|
||||
} else {
|
||||
CustomEffect existing = customEffects.get(customEffect.getID());
|
||||
if (existing != null && !existing.name.equals(customEffect.getName()))
|
||||
while (has(customEffect.getName()))
|
||||
customEffect.setName(customEffect.getName() + "_");
|
||||
}
|
||||
|
||||
customEffects.remove(customEffect.getID());
|
||||
customEffects.put(customEffect.getID(), (CustomEffect) customEffect);
|
||||
|
||||
saveEffectLoadMap();
|
||||
|
||||
File dir = this.getDir();
|
||||
if (!dir.exists())
|
||||
dir.mkdirs();
|
||||
|
||||
File file = new File(dir, customEffect.getName() + ".json_new");
|
||||
File file2 = new File(dir, customEffect.getName() + ".json");
|
||||
|
||||
try {
|
||||
NBTTagCompound nbtTagCompound = ((CustomEffect) customEffect).writeToNBT(true);
|
||||
NBTJsonUtil.SaveFile(file, nbtTagCompound);
|
||||
if (file2.exists())
|
||||
file2.delete();
|
||||
file.renameTo(file2);
|
||||
nbtTagCompound.removeTag("ScriptData");
|
||||
// TODO: Add Packets
|
||||
// DBCPacketHandler.Instance.sendToAll(new DBCInfoSyncPacket(DBCSyncType.CUSTOM_EFFECT, EnumSyncAction.UPDATE, -1, nbtTagCompound));
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
return customEffects.get(customEffect.getID());
|
||||
}
|
||||
|
||||
public void clearEffects(Entity player) {
|
||||
Map<Integer, PlayerEffect> effects = playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
if (effects != null) {
|
||||
effects.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasEffect(EntityPlayer player, int id) {
|
||||
Map<Integer, PlayerEffect> currentEffects;
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(uuid);
|
||||
else
|
||||
return false;
|
||||
|
||||
return currentEffects.containsKey(id);
|
||||
}
|
||||
|
||||
public int getEffectDuration(EntityPlayer player, int id) {
|
||||
Map<Integer, PlayerEffect> currentEffects = new ConcurrentHashMap<>();
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(uuid);
|
||||
else
|
||||
playerEffects.put(uuid, currentEffects);
|
||||
|
||||
if (currentEffects.containsKey(id))
|
||||
return currentEffects.get(id).duration;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void applyEffect(EntityPlayer player, int id, int duration, byte level) {
|
||||
if (player == null || id <= 0)
|
||||
return;
|
||||
|
||||
Map<Integer, PlayerEffect> currentEffects = new ConcurrentHashMap<>();
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
else
|
||||
playerEffects.put(uuid, currentEffects);
|
||||
|
||||
CustomEffect parent = get(id);
|
||||
if (parent != null) {
|
||||
PlayerEffect playerEffect = new PlayerEffect(id, duration, level);
|
||||
currentEffects.put(id, playerEffect);
|
||||
parent.onAdded(player, playerEffect);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeEffect(EntityPlayer player, int id, ExpirationType type) {
|
||||
if (player == null || id <= 0)
|
||||
return;
|
||||
|
||||
Map<Integer, PlayerEffect> currentEffects = new ConcurrentHashMap<>();
|
||||
UUID uuid = NoppesUtilServer.getUUID(player);
|
||||
if (playerEffects.containsKey(uuid))
|
||||
currentEffects = playerEffects.get(NoppesUtilServer.getUUID(player));
|
||||
else
|
||||
playerEffects.put(uuid, currentEffects);
|
||||
|
||||
if (currentEffects.containsKey(id)) {
|
||||
PlayerEffect current = currentEffects.get(id);
|
||||
this.removeEffect(player, current, type);
|
||||
}
|
||||
}
|
||||
|
||||
public void decrementEffects(EntityPlayer player) {
|
||||
|
||||
Iterator<PlayerEffect> iterator = getPlayerEffects(player).values().iterator();
|
||||
|
||||
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
|
||||
while (iterator.hasNext()) {
|
||||
PlayerEffect effect = iterator.next();
|
||||
|
||||
if (effect == null) {
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (effect.duration == -100)
|
||||
continue;
|
||||
|
||||
if (effect.duration <= 0) {
|
||||
CustomEffect parent = StatusEffectController.Instance.get(effect.id);
|
||||
if (parent != null) {
|
||||
parent.onRemoved(player, effect, ExpirationType.RUN_OUT);
|
||||
}
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
effect.duration--;
|
||||
}
|
||||
}
|
||||
|
||||
public File getMapDir() {
|
||||
File dir = CustomNpcs.getWorldSaveDirectory();
|
||||
if (!dir.exists())
|
||||
dir.mkdir();
|
||||
return dir;
|
||||
}
|
||||
|
||||
private void loadCustomEffects() {
|
||||
customEffects.clear();
|
||||
|
||||
File dir = getDir();
|
||||
if (!dir.exists()) {
|
||||
dir.mkdir();
|
||||
} else {
|
||||
for (File file : dir.listFiles()) {
|
||||
if (!file.isFile() || !file.getName().endsWith(".json"))
|
||||
continue;
|
||||
try {
|
||||
CustomEffect effect = new CustomEffect();
|
||||
effect.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
effect.name = file.getName().substring(0, file.getName().length() - 5);
|
||||
|
||||
if (effect.id == -1) {
|
||||
effect.id = getUnusedId();
|
||||
}
|
||||
|
||||
int originalID = effect.id;
|
||||
int setID = effect.id;
|
||||
while (bootOrder.containsKey(setID) || customEffects.containsKey(setID)) {
|
||||
if (bootOrder.containsKey(setID))
|
||||
if (bootOrder.get(setID).equals(effect.name))
|
||||
break;
|
||||
|
||||
setID++;
|
||||
}
|
||||
|
||||
effect.id = setID;
|
||||
if (originalID != setID) {
|
||||
LogWriter.info("Found Custom Effect ID Mismatch: " + effect.name + ", New ID: " + setID);
|
||||
effect.save();
|
||||
}
|
||||
|
||||
customEffects.put(effect.id, effect);
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
indexMapper.put(0, customEffects);
|
||||
saveEffectLoadMap();
|
||||
}
|
||||
|
||||
public HashMap<Integer, CustomEffect> getEffectMap(int index){
|
||||
if(indexMapper.containsKey(index)){
|
||||
return getEffectMap(index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private File getDir() {
|
||||
return new File(CustomNpcs.getWorldSaveDirectory(), "customeffects");
|
||||
}
|
||||
|
||||
private void saveEffectLoadMap() {
|
||||
try {
|
||||
File saveDir = getMapDir();
|
||||
File file = new File(saveDir, "customeffects.dat_new");
|
||||
File file1 = new File(saveDir, "customeffects.dat_old");
|
||||
File file2 = new File(saveDir, "customeffects.dat");
|
||||
CompressedStreamTools.writeCompressed(this.writeMapNBT(), new FileOutputStream(file));
|
||||
if (file1.exists()) {
|
||||
file1.delete();
|
||||
}
|
||||
file2.renameTo(file1);
|
||||
if (file2.exists()) {
|
||||
file2.delete();
|
||||
}
|
||||
file.renameTo(file2);
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
}
|
||||
|
||||
private NBTTagCompound writeMapNBT() {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
NBTTagList customEffects = new NBTTagList();
|
||||
for (Integer key : this.customEffects.keySet()) {
|
||||
CustomEffect customEffect = this.customEffects.get(key);
|
||||
if (!customEffect.getName().isEmpty()) {
|
||||
NBTTagCompound effectCompound = new NBTTagCompound();
|
||||
effectCompound.setString("Name", customEffect.getName());
|
||||
effectCompound.setInteger("ID", key);
|
||||
|
||||
customEffects.appendTag(effectCompound);
|
||||
}
|
||||
}
|
||||
nbt.setTag("CustomEffects", customEffects);
|
||||
nbt.setInteger("lastID", lastUsedID);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
private void readCustomEffectMap() {
|
||||
bootOrder.clear();
|
||||
|
||||
try {
|
||||
File file = new File(getMapDir(), "customeffects.dat");
|
||||
if (file.exists()) {
|
||||
loadCustomEffectMap(file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
File file = new File(getMapDir(), "customeffects.dat_old");
|
||||
if (file.exists()) {
|
||||
loadCustomEffectMap(file);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCustomEffectMap(File file) throws IOException {
|
||||
DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))));
|
||||
readCustomEffectMap(var1);
|
||||
var1.close();
|
||||
}
|
||||
|
||||
private void readCustomEffectMap(DataInputStream stream) throws IOException {
|
||||
NBTTagCompound nbtCompound = CompressedStreamTools.read(stream);
|
||||
this.readMapNBT(nbtCompound);
|
||||
}
|
||||
|
||||
private void readMapNBT(NBTTagCompound compound) {
|
||||
lastUsedID = compound.getInteger("lastID");
|
||||
NBTTagList list = compound.getTagList("CustomEffects", 10);
|
||||
if (list != null) {
|
||||
for (int i = 0; i < list.tagCount(); i++) {
|
||||
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
|
||||
String effectName = nbttagcompound.getString("Name");
|
||||
Integer key = nbttagcompound.getInteger("ID");
|
||||
bootOrder.put(key, effectName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteEffectFile(String prevName) {
|
||||
File dir = this.getDir();
|
||||
if (!dir.exists())
|
||||
dir.mkdirs();
|
||||
File file2 = new File(dir, prevName + ".json");
|
||||
if (file2.exists())
|
||||
file2.delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.NoppesUtilServer;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.event.IPlayerEvent;
|
||||
import noppes.npcs.api.handler.data.ICustomEffect;
|
||||
import noppes.npcs.controllers.StatusEffectController;
|
||||
import noppes.npcs.scripted.event.PlayerEvent;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class CustomEffect implements ICustomEffect {
|
||||
public int id = -1;
|
||||
|
||||
public String name = "";
|
||||
public boolean lossOnDeath = true;
|
||||
public int length = 30;
|
||||
|
||||
// Must be a multiple of 10
|
||||
public int everyXTick = 20;
|
||||
|
||||
public String icon = "";
|
||||
// CustomNpcPlusDBC.ID + ":textures/gui/icons.png";
|
||||
public int iconX = 0, iconY = 0;
|
||||
|
||||
/**
|
||||
* Experimental script stuff.
|
||||
*/
|
||||
public Consumer<PlayerEvent.EffectEvent.Added> onAddedConsumer;
|
||||
public Consumer<PlayerEvent.EffectEvent.Ticked> onTickConsumer;
|
||||
public Consumer<PlayerEvent.EffectEvent.Removed> onRemovedConsumer;
|
||||
public String menuName = "§aNEW EFFECT";
|
||||
public int width = 16, height = 16;
|
||||
|
||||
public int index = 0;
|
||||
|
||||
public CustomEffect() {}
|
||||
|
||||
public CustomEffect(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CustomEffect(int id, String name) {
|
||||
this(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getID() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMenuName(String name) {
|
||||
if (name != null && !name.isEmpty())
|
||||
this.menuName = name.replaceAll("&", "§");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMenuName() {
|
||||
return menuName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
if (name != null && !name.isEmpty())
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEveryXTick() {
|
||||
return everyXTick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEveryXTick(int everyXTick) {
|
||||
if (everyXTick < 10)
|
||||
everyXTick = 10;
|
||||
int remainder = everyXTick % 10;
|
||||
if (remainder >= 5)
|
||||
everyXTick += 10 - remainder;
|
||||
else
|
||||
everyXTick -= remainder;
|
||||
this.everyXTick = everyXTick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconX() {
|
||||
return iconX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIconX(int iconX) {
|
||||
this.iconX = iconX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconY() {
|
||||
return iconY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIconY(int iconY) {
|
||||
this.iconY = iconY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return this.width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLossOnDeath() {
|
||||
return lossOnDeath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLossOnDeath(boolean lossOnDeath) {
|
||||
this.lossOnDeath = lossOnDeath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICustomEffect save() {
|
||||
return StatusEffectController.getInstance().saveEffect(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setID(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex() {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
|
||||
public void onAdded(Consumer<PlayerEvent.EffectEvent.Added> function) {
|
||||
onAddedConsumer = function;
|
||||
}
|
||||
|
||||
public void onTick(Consumer<PlayerEvent.EffectEvent.Ticked> function) {
|
||||
onTickConsumer = function;
|
||||
}
|
||||
|
||||
public void onRemoved(Consumer<PlayerEvent.EffectEvent.Removed> function) {
|
||||
onRemovedConsumer = function;
|
||||
}
|
||||
|
||||
public void onAdded(EntityPlayer player, PlayerEffect playerEffect) {
|
||||
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
|
||||
|
||||
PlayerEvent.EffectEvent.Added event = new PlayerEvent.EffectEvent.Added(iPlayer, playerEffect);
|
||||
if (onAddedConsumer != null) onAddedConsumer.accept(event);
|
||||
EffectScriptHandler script = getScriptHandler();
|
||||
if (script == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
script.callScript(EffectScriptHandler.ScriptType.OnAdd, event);
|
||||
}
|
||||
|
||||
public void onTick(EntityPlayer player, PlayerEffect playerEffect) {
|
||||
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
|
||||
PlayerEvent.EffectEvent.Ticked event = new PlayerEvent.EffectEvent.Ticked(iPlayer, playerEffect);
|
||||
|
||||
if (onTickConsumer != null) {
|
||||
onTickConsumer.accept(event);
|
||||
}
|
||||
|
||||
EffectScriptHandler script = getScriptHandler();
|
||||
if (script == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
script.callScript(EffectScriptHandler.ScriptType.OnTick, event);
|
||||
}
|
||||
|
||||
public void onRemoved(EntityPlayer player, PlayerEffect playerEffect, PlayerEvent.EffectEvent.ExpirationType type) {
|
||||
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
|
||||
|
||||
PlayerEvent.EffectEvent.Removed event = new PlayerEvent.EffectEvent.Removed(iPlayer, playerEffect, type);
|
||||
|
||||
if (onRemovedConsumer != null) {
|
||||
onRemovedConsumer.accept(event);
|
||||
}
|
||||
|
||||
EffectScriptHandler script = getScriptHandler();
|
||||
if (script == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
script.callScript(EffectScriptHandler.ScriptType.OnRemove, event);
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT(boolean saveScripts) {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
compound.setInteger("ID", id);
|
||||
compound.setString("name", name);
|
||||
compound.setString("menuName", menuName);
|
||||
compound.setInteger("length", length);
|
||||
compound.setInteger("everyXTick", everyXTick);
|
||||
compound.setInteger("iconX", iconX);
|
||||
compound.setInteger("iconY", iconY);
|
||||
compound.setInteger("iconWidth", width);
|
||||
compound.setInteger("iconHeight", height);
|
||||
compound.setString("icon", icon);
|
||||
compound.setBoolean("lossOnDeath", lossOnDeath);
|
||||
|
||||
// if (saveScripts && scriptContainer != null) {
|
||||
if (saveScripts) {
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
EffectScriptHandler handler = getScriptHandler();
|
||||
if (handler != null)
|
||||
handler.writeToNBT(scriptData);
|
||||
compound.setTag("ScriptData", scriptData);
|
||||
}
|
||||
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
if (compound.hasKey("ID"))
|
||||
id = compound.getInteger("ID");
|
||||
else
|
||||
id = StatusEffectController.Instance.getUnusedId();
|
||||
name = compound.getString("name");
|
||||
|
||||
if (compound.hasKey("menuName", Constants.NBT.TAG_STRING))
|
||||
menuName = compound.getString("menuName");
|
||||
else
|
||||
menuName = name;
|
||||
|
||||
length = compound.getInteger("length");
|
||||
everyXTick = compound.getInteger("everyXTick");
|
||||
iconX = compound.getInteger("iconX");
|
||||
iconY = compound.getInteger("iconY");
|
||||
|
||||
if (compound.hasKey("iconWidth", Constants.NBT.TAG_INT))
|
||||
width = compound.getInteger("iconWidth");
|
||||
else
|
||||
width = 16;
|
||||
|
||||
if (compound.hasKey("iconHeight", Constants.NBT.TAG_INT))
|
||||
height = compound.getInteger("iconHeight");
|
||||
else
|
||||
height = 16;
|
||||
|
||||
icon = compound.getString("icon");
|
||||
lossOnDeath = compound.getBoolean("lossOnDeath");
|
||||
|
||||
if (compound.hasKey("ScriptData", Constants.NBT.TAG_COMPOUND)) {
|
||||
EffectScriptHandler handler = new EffectScriptHandler();
|
||||
handler.readFromNBT(compound.getCompoundTag("ScriptData"));
|
||||
setScriptHandler(handler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public CustomEffect cloneEffect() {
|
||||
CustomEffect newEffect = new CustomEffect();
|
||||
newEffect.readFromNBT(this.writeToNBT(true));
|
||||
newEffect.id = -1;
|
||||
return newEffect;
|
||||
}
|
||||
|
||||
public EffectScriptHandler getScriptHandler() {
|
||||
return StatusEffectController.getInstance().customEffectScriptHandlers.get(this.id);
|
||||
}
|
||||
public void setScriptHandler(EffectScriptHandler handler) {
|
||||
StatusEffectController.getInstance().customEffectScriptHandlers.put(this.id, handler);
|
||||
}
|
||||
|
||||
public EffectScriptHandler getOrCreateScriptHandler() {
|
||||
EffectScriptHandler data = getScriptHandler();
|
||||
if (data == null)
|
||||
setScriptHandler(data =new EffectScriptHandler());
|
||||
return data;
|
||||
}
|
||||
|
||||
public void runEffect(EntityPlayer player, PlayerEffect playerEffect) {
|
||||
if (player.ticksExisted % everyXTick == 0) {
|
||||
onTick(player, playerEffect);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import kamkeel.npcs.util.ByteBufUtils;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.event.PlayerEvent;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class EffectScriptHandler implements IScriptHandler {
|
||||
public ScriptContainer container;
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
|
||||
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setString("ScriptLanguage", scriptLanguage);
|
||||
compound.setBoolean("ScriptEnabled", enabled);
|
||||
|
||||
if (container != null)
|
||||
compound.setTag("ScriptContent", container.writeToNBT(new NBTTagCompound()));
|
||||
return compound;
|
||||
}
|
||||
|
||||
public EffectScriptHandler readFromNBT(NBTTagCompound compound) {
|
||||
scriptLanguage = compound.getString("ScriptLanguage");
|
||||
enabled = compound.getBoolean("ScriptEnabled");
|
||||
|
||||
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
|
||||
container = new ScriptContainer(this);
|
||||
container.readFromNBT(compound.getCompoundTag("ScriptContent"));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void callScript(ScriptType type, PlayerEvent.EffectEvent event) {
|
||||
callScript(type.function, event);
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled && ScriptController.HasStart && container != null && ConfigScript.ScriptingEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callScript(String s, Event event) {
|
||||
if (!this.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ScriptEngine engine = container.engine;
|
||||
engine.put("API", NpcAPI.Instance());
|
||||
|
||||
for (Map.Entry<String, Object> engineObjects : NpcAPI.engineObjects.entrySet()) {
|
||||
engine.put(engineObjects.getKey(), engineObjects.getValue());
|
||||
}
|
||||
|
||||
container.run(s, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClient() {
|
||||
return FMLCommonHandler.instance().getEffectiveSide().isClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean b) {
|
||||
this.enabled = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return this.scriptLanguage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLanguage(String s) {
|
||||
this.scriptLanguage = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
container = null;
|
||||
return;
|
||||
}
|
||||
container = list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScriptContainer> getScripts() {
|
||||
if (container == null)
|
||||
return new ArrayList<>();
|
||||
return Collections.singletonList(container);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String noticeString() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap();
|
||||
int tab = 0;
|
||||
Iterator var3 = this.getScripts().iterator();
|
||||
|
||||
while(var3.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer)var3.next();
|
||||
++tab;
|
||||
Iterator var5 = script.console.entrySet().iterator();
|
||||
|
||||
while(var5.hasNext()) {
|
||||
Map.Entry<Long, String> longStringEntry = (Map.Entry)var5.next();
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String)longStringEntry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
Iterator var1 = this.getScripts().iterator();
|
||||
|
||||
while(var1.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer)var1.next();
|
||||
script.console.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveScript(ByteBuf buffer) throws IOException {
|
||||
int tab = buffer.readInt();
|
||||
int totalScripts = buffer.readInt();
|
||||
if (totalScripts == 0) {
|
||||
this.container = null;
|
||||
}
|
||||
|
||||
if (tab == 0) {
|
||||
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
|
||||
ScriptContainer script = new ScriptContainer(this);
|
||||
script.readFromNBT(tabCompound);
|
||||
this.container = script;
|
||||
} else {
|
||||
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
|
||||
this.setLanguage(compound.getString("ScriptLanguage"));
|
||||
if (!ScriptController.Instance.languages.containsKey(this.getLanguage())) {
|
||||
if (!ScriptController.Instance.languages.isEmpty()) {
|
||||
this.setLanguage((String) ScriptController.Instance.languages.keySet().toArray()[0]);
|
||||
} else {
|
||||
this.setLanguage("ECMAScript");
|
||||
}
|
||||
}
|
||||
this.setEnabled(compound.getBoolean("ScriptEnabled"));
|
||||
}
|
||||
}
|
||||
|
||||
public enum ScriptType {
|
||||
OnAdd("onAdded"),
|
||||
OnTick("onTick"),
|
||||
OnRemove("onRemove");
|
||||
|
||||
public final String function;
|
||||
ScriptType(String functionName) {
|
||||
this.function = functionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.handler.data.IPlayerEffect;
|
||||
import noppes.npcs.controllers.StatusEffectController;
|
||||
|
||||
public class PlayerEffect implements IPlayerEffect {
|
||||
public int id;
|
||||
public int duration;
|
||||
public byte level;
|
||||
public int index;
|
||||
|
||||
public PlayerEffect(int id, int duration, byte level) {
|
||||
this.id = id;
|
||||
this.duration = duration;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public static PlayerEffect readEffectData(NBTTagCompound nbt) {
|
||||
int id = nbt.getInteger("Id");
|
||||
int index = nbt.getInteger("Index");
|
||||
if (id > 0) {
|
||||
StatusEffectController controller = StatusEffectController.getInstance();
|
||||
boolean found = controller.has(id);
|
||||
// StatusEffectController.getInstance().customEffects.containsKey(id);
|
||||
if (found) {
|
||||
byte level = nbt.getByte("Level");
|
||||
int dur = nbt.getInteger("Dur");
|
||||
return new PlayerEffect(id, dur, level);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public NBTTagCompound writeEffectData(NBTTagCompound nbt) {
|
||||
nbt.setInteger("Id", this.id);
|
||||
nbt.setByte("Level", this.level);
|
||||
nbt.setInteger("Dur", this.duration);
|
||||
nbt.setInteger("Index", this.index);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kill() {
|
||||
duration = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDuration(int duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLevel(byte level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
CustomEffect effect = StatusEffectController.getInstance().get(this.id);
|
||||
if(effect != null)
|
||||
return effect.getName();
|
||||
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performEffect(IPlayer player) {
|
||||
if (player != null && player.getMCEntity() != null && player.getMCEntity() instanceof EntityPlayer) {
|
||||
CustomEffect effect = StatusEffectController.getInstance().get(this.id);
|
||||
if(effect != null)
|
||||
effect.onTick((EntityPlayer) player.getMCEntity(), this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex() {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,9 @@ public class PlayerMail implements IInventory, IPlayerMail {
|
||||
public int questId = -1;
|
||||
public String questTitle = "";
|
||||
public ItemStack[] items = new ItemStack[4];
|
||||
|
||||
|
||||
public long timePast;
|
||||
|
||||
|
||||
public void readNBT(NBTTagCompound compound) {
|
||||
subject = compound.getString("Subject");
|
||||
sender = compound.getString("Sender");
|
||||
@@ -81,7 +81,7 @@ public class PlayerMail implements IInventory, IPlayerMail {
|
||||
compound.setTag("MailItems", nbttaglist);
|
||||
return compound;
|
||||
}
|
||||
|
||||
|
||||
public boolean isValid(){
|
||||
return !subject.isEmpty() && !message.hasNoTags() && !sender.isEmpty();
|
||||
}
|
||||
@@ -168,20 +168,20 @@ public class PlayerMail implements IInventory, IPlayerMail {
|
||||
|
||||
@Override
|
||||
public String getInventoryName() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomInventoryName() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,7 +191,7 @@ public class PlayerMail implements IInventory, IPlayerMail {
|
||||
|
||||
@Override
|
||||
public void openInventory() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -268,7 +268,7 @@ public class JobItemGiver extends JobInterface{
|
||||
|
||||
@Override
|
||||
public void killed() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ public class JobItemGiver extends JobInterface{
|
||||
|
||||
@Override
|
||||
public void delete() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import noppes.npcs.api.entity.IEntity;
|
||||
import noppes.npcs.api.entity.IEntityLivingBase;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.api.event.IPlayerEvent;
|
||||
import noppes.npcs.api.handler.data.IPlayerEffect;
|
||||
import noppes.npcs.api.item.IItemStack;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
@@ -949,4 +950,59 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
|
||||
return EnumScriptType.RANGED_LAUNCHED.function;
|
||||
}
|
||||
}
|
||||
|
||||
public static class EffectEvent extends PlayerEvent implements IPlayerEvent.EffectEvent {
|
||||
|
||||
public final IPlayerEffect effect;
|
||||
|
||||
public EffectEvent(IPlayer player, IPlayerEffect statusEffect) {
|
||||
super(player);
|
||||
this.effect = statusEffect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPlayerEffect getEffect() {
|
||||
return this.effect;
|
||||
}
|
||||
|
||||
public static class Added extends PlayerEvent.EffectEvent implements IPlayerEvent.EffectEvent.Added {
|
||||
|
||||
public Added(IPlayer player, IPlayerEffect statusEffect) {
|
||||
super(player, statusEffect);
|
||||
}
|
||||
|
||||
}
|
||||
public static class Ticked extends PlayerEvent.EffectEvent implements IPlayerEvent.EffectEvent.Ticked {
|
||||
|
||||
public Ticked(IPlayer player, IPlayerEffect statusEffect) {
|
||||
super(player, statusEffect);
|
||||
}
|
||||
|
||||
}
|
||||
public static class Removed extends PlayerEvent.EffectEvent implements IPlayerEvent.EffectEvent.Removed {
|
||||
|
||||
private final ExpirationType type;
|
||||
|
||||
public Removed(IPlayer player, IPlayerEffect statusEffect, ExpirationType type) {
|
||||
super(player, statusEffect);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTimerRunOut() {
|
||||
return type == ExpirationType.RUN_OUT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean causedByDeath() {
|
||||
return type == ExpirationType.DEATH;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ExpirationType {
|
||||
REMOVED,
|
||||
RUN_OUT,
|
||||
DEATH
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user