Get a chest inventory on interact

Discussion in 'Plugin Development' started by travja, May 9, 2012.

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

    travja

    What does that have to do with it crashing the server?
     
  2. Offline

    Craftiii4

    Thats just the code that i used to do somthing simular.

    If you post your latest code, the part that is crashing, i might be able to help.
     
  3. Offline

    travja

    Craftiii4 The problem is I don't know what aspect of the chest refilling is causing it to crash...
    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. int list056;
    3. list056 = 0;
    4. int limit;
    5. limit = getConfig().getStringList("StorageXYZ").size();
    6. while(limit > list056){
    7. String xyz2 = getConfig().getStringList("StorageXYZ").get(list056);
    8. int chestx = getConfig().getInt("Storage." + xyz2 + ".Location.X");
    9. int chesty = getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    10. int chestz = getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    11. String chestw = getConfig().getString("Storage." + xyz2 + ".Location.W");
    12. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    13. exists = false;
    14. if(blockatlocation.getState() instanceof Chest){
    15. exists = true;
    16. Chest chest = (Chest) blockatlocation.getState();
    17. chest.getInventory().clear();
    18. ItemStack[] itemsinchest = null;
    19. Object o = getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    20. if(o instanceof ItemStack[]){
    21. itemsinchest = (ItemStack[]) o;
    22. }else if(o instanceof List){
    23. itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    24. }else{
    25. try{
    26. throw new Exception();
    27. }catch (Exception e) {
    28. e.printStackTrace();
    29. }
    30. chest.getInventory().setContents(itemsinchest);
    31. }
    32. }
    33. }
    34. }
     
  4. try{
    throw new Exception();
    }catch (Exception e) {
    e.printStackTrace();
    }
    Umm????
     
  5. Offline

    travja

    I don't know myself although I don't think that was the cause Craftiii4 you know why the problem is?
     
  6. We never know what you mean whit error, plz copy it and send.
     
  7. Offline

    travja

    By error I mean something is going wrong. Nothing is sent to the console as the whole server freezes upon trying to refill the chests... Craftiii4 So you really have used this before?

    I really dunno!!! Driving me nuts!

    Code:
    Storage:
      -90,63,311:
        Location:
          X: -90
          Y: 63
          Z: 311
          W: world
        ItemsInStorage:
        - ==: org.bukkit.inventory.ItemStack
          type: BUCKET
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
    StorageXYZ:
    - -90,63,311
    
    That's what it puts into the config.yml When I try to refill with this code:
    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. int list056;
    3. list056 = 0;
    4. int limit;
    5. limit = getConfig().getStringList("StorageXYZ").size();
    6. while(limit > list056){
    7. String xyz2 = getConfig().getStringList("StorageXYZ").get(list056);
    8. int chestx = getConfig().getInt("Storage." + xyz2 + ".Location.X");
    9. int chesty = getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    10. int chestz = getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    11. String chestw = getConfig().getString("Storage." + xyz2 + ".Location.W");
    12. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    13. exists = false;
    14. if(blockatlocation.getState() instanceof Chest){
    15. exists = true;
    16. Chest chest = (Chest) blockatlocation.getState();
    17. chest.getInventory().clear();
    18. ItemStack[] itemsinchest = null;
    19. Object o = getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    20. if(o instanceof ItemStack[]){
    21. itemsinchest = (ItemStack[]) o;
    22. }else if(o instanceof List){
    23. itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    24. }else{
    25. try{
    26. throw new Exception();
    27. }catch (Exception e) {
    28. e.printStackTrace();
    29. }
    30. chest.getInventory().setContents(itemsinchest);
    31. }
    32. }
    33. }
    34. }

    It then crashes the server! If ANYONE could provide ANY help on this I would GREATLY appreciate it! Thanks in advance!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  8. You never increasing list056, isn;t it?
     
  9. Offline

    travja

    I have no clue....
     
  10. why you dont know whats your own code doing, as far I seen the variable list056 keeps zero
    print the value out each iterator of the loop to verify
     
  11. Offline

    travja

    This isn't my code... Craftiii4 gave it to me. So you want me to print the value of list056 when I link a chest or when I try to refill?
     
  12. Offline

    Craftiii4

    Don't use the exact code... just see how I did it and see how you might be able to. Copying the code wont help because a) you don't have all of it, b) your not learning, c) its not designed for the exact use your using it for.


    If you want help, log stages through out and print the errors.
     
  13. Offline

    travja

    Yeah... I have no clue how to do this, that's why I'm asking you. Everything works until I try to refill, then it crashes the server.
     
  14. Offline

    Craftiii4

    Could you post the error then please?

    Try logging the code as you go along as well.
     
  15. Offline

    travja

    Craftiii4 There is no error, however, I'll log as I go.

    K, when I run the command it spams this infinately:

    2012-05-28 11:19:49 [INFO] While run!
    2012-05-28 11:19:49 [INFO] Ints registered!
    2012-05-28 11:19:49 [INFO] Is Chest!


    Logged those right after the While, after the ints are taken from the config and after it recongnizes it's a chest. So it's with the while...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  16. Offline

    Craftiii4

    Can you log in the while as well please
     
  17. Offline

    travja

    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. int list056;
    3. list056 = 0;
    4. int limit = plugin.getConfig().getStringList("StorageXYZ").size();
    5. System.out.println("Command Run!");
    6. while(limit > list056){
    7. System.out.println("While run!");
    8. String xyz2 = plugin.getConfig().getStringList("StorageXYZ").get(list056);
    9. int chestx = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.X");
    10. int chesty = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    11. int chestz = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    12. String chestw = plugin.getConfig().getString("Storage." + xyz2 + ".Location.W");
    13. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    14. plugin.exists = false;
    15. System.out.println("Ints registered!");
    16. if(blockatlocation.getState() instanceof Chest){
    17. plugin.exists = true;
    18. System.out.println("Is Chest!");
    19. Chest chest = (Chest) blockatlocation.getState();
    20. chest.getInventory().clear();
    21. ItemStack[] itemsinchest = null;
    22. Object o = plugin.getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    23. if(o instanceof ItemStack[]){
    24. itemsinchest = (ItemStack[]) o;
    25. }else if(o instanceof List){
    26. itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    27. }else{
    28. try{
    29. throw new Exception();
    30. }catch (Exception e) {
    31. e.printStackTrace();
    32. }
    33. System.out.println("Try run!");
    34. chest.getInventory().setContents(itemsinchest);
    35. System.out.println("Refilled!");
    36. }
    37. }
    38. }
    39. }


    That's what I'm running, with the printing and everything..
     
  18. Offline

    Craftiii4

    Try replacing with this?

    Code:java
    1. if (blockatlocation.getState() instanceof Chest) {
    2.  
    3. exists = true;
    4.  
    5. Chest chest = (Chest) blockatlocation.getState();
    6.  
    7. chest.getInventory().clear();
    8.  
    9. ItemStack[] itemsinchest = null;
    10. Object o = getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    11. if (o instanceof ItemStack[])itemsinchest = (ItemStack[]) o;
    12. else if (o instanceof List)itemsinchest = (ItemStack[]) ((List) o).toArray(new ItemStack[0]);
    13. else
    14. try {
    15. throw new Exception();
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19.  
    20. chest.getInventory().setContents(itemsinchest);
    21. }
     
  19. Offline

    travja

    Isn't that the same??


    I don't think the value of list056 is changing because whenever limit is greater that list056 it will run that code... So because list056 never changes it just makes an infinite loop and crashes the server...
     
  20. Offline

    Craftiii4

    Just try it please.

    Yes you need to change the value by 1 each time. It is in the code i gave you above though?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  21. Offline

    travja

    Craftiii4 None of the code I have seen has increased the value of list056 by one anywhere... So that would probably explain the loop.... So I need to put that right after it restocks the chest right?

    Craftiii4 Using this code:
    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. int list056;
    3. list056 = 0;
    4. int limit = plugin.getConfig().getStringList("StorageXYZ").size();
    5. System.out.println("Command Run!");
    6. while(limit > list056){
    7. System.out.println("While run!");
    8. String xyz2 = plugin.getConfig().getStringList("StorageXYZ").get(list056);
    9. int chestx = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.X");
    10. int chesty = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    11. int chestz = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    12. String chestw = plugin.getConfig().getString("Storage." + xyz2 + ".Location.W");
    13. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    14. plugin.exists = false;
    15. System.out.println("Ints registered!");
    16. if(blockatlocation.getState() instanceof Chest){
    17. plugin.exists = true;
    18. System.out.println("Is Chest!");
    19. Chest chest = (Chest) blockatlocation.getState();
    20. chest.getInventory().clear();
    21. ItemStack[] itemsinchest = null;
    22. Object o = plugin.getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    23. if(o instanceof ItemStack[]){
    24. itemsinchest = (ItemStack[]) o;
    25. System.out.println("o is an ItemStack[]");
    26. }else if(o instanceof List){
    27. System.out.println("o isn't and ItemStack[]");
    28. itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    29. }
    30. try{
    31. throw new Exception();
    32. }catch (Exception e) {
    33. e.printStackTrace();
    34. }
    35. System.out.println("Try run!");
    36. limit = limit+1;
    37. chest.getInventory().setContents(itemsinchest);
    38. System.out.println("Refilled!");
    39. }
    40. }
    41. }

    I get this spammed:

    2012-05-28 16:04:46 [INFO] While run!
    2012-05-28 16:04:46 [INFO] Ints registered!
    2012-05-28 16:04:46 [INFO] Is Chest!
    2012-05-28 16:04:46 [INFO] o isn't and ItemStack[]
    2012-05-28 16:04:46 [SEVERE] java.lang.Exception
    2012-05-28 16:04:46 [SEVERE] at me.Travja.HungerArena.HaCommands.onCommand(HaCommands.java:270)
    2012-05-28 16:04:46 [SEVERE] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    2012-05-28 16:04:46 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    2012-05-28 16:04:46 [SEVERE] at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
    2012-05-28 16:04:46 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    2012-05-28 16:04:46 [INFO] Try run!
    2012-05-28 16:04:46 [INFO] Refilled!


    EDIT: Nevermind all that, I was increasing limit instead of list056! And is the try catch block necissary?

    OK! I got it working using this code to store:
    Code:java
    1. @EventHandler
    2. public void ChestSaves(PlayerInteractEvent event){
    3. Block block = event.getClickedBlock();
    4. Player p = event.getPlayer();
    5. if(p.getItemInHand().getType()== Material.BLAZE_ROD && event.getAction() == Action.LEFT_CLICK_BLOCK){
    6. if(block.getState() instanceof Chest){
    7. ItemStack[] itemsinchest = ((Chest) block.getState()).getInventory().getContents();
    8. int blockx = block.getX();
    9. int blocky = block.getY();
    10. int blockz = block.getZ();
    11. String blockw = block.getWorld().getName().toString();
    12. if(!plugin.getConfig().contains("Storage." + blockx + "," + blocky + "," + blockz + ".Location.X")){
    13. plugin.getConfig().addDefault("Storage." + blockx + "," + blocky + "," + blockz + ".Location.X", blockx);
    14. plugin.getConfig().addDefault("Storage." + blockx + "," + blocky + "," + blockz + ".Location.Y", blocky);
    15. plugin.getConfig().addDefault("Storage." + blockx + "," + blocky + "," + blockz + ".Location.Z",blockz);
    16. plugin.getConfig().addDefault("Storage." + blockx + "," + blocky + "," + blockz + ".Location.W", blockw);
    17. plugin.getConfig().addDefault("Storage." + blockx + "," + blocky + "," + blockz + ".ItemsInStorage", itemsinchest);
    18. }else{
    19. plugin.getConfig().set("Storage." + blockx + "," + blocky+ "," + blockz + ".Location.X",blockx);
    20. plugin.getConfig().set("Storage." + blockx + "," + blocky + "," + blockz + ".Location.Y", blocky);
    21. plugin.getConfig().set("Storage." + blockx + "," + blocky + "," + blockz + ".Location.Z", blockz);
    22. plugin.getConfig().set("Storage." + blockx + "," + blocky + "," + blockz + ".Location.W", blockw);
    23. plugin.getConfig().set("Storage." + blockx + "," + blocky + "," + blockz + ".ItemsInStorage", itemsinchest);
    24. }
    25. List<String> list2 = plugin.getConfig().getStringList("StorageXYZ");
    26. list2.add(blockx + "," + blocky + "," + blockz);
    27. plugin.getConfig().set("StorageXYZ", list2);
    28. plugin.getConfig().options().copyDefaults(true);
    29. plugin.saveConfig();
    30. p.sendMessage(ChatColor.GREEN + "Chest Stored!");
    31. }
    32. }
    33. }

    And this code to refill:
    Code:java
    1. if(args[0].equalsIgnoreCase("Refill")){
    2. int list056;
    3. list056 = 0;
    4. int limit = plugin.getConfig().getStringList("StorageXYZ").size();
    5. while(limit > list056){
    6. String xyz2 = plugin.getConfig().getStringList("StorageXYZ").get(list056);
    7. int chestx = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.X");
    8. int chesty = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Y");
    9. int chestz = plugin.getConfig().getInt("Storage." + xyz2 + ".Location.Z");
    10. String chestw = plugin.getConfig().getString("Storage." + xyz2 + ".Location.W");
    11. Block blockatlocation = Bukkit.getWorld(chestw).getBlockAt(chestx, chesty, chestz);
    12. plugin.exists = false;
    13. if(blockatlocation.getState() instanceof Chest){
    14. plugin.exists = true;
    15. Chest chest = (Chest) blockatlocation.getState();
    16. chest.getInventory().clear();
    17. ItemStack[] itemsinchest = null;
    18. Object o = plugin.getConfig().get("Storage." + xyz2 + ".ItemsInStorage");
    19. if(o instanceof ItemStack[]){
    20. itemsinchest = (ItemStack[]) o;
    21. }else if(o instanceof List){
    22. itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    23. }
    24. list056 = list056+1;
    25. chest.getInventory().setContents(itemsinchest);
    26. }
    27. }
    28. if(limit== list056){
    29. p.sendMessage(ChatColor.GREEN + "All chests refilled!");
    30. }
    31. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  22. Offline

    TheAwesomeJocke

    Am i allowed to copy the code? I really want to create my own Survival Games plugin.
     
  23. Offline

    MrMag518

    If it's not Copyrighted, I may believe you can. (Correct me if I'm wrong please)
     
  24. Offline

    TheAwesomeJocke

    I doesn't seem to work for me...
     
  25. Offline

    Craftiii4

    Sure ^^

    Post your code and I will try and help.
     
  26. Offline

    TheAwesomeJocke

    There's not much to post because I just copied the code that travja wrote.
    EDIT:
    itemsinchest = (ItemStack[]) ((List<ItemStack>) o).toArray(new ItemStack[0]);
    But I get an yellow line beneath ((List<ItemStack>) o)
     
  27. Offline

    Craftiii4

    yer, thats fine. Is there any error?
     
  28. Offline

    TheAwesomeJocke

    I'm sorry I'm really new to making plugins but do you have to create a config file? Because when I left click with a blaze rod on a chest it doesn't say anything. And when I type /refill it says "An internal error occurred while attempting to perform this command".
     
  29. Offline

    travja

    Just follow what it says, add @SuppressWarnings("unchecked")

    And lol, aren't we all trying to make Hunger Games plugins??
     
  30. Offline

    CorrieKay

    rule of thumb, never copypaste code :p
     
Thread Status:
Not open for further replies.

Share This Page