2022-05-11 01:10:31 -05:00
|
|
|
package noppes.npcs.scripted.item;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import noppes.npcs.EventHooks;
|
|
|
|
|
import noppes.npcs.NBTTags;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.item.IItemCustom;
|
2022-05-11 01:10:31 -05:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
2026-01-13 17:34:14 +02:00
|
|
|
import noppes.npcs.controllers.data.IScriptUnit;
|
2022-05-11 01:10:31 -05:00
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
2023-07-13 13:56:32 -05:00
|
|
|
import noppes.npcs.controllers.data.INpcScriptHandler;
|
2022-05-11 01:10:31 -05:00
|
|
|
import noppes.npcs.scripted.CustomNPCsException;
|
|
|
|
|
|
2025-07-23 14:22:52 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.TreeMap;
|
2022-05-11 01:10:31 -05:00
|
|
|
|
2025-02-13 13:48:59 -06:00
|
|
|
public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCustom, INpcScriptHandler {
|
2026-01-13 17:34:14 +02:00
|
|
|
public List<IScriptUnit> scripts = new ArrayList();
|
2022-07-03 09:46:40 -05:00
|
|
|
public List<Integer> errored = new ArrayList();
|
2022-05-11 01:10:31 -05:00
|
|
|
public String scriptLanguage = "ECMAScript";
|
|
|
|
|
public boolean enabled = false;
|
2022-05-11 02:07:06 -05:00
|
|
|
public boolean loaded = false;
|
2022-05-11 01:10:31 -05:00
|
|
|
|
|
|
|
|
public double durabilityValue = 1.0D;
|
|
|
|
|
public int stackSize = 64;
|
2022-05-11 02:33:56 -05:00
|
|
|
|
2022-05-11 01:10:31 -05:00
|
|
|
public int maxItemUseDuration = 20;
|
2022-05-11 02:33:56 -05:00
|
|
|
public int itemUseAction = 0; //0: none, 1: block, 2: bow, 3: eat, 4: drink
|
2022-05-11 02:07:06 -05:00
|
|
|
|
2024-03-27 04:28:12 -04:00
|
|
|
public boolean isNormalItem = false;
|
2022-05-11 02:07:06 -05:00
|
|
|
public boolean isTool = false;
|
|
|
|
|
public int digSpeed = 1;
|
|
|
|
|
public int armorType = -2; //-2: Fits in no armor slot, -1: Fits in all slots, 0 - 4: Fits in Head -> Boots slot respectively
|
|
|
|
|
public int enchantability;
|
2025-12-30 19:29:44 -05:00
|
|
|
public int attackSpeed = 10;
|
2022-05-11 01:10:31 -05:00
|
|
|
|
2024-05-25 18:07:41 -04:00
|
|
|
public long lastInited = -1;
|
2022-05-11 01:10:31 -05:00
|
|
|
|
|
|
|
|
public ScriptCustomItem(ItemStack item) {
|
|
|
|
|
super(item);
|
|
|
|
|
loadItemData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound getScriptNBT(NBTTagCompound compound) {
|
|
|
|
|
compound.setTag("Scripts", NBTTags.NBTScript(this.scripts));
|
|
|
|
|
compound.setString("ScriptLanguage", this.scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", this.enabled);
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setScriptNBT(NBTTagCompound compound) {
|
|
|
|
|
if (compound.hasKey("Scripts")) {
|
2023-02-19 13:38:43 -06:00
|
|
|
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
|
2022-05-11 01:10:31 -05:00
|
|
|
this.scriptLanguage = compound.getString("ScriptLanguage");
|
|
|
|
|
this.enabled = compound.getBoolean("ScriptEnabled");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getType() {
|
|
|
|
|
return 6;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
private boolean isEnabled() {
|
|
|
|
|
return this.enabled && ScriptController.HasStart;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 12:28:17 -06:00
|
|
|
@Override
|
|
|
|
|
public INpcScriptHandler getScriptHandler() {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 01:10:31 -05:00
|
|
|
public void callScript(EnumScriptType type, Event event) {
|
2023-07-13 13:56:32 -05:00
|
|
|
this.callScript(type.function, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void callScript(String hookName, Event event) {
|
2022-05-11 01:10:31 -05:00
|
|
|
if (!this.loaded) {
|
|
|
|
|
this.loadScriptData();
|
|
|
|
|
this.loaded = true;
|
2024-05-25 18:07:41 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (!this.isEnabled())
|
2024-05-25 18:07:41 -04:00
|
|
|
return;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (ScriptController.Instance.lastLoaded > lastInited) {
|
2024-05-25 18:07:41 -04:00
|
|
|
lastInited = ScriptController.Instance.lastLoaded;
|
2023-07-13 13:56:32 -05:00
|
|
|
if (!Objects.equals(hookName, EnumScriptType.INIT.function)) {
|
2023-05-30 00:56:39 -05:00
|
|
|
EventHooks.onScriptItemInit(this);
|
|
|
|
|
}
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
2024-05-25 18:07:41 -04:00
|
|
|
for (int i = 0; i < this.scripts.size(); i++) {
|
2026-01-13 17:34:14 +02:00
|
|
|
IScriptUnit script = this.scripts.get(i);
|
2024-05-25 18:07:41 -04:00
|
|
|
if (!this.errored.contains(i)) {
|
2026-01-13 17:34:14 +02:00
|
|
|
if (script == null || script.hasErrored() || !script.hasCode())
|
2024-05-25 18:07:41 -04:00
|
|
|
continue;
|
2022-05-11 01:10:31 -05:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
if (script instanceof ScriptContainer) {
|
|
|
|
|
((ScriptContainer) script).run(hookName, event);
|
|
|
|
|
}
|
2022-07-03 09:46:40 -05:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
if (script.hasErrored()) {
|
2024-05-25 18:07:41 -04:00
|
|
|
this.errored.add(i);
|
2022-07-03 09:46:40 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isClient() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getEnabled() {
|
|
|
|
|
return this.enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 04:28:12 -04:00
|
|
|
public void setEnabled(boolean enable) {
|
|
|
|
|
this.enabled = enable;
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLanguage() {
|
|
|
|
|
return this.scriptLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLanguage(String lang) {
|
|
|
|
|
this.scriptLanguage = lang;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public void setScripts(List<IScriptUnit> list) {
|
2023-02-19 16:22:28 -06:00
|
|
|
this.scripts = list;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public List<IScriptUnit> getScripts() {
|
2022-05-11 01:10:31 -05:00
|
|
|
return this.scripts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String noticeString() {
|
|
|
|
|
return "ScriptedItem";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Map<Long, String> getConsoleText() {
|
2023-03-29 21:35:16 -05:00
|
|
|
return new TreeMap<>();
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clearConsole() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getMaxStackSize() {
|
|
|
|
|
return this.stackSize;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-02 03:15:38 -05:00
|
|
|
public void setArmorType(int armorType) {
|
2022-05-11 02:07:06 -05:00
|
|
|
this.armorType = armorType;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getArmorType() {
|
2022-05-11 02:07:06 -05:00
|
|
|
return this.armorType;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void setIsTool(boolean isTool) {
|
2022-05-11 02:07:06 -05:00
|
|
|
this.isTool = isTool;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public boolean isTool() {
|
2022-05-11 02:07:06 -05:00
|
|
|
return this.isTool;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void setIsNormalItem(boolean normalItem) {
|
2024-03-27 04:28:12 -04:00
|
|
|
this.isNormalItem = normalItem;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public boolean isNormalItem() {
|
2024-03-27 04:28:12 -04:00
|
|
|
return this.isNormalItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void setDigSpeed(int digSpeed) {
|
2022-05-11 02:07:06 -05:00
|
|
|
this.digSpeed = digSpeed;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getDigSpeed() {
|
2022-05-11 02:07:06 -05:00
|
|
|
return this.digSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 01:10:31 -05:00
|
|
|
public void setMaxStackSize(int size) {
|
2022-07-02 03:14:06 -05:00
|
|
|
if (size >= 1 && size <= 127) {
|
2022-05-11 01:10:31 -05:00
|
|
|
this.stackSize = size;
|
|
|
|
|
saveItemData();
|
|
|
|
|
} else {
|
2022-07-02 03:14:06 -05:00
|
|
|
throw new CustomNPCsException("Stacksize has to be between 1 and 127", new Object[0]);
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getDurabilityValue() {
|
|
|
|
|
return this.durabilityValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDurabilityValue(float value) {
|
2025-03-13 17:14:45 -04:00
|
|
|
this.durabilityValue = (double) value;
|
2022-05-11 01:10:31 -05:00
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getMaxItemUseDuration() {
|
2022-05-11 01:10:31 -05:00
|
|
|
return this.maxItemUseDuration;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 02:33:56 -05:00
|
|
|
public void setMaxItemUseDuration(int duration) {
|
2022-05-11 01:10:31 -05:00
|
|
|
this.maxItemUseDuration = duration;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 02:33:56 -05:00
|
|
|
public void setItemUseAction(int action) {
|
|
|
|
|
this.itemUseAction = action;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getItemUseAction() {
|
|
|
|
|
return this.itemUseAction;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getEnchantability() {
|
2022-05-11 02:07:06 -05:00
|
|
|
return this.enchantability;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:29:44 -05:00
|
|
|
public int getAttackSpeed() {
|
|
|
|
|
return this.attackSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAttackSpeed(int speed) {
|
|
|
|
|
if(speed <= 0) {
|
|
|
|
|
speed = 10;
|
|
|
|
|
}
|
|
|
|
|
this.attackSpeed = speed;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public void setEnchantability(int enchantability) {
|
2022-05-11 02:07:06 -05:00
|
|
|
this.enchantability = enchantability;
|
|
|
|
|
saveItemData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 13:48:59 -06:00
|
|
|
public void saveScriptData() {
|
|
|
|
|
NBTTagCompound c = this.item.getTagCompound();
|
|
|
|
|
if (c == null) {
|
|
|
|
|
this.item.setTagCompound(c = new NBTTagCompound());
|
|
|
|
|
}
|
2022-05-11 01:10:31 -05:00
|
|
|
|
2025-02-13 13:48:59 -06:00
|
|
|
c.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound()));
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-13 13:48:59 -06:00
|
|
|
public void loadScriptData() {
|
|
|
|
|
NBTTagCompound c = this.item.getTagCompound();
|
|
|
|
|
if (c != null) {
|
|
|
|
|
this.setScriptNBT(c.getCompoundTag("ScriptedData"));
|
|
|
|
|
}
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound getMCNbt() {
|
|
|
|
|
NBTTagCompound compound = super.getMCNbt();
|
|
|
|
|
compound.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound()));
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMCNbt(NBTTagCompound compound) {
|
2025-02-13 13:48:59 -06:00
|
|
|
super.setMCNbt(compound);
|
2022-05-11 01:10:31 -05:00
|
|
|
setScriptNBT(compound.getCompoundTag("ScriptedData"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound getItemNBT(NBTTagCompound compound) {
|
2025-02-13 13:48:59 -06:00
|
|
|
super.getItemNBT(compound);
|
2025-03-13 17:14:45 -04:00
|
|
|
compound.setDouble("DurabilityValue", this.durabilityValue);
|
|
|
|
|
compound.setInteger("MaxStackSize", this.stackSize);
|
2022-05-11 01:10:31 -05:00
|
|
|
|
2022-05-11 02:07:06 -05:00
|
|
|
compound.setBoolean("IsTool", this.isTool);
|
2024-03-27 04:28:12 -04:00
|
|
|
compound.setBoolean("IsNormalItem", this.isNormalItem);
|
2022-05-11 02:07:06 -05:00
|
|
|
compound.setInteger("DigSpeed", this.digSpeed);
|
|
|
|
|
compound.setInteger("ArmorType", this.armorType);
|
|
|
|
|
compound.setInteger("Enchantability", this.enchantability);
|
2025-12-30 19:29:44 -05:00
|
|
|
compound.setInteger("AttackSpeed", this.attackSpeed);
|
2022-05-11 02:33:56 -05:00
|
|
|
|
|
|
|
|
compound.setInteger("MaxItemUseDuration", this.maxItemUseDuration);
|
|
|
|
|
compound.setInteger("ItemUseAction", this.itemUseAction);
|
2022-05-11 01:10:31 -05:00
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setItemNBT(NBTTagCompound compound) {
|
2025-02-13 13:48:59 -06:00
|
|
|
super.setItemNBT(compound);
|
2022-05-11 01:10:31 -05:00
|
|
|
this.durabilityValue = compound.getDouble("DurabilityValue");
|
|
|
|
|
this.stackSize = compound.getInteger("MaxStackSize");
|
|
|
|
|
|
2022-05-11 02:07:06 -05:00
|
|
|
this.isTool = compound.getBoolean("IsTool");
|
2024-03-27 04:28:12 -04:00
|
|
|
this.isNormalItem = compound.getBoolean("IsNormalItem");
|
2022-05-11 02:07:06 -05:00
|
|
|
this.digSpeed = compound.getInteger("DigSpeed");
|
|
|
|
|
this.armorType = compound.getInteger("ArmorType");
|
|
|
|
|
this.enchantability = compound.getInteger("Enchantability");
|
2025-12-30 19:29:44 -05:00
|
|
|
setAttackSpeed(compound.getInteger("AttackSpeed"));
|
2022-05-11 02:33:56 -05:00
|
|
|
|
|
|
|
|
this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration");
|
|
|
|
|
this.itemUseAction = compound.getInteger("ItemUseAction");
|
2022-05-11 01:10:31 -05:00
|
|
|
}
|
|
|
|
|
}
|