Files
CustomNPC-Plus/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java
T

293 lines
8.2 KiB
Java
Raw Normal View History

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;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.ScriptController;
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
public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCustom, INpcScriptHandler {
2022-05-11 01:10:31 -05:00
public List<ScriptContainer> 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;
public boolean loaded = false;
2022-05-11 01:10:31 -05:00
public double durabilityValue = 1.0D;
public int stackSize = 64;
2022-05-11 01:10:31 -05:00
public int maxItemUseDuration = 20;
public int itemUseAction = 0; //0: none, 1: block, 2: bow, 3: eat, 4: drink
public boolean isNormalItem = false;
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;
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")) {
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;
}
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) {
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;
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++) {
ScriptContainer script = this.scripts.get(i);
if (!this.errored.contains(i)) {
2025-03-13 17:14:45 -04:00
if (script == null || script.errored || !script.hasCode())
2024-05-25 18:07:41 -04:00
continue;
2022-05-11 01:10:31 -05:00
2024-05-25 18:07:41 -04:00
script.run(hookName, event);
2022-07-03 09:46:40 -05:00
2024-05-25 18:07:41 -04:00
if (script.errored) {
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;
}
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;
}
public void setScripts(List<ScriptContainer> list) {
this.scripts = list;
}
2022-05-11 01:10:31 -05:00
public List<ScriptContainer> getScripts() {
return this.scripts;
}
public String noticeString() {
return "ScriptedItem";
}
public Map<Long, String> getConsoleText() {
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) {
this.armorType = armorType;
saveItemData();
}
2025-03-13 17:14:45 -04:00
public int getArmorType() {
return this.armorType;
}
2025-03-13 17:14:45 -04:00
public void setIsTool(boolean isTool) {
this.isTool = isTool;
saveItemData();
}
2025-03-13 17:14:45 -04:00
public boolean isTool() {
return this.isTool;
}
2025-03-13 17:14:45 -04:00
public void setIsNormalItem(boolean normalItem) {
this.isNormalItem = normalItem;
saveItemData();
}
2025-03-13 17:14:45 -04:00
public boolean isNormalItem() {
return this.isNormalItem;
}
2025-03-13 17:14:45 -04:00
public void setDigSpeed(int digSpeed) {
this.digSpeed = digSpeed;
saveItemData();
}
2025-03-13 17:14:45 -04:00
public int getDigSpeed() {
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;
}
public void setMaxItemUseDuration(int duration) {
2022-05-11 01:10:31 -05:00
this.maxItemUseDuration = duration;
saveItemData();
}
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() {
return this.enchantability;
}
2025-03-13 17:14:45 -04:00
public void setEnchantability(int enchantability) {
this.enchantability = enchantability;
saveItemData();
}
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
c.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound()));
2022-05-11 01:10:31 -05: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) {
super.setMCNbt(compound);
2022-05-11 01:10:31 -05:00
setScriptNBT(compound.getCompoundTag("ScriptedData"));
}
public NBTTagCompound getItemNBT(NBTTagCompound compound) {
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
compound.setBoolean("IsTool", this.isTool);
compound.setBoolean("IsNormalItem", this.isNormalItem);
compound.setInteger("DigSpeed", this.digSpeed);
compound.setInteger("ArmorType", this.armorType);
compound.setInteger("Enchantability", this.enchantability);
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) {
super.setItemNBT(compound);
2022-05-11 01:10:31 -05:00
this.durabilityValue = compound.getDouble("DurabilityValue");
this.stackSize = compound.getInteger("MaxStackSize");
this.isTool = compound.getBoolean("IsTool");
this.isNormalItem = compound.getBoolean("IsNormalItem");
this.digSpeed = compound.getInteger("DigSpeed");
this.armorType = compound.getInteger("ArmorType");
this.enchantability = compound.getInteger("Enchantability");
this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration");
this.itemUseAction = compound.getInteger("ItemUseAction");
2022-05-11 01:10:31 -05:00
}
}