Solved /furnace

Discussion in 'Spigot Plugin Development' started by Deathworld, May 2, 2017.

Thread Status:
Not open for further replies.
  1. Hey, i'm actually coding an /furnace plugin, it's basically an automatically smelting command.
    I did that :
    Code (Text):
    package fr.dragonia;

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;

    public class Furnace implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] strings) {
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("furnace")) {
                    if ((sender instanceof Player)) {
                        ItemStack item = p.getItemInHand();
                        Material mat = item.getType();
                        if ((p.hasPermission("dragonia.duc"))) {
                            if (item.getType() == Material.AIR) {
                                p.sendMessage("§cVous n'avez aucun item en main !");
                            }
                            switch (mat) {
                                case COBBLESTONE:
                                    p.getInventory().remove(new ItemStack(mat, p.getItemInHand().getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.STONE, item.getAmount())});
                                    break;
                                case IRON_ORE:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.IRON_INGOT, item.getAmount())});
                                    break;
                                case GOLD_ORE:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.GOLD_INGOT, item.getAmount())});
                                    break;
                                case SAND:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.GLASS, item.getAmount())});
                                    break;
                                case PORK:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.GRILLED_PORK, item.getAmount())});
                                    break;
                                case RAW_BEEF:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_BEEF, item.getAmount())});
                                    break;
                                case RAW_FISH:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_FISH, item.getAmount())});
                                    break;
                                case RAW_CHICKEN:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_CHICKEN, item.getAmount())});
                                    break;
                                case LOG:
                                    //p.getInventory().remove(new ItemStack(Material.LOG, item.getAmount()));
                                    //p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COAL, item.getAmount())});
                                    p.sendMessage("§cProchainement... !");
                                    break;
                                case POTATO:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_CHICKEN, item.getAmount())});
                                    break;
                                case RABBIT:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_RABBIT, item.getAmount())});
                                    break;
                                case MUTTON:
                                    p.getInventory().remove(new ItemStack(mat, item.getAmount()));
                                    p.getInventory().addItem(new ItemStack[]{new ItemStack(Material.COOKED_MUTTON, item.getAmount())});
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
                else{
                    p.sendMessage("§cVous n'avez pas la permission !");
            }
            return false;
        }
    }
     

    But when I do /furnace, for exemple if I had 64 cobblestones in my hand and many others in my inventory, when I do /furnace it gives me 64 stones but it takes all my cobblestone who are in my inventory...
    Could you please help me ?

    Thanks & sorry for my bad english !
     
  2. It's because you are calling a new item stack when you should just use the item in hand item stack.

    p.getInventory().remove(item, item.getAmount);
     
  3. [​IMG]

    You need to check if sender is instance of player before casting him. Otherwise you'll cause a ClassCastException.
     
    • Informative Informative x 1
    • Useful Useful x 1
  4. Oh yeah, didn't notice that, do that also!
     
  5. Thanks for helping, but I have an error : "Cannot resolve method 'remove(org.bukkit.inventory.ItemStack, int)'
    I also changed the line where I cast the player :p

    Thanks !
     
  6. Looking at the Javadocs may help...
    Inventory#remove removes all of that ItemStack in the inventory. Also the method that other guy told you doesnt exist, that's why its throwing that error.
    Try ItemStack#setAmount and set it to 0? Untested but should work, however may also give you 0 of the itemstack, literally, giving you an unplaceable unusable 0 of the block haha, test it out though
     
    • Informative Informative x 1
  7. I
    It works, thanks ! :D
    But I have an problem with Logs, it doesn't work, it works with Oak Logs but not with others...
    How can I do it ?
    Thanks ! :)
     
  8. Explain further, that should work with all ItemStacks... ;p
     
    • Like Like x 1
  9. Damn, sorry, it's working '-'
    And sorry if you didn't understand, i'm bad in english ! XD

    So, I finished the plugin, thanks all ! :3
     
Thread Status:
Not open for further replies.