Multiple classes and spawning items...

Discussion in 'Plugin Development' started by maxxb123, Mar 18, 2014.

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

    maxxb123

    Hello, I've currently got two questions for all of you.


    1) How do I do commands in multiple classes?

    I know I can do something like the thing bellow in the onEnable:

    Code:java
    1. this.getCommand("YOUR_COMMAND").setExecutor(new [CLASS_NAME_WHICH_THE_COMMAND_IS_IN]());


    But, how does the code work in the class I've coded the commands in? I've heard you need to do something with CommandExecutor?

    2) How do I change the item name when I give the player a item?

    What if I used the following code to spawn an apple:


    Code:java
    1. pi.addItem(new ItemStack(Material.APPLE, 15));


    But, how would I make the item name of the apple be "Happy Apple"? Can I change the color of the text as well?
     
  2. Offline

    Amgis

    maxxb123

    I created my own command parser so I don't know the answer to the first question.

    2.
    Code:JAVA
    1. event.getPlayer().getInventory().addItem(getApple("Happy Apple", ChatColor.GREEN));
    2.  
    3. public ItemStack getApple(String name, ChatColor chatColor) {
    4.  
    5. ItemStack apple = new ItemStack(Material.APPLE);
    6. ItemMeta itemMeta = apple.getItemMeta();
    7. itemMeta.setDisplayName(chatColor + name);
    8. apple.setItemMeta(itemMeta);
    9.  
    10. return apple;
    11. }
     
  3. Offline

    maxxb123

    Thanks, but, I still could use a little bit more information on the first question, I'll try everything you two gave me for the Happy Apple thingy.
     
  4. Offline

    Barinade

    Add this method to your classes that need to manage commands
    http://wiki.bukkit.org/Plugin_Tutorial#Setting_up_the_command

    For #2, looks like it's already solved, but I'm inputting anyways.

    ItemStack dirt = new ItemStack(Material.DIRT);
    ItemMeta meta = dirt.getItemMeta();
    meta.setDisplayName("Sand"):
    dirt.setItemMeta(meta);
    pi.addItem(dirt);
     
  5. Offline

    maxxb123

    Okay, how would I put #2 in a command? Like what would the code be for a command? I know to give an apple I'd do:

    Code:java
    1. PlayerInventory pi = p.getInventory();
    2.  
    3. if (cmd.getName().equalsIgnoreCase("pvp")) {
    4. pi.addItem(new ItemStack(Material.APPLE, 15));



    But, how would I make the item name Happy Apple using that code that you all gave me above?

    Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Niknea

    maxxb123 assuming you got the second question, here is the answer for the first question, yes you need to add something involving CommandExecutor, type "implements CommandExecutor" in the command class where you would normally type "extends JavaPlugin". If you still don't get it, check out this guide, it explains everything perfectly. https://forums.bukkit.org/threads/tutorial-using-multiple-classes.179833/

    PS: Don't forget to add your command in the main class and in your plugin.yml ;)
     
  7. Offline

    GameplayJDK

    maxxb123
    You can do something like
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("pvp")) {
    3. ItemStack happy = new ItemStack(Material.APPLE, 15);
    4. happy.getItemMeta().setDisplayName(ChatColor.RED + "Happy Apple");
    5. p.getInventory().addItem(happy);
    6. p.updateInventory();
    7.  
     
Thread Status:
Not open for further replies.

Share This Page