Cannot get this thing to work!

Discussion in 'Plugin Development' started by HeadGam3z, May 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    HeadGam3z

    I need to serialize a player's inventory. So I want to save an inventory, and load it back when called upon to do so. My code:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("duty")) {
    2. Player player = (Player) sender;
    3. if (!sender.hasPermission("fcGuardPlugin.on")) {
    4. sender.sendMessage("You do not have permission");
    5. }
    6. if (sender.hasPermission("fcGuardPlugin.on")) {
    7. if (args.length == 1 && args[0].equalsIgnoreCase("on")) {
    8. if (sender instanceof Player) {
    9. File fcGuardPlugin = new File("plugins"
    10. + File.separator + "fcGuardPlugin"
    11. + File.separator + "Data");
    12. File GuardInv = new File(fcGuardPlugin,
    13. player.getName() + ".yml");
    14. YamlConfiguration Guardconfig = YamlConfiguration
    15. .loadConfiguration(GuardInv);
    16. PlayerInventory inv = player.getInventory();
    17. String invAsString = InventoryToString(inv);
    18.  
    19. if (!GuardInv.exists()) {
    20. try {
    21. GuardInv.createNewFile();
    22. } catch (IOException e) {
    23. e.printStackTrace();
    24. }
    25. Msg(" "
    26. + c.getString("Duty_On_Message")
    27. .replace("%p", player.getName())
    28. .replace("&", "§"));
    29.  
    30. Guardconfig.set(player.getName() + " Inventory",
    31. invAsString);
    32. Guardconfig.set(player.getName() + " OnGuard",
    33. Boolean.valueOf(true));
    34.  
    35. try {
    36. Guardconfig.save(GuardInv);
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40.  
    41. /* all items below deleted atm
    42.   player.getInventory().clear();
    43.   player.getInventory().setArmorContents(null);
    44.   player.getInventory().setHelmet(getHelm());
    45.   player.getInventory().setChestplate(getChest());
    46.   player.getInventory().setLeggings(getLegs());
    47.   player.getInventory().setBoots(getBoots());
    48.  
    49.   player.getInventory().addItem(
    50.   new ItemStack[] { getSword() });
    51.   player.getInventory().addItem(
    52.   new ItemStack[] { getBow() });
    53.   player.getInventory().addItem(
    54.   new ItemStack[] { getStick() });
    55.   player.getInventory().addItem(
    56.   new ItemStack[] { getFood() });
    57.   player.getInventory().addItem(
    58.   new ItemStack[] { getApples() });
    59.   player.getInventory().addItem(
    60.   new ItemStack[] { getArrows() });*/
    61. return true;
    62. }
    63. }
    64. } else if (args.length == 1 && args[0].equalsIgnoreCase("off")) {
    65. Msg("Debug 1"); // in-game debug
    66. File fcGuardPlugin = new File("plugins" + File.separator
    67. + "fcGuardPlugin" + File.separator + "Data");
    68. File GuardInv = new File(fcGuardPlugin, player.getName()
    69. + ".yml");
    70. YamlConfiguration Guardconfig = YamlConfiguration
    71. .loadConfiguration(GuardInv);
    72. Inventory i = StringToInventory(player.getName()
    73. + " Inventory");
    74. Msg("Debug 2"); // in-game debug
    75. if (Guardconfig.getBoolean(player.getName() + " OnGuard")) {
    76. Msg(c.getString("Duty_Off_Message")
    77. .replace("%p", player.getName())
    78. .replace("&", "§"));
    79.  
    80. player.getInventory().clear();
    81. player.getInventory().setArmorContents(null);
    82.  
    83.  
    84. player.getInventory().setContents(i.getContents());
    85. Msg("Debug 3"); // in-game debug
    86. Guardconfig.set(player.getName() + " OnGuard",
    87. Boolean.valueOf(false));
    88. Msg("Debug 4"); // in-game debug
    89.  
    90. try {
    91. Guardconfig.save(GuardInv);
    92. } catch (IOException e) {
    93. e.printStackTrace();
    94. }
    95. return true;
    96. }
    97.  
    98. }
    99. }
    100. }
    101. return false;
    102. }
    103.  
    104. //deleted
    105.  
    106. public void Msg(String msg) {
    107. Bukkit.getServer().broadcastMessage(msg);
    108. }
    109.  
    110. @SuppressWarnings("deprecation")
    111. public static String InventoryToString(Inventory invInventory) {
    112. String serialization = invInventory.getSize() + ";";
    113. for (int i = 0; i < invInventory.getSize(); i++) {
    114. ItemStack is = invInventory.getItem(i);
    115. if (is != null) {
    116. String serializedItemStack = new String();
    117.  
    118. String isType = String.valueOf(is.getType().getId());
    119. serializedItemStack += "t@" + isType;
    120.  
    121. if (is.getDurability() != 0) {
    122. String isDurability = String.valueOf(is.getDurability());
    123. serializedItemStack += ":d@" + isDurability;
    124. }
    125.  
    126. if (is.getAmount() != 1) {
    127. String isAmount = String.valueOf(is.getAmount());
    128. serializedItemStack += ":a@" + isAmount;
    129. }
    130.  
    131. Map<Enchantment, Integer> isEnch = is.getEnchantments();
    132. if (isEnch.size() > 0) {
    133. for (Entry<Enchantment, Integer> ench : isEnch.entrySet()) {
    134. serializedItemStack += ":e@" + ench.getKey().getId()
    135. + "@" + ench.getValue();
    136. }
    137. }
    138.  
    139. serialization += i + "#" + serializedItemStack + ";";
    140. }
    141. }
    142. return serialization;
    143. }
    144.  
    145. @SuppressWarnings("deprecation")
    146. public static Inventory StringToInventory(String invString) {
    147. String[] serializedBlocks = invString.split(";");
    148. String invInfo = serializedBlocks[0];
    149. Inventory deserializedInventory = Bukkit.getServer().createInventory(
    150. null, Integer.valueOf(invInfo));
    151.  
    152. for (int i = 1; i < serializedBlocks.length; i++) {
    153. String[] serializedBlock = serializedBlocks[i].split("#");
    154. int stackPosition = Integer.valueOf(serializedBlock[0]);
    155.  
    156. if (stackPosition >= deserializedInventory.getSize()) {
    157. continue;
    158. }
    159.  
    160. ItemStack is = null;
    161. Boolean createdItemStack = false;
    162.  
    163. String[] serializedItemStack = serializedBlock[1].split(":");
    164. for (String itemInfo : serializedItemStack) {
    165. String[] itemAttribute = itemInfo.split("@");
    166. if (itemAttribute[0].equals("t")) {
    167. is = new ItemStack(Material.getMaterial(Integer
    168. .valueOf(itemAttribute[1])));
    169. createdItemStack = true;
    170. } else if (itemAttribute[0].equals("d") && createdItemStack) {
    171. is.setDurability(Short.valueOf(itemAttribute[1]));
    172. } else if (itemAttribute[0].equals("a") && createdItemStack) {
    173. is.setAmount(Integer.valueOf(itemAttribute[1]));
    174. } else if (itemAttribute[0].equals("e") && createdItemStack) {
    175. is.addEnchantment(Enchantment.getById(Integer
    176. .valueOf(itemAttribute[1])), Integer
    177. .valueOf(itemAttribute[2]));
    178. }
    179. }
    180. deserializedInventory.setItem(stackPosition, is);
    181. }
    182.  
    183. return deserializedInventory;
    184. }[/i]


    Debug message #2 does not show up when /duty off is used. I get this error:

    Code:
    [23:53:35] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duty' in plugin fcGuardPlugin v0.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.NumberFormatException: For input string: "HeadGam3z Inventory"
        at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.7.0_51]
        at java.lang.Integer.parseInt(Unknown Source) ~[?:1.7.0_51]
        at java.lang.Integer.valueOf(Unknown Source) ~[?:1.7.0_51]
        at com.gmail.mcheadgam3z.Main.StringToInventory(Main.java:317) ~[?:?]
        at com.gmail.mcheadgam3z.Main.onCommand(Main.java:123) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        ... 13 more
    
    Line 123:
    Code:java
    1. Inventory i = StringToInventory(player.getName()
    2. + " Inventory");


    Line 317:
    Code:java
    1. Inventory deserializedInventory = Bukkit.getServer().createInventory(
    2. null, Integer.valueOf(invInfo));
    3.  

    I am completely out of ideas and just frustrated; been dealing with this issue for about a week now.
     
  2. Offline

    mrkirby153

    HeadGam3z

    I am gonna go out on a limb and say that invInfo is incorrectly assigned. What should it be?
     
  3. Offline

    HeadGam3z

    mrkirby153
    What do you mean what should it be?
     
  4. Offline

    mrkirby153

    HeadGam3z

    A number. Not a string. Print out the value of invInfo and chances are, it is not what you expect.
     
  5. Offline

    HeadGam3z

    mrkirby153
    I tried to print it out by using System.out.print(invInfo); and nothing happened when it got called - other than the error I've been getting.
     
  6. Offline

    xTigerRebornx

    HeadGam3z You've made like 10 posts, with the exact same problem. When you are trying to turn the String into an Inventory, you just keep passing in "<PlayerName> Inventory", the method expects that you pass in a String that is a serialized inventory, which "<PlayerName> Inventory" clearly isn't. You pass in the invalid String, it tries to turn that into an Inventory, and errors are thrown because its not an Inventory that was serialized.
    Solution: Pass in the serialized inventory
     
  7. Offline

    HeadGam3z

    xTigerRebornx
    The serialized inventory is right after "<PlayerName> Inventory" (i.e HeadGam3z Inventory: 32#432;324234yadayda) The problem for me is, I don't know how to pass it to the serialized inventory, because I do not know how to get to it.
     
  8. Offline

    HeadGam3z

Thread Status:
Not open for further replies.

Share This Page