diff --git a/src/main/java/com/hbm/handler/ArmorModHandler.java b/src/main/java/com/hbm/handler/ArmorModHandler.java index 10e6519b5..48bba930f 100644 --- a/src/main/java/com/hbm/handler/ArmorModHandler.java +++ b/src/main/java/com/hbm/handler/ArmorModHandler.java @@ -2,8 +2,10 @@ package com.hbm.handler; import com.hbm.items.armor.ItemArmorMod; +import net.minecraft.item.Item; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; public class ArmorModHandler { @@ -16,6 +18,18 @@ public class ArmorModHandler { public static final int kevlar = 6; public static final int plating = 7; + //The key for the NBTTagCompound that holds the armor mods + public static final String MOD_COMPOUND_KEY = "ntm_armor_mods"; + //The key for the specific slot inside the armor mod NBT Tag + public static final String MOD_SLOT_KEY = "mod_slot_"; + + /** + * Checks if a mod can be applied to an armor piece + * Needs to be used to prevent people from inserting invalid items into the armor table + * @param armor + * @param mod + * @return + */ public static boolean isApplicable(ItemStack armor, ItemStack mod) { if(armor == null || mod == null) @@ -33,4 +47,104 @@ public class ArmorModHandler { return (type == 0 && aMod.helmet) || (type == 1 && aMod.chestplate) || (type == 2 && aMod.leggings) || (type == 3 && aMod.boots); } + + /** + * Applies an mod to the given armor piece + * Make sure to check for applicability first + * Will override present mods so make sure to only use unmodded armor pieces + * @param armor + * @param mod + */ + public static void applyMod(ItemStack armor, ItemStack mod) { + + if(!armor.hasTagCompound()) + armor.stackTagCompound = new NBTTagCompound(); + + NBTTagCompound nbt = armor.getTagCompound(); + + if(!nbt.hasKey(MOD_COMPOUND_KEY)) + nbt.setTag(MOD_COMPOUND_KEY, new NBTTagCompound()); + + NBTTagCompound mods = nbt.getCompoundTag(MOD_COMPOUND_KEY); + + ItemArmorMod aMod = (ItemArmorMod)mod.getItem(); + int slot = aMod.type; + + mods.setString(MOD_SLOT_KEY + slot, Item.itemRegistry.getNameForObject(mod.getItem())); + } + + /** + * Removes the mod from the given slot + * @param armor + * @param slot + */ + public static void removeMod(ItemStack armor, int slot) { + + if(armor == null) + return; + + if(!armor.hasTagCompound()) + armor.stackTagCompound = new NBTTagCompound(); + + NBTTagCompound nbt = armor.getTagCompound(); + + if(!nbt.hasKey(MOD_COMPOUND_KEY)) + nbt.setTag(MOD_COMPOUND_KEY, new NBTTagCompound()); + + NBTTagCompound mods = nbt.getCompoundTag(MOD_COMPOUND_KEY); + mods.removeTag(MOD_SLOT_KEY + slot); + + if(mods.hasNoTags()) + clearMods(armor); + } + + /** + * Removes ALL mods + * Should be used when the armor piece is put in the armor table slot AFTER the armor pieces have been separated + * @param armor + */ + public static void clearMods(ItemStack armor) { + + if(!armor.hasTagCompound()) + return; + + NBTTagCompound nbt = armor.getTagCompound(); + nbt.removeTag(MOD_COMPOUND_KEY); + } + + /** + * Does what the name implies + * @param armor + * @return + */ + public static boolean hasMods(ItemStack armor) { + + if(!armor.hasTagCompound()) + return false; + + NBTTagCompound nbt = armor.getTagCompound(); + return nbt.hasKey(MOD_COMPOUND_KEY); + } + + public static ItemStack[] pryMods(ItemStack armor) { + + ItemStack[] slots = new ItemStack[8]; + + if(!hasMods(armor)) + return slots; + + NBTTagCompound nbt = armor.getTagCompound(); + NBTTagCompound mods = nbt.getCompoundTag(MOD_COMPOUND_KEY); + + for(int i = 0; i < 8; i++) { + + String mod = mods.getString(MOD_SLOT_KEY + i); + Item item = (Item)Item.itemRegistry.getObject(mod); + + if(item != null) + slots[i] = new ItemStack(item); + } + + return slots; + } } diff --git a/src/main/java/com/hbm/handler/HazmatRegistry.java b/src/main/java/com/hbm/handler/HazmatRegistry.java index 9c93d6e1d..ee3ef4d52 100644 --- a/src/main/java/com/hbm/handler/HazmatRegistry.java +++ b/src/main/java/com/hbm/handler/HazmatRegistry.java @@ -2,6 +2,7 @@ package com.hbm.handler; import java.util.HashMap; +import com.hbm.items.special.ItemCladding; import com.hbm.potion.HbmPotion; import net.minecraft.entity.player.EntityPlayer; @@ -37,6 +38,16 @@ public class HazmatRegistry { if(stack.hasTagCompound() && stack.stackTagCompound.getFloat("hfr_cladding") > 0) return stack.stackTagCompound.getFloat("hfr_cladding"); + if(ArmorModHandler.hasMods(stack)) { + + ItemStack[] mods = ArmorModHandler.pryMods(stack); + ItemStack cladding = mods[ArmorModHandler.cladding]; + + if(cladding != null && cladding.getItem() instanceof ItemCladding) { + return ((ItemCladding)cladding.getItem()).rad; + } + } + return 0; } diff --git a/src/main/java/com/hbm/inventory/container/ContainerArmorTable.java b/src/main/java/com/hbm/inventory/container/ContainerArmorTable.java index 482307157..d15c45b61 100644 --- a/src/main/java/com/hbm/inventory/container/ContainerArmorTable.java +++ b/src/main/java/com/hbm/inventory/container/ContainerArmorTable.java @@ -31,11 +31,44 @@ public class ContainerArmorTable extends Container { this.addSlotToContainer(new Slot(armor, 0, 44, 63) { + @Override public boolean isItemValid(ItemStack stack) { return stack.getItem() instanceof ItemArmor; } - - public void onSlotChanged() { + + @Override + public void putStack(ItemStack stack) { + + //when inserting a new armor piece, unload all mods to display + if(stack != null) { + ItemStack[] mods = ArmorModHandler.pryMods(stack); + + for(int i = 0; i < 8; i++) { + + if(mods != null) + upgrades.setInventorySlotContents(i, mods[i]); + } + + } + + super.putStack(stack); + } + + @Override + public void onPickupFromSlot(EntityPlayer player, ItemStack stack) { + super.onPickupFromSlot(player, stack); + + //if the armor piece is taken, absorb all armor pieces + + for(int i = 0; i < 8; i++) { + + ItemStack mod = upgrades.getStackInSlot(i); + + //ideally, this should always return true so long as the mod slot is not null due to the insert restriction + if(ArmorModHandler.isApplicable(stack, mod)) { + upgrades.setInventorySlotContents(i, null); + } + } } }); @@ -59,7 +92,8 @@ public class ContainerArmorTable extends Container { public boolean canInteractWith(EntityPlayer player) { return true; } - + + @Override public void onContainerClosed(EntityPlayer player) { super.onContainerClosed(player); @@ -69,8 +103,15 @@ public class ContainerArmorTable extends Container { if(itemstack != null) { player.dropPlayerItemWithRandomChoice(itemstack, false); + ArmorModHandler.removeMod(armor.getStackInSlot(0), i); } } + + ItemStack itemstack = this.armor.getStackInSlotOnClosing(0); + + if(itemstack != null) { + player.dropPlayerItemWithRandomChoice(itemstack, false); + } } } @@ -80,8 +121,25 @@ public class ContainerArmorTable extends Container { super(inventory, index, x, y); } + @Override public boolean isItemValid(ItemStack stack) { - return armor.getStackInSlot(0) != null && stack.getItem() instanceof ItemArmorMod && ((ItemArmorMod)stack.getItem()).type == this.slotNumber; + return armor.getStackInSlot(0) != null && ArmorModHandler.isApplicable(armor.getStackInSlot(0), stack) && ((ItemArmorMod)stack.getItem()).type == this.slotNumber; + } + + @Override + public void putStack(ItemStack stack) { + super.putStack(stack); + + if(stack != null) { + if(ArmorModHandler.isApplicable(armor.getStackInSlot(0), stack)) + ArmorModHandler.applyMod(armor.getStackInSlot(0), stack); + } + } + + public void onPickupFromSlot(EntityPlayer player, ItemStack stack) { + super.onPickupFromSlot(player, stack); + + ArmorModHandler.removeMod(armor.getStackInSlot(0), this.slotNumber); } } } diff --git a/src/main/java/com/hbm/items/special/ItemCladding.java b/src/main/java/com/hbm/items/special/ItemCladding.java index 0d107699b..9a3bc4d54 100644 --- a/src/main/java/com/hbm/items/special/ItemCladding.java +++ b/src/main/java/com/hbm/items/special/ItemCladding.java @@ -11,7 +11,7 @@ import net.minecraft.util.EnumChatFormatting; public class ItemCladding extends ItemArmorMod { - float rad; + public float rad; public ItemCladding(float rad) { super(ArmorModHandler.cladding, true, true, true, true); @@ -21,6 +21,6 @@ public class ItemCladding extends ItemArmorMod { @Override public void addInformation(ItemStack itemstack, EntityPlayer player, List list, boolean bool) { - list.add(EnumChatFormatting.YELLOW + "Adds " + rad + " rad-resistance to all armor pieces."); + list.add(EnumChatFormatting.YELLOW + "Adds " + rad + " rad-resistance to armor pieces."); } } diff --git a/src/main/java/com/hbm/main/ModEventHandlerClient.java b/src/main/java/com/hbm/main/ModEventHandlerClient.java index 05be5f818..711bfddad 100644 --- a/src/main/java/com/hbm/main/ModEventHandlerClient.java +++ b/src/main/java/com/hbm/main/ModEventHandlerClient.java @@ -8,12 +8,14 @@ import org.lwjgl.opengl.GL11; import com.hbm.entity.mob.EntityHunterChopper; import com.hbm.entity.projectile.EntityChopperMine; import com.hbm.extprop.HbmLivingProps; +import com.hbm.handler.ArmorModHandler; import com.hbm.handler.HTTPHandler; import com.hbm.handler.HazmatRegistry; import com.hbm.interfaces.IHoldableWeapon; import com.hbm.interfaces.IItemHUD; import com.hbm.interfaces.Spaghetti; import com.hbm.inventory.RecipesCommon.ComparableStack; +import com.hbm.inventory.gui.GUIArmorTable; import com.hbm.items.ModItems; import com.hbm.items.armor.ArmorFSB; import com.hbm.items.armor.ArmorFSBPowered; @@ -53,6 +55,7 @@ import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.entity.RenderPlayer; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; @@ -323,7 +326,7 @@ public class ModEventHandlerClient { float rad = HazmatRegistry.getResistance(stack); - rad = ((int)(rad * 100)) / 100F; + rad = ((int)(rad * 1000)) / 1000F; if(rad > 0) list.add(EnumChatFormatting.YELLOW + I18nUtil.resolveKey("trait.radResistance", rad)); @@ -343,6 +346,28 @@ public class ModEventHandlerClient { if(entry.entry == EnumEntryType.MULT) list.add(EnumChatFormatting.GOLD + "Adds multiplier " + entry.value + " to the custom nuke stage " + entry.type); } + + if(stack.getItem() instanceof ItemArmor && ArmorModHandler.hasMods(stack)) { + + if(!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && !(Minecraft.getMinecraft().currentScreen instanceof GUIArmorTable)) { + + list.add(EnumChatFormatting.DARK_GRAY + "" + EnumChatFormatting.ITALIC +"Hold <" + + EnumChatFormatting.YELLOW + "" + EnumChatFormatting.ITALIC + "LSHIFT" + + EnumChatFormatting.DARK_GRAY + "" + EnumChatFormatting.ITALIC + "> to display installed armor mods"); + + } else { + + list.add(EnumChatFormatting.YELLOW + "Mods:"); + + ItemStack[] mods = ArmorModHandler.pryMods(stack); + + for(int i = 0; i < 8; i++) { + + if(mods[i] != null) + list.add(" " + EnumChatFormatting.DARK_RED + mods[i].getDisplayName()); + } + } + } } @SideOnly(Side.CLIENT) diff --git a/src/main/java/com/hbm/render/util/RenderOverhead.java b/src/main/java/com/hbm/render/util/RenderOverhead.java index 26055d3ae..8352163f3 100644 --- a/src/main/java/com/hbm/render/util/RenderOverhead.java +++ b/src/main/java/com/hbm/render/util/RenderOverhead.java @@ -14,7 +14,7 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.boss.IBossDisplayData; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityXPOrb; -import net.minecraft.entity.monster.EntityMob; +import net.minecraft.entity.monster.IMob; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.AxisAlignedBB; @@ -27,12 +27,10 @@ public class RenderOverhead { GL11.glAlphaFunc(GL11.GL_GREATER, 0.1F); if(shouldRenderTag(living)) { - float f = 1.6F; double distSq = living.getDistanceSqToEntity(thePlayer); float range = living.isSneaking() ? renderer.NAME_TAG_RANGE_SNEAK : renderer.NAME_TAG_RANGE; if(distSq < (double) (range * range)) { - String s = name; drawTagAware(living, x, y, z, name, depthTest); } } @@ -130,7 +128,7 @@ public class RenderOverhead { if(ent instanceof IBossDisplayData) tess.setColorOpaque_F(1F, 0.5F, 0F); - else if(ent instanceof EntityMob) + else if(ent instanceof IMob) tess.setColorOpaque_F(1F, 0F, 0F); else if(ent instanceof EntityPlayer) tess.setColorOpaque_F(1F, 0F, 1F);