无法将VirtualInventory转换为Inventory Minecraft

时间:2015-01-05 22:43:50

标签: java minecraft bukkit

package me.bukkit.Garnetty;

import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public class VirtualInventory {

    Inventory inventory;
    String owner;
    String name;
    int slots;

    /** 
     * @param Player p
     * @param Integer slots
     * @param String name
     */
    public VirtualInventory(Player p, Integer slots, String name) {

        this.inventory = Bukkit.createInventory(p, slots, name);
        this.owner = p.getName();
        this.name = name;
    }

    /** Returns the virtual inventory ID
     * 
     *  @return The current inventory ID*/
    public Inventory getInventory() {
        return this.inventory;
    }

    /** Returns the owner of the inventory*/
    public String getOwner() {
        return this.owner;
    }

    /** Returns the name of the inventory */
    public String getName() {
        return this.name;
    }

    /**
     * Creates a new virtual inventory with the player as the holder
     *  
     * @param Player p The player that will be the holder
     * @param Integer slots - The amount of slots (divisable by 9)
     * @param String name - The name of the inventory shown at the top
     * 
     * @return The created Virtual Inventory
     */

    public static VirtualInventory createInventory(Player p, Integer slots, String name) {

        VirtualInventory vi = new VirtualInventory(p, slots, name);

        return vi;

    }

    /**
     * The HashMap that the inventories will be saved in for later loading
     */
    static HashMap<String, VirtualInventory> inventories = new HashMap<String, VirtualInventory>();

    public static void saveInventory(Player player, VirtualInventory vi) {

        inventories.put(player.getName(), vi);


    }

    /**
     * Opens the inventory of the specified player
     * @param Player p - The player who's inventory will be opened
     */
    public static void loadInventory(Player p) throws NullPointerException {

        p.openInventory(inventories.get(p.getName()));
        /* Get an error right here, it says that I have to cast it to Inventory or let
         * it implement inventory, but when I cast it, the console throws a ClassCastException
         * and if I try and make it implement inventory, there is an error stating how
         * a listener is already listening
         */
    }


}

我尝试将VirtualInventory转换为Inventory,但我不能,它要求我实现库存,但是当我这样做时,控制台会抛出Cast异常,当我实现它时实现库存界面,我得到了一个关于已经听的听众的错误。

1 个答案:

答案 0 :(得分:1)

如果您希望能够将VirtualInventory投射到Inventory,则必须VirtualInventory延长Inventory

public class VirtualInventory extends Inventory{

  //code

}

这称为inheritance