summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-20 20:51:11 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-20 20:51:11 +0100
commit3e1ce1482d7e74a7edeb9cd70d0edecf08480565 (patch)
treeeb4ccacc6158e494ec6dc2bc708b31cb07f9eb47
parent67b776ea3c9af5f0df8ac2fe0664c6075f0dbf2a (diff)
downloadnoctis-3e1ce1482d7e74a7edeb9cd70d0edecf08480565.tar.bz2
Add NBT helper, Player helper, and use them to allow catalysts to be bound to players
-rw-r--r--src/main/java/net/flarn/noctis/item/ItemCatalyst.java98
-rw-r--r--src/main/java/net/flarn/noctis/utils/NBTHelper.java70
-rw-r--r--src/main/java/net/flarn/noctis/utils/PlayerHelper.java17
3 files changed, 178 insertions, 7 deletions
diff --git a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
index 3c95fb0..d2038a2 100644
--- a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
+++ b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
@@ -1,19 +1,55 @@
package net.flarn.noctis.item;
+import com.mojang.authlib.GameProfile;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.flarn.noctis.refs.Names;
import net.flarn.noctis.refs.Textures;
+import net.flarn.noctis.utils.LogHelper;
+import net.flarn.noctis.utils.NBTHelper;
+import net.flarn.noctis.utils.PlayerHelper;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
+import net.minecraft.entity.Entity;
+import net.minecraft.entity.item.EntityItem;
+import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
+import net.minecraft.server.MinecraftServer;
import net.minecraft.util.IIcon;
+import net.minecraft.world.World;
import java.util.List;
+import java.util.UUID;
public class ItemCatalyst extends ItemNoctisBase {
+ public static enum CatalystType {
+ LIGHT(0),
+ DARK(1),
+ MIXED(2);
+
+ private final int typeIndex;
+ private CatalystType(int idx) {
+ this.typeIndex = idx;
+ }
+
+ public int getIndex() { return this.typeIndex; }
+
+ public static CatalystType fromIndex(int idx) {
+ switch (idx) {
+ case 0:
+ return LIGHT;
+ case 1:
+ return DARK;
+ case 2:
+ return MIXED;
+ }
+ return LIGHT;
+ }
+
+ }
+
@SideOnly(Side.CLIENT)
private IIcon[] icons;
@@ -31,23 +67,30 @@ public class ItemCatalyst extends ItemNoctisBase {
@Override
public String getUnlocalizedName(ItemStack itemStack) {
- int dmg = itemStack.getItemDamage();
- if (dmg < 0 || dmg > Names.Items.CATALYSTS.length - 1) { dmg = 0; }
- return String.format("item.%s%s", Textures.RESOURCE_PREFIX, Names.Items.CATALYSTS[dmg]);
+ CatalystType ctype = CatalystType.fromIndex(NBTHelper.getInt(itemStack, "type", 0));
+ return String.format("item.%s%s", Textures.RESOURCE_PREFIX, Names.Items.CATALYSTS[ctype.getIndex()]);
}
@Override
public void getSubItems(Item item, CreativeTabs creativeTab, List list) {
for (int meta = 0; meta < Names.Items.CATALYSTS.length; ++meta) {
- list.add(new ItemStack(this, 1, meta));
+ ItemStack s = new ItemStack(this, 1, 0);
+ NBTHelper.setInt(s, "type", meta);
+ list.add(s);
}
}
@Override
@SideOnly(Side.CLIENT)
- public IIcon getIconFromDamage(int meta) {
- if (meta < 0 || meta > Names.Items.CATALYSTS.length - 1) { meta = 0; }
- return icons[meta];
+ public IIcon getIconIndex(ItemStack itemStack) {
+ CatalystType ctype = CatalystType.fromIndex(NBTHelper.getInt(itemStack, "type", 0));
+ return icons[ctype.getIndex()];
+ }
+
+ @Override
+ @SideOnly(Side.CLIENT)
+ public IIcon getIcon(ItemStack stack, int pass) {
+ return getIconIndex(stack);
}
@Override
@@ -59,4 +102,45 @@ public class ItemCatalyst extends ItemNoctisBase {
}
}
+ @Override
+ public boolean onEntityItemUpdate(EntityItem entityItem) {
+ // We return true only if we do not want normal entity update behaviour (which we do)
+ if (entityItem.age % 20 == 0) {
+ LogHelper.warn("Ruhroh! " + entityItem + " is on the floor!");
+ }
+ return false;
+ }
+
+ @Override
+ public ItemStack onItemRightClick(ItemStack s, World w, EntityPlayer p)
+ {
+ if (!NBTHelper.hasEntry(s, "owner")) {
+ NBTHelper.setUUID(s, "owner", p.getUniqueID());
+ LogHelper.warn("Set ownership of " + s + " to " + p.getUniqueID());
+ } else {
+ LogHelper.warn("Owner already set to " + NBTHelper.getUUID(s, "owner"));
+ LogHelper.warn("Player ID is " + p.getUniqueID());
+ }
+
+ return s;
+ }
+
+ @Override
+ @SideOnly(Side.CLIENT)
+ public void addInformation(ItemStack s, EntityPlayer p, List strs, boolean b) {
+ strs.add("");
+ if (!NBTHelper.hasEntry(s, "owner")) {
+ strs.add("This catalyst seems dull and lifeless");
+ } else {
+ UUID ownerid = NBTHelper.getUUID(s, "owner");
+ strs.add("This catalyst thrums with the power of " + PlayerHelper.getPlayerNameFromUUID(ownerid));
+ }
+ }
+
+ @Override
+ @SideOnly(Side.CLIENT)
+ public boolean hasEffect(ItemStack s, int pass) {
+ return NBTHelper.hasEntry(s, "owner");
+ }
+
}
diff --git a/src/main/java/net/flarn/noctis/utils/NBTHelper.java b/src/main/java/net/flarn/noctis/utils/NBTHelper.java
new file mode 100644
index 0000000..ed5217f
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/utils/NBTHelper.java
@@ -0,0 +1,70 @@
+package net.flarn.noctis.utils;
+
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NBTTagCompound;
+
+import java.util.UUID;
+
+// Based loosely off pahimar's EE3 helper, but rewritten because EE3 is GPL
+public class NBTHelper {
+
+ private static NBTTagCompound COMP(ItemStack s) {
+ if (s.stackTagCompound == null) {
+ s.setTagCompound(new NBTTagCompound());
+ }
+ return s.stackTagCompound;
+ }
+
+ public static void removeEntry(ItemStack s, String keyname) {
+ if (s.stackTagCompound != null) {
+ s.stackTagCompound.removeTag(keyname);
+ }
+ }
+
+ public static boolean hasEntry(ItemStack s, String keyname) {
+ return (s.stackTagCompound != null) && (s.stackTagCompound.hasKey(keyname));
+ }
+
+ public static void setLong(ItemStack s, String keyname, long value) { COMP(s).setLong(keyname, value); }
+ public static long getLong(ItemStack s, String keyname, long def) { return hasEntry(s, keyname) ? COMP(s).getLong(keyname) : def; }
+
+ public static void setString(ItemStack s, String keyname, String value) { COMP(s).setString(keyname, value); }
+ public static String getString(ItemStack s, String keyname, String def) { return hasEntry(s, keyname) ? COMP(s).getString(keyname) : def; }
+
+ public static void setBoolean(ItemStack s, String keyname, boolean value) { COMP(s).setBoolean(keyname, value); }
+ public static boolean getBoolean(ItemStack s, String keyname, boolean def) { return hasEntry(s, keyname) ? COMP(s).getBoolean(keyname) : def; }
+
+ public static void setByte(ItemStack s, String keyname, byte value) { COMP(s).setByte(keyname, value); }
+ public static byte getByte(ItemStack s, String keyname, byte def) { return hasEntry(s, keyname) ? COMP(s).getByte(keyname) : def; }
+
+ public static void setShort(ItemStack s, String keyname, short value) { COMP(s).setShort(keyname, value); }
+ public static short getShort(ItemStack s, String keyname, short def) { return hasEntry(s, keyname) ? COMP(s).getShort(keyname) : def; }
+
+ public static void setInt(ItemStack s, String keyname, int value) { COMP(s).setInteger(keyname, value); }
+ public static int getInt(ItemStack s, String keyname, int def) { return hasEntry(s, keyname) ? COMP(s).getInteger(keyname) : def; }
+
+ public static void setFloat(ItemStack s, String keyname, float value) { COMP(s).setFloat(keyname, value); }
+ public static float getFloat(ItemStack s, String keyname, float def) { return hasEntry(s, keyname) ? COMP(s).getFloat(keyname) : def; }
+
+ public static void setDouble(ItemStack s, String keyname, double value) { COMP(s).setDouble(keyname, value); }
+ public static double getDouble(ItemStack s, String keyname, double def) { return hasEntry(s, keyname) ? COMP(s).getDouble(keyname) : def; }
+
+ public static void setTagCompound(ItemStack s, String keyname, NBTTagCompound value) { COMP(s).setTag(keyname, value); }
+ public static NBTTagCompound getTagCompound(ItemStack s, String keyname) { if (!hasEntry(s, keyname)) { setTagCompound(s, keyname, new NBTTagCompound()); } return COMP(s).getCompoundTag(keyname); }
+
+ public static void setUUID(ItemStack s, String keyname, UUID id) {
+ NBTTagCompound utag = getTagCompound(s, keyname);
+ utag.setLong("high", id.getMostSignificantBits());
+ utag.setLong("low", id.getLeastSignificantBits());
+ utag.setString("str", id.toString());
+ }
+
+ public static UUID getUUID(ItemStack s, String keyname) {
+ if (!hasEntry(s, keyname)) { return null; }
+ NBTTagCompound utag = getTagCompound(s, keyname);
+ UUID ret = UUID.fromString(utag.getString("str"));
+ return ret;
+ }
+
+}
diff --git a/src/main/java/net/flarn/noctis/utils/PlayerHelper.java b/src/main/java/net/flarn/noctis/utils/PlayerHelper.java
new file mode 100644
index 0000000..fde7638
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/utils/PlayerHelper.java
@@ -0,0 +1,17 @@
+package net.flarn.noctis.utils;
+
+
+import com.mojang.authlib.GameProfile;
+import net.minecraft.server.MinecraftServer;
+
+import java.util.UUID;
+
+public class PlayerHelper {
+
+ public static String getPlayerNameFromUUID(UUID id) {
+ MinecraftServer server = MinecraftServer.getServer();
+ GameProfile profile = server.func_152358_ax().func_152652_a(id);
+ return profile.getName();
+ }
+
+}