[Solved] Wierd glitch, setting diffrent inventories for diffrent worlds

Discussion in 'Plugin Development' started by LucasEmanuel, May 31, 2012.

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

    LucasEmanuel

    Im adding a feuture for our community plugin and its diffrent inventories for diffrent worlds.

    When a player is leaving a specifik world my plugin saves that players inventory to a hashmap of this type:
    PHP:
    // Mapception! Description: HashMap<playername, HashMap<index in inventory, HashMap<Item id, item amount>>>
     
    private HashMap<StringHashMap<IntegerHashMap<IntegerInteger>>> inventories;
    But when the player has left, his/her inventory isnt cleaned as i described in the playerlistener. The player keeps his/her inventory but when the player returns to the other world the inventory is cleaned. Its supposed to be the other way arround.

    Its really complicated to describe so bear with me. For example.

    My server has a standard world, a faction world. This world is what every player that joins the server is greeted with. Its a standard survival, factions world. But then we also have a hungergames world. When a player interracts with a sign that teleports him/her to the hungergames world, my plugin is suppossed to save his/her inventory to the HashMap described above and then clean it so the player cant use any stuff from the survival world in the hungergames world. When that player then rejoins the survival world, the player should get all of his/her stuff back.

    But this is where this wierd glitch is starting to happen. I listen for the "PlayerTeleportEvent" and get the source location by calling event.getFrom(). This seems correct ey? But it doesnt appear to be so. When i compare the source world like this: "source.getWorld().getName().equals("factionsworld");" something goes wrong. It doesnt save the inventory and it doesnt clean it. Instead it does nothing and just lets the player join the hungergames world with all his/her stuff safely in the inventory.

    But when the player tries to go back to the survival world the inventory is saved and cleared. But thats not what i described in the if-statement.

    Are you still with me? Well you can look at the code below and see if you can spot the problem because im just confused.

    PlayerListener (the PlayerTeleportEvent is at row 327):
    https://github.com/Chilinot/LetsPla...etsplay/listeners/LetsPlayPlayerListener.java

    InventoryManager, the class that stores and manages all the inventories i want to save:
    https://github.com/Chilinot/LetsPla...tsplay/managers/LetsPlayInventoryManager.java
     
  2. I recommend saving ItemStacks instead of your own format, whiot the format you use, you loose al enchants and wool coler
     
  3. Offline

    LucasEmanuel

    Yes i know, the first try i saved a nested HashMap like this HashMap<Player, HashMap<index in inventory, ItemStack>> but then i couldnt save it because it couldnt serialize player, so i used the playername instead, but then it couldnt serialize the itemstack so i had to do it my way.
     
  4. Offline

    Njol

    NuclearW and LucasEmanuel like this.
  5. Offline

    LucasEmanuel

  6. you can save an itemstack to an config file as far I know, made use the methode for that, but save the hashmap it returns to an file instead or something
     
  7. Offline

    LucasEmanuel

    Well yea that should work. But i prefer saving it in bit-format that way i dont have to worry about admins going inside of the file and messing everything up :)

    Well i seem to have identified the problem. Appearently the PlayerTeleportedEvent gets called twice if the player gets teleported to another world, but only once in the same world.

    This causes my code to execute twice wich causes the wierd glitch that makes my if-statements seem like the go reverse.

    By adding a check for the destination to make sure that it is another world the player is going to i fixed the glitch and the inventories now gets saved correctly. Thanks so much for the help tho :)


    EDIT: I also rewrote the way of saving the hashmaps with the help from the link Njol gave me :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page