Sending Killers Sword ToolTip

Discussion in 'Plugin Development' started by JacknDaBox42, Jun 7, 2014.

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

    JacknDaBox42

    So I have been working with JSON messages lately and wanted to how a player that was killed see what the other players sword was. I have gotten to the point where I need to get the killers swords tooltip so I can set that as the JSON message in chat. Anyone know how to get the tooltip for tools in a players inventory? This is what I mean for those who don't understand.
    [​IMG]

    So here is my code so far so if anyone can help, that would be appreciated!

    PHP:
    @EventHandler
        
    public void onDeath(PlayerDeathEvent e) {
            
    Player p = (Player)e.getEntity();
            
    Player killer = (Player)p.getKiller();
           
            if(
    p.getKiller() instanceof Player) {
                if(
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.DIAMOND_SWORD) ||
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.DIAMOND_AXE) ||
                       
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.IRON_SWORD) ||
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.IRON_AXE) ||
                       
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.STONE_SWORD) ||
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.STONE_AXE) ||
                       
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.WOOD_SWORD) ||
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.WOOD_AXE) ||
                       
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.GOLD_SWORD) ||
                        
    p.getKiller().getInventory().getItemInHand().getType().equals(Material.GOLD_AXE)) {
                       
                
    String string killer.getItemInHand().getType().toString().toLowerCase().replaceAll("_"" ");
                
    net.minecraft.server.v1_7_R1.ItemStack nms CraftItemStack.asNMSCopy(new ItemStack(p.getKiller().getItemInHand().getType()));
                
    String name nms.getName();   
               
                for(
    Player pp Bukkit.getOnlinePlayers()) {
                
    JSONChatMessage message = new JSONChatMessage(p.getName() + " was slain by " killer.getName() + " using a "JSONChatColor.WHITEnull);
                
    JSONChatExtra extra = new JSONChatExtra(" [" Utils.capitalize(string) + "]"JSONChatColor.AQUA);
                
    extra.setHoverEvent(JSONChatHoverEventType.SHOW_TEXTname);
                
    message.addExtra(extra);
                
    message.sendToPlayer(pp);
                
    e.setDeathMessage(null);
                }
            } else {
                return;
                }
            }
        }
     
  2. Offline

    Mrawesomecookie

    JacknDaBox42
    Mostly, it comes in client side. Its not really there.
     
  3. Offline

    JacknDaBox42

    Mrawesomecookie

    I've seen a couple other servers do it, is that because they did it themselves? Basically copying what the tooltip should be? I tried doing that but I guess you can't use \n to make a new line. Also I would have to get the enchantments and list them.
     
  4. Offline

    fireblast709

    JacknDaBox42
    • You can use the ItemMeta to get the display name.
    • Also in this case JSON chat is a bit overkill, since you only use ChatColors. You should just set the death message in the event.
    • Check if(killer != null) instead of the instanceof, though it seems to have the same effect, the comparison is a bit more formal
    • Instead of checking every single type manually, what about an EnumSet:
      Code:java
      1. Set<Material> types = EnumSet.of(Material.DIAMOND_AXE, ...etc);
      2. ...
      3. if(player.getItemInHand() != null && types.contains(player.getItemInHand().getType()))
      4.  
    • Also, as shown above, make sure that getItemInHand() doesn't return null.
    • event.getEntity(), despite the horrible name, always returns a Player object in PlayerDeathEvent, no need to cast. Same goes for getKiller().
    • Javadocs: ItemStack and ItemMeta should contain enough information for you to filter the name. Sidenote: the display name will be null if not set, be aware.
     
  5. Offline

    Mrawesomecookie

    JacknDaBox42
    Thats why you use setLore and setDisplayName
     
Thread Status:
Not open for further replies.

Share This Page