mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-24 11:44:25 +00:00
Merge branch 'master' into dev
# Conflicts: # src/main/java/noppes/npcs/scripted/ScriptFaction.java # src/main/java/noppes/npcs/scripted/ScriptWorld.java # src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java # src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java # src/main/java/noppes/npcs/scripted/interfaces/IEntity.java # src/main/java/noppes/npcs/scripted/interfaces/IWorld.java # src/main/java/noppes/npcs/scripted/interfaces/entity/ICustomNpc.java # src/main/java/noppes/npcs/scripted/interfaces/entity/IPlayer.java
This commit is contained in:
commit
ec5fc71413
300 files changed
+9920
-2959
No files matched your search
@@ -0,0 +1,524 @@
|
||||
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;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.IScriptHandler;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.scripted.CustomNPCsException;
|
||||
import noppes.npcs.scripted.interfaces.item.IItemCustom;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IScriptHandler {
|
||||
public List<ScriptContainer> scripts = new ArrayList();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
public long lastInited = -1L;
|
||||
public boolean loaded = false;
|
||||
|
||||
public boolean durabilityShow = false;
|
||||
public double durabilityValue = 1.0D;
|
||||
public int durabilityColor = -1;
|
||||
public int itemColor = 0x8B4513;
|
||||
public int stackSize = 64;
|
||||
|
||||
public int maxItemUseDuration = 20;
|
||||
public int itemUseAction = 0; //0: none, 1: block, 2: bow, 3: eat, 4: drink
|
||||
|
||||
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;
|
||||
|
||||
public String texture = "minecraft:textures/items/iron_pickaxe.png";
|
||||
public int width = -1, height = -1;
|
||||
|
||||
public float translateX, translateY, translateZ;
|
||||
public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F;
|
||||
public float rotationX, rotationY, rotationZ;
|
||||
public float rotationXRate, rotationYRate, rotationZRate;
|
||||
|
||||
public ScriptCustomItem(ItemStack item) {
|
||||
super(item);
|
||||
loadScriptData();
|
||||
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.GetScript(compound.getTagList("Scripts", 10), this);
|
||||
this.scriptLanguage = compound.getString("ScriptLanguage");
|
||||
this.enabled = compound.getBoolean("ScriptEnabled");
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return 6;
|
||||
}
|
||||
|
||||
public void callScript(EnumScriptType type, Event event) {
|
||||
if (!this.loaded) {
|
||||
this.loadScriptData();
|
||||
this.loaded = true;
|
||||
}
|
||||
|
||||
if (this.isEnabled()) {
|
||||
if (ScriptController.Instance.lastLoaded > this.lastInited) {
|
||||
this.lastInited = ScriptController.Instance.lastLoaded;
|
||||
if (type != EnumScriptType.INIT) {
|
||||
EventHooks.onScriptItemInit(this);
|
||||
}
|
||||
}
|
||||
|
||||
Iterator var3 = this.scripts.iterator();
|
||||
|
||||
while(var3.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer)var3.next();
|
||||
script.run(type, event);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
return this.enabled && ScriptController.HasStart;
|
||||
}
|
||||
|
||||
public boolean isClient() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean bo) {
|
||||
this.enabled = bo;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return this.scriptLanguage;
|
||||
}
|
||||
|
||||
public void setLanguage(String lang) {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public List<ScriptContainer> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
public String noticeString() {
|
||||
return "ScriptedItem";
|
||||
}
|
||||
|
||||
public Map<Long, String> getConsoleText() {
|
||||
Map<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> entry = (Map.Entry)var5.next();
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + (String)entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public void clearConsole() {
|
||||
Iterator var1 = this.getScripts().iterator();
|
||||
|
||||
while(var1.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer)var1.next();
|
||||
script.console.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getTexture() {
|
||||
return this.texture == null ? "" : this.texture;
|
||||
}
|
||||
|
||||
public void setTexture(String texture){
|
||||
if(texture == null)
|
||||
texture = "";
|
||||
this.texture = texture;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getMaxStackSize() {
|
||||
return this.stackSize;
|
||||
}
|
||||
|
||||
public void setArmorType(int armorType){
|
||||
this.armorType = armorType;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getArmorType(){
|
||||
return this.armorType;
|
||||
}
|
||||
|
||||
public void setIsTool(boolean isTool){
|
||||
this.isTool = isTool;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public boolean isTool(){
|
||||
return this.isTool;
|
||||
}
|
||||
|
||||
public void setDigSpeed(int digSpeed){
|
||||
this.digSpeed = digSpeed;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getDigSpeed(){
|
||||
return this.digSpeed;
|
||||
}
|
||||
|
||||
public void setMaxStackSize(int size) {
|
||||
if (size >= 1 && size <= 64) {
|
||||
this.stackSize = size;
|
||||
saveItemData();
|
||||
} else {
|
||||
throw new CustomNPCsException("Stacksize has to be between 1 and 64", new Object[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public double getDurabilityValue() {
|
||||
return this.durabilityValue;
|
||||
}
|
||||
|
||||
public void setDurabilityValue(float value) {
|
||||
this.durabilityValue = (double)value;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public boolean getDurabilityShow() {
|
||||
return this.durabilityShow;
|
||||
}
|
||||
|
||||
public void setDurabilityShow(boolean bo) {
|
||||
this.durabilityShow = bo;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getDurabilityColor() {
|
||||
return this.durabilityColor;
|
||||
}
|
||||
|
||||
public void setDurabilityColor(int color) {
|
||||
this.durabilityColor = color;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
return this.itemColor;
|
||||
}
|
||||
|
||||
public void setColor(int color) {
|
||||
this.itemColor = color;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getMaxItemUseDuration(){
|
||||
return this.maxItemUseDuration;
|
||||
}
|
||||
|
||||
public void setMaxItemUseDuration(int duration) {
|
||||
this.maxItemUseDuration = duration;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setItemUseAction(int action) {
|
||||
this.itemUseAction = action;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public int getItemUseAction() {
|
||||
return this.itemUseAction;
|
||||
}
|
||||
|
||||
public int getEnchantability(){
|
||||
return this.enchantability;
|
||||
}
|
||||
|
||||
public void setEnchantability(int enchantability){
|
||||
this.enchantability = enchantability;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotation(float rotationX, float rotationY, float rotationZ){
|
||||
this.rotationX = rotationX;
|
||||
this.rotationY = rotationY;
|
||||
this.rotationZ = rotationZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationRate(float rotationXRate, float rotationYRate, float rotationZRate){
|
||||
this.rotationXRate = rotationXRate;
|
||||
this.rotationYRate = rotationYRate;
|
||||
this.rotationZRate = rotationZRate;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setScale(float scaleX, float scaleY, float scaleZ){
|
||||
this.scaleX = scaleX;
|
||||
this.scaleY = scaleY;
|
||||
this.scaleZ = scaleZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setTranslate(float translateX, float translateY, float translateZ){
|
||||
this.translateX = translateX;
|
||||
this.translateY = translateY;
|
||||
this.translateZ = translateZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationX(float rotationX) {
|
||||
this.rotationX = rotationX;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationY(float rotationY) {
|
||||
this.rotationY = rotationY;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationZ(float rotationZ) {
|
||||
this.rotationZ = rotationZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationXRate(float rotationXRate) {
|
||||
this.rotationXRate = rotationXRate;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationYRate(float rotationYRate) {
|
||||
this.rotationYRate = rotationYRate;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setRotationZRate(float rotationZRate) {
|
||||
this.rotationZRate = rotationZRate;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setScaleX(float scaleX) {
|
||||
this.scaleX = scaleX;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setScaleY(float scaleY) {
|
||||
this.scaleY = scaleY;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setScaleZ(float scaleZ) {
|
||||
this.scaleZ = scaleZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setTranslateX(float translateX) {
|
||||
this.translateX = translateX;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setTranslateY(float translateY) {
|
||||
this.translateY = translateY;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public void setTranslateZ(float translateZ) {
|
||||
this.translateZ = translateZ;
|
||||
saveItemData();
|
||||
}
|
||||
|
||||
public float getRotationX() {
|
||||
return rotationX;
|
||||
}
|
||||
|
||||
public float getRotationY() {
|
||||
return rotationY;
|
||||
}
|
||||
|
||||
public float getRotationZ() {
|
||||
return rotationZ;
|
||||
}
|
||||
|
||||
public float getRotationXRate() {
|
||||
return rotationXRate;
|
||||
}
|
||||
|
||||
public float getRotationYRate() {
|
||||
return rotationYRate;
|
||||
}
|
||||
|
||||
public float getRotationZRate() {
|
||||
return rotationZRate;
|
||||
}
|
||||
|
||||
public float getScaleX() {
|
||||
return scaleX;
|
||||
}
|
||||
|
||||
public float getScaleY() {
|
||||
return scaleY;
|
||||
}
|
||||
|
||||
public float getScaleZ() {
|
||||
return scaleZ;
|
||||
}
|
||||
|
||||
public float getTranslateX() {
|
||||
return translateX;
|
||||
}
|
||||
|
||||
public float getTranslateY() {
|
||||
return translateY;
|
||||
}
|
||||
|
||||
public float getTranslateZ() {
|
||||
return translateZ;
|
||||
}
|
||||
|
||||
public NBTTagCompound getMCNbt() {
|
||||
NBTTagCompound compound = super.getMCNbt();
|
||||
compound.setTag("ItemData", this.getItemNBT(new NBTTagCompound()));
|
||||
compound.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound()));
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void setMCNbt(NBTTagCompound compound) {
|
||||
setScriptNBT(compound.getCompoundTag("ScriptedData"));
|
||||
setItemNBT(compound.getCompoundTag("ItemData"));
|
||||
}
|
||||
|
||||
public NBTTagCompound getItemNBT(NBTTagCompound compound) {
|
||||
compound.setBoolean("DurabilityShow",this.durabilityShow);
|
||||
compound.setDouble("DurabilityValue",this.durabilityValue);
|
||||
compound.setInteger("DurabilityColor",this.durabilityColor );
|
||||
compound.setInteger("ItemColor",this.itemColor);
|
||||
compound.setInteger("MaxStackSize",this.stackSize);
|
||||
compound.setString("ItemTexture",this.texture);
|
||||
|
||||
compound.setFloat("RotationX",this.rotationX);
|
||||
compound.setFloat("RotationY",this.rotationY);
|
||||
compound.setFloat("RotationZ",this.rotationZ);
|
||||
|
||||
compound.setFloat("RotationXRate",this.rotationXRate);
|
||||
compound.setFloat("RotationYRate",this.rotationYRate);
|
||||
compound.setFloat("RotationZRate",this.rotationZRate);
|
||||
|
||||
compound.setFloat("ScaleX",this.scaleX);
|
||||
compound.setFloat("ScaleY",this.scaleY);
|
||||
compound.setFloat("ScaleZ",this.scaleZ);
|
||||
|
||||
compound.setFloat("TranslateX",this.translateX);
|
||||
compound.setFloat("TranslateY",this.translateY);
|
||||
compound.setFloat("TranslateZ",this.translateZ);
|
||||
|
||||
compound.setInteger("Width", this.width);
|
||||
compound.setInteger("Height", this.height);
|
||||
|
||||
compound.setBoolean("IsTool", this.isTool);
|
||||
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);
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void setItemNBT(NBTTagCompound compound) {
|
||||
this.durabilityShow = compound.getBoolean("DurabilityShow");
|
||||
this.durabilityValue = compound.getDouble("DurabilityValue");
|
||||
if (compound.hasKey("DurabilityColor")) {
|
||||
this.durabilityColor = compound.getInteger("DurabilityColor");
|
||||
}
|
||||
this.itemColor = compound.getInteger("ItemColor");
|
||||
this.stackSize = compound.getInteger("MaxStackSize");
|
||||
this.texture = compound.getString("ItemTexture");
|
||||
|
||||
this.rotationX = compound.getFloat("RotationX");
|
||||
this.rotationY = compound.getFloat("RotationY");
|
||||
this.rotationZ = compound.getFloat("RotationZ");
|
||||
|
||||
this.rotationXRate = compound.getFloat("RotationXRate");
|
||||
this.rotationYRate = compound.getFloat("RotationYRate");
|
||||
this.rotationZRate = compound.getFloat("RotationZRate");
|
||||
|
||||
this.scaleX = compound.getFloat("ScaleX");
|
||||
this.scaleY = compound.getFloat("ScaleY");
|
||||
this.scaleZ = compound.getFloat("ScaleZ");
|
||||
|
||||
this.translateX = compound.getFloat("TranslateX");
|
||||
this.translateY = compound.getFloat("TranslateY");
|
||||
this.translateZ = compound.getFloat("TranslateZ");
|
||||
|
||||
this.width = compound.getInteger("Width");
|
||||
this.height = compound.getInteger("Height");
|
||||
|
||||
this.isTool = compound.getBoolean("IsTool");
|
||||
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");
|
||||
}
|
||||
|
||||
public void saveScriptData() {
|
||||
NBTTagCompound c = this.item.getTagCompound();
|
||||
if (c == null) {
|
||||
this.item.setTagCompound(c = new NBTTagCompound());
|
||||
}
|
||||
|
||||
c.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound()));
|
||||
}
|
||||
|
||||
public void loadScriptData() {
|
||||
NBTTagCompound c = this.item.getTagCompound();
|
||||
if (c != null) {
|
||||
this.setScriptNBT(c.getCompoundTag("ScriptedData"));
|
||||
}
|
||||
}
|
||||
|
||||
public void saveItemData() {
|
||||
NBTTagCompound c = this.item.getTagCompound();
|
||||
if (c == null) {
|
||||
this.item.setTagCompound(c = new NBTTagCompound());
|
||||
}
|
||||
|
||||
c.setTag("ItemData", this.getItemNBT(new NBTTagCompound()));
|
||||
}
|
||||
|
||||
public void loadItemData() {
|
||||
NBTTagCompound c = this.item.getTagCompound();
|
||||
if (c != null){
|
||||
this.setItemNBT(c.getCompoundTag("ItemData"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package noppes.npcs.scripted.item;
|
||||
|
||||
import net.minecraft.item.ItemArmor;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import noppes.npcs.scripted.interfaces.item.IItemArmor;
|
||||
|
||||
public class ScriptItemArmor extends ScriptItemStack implements IItemArmor {
|
||||
protected ItemArmor armor;
|
||||
|
||||
public ScriptItemArmor(ItemStack item) {
|
||||
super(item);
|
||||
this.armor = (ItemArmor) item.getItem();
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public int getArmorSlot() {
|
||||
return armor.armorType;
|
||||
}
|
||||
|
||||
public String getArmorMaterial() {
|
||||
return armor.getArmorMaterial().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package noppes.npcs.scripted.item;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import noppes.npcs.scripted.interfaces.item.IItemBlock;
|
||||
|
||||
public class ScriptItemBlock extends ScriptItemStack implements IItemBlock {
|
||||
protected String blockName;
|
||||
|
||||
public ScriptItemBlock(ItemStack item) {
|
||||
super(item);
|
||||
Block b = Block.getBlockFromItem(item.getItem());
|
||||
this.blockName = Block.blockRegistry.getNameForObject(b);
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public String getBlockName() {
|
||||
return blockName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package noppes.npcs.scripted.item;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagString;
|
||||
import noppes.npcs.scripted.interfaces.item.IItemBook;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ScriptItemBook extends ScriptItemStack implements IItemBook {
|
||||
|
||||
public ScriptItemBook(ItemStack item) {
|
||||
super(item);
|
||||
}
|
||||
|
||||
public String[] getText() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
NBTTagList pages = this.getTag().getTagList("pages", 8);
|
||||
|
||||
for(int i = 0; i < pages.tagCount(); ++i) {
|
||||
list.add(pages.getStringTagAt(i));
|
||||
}
|
||||
|
||||
return (String[])list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
public void setText(String[] pages) {
|
||||
NBTTagList list = new NBTTagList();
|
||||
if (pages != null && pages.length > 0) {
|
||||
String[] var3 = pages;
|
||||
int var4 = pages.length;
|
||||
|
||||
for(int var5 = 0; var5 < var4; ++var5) {
|
||||
String page = var3[var5];
|
||||
list.appendTag(new NBTTagString(page));
|
||||
}
|
||||
}
|
||||
|
||||
this.getTag().setTag("pages", list);
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return this.getTag().getString("author");
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.getTag().setString("author",author);
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.getTag().getString("title");
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.getTag().setString("title",title);
|
||||
}
|
||||
|
||||
private NBTTagCompound getTag() {
|
||||
NBTTagCompound comp = this.item.getTagCompound();
|
||||
if (comp == null) {
|
||||
this.item.setTagCompound(comp = new NBTTagCompound());
|
||||
}
|
||||
|
||||
return comp;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,409 @@
|
||||
package noppes.npcs.scripted.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemFood;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.ItemSword;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTBase.NBTPrimitive;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagString;
|
||||
import net.minecraftforge.common.IPlantable;
|
||||
import noppes.npcs.scripted.CustomNPCsException;
|
||||
import noppes.npcs.scripted.ScriptNbt;
|
||||
import noppes.npcs.scripted.interfaces.item.IItemStack;
|
||||
import noppes.npcs.scripted.interfaces.INbt;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
|
||||
public class ScriptItemStack implements IItemStack {
|
||||
public ItemStack item;
|
||||
|
||||
public ScriptItemStack(ItemStack item){
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (this.item.getItem() instanceof IPlantable) {
|
||||
return 5;
|
||||
} else {
|
||||
return this.item.getItem() instanceof ItemSword ? 4 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The minecraft name for this item
|
||||
*/
|
||||
public String getName(){
|
||||
return Item.itemRegistry.getNameForObject(item.getItem());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the stacksize
|
||||
*/
|
||||
public int getStackSize(){
|
||||
return item.stackSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Return whether or not the item has a custom name
|
||||
*/
|
||||
public boolean hasCustomName(){
|
||||
return item.hasDisplayName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name The custom name this item will get
|
||||
*/
|
||||
public void setCustomName(String name){
|
||||
item.setStackDisplayName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Return the ingame displayed name. This is either the item name or the custom name if it has one.
|
||||
*/
|
||||
public String getDisplayName(){
|
||||
return item.getDisplayName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Get the items ingame name. Use this incase the item ingame has custom name and you want the original name.
|
||||
*/
|
||||
public String getItemName(){
|
||||
return item.getItem().getItemStackDisplayName(item);
|
||||
}
|
||||
|
||||
|
||||
public int getMaxStackSize() {
|
||||
return this.item.getMaxStackSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param size The size of the itemstack. A number between 1 and 64
|
||||
*/
|
||||
public void setStackSize(int size){
|
||||
if(size < 0)
|
||||
size = 1;
|
||||
else if(size > 64)
|
||||
size = 64;
|
||||
item.stackSize = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the item damage of this item. For tools this is the durability for other items the color and more.
|
||||
*/
|
||||
public int getItemDamage(){
|
||||
return item.getItemDamage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value The value to be set as item damage. For tools this is the durability for other items the color and more.
|
||||
*/
|
||||
public void setItemDamage(int value){
|
||||
item.setItemDamage(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key The key of this NBTTag
|
||||
* @param value The value to be stored. Can be a Number or String
|
||||
*/
|
||||
public void setTag(String key, Object value){
|
||||
if(value instanceof Number)
|
||||
getTag().setDouble(key, ((Number) value).doubleValue());
|
||||
else if(value instanceof String)
|
||||
getTag().setString(key, (String) value);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key The key of the NBTTag
|
||||
* @return Returns whether or not the key exists
|
||||
*/
|
||||
public boolean hasTag(String key){
|
||||
if(item.stackTagCompound == null)
|
||||
return false;
|
||||
return getTag().hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key The key of the NBTTag
|
||||
* @return Returns the value associated with the key. Returns null of it doesnt exist
|
||||
*/
|
||||
public Object getTag(String key){
|
||||
if(item.stackTagCompound == null)
|
||||
return null;
|
||||
|
||||
NBTBase tag = getTag().getTag(key);
|
||||
if(tag == null)
|
||||
return null;
|
||||
if(tag instanceof NBTPrimitive)
|
||||
return ((NBTPrimitive)tag).func_150286_g();
|
||||
if(tag instanceof NBTTagString)
|
||||
return ((NBTTagString)tag).func_150285_a_();
|
||||
return tag;
|
||||
}
|
||||
|
||||
public INbt removeTags() {
|
||||
ScriptNbt nbt = (ScriptNbt) NpcAPI.Instance().getINbt(item.stackTagCompound);
|
||||
item.stackTagCompound = null;
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public boolean isEnchanted(){
|
||||
return item.isItemEnchanted();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.7.10d
|
||||
* @param id The enchantment id
|
||||
* @return
|
||||
*/
|
||||
public boolean hasEnchant(int id){
|
||||
if(!isEnchanted())
|
||||
return false;
|
||||
NBTTagList list = item.getEnchantmentTagList();
|
||||
for(int i = 0; i < list.tagCount(); i++){
|
||||
NBTTagCompound compound = list.getCompoundTagAt(i);
|
||||
if(compound.getShort("id") == id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addEnchant(int id, int strength) {
|
||||
Enchantment ench = Enchantment.enchantmentsList[id];
|
||||
if (ench == null) {
|
||||
throw new CustomNPCsException("Unknown enchant id:" + id, new Object[0]);
|
||||
} else {
|
||||
this.item.addEnchantment(ench, strength);
|
||||
}
|
||||
}
|
||||
|
||||
public void setAttribute(String name, double value) {
|
||||
NBTTagCompound compound = this.item.getTagCompound();
|
||||
if (compound == null) {
|
||||
this.item.setTagCompound(compound = new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagList nbttaglist = compound.getTagList("AttributeModifiers", 10);
|
||||
NBTTagList newList = new NBTTagList();
|
||||
|
||||
NBTTagCompound nbttagcompound;
|
||||
for(int i = 0; i < nbttaglist.tagCount(); ++i) {
|
||||
nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
||||
if (!nbttagcompound.getString("AttributeName").equals(name)) {
|
||||
newList.appendTag(nbttagcompound);
|
||||
}
|
||||
}
|
||||
|
||||
if (value != 0.0D) {
|
||||
AttributeModifier attributeModifier = new AttributeModifier(name, value, 0);
|
||||
nbttagcompound = new NBTTagCompound();
|
||||
nbttagcompound.setString("Name", attributeModifier.getName());
|
||||
nbttagcompound.setDouble("Amount", attributeModifier.getAmount());
|
||||
nbttagcompound.setInteger("Operation", attributeModifier.getOperation());
|
||||
nbttagcompound.setLong("UUIDMost", attributeModifier.getID().getMostSignificantBits());
|
||||
nbttagcompound.setLong("UUIDLeast", attributeModifier.getID().getLeastSignificantBits());
|
||||
nbttagcompound.setString("AttributeName", name);
|
||||
newList.appendTag(nbttagcompound);
|
||||
}
|
||||
|
||||
compound.setTag("AttributeModifiers", newList);
|
||||
}
|
||||
|
||||
public double getAttribute(String name) {
|
||||
NBTTagCompound compound = this.item.getTagCompound();
|
||||
if (compound == null) {
|
||||
return 0.0D;
|
||||
} else {
|
||||
Multimap<String, AttributeModifier> map = this.item.getAttributeModifiers();
|
||||
Iterator var4 = map.entries().iterator();
|
||||
|
||||
while(var4.hasNext()) {
|
||||
Map.Entry entry = (Map.Entry)var4.next();
|
||||
if (((String)entry.getKey()).equals(name)) {
|
||||
AttributeModifier mod = (AttributeModifier)entry.getValue();
|
||||
return mod.getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
return 0.0D;
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getLore() {
|
||||
if (this.item.getTagCompound() != null) {
|
||||
NBTTagCompound compound = this.item.getTagCompound().getCompoundTag("display");
|
||||
if(compound != null && compound.func_150299_b("Lore") == 9) {
|
||||
NBTTagList nbttaglist = compound.getTagList("Lore", 8);
|
||||
if (nbttaglist.tagCount() > 0) {
|
||||
List<String> lore = new ArrayList();
|
||||
|
||||
for (int i = 0; i < nbttaglist.tagCount(); ++i) {
|
||||
lore.add(nbttaglist.getStringTagAt(i));
|
||||
}
|
||||
|
||||
return (String[]) ((String[]) lore.toArray(new String[lore.size()]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
public boolean hasLore() {
|
||||
if (this.item.getTagCompound() != null) {
|
||||
NBTTagCompound compound = this.item.getTagCompound().getCompoundTag("display");
|
||||
if (compound != null && compound.func_150299_b("Lore") == 9) {
|
||||
NBTTagList nbttaglist = compound.getTagList("Lore", 8);
|
||||
return nbttaglist.tagCount() > 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setLore(String[] lore) {
|
||||
NBTTagCompound compound = this.item.getTagCompound();
|
||||
if (compound == null) {
|
||||
this.item.setTagCompound(compound = new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagList nbttaglist = compound.getTagList("display", 10);
|
||||
NBTTagList newList = new NBTTagList();
|
||||
String[] var4 = lore;
|
||||
int var5 = lore.length;
|
||||
|
||||
for(int var6 = 0; var6 < var5; ++var6) {
|
||||
String s = var4[var6];
|
||||
newList.appendTag(new NBTTagString(s));
|
||||
}
|
||||
|
||||
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
||||
nbttagcompound.setTag("Lore", newList);
|
||||
compound.setTag("display", nbttagcompound);
|
||||
}
|
||||
|
||||
public boolean hasAttribute(String name) {
|
||||
NBTTagCompound compound = this.item.getTagCompound();
|
||||
if (compound == null) {
|
||||
return false;
|
||||
} else {
|
||||
NBTTagList nbttaglist = compound.getTagList("AttributeModifiers", 10);
|
||||
|
||||
for(int i = 0; i < nbttaglist.tagCount(); ++i) {
|
||||
NBTTagCompound c = nbttaglist.getCompoundTagAt(i);
|
||||
if (c.getString("AttributeName").equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.7.10d
|
||||
* @return Returns whether this item is a book
|
||||
*/
|
||||
public boolean isWrittenBook(){
|
||||
return item.getItem() == Items.written_book || item.getItem() == Items.writable_book;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.7.10d
|
||||
* @return Returns the books title
|
||||
*/
|
||||
public String getBookTitle(){
|
||||
return item.getTagCompound().getString("title");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.7.10d
|
||||
* @return Returns the books author
|
||||
*/
|
||||
public String getBookAuthor(){
|
||||
return item.getTagCompound().getString("author");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.7.10d
|
||||
* @return If the item is a book, returns a string array with book pages
|
||||
*/
|
||||
public String[] getBookText(){
|
||||
if(!isWrittenBook())
|
||||
return null;
|
||||
List<String> list = new ArrayList<String>();
|
||||
NBTTagList pages = item.getTagCompound().getTagList("pages", 8);
|
||||
for (int i = 0; i < pages.tagCount(); ++i){
|
||||
list.add(pages.getStringTagAt(i));
|
||||
}
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
private NBTTagCompound getTag(){
|
||||
if(item.stackTagCompound == null)
|
||||
item.stackTagCompound = new NBTTagCompound();
|
||||
return item.stackTagCompound;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns whether or not this item is a block
|
||||
*/
|
||||
public boolean isBlock(){
|
||||
Block block = Block.getBlockFromItem(item.getItem());
|
||||
if(block == null || block == Blocks.air)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isFood() {
|
||||
return item.getItem() instanceof ItemFood;
|
||||
}
|
||||
|
||||
public int getFoodPoints() {
|
||||
if (isFood()) return ((ItemFood)item.getItem()).func_150905_g(item);
|
||||
return -1;
|
||||
}
|
||||
|
||||
public INbt getNbt() {
|
||||
NBTTagCompound compound = this.item.getTagCompound();
|
||||
if(compound == null) {
|
||||
this.item.setTagCompound(compound = new NBTTagCompound());
|
||||
}
|
||||
|
||||
return NpcAPI.Instance().getINbt(compound);
|
||||
}
|
||||
|
||||
public INbt getItemNbt() {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
this.item.writeToNBT(compound);
|
||||
return NpcAPI.Instance().getINbt(compound);
|
||||
}
|
||||
|
||||
public NBTTagCompound getMCNbt() {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
this.item.writeToNBT(compound);
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void setMCNbt(NBTTagCompound compound) {}
|
||||
|
||||
/**
|
||||
* No support is given for this method. Dont use if you dont know what you are doing.
|
||||
* @return Minecraft ItemStack
|
||||
*/
|
||||
public ItemStack getMCItemStack() {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user