Checking for a certain 'keyword' in an Item.

Discussion in 'Plugin Development' started by Techno, Nov 2, 2014.

Thread Status:
Not open for further replies.
  1. Hey yall! I'm working on an item that when you type /soulbound holding an item / block.
    Example. I have a dirt block in my hand, I type /soulband it changes it to: Dirt [Soulbound] and I cannot drop it. :)

    The problem is that I don't know how to check for the [Soulbound] tag in the item name..

    Current code:
    Code:
    package me.soulbounditems.billforty;
     
    import net.md_5.bungee.api.ChatColor;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SoulboundItems extends JavaPlugin {
     
    @Override
    public void onEnable() {
    getLogger().info("[SoulboundItems] Plugin loaded.");
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    Player p = (Player) sender;
     
    if (p.hasPermission("soulbounditems.use.apply")) {
    if (cmd.getLabel().equalsIgnoreCase("soulbound")) {
     
    ItemStack item = new ItemStack(p.getItemInHand());
    ItemMeta itemm = item.getItemMeta();
    itemm.setDisplayName(item.getType().toString().replace("_","") + ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] ");
     
    p.getItemInHand().setItemMeta(itemm);
     
    return true;
    }
    }
     
    return false;
    }
     
    @EventHandler
       public void onItemDrop (PlayerDropItemEvent e) {
    Player dp = e.getPlayer();
     
    }
          
    }
    
     
  2. Offline

    leon3001

  3. Offline

    mine-care

    Player p = (Player) sender;
    Ouch! my eye! read my signature...
    dont use cmd.getabel() insted use cmd.getName() for alias to work (future use)
    And as sugested use the .contains() so do ChatColor.stripcolor(itemname).contains("[Soulbound]")
     
  4. Techno Also, seek support where you acquired your server mod.
     

  5. Like:
    Code:
    @EventHandler
       public void onItemDrop (PlayerDropItemEvent e) {
    Player dp = e.getPlayer();
     
    if (dp.getItemInHand().equals(ChatColor.stripColor(dp.getItemInHand().getItemMeta().getDisplayName().contains("[Soulbound]")))) {
     
    }
     
    }
    
    ?????
     
  6. Offline

    guitargun

    Techno.
    if I am correct the dp.getItemInHand() will return null or with a lesser amount than before the drop.
    better thing to do is get the item without parsing to the player first.
    then check if the display name has you soulbound thing.
     

  7. But, how would you even do that ? Haha!

    I changed stuff a bit:
    Code:
     @EventHandler
       public void onItemDrop (PlayerDropItemEvent e) {
    Player dp = e.getPlayer();
     
    if (dp.getItemInHand().equals(ChatColor.stripColor(dp.getItemInHand().getItemMeta().getDisplayName()).contains(ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] "))) {
    e.setCancelled(true);
    dp.sendMessage(ChatColor.RED + "This is too cool?");
    } 
    }
    
     
  8. Offline

    guitargun

    small example. I am not giving you the full answer since you need to do it yourself.
    I think that this is even to much to give.
    Code:java
    1. public void yourevent(PlayerDropItemEvent e){
    2. //THIS is what you need. also note that Item is an entity not a item in your inventory.
    3. Item it = e.getItemDrop();
    4. //there is something magical withing this item to convert it to itemstack
    5. ItemStack youritemstack = it.<insertmagicalthingyhere>;
    6.  
    7. //now we want to know if our item has the right name
    8. //btw we first want t know it has any ItemMeta (to prevent errors later down the road).
    9. if(youritemstack.hasItemMeta()){
    10. // we want our itemmeta infomartion
    11. ItemMeta im = youritemstack.<yupmetapat>;
    12. //with this ItemMeta check if the display name contains your soulbound tagg.
    13. //not sure if you need to check for coloring (perhaps to prevent any anvils?)
    14. }
    15. }


    perhaps better to check with .contains first before the colour.
     
  9. guitargun
    I tried this:
    Code:
    @EventHandler
       public void onItemDrop (PlayerDropItemEvent e) {
    Player dp = e.getPlayer();
    Item it = e.getItemDrop();
    ItemStack stack = it.getItemStack();
     
    if (stack.hasItemMeta()) {
    ItemMeta im = stack.getItemMeta();
    if (im.equals(ChatColor.stripColor(dp.getItemInHand().getItemMeta().getDisplayName()).contains(ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] "))) {
    e.setCancelled(true);
    dp.sendMessage(ChatColor.RED + "This is too cool?");
    }
    } 
    }
    
    but, what do you mean by 'perhaps better to check .contains first before the colour' ?
     
  10. Offline

    guitargun


    I mean with it to check the name first then the color. also the dp.getiteminhand. shouldn't that give a nullpointer if your soul-bound item is a sword or something non-stacking.

    btw. at
    Code:java
    1. if (im.equals(ChatColor.stripColor(dp.getItemInHand().getItemMeta().getDisplayName()).contains(ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] "))) {
    2.  


    why are you checking if the item you dropped has the same name as the item (or nothing) you have in your hand. If you have a constant name for it you can better snip some code to
    Code:java
    1. if (im.equals(ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] ")) {
    2.  
     

  11. Ah okay, well.. I tried this and it still doesn't work..
    Code:
    package me.soulbounditems.billforty;
     
    import net.md_5.bungee.api.ChatColor;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SoulboundItems extends JavaPlugin {
     
    @Override
    public void onEnable() {
    getLogger().info("[SoulboundItems] Plugin loaded.");
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    Player p = (Player) sender;
     
    if (p.hasPermission("soulbounditems.use.apply")) {
    if (cmd.getName().equalsIgnoreCase("soulbound")) {
     
    ItemStack item = new ItemStack(p.getItemInHand());
    ItemMeta itemm = item.getItemMeta();
    itemm.setDisplayName(item.getType().toString().replace("_","") + ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] ");
     
    p.getItemInHand().setItemMeta(itemm);
     
    return true;
    }
    }
     
    return false;
    }
     
    @EventHandler
       public void onItemDrop (PlayerDropItemEvent e) {
    Player dp = e.getPlayer();
    Item it = e.getItemDrop();
    ItemStack stack = it.getItemStack();
     
    if (stack.hasItemMeta()) {
    ItemMeta im = stack.getItemMeta();
    if (im.equals(im.getDisplayName())) {
    if (ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] " != null) {
    e.setCancelled(true);
    dp.sendMessage(ChatColor.RED + "This is too cool?");
    }
    }
    } 
    }
          
    }
    
     
  12. Offline

    ChipDev

    Easily..
    Code:java
    1. if(e.getItem().getItemMeta().getDisplayName().contains(ChatColor.DARK_BLUE + "[SoulBound]")) {
    2. // If item display name contains blue [SoulBound]
    3. }

    Have not read the post. blame me if this is already said xD
     
  13. Offline

    guitargun

    Techno Why are you using a api for the colors? you can just use
    Code:java
    1. import org.bukkit.ChatColor;



    secondly:
    Code:java
    1. if (im.equals(im.getDisplayName())) {
    will always return false since itemMeta isn't the same as the display name.

    third
    Code:java
    1. if (ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] " != null) {
    is wrong because you are using a constant now and not the item that is dropped.

    to fix this all remove
    Code:java
    1. if (im.equals(im.getDisplayName())) {
    2. if (ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] " != null) {
    3.  

    and replace it with
    Code:java
    1. if (ChatColor.stripColor(im.getDisplayName()).contains("[Soulbound]")) {


    btw. you should look if your plugin uses the listeners I have some more things in it in my testplugin project.
     

  14. Thanks, I fixed everything :D
    But when you do /soulbound with anything it would be like DIRT [Soulbound] or DIAMONDAXE [Soulbound]
     
  15. Offline

    guitargun

    Techno thats because you use the .getType.toString. use .getType.name. that gives a better name.
     
Thread Status:
Not open for further replies.

Share This Page