Command to convert cactus to cactus green

Discussion in 'Plugin Development' started by Baconer, Dec 9, 2014.

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

    Baconer

    Hi, I've been trying for a few days to do this but I haven't figured it out yet. If someone could tell me how to do it that would be awesome :)
     
  2. Offline

    Unica

    @Baconer

    - What do you mean with 'convert' ?
    - What have you tried?
     
  3. Offline

    Skionz

    @Baconer Remove the cactus. Add the cactus green.
     
    TheOatBaron likes this.
  4. Offline

    TheOatBaron

    More in-depth would be:

    >On Command
    Check if inventory contains cactus.
    Inside for loop going through the inventory:
    - Check if cactus
    - Get the slot of cactus.
    - Set Cactus Green at slot of cactus.

    Untested, probably can be condensed:

    Code:
    if (label.equalsIgnoreCase("scan")){
                if ((sender instanceof Player)){
                    Player p = (Player) sender;
                    Inventory inv = p.getInventory();
                    if(inv.contains(Material.CACTUS)){
                        ItemStack cg = new ItemStack(Material.INK_SACK);
                        cg.setDurability((short) 2);
                        for(int i=0;i<inv.getSize();i++){
                            if(inv.getItem(i).getType().equals(Material.CACTUS)){
                                cg.setAmount(inv.getItem(i).getAmount());
                                inv.setItem(i, cg);
                            }
                        }
                    }
                }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  5. @TheOatBaron Please don't spoonfeed, and you seem to be making assumptions about the OP's use case here :)
     
  6. Offline

    MooshViolet

    I am going to use
    @TheOatBaron 's code and explain it with annotations.
    Code:
    if (label.equalsIgnoreCase("scan")){
    //See if a player runs the command /scan
                if ((sender instanceof Player)){
    //check to see if the actual thing running the command is a player not a console
                    Player p = (Player) sender;
    //define the player sending the command as p
                    Inventory inv = p.getInventory();
    // define the player's inventory
                    if(inv.contains(Material.CACTUS)){
    //check if his inventory has cactus in it
                        ItemStack cg = new
    ItemStack(Material.INK_SACK);
    //declare an item as an ink sac
                        cg.setDurability((short) 2);
    // setting the durrability so it is cactus green
                        for(int i=0;i<inv.getSize();i++){
    //simple for loop with integers saying that if the i is less than the inventory size, add.
                            if(inv.getItem(i).getType().equals(Material.CACTUS)){
    //checking to see if the player has cactus
                                cg.setAmount(inv.getItem(i).getAmount());
                                inv.setItem(i, cg);
    //replacing the item
                            }
                        }
                    }
                }
            }
     
  7. Offline

    TheOatBaron

    Seeing code in action is a lot easier to comprehend then saying what to do.

    @MooshViolet Thanks! :)
     
Thread Status:
Not open for further replies.

Share This Page