Allow the renaming of items and tools by using a nametag in a tool station/forge

This commit is contained in:
Bernhard Bonigl committed 2015-02-26 22:26:12 +01:00
1 parent ab98ec2b3a
commit 3b8777f8eb
4 files changed
+54 -5

No files matched your search

@@ -2,12 +2,14 @@ package tconstruct.tools.inventory;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.MinecraftForge;
import tconstruct.library.event.ToolCraftedEvent;
import tconstruct.library.modifier.IModifyable;
import tconstruct.tools.logic.ToolStationLogic;
public class SlotTool extends Slot
{
@@ -52,7 +54,7 @@ public class SlotTool extends Slot
*/
protected void onCrafting (ItemStack stack)
{
if (stack.getItem() instanceof IModifyable)
if (stack.getItem() instanceof IModifyable && inventory.getStackInSlot(1) != null && !(inventory.getStackInSlot(1).getItem() instanceof IModifyable))
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag(((IModifyable) stack.getItem()).getBaseTagName());
Boolean full = (inventory.getStackInSlot(2) != null || inventory.getStackInSlot(3) != null);
@@ -70,6 +72,13 @@ public class SlotTool extends Slot
{
int amount = inventory.getStackInSlot(1).stackSize;
inventory.decrStackSize(1, amount);
for(int i = 0; i < inventory.getSizeInventory(); i++) {
if(inventory.getStackInSlot(i) != null && inventory.getStackInSlot(i).getItem() == Items.name_tag) {
inventory.decrStackSize(i, 1);
break;
}
}
}
}
}
@@ -2,6 +2,7 @@ package tconstruct.tools.inventory;
import net.minecraft.block.Block;
import net.minecraft.entity.player.*;
import net.minecraft.init.Items;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -95,8 +96,18 @@ public class ToolForgeContainer extends ToolStationContainer
else
//Simply naming items
{
ItemStack stack2 = logic.getStackInSlot(1);
int amount = logic.getStackInSlot(1).stackSize;
logic.decrStackSize(1, amount);
if(!ToolStationLogic.canRename(stack2.getTagCompound(), stack2)) {
for(int i = 0; i < logic.getSizeInventory(); i++) {
if(logic.getStackInSlot(i) != null && logic.getStackInSlot(i).getItem() == Items.name_tag) {
logic.decrStackSize(i, 1);
break;
}
}
}
}
}
@@ -3,6 +3,7 @@ package tconstruct.tools.inventory;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.entity.player.*;
import net.minecraft.init.Items;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -152,8 +153,18 @@ public class ToolStationContainer extends ActiveContainer
else
//Simply naming items
{
ItemStack stack2 = logic.getStackInSlot(1);
int amount = logic.getStackInSlot(1).stackSize;
logic.decrStackSize(1, amount);
if(!ToolStationLogic.canRename(stack2.getTagCompound(), stack2)) {
for(int i = 0; i < logic.getSizeInventory(); i++) {
if(logic.getStackInSlot(i) != null && logic.getStackInSlot(i).getItem() == Items.name_tag) {
logic.decrStackSize(i, 1);
break;
}
}
}
}
}
@@ -2,7 +2,9 @@ package tconstruct.tools.logic;
import mantle.blocks.abstracts.InventoryLogic;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
@@ -118,8 +120,9 @@ public class ToolStationLogic extends InventoryLogic implements ISidedInventory
else
temp = inventory[1].copy();
if (temp == null || !(temp.getItem() instanceof ToolCore))
return null; // output as well as inventory is null or not a tool :(
if (temp == null) {
return null; // output is null or not a tool :(
}
NBTTagCompound tags = temp.getTagCompound();
if (tags == null)
@@ -136,8 +139,19 @@ public class ToolStationLogic extends InventoryLogic implements ISidedInventory
if (display == null)
return output;
if (display.hasKey("Name") && !display.getString("Name").equals("\u00A7f" + ToolBuilder.defaultToolName(temp)))
// no default name anymore
boolean doRename = false;
if (canRename(display, temp)) {
doRename = true;
}
// we only allow renaming with a nametag otherwise
else if (!("\u00A7f" + name).equals(display.getString("Name")) && !name.equals(display.getString("Name"))) {
for(int i = 0; i < inventory.length; i++)
if(inventory[i] != null && inventory[i].getItem() == Items.name_tag)
doRename = true;
}
if(!doRename)
return output;
String dName = temp.getItem() instanceof IModifyable ? "\u00A7f" + name : name;
@@ -194,4 +208,8 @@ public class ToolStationLogic extends InventoryLogic implements ISidedInventory
public void closeInventory ()
{
}
public static boolean canRename(NBTTagCompound tags, ItemStack tool) {
return !tags.hasKey("Name") || tags.getString("Name").equals("\u00A7f" + ToolBuilder.defaultToolName(tool));
}
}