Util [ProtocolLib] Hide lore from client(s)

Discussion in 'Resources' started by Plo124, May 29, 2014.

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

    Plo124

    So have you wished you could store per-item data? Using this snippet of code, you can.

    Basically any lore that starts with 'Hidden:' (and its easily customisable) will not be sent to the client, providing this code is active, will still be in memory with bukkit.

    So, lets discuss how to use this code:
    Simply put it in your onEnable.

    Download link for code:
    http://hastebin.com/uqebupokuh.avrasm

    Is that website blocked for you (idk why though), here is the code:
    Code (open)
    Code:java
    1. ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this /*your plugin instance*/,
    2. ListenerPriority.NORMAL, PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.WINDOW_ITEMS) {
    3. @Override
    4. public void onPacketSending(PacketEvent event) {
    5. if (event.getPacketType() == PacketType.Play.Server.SET_SLOT) {
    6. PacketContainer packet = event.getPacket().deepClone();
    7. StructureModifier<ItemStack> sm = packet
    8. .getItemModifier();
    9. for (int j = 0; j < sm.size(); j++) {
    10. if (sm.getValues().get(j) != null) {
    11. ItemStack item = sm.getValues().get(j);
    12. ItemMeta itemMeta = item.getItemMeta();
    13. if (itemMeta.hasLore()) {
    14. List<String> lore = itemMeta.getLore();
    15. for (String s : lore) {
    16. if (s.startsWith("Hidden:")) {
    17. lore.remove(s);
    18. }
    19. }
    20. itemMeta.setLore(lore);
    21. item.setItemMeta(itemMeta);
    22. }
    23. }
    24. }
    25. event.setPacket(packet);
    26. }
    27. if (event.getPacketType() == PacketType.Play.Server.WINDOW_ITEMS) {
    28. PacketContainer packet = event.getPacket().deepClone();
    29. StructureModifier<ItemStack[]> sm = packet
    30. .getItemArrayModifier();
    31. for (int j = 0; j < sm.size(); j++) {
    32. for (int i = 0; i < sm.getValues().size(); i++) {
    33. if (sm.getValues().get(j)[I] != null) {[/I]
    34. [I] ItemStack item = sm.getValues().get(j)[I];[/I][/I]
    35. [I] ItemMeta itemMeta = item.getItemMeta();[/I]
    36. [I] if (itemMeta.hasLore()) {[/I]
    37. [I] List<String> lore = itemMeta.getLore();[/I]
    38. [I] for (String s : lore) {[/I]
    39. [I] if (s.startsWith("Hidden:")) {[/I]
    40. [I] lore.remove(s);[/I]
    41. [I] }[/I]
    42. [I] }[/I]
    43. [I] itemMeta.setLore(lore);[/I]
    44. [I] item.setItemMeta(itemMeta);[/I]
    45. [I] }[/I]
    46. [I] }[/I]
    47. [I] }[/I]
    48. [I] }[/I]
    49. [I] event.setPacket(packet);[/I]
    50. [I] }[/I]
    51. [I] }[/I]
    52. [I] });[/I]


    Thanks for reading this, and enjoy *bows down*
     
    physanus likes this.
  2. Offline

    ccrama

    Pretty good idea. To make sure I'm getting this straight, this makes it so the client can't see an item's lore, letting you store data as lines of lore? If so, that's awesome!
     
    Plo124 likes this.
  3. Offline

    Comphenix

    The only downside with this approach is that it doesn't quite work with creative players. This is because the client is able to directly set the item stack on the server side, so when it gets a stack with a missing lore line it will remove it on the server as well.

    A simple workaround is to disable this effect for creative players - you just have to be careful, and reset the inventory when players switch from adventure to creative mode. Alternatively, you could do as Portable-Horses did, and use (or shade in) ItemRenamer to handle creative mode, which uses a relatively complex workaround for creative players.

    Another possibility is AttributeStorage.
     
    synquall, L33m4n123 and Plo124 like this.
  4. Offline

    Plo124

    Yay, its Comphenix my favourite plugin dev with packets!
    This day just keeps getting better and better.

    Thanks for pointing this out,
    Im sure that anyone who is writing a plugin with this example could then follow your idea and make the inventories seperate, but my code is provided as-is, it works for players in survival.
     
  5. Offline

    Comphenix

    No problem. :D

    Sure, I just wanted to point out that it won't quite work for creative players. You could add that as a disclaimer in the original post.
     
  6. Offline

    Plo124

    Comphenix
    Are you saying that with a client sided mod, and creative, a player can spawn any item he/she wants with or without enchanting, and can set the lore and stuff?
     
  7. Offline

    Comphenix

    Basically, yeah. The creative client sends an entire ItemStack to the server, replacing the existing stack.

    There's barely any restrictions put in place here, the only exception is custom NBT tags - they're automatically removed. But anything else will be accepted.
     
  8. Offline

    synquall

    Comphenix

    I can't seem to find any references to ItemRenamer in PortableHorses' code. Mind helping me out?
     
  9. Offline

    Comphenix

    Ah, looks like that has been removed.

    But it's still the same approach as ItemRenamer, and it still depends on ProtocolLib. Take a look at this class for instance (that gets activated for 1.7.8).

    There's also an API for doing this very thing (hiding lore by prefix) in ItemRenamer.
     
    synquall likes this.
  10. Offline

    ChipDev

    Can't you store just a word in data and test for it?
    EX: spawn an item with NoPickup data.
    onPlayerPickupEvent , get the item, if it has the hidden data (Already hidden!) It will cancel it.
    You can do that.. i think
     
  11. Offline

    Plo124

    ChipDev
    Yes you can, go for it, you can store Hidden:HiTherePlzDontLetMeGetPickedUp,
    as long as your plugin then recognises this, then it will not pick up.
     
  12. Offline

    ChipDev

    When i read 'HiTherePlzDontLetMeGetPickedUp' It sounded like a tiny thing screaming "Mommy! I want Ice cream!!" lol
     
  13. Offline

    Mathias Eklund

    Why not just add ChatColor.COLOR_CHAR ? makes it invisible.
     
Thread Status:
Not open for further replies.

Share This Page