Solved Get a placed block displayname or lore?

Discussion in 'Plugin Development' started by _Ass4ssin_, Dec 24, 2012.

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

    _Ass4ssin_

    Hello,

    I need some help with a small problem. I have figured out with the help of google :rolleyes: how I can get a item/block with a custom name. Now for my plugin I want to run a code when a player place the block with the custom name. I gave the display name also a chat color so players couldn't rename random items to get the special plugin item. But, now I get a block with a custom name and chat color. but how could I make it when you place check if it got the same displayname with chatcolor?
     
  2. Offline

    Jogy34

    I don't quite understand what you mean but it sounds like you are trying to compare a string with a ChatColor in it. For this all you do is add the chat color to the string you are comparing it to which would be something like this:
    Code:
    if(string1.equals(ChatColor.RED + "Something"))
    {
        //do stuff
    }
     
  3. Offline

    _Ass4ssin_

    Well, I am sometimes a bit hard to understand as English isn't my main language, so my excuse for that.

    Anyway I got a command that gives you a diamond sword with a custom colored name.
    I use this code for it:
    Code:
    ItemStack is = new ItemStack(Material.DIAMOND_BLOCK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Diamond Block");
    is.setItemMeta(im);
    player.getInventory().addItem(is);
    Now I want to check when someone place a block if it is a diamondblock and if it is a diamond it should check if it is the diamond block where the display name is (ChatColor.DARK_BLUE + "Diamond Block"), So not random people just craft a diamond block and place it down...
    But I can't figure out how to check if it is a diamond block with a special name or just a regular diamond block!

    Code:
    public void onBlockPlace(BlockPlaceEvent event) {
            if(event.getBlock().getType() == Material.DIAMOND_BLOCK){
                Player player = event.getPlayer();
                if(event.getBlock().hasMetadata(ChatColor.DARK_BLUE + "Diamond Block")){
                    player.sendMessage(ChatColor.AQUA + "You have just placed a special diamond block!");
                                    // some other code here
                }
    
    But everytime I try placing the special diamond block given with the command it just don't do anything...
    There are no errors in the console cause it just think it doesn't got this metadata.
    So how do I do it?
     
  4. Offline

    Jogy34

    try this:
    Code:
    ItemMeta im = player.getItemInHand();
    if(im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block"))
    {
        //Do Stuff
    }
     
  5. Offline

    _Ass4ssin_

    Well, I still don't get a message returning I have placed a special diamond block?

    Here is all of my code
    Code:
    package me.Rang0.specialdiamondblock;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class specialdiamondblock extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static specialdiamondblock plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("specialdiamondblock")){
                ItemStack is = new ItemStack(Material.DIAMOND_BLOCK, 1);
                ItemMeta im = is.getItemMeta();
                im.setDisplayName(ChatColor.DARK_BLUE + "Diamond Block");
                is.setItemMeta(im);
                player.getInventory().addItem(is);
                player.sendMessage(ChatColor.AQUA + "You have gain a special diamond block!");
            }
            return false;
        }
       
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            if(event.getBlock().getType() == Material.SPONGE){
                ItemMeta im = (ItemMeta) player.getItemInHand();
                if(im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block"))
                {
                    player.sendMessage(ChatColor.AQUA + "You have placed down a special diamond block!");
                    // other code here
                }
            }
        }
    }
    
    Thanks already for helping btw, I appreciate the quick responds!
     
  6. Offline

    Jogy34

    You aren't registering your event listeners in your onEnable()

    Add this in your onEnable() method
    Code:
    this.getServer().getPluginManager().registerEvents(this, this);
    And your main class also needs to implement Listener
     
  7. Offline

    _Ass4ssin_

    Oh, that is my bad, even though I add it, I still don't get any message nor error ?

    My new code at the moment is as followed:

    Code:
    package me.Rang0.specialdiamondblock;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class specialdiamondblock extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static specialdiamondblock plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("specialdiamondblock")){
                ItemStack is = new ItemStack(Material.DIAMOND_BLOCK, 1);
                ItemMeta im = is.getItemMeta();
                im.setDisplayName(ChatColor.DARK_BLUE + "Diamond Block");
                is.setItemMeta(im);
                player.getInventory().addItem(is);
                player.sendMessage(ChatColor.AQUA + "You have gain a special diamond block!");
            }
            return false;
        }
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            if(event.getBlock().getType() == Material.SPONGE){
                ItemMeta im = (ItemMeta) player.getItemInHand();
                if(im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block"))
                {
                    player.sendMessage(ChatColor.AQUA + "You have placed down a special diamond block!");
                    // other code here
                }
            }
        }
    }
    
     
  8. Offline

    Jogy34

    The problem is probably that this: "if(event.getBlock().getType() == Material.SPONGE){" should probably be this: "if(event.getBlock().getType() == Material.DIAMOND_BLOCK){" unless of course the block you gave the player was a sponge.
     
  9. Offline

    fireblast709

    You are casting ItemStack to ItemMeta. Use
    Code:
    player.getItemInHand().getItemMeta();
    to get the ItemMeta from the players hand
     
  10. Offline

    Jogy34

    Whoops. I thought I had the .getItemMeta() on the end in my post.
     
  11. Offline

    _Ass4ssin_

    Whoops, well yea first it was if you had a special sponge but I changed it to a diamond block, so I should fix that, let me also try the code fireblast gave and hopefully it will work!

    Now it works but I get a weird error when placing a regular block?
    I tried doing a lot of things like else player.sendMessage(blah blah) but still an error and I tried if(im != null){ player.sendMessage(blah blah)} still errors?

    My nearly finished code:

    Code:
    package me.Rang0.specialdiamondblock;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class specialdiamondblock extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static specialdiamondblock plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("specialdiamondblock")){
                ItemStack is = new ItemStack(Material.DIAMOND_BLOCK, 1);
                ItemMeta im = is.getItemMeta();
                im.setDisplayName(ChatColor.DARK_BLUE + "Diamond Block");
                is.setItemMeta(im);
                player.getInventory().addItem(is);
                player.sendMessage(ChatColor.AQUA + "You have gain a special diamond block!");
            }
            return false;
        }
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            if(event.getBlock().getType() == Material.DIAMOND_BLOCK){
                ItemMeta im = player.getItemInHand().getItemMeta();
                if(im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block"))
                {
                    player.sendMessage(ChatColor.AQUA + "You have placed down a special diamond block!");
                    // other code here
                }
                else {
                    player.sendMessage(ChatColor.RED + "This is a regular diamond block and doesn't do anything");
                }
            }
        }
    }
    
    Might be handy if I would also give the error so here you go:

    Code:
    11:21:01 [SEVERE] Could not pass event BlockPlaceEvent to SDB v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_4_6.event.CraftEventFactory.callBlockPlaceE
    vent(CraftEventFactory.java:100)
            at net.minecraft.server.v1_4_6.ItemBlock.interactWith(ItemBlock.java:78)
     
            at net.minecraft.server.v1_4_6.ItemStack.placeItem(ItemStack.java:72)
            at net.minecraft.server.v1_4_6.PlayerInteractManager.interact(PlayerInte
    ractManager.java:392)
            at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:
    657)
            at net.minecraft.server.v1_4_6.Packet15Place.handle(SourceFile:58)
            at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
     
            at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:
    112)
            at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30
    )
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:59
    8)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.NullPointerException
            at me.Rang0.specialdiamondblock.specialdiamondblock.onBlockPlace(special
    diamondblock.java:53)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 18 more
     
  12. Offline

    ZeusAllMighty11

    Code:
                if(im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block"))
    
    That is the error line I'm guessing (53)
    Try seeing how this works:
    Code:
                if(!im.getDisplayName().equals(ChatColor.DARK_BLUE + "Diamond Block")){
                player.getServer().broadcastMessage(ChatColor.RED + " isn't a diamond block " );
                } else {
                player.getServer().broadcastMessage(ChatColor.GREEN+ " ITS A DIAMOND BLOCK" );
                }
     
  13. Offline

    _Ass4ssin_

    Same, it works but if someone places the regular diamond it gives a error. But it is for private use only so I guess it doesn't really matter as long as it works, maybe not the smartest thing to do but well, I might change it to sponge as players can't get regular sponges :3! Also is there a way to keep the blocks data when it has been placed? So if someone removes the diamond block (or sponge) it will become a regular one without the display name? Is this fixable with some lines of code?

    Thanks for helping and I think I just change it to sponge and then there won't be errors (as long as I get the display name to stay when place or cancel drop)
     
  14. Offline

    fireblast709

    You could try (as diamond blocks don't use datavalues) set its datavalue to 1. And if onBlockBreak it has datavalue 1, drop a named block
     
  15. Offline

    _Ass4ssin_

    Going to try that as soon as possible, at the moment a bit busy doing other stuff and don't have enough time to actually try it out and all. So if I run into problems I will ask for help again. For now I keep this thread without solved if you guys see it is solved it worked!

    Thanks a lot guys! I always get fast and good help if I got problems!

    Could you explain how I can set the datavalues? As I can only find the .setMetaData or is it the same/could I use it for the same effect? cause I can do onBlockBreak event
    if(event.getBlock().hasMetaData("1"){
    //code
    }

    I also tried with .setData((byte)1) and later tried:
    if (event.getBlock().getData == (byte)1){
    //code
    }
    But it doesnt seem to work either?
    Any idea?

    Thanks in advance
     
  16. Offline

    FozZeW

    I have same problem, i want to get rid of that stupid error :/
     
  17. Offline

    jazaazma

    I know this is a solved post, but if you are still having a problem with the error i fixed it by:
    public void blockPlaceEvent(BlockPlaceEvent event){
    try {
    // code
    } catch (NullPointerException ex){
    }
     
  18. Offline

    Hugs

    if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().toString().toLowerCase().contains("STRING TO TEST")){

    Works just fine for me, provided you're checking for

    if (e.getItemInHand().hasItemMeta()){
     
Thread Status:
Not open for further replies.

Share This Page