diff --git a/build.gradle b/build.gradle index a1ad84b..56870df 100644 --- a/build.gradle +++ b/build.gradle @@ -20,19 +20,18 @@ repositories { apply plugin: 'net.minecraftforge.gradle.forge' -version = "1.10.2-2.0.0" -group= "com.mcf.davidee.nbtedit" +version = "1.11.2-2.0.0" +group = "com.mcf.davidee.nbtedit" archivesBaseName = "NBTEdit" -sourceCompatibility = 1.7 -targetCompatibility = 1.7 +sourceCompatibility = targetCompatibility = 1.7 idea { module { inheritOutputDirs = true } } minecraft { - version = "1.10.2-12.18.1.2011" + version = "1.11-13.19.0.2153" runDir = "run" - mappings = "snapshot_20160518" + mappings = "snapshot_20161111" //makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. } diff --git a/src/main/java/com/mcf/davidee/nbtedit/CommandNBTEdit.java b/src/main/java/com/mcf/davidee/nbtedit/CommandNBTEdit.java index f357b64..8822f68 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/CommandNBTEdit.java +++ b/src/main/java/com/mcf/davidee/nbtedit/CommandNBTEdit.java @@ -17,15 +17,16 @@ public class CommandNBTEdit extends CommandBase { public String getCommandName() { return "nbtedit"; } + @Override - public String getCommandUsage(ICommandSender par1ICommandSender) { + public String getCommandUsage(ICommandSender sender) { return "/nbtedit OR /nbtedit OR /nbtedit "; } @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if (sender instanceof EntityPlayerMP) { - EntityPlayerMP player = (EntityPlayerMP)sender; + EntityPlayerMP player = (EntityPlayerMP) sender; if (args.length == 3) { int x = parseInt(args[0]); @@ -36,7 +37,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args } else if (args.length == 1) { int entityID = (args[0].equalsIgnoreCase("me")) ? player.getEntityId() : parseInt(args[0], 0); - NBTEdit.log(Level.TRACE, sender.getName() + " issued command \"/nbtedit " + entityID + "\""); + NBTEdit.log(Level.TRACE, sender.getName() + " issued command \"/nbtedit " + entityID + "\""); NBTEdit.NETWORK.sendEntity(player, entityID); } else if (args.length == 0) { @@ -45,7 +46,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args } else { String s = ""; - for (int i =0; i < args.length; ++i) { + for (int i = 0; i < args.length; ++i) { s += args[i]; if (i != args.length - 1) s += " "; diff --git a/src/main/java/com/mcf/davidee/nbtedit/NBTEdit.java b/src/main/java/com/mcf/davidee/nbtedit/NBTEdit.java index a86ee57..a33618c 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/NBTEdit.java +++ b/src/main/java/com/mcf/davidee/nbtedit/NBTEdit.java @@ -28,9 +28,9 @@ @Mod(modid = NBTEdit.MODID, name = NBTEdit.NAME, version = NBTEdit.VERSION, acceptableRemoteVersions = "*") public class NBTEdit { - public static final String MODID = "NBTEdit"; + public static final String MODID = "nbtedit"; public static final String NAME = "In-game NBTEdit"; - public static final String VERSION = "1.10.2-2.0.0"; + public static final String VERSION = "1.11.2-2.0.1"; public static final NBTNodeSorter SORTER = new NBTNodeSorter(); public static final PacketHandler NETWORK = new PacketHandler(); @@ -39,6 +39,7 @@ public class NBTEdit { public static NamedNBT clipboard = null; public static boolean opOnly = true; + public static boolean editOtherPlayers = false; @Instance(MODID) private static NBTEdit instance; @@ -53,6 +54,7 @@ public void preInit(FMLPreInitializationEvent event) { Configuration config = new Configuration(event.getSuggestedConfigurationFile()); config.load(); opOnly = config.get("General", "opOnly", true, "true if only Ops can NBTEdit; false allows users in creative mode to NBTEdit").getBoolean(true); + editOtherPlayers = config.get("General", "editOtherPlayers", false, "true if editing players other then your self is allowed. false by default. USE AT YOUR OWN RISK").getBoolean(false); if (config.hasChanged()) { config.save(); } @@ -83,8 +85,7 @@ public void preInit(FMLPreInitializationEvent event) { @EventHandler public void init(FMLInitializationEvent event) { logger.trace("NBTEdit Initalized"); - saves = new SaveStates(new File(new File(proxy.getMinecraftDirectory(),"saves"), "NBTEdit.dat")); - //DISPATCHER.initialize(); + saves = new SaveStates(new File(new File(proxy.getMinecraftDirectory(), "saves"), "NBTEdit.dat")); NETWORK.initialize(); } @@ -95,7 +96,7 @@ public void postInit(FMLPostInitializationEvent event) { @EventHandler public void serverStarting(FMLServerStartingEvent event) { - MinecraftServer server= event.getServer(); + MinecraftServer server = event.getServer(); ServerCommandManager serverCommandManager = (ServerCommandManager) server.getCommandManager(); serverCommandManager.registerCommand(new CommandNBTEdit()); logger.trace("Server Starting -- Added \"/nbtedit\" command"); @@ -110,16 +111,17 @@ public static void throwing(String cls, String mthd, Throwable thr) { } static final String SEP = System.getProperty("line.separator"); + public static void logTag(NBTTagCompound tag) { NBTTree tree = new NBTTree(tag); String sb = ""; - for (String s : tree.toStrings()){ - sb += SEP + "\t\t\t"+ s; + for (String s : tree.toStrings()) { + sb += SEP + "\t\t\t" + s; } NBTEdit.log(Level.TRACE, sb); } - public static SaveStates getSaveStates(){ + public static SaveStates getSaveStates() { return instance.saves; } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/NBTHelper.java b/src/main/java/com/mcf/davidee/nbtedit/NBTHelper.java index c8bfa54..a5e44e3 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/NBTHelper.java +++ b/src/main/java/com/mcf/davidee/nbtedit/NBTHelper.java @@ -14,19 +14,19 @@ import java.util.Map; public class NBTHelper { - + public static NBTTagCompound nbtRead(DataInputStream in) throws IOException { return CompressedStreamTools.read(in); } - + public static void nbtWrite(NBTTagCompound compound, DataOutput out) throws IOException { CompressedStreamTools.write(compound, out); } - - public static Map getMap(NBTTagCompound tag){ + + public static Map getMap(NBTTagCompound tag) { return ReflectionHelper.getPrivateValue(NBTTagCompound.class, tag, 0); } - + public static NBTBase getTagAt(NBTTagList tag, int index) { List list = ReflectionHelper.getPrivateValue(NBTTagList.class, tag, 1); return list.get(index); diff --git a/src/main/java/com/mcf/davidee/nbtedit/NBTStringHelper.java b/src/main/java/com/mcf/davidee/nbtedit/NBTStringHelper.java index 5bd7081..6e37ce1 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/NBTStringHelper.java +++ b/src/main/java/com/mcf/davidee/nbtedit/NBTStringHelper.java @@ -21,7 +21,7 @@ public class NBTStringHelper { public static final char SECTION_SIGN = '\u00A7'; - public static String getNBTName(NamedNBT namedNBT){ + public static String getNBTName(NamedNBT namedNBT) { String name = namedNBT.getName(); NBTBase obj = namedNBT.getNBT(); @@ -29,7 +29,7 @@ public static String getNBTName(NamedNBT namedNBT){ return Strings.isNullOrEmpty(name) ? "" + s : name + ": " + s; } - public static String getNBTNameSpecial(NamedNBT namedNBT){ + public static String getNBTNameSpecial(NamedNBT namedNBT) { String name = namedNBT.getName(); NBTBase obj = namedNBT.getNBT(); @@ -37,103 +37,102 @@ public static String getNBTNameSpecial(NamedNBT namedNBT){ return Strings.isNullOrEmpty(name) ? "" + s : name + ": " + s + SECTION_SIGN + 'r'; } - public static NBTBase newTag(byte type){ - switch (type) - { - case 0: - return new NBTTagEnd(); - case 1: - return new NBTTagByte((byte) 0); - case 2: - return new NBTTagShort(); - case 3: - return new NBTTagInt(0); - case 4: - return new NBTTagLong(0); - case 5: - return new NBTTagFloat(0); - case 6: - return new NBTTagDouble(0); - case 7: - return new NBTTagByteArray(new byte[0]); - case 8: - return new NBTTagString(""); - case 9: - return new NBTTagList(); - case 10: - return new NBTTagCompound(); - case 11: - return new NBTTagIntArray(new int[0]); - default: - return null; + public static NBTBase newTag(byte type) { + switch (type) { + case 0: + return new NBTTagEnd(); + case 1: + return new NBTTagByte((byte) 0); + case 2: + return new NBTTagShort(); + case 3: + return new NBTTagInt(0); + case 4: + return new NBTTagLong(0); + case 5: + return new NBTTagFloat(0); + case 6: + return new NBTTagDouble(0); + case 7: + return new NBTTagByteArray(new byte[0]); + case 8: + return new NBTTagString(""); + case 9: + return new NBTTagList(); + case 10: + return new NBTTagCompound(); + case 11: + return new NBTTagIntArray(new int[0]); + default: + return null; } } public static String toString(NBTBase base) { - switch(base.getId()) { - case 1: - return "" + ((NBTTagByte)base).getByte(); - case 2: - return "" + ((NBTTagShort)base).getShort(); - case 3: - return "" + ((NBTTagInt)base).getInt(); - case 4: - return "" + ((NBTTagLong)base).getLong(); - case 5: - return "" + ((NBTTagFloat)base).getFloat(); - case 6: - return "" + ((NBTTagDouble)base).getDouble(); - case 7: - return base.toString(); - case 8: - return ((NBTTagString)base).getString(); - case 9: - return "(TagList)"; - case 10: - return "(TagCompound)"; - case 11: - return base.toString(); - default: - return "?"; + switch (base.getId()) { + case 1: + return "" + ((NBTTagByte) base).getByte(); + case 2: + return "" + ((NBTTagShort) base).getShort(); + case 3: + return "" + ((NBTTagInt) base).getInt(); + case 4: + return "" + ((NBTTagLong) base).getLong(); + case 5: + return "" + ((NBTTagFloat) base).getFloat(); + case 6: + return "" + ((NBTTagDouble) base).getDouble(); + case 7: + return base.toString(); + case 8: + return ((NBTTagString) base).getString(); + case 9: + return "(TagList)"; + case 10: + return "(TagCompound)"; + case 11: + return base.toString(); + default: + return "?"; } } - public static String getButtonName(byte id){ - switch(id){ - case 1 : - return "Byte"; - case 2: - return "Short"; - case 3: - return "Int"; - case 4: - return "Long"; - case 5: - return "Float"; - case 6: - return "Double"; - case 7: - return "Byte[]"; - case 8: - return "String"; - case 9: - return "List"; - case 10: - return "Compound"; - case 11: - return "Int[]"; - case 12: - return "Edit"; - case 13: - return "Delete"; - case 14: - return "Copy"; - case 15: - return "Cut"; - case 16: - return "Paste"; - default: - return "Unknown"; + public static String getButtonName(byte id) { + switch (id) { + case 1: + return "Byte"; + case 2: + return "Short"; + case 3: + return "Int"; + case 4: + return "Long"; + case 5: + return "Float"; + case 6: + return "Double"; + case 7: + return "Byte[]"; + case 8: + return "String"; + case 9: + return "List"; + case 10: + return "Compound"; + case 11: + return "Int[]"; + case 12: + return "Edit"; + case 13: + return "Delete"; + case 14: + return "Copy"; + case 15: + return "Cut"; + case 16: + return "Paste"; + default: + return "Unknown"; } } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/TileEntityHelper.java b/src/main/java/com/mcf/davidee/nbtedit/TileEntityHelper.java index 8a4fa9b..1ed294a 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/TileEntityHelper.java +++ b/src/main/java/com/mcf/davidee/nbtedit/TileEntityHelper.java @@ -9,24 +9,24 @@ import net.minecraft.tileentity.TileEntity; public class TileEntityHelper { - - public static void copyData(T from, T into) throws Exception{ + + public static void copyData(T from, T into) throws Exception { Class clazz = from.getClass(); - Set fields = asSet(clazz.getFields(),clazz.getDeclaredFields()); + Set fields = asSet(clazz.getFields(), clazz.getDeclaredFields()); Field modifiers = Field.class.getDeclaredField("modifiers"); modifiers.setAccessible(true); - for (Field field : fields){ + for (Field field : fields) { field.setAccessible(true); modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(into, field.get(from)); } } - - public static Set asSet(Field[] a, Field[] b){ + + public static Set asSet(Field[] a, Field[] b) { HashSet s = new HashSet<>(); Collections.addAll(s, a); Collections.addAll(s, b); return s; } - + } diff --git a/src/main/java/com/mcf/davidee/nbtedit/forge/ClientProxy.java b/src/main/java/com/mcf/davidee/nbtedit/forge/ClientProxy.java index e55818c..da72baf 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/forge/ClientProxy.java +++ b/src/main/java/com/mcf/davidee/nbtedit/forge/ClientProxy.java @@ -64,7 +64,7 @@ public void run() { } }); } - + @Override public void openEditGUI(final BlockPos pos, final NBTTagCompound tag) { Minecraft.getMinecraft().addScheduledTask(new Runnable() { @@ -85,13 +85,13 @@ public void sendMessage(EntityPlayer player, String message, TextFormatting colo @SubscribeEvent public void renderWorldLast(RenderWorldLastEvent event) { GuiScreen curScreen = Minecraft.getMinecraft().currentScreen; - if (curScreen instanceof GuiEditNBTTree){ - GuiEditNBTTree screen = (GuiEditNBTTree)curScreen; + if (curScreen instanceof GuiEditNBTTree) { + GuiEditNBTTree screen = (GuiEditNBTTree) curScreen; Entity e = screen.getEntity(); - + if (e != null && e.isEntityAlive()) drawBoundingBox(event.getContext(), event.getPartialTicks(), e.getEntityBoundingBox()); - else if (screen.isTileEntity()){ + else if (screen.isTileEntity()) { int x = screen.getBlockX(); int y = screen.y; int z = screen.z; @@ -129,9 +129,9 @@ private void drawBoundingBox(RenderGlobal r, float f, AxisAlignedBB aabb) { Entity player = Minecraft.getMinecraft().getRenderViewEntity(); - double var8 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double)f; - double var10 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double)f; - double var12 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double)f; + double var8 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) f; + double var10 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) f; + double var12 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) f; aabb = aabb.addCoord(-var8, -var10, -var12); diff --git a/src/main/java/com/mcf/davidee/nbtedit/forge/CommonProxy.java b/src/main/java/com/mcf/davidee/nbtedit/forge/CommonProxy.java index df1fdee..7003b25 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/forge/CommonProxy.java +++ b/src/main/java/com/mcf/davidee/nbtedit/forge/CommonProxy.java @@ -12,19 +12,16 @@ public class CommonProxy { public void registerInformation() { - } public File getMinecraftDirectory() { return new File("."); } - + public void openEditGUI(int entityID, NBTTagCompound tag) { - } - + public void openEditGUI(BlockPos pos, NBTTagCompound tag) { - } public void sendMessage(EntityPlayer player, String message, TextFormatting color) { diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/CharacterFilter.java b/src/main/java/com/mcf/davidee/nbtedit/gui/CharacterFilter.java index 2185021..567364f 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/CharacterFilter.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/CharacterFilter.java @@ -6,13 +6,13 @@ public class CharacterFilter { public static String filerAllowedCharacters(String str, boolean section) { - StringBuilder sb = new StringBuilder(); - char[] arr = str.toCharArray(); + StringBuilder sb = new StringBuilder(); + char[] arr = str.toCharArray(); for (char c : arr) { if (ChatAllowedCharacters.isAllowedCharacter(c) || (section && (c == NBTStringHelper.SECTION_SIGN || c == '\n'))) sb.append(c); } - return sb.toString(); - } + return sb.toString(); + } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiCharacterButton.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiCharacterButton.java index 1e1c3da..150d150 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiCharacterButton.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiCharacterButton.java @@ -4,9 +4,7 @@ import net.minecraft.client.gui.Gui; import net.minecraft.client.renderer.GlStateManager; -public class GuiCharacterButton extends Gui{ - - +public class GuiCharacterButton extends Gui { public static final int WIDTH = 14, HEIGHT = 14; private Minecraft mc = Minecraft.getMinecraft(); @@ -15,15 +13,16 @@ public class GuiCharacterButton extends Gui{ private boolean enabled; - public GuiCharacterButton(byte id, int x, int y){ + public GuiCharacterButton(byte id, int x, int y) { this.id = id; - this.x = x; + this.x = x; this.y = y; } - public void draw(int mx, int my){ + + public void draw(int mx, int my) { mc.renderEngine.bindTexture(GuiNBTNode.WIDGET_TEXTURE); - if(inBounds(mx,my)) - Gui.drawRect(x, y, x+WIDTH, y+HEIGHT, 0x80ffffff); + if (inBounds(mx, my)) + Gui.drawRect(x, y, x + WIDTH, y + HEIGHT, 0x80ffffff); if (enabled) { GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); @@ -31,16 +30,16 @@ public void draw(int mx, int my){ drawTexturedModalRect(x, y, id * WIDTH, 27, WIDTH, HEIGHT); } - - public void setEnabled(boolean aFlag){ + + public void setEnabled(boolean aFlag) { enabled = aFlag; } - - public boolean inBounds(int mx, int my){ + + public boolean inBounds(int mx, int my) { return enabled && mx >= x && my >= y && mx < x + WIDTH && my < y + HEIGHT; } - - public byte getId(){ + + public byte getId() { return id; } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBT.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBT.java index 1bb071e..b8c32a3 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBT.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBT.java @@ -24,10 +24,10 @@ import com.mcf.davidee.nbtedit.nbt.Node; import com.mcf.davidee.nbtedit.nbt.ParseHelper; -public class GuiEditNBT extends Gui{ +public class GuiEditNBT extends Gui { public static final ResourceLocation WINDOW_TEXTURE = new ResourceLocation("nbtedit", "textures/gui/window.png"); - + public static final int WIDTH = 178, HEIGHT = 93; private Minecraft mc = Minecraft.getMinecraft(); @@ -39,32 +39,32 @@ public class GuiEditNBT extends Gui{ private int x, y; - private GuiTextField key, value; + private GuiTextField key, value; private GuiButton save, cancel; private String kError, vError; private GuiCharacterButton newLine, section; - public GuiEditNBT(GuiNBTTree parent, Node node, boolean editText, boolean editValue){ + public GuiEditNBT(GuiNBTTree parent, Node node, boolean editText, boolean editValue) { this.parent = parent; this.node = node; this.nbt = node.getObject().getNBT(); canEditText = editText; canEditValue = editValue; } - - public void initGUI(int x, int y){ - this.x=x; - this.y=y; - - section = new GuiCharacterButton((byte)0,x+WIDTH-1,y+34); - newLine = new GuiCharacterButton((byte)1,x+WIDTH-1,y+50); + + public void initGUI(int x, int y) { + this.x = x; + this.y = y; + + section = new GuiCharacterButton((byte) 0, x + WIDTH - 1, y + 34); + newLine = new GuiCharacterButton((byte) 1, x + WIDTH - 1, y + 50); String sKey = (key == null) ? node.getObject().getName() : key.getText(); String sValue = (value == null) ? getValue(nbt) : value.getText(); - this.key = new GuiTextField(mc.fontRendererObj,x+46,y+18,116,15,false); - this.value = new GuiTextField(mc.fontRendererObj,x+46,y+44,116,15,true); - + this.key = new GuiTextField(mc.fontRendererObj, x + 46, y + 18, 116, 15, false); + this.value = new GuiTextField(mc.fontRendererObj, x + 46, y + 44, 116, 15, true); + key.setText(sKey); key.setEnableBackgroundDrawing(false); key.func_82265_c(canEditText); @@ -72,8 +72,8 @@ public void initGUI(int x, int y){ value.setText(sValue); value.setEnableBackgroundDrawing(false); value.func_82265_c(canEditValue); - save = new GuiButton(1,x+9,y+62,75,20,"Save"); - if(!key.isFocused() && !value.isFocused()){ + save = new GuiButton(1, x + 9, y + 62, 75, 20, "Save"); + if (!key.isFocused() && !value.isFocused()) { if (canEditText) key.setFocused(true); else if (canEditValue) @@ -81,31 +81,29 @@ else if (canEditValue) } section.setEnabled(value.isFocused()); newLine.setEnabled(value.isFocused()); - cancel = new GuiButton(0,x+93,y+62,75,20,"Cancel"); + cancel = new GuiButton(0, x + 93, y + 62, 75, 20, "Cancel"); } - public void click(int mx, int my){ - if (newLine.inBounds(mx, my) && value.isFocused()){ + public void click(int mx, int my) { + if (newLine.inBounds(mx, my) && value.isFocused()) { value.writeText("\n"); checkValidInput(); - } - else if (section.inBounds(mx,my) && value.isFocused()){ + } else if (section.inBounds(mx, my) && value.isFocused()) { value.writeText("" + NBTStringHelper.SECTION_SIGN); checkValidInput(); - } - else{ + } else { key.mouseClicked(mx, my, 0); value.mouseClicked(mx, my, 0); - if(save.mousePressed(mc, mx, my)) + if (save.mousePressed(mc, mx, my)) saveAndQuit(); - if(cancel.mousePressed(mc, mx, my)) + if (cancel.mousePressed(mc, mx, my)) parent.closeWindow(); section.setEnabled(value.isFocused()); newLine.setEnabled(value.isFocused()); } } - - private void saveAndQuit(){ + + private void saveAndQuit() { if (canEditText) node.getObject().setName(key.getText()); setValidValue(node, value.getText()); @@ -113,16 +111,16 @@ private void saveAndQuit(){ parent.closeWindow(); } - public void draw(int mx, int my){ + public void draw(int mx, int my) { //GL11.glBindTexture(GL11.GL_TEXTURE_2D, mc.renderEngine.getTexture("/nbtedit_textures/nbteditwindow.png")); mc.renderEngine.bindTexture(WINDOW_TEXTURE); - + GL11.glColor4f(1, 1, 1, 1); - drawTexturedModalRect(x,y,0,0,WIDTH,HEIGHT); + drawTexturedModalRect(x, y, 0, 0, WIDTH, HEIGHT); if (!canEditText) - drawRect(x+42, y+15, x+169, y+31, 0x80000000); - if(!canEditValue) - drawRect(x+42, y+41, x+169, y+57, 0x80000000); + drawRect(x + 42, y + 15, x + 169, y + 31, 0x80000000); + if (!canEditValue) + drawRect(x + 42, y + 41, x + 169, y + 57, 0x80000000); key.drawTextBox(); value.drawTextBox(); @@ -130,83 +128,78 @@ public void draw(int mx, int my){ cancel.drawButton(mc, mx, my); if (kError != null) - drawCenteredString(mc.fontRendererObj, kError, x+WIDTH/2, y+4, 0xFF0000); + drawCenteredString(mc.fontRendererObj, kError, x + WIDTH / 2, y + 4, 0xFF0000); if (vError != null) - drawCenteredString(mc.fontRendererObj,vError,x+WIDTH/2,y+32,0xFF0000); + drawCenteredString(mc.fontRendererObj, vError, x + WIDTH / 2, y + 32, 0xFF0000); newLine.draw(mx, my); section.draw(mx, my); } - + public void drawCenteredString(FontRenderer par1FontRenderer, String par2Str, int par3, int par4, int par5) { par1FontRenderer.drawString(par2Str, par3 - par1FontRenderer.getStringWidth(par2Str) / 2, par4, par5); } - + public void update() { value.updateCursorCounter(); key.updateCursorCounter(); } - + public void keyTyped(char c, int i) { - if (i == Keyboard.KEY_ESCAPE){ + if (i == Keyboard.KEY_ESCAPE) { parent.closeWindow(); - } - else if (i == Keyboard.KEY_TAB){ - if (key.isFocused() && canEditValue){ + } else if (i == Keyboard.KEY_TAB) { + if (key.isFocused() && canEditValue) { key.setFocused(false); value.setFocused(true); - } - else if (value.isFocused() && canEditText){ + } else if (value.isFocused() && canEditText) { key.setFocused(true); value.setFocused(false); } section.setEnabled(value.isFocused()); newLine.setEnabled(value.isFocused()); - } - else if (i == Keyboard.KEY_RETURN) { + } else if (i == Keyboard.KEY_RETURN) { checkValidInput(); if (save.enabled) saveAndQuit(); - } - else{ + } else { key.textboxKeyTyped(c, i); value.textboxKeyTyped(c, i); checkValidInput(); } } - - private void checkValidInput(){ + + private void checkValidInput() { boolean valid = true; kError = null; vError = null; - if (canEditText && !validName()){ + if (canEditText && !validName()) { valid = false; kError = "Duplicate Tag Name"; } try { - validValue(value.getText(),nbt.getId()); + validValue(value.getText(), nbt.getId()); valid &= true; - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { vError = e.getMessage(); valid = false; } save.enabled = valid; } - - private boolean validName(){ - for (Node node : this.node.getParent().getChildren()){ + + private boolean validName() { + for (Node node : this.node.getParent().getChildren()) { NBTBase base = node.getObject().getNBT(); if (base != nbt && node.getObject().getName().equals(key.getText())) return false; } return true; } - - private static void setValidValue(Node node, String value){ + + private static void setValidValue(Node node, String value) { NamedNBT named = node.getObject(); NBTBase base = named.getNBT(); - + if (base instanceof NBTTagByte) named.setNBT(new NBTTagByte(ParseHelper.parseByte(value))); if (base instanceof NBTTagShort) @@ -215,67 +208,67 @@ private static void setValidValue(Node node, String value){ named.setNBT(new NBTTagInt(ParseHelper.parseInt(value))); if (base instanceof NBTTagLong) named.setNBT(new NBTTagLong(ParseHelper.parseLong(value))); - if(base instanceof NBTTagFloat) + if (base instanceof NBTTagFloat) named.setNBT(new NBTTagFloat(ParseHelper.parseFloat(value))); - if(base instanceof NBTTagDouble) + if (base instanceof NBTTagDouble) named.setNBT(new NBTTagDouble(ParseHelper.parseDouble(value))); - if(base instanceof NBTTagByteArray) + if (base instanceof NBTTagByteArray) named.setNBT(new NBTTagByteArray(ParseHelper.parseByteArray(value))); - if(base instanceof NBTTagIntArray) + if (base instanceof NBTTagIntArray) named.setNBT(new NBTTagIntArray(ParseHelper.parseIntArray(value))); if (base instanceof NBTTagString) named.setNBT(new NBTTagString(value)); } - private static void validValue(String value, byte type) throws NumberFormatException{ - switch(type){ - case 1: - ParseHelper.parseByte(value); - break; - case 2: - ParseHelper.parseShort(value); - break; - case 3: - ParseHelper.parseInt(value); - break; - case 4: - ParseHelper.parseLong(value); - break; - case 5: - ParseHelper.parseFloat(value); - break; - case 6: - ParseHelper.parseDouble(value); - break; - case 7: - ParseHelper.parseByteArray(value); - break; - case 11: - ParseHelper.parseIntArray(value); - break; + private static void validValue(String value, byte type) throws NumberFormatException { + switch (type) { + case 1: + ParseHelper.parseByte(value); + break; + case 2: + ParseHelper.parseShort(value); + break; + case 3: + ParseHelper.parseInt(value); + break; + case 4: + ParseHelper.parseLong(value); + break; + case 5: + ParseHelper.parseFloat(value); + break; + case 6: + ParseHelper.parseDouble(value); + break; + case 7: + ParseHelper.parseByteArray(value); + break; + case 11: + ParseHelper.parseIntArray(value); + break; } } - - private static String getValue(NBTBase base){ - switch(base.getId()){ - case 7: - String s = ""; - for (byte b : ((NBTTagByteArray)base).getByteArray()){ - s += b + " "; - } - return s; - case 9: - return "TagList"; - case 10: - return "TagCompound"; - case 11: - String i = ""; - for (int a : ((NBTTagIntArray)base).getIntArray()){ - i += a + " "; - } - return i; - default: - return NBTStringHelper.toString(base); + + private static String getValue(NBTBase base) { + switch (base.getId()) { + case 7: + String s = ""; + for (byte b : ((NBTTagByteArray) base).getByteArray()) { + s += b + " "; + } + return s; + case 9: + return "TagList"; + case 10: + return "TagCompound"; + case 11: + String i = ""; + for (int a : ((NBTTagIntArray) base).getIntArray()) { + i += a + " "; + } + return i; + default: + return NBTStringHelper.toString(base); } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBTTree.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBTTree.java index fdcf3b2..7dced06 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBTTree.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiEditNBTTree.java @@ -16,27 +16,28 @@ import java.io.IOException; -public class GuiEditNBTTree extends GuiScreen{ +public class GuiEditNBTTree extends GuiScreen { public final int entityOrX, y, z; private boolean entity; protected String screenTitle; private GuiNBTTree guiTree; - public GuiEditNBTTree(int entity, NBTTagCompound tag){ - this.entity =true; + public GuiEditNBTTree(int entity, NBTTagCompound tag) { + this.entity = true; entityOrX = entity; - y =0; - z =0; - screenTitle = "NBTEdit -- EntityId #" + entityOrX; + y = 0; + z = 0; + screenTitle = "NBTEdit -- EntityId #" + entityOrX; guiTree = new GuiNBTTree(new NBTTree(tag)); } - public GuiEditNBTTree(BlockPos pos, NBTTagCompound tag){ + + public GuiEditNBTTree(BlockPos pos, NBTTagCompound tag) { this.entity = false; entityOrX = pos.getX(); this.y = pos.getY(); this.z = pos.getZ(); - screenTitle = "NBTEdit -- TileEntity at "+pos.getX()+","+pos.getY()+","+pos.getZ(); + screenTitle = "NBTEdit -- TileEntity at " + pos.getX() + "," + pos.getY() + "," + pos.getZ(); guiTree = new GuiNBTTree(new NBTTree(tag)); } @@ -44,11 +45,11 @@ public GuiEditNBTTree(BlockPos pos, NBTTagCompound tag){ public void initGui() { Keyboard.enableRepeatEvents(true); buttonList.clear(); - guiTree.initGUI(width,height,height-35); - this.buttonList.add(new GuiButton(1, width / 4 - 100, this.height -27, "Save")); - this.buttonList.add(new GuiButton(0, width *3 / 4 -100, this.height -27, "Quit")); + guiTree.initGUI(width, height, height - 35); + this.buttonList.add(new GuiButton(1, width / 4 - 100, this.height - 27, "Save")); + this.buttonList.add(new GuiButton(0, width * 3 / 4 - 100, this.height - 27, "Quit")); } - + public void onGuiClosed() { Keyboard.enableRepeatEvents(false); } @@ -57,14 +58,13 @@ protected void keyTyped(char par1, int key) { GuiEditNBT window = guiTree.getWindow(); if (window != null) window.keyTyped(par1, key); - else{ - if (key == 1){ + else { + if (key == 1) { if (guiTree.isEditingSlot()) guiTree.stopEditingSlot(); else quitWithoutSaving(); - } - else if (key == Keyboard.KEY_DELETE) + } else if (key == Keyboard.KEY_DELETE) guiTree.deleteSelected(); else if (key == Keyboard.KEY_RETURN) guiTree.editSelected(); @@ -76,45 +76,46 @@ else if (key == Keyboard.KEY_DOWN) guiTree.keyTyped(par1, key); } } + protected void mouseClicked(int x, int y, int t) throws IOException { if (guiTree.getWindow() == null) super.mouseClicked(x, y, t); if (t == 0) - guiTree.mouseClicked(x,y); + guiTree.mouseClicked(x, y); if (t == 1) - guiTree.rightClick(x,y); + guiTree.rightClick(x, y); } - + public void handleMouseInput() throws IOException { super.handleMouseInput(); int ofs = Mouse.getEventDWheel(); - if (ofs != 0){ + if (ofs != 0) { guiTree.shift((ofs >= 1) ? 6 : -6); } } - + protected void actionPerformed(GuiButton b) { - if (b.enabled){ - switch(b.id){ - case 1: - quitWithSave(); - break; - default: - quitWithoutSaving(); - break; + if (b.enabled) { + switch (b.id) { + case 1: + quitWithSave(); + break; + default: + quitWithoutSaving(); + break; } } } - + public void updateScreen() { if (!mc.thePlayer.isEntityAlive()) quitWithoutSaving(); else guiTree.updateScreen(); } - + private void quitWithSave() { if (entity) NBTEdit.NETWORK.INSTANCE.sendToServer(new EntityNBTPacket(entityOrX, guiTree.getNBTTree().toNBTTagCompound())); @@ -124,29 +125,29 @@ private void quitWithSave() { mc.setIngameFocus(); } - + private void quitWithoutSaving() { mc.displayGuiScreen(null); } - + public void drawScreen(int x, int y, float par3) { this.drawDefaultBackground(); - guiTree.draw( x, y); + guiTree.draw(x, y); this.drawCenteredString(mc.fontRendererObj, this.screenTitle, this.width / 2, 5, 16777215); if (guiTree.getWindow() == null) super.drawScreen(x, y, par3); else super.drawScreen(-1, -1, par3); } - + public boolean doesGuiPauseGame() { return true; } - + public Entity getEntity() { - return entity ? mc.theWorld.getEntityByID(entityOrX) : null; + return entity ? mc.theWorld.getEntityByID(entityOrX) : null; } - + public boolean isTileEntity() { return !entity; } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTButton.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTButton.java index 4d7c599..2bf9c6f 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTButton.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTButton.java @@ -1,68 +1,68 @@ package com.mcf.davidee.nbtedit.gui; +import com.mcf.davidee.nbtedit.NBTStringHelper; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; - -import net.minecraft.client.renderer.GlStateManager; import org.lwjgl.opengl.GL11; -import com.mcf.davidee.nbtedit.NBTStringHelper; - -import java.awt.*; +public class GuiNBTButton extends Gui { -public class GuiNBTButton extends Gui{ - public static final int WIDTH = 9, HEIGHT = 9; - + private Minecraft mc = Minecraft.getMinecraft(); - + private byte id; private int x, y; private boolean enabled; - + private long hoverTime; - - public GuiNBTButton(byte id, int x, int y){ + + public GuiNBTButton(byte id, int x, int y) { this.id = id; - this.x = x; + this.x = x; this.y = y; } - public void draw(int mx, int my){ + + public void draw(int mx, int my) { mc.renderEngine.bindTexture(GuiNBTNode.WIDGET_TEXTURE); - if(inBounds(mx,my)){//checks if the mouse is over the button - Gui.drawRect(x, y, x+WIDTH, y+HEIGHT, 0x80ffffff);//draw a grayish background + if (inBounds(mx, my)) {//checks if the mouse is over the button + Gui.drawRect(x, y, x + WIDTH, y + HEIGHT, 0x80ffffff);//draw a grayish background if (hoverTime == -1) hoverTime = System.currentTimeMillis(); - } - else + } else hoverTime = -1; GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - if(enabled) - drawTexturedModalRect(x, y, (id-1) * 9, 18, WIDTH, HEIGHT);//Draw the texture + if (enabled) + drawTexturedModalRect(x, y, (id - 1) * 9, 18, WIDTH, HEIGHT);//Draw the texture - if (hoverTime != -1 && System.currentTimeMillis() - hoverTime > 300){ - drawToolTip(mx,my); + if (hoverTime != -1 && System.currentTimeMillis() - hoverTime > 300) { + drawToolTip(mx, my); } } - private void drawToolTip(int mx, int my){ + + private void drawToolTip(int mx, int my) { String s = NBTStringHelper.getButtonName(id); int width = mc.fontRendererObj.getStringWidth(s); - drawRect(mx+4,my+7,mx+5+width,my+17, 0xff000000); - mc.fontRendererObj.drawString(s, mx+5, my+8, 0xffffff); + drawRect(mx + 4, my + 7, mx + 5 + width, my + 17, 0xff000000); + mc.fontRendererObj.drawString(s, mx + 5, my + 8, 0xffffff); } - public void setEnabled(boolean aFlag){ + + public void setEnabled(boolean aFlag) { enabled = aFlag; } - public boolean isEnabled(){ + + public boolean isEnabled() { return enabled; } - public boolean inBounds(int mx, int my){ + + public boolean inBounds(int mx, int my) { return enabled && mx >= x && my >= y && mx < x + WIDTH && my < y + HEIGHT; } - public byte getId(){ + + public byte getId() { return id; } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTNode.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTNode.java index 1fb73c4..313fac1 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTNode.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTNode.java @@ -10,12 +10,12 @@ import com.mcf.davidee.nbtedit.nbt.NamedNBT; import com.mcf.davidee.nbtedit.nbt.Node; -public class GuiNBTNode extends Gui{ - +public class GuiNBTNode extends Gui { + public static final ResourceLocation WIDGET_TEXTURE = new ResourceLocation("nbtedit", "textures/gui/widgets.png"); private Minecraft mc = Minecraft.getMinecraft(); - + private Node node; private GuiNBTTree tree; @@ -23,9 +23,9 @@ public class GuiNBTNode extends Gui{ protected int x, y; private String displayString; - - public GuiNBTNode(GuiNBTTree tree, Node node, int x, int y){ + + public GuiNBTNode(GuiNBTTree tree, Node node, int x, int y) { this.tree = tree; this.node = node; this.x = x; @@ -33,70 +33,67 @@ public GuiNBTNode(GuiNBTTree tree, Node node, int x, int y){ height = mc.fontRendererObj.FONT_HEIGHT; updateDisplay(); } - - private boolean inBounds(int mx, int my){ - return mx >= x && my >= y && mx < width+x && my < height+y; + + private boolean inBounds(int mx, int my) { + return mx >= x && my >= y && mx < width + x && my < height + y; } - - private boolean inHideShowBounds(int mx, int my){ - return mx >= x-9 && my >= y && mx < x && my < y+height; + + private boolean inHideShowBounds(int mx, int my) { + return mx >= x - 9 && my >= y && mx < x && my < y + height; } - - public boolean shouldDrawChildren(){ + + public boolean shouldDrawChildren() { return node.shouldDrawChildren(); } - - public boolean clicked(int mx, int my){ - return inBounds(mx,my); + + public boolean clicked(int mx, int my) { + return inBounds(mx, my); } - - public boolean hideShowClicked(int mx, int my){ - if(node.hasChildren() && inHideShowBounds(mx,my)){ + + public boolean hideShowClicked(int mx, int my) { + if (node.hasChildren() && inHideShowBounds(mx, my)) { node.setDrawChildren(!node.shouldDrawChildren()); return true; } return false; } - - public Node getNode(){ + + public Node getNode() { return node; } - - public void shift(int dy){ + + public void shift(int dy) { y += dy; } - - public void updateDisplay(){ + + public void updateDisplay() { displayString = NBTStringHelper.getNBTNameSpecial(node.getObject()); - width = mc.fontRendererObj.getStringWidth(displayString)+12; + width = mc.fontRendererObj.getStringWidth(displayString) + 12; } - - public void draw(int mx, int my) - { + + public void draw(int mx, int my) { boolean selected = tree.getFocused() == node; - boolean hover = inBounds(mx,my); - boolean chHover = inHideShowBounds(mx,my); + boolean hover = inBounds(mx, my); + boolean chHover = inHideShowBounds(mx, my); int color = selected ? 0xff : hover ? 16777120 : (node.hasParent()) ? 14737632 : -6250336; - + mc.renderEngine.bindTexture(WIDGET_TEXTURE); - if (selected){ + if (selected) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - Gui.drawRect(x+11, y, x+width, y+height, Integer.MIN_VALUE); + Gui.drawRect(x + 11, y, x + width, y + height, Integer.MIN_VALUE); } - if (node.hasChildren()){ + if (node.hasChildren()) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - this.drawTexturedModalRect(x-9, y, (node.shouldDrawChildren()) ? 9 : 0 ,(chHover)?height : 0,9,height); + this.drawTexturedModalRect(x - 9, y, (node.shouldDrawChildren()) ? 9 : 0, (chHover) ? height : 0, 9, height); } - + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - drawTexturedModalRect(x+1, y, (node.getObject().getNBT().getId() -1)*9, 18, 9, 9); - drawString(mc.fontRendererObj, displayString, x+11, y + (this.height - 8) / 2, color); + drawTexturedModalRect(x + 1, y, (node.getObject().getNBT().getId() - 1) * 9, 18, 9, 9); + drawString(mc.fontRendererObj, displayString, x + 11, y + (this.height - 8) / 2, color); } - + public boolean shouldDraw(int top, int bottom) { - return y+height >= top && y <= bottom; + return y + height >= top && y <= bottom; } - - } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTTree.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTTree.java index fcfc8a4..5f50509 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTTree.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiNBTTree.java @@ -49,7 +49,7 @@ public class GuiNBTTree extends Gui { private GuiNBTButton[] buttons; private final int X_GAP = 10, START_X = 10, START_Y = 30; - private final int Y_GAP = Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT+2; + private final int Y_GAP = Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT + 2; private int y, yClick, bottom, width, height, heightDiff, offset; @@ -58,19 +58,19 @@ public class GuiNBTTree extends Gui { private GuiEditNBT window; - public Node getFocused(){ + public Node getFocused() { return focused; } - public GuiSaveSlotButton getFocusedSaveSlot(){ + public GuiSaveSlotButton getFocusedSaveSlot() { return (focusedSlotIndex != -1) ? saves[focusedSlotIndex] : null; } - public NBTTree getNBTTree(){ + public NBTTree getNBTTree() { return tree; } - public GuiNBTTree(NBTTree tree){ + public GuiNBTTree(NBTTree tree) { this.tree = tree; yClick = -1; focusedSlotIndex = -1; @@ -79,71 +79,67 @@ public GuiNBTTree(NBTTree tree){ saves = new GuiSaveSlotButton[7]; } - private int getHeightDifference(){ + private int getHeightDifference() { return getContentHeight() - (bottom - START_Y + 2); } - private int getContentHeight(){ + private int getContentHeight() { return Y_GAP * nodes.size(); } - public GuiEditNBT getWindow(){ + public GuiEditNBT getWindow() { return window; } - public void initGUI(int width, int height, int bottom){ + public void initGUI(int width, int height, int bottom) { this.width = width; this.height = height; this.bottom = bottom; yClick = -1; initGUI(false); if (window != null) - window.initGUI((width-GuiEditNBT.WIDTH)/2, (height-GuiEditNBT.HEIGHT)/2); + window.initGUI((width - GuiEditNBT.WIDTH) / 2, (height - GuiEditNBT.HEIGHT) / 2); } - public void updateScreen(){ + public void updateScreen() { if (window != null) window.update(); if (focusedSlotIndex != -1) saves[focusedSlotIndex].update(); } - private void setFocused(Node toFocus){ - if (toFocus == null){ - for (GuiNBTButton b: buttons) + private void setFocused(Node toFocus) { + if (toFocus == null) { + for (GuiNBTButton b : buttons) b.setEnabled(false); - } - else if (toFocus.getObject().getNBT() instanceof NBTTagCompound){ - for (GuiNBTButton b: buttons) + } else if (toFocus.getObject().getNBT() instanceof NBTTagCompound) { + for (GuiNBTButton b : buttons) b.setEnabled(true); buttons[12].setEnabled(toFocus != tree.getRoot()); buttons[11].setEnabled(toFocus.hasParent() && !(toFocus.getParent().getObject().getNBT() instanceof NBTTagList)); buttons[13].setEnabled(true); buttons[14].setEnabled(toFocus != tree.getRoot()); buttons[15].setEnabled(NBTEdit.clipboard != null); - } - else if (toFocus.getObject().getNBT() instanceof NBTTagList){ - if (toFocus.hasChildren()){ + } else if (toFocus.getObject().getNBT() instanceof NBTTagList) { + if (toFocus.hasChildren()) { byte type = toFocus.getChildren().get(0).getObject().getNBT().getId(); - for (GuiNBTButton b: buttons) + for (GuiNBTButton b : buttons) b.setEnabled(false); - buttons[type-1].setEnabled(true); + buttons[type - 1].setEnabled(true); buttons[12].setEnabled(true); - buttons[11].setEnabled(! (toFocus.getParent().getObject().getNBT() instanceof NBTTagList)); + buttons[11].setEnabled(!(toFocus.getParent().getObject().getNBT() instanceof NBTTagList)); buttons[13].setEnabled(true); buttons[14].setEnabled(true); buttons[15].setEnabled(NBTEdit.clipboard != null && NBTEdit.clipboard.getNBT().getId() == type); - } - else - for (GuiNBTButton b: buttons) + } else + for (GuiNBTButton b : buttons) b.setEnabled(true); - buttons[11].setEnabled(! (toFocus.getParent().getObject().getNBT() instanceof NBTTagList)); + buttons[11].setEnabled(!(toFocus.getParent().getObject().getNBT() instanceof NBTTagList)); buttons[13].setEnabled(true); buttons[14].setEnabled(true); buttons[15].setEnabled(NBTEdit.clipboard != null); - } - else{ - for (GuiNBTButton b: buttons) + } else { + for (GuiNBTButton b : buttons) b.setEnabled(false); buttons[12].setEnabled(true); buttons[11].setEnabled(true); @@ -153,76 +149,77 @@ else if (toFocus.getObject().getNBT() instanceof NBTTagList){ } focused = toFocus; - if (focused != null && focusedSlotIndex != -1){ + if (focused != null && focusedSlotIndex != -1) { stopEditingSlot(); } } - public void initGUI(){ + public void initGUI() { initGUI(false); } public void initGUI(boolean shiftToFocused) { y = START_Y; nodes.clear(); - addNodes(tree.getRoot(), START_X); + addNodes(tree.getRoot(), START_X); addButtons(); addSaveSlotButtons(); - if (focused != null){ - if (!checkValidFocus(focused)){ + if (focused != null) { + if (!checkValidFocus(focused)) { setFocused(null); } } - if (focusedSlotIndex != -1){ + if (focusedSlotIndex != -1) { saves[focusedSlotIndex].startEditing(); } heightDiff = getHeightDifference(); if (heightDiff <= 0) offset = 0; - else{ + else { if (offset < -heightDiff) offset = -heightDiff; if (offset > 0) offset = 0; for (GuiNBTNode node : nodes) node.shift(offset); - if (shiftToFocused && focused != null){ + if (shiftToFocused && focused != null) { shiftTo(focused); } } } - private void addSaveSlotButtons(){ + + private void addSaveSlotButtons() { SaveStates saveStates = NBTEdit.getSaveStates(); - for (int i =0; i < 7; ++i) - saves[i] = new GuiSaveSlotButton(saveStates.getSaveState(i),width-24,31+i*25); + for (int i = 0; i < 7; ++i) + saves[i] = new GuiSaveSlotButton(saveStates.getSaveState(i), width - 24, 31 + i * 25); } - private void addButtons(){ + private void addButtons() { int x = 18, y = 4; - for (byte i = 14; i < 17; ++i){ - buttons[i-1] = new GuiNBTButton(i,x,y); - x+=15; + for (byte i = 14; i < 17; ++i) { + buttons[i - 1] = new GuiNBTButton(i, x, y); + x += 15; } x += 30; - for (byte i = 12; i < 14; ++i){ - buttons[i-1] = new GuiNBTButton(i,x,y); - x +=15; + for (byte i = 12; i < 14; ++i) { + buttons[i - 1] = new GuiNBTButton(i, x, y); + x += 15; } x = 18; y = 17; - for (byte i = 1; i < 12; ++i){ - buttons[i-1] = new GuiNBTButton(i,x,y); + for (byte i = 1; i < 12; ++i) { + buttons[i - 1] = new GuiNBTButton(i, x, y); x += 9; } } private boolean checkValidFocus(Node fc) { - for (GuiNBTNode node : nodes){ //Check all nodes - if (node.getNode() == fc){ + for (GuiNBTNode node : nodes) { //Check all nodes + if (node.getNode() == fc) { setFocused(fc); return true; } @@ -231,29 +228,29 @@ private boolean checkValidFocus(Node fc) { } private void addNodes(Node node, int x) { - nodes.add(new GuiNBTNode(this, node, x, y)); + nodes.add(new GuiNBTNode(this, node, x, y)); x += X_GAP; y += Y_GAP; - + if (node.shouldDrawChildren()) for (Node child : node.getChildren()) - addNodes(child,x); + addNodes(child, x); } - public void draw(int mx, int my){ + public void draw(int mx, int my) { int cmx = mx, cmy = my; - if (window != null){ + if (window != null) { cmx = -1; cmy = -1; } - for (GuiNBTNode node : nodes){ - if (node.shouldDraw(START_Y-1,bottom)) + for (GuiNBTNode node : nodes) { + if (node.shouldDraw(START_Y - 1, bottom)) node.draw(cmx, cmy); } - overlayBackground(0, START_Y-1, 255, 255); + overlayBackground(0, START_Y - 1, 255, 255); overlayBackground(bottom, height, 255, 255); for (GuiNBTButton but : buttons) - but.draw(cmx,cmy); + but.draw(cmx, cmy); for (GuiSaveSlotButton but : saves) but.draw(cmx, cmy); drawScrollBar(cmx, cmy); @@ -261,65 +258,57 @@ public void draw(int mx, int my){ window.draw(mx, my); } - private void drawScrollBar(int mx, int my){ - if (heightDiff > 0){ - if (Mouse.isButtonDown(0)){ - if (yClick == -1){ - if (mx >= width-20 && mx < width && my >= START_Y -1 && my < bottom){ + private void drawScrollBar(int mx, int my) { + if (heightDiff > 0) { + if (Mouse.isButtonDown(0)) { + if (yClick == -1) { + if (mx >= width - 20 && mx < width && my >= START_Y - 1 && my < bottom) { yClick = my; } - } - else { + } else { float scrollMultiplier = 1.0F; int height = getHeightDifference(); - if (height < 1) - { + if (height < 1) { height = 1; } - int length = (bottom - (START_Y-1)) * (bottom - (START_Y-1)) / getContentHeight(); + int length = (bottom - (START_Y - 1)) * (bottom - (START_Y - 1)) / getContentHeight(); if (length < 32) length = 32; - if (length > bottom - (START_Y-1) - 8) - length = bottom - (START_Y-1) - 8; - - scrollMultiplier /= (float)(this.bottom - (START_Y - 1) - length) / (float) height; + if (length > bottom - (START_Y - 1) - 8) + length = bottom - (START_Y - 1) - 8; + scrollMultiplier /= (float) (this.bottom - (START_Y - 1) - length) / (float) height; - shift( (int)((yClick-my) * scrollMultiplier)); + shift((int) ((yClick - my) * scrollMultiplier)); yClick = my; } - } - else + } else yClick = -1; - drawRect(width-20,START_Y - 1, width, bottom, Integer.MIN_VALUE); + drawRect(width - 20, START_Y - 1, width, bottom, Integer.MIN_VALUE); - int length = (bottom - (START_Y-1)) * (bottom - (START_Y-1)) / getContentHeight(); + int length = (bottom - (START_Y - 1)) * (bottom - (START_Y - 1)) / getContentHeight(); if (length < 32) length = 32; - if (length > bottom - (START_Y-1) - 8) - length = bottom - (START_Y-1) - 8; - int y = -offset * (this.bottom - (START_Y-1) - length) / heightDiff + (START_Y-1); - - if (y < START_Y-1) - y = START_Y-1; - - + if (length > bottom - (START_Y - 1) - 8) + length = bottom - (START_Y - 1) - 8; + int y = -offset * (this.bottom - (START_Y - 1) - length) / heightDiff + (START_Y - 1); + if (y < START_Y - 1) + y = START_Y - 1; // this.drawGradientRect(width-20,y,width,y+length,8421504, 12632256); //drawRect(width-20,y,width,y+length,0x80ffffff); - drawGradientRect(width-20,y,width,y+length,0x80ffffff,0x80333333); + drawGradientRect(width - 20, y, width, y + length, 0x80ffffff, 0x80333333); } } - protected void overlayBackground(int par1, int par2, int par3, int par4) - { + protected void overlayBackground(int par1, int par2, int par3, int par4) { Tessellator tessellator = Tessellator.getInstance(); VertexBuffer worldRenderer = tessellator.getBuffer(); mc.renderEngine.bindTexture(OPTIONS_BACKGROUND); @@ -328,49 +317,49 @@ protected void overlayBackground(int par1, int par2, int par3, int par4) worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR); Color color = new Color(4210752); worldRenderer.color(color.getRed(), color.getGreen(), color.getBlue(), par4); - worldRenderer.pos(0.0D, (double)par2, 0.0D).tex(0.0D, (double)((float)par2 / var6)); - worldRenderer.pos((double)this.width, (double)par2, 0.0D).tex ((double)((float)this.width / var6), (double)((float)par2 / var6)); + worldRenderer.pos(0.0D, (double) par2, 0.0D).tex(0.0D, (double) ((float) par2 / var6)); + worldRenderer.pos((double) this.width, (double) par2, 0.0D).tex((double) ((float) this.width / var6), (double) ((float) par2 / var6)); worldRenderer.color(color.getRed(), color.getGreen(), color.getBlue(), par3); - worldRenderer.pos((double)this.width, (double)par1, 0.0D).tex ((double)((float)this.width / var6), (double)((float)par1 / var6)); - worldRenderer.pos(0.0D, (double)par1, 0.0D).tex(0.0D, (double)((float)par1 / var6)); + worldRenderer.pos((double) this.width, (double) par1, 0.0D).tex((double) ((float) this.width / var6), (double) ((float) par1 / var6)); + worldRenderer.pos(0.0D, (double) par1, 0.0D).tex(0.0D, (double) ((float) par1 / var6)); tessellator.draw(); } public void mouseClicked(int mx, int my) { - if (window == null){ + if (window == null) { boolean reInit = false; - for (GuiNBTNode node : nodes){ - if (node.hideShowClicked(mx, my)){ // Check hide/show children buttons + for (GuiNBTNode node : nodes) { + if (node.hideShowClicked(mx, my)) { // Check hide/show children buttons reInit = true; if (node.shouldDrawChildren()) - offset = (START_Y+1) - (node.y) + offset; + offset = (START_Y + 1) - (node.y) + offset; break; } } - if (!reInit){ - for (GuiNBTButton button : buttons){ //Check top buttons - if (button.inBounds(mx, my)){ + if (!reInit) { + for (GuiNBTButton button : buttons) { //Check top buttons + if (button.inBounds(mx, my)) { buttonClicked(button); return; } } - for (GuiSaveSlotButton button : saves){ - if (button.inBoundsOfX(mx, my)){ + for (GuiSaveSlotButton button : saves) { + if (button.inBoundsOfX(mx, my)) { button.reset(); NBTEdit.getSaveStates().save(); mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F)); return; } - if (button.inBounds(mx, my)){ + if (button.inBounds(mx, my)) { saveButtonClicked(button); return; } } - if (my >= START_Y && mx <= width-175){ //Check actual nodes, remove focus if nothing clicked + if (my >= START_Y && mx <= width - 175) { //Check actual nodes, remove focus if nothing clicked Node newFocus = null; - for (GuiNBTNode node : nodes){ - if (node.clicked(mx, my)){ + for (GuiNBTNode node : nodes) { + if (node.clicked(mx, my)) { newFocus = node.getNode(); break; } @@ -379,60 +368,52 @@ public void mouseClicked(int mx, int my) { stopEditingSlot(); setFocused(newFocus); } - } - else + } else initGUI(); - } - else + } else window.click(mx, my); } - private void saveButtonClicked(GuiSaveSlotButton button){ - if (button.save.tag.hasNoTags()){ //Copy into save slot + private void saveButtonClicked(GuiSaveSlotButton button) { + if (button.save.tag.hasNoTags()) { //Copy into save slot Node obj = (focused == null) ? tree.getRoot() : focused; NBTBase base = obj.getObject().getNBT(); String name = obj.getObject().getName(); - if (base instanceof NBTTagList){ + if (base instanceof NBTTagList) { NBTTagList list = new NBTTagList(); tree.addChildrenToList(obj, list); button.save.tag.setTag(name, list); - } - else if (base instanceof NBTTagCompound){ + } else if (base instanceof NBTTagCompound) { NBTTagCompound compound = new NBTTagCompound(); tree.addChildrenToTag(obj, compound); button.save.tag.setTag(name, compound); - } - - else + } else button.save.tag.setTag(name, base.copy()); button.saved(); NBTEdit.getSaveStates().save(); mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F)); - } - else{ //Paste into + } else { //Paste into Map nbtMap = NBTHelper.getMap(button.save.tag); - if (nbtMap.isEmpty()){ - NBTEdit.log(Level.WARN, "Unable to copy from save \"" + button.save.name +"\"."); + if (nbtMap.isEmpty()) { + NBTEdit.log(Level.WARN, "Unable to copy from save \"" + button.save.name + "\"."); NBTEdit.log(Level.WARN, "The save is invalid - a valid save must only contain 1 core NBTBase"); - } - else{ + } else { if (focused == null) setFocused(tree.getRoot()); - - Entry firstEntry = nbtMap.entrySet().iterator().next(); + + Entry firstEntry = nbtMap.entrySet().iterator().next(); assert firstEntry != null; String name = firstEntry.getKey(); NBTBase nbt = firstEntry.getValue().copy(); - if (focused == tree.getRoot() && nbt instanceof NBTTagCompound && name.equals("ROOT")){ + if (focused == tree.getRoot() && nbt instanceof NBTTagCompound && name.equals("ROOT")) { setFocused(null); - tree = new NBTTree((NBTTagCompound)nbt); + tree = new NBTTree((NBTTagCompound) nbt); initGUI(); mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F)); - } - else if (canAddToParent(focused.getObject().getNBT(), nbt)){ + } else if (canAddToParent(focused.getObject().getNBT(), nbt)) { focused.setDrawChildren(true); - for (Iterator> it = focused.getChildren().iterator(); it.hasNext();){ //Replace object with same name - if (it.next().getObject().getName().equals(name)){ + for (Iterator> it = focused.getChildren().iterator(); it.hasNext(); ) { //Replace object with same name + if (it.next().getObject().getName().equals(name)) { it.remove(); break; } @@ -448,7 +429,7 @@ else if (canAddToParent(focused.getObject().getNBT(), nbt)){ } } - private void buttonClicked(GuiNBTButton button){ + private void buttonClicked(GuiNBTButton button) { if (button.getId() == 16) paste(); else if (button.getId() == 15) @@ -459,27 +440,25 @@ else if (button.getId() == 13) deleteSelected(); else if (button.getId() == 12) edit(); - else if (focused != null){ + else if (focused != null) { focused.setDrawChildren(true); List> children = focused.getChildren(); String type = NBTStringHelper.getButtonName(button.getId()); - if (focused.getObject().getNBT() instanceof NBTTagList){ + if (focused.getObject().getNBT() instanceof NBTTagList) { NBTBase nbt = NBTStringHelper.newTag(button.getId()); - if (nbt != null){ - Node newNode = new Node<>(focused, new NamedNBT("",nbt)); + if (nbt != null) { + Node newNode = new Node<>(focused, new NamedNBT("", nbt)); children.add(newNode); setFocused(newNode); } - } - else if (children.size() == 0){ + } else if (children.size() == 0) { setFocused(insert(type + "1", button.getId())); - } - else{ - for (int i =1; i <= children.size()+1; ++i){ + } else { + for (int i = 1; i <= children.size() + 1; ++i) { String name = type + i; - if (validName(name,children)){ - setFocused(insert(name,button.getId())); + if (validName(name, children)) { + setFocused(insert(name, button.getId())); break; } } @@ -488,17 +467,17 @@ else if (children.size() == 0){ } } - private boolean validName(String name, List> list){ + private boolean validName(String name, List> list) { for (Node node : list) if (node.getObject().getName().equals(name)) return false; return true; } - private Node insert(NamedNBT nbt){ + private Node insert(NamedNBT nbt) { Node newNode = new Node<>(focused, nbt); - if (focused.hasChildren()){ + if (focused.hasChildren()) { List> children = focused.getChildren(); boolean added = false; @@ -511,13 +490,12 @@ private Node insert(NamedNBT nbt){ } if (!added) children.add(newNode); - } - else + } else focused.addChild(newNode); return newNode; } - private Node insert(String name, byte type){ + private Node insert(String name, byte type) { NBTBase nbt = NBTStringHelper.newTag(type); if (nbt != null) return insert(new NamedNBT(name, nbt)); @@ -525,8 +503,8 @@ private Node insert(String name, byte type){ } public void deleteSelected() { - if (focused != null){ - if (tree.delete(focused)){ + if (focused != null) { + if (tree.delete(focused)) { Node oldFocused = focused; shiftFocus(true); if (focused == oldFocused) @@ -538,22 +516,20 @@ public void deleteSelected() { } public void editSelected() { - if (focused != null){ + if (focused != null) { NBTBase base = focused.getObject().getNBT(); - if (focused.hasChildren() && (base instanceof NBTTagCompound || base instanceof NBTTagList)){ + if (focused.hasChildren() && (base instanceof NBTTagCompound || base instanceof NBTTagList)) { focused.setDrawChildren(!focused.shouldDrawChildren()); int index; - - if(focused.shouldDrawChildren() && (index = indexOf(focused)) != -1) - offset = (START_Y+1) - nodes.get(index).y + offset; - + + if (focused.shouldDrawChildren() && (index = indexOf(focused)) != -1) + offset = (START_Y + 1) - nodes.get(index).y + offset; + initGUI(); - } - else if (buttons[11].isEnabled()){ + } else if (buttons[11].isEnabled()) { edit(); } - } - else if (focusedSlotIndex != -1){ + } else if (focusedSlotIndex != -1) { stopEditingSlot(); } } @@ -561,7 +537,7 @@ else if (focusedSlotIndex != -1){ private boolean canAddToParent(NBTBase parent, NBTBase child) { if (parent instanceof NBTTagCompound) return true; - if (parent instanceof NBTTagList){ + if (parent instanceof NBTTagList) { NBTTagList list = (NBTTagList) parent; return list.tagCount() == 0 || list.getTagType() == child.getId(); } @@ -571,27 +547,26 @@ private boolean canAddToParent(NBTBase parent, NBTBase child) { private boolean canPaste() { return NBTEdit.clipboard != null && focused != null && canAddToParent(focused.getObject().getNBT(), NBTEdit.clipboard.getNBT()); } - + private void paste() { - if (NBTEdit.clipboard != null){ + if (NBTEdit.clipboard != null) { focused.setDrawChildren(true); - + NamedNBT namedNBT = NBTEdit.clipboard.copy(); - if (focused.getObject().getNBT() instanceof NBTTagList){ + if (focused.getObject().getNBT() instanceof NBTTagList) { namedNBT.setName(""); Node node = new Node<>(focused, namedNBT); focused.addChild(node); tree.addChildrenToTree(node); tree.sort(node); setFocused(node); - } - else{ + } else { String name = namedNBT.getName(); List> children = focused.getChildren(); - if (!validName(name, children)){ - for (int i = 1; i <= children.size() + 1; ++i){ + if (!validName(name, children)) { + for (int i = 1; i <= children.size() + 1; ++i) { String n = name + "(" + i + ")"; - if (validName(n,children)){ + if (validName(n, children)) { namedNBT.setName(n); break; } @@ -606,75 +581,74 @@ private void paste() { initGUI(true); } } - - private void copy(){ - if (focused != null){ + + private void copy() { + if (focused != null) { NamedNBT namedNBT = focused.getObject(); - if (namedNBT.getNBT() instanceof NBTTagList){ + if (namedNBT.getNBT() instanceof NBTTagList) { NBTTagList list = new NBTTagList(); tree.addChildrenToList(focused, list); NBTEdit.clipboard = new NamedNBT(namedNBT.getName(), list); - } - else if (namedNBT.getNBT() instanceof NBTTagCompound){ + } else if (namedNBT.getNBT() instanceof NBTTagCompound) { NBTTagCompound compound = new NBTTagCompound(); tree.addChildrenToTag(focused, compound); NBTEdit.clipboard = new NamedNBT(namedNBT.getName(), compound); - } - else + } else NBTEdit.clipboard = focused.getObject().copy(); setFocused(focused); } } - private void cut(){ + private void cut() { copy(); deleteSelected(); } - private void edit(){ + private void edit() { NBTBase base = focused.getObject().getNBT(); NBTBase parent = focused.getParent().getObject().getNBT(); window = new GuiEditNBT(this, focused, !(parent instanceof NBTTagList), !(base instanceof NBTTagCompound || base instanceof NBTTagList)); - window.initGUI((width-GuiEditNBT.WIDTH)/2, (height-GuiEditNBT.HEIGHT)/2); + window.initGUI((width - GuiEditNBT.WIDTH) / 2, (height - GuiEditNBT.HEIGHT) / 2); } - public void nodeEdited(Node node){ + public void nodeEdited(Node node) { Node parent = node.getParent(); Collections.sort(parent.getChildren(), NBTEdit.SORTER); initGUI(true); } - public void arrowKeyPressed(boolean up){ + public void arrowKeyPressed(boolean up) { if (focused == null) shift((up) ? Y_GAP : -Y_GAP); else shiftFocus(up); } - private int indexOf(Node node){ - for (int i =0; i < nodes.size(); ++i){ - if (nodes.get(i).getNode() == node){ + private int indexOf(Node node) { + for (int i = 0; i < nodes.size(); ++i) { + if (nodes.get(i).getNode() == node) { return i; } } return -1; } - - private void shiftFocus(boolean up){ + + private void shiftFocus(boolean up) { int index = indexOf(focused); - if (index != -1){ + if (index != -1) { index += (up) ? -1 : 1; - if (index >= 0 && index < nodes.size()){ + if (index >= 0 && index < nodes.size()) { setFocused(nodes.get(index).getNode()); shift((up) ? Y_GAP : -Y_GAP); } } } - private void shiftTo(Node node){ + + private void shiftTo(Node node) { int index = indexOf(node); - if (index != -1){ + if (index != -1) { GuiNBTNode gui = nodes.get(index); - shift((bottom+START_Y+1)/2 - (gui.y+gui.height)); + shift((bottom + START_Y + 1) / 2 - (gui.y + gui.height)); } } @@ -687,29 +661,28 @@ public void shift(int i) { if (dif < -heightDiff) dif = -heightDiff; for (GuiNBTNode node : nodes) - node.shift(dif-offset); - offset = dif; + node.shift(dif - offset); + offset = dif; } public void closeWindow() { window = null; } - public boolean isEditingSlot(){ + public boolean isEditingSlot() { return focusedSlotIndex != -1; } - public void stopEditingSlot(){ + public void stopEditingSlot() { saves[focusedSlotIndex].stopEditing(); NBTEdit.getSaveStates().save(); focusedSlotIndex = -1; } public void keyTyped(char ch, int key) { - if (focusedSlotIndex != -1){ + if (focusedSlotIndex != -1) { saves[focusedSlotIndex].keyTyped(ch, key); - } - else{ + } else { if (key == Keyboard.KEY_C && GuiControls.isCtrlKeyDown()) copy(); if (key == Keyboard.KEY_V && GuiControls.isCtrlKeyDown() && canPaste()) @@ -720,16 +693,15 @@ public void keyTyped(char ch, int key) { } public void rightClick(int mx, int my) { - for (int i = 0; i < 7; ++i){ - if (saves[i].inBounds(mx, my)){ + for (int i = 0; i < 7; ++i) { + if (saves[i].inBounds(mx, my)) { setFocused(null); - if (focusedSlotIndex != -1){ - if (focusedSlotIndex != i){ + if (focusedSlotIndex != -1) { + if (focusedSlotIndex != i) { saves[focusedSlotIndex].stopEditing(); NBTEdit.getSaveStates().save(); - } - else //Already editing the correct one! - return; + } else //Already editing the correct one! + return; } saves[i].startEditing(); focusedSlotIndex = i; @@ -738,8 +710,7 @@ public void rightClick(int mx, int my) { } } - private void putColor(VertexBuffer renderer, int argb, int p_178988_2_) - { + private void putColor(VertexBuffer renderer, int argb, int p_178988_2_) { int i = renderer.getColorIndex(p_178988_2_); int j = argb >> 16 & 255; int k = argb >> 8 & 255; diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiSaveSlotButton.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiSaveSlotButton.java index 3c1cd5e..aa2dbcf 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiSaveSlotButton.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiSaveSlotButton.java @@ -26,7 +26,7 @@ public class GuiSaveSlotButton extends Gui { private int tickCount; - public GuiSaveSlotButton(SaveStates.SaveState save, int rightX, int y){ + public GuiSaveSlotButton(SaveStates.SaveState save, int rightX, int y) { this.save = save; this.rightX = rightX; this.y = y; @@ -38,80 +38,82 @@ public GuiSaveSlotButton(SaveStates.SaveState save, int rightX, int y){ } - public void draw(int mx, int my){ + public void draw(int mx, int my) { - int textColor = ((inBounds(mx,my))) ? 16777120 : 0xffffff; - renderVanillaButton(x,y,0,66,width,HEIGHT); - drawCenteredString(mc.fontRendererObj, text, x + width/2, y + 6, textColor); - if (tickCount != -1 && tickCount / 6 % 2 == 0){ - mc.fontRendererObj.drawStringWithShadow("_", x+(width+mc.fontRendererObj.getStringWidth(text))/2+1, y+6, 0xffffff); + int textColor = ((inBounds(mx, my))) ? 16777120 : 0xffffff; + renderVanillaButton(x, y, 0, 66, width, HEIGHT); + drawCenteredString(mc.fontRendererObj, text, x + width / 2, y + 6, textColor); + if (tickCount != -1 && tickCount / 6 % 2 == 0) { + mc.fontRendererObj.drawStringWithShadow("_", x + (width + mc.fontRendererObj.getStringWidth(text)) / 2 + 1, y + 6, 0xffffff); } - if (xVisible){ - textColor = ((inBoundsOfX(mx,my))) ? 16777120 : 0xffffff; - renderVanillaButton(leftBoundOfX(),topBoundOfX(),0,66,X_SIZE,X_SIZE); - drawCenteredString(mc.fontRendererObj, "x", x-GAP-X_SIZE/2, y + 6, textColor); + if (xVisible) { + textColor = ((inBoundsOfX(mx, my))) ? 16777120 : 0xffffff; + renderVanillaButton(leftBoundOfX(), topBoundOfX(), 0, 66, X_SIZE, X_SIZE); + drawCenteredString(mc.fontRendererObj, "x", x - GAP - X_SIZE / 2, y + 6, textColor); } } - - private void renderVanillaButton(int x, int y, int u, int v, int width, int height){ + + private void renderVanillaButton(int x, int y, int u, int v, int width, int height) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.renderEngine.bindTexture(TEXTURE); - + //Top Left - this.drawTexturedModalRect(x, y, u, v, width/2, height/2); + this.drawTexturedModalRect(x, y, u, v, width / 2, height / 2); //Top Right - this.drawTexturedModalRect(x+width/2, y, u +200 - width /2, v, width/2, height/2); + this.drawTexturedModalRect(x + width / 2, y, u + 200 - width / 2, v, width / 2, height / 2); //Bottom Left - this.drawTexturedModalRect(x, y+height/2, u, v+20-height/2, width/2, height/2); + this.drawTexturedModalRect(x, y + height / 2, u, v + 20 - height / 2, width / 2, height / 2); //Bottom Right - this.drawTexturedModalRect(x+width/2, y+height/2, u + 200-width/2, v+20-height/2, width/2, height/2); + this.drawTexturedModalRect(x + width / 2, y + height / 2, u + 200 - width / 2, v + 20 - height / 2, width / 2, height / 2); } - private int leftBoundOfX(){ + private int leftBoundOfX() { return x - X_SIZE - GAP; } - private int topBoundOfX(){ - return y + (HEIGHT-X_SIZE)/2; + + private int topBoundOfX() { + return y + (HEIGHT - X_SIZE) / 2; } - public boolean inBoundsOfX(int mx, int my){ + public boolean inBoundsOfX(int mx, int my) { int buttonX = leftBoundOfX(); int buttonY = topBoundOfX(); return xVisible && mx >= buttonX && my >= buttonY && mx < buttonX + X_SIZE && my < buttonY + X_SIZE; } - public boolean inBounds(int mx, int my){ + + public boolean inBounds(int mx, int my) { return mx >= x && my >= y && mx < x + width && my < y + HEIGHT; } - private void updatePosition(){ - width = mc.fontRendererObj.getStringWidth(text)+24; + private void updatePosition() { + width = mc.fontRendererObj.getStringWidth(text) + 24; if (width % 2 == 1) ++width; width = MathHelper.clamp_int(width, MIN_WIDTH, MAX_WIDTH); x = rightX - width; } - public void reset(){ + public void reset() { xVisible = false; save.tag = new NBTTagCompound(); - text = "Save " + save.name; + text = "Save " + save.name; updatePosition(); } public void saved() { xVisible = true; - text = "Load " + save.name; + text = "Load " + save.name; updatePosition(); } public void keyTyped(char c, int key) { - if (key == Keyboard.KEY_BACK){ + if (key == Keyboard.KEY_BACK) { backSpace(); } - if (Character.isDigit(c) || Character.isLetter(c)){ + if (Character.isDigit(c) || Character.isLetter(c)) { save.name += c; text = (save.tag.hasNoTags() ? "Save " : "Load ") + save.name; updatePosition(); @@ -119,15 +121,15 @@ public void keyTyped(char c, int key) { } - public void backSpace(){ - if (save.name.length() > 0){ - save.name = save.name.substring(0,save.name.length()-1); + public void backSpace() { + if (save.name.length() > 0) { + save.name = save.name.substring(0, save.name.length() - 1); text = (save.tag.hasNoTags() ? "Save " : "Load ") + save.name; updatePosition(); } } - public void startEditing(){ + public void startEditing() { tickCount = 0; } diff --git a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiTextField.java b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiTextField.java index f26d3e2..63277aa 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/gui/GuiTextField.java +++ b/src/main/java/com/mcf/davidee/nbtedit/gui/GuiTextField.java @@ -13,7 +13,7 @@ import com.mcf.davidee.nbtedit.NBTStringHelper; -public class GuiTextField extends Gui{ +public class GuiTextField extends Gui { private final FontRenderer fontRenderer; @@ -32,18 +32,21 @@ public class GuiTextField extends Gui{ private int field_73816_n = 0; private int cursorPosition = 0; - /** other selection position, maybe the same as the cursor */ + /** + * other selection position, maybe the same as the cursor + */ private int selectionEnd = 0; private int enabledColor = 14737632; private int disabledColor = 7368816; - /** True if this textbox is visible */ + /** + * True if this textbox is visible + */ private boolean visible = true; private boolean enableBackgroundDrawing = true; private boolean allowSection; - public GuiTextField(FontRenderer par1FontRenderer, int x, int y, int w, int h, boolean allowSection) - { + public GuiTextField(FontRenderer par1FontRenderer, int x, int y, int w, int h, boolean allowSection) { this.fontRenderer = par1FontRenderer; this.xPos = x; this.yPos = y; @@ -55,22 +58,17 @@ public GuiTextField(FontRenderer par1FontRenderer, int x, int y, int w, int h, b /** * Increments the cursor counter */ - public void updateCursorCounter() - { + public void updateCursorCounter() { ++this.cursorCounter; } /** * Sets the text of the textbox. */ - public void setText(String par1Str) - { - if (par1Str.length() > this.maxStringLength) - { + public void setText(String par1Str) { + if (par1Str.length() > this.maxStringLength) { this.text = par1Str.substring(0, this.maxStringLength); - } - else - { + } else { this.text = par1Str; } @@ -80,16 +78,14 @@ public void setText(String par1Str) /** * Returns the text beign edited on the textbox. */ - public String getText() - { + public String getText() { return this.text; } /** * @return returns the text between the cursor and selectionEnd */ - public String getSelectedtext() - { + public String getSelectedtext() { int var1 = this.cursorPosition < this.selectionEnd ? this.cursorPosition : this.selectionEnd; int var2 = this.cursorPosition < this.selectionEnd ? this.selectionEnd : this.cursorPosition; return this.text.substring(var1, var2); @@ -98,34 +94,28 @@ public String getSelectedtext() /** * replaces selected text, or inserts text at the position on the cursor */ - public void writeText(String par1Str) - { + public void writeText(String par1Str) { String var2 = ""; - String var3 = CharacterFilter.filerAllowedCharacters(par1Str,allowSection); + String var3 = CharacterFilter.filerAllowedCharacters(par1Str, allowSection); int var4 = this.cursorPosition < this.selectionEnd ? this.cursorPosition : this.selectionEnd; int var5 = this.cursorPosition < this.selectionEnd ? this.selectionEnd : this.cursorPosition; int var6 = this.maxStringLength - this.text.length() - (var4 - this.selectionEnd); - if (this.text.length() > 0) - { + if (this.text.length() > 0) { var2 = var2 + this.text.substring(0, var4); } int var8; - if (var6 < var3.length()) - { + if (var6 < var3.length()) { var2 = var2 + var3.substring(0, var6); var8 = var6; - } - else - { + } else { var2 = var2 + var3; var8 = var3.length(); } - if (this.text.length() > 0 && var5 < this.text.length()) - { + if (this.text.length() > 0 && var5 < this.text.length()) { var2 = var2 + this.text.substring(var5); } @@ -137,53 +127,40 @@ public void writeText(String par1Str) * Deletes the specified number of words starting at the cursor position. Negative numbers will delete words left of * the cursor. */ - public void deleteWords(int par1) - { - if (this.text.length() != 0) - { - if (this.selectionEnd != this.cursorPosition) - { + public void deleteWords(int par1) { + if (this.text.length() != 0) { + if (this.selectionEnd != this.cursorPosition) { this.writeText(""); - } - else - { + } else { this.deleteFromCursor(this.getNthWordFromCursor(par1) - this.cursorPosition); } } } /** - * delete the selected text, otherwsie deletes characters from either side of the cursor. params: delete num + * delete the selected text, otherwise deletes characters from either side of the cursor. params: delete num */ - public void deleteFromCursor(int par1) - { - if (this.text.length() != 0) - { - if (this.selectionEnd != this.cursorPosition) - { + public void deleteFromCursor(int par1) { + if (this.text.length() != 0) { + if (this.selectionEnd != this.cursorPosition) { this.writeText(""); - } - else - { + } else { boolean var2 = par1 < 0; int var3 = var2 ? this.cursorPosition + par1 : this.cursorPosition; int var4 = var2 ? this.cursorPosition : this.cursorPosition + par1; String var5 = ""; - if (var3 >= 0) - { + if (var3 >= 0) { var5 = this.text.substring(0, var3); } - if (var4 < this.text.length()) - { + if (var4 < this.text.length()) { var5 = var5 + this.text.substring(var4); } this.text = var5; - if (var2) - { + if (var2) { this.moveCursorBy(par1); } } @@ -193,52 +170,39 @@ public void deleteFromCursor(int par1) /** * see @getNthNextWordFromPos() params: N, position */ - public int getNthWordFromCursor(int par1) - { + public int getNthWordFromCursor(int par1) { return this.getNthWordFromPos(par1, this.getCursorPosition()); } /** * gets the position of the nth word. N may be negative, then it looks backwards. params: N, position */ - public int getNthWordFromPos(int par1, int par2) - { + public int getNthWordFromPos(int par1, int par2) { return this.func_73798_a(par1, this.getCursorPosition(), true); } - public int func_73798_a(int par1, int par2, boolean par3) - { + public int func_73798_a(int par1, int par2, boolean par3) { int var4 = par2; boolean var5 = par1 < 0; int var6 = Math.abs(par1); - for (int var7 = 0; var7 < var6; ++var7) - { - if (var5) - { - while (par3 && var4 > 0 && this.text.charAt(var4 - 1) == 32) - { + for (int var7 = 0; var7 < var6; ++var7) { + if (var5) { + while (par3 && var4 > 0 && this.text.charAt(var4 - 1) == 32) { --var4; } - while (var4 > 0 && this.text.charAt(var4 - 1) != 32) - { + while (var4 > 0 && this.text.charAt(var4 - 1) != 32) { --var4; } - } - else - { + } else { int var8 = this.text.length(); var4 = this.text.indexOf(32, var4); - if (var4 == -1) - { + if (var4 == -1) { var4 = var8; - } - else - { - while (par3 && var4 < var8 && this.text.charAt(var4) == 32) - { + } else { + while (par3 && var4 < var8 && this.text.charAt(var4) == 32) { ++var4; } } @@ -251,485 +215,394 @@ public int func_73798_a(int par1, int par2, boolean par3) /** * Moves the text cursor by a specified number of characters and clears the selection */ - public void moveCursorBy(int par1) - { + public void moveCursorBy(int par1) { this.setCursorPosition(this.selectionEnd + par1); } /** * sets the position of the cursor to the provided index */ - public void setCursorPosition(int par1) - { + public void setCursorPosition(int par1) { this.cursorPosition = par1; int var2 = this.text.length(); - if (this.cursorPosition < 0) - { + if (this.cursorPosition < 0) { this.cursorPosition = 0; } - if (this.cursorPosition > var2) - { + if (this.cursorPosition > var2) { this.cursorPosition = var2; } this.setSelectionPos(this.cursorPosition); } - /** - * sets the cursors position to the beginning - */ - public void setCursorPositionZero() - { - this.setCursorPosition(0); - } - - /** - * sets the cursors position to after the text - */ - public void setCursorPositionEnd() - { - this.setCursorPosition(this.text.length()); - } - - /** - * Call this method from you GuiScreen to process the keys into textbox. - */ - public boolean textboxKeyTyped(char par1, int par2) - { - if (this.isEnabled && this.isFocused) - { - switch (par1) - { - case 1: - this.setCursorPositionEnd(); - this.setSelectionPos(0); - return true; - case 3: - GuiScreen.setClipboardString(this.getSelectedtext()); - return true; - case 22: - this.writeText(GuiScreen.getClipboardString()); - return true; - case 24: - GuiScreen.setClipboardString(this.getSelectedtext()); - this.writeText(""); - return true; - default: - switch (par2) - { - case 14: - if (GuiScreen.isCtrlKeyDown()) - { - this.deleteWords(-1); - } - else - { - this.deleteFromCursor(-1); - } - - return true; - case 199: - if (GuiScreen.isShiftKeyDown()) - { - this.setSelectionPos(0); - } - else - { - this.setCursorPositionZero(); - } - - return true; - case 203: - if (GuiScreen.isShiftKeyDown()) - { - if (GuiScreen.isCtrlKeyDown()) - { - this.setSelectionPos(this.getNthWordFromPos(-1, this.getSelectionEnd())); - } - else - { - this.setSelectionPos(this.getSelectionEnd() - 1); - } - } - else if (GuiScreen.isCtrlKeyDown()) - { - this.setCursorPosition(this.getNthWordFromCursor(-1)); - } - else - { - this.moveCursorBy(-1); - } - - return true; - case 205: - if (GuiScreen.isShiftKeyDown()) - { - if (GuiScreen.isCtrlKeyDown()) - { - this.setSelectionPos(this.getNthWordFromPos(1, this.getSelectionEnd())); - } - else - { - this.setSelectionPos(this.getSelectionEnd() + 1); - } - } - else if (GuiScreen.isCtrlKeyDown()) - { - this.setCursorPosition(this.getNthWordFromCursor(1)); - } - else - { - this.moveCursorBy(1); - } - - return true; - case 207: - if (GuiScreen.isShiftKeyDown()) - { - this.setSelectionPos(this.text.length()); - } - else - { - this.setCursorPositionEnd(); - } - - return true; - case 211: - if (GuiScreen.isCtrlKeyDown()) - { - this.deleteWords(1); - } - else - { - this.deleteFromCursor(1); - } - - return true; - default: - if (ChatAllowedCharacters.isAllowedCharacter(par1)) - { - this.writeText(Character.toString(par1)); - return true; - } - else - { - return false; - } - } - } - } - else - { - return false; - } - } - - /** - * Args: x, y, buttonClicked - */ - public void mouseClicked(int par1, int par2, int par3) - { - String displayString = text.replace(NBTStringHelper.SECTION_SIGN, '?'); - boolean var4 = par1 >= this.xPos && par1 < this.xPos + this.width && par2 >= this.yPos && par2 < this.yPos + this.height; - - this.setFocused(this.isEnabled && var4); - - if (this.isFocused && par3 == 0) - { - int var5 = par1 - this.xPos; - - if (this.enableBackgroundDrawing) - { - var5 -= 4; - } - - String var6 = this.fontRenderer.trimStringToWidth(displayString.substring(this.field_73816_n), this.getWidth()); - this.setCursorPosition(this.fontRenderer.trimStringToWidth(var6, var5).length() + this.field_73816_n); - } - } - - /** - * Draws the textbox - */ - public void drawTextBox() - { - String textToDisplay = text.replace(NBTStringHelper.SECTION_SIGN, '?'); - if (this.getVisible()) - { - if (this.getEnableBackgroundDrawing()) - { - drawRect(this.xPos - 1, this.yPos - 1, this.xPos + this.width + 1, this.yPos + this.height + 1, -6250336); - drawRect(this.xPos, this.yPos, this.xPos + this.width, this.yPos + this.height, -16777216); - } - - int var1 = this.isEnabled ? this.enabledColor : this.disabledColor; - int var2 = this.cursorPosition - this.field_73816_n; - int var3 = this.selectionEnd - this.field_73816_n; - String var4 = this.fontRenderer.trimStringToWidth(textToDisplay.substring(this.field_73816_n), this.getWidth()); - boolean var5 = var2 >= 0 && var2 <= var4.length(); - boolean var6 = this.isFocused && this.cursorCounter / 6 % 2 == 0 && var5; - int var7 = this.enableBackgroundDrawing ? this.xPos + 4 : this.xPos; - int var8 = this.enableBackgroundDrawing ? this.yPos + (this.height - 8) / 2 : this.yPos; - int var9 = var7; - - if (var3 > var4.length()) - { - var3 = var4.length(); - } - - if (var4.length() > 0) - { - String var10 = var5 ? var4.substring(0, var2) : var4; - var9 = this.fontRenderer.drawStringWithShadow(var10, var7, var8, var1); - } - - boolean var13 = this.cursorPosition < this.text.length() || this.text.length() >= this.getMaxStringLength(); - int var11 = var9; - - if (!var5) - { - var11 = var2 > 0 ? var7 + this.width : var7; - } - else if (var13) - { - var11 = var9 - 1; - --var9; - } - - if (var4.length() > 0 && var5 && var2 < var4.length()) - { - this.fontRenderer.drawStringWithShadow(var4.substring(var2), var9, var8, var1); - } - - if (var6) - { - if (var13) - { - Gui.drawRect(var11, var8 - 1, var11 + 1, var8 + 1 + this.fontRenderer.FONT_HEIGHT, -3092272); - } - else - { - this.fontRenderer.drawStringWithShadow("_", var11, var8, var1); - } - } - - if (var3 != var2) - { - int var12 = var7 + this.fontRenderer.getStringWidth(var4.substring(0, var3)); - this.drawCursorVertical(var11, var8 - 1, var12 - 1, var8 + 1 + this.fontRenderer.FONT_HEIGHT); - } - } - } - - /** - * draws the vertical line cursor in the textbox - */ - private void drawCursorVertical(int par1, int par2, int par3, int par4) - { - int var5; - - if (par1 < par3) - { - var5 = par1; - par1 = par3; - par3 = var5; - } - - if (par2 < par4) - { - var5 = par2; - par2 = par4; - par4 = var5; - } - - Tessellator tessellator = Tessellator.getInstance(); - VertexBuffer worldRenderer = tessellator.getBuffer(); - GL11.glColor4f(0.0F, 0.0F, 255.0F, 255.0F); - GlStateManager.disableTexture2D(); - GlStateManager.enableColorLogic(); - GlStateManager.colorLogicOp(GL11.GL_OR_REVERSE); - - worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR); - worldRenderer.pos((double)par1, (double)par4, 0.0D); - worldRenderer.pos((double)par3, (double)par4, 0.0D); - worldRenderer.pos((double)par3, (double)par2, 0.0D); - worldRenderer.pos((double) par1, (double) par2, 0.0D); - tessellator.draw(); - GlStateManager.disableColorLogic(); - GlStateManager.enableTexture2D(); - } - - public void setMaxStringLength(int par1) - { - this.maxStringLength = par1; - - if (this.text.length() > par1) - { - this.text = this.text.substring(0, par1); - } - } - - /** - * returns the maximum number of character that can be contained in this textbox - */ - public int getMaxStringLength() - { - return this.maxStringLength; - } - - /** - * returns the current position of the cursor - */ - public int getCursorPosition() - { - return this.cursorPosition; - } - - /** - * get enable drawing background and outline - */ - public boolean getEnableBackgroundDrawing() - { - return this.enableBackgroundDrawing; - } - - /** - * enable drawing background and outline - */ - public void setEnableBackgroundDrawing(boolean par1) - { - this.enableBackgroundDrawing = par1; - } - - /** - * Sets the text colour for this textbox (disabled text will not use this colour) - */ - public void setTextColor(int par1) - { - this.enabledColor = par1; - } - - public void func_82266_h(int par1) - { - this.disabledColor = par1; - } - - /** - * setter for the focused field - */ - public void setFocused(boolean par1) - { - if (par1 && !this.isFocused) - { - this.cursorCounter = 0; - } - - this.isFocused = par1; - } - - /** - * getter for the focused field - */ - public boolean isFocused() - { - return this.isFocused; - } - - public void func_82265_c(boolean par1) - { - this.isEnabled = par1; - } - - /** - * the side of the selection that is not the cursor, maye be the same as the cursor - */ - public int getSelectionEnd() - { - return this.selectionEnd; - } - - /** - * returns the width of the textbox depending on if the the box is enabled - */ - public int getWidth() - { - return this.getEnableBackgroundDrawing() ? this.width - 8 : this.width; - } - - /** - * Sets the position of the selection anchor (i.e. position the selection was started at) - */ - public void setSelectionPos(int par1) - { - String displayString = text.replace(NBTStringHelper.SECTION_SIGN, '?'); - int var2 = displayString.length(); - - if (par1 > var2) - { - par1 = var2; - } - - if (par1 < 0) - { - par1 = 0; - } - - this.selectionEnd = par1; - - if (this.fontRenderer != null) - { - if (this.field_73816_n > var2) - { - this.field_73816_n = var2; - } - - int var3 = this.getWidth(); - String var4 = this.fontRenderer.trimStringToWidth(displayString.substring(this.field_73816_n), var3); - int var5 = var4.length() + this.field_73816_n; - - if (par1 == this.field_73816_n) - { - this.field_73816_n -= this.fontRenderer.trimStringToWidth(displayString, var3, true).length(); - } - - if (par1 > var5) - { - this.field_73816_n += par1 - var5; - } - else if (par1 <= this.field_73816_n) - { - this.field_73816_n -= this.field_73816_n - par1; - } - - if (this.field_73816_n < 0) - { - this.field_73816_n = 0; - } - - if (this.field_73816_n > var2) - { - this.field_73816_n = var2; - } - } - } - - - - /** - * @return {@code true} if this textbox is visible - */ - public boolean getVisible() - { - return this.visible; - } - - /** - * Sets whether or not this textbox is visible - */ - public void setVisible(boolean par1) - { - this.visible = par1; - } + /** + * sets the cursors position to the beginning + */ + public void setCursorPositionZero() { + this.setCursorPosition(0); + } + + /** + * sets the cursors position to after the text + */ + public void setCursorPositionEnd() { + this.setCursorPosition(this.text.length()); + } + + /** + * Call this method from you GuiScreen to process the keys into textbox. + */ + public boolean textboxKeyTyped(char par1, int par2) { + if (this.isEnabled && this.isFocused) { + switch (par1) { + case 1: + this.setCursorPositionEnd(); + this.setSelectionPos(0); + return true; + case 3: + GuiScreen.setClipboardString(this.getSelectedtext()); + return true; + case 22: + this.writeText(GuiScreen.getClipboardString()); + return true; + case 24: + GuiScreen.setClipboardString(this.getSelectedtext()); + this.writeText(""); + return true; + default: + switch (par2) { + case 14: + if (GuiScreen.isCtrlKeyDown()) { + this.deleteWords(-1); + } else { + this.deleteFromCursor(-1); + } + + return true; + case 199: + if (GuiScreen.isShiftKeyDown()) { + this.setSelectionPos(0); + } else { + this.setCursorPositionZero(); + } + + return true; + case 203: + if (GuiScreen.isShiftKeyDown()) { + if (GuiScreen.isCtrlKeyDown()) { + this.setSelectionPos(this.getNthWordFromPos(-1, this.getSelectionEnd())); + } else { + this.setSelectionPos(this.getSelectionEnd() - 1); + } + } else if (GuiScreen.isCtrlKeyDown()) { + this.setCursorPosition(this.getNthWordFromCursor(-1)); + } else { + this.moveCursorBy(-1); + } + + return true; + case 205: + if (GuiScreen.isShiftKeyDown()) { + if (GuiScreen.isCtrlKeyDown()) { + this.setSelectionPos(this.getNthWordFromPos(1, this.getSelectionEnd())); + } else { + this.setSelectionPos(this.getSelectionEnd() + 1); + } + } else if (GuiScreen.isCtrlKeyDown()) { + this.setCursorPosition(this.getNthWordFromCursor(1)); + } else { + this.moveCursorBy(1); + } + + return true; + case 207: + if (GuiScreen.isShiftKeyDown()) { + this.setSelectionPos(this.text.length()); + } else { + this.setCursorPositionEnd(); + } + + return true; + case 211: + if (GuiScreen.isCtrlKeyDown()) { + this.deleteWords(1); + } else { + this.deleteFromCursor(1); + } + + return true; + default: + if (ChatAllowedCharacters.isAllowedCharacter(par1)) { + this.writeText(Character.toString(par1)); + return true; + } else { + return false; + } + } + } + } else { + return false; + } + } + + /** + * Args: x, y, buttonClicked + */ + public void mouseClicked(int par1, int par2, int par3) { + String displayString = text.replace(NBTStringHelper.SECTION_SIGN, '?'); + boolean var4 = par1 >= this.xPos && par1 < this.xPos + this.width && par2 >= this.yPos && par2 < this.yPos + this.height; + + this.setFocused(this.isEnabled && var4); + + if (this.isFocused && par3 == 0) { + int var5 = par1 - this.xPos; + + if (this.enableBackgroundDrawing) { + var5 -= 4; + } + + String var6 = this.fontRenderer.trimStringToWidth(displayString.substring(this.field_73816_n), this.getWidth()); + this.setCursorPosition(this.fontRenderer.trimStringToWidth(var6, var5).length() + this.field_73816_n); + } + } + + /** + * Draws the textbox + */ + public void drawTextBox() { + String textToDisplay = text.replace(NBTStringHelper.SECTION_SIGN, '?'); + if (this.getVisible()) { + if (this.getEnableBackgroundDrawing()) { + drawRect(this.xPos - 1, this.yPos - 1, this.xPos + this.width + 1, this.yPos + this.height + 1, -6250336); + drawRect(this.xPos, this.yPos, this.xPos + this.width, this.yPos + this.height, -16777216); + } + + int var1 = this.isEnabled ? this.enabledColor : this.disabledColor; + int var2 = this.cursorPosition - this.field_73816_n; + int var3 = this.selectionEnd - this.field_73816_n; + String var4 = this.fontRenderer.trimStringToWidth(textToDisplay.substring(this.field_73816_n), this.getWidth()); + boolean var5 = var2 >= 0 && var2 <= var4.length(); + boolean var6 = this.isFocused && this.cursorCounter / 6 % 2 == 0 && var5; + int var7 = this.enableBackgroundDrawing ? this.xPos + 4 : this.xPos; + int var8 = this.enableBackgroundDrawing ? this.yPos + (this.height - 8) / 2 : this.yPos; + int var9 = var7; + + if (var3 > var4.length()) { + var3 = var4.length(); + } + + if (var4.length() > 0) { + String var10 = var5 ? var4.substring(0, var2) : var4; + var9 = this.fontRenderer.drawStringWithShadow(var10, var7, var8, var1); + } + + boolean var13 = this.cursorPosition < this.text.length() || this.text.length() >= this.getMaxStringLength(); + int var11 = var9; + + if (!var5) { + var11 = var2 > 0 ? var7 + this.width : var7; + } else if (var13) { + var11 = var9 - 1; + --var9; + } + + if (var4.length() > 0 && var5 && var2 < var4.length()) { + this.fontRenderer.drawStringWithShadow(var4.substring(var2), var9, var8, var1); + } + + if (var6) { + if (var13) { + Gui.drawRect(var11, var8 - 1, var11 + 1, var8 + 1 + this.fontRenderer.FONT_HEIGHT, -3092272); + } else { + this.fontRenderer.drawStringWithShadow("_", var11, var8, var1); + } + } + + if (var3 != var2) { + int var12 = var7 + this.fontRenderer.getStringWidth(var4.substring(0, var3)); + this.drawCursorVertical(var11, var8 - 1, var12 - 1, var8 + 1 + this.fontRenderer.FONT_HEIGHT); + } + } + } + + /** + * draws the vertical line cursor in the textbox + */ + private void drawCursorVertical(int par1, int par2, int par3, int par4) { + int var5; + + if (par1 < par3) { + var5 = par1; + par1 = par3; + par3 = var5; + } + + if (par2 < par4) { + var5 = par2; + par2 = par4; + par4 = var5; + } + + Tessellator tessellator = Tessellator.getInstance(); + VertexBuffer worldRenderer = tessellator.getBuffer(); + GL11.glColor4f(0.0F, 0.0F, 255.0F, 255.0F); + GlStateManager.disableTexture2D(); + GlStateManager.enableColorLogic(); + GlStateManager.colorLogicOp(GL11.GL_OR_REVERSE); + + worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR); + worldRenderer.pos((double) par1, (double) par4, 0.0D); + worldRenderer.pos((double) par3, (double) par4, 0.0D); + worldRenderer.pos((double) par3, (double) par2, 0.0D); + worldRenderer.pos((double) par1, (double) par2, 0.0D); + tessellator.draw(); + GlStateManager.disableColorLogic(); + GlStateManager.enableTexture2D(); + } + + public void setMaxStringLength(int par1) { + this.maxStringLength = par1; + + if (this.text.length() > par1) { + this.text = this.text.substring(0, par1); + } + } + + /** + * returns the maximum number of character that can be contained in this textbox + */ + public int getMaxStringLength() { + return this.maxStringLength; + } + + /** + * returns the current position of the cursor + */ + public int getCursorPosition() { + return this.cursorPosition; + } + + /** + * get enable drawing background and outline + */ + public boolean getEnableBackgroundDrawing() { + return this.enableBackgroundDrawing; + } + + /** + * enable drawing background and outline + */ + public void setEnableBackgroundDrawing(boolean par1) { + this.enableBackgroundDrawing = par1; + } + + /** + * Sets the text colour for this textbox (disabled text will not use this colour) + */ + public void setTextColor(int par1) { + this.enabledColor = par1; + } + + public void func_82266_h(int par1) { + this.disabledColor = par1; + } + + /** + * setter for the focused field + */ + public void setFocused(boolean par1) { + if (par1 && !this.isFocused) { + this.cursorCounter = 0; + } + + this.isFocused = par1; + } + + /** + * getter for the focused field + */ + public boolean isFocused() { + return this.isFocused; + } + + public void func_82265_c(boolean par1) { + this.isEnabled = par1; + } + + /** + * the side of the selection that is not the cursor, maye be the same as the cursor + */ + public int getSelectionEnd() { + return this.selectionEnd; + } + + /** + * returns the width of the textbox depending on if the the box is enabled + */ + public int getWidth() { + return this.getEnableBackgroundDrawing() ? this.width - 8 : this.width; + } + + /** + * Sets the position of the selection anchor (i.e. position the selection was started at) + */ + public void setSelectionPos(int par1) { + String displayString = text.replace(NBTStringHelper.SECTION_SIGN, '?'); + int var2 = displayString.length(); + + if (par1 > var2) { + par1 = var2; + } + + if (par1 < 0) { + par1 = 0; + } + + this.selectionEnd = par1; + + if (this.fontRenderer != null) { + if (this.field_73816_n > var2) { + this.field_73816_n = var2; + } + + int var3 = this.getWidth(); + String var4 = this.fontRenderer.trimStringToWidth(displayString.substring(this.field_73816_n), var3); + int var5 = var4.length() + this.field_73816_n; + + if (par1 == this.field_73816_n) { + this.field_73816_n -= this.fontRenderer.trimStringToWidth(displayString, var3, true).length(); + } + + if (par1 > var5) { + this.field_73816_n += par1 - var5; + } else if (par1 <= this.field_73816_n) { + this.field_73816_n -= this.field_73816_n - par1; + } + + if (this.field_73816_n < 0) { + this.field_73816_n = 0; + } + + if (this.field_73816_n > var2) { + this.field_73816_n = var2; + } + } + } + + + /** + * @return {@code true} if this textbox is visible + */ + public boolean getVisible() { + return this.visible; + } + + /** + * Sets whether or not this textbox is visible + */ + public void setVisible(boolean par1) { + this.visible = par1; + } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTNodeSorter.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTNodeSorter.java index fa061f9..da6ce2b 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTNodeSorter.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTNodeSorter.java @@ -6,14 +6,14 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -public class NBTNodeSorter implements Comparator>{ +public class NBTNodeSorter implements Comparator> { @Override public int compare(Node a, Node b) { NBTBase n1 = a.getObject().getNBT(), n2 = b.getObject().getNBT(); String s1 = a.getObject().getName(), s2 = b.getObject().getName(); - if (n1 instanceof NBTTagCompound || n1 instanceof NBTTagList){ - if (n2 instanceof NBTTagCompound || n2 instanceof NBTTagList){ + if (n1 instanceof NBTTagCompound || n1 instanceof NBTTagList) { + if (n2 instanceof NBTTagCompound || n2 instanceof NBTTagList) { int dif = n1.getId() - n2.getId(); return (dif == 0) ? s1.compareTo(s2) : dif; } @@ -21,7 +21,7 @@ public int compare(Node a, Node b) { } if (n2 instanceof NBTTagCompound || n2 instanceof NBTTagList) return -1; - int dif =n1.getId() - n2.getId(); + int dif = n1.getId() - n2.getId(); return (dif == 0) ? s1.compareTo(s2) : dif; } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTTree.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTTree.java index 20ea680..aecffc1 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTTree.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/NBTTree.java @@ -16,70 +16,70 @@ import com.mcf.davidee.nbtedit.NBTStringHelper; public class NBTTree { - + private NBTTagCompound baseTag; - + private Node root; - - public NBTTree (NBTTagCompound tag){ + + public NBTTree(NBTTagCompound tag) { baseTag = tag; construct(); } - public Node getRoot(){ + + public Node getRoot() { return root; } - - public boolean canDelete(Node node){ + + public boolean canDelete(Node node) { return node != root; } - + public boolean delete(Node node) { return !(node == null || node == root) && deleteNode(node, root); } - - private boolean deleteNode(Node toDelete, Node cur){ - for (Iterator> it = cur.getChildren().iterator(); it.hasNext();){ + + private boolean deleteNode(Node toDelete, Node cur) { + for (Iterator> it = cur.getChildren().iterator(); it.hasNext(); ) { Node child = it.next(); - if (child == toDelete){ + if (child == toDelete) { it.remove(); return true; } - boolean flag = deleteNode(toDelete,child); + boolean flag = deleteNode(toDelete, child); if (flag) return true; } return false; } - - + + private void construct() { root = new Node<>(new NamedNBT("ROOT", baseTag.copy())); root.setDrawChildren(true); addChildrenToTree(root); sort(root); } - + public void sort(Node node) { Collections.sort(node.getChildren(), NBTEdit.SORTER); for (Node c : node.getChildren()) sort(c); } - - public void addChildrenToTree(Node parent){ + + public void addChildrenToTree(Node parent) { NBTBase tag = parent.getObject().getNBT(); - if (tag instanceof NBTTagCompound){ - Map map = NBTHelper.getMap((NBTTagCompound)tag); - for (Entry entry : map.entrySet()){ + if (tag instanceof NBTTagCompound) { + Map map = NBTHelper.getMap((NBTTagCompound) tag); + for (Entry entry : map.entrySet()) { NBTBase base = entry.getValue(); Node child = new Node<>(parent, new NamedNBT(entry.getKey(), base)); parent.addChild(child); addChildrenToTree(child); } - - } - else if (tag instanceof NBTTagList){ - NBTTagList list = (NBTTagList)tag; - for (int i =0; i < list.tagCount(); ++ i){ + + } else if (tag instanceof NBTTagList) { + NBTTagList list = (NBTTagList) tag; + for (int i = 0; i < list.tagCount(); ++i) { NBTBase base = NBTHelper.getTagAt(list, i); Node child = new Node<>(parent, new NamedNBT(base)); parent.addChild(child); @@ -87,75 +87,71 @@ else if (tag instanceof NBTTagList){ } } } - - public NBTTagCompound toNBTTagCompound(){ + + public NBTTagCompound toNBTTagCompound() { NBTTagCompound tag = new NBTTagCompound(); addChildrenToTag(root, tag); return tag; } - - public void addChildrenToTag (Node parent, NBTTagCompound tag){ - for (Node child : parent.getChildren()){ + + public void addChildrenToTag(Node parent, NBTTagCompound tag) { + for (Node child : parent.getChildren()) { NBTBase base = child.getObject().getNBT(); String name = child.getObject().getName(); - if (base instanceof NBTTagCompound){ + if (base instanceof NBTTagCompound) { NBTTagCompound newTag = new NBTTagCompound(); addChildrenToTag(child, newTag); tag.setTag(name, newTag); - } - else if (base instanceof NBTTagList){ + } else if (base instanceof NBTTagList) { NBTTagList list = new NBTTagList(); addChildrenToList(child, list); tag.setTag(name, list); - } - else + } else tag.setTag(name, base.copy()); } } - - public void addChildrenToList(Node parent, NBTTagList list){ - for (Node child: parent.getChildren()){ + + public void addChildrenToList(Node parent, NBTTagList list) { + for (Node child : parent.getChildren()) { NBTBase base = child.getObject().getNBT(); - if (base instanceof NBTTagCompound){ + if (base instanceof NBTTagCompound) { NBTTagCompound newTag = new NBTTagCompound(); addChildrenToTag(child, newTag); list.appendTag(newTag); - } - else if (base instanceof NBTTagList){ + } else if (base instanceof NBTTagList) { NBTTagList newList = new NBTTagList(); addChildrenToList(child, newList); list.appendTag(newList); - } - else + } else list.appendTag(base.copy()); } } - - public void print(){ - print(root,0); + + public void print() { + print(root, 0); } - - private void print(Node n, int i){ - System.out.println(repeat("\t",i) + NBTStringHelper.getNBTName(n.getObject())); + + private void print(Node n, int i) { + System.out.println(repeat("\t", i) + NBTStringHelper.getNBTName(n.getObject())); for (Node child : n.getChildren()) - print(child,i+1); + print(child, i + 1); } - - public List toStrings(){ + + public List toStrings() { List s = new ArrayList<>(); - toStrings(s,root,0); + toStrings(s, root, 0); return s; } - - private void toStrings(List s, Node n, int i){ - s.add(repeat(" ",i) + NBTStringHelper.getNBTName(n.getObject())); + + private void toStrings(List s, Node n, int i) { + s.add(repeat(" ", i) + NBTStringHelper.getNBTName(n.getObject())); for (Node child : n.getChildren()) - toStrings(s,child,i+1); + toStrings(s, child, i + 1); } - - public static String repeat(String c, int i){ - StringBuilder b = new StringBuilder(i+1); - for (int j =0; j < i; ++ j) + + public static String repeat(String c, int i) { + StringBuilder b = new StringBuilder(i + 1); + for (int j = 0; j < i; ++j) b.append(c); return b.toString(); } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/NamedNBT.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/NamedNBT.java index 564b1ea..1364031 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/NamedNBT.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/NamedNBT.java @@ -3,37 +3,37 @@ import net.minecraft.nbt.NBTBase; public class NamedNBT { - + protected String name; protected NBTBase nbt; - + public NamedNBT(NBTBase nbt) { this("", nbt); } - + public NamedNBT(String name, NBTBase nbt) { this.name = name; this.nbt = nbt; } - + public String getName() { return name; } - + public void setName(String name) { this.name = name; } - + public NBTBase getNBT() { return nbt; } - + public void setNBT(NBTBase nbt) { this.nbt = nbt; } - + public NamedNBT copy() { return new NamedNBT(name, nbt.copy()); } - + } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/Node.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/Node.java index 649978a..d94a8e4 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/Node.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/Node.java @@ -6,70 +6,68 @@ public class Node { private List> children; - + private Node parent; private T obj; - + private boolean drawChildren; - - public Node(){ - this((T)null); + + public Node() { + this((T) null); } - - public Node(T obj){ + + public Node(T obj) { children = new ArrayList<>(); this.obj = obj; } - - public boolean shouldDrawChildren(){ + + public boolean shouldDrawChildren() { return drawChildren; } - - public void setDrawChildren(boolean draw){ + + public void setDrawChildren(boolean draw) { drawChildren = draw; } - - public Node(Node parent){ - this(parent,null); + + public Node(Node parent) { + this(parent, null); } - - public Node(Node parent, T obj){ + + public Node(Node parent, T obj) { this.parent = parent; children = new ArrayList<>(); this.obj = obj; } - - public void addChild(Node n){ + + public void addChild(Node n) { children.add(n); } - - public boolean removeChild(Node n){ + + public boolean removeChild(Node n) { return children.remove(n); } - - public List> getChildren(){ + + public List> getChildren() { return children; } - - public Node getParent(){ + + public Node getParent() { return parent; } - - public T getObject(){ + + public T getObject() { return obj; } - - public String toString(){ + + public String toString() { return "" + obj; } public boolean hasChildren() { return children.size() > 0; } - - public boolean hasParent(){ + + public boolean hasParent() { return parent != null; } - - } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/ParseHelper.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/ParseHelper.java index 815a85d..1c413d4 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/ParseHelper.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/ParseHelper.java @@ -2,81 +2,75 @@ public class ParseHelper { - public static byte parseByte(String s) throws NumberFormatException{ + public static byte parseByte(String s) throws NumberFormatException { try { return Byte.parseByte(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid byte"); } } - public static short parseShort(String s) throws NumberFormatException{ + + public static short parseShort(String s) throws NumberFormatException { try { return Short.parseShort(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid short"); } } - public static int parseInt(String s) throws NumberFormatException{ + public static int parseInt(String s) throws NumberFormatException { try { return Integer.parseInt(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid int"); } } - public static long parseLong(String s) throws NumberFormatException{ + + public static long parseLong(String s) throws NumberFormatException { try { return Long.parseLong(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid long"); } } - public static float parseFloat(String s) throws NumberFormatException{ + public static float parseFloat(String s) throws NumberFormatException { try { return Float.parseFloat(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid float"); } } - public static double parseDouble(String s) throws NumberFormatException{ + + public static double parseDouble(String s) throws NumberFormatException { try { return Double.parseDouble(s); - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid double"); } } - public static byte[] parseByteArray(String s) throws NumberFormatException{ + public static byte[] parseByteArray(String s) throws NumberFormatException { try { String[] input = s.split(" "); byte[] arr = new byte[input.length]; - for (int i =0; i < input.length; ++i) + for (int i = 0; i < input.length; ++i) arr[i] = parseByte(input[i]); return arr; - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid byte array"); } } - public static int[] parseIntArray(String s) throws NumberFormatException{ + + public static int[] parseIntArray(String s) throws NumberFormatException { try { String[] input = s.split(" "); int[] arr = new int[input.length]; - for (int i =0; i < input.length; ++i) + for (int i = 0; i < input.length; ++i) arr[i] = parseInt(input[i]); return arr; - } - catch(NumberFormatException e){ + } catch (NumberFormatException e) { throw new NumberFormatException("Not a valid int array"); } } - - } diff --git a/src/main/java/com/mcf/davidee/nbtedit/nbt/SaveStates.java b/src/main/java/com/mcf/davidee/nbtedit/nbt/SaveStates.java index 38d04f5..03d73ad 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/nbt/SaveStates.java +++ b/src/main/java/com/mcf/davidee/nbtedit/nbt/SaveStates.java @@ -15,66 +15,64 @@ public class SaveStates { private File file; private SaveState[] tags; - public SaveStates(File file){ + public SaveStates(File file) { this.file = file; tags = new SaveState[7]; - for (int i =0; i < 7; ++i) - tags[i] = new SaveState("Slot " + (i+1)); + for (int i = 0; i < 7; ++i) + tags[i] = new SaveState("Slot " + (i + 1)); } - - public void read() throws IOException{ - if (file.exists() && file.canRead()){ + + public void read() throws IOException { + if (file.exists() && file.canRead()) { NBTTagCompound root = CompressedStreamTools.read(file); - for (int i =0; i < 7; ++i){ - String name = "slot" + (i+1); + for (int i = 0; i < 7; ++i) { + String name = "slot" + (i + 1); if (root.hasKey(name)) tags[i].tag = root.getCompoundTag(name); - if (root.hasKey(name+"Name")) - tags[i].name = root.getString(name+"Name"); + if (root.hasKey(name + "Name")) + tags[i].name = root.getString(name + "Name"); } } } - - public void write() throws IOException{ + + public void write() throws IOException { NBTTagCompound root = new NBTTagCompound(); - for (int i = 0; i <7; ++i){ - root.setTag("slot" + (i+1), tags[i].tag); - root.setString("slot" + (i+1)+"Name", tags[i].name); + for (int i = 0; i < 7; ++i) { + root.setTag("slot" + (i + 1), tags[i].tag); + root.setString("slot" + (i + 1) + "Name", tags[i].name); } CompressedStreamTools.write(root, file); } - - public void save(){ + + public void save() { try { write(); - NBTEdit.log(Level.TRACE,"NBTEdit saved successfully."); - } - catch(IOException e){ + NBTEdit.log(Level.TRACE, "NBTEdit saved successfully."); + } catch (IOException e) { NBTEdit.log(Level.WARN, "Unable to write NBTEdit save."); NBTEdit.throwing("SaveStates", "save", e); } } - - public void load(){ + + public void load() { try { read(); - NBTEdit.log(Level.TRACE,"NBTEdit save loaded successfully."); - } - catch(IOException e){ + NBTEdit.log(Level.TRACE, "NBTEdit save loaded successfully."); + } catch (IOException e) { NBTEdit.log(Level.WARN, "Unable to read NBTEdit save."); NBTEdit.throwing("SaveStates", "load", e); } } - - public SaveState getSaveState(int index){ + + public SaveState getSaveState(int index) { return tags[index]; } - public static final class SaveState{ + public static final class SaveState { public String name; public NBTTagCompound tag; - public SaveState(String name){ + public SaveState(String name) { this.name = name; this.tag = new NBTTagCompound(); } diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/EntityNBTPacket.java b/src/main/java/com/mcf/davidee/nbtedit/packets/EntityNBTPacket.java index 915273f..c369f78 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/EntityNBTPacket.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/EntityNBTPacket.java @@ -17,13 +17,20 @@ import org.apache.logging.log4j.Level; public class EntityNBTPacket implements IMessage { - /** The id of the entity being edited. */ + /** + * The id of the entity being edited. + */ protected int entityID; - /** The nbt data of the entity. */ + /** + * The nbt data of the entity. + */ protected NBTTagCompound tag; - /** Required default constructor. */ - public EntityNBTPacket() {} + /** + * Required default constructor. + */ + public EntityNBTPacket() { + } public EntityNBTPacket(int entityID, NBTTagCompound tag) { this.entityID = entityID; @@ -54,21 +61,27 @@ public void run() { Entity entity = player.worldObj.getEntityByID(packet.entityID); if (entity != null && NBTEdit.proxy.checkPermission(player)) { try { - GameType preGameType = player.interactionManager.getGameType(); + GameType preGameType = null; + if (entity instanceof EntityPlayerMP) + preGameType = ((EntityPlayerMP) entity).interactionManager.getGameType(); entity.readFromNBT(packet.tag); NBTEdit.log(Level.TRACE, player.getName() + " edited a tag -- Entity ID #" + packet.entityID); NBTEdit.logTag(packet.tag); - if (entity == player) { //Update player info + if (entity instanceof EntityPlayerMP) {//Update player info // This is fairly hacky. Consider swapping to an event driven system, where classes can register to // receive entity edit events and provide feedback/send packets as necessary. - - player.sendContainerToPlayer(player.inventoryContainer); - GameType type = player.interactionManager.getGameType(); - if (preGameType != type) - player.setGameType(type); - player.connection.sendPacket(new SPacketUpdateHealth(player.getHealth(), player.getFoodStats().getFoodLevel(), player.getFoodStats().getSaturationLevel())); - player.connection.sendPacket(new SPacketSetExperience(player.experience, player.experienceTotal, player.experienceLevel)); - player.sendPlayerAbilities(); + EntityPlayerMP targetPlayer = (EntityPlayerMP) entity; + targetPlayer.sendContainerToPlayer(targetPlayer.inventoryContainer); + GameType type = targetPlayer.interactionManager.getGameType(); + if (preGameType != type) { + targetPlayer.setGameType(type); + } + targetPlayer.connection.sendPacket(new SPacketUpdateHealth(targetPlayer.getHealth(), + targetPlayer.getFoodStats().getFoodLevel(), targetPlayer.getFoodStats().getSaturationLevel())); + targetPlayer.connection.sendPacket(new SPacketSetExperience(targetPlayer.experience, + targetPlayer.experienceTotal, targetPlayer.experienceLevel)); + targetPlayer.sendPlayerAbilities(); + targetPlayer.setPositionAndUpdate(targetPlayer.posX, targetPlayer.posY, targetPlayer.posZ); } NBTEdit.proxy.sendMessage(player, "Your changes have been saved", TextFormatting.WHITE); } catch (Throwable t) { diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/EntityRequestPacket.java b/src/main/java/com/mcf/davidee/nbtedit/packets/EntityRequestPacket.java index ee84816..652c5f8 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/EntityRequestPacket.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/EntityRequestPacket.java @@ -9,11 +9,16 @@ import org.apache.logging.log4j.Level; public class EntityRequestPacket implements IMessage { - /** The id of the entity being requested. */ + /** + * The id of the entity being requested. + */ private int entityID; - /** Required default constructor. */ - public EntityRequestPacket() {} + /** + * Required default constructor. + */ + public EntityRequestPacket() { + } public EntityRequestPacket(int entityID) { this.entityID = entityID; diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/MouseOverPacket.java b/src/main/java/com/mcf/davidee/nbtedit/packets/MouseOverPacket.java index c854bf4..1219cda 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/MouseOverPacket.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/MouseOverPacket.java @@ -11,14 +11,19 @@ public class MouseOverPacket implements IMessage { - /** Required default constructor. */ - public MouseOverPacket() {} + /** + * Required default constructor. + */ + public MouseOverPacket() { + } @Override - public void fromBytes(ByteBuf buf) {} + public void fromBytes(ByteBuf buf) { + } @Override - public void toBytes(ByteBuf buf) {} + public void toBytes(ByteBuf buf) { + } public static class Handler implements IMessageHandler { diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/PacketHandler.java b/src/main/java/com/mcf/davidee/nbtedit/packets/PacketHandler.java index 62c95fa..462c04d 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/PacketHandler.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/PacketHandler.java @@ -18,72 +18,75 @@ * */ public class PacketHandler { - public SimpleNetworkWrapper INSTANCE; - private static int ID = 0; + public SimpleNetworkWrapper INSTANCE; + private static int ID = 0; - public void initialize() { - INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(NBTEdit.MODID); - registerPackets(); - } + public void initialize() { + INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(NBTEdit.MODID); + registerPackets(); + } - public void registerPackets() { - INSTANCE.registerMessage(TileRequestPacket.Handler.class, TileRequestPacket.class, ID++, Side.SERVER); - INSTANCE.registerMessage(TileNBTPacket.Handler.class, TileNBTPacket.class, ID++, Side.CLIENT); - INSTANCE.registerMessage(TileNBTPacket.Handler.class, TileNBTPacket.class, ID++, Side.SERVER); - INSTANCE.registerMessage(EntityRequestPacket.Handler.class, EntityRequestPacket.class, ID++, Side.SERVER); - INSTANCE.registerMessage(EntityNBTPacket.Handler.class, EntityNBTPacket.class, ID++, Side.CLIENT); - INSTANCE.registerMessage(EntityNBTPacket.Handler.class, EntityNBTPacket.class, ID++, Side.SERVER); - INSTANCE.registerMessage(MouseOverPacket.Handler.class, MouseOverPacket.class, ID++, Side.CLIENT); - } + public void registerPackets() { + INSTANCE.registerMessage(TileRequestPacket.Handler.class, TileRequestPacket.class, ID++, Side.SERVER); + INSTANCE.registerMessage(TileNBTPacket.Handler.class, TileNBTPacket.class, ID++, Side.CLIENT); + INSTANCE.registerMessage(TileNBTPacket.Handler.class, TileNBTPacket.class, ID++, Side.SERVER); + INSTANCE.registerMessage(EntityRequestPacket.Handler.class, EntityRequestPacket.class, ID++, Side.SERVER); + INSTANCE.registerMessage(EntityNBTPacket.Handler.class, EntityNBTPacket.class, ID++, Side.CLIENT); + INSTANCE.registerMessage(EntityNBTPacket.Handler.class, EntityNBTPacket.class, ID++, Side.SERVER); + INSTANCE.registerMessage(MouseOverPacket.Handler.class, MouseOverPacket.class, ID++, Side.CLIENT); + } - /** - * Sends a TileEntity's nbt data to the player for editing. - * @param player The player to send the TileEntity to. - * @param pos The block containing the TileEntity. - */ - public void sendTile(final EntityPlayerMP player, final BlockPos pos) { - if (NBTEdit.proxy.checkPermission(player)) { - player.getServerWorld().addScheduledTask(new Runnable() { - @Override - public void run() { - TileEntity te = player.getServerWorld().getTileEntity(pos); - if (te != null) { - NBTTagCompound tag = new NBTTagCompound(); - te.writeToNBT(tag); - INSTANCE.sendTo(new TileNBTPacket(pos, tag), player); - } else { - NBTEdit.proxy.sendMessage(player, "Error - There is no TileEntity at " - + pos.getX() + ", " + pos.getY() + ", " + pos.getZ(), TextFormatting.RED); - } - } - }); - } - } + /** + * Sends a TileEntity's nbt data to the player for editing. + * + * @param player The player to send the TileEntity to. + * @param pos The block containing the TileEntity. + */ + public void sendTile(final EntityPlayerMP player, final BlockPos pos) { + if (NBTEdit.proxy.checkPermission(player)) { + player.getServerWorld().addScheduledTask(new Runnable() { + @Override + public void run() { + TileEntity te = player.getServerWorld().getTileEntity(pos); + if (te != null) { + NBTTagCompound tag = new NBTTagCompound(); + te.writeToNBT(tag); + INSTANCE.sendTo(new TileNBTPacket(pos, tag), player); + } else { + NBTEdit.proxy.sendMessage(player, "Error - There is no TileEntity at " + + pos.getX() + ", " + pos.getY() + ", " + pos.getZ(), TextFormatting.RED); + } + } + }); + } + } - /** - * Sends a Entity's nbt data to the player for editing. - * @param player The player to send the Entity data to. - * @param entityId The id of the Entity. - */ - public void sendEntity(final EntityPlayerMP player, final int entityId) { - if (NBTEdit.proxy.checkPermission(player)) { - player.getServerWorld().addScheduledTask(new Runnable() { - @Override - public void run() { - Entity entity = player.getServerWorld().getEntityByID(entityId); - if (entity instanceof EntityPlayer && entity != player) { - NBTEdit.proxy.sendMessage(player, "Error - You may not use NBTEdit on other Players", TextFormatting.RED); - NBTEdit.log(Level.WARN, player.getName() + " tried to use NBTEdit on another player, " + entity.getName()); - } - if (entity != null) { - NBTTagCompound tag = new NBTTagCompound(); - entity.writeToNBT(tag); - INSTANCE.sendTo(new EntityNBTPacket(entityId, tag), player); - } else { - NBTEdit.proxy.sendMessage(player, "\"Error - Unknown EntityID #" + entityId, TextFormatting.RED); - } - } - }); - } - } + /** + * Sends a Entity's nbt data to the player for editing. + * + * @param player The player to send the Entity data to. + * @param entityId The id of the Entity. + */ + public void sendEntity(final EntityPlayerMP player, final int entityId) { + if (NBTEdit.proxy.checkPermission(player)) { + player.getServerWorld().addScheduledTask(new Runnable() { + @Override + public void run() { + Entity entity = player.getServerWorld().getEntityByID(entityId); + if (entity instanceof EntityPlayer && entity != player && !NBTEdit.editOtherPlayers) { + NBTEdit.proxy.sendMessage(player, "Error - You may not use NBTEdit on other Players", TextFormatting.RED); + NBTEdit.log(Level.WARN, player.getName() + " tried to use NBTEdit on another player, " + entity.getName()); + return; + } + if (entity != null) { + NBTTagCompound tag = new NBTTagCompound(); + entity.writeToNBT(tag); + INSTANCE.sendTo(new EntityNBTPacket(entityId, tag), player); + } else { + NBTEdit.proxy.sendMessage(player, "\"Error - Unknown EntityID #" + entityId, TextFormatting.RED); + } + } + }); + } + } } diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/TileNBTPacket.java b/src/main/java/com/mcf/davidee/nbtedit/packets/TileNBTPacket.java index 35a7f88..85083c0 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/TileNBTPacket.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/TileNBTPacket.java @@ -16,13 +16,20 @@ import org.apache.logging.log4j.Level; public class TileNBTPacket implements IMessage { - /** The block of the tileEntity. */ + /** + * The block of the tileEntity. + */ protected BlockPos pos; - /** The nbt data of the tileEntity. */ + /** + * The nbt data of the tileEntity. + */ protected NBTTagCompound tag; - /** Required default constructor. */ - public TileNBTPacket() {} + /** + * Required default constructor. + */ + public TileNBTPacket() { + } public TileNBTPacket(BlockPos pos, NBTTagCompound tag) { this.pos = pos; diff --git a/src/main/java/com/mcf/davidee/nbtedit/packets/TileRequestPacket.java b/src/main/java/com/mcf/davidee/nbtedit/packets/TileRequestPacket.java index ffa3a7e..6b7129c 100644 --- a/src/main/java/com/mcf/davidee/nbtedit/packets/TileRequestPacket.java +++ b/src/main/java/com/mcf/davidee/nbtedit/packets/TileRequestPacket.java @@ -10,12 +10,17 @@ import org.apache.logging.log4j.Level; public class TileRequestPacket implements IMessage { - /** The position of the tileEntity requested. */ + /** + * The position of the tileEntity requested. + */ private BlockPos pos; - /** Required default constructor. */ - public TileRequestPacket() {} - + /** + * Required default constructor. + */ + public TileRequestPacket() { + } + public TileRequestPacket(BlockPos pos) { this.pos = pos; }