Why Isn't This Enchanting?

Discussion in 'Plugin Development' started by Shad, Jun 23, 2012.

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

    Shad

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
            Player player = (Player) sender;
            if(commandlabel.equalsIgnoreCase("win")){
                int itemCode = 276;
                int effectId = 16;
                int enchantmentLevel = 100;
               
                ItemStack myItem = new ItemStack(itemCode);
                Enchantment myEnchantment = new EnchantmentWrapper(effectId);
                myItem.addUnsafeEnchantment(myEnchantment, enchantmentLevel);

    Why isn't this working? How can I get 276 enchanted with 16 at level 100?
     
  2. Offline

    r0306

    Shad
    Make sure that its not your command executor that's not working.
     
  3. Offline

    Shad

    It outputs a blank space and when I attached a message to it, the message sent. I am confuzzled. There is something wrong with what I'm trying to do with my enchant, and since I am uber noob with Java (just started), I cannot figure out what I'm doing wrong. If it helps someone, I'm trying to get it to spawn a diamond sword with level 100 sharpness when you type /win.
     
  4. Offline

    Everdras

    You appear to not ever be adding the item to your inventory.
     
  5. Offline

    Shad

    Again, uber noob, how do I go about putting the enchanted weapon in my inventory?
     
  6. Offline

    r0306

    Code:
    player.getInventory().addItem(myItem);
     
  7. Offline

    Everdras

    player.getPlayerInventory().add(itemStack);

    Or something like that, I don't remember the exact calls off the top of my head. You'll be calling some accessor method of Player that will return a PlayerInventory object. Then you'll calls some method of PlayerInventory to add the ItemStack object to it.
     
  8. Offline

    Shad

    Thank you guys! This actually helped expand my knowledge a lot, I was unsure about all these player.getInventory stuff and I think I know how to properly use them to an extent.
     
  9. Offline

    Everdras

    When I'm stuck as to "how do I do X" I usually just pull up the JavaDoc for any classes that I have objects of in the scope I'm working in, then look at all the methods and shit that they can do.

    I was in your place once; I believe what I did then was this:

    "Hm, I have a Player object and a Server object, as well as static Bukkit and such methods at my disposal. Let's see if any of their methods have to do with inventories..."

    Google search: "Bukkit javadoc"

    Javadoc search: "Player"

    "Hmm... looks like there's a "getInventory() method that returns a PlayerInventory... let's look up PlayerInventory"

    "Hm, PlayerInventory has an addItem(ItemStack) method, that says it adds an item to the player's inventory. Oh, there we go."

    *continues hacking away at plugin*

    tldr: I for one welcome our new Google overlords.
     
    ferrybig likes this.
Thread Status:
Not open for further replies.

Share This Page