Solved Need some help, Itemstacks and arrays, and PlayerTeleportEvents

Discussion in 'Plugin Development' started by MaxFireIce, Nov 8, 2016.

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

    MaxFireIce

    So, working on a plugin, have 2 issues. The first is that I am saving the players inventory like this:

    Code:
        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent event){
            String oldWorld = event.getFrom().getWorld().toString();
            String newWorld = event.getTo().getWorld().toString();
           
            // if they are teleporting but not switching worlds, screw it
            if (oldWorld == newWorld) return;
           
            Player player = event.getPlayer();
            String playerName = player.getName();
            FileConfiguration config = Main.plugin.getConfig();
            ItemStack[] playerOldInv = player.getInventory().getContents();
           
            /*
             * if the config does'nt have an instance of the player's, world's, inventory, add it.
             */
           
            if (!config.contains(playerName + ".Worlds." + oldWorld + ".Inventory" + playerOldInv)){
                config.addDefault(playerName + ".Worlds." + oldWorld + ".Inventory", playerOldInv);
                Main.plugin.saveConfig();
            }
    And then I set their inventory like this:

    Code:
            /*
             * if the config does not have a saved inventory for the world the player is entering,
             * then create a new one and save it. Otherwise set the players inventory to the saved one
             */
           
            if (!config.contains(playerName + ".Worlds." + newWorld + ".Inventory")){
                player.getInventory().clear();
                ItemStack[] playerNewInv = player.getInventory().getContents();
                config.addDefault(playerName + ".Worlds." + newWorld + ".Inventory", playerNewInv);
                Main.plugin.saveConfig();
            } else {
                // this is where it claims the error is
                player.getInventory().setContents((ItemStack[]) config.get(playerName + ".Worlds." + newWorld + ".Inventory"));
            }
    Now, it was working 2 seconds ago, however i added 2 events, PlayerQuit, and PlayerJoin.

    PlayerJoin:

    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
           
            Player player = event.getPlayer();
            String playerWorld = player.getLocation().getWorld().toString();
           
            if (Main.plugin.getConfig().contains(player.getName() + ".Worlds." + playerWorld + "Inventory")){
                player.getInventory().setContents((ItemStack[]) Main.plugin.getConfig().get(player.getName() + ".Worlds." + playerWorld + ".Inventory"));
            } else {
                Main.plugin.getConfig().addDefault(player.getName() + ".Worlds." + playerWorld + "Inventory", player.getInventory().getContents());
                Main.plugin.saveConfig();
            }
           
        }
    PlayerQuit:

    Code:
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event){
           
            Player player = event.getPlayer();
            String playerWorld = player.getLocation().getWorld().toString();
           
            if (!Main.plugin.getConfig().contains(player.getName() + ".Worlds." + playerWorld + ".Inventory")){
                Main.plugin.getConfig().addDefault(player.getName() + ".Worlds." + playerWorld + ".Inventory", player.getInventory().getContents());
                Main.plugin.saveConfig();
            } else {
                Main.plugin.getConfig().set(player.getName() + ".Worlds." + playerWorld + ".Inventory", player.getInventory().getContents());
                Main.plugin.saveConfig();
            }
           
        }
    Now, when i join the game, i get this:
    PHP:
    Caused byjava.lang.ClassCastExceptionjava.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
            
    at worldinventories.events.PlayerSwitchWorld.onPlayerTeleport(PlayerSwitchWorld.java:48) ~[?:?]
    I have showed you where the error is, please help.

    Also, I have my events registered and everything, but whenever a player joins, it claims that it is a PlayerTeleportEvent:

    Code:
    [07:12:59 ERROR]: Could not pass event PlayerTeleportEvent to WorldInventories v1.0
    org.bukkit.event.EventException
    Im getting that when i join. But it should not be a teleport event when they join.

    Thanks in advance :)
     
  2. Online

    timtower Administrator Administrator Moderator

    @MaxFireIce
    1. You don't need addDefault that much.
      Just using set will do, also if the value isn't there yet.
    2. You are getting a list of ItemStacks, you are casting that to an Array.
      You can't do that.
      Loop through the list instead and put them in the array.
     
  3. Offline

    MaxFireIce

    Umm, where am I casting it to an Array? XD I dont even have Arrays in my imports. Thx for the tip btw ;)
    I was using it so it would set instead of add everytime, but i had no idea that worked as well

    @Lordloss
    I see where it is, why will that not work?
     
    Last edited: Nov 8, 2016
  4. Offline

    Lordloss

    Btw you are comparing Strings with ==, which will not work.
     
  5. Online

    timtower Administrator Administrator Moderator

    @MaxFireIce
    (ItemStack[]) Main.plugin.getConfig().get(player.getName() + ".Worlds." + playerWorld + ".Inventory")

    That ItemStack[] is an array
     
  6. Offline

    MaxFireIce

    @timtower

    But why? I saved it as an itemstack[]...

    Code:
            Player player = event.getPlayer();
            String playerName = player.getName();
            FileConfiguration config = Main.plugin.getConfig();
            ItemStack[] playerOldInv = player.getInventory().getContents();
         
            /*
             * if the config does'nt have an instance of the player's, world's, inventory, add it.
             */
         
            if (!config.contains(playerName + ".Worlds." + oldWorld + ".Inventory" + playerOldInv)){
                config.addDefault(playerName + ".Worlds." + oldWorld + ".Inventory", playerOldInv);
                Main.plugin.saveConfig();
            }
    And here:

    Code:
    if (!config.contains(playerName + ".Worlds." + newWorld + ".Inventory")){
    player.getInventory().clear();
    ItemStack[] playerNewInv = player.getInventory().getContents();
    config.addDefault(playerName + ".Worlds." + newWorld + ".Inventory", playerNewInv);
    Main.plugin.saveConfig();
    
     
  7. Online

    timtower Administrator Administrator Moderator

    @MaxFireIce Array and List are storage wise the same.
     
  8. Offline

    MaxFireIce

    @timtower
    I saved it as ItemStack[], how does it turn into list?
     
  9. Online

    timtower Administrator Administrator Moderator

    @MaxFireIce It doesn't store what type it is.
    That is why it can mess up things.
     
  10. Offline

    MaxFireIce

  11. Online

    timtower Administrator Administrator Moderator

  12. Offline

    MaxFireIce

    @timtower
    well, is there any other way to save the inventory to the config then add it again later?
     
  13. Online

    timtower Administrator Administrator Moderator

    @MaxFireIce Could loop through it and set it with key-values
    Key would be the slot, value the itemstack.
     
  14. Offline

    MaxFireIce

    Last edited: Nov 8, 2016
Thread Status:
Not open for further replies.

Share This Page