Solved USE CUSTOMITEM, IN GETMATERIAL

Discussion in 'Plugin Development' started by Texgabs, Apr 22, 2016.

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

    Texgabs

    Hi guys, I 'm doing a plug broomsticks (like harry potter ), the plugin is almost ready but I have a problem with the item that will be used to fly.
    In my plugin
    uses different items that work to fly for example in the following line:

    Code:
        if (p.getItemInHand().getType() == Material.getMaterial(this.config.getInt("nimbus2000.itemid"))) 
    considering that the item in this broom is the number disc 9:

    Code:
        if (getConfig().get("nimbus2000.itemid") == null)
        {
          getConfig().set("nimbus2000.itemid", Integer.valueOf(Material.RECORD_9.getId()));
          saveConfig();
    but the problem is that the player can fly with any item ( record_9 ) regardless of the name that has.
    and I need only you can fly if the item has the name :
    for example : & aNimbus2000

    and also tried to use an API CustomItem like this:
    Code:
        package me.texgabs.mdescobas;
        
        import java.util.Arrays;
        import java.util.List;
        
        import org.bukkit.Material;
        import org.bukkit.entity.Player;
        import org.bukkit.inventory.Inventory;
        import org.bukkit.inventory.ItemStack;
        import org.bukkit.inventory.meta.ItemMeta;
        
        
        public class CustomItem extends ItemStack {
        
        public CustomItem(Material material){
        this.setType(material);
        this.setAmount(1);
        }
        
        
        public CustomItem(Material material, Integer count){
        this.setType(material);
        this.setAmount(count);
        }
        
        public CustomItem(Material material, String displayName){
        this.setType(material);
        ItemMeta im = this.getItemMeta();
        im.setDisplayName(displayName);
        this.setItemMeta(im);
        this.setAmount(1);
        }
        
        public CustomItem(Material material, List<String> lores){
        this.setType(material);
        ItemMeta im = this.getItemMeta();
        im.setLore(lores);
        this.setItemMeta(im);
        this.setAmount(1);
        }
        
        public CustomItem(Material material, Integer count, String displayName){
        this.setType(material);
        this.setAmount(count);
        ItemMeta im = this.getItemMeta();
        im.setDisplayName(displayName);
        this.setItemMeta(im);
        
        }
        
        public CustomItem(Material material, Integer count, List<String> lores){
        this.setType(material);
        this.setAmount(count);
        ItemMeta im = this.getItemMeta();
        im.setLore(lores);
        this.setItemMeta(im);
        }
        
        public CustomItem(Material material, String displayName, List<String> lores){
        this.setType(material);
        ItemMeta im = this.getItemMeta();
        im.setDisplayName(displayName);
        im.setLore(lores);
        this.setItemMeta(im);
        this.setAmount(1);
        }
        
        public CustomItem(Material material, Integer count, String displayName, List<String> lores){
        this.setType(material);
        this.setAmount(count);
        ItemMeta im = this.getItemMeta();
        im.setDisplayName(displayName);
        im.setLore(lores);
        this.setItemMeta(im);
        }
        
        public void add(Player player){
        player.getInventory().addItem(this);
        }
        
        public void add(Inventory inventory){
        inventory.addItem(this);
        }
        
        public void set(Player player, Integer slot){
        player.getInventory().setItem(slot, this);
        }
        
        public void set(Inventory inventory, Integer slot){
        inventory.setItem(slot, this);
        }
        
        }
    
    
    But nothing :/
    Could you help me?
    Thanks and sorry for my bad english :S
     
  2. Offline

    elian1203

    You're going to want to check for the ItemMeta on the player's item in hand. Like this: p.getItemInHand().getItemMeta().getDisplayName(). You can do a lot of stuff with ItemMeta, so you should look into that.
     
  3. Offline

    Texgabs

    well, then I'll use it in this way :
    Code:
        if (p.getItemInHand().getItemMeta().getDisplayName().contains("Nimbus2000")) {
    But for check the color, i need put in the name §aNimbus2000?
     
  4. Offline

    Zombie_Striker

    So what your testing is if the type is the same as a typeID. Either test if the types are the same (recommended, as you should no longer use Item IDs),or test if the ids are the same.

    BTW: Format your code. It really makes a difference when you are debugging/looking for errors.
     
  5. Offline

    Texgabs

    Thanks, i tried with the next line:
    Code:
        if (p.getItemInHand().getItemMeta().getDisplayName().contains("Nimbus2000")) {
    And now only you can fly when the item is called "Nimbus2000" but when you use right-click in any item into the game , this throw errors like this:

    HTML:
    [14:37:20 ERROR]: Could not pass event PlayerInteractEvent to MDescobas v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:717) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_74]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_74]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_74]
    Caused by: java.lang.NullPointerException
            at me.texgabs.mdescobas.brooms.onFirebolt(brooms.java:298) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_74]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_74]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_74]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]

    why? , I am doing something wrong? :(
     
  6. Offline

    mcdorli

    Check first if the item has a displayname, if it doesn't have a custom one, it is going to be null
     
  7. Offline

    Texgabs

    Excellent! Thanks for all guys, i use that like:
    Code:
        if(p.getItemInHand() != null){
           
            if(p.getItemInHand().hasItemMeta()){
                if(p.getItemInHand().getItemMeta().hasDisplayName()){
        if (p.getItemInHand().getItemMeta().getDisplayName().contains("Nimbus2000")) {
    And works excellent thanks :D :D
     
Thread Status:
Not open for further replies.

Share This Page