Solved Create New PlayerInventory

Discussion in 'Plugin Development' started by Smeary_Subset, Mar 23, 2022.

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

    Smeary_Subset

    Hello, I am creating a kit pvp-like plugin that features different kits players can fight with, each of which are represented by a PlayerInventory variable in my AbstractKit class. The method below shows how I generate each kit:

    Code:
    private final void generateKit() {
    Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
    inv.setContents(hotbar());
    ((PlayerInventory) inv).setArmorContents(armor());
    }
    I receive the following error in the last line of the method:

    Code:
    Caused by: java.lang.ClassCastException: class org.bukkit.craftbukkit.v1_18_R1.inventory.CraftInventoryCustom cannot be cast to class org.bukkit.inventory.PlayerInventory (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftInventoryCustom and org.bukkit.inventory.PlayerInventory are in unnamed module of loader java.net.URLClassLoader @3b764bce)
    Why can I not do this if the inventory being created is clearly a PlayerInventory? I have also tried declaring the initial inv variable as a PlayerInventory, but I get the same error.

    In the past, I worked around this problem by getting an instance of a PlayerInventory through a random one of the players in the game (i.e. #player.getInventory()), but I want a real solution.

    Any ideas?
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Smeary_Subset

    That would require me to pass in a player's inventory into the AbstractKit class, which I do not want to do because I don't believe that to be clean code. The AbstractKit represents the kit, not a specific player's inventory.

    Additionally, I set up my code so that, at the beginning of the game, only one (final) instance of each kit is created for the entire game. Whenever a player selects that kit, their inventory is set to that kit's contents.
     
  4. Online

    timtower Administrator Administrator Moderator

    @Smeary_Subset Map<uint slot, ItemStack item>
    And what does it matter if you pass the inventory along?
     
  5. Offline

    Smeary_Subset

    Functionality wise, it does not. I've been reading Clean Code by Robert Martin and it talks about the importance of being intentional with what attributes you include in what class, so I was trying not to include a random instance of a player's inventory.

    If you were another developer looking at my code, you would probably ask "what's the significance of this random player's inventory being passed into this class instead of another PlayerInventory being created?" You'd be driving yourself crazy trying to find the answer when the answer was that it was simply a sloppy solution by the developer (me).

    Regardless...whatever, I tried ¯\_(ツ)_/¯

    Before I mark as solved, do you have any idea why I was getting that initial error? I even printed out #inv.getType() and it came up as Player, so there's no doubt that it's a PlayerInventory but it still yells at me.
     
  6. Online

    timtower Administrator Administrator Moderator

    @Smeary_Subset Can't make new PlayerInventories I believe.
    And then you should just set the content without making a new inventory.
    If I see an inventory being passed along and being changed then I know perfectly well why it is there.
     
  7. Offline

    Smeary_Subset

    Ok, you've convinced me. After all, the kit is technically an inventory. I'll refactor my code.
     
Thread Status:
Not open for further replies.

Share This Page