How can I cast an Object to an ItemStack?

Discussion in 'Plugin Development' started by aloxMCT, Jan 1, 2021.

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

    aloxMCT

    Please I need help :( When I try to cast the Object "obj" to an Itemstack after I see it's an IItemStack it doest cast it :(.

    Here's the code:

    Code:
    package alox.proyecto;
    
    import java.lang.reflect.Method;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.entity.Player;
    
    public class comando implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                    try{
                        Object obj = ItemStacks.class.getMethod(args[0]);
                        if(((Method) obj).getReturnType().equals(ItemStack.class)){
                            ItemStack ite = (ItemStack) obj;
                            p.getInventory().addItem(ite);
                           
                        }
                    }catch(Exception e){
                }
            }
            return true;
        }
    
    
    
    }
    
    The method I'm refering to is something like this:

    Code:
    public class ItemStacks {
       
        public static ItemStack item1() {
            <ItemStack things>
            return item1;
            }
    
            public static ItemStack item2() {
            <ItemStack things>
            return item2;
            }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 1, 2021
  2. Offline

    Mathias Eklund

    Erm, the way you are trying to get different item stacks is ultimately not done in a good way.

    Try making the ItemStacks an instanced object that creates the item stacks in its constructor and add them to either a List or a HashMap where you can assign them an ID. After that, you could simply create a function in the object class to get an item from either the List or the HashMap.
     
  3. Offline

    aloxMCT

    Hi guys I just found out other way to do it, thanks to @Mathias Eklund for the list idea<3

    Command class:
    Code:
    public class comando implements CommandExecutor{
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if(!args[0].isEmpty()) {
                    if(args[0].equals("*")) {
                        for (ItemStack element : lista.item()) {
                            p.getInventory().addItem(element);                       
                        }return true;
                    }
                    for(int i = 0;i<lista.item().size();i++) {
                        if(lista.item().get(i).getItemMeta().getDisplayName().toLowerCase().replace(" ", "").contains(args[0].toLowerCase())) {
                            p.getInventory().addItem(lista.item().get(i));
                            return true;
                        }
                    }
                }
               
                return false;
            }
            return true;
        }
    
    
    
    }
    ItemStacks class:
    Code:
    public class ItemStacks {
      
        public static ItemStack item1() {
            <ItemStack things>
            return item1;
            }
    
            public static ItemStack item2() {
            <ItemStack things>
            return item2;
            }
    }
    Lista class:
    Code:
    public class lista extends ItemStack{
            public static ArrayList<ItemStack> item() {
                ArrayList<ItemStack> list = new ArrayList<ItemStack>();
                list.add(ItemStacks.item1());
                list.add(ItemStacks.item2());
                return list;
            }
    }
    
     
  4. Offline

    Strahan

    This is a weird way to handle a list of ItemStacks, and it's static abuse to boot. Instead of making a class just for a list, simply put the list in a class of which you already have access.
     
Thread Status:
Not open for further replies.

Share This Page