Replacing Message with item

Discussion in 'Plugin Development' started by AdamDev, Aug 21, 2017.

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

    AdamDev

    Hi, as the title explains as what I'm trying to do I'll give you a break down.

    In my code I'm trying to do a player chat event that every time someone types "[item]" it replaces the message with the item in their hand. Everytime I do it, it returns in chat [null].

    Here's my chat event:
    Code:
    public void replace chat(AsyncPlayerChatEvent e) {
        if (e.getMessage().contains("[item]")) {
            String is = p.getInventory().getItemInHand().getItemMeta().getDisplayName();
    String rmessage = e.getMessage().replace("[item]", "["+is+"]");
    e.setMessage(rmessage);
    return;
       }
    }
    P.S I know that getItemInHand() is a 1.8 part. I'm making for a server. Will probably move it to another server for 1.9
    P.S.S I typed this on my phone might be some errors, correct my code if I'm wrong.
     
  2. Online

    timtower Administrator Administrator Moderator

    @AdamDev Item might not have a displayname, displayname is a name given by plugins or an anvil.
     
  3. Offline

    AdamDev

    So, that's for custom items so what about normal items, for example stone?
     
  4. Online

    timtower Administrator Administrator Moderator

    @AdamDev You could use the material name, but there are also ways to get the name that is similar or the same to what the client uses, not sure how though.
     
  5. Offline

    AdamDev

    I'll keep this thread open to anyone else that comes over it. For now I'll look for my self to see if I find it.
     
    timtower likes this.
  6. Offline

    Reflxction

    Check if the item has an item meta, then check if it has a display name, then check if the name equals the thingy I guess
     
  7. Offline

    Horsey

    You can create a map of materials and their user friendly name, or you could look into the LocaleLanguage class, which seems to already have a map of everything and a translation based on the player's locale, or you could use a plugin like WhatIsIt.

    An example that I came across for the locale translation:
    Code:java
    1.  
    2. Player player = // Your player
    3. LocaleLanguage locale = ((CraftPlayer) Player).getHandle().getLocale();
    4. player.sendMessage(l.c("gui.yes"));
    5.  
    6. /*
    7. Will send the player "yes" in english, and "aye" in pirate language.
    8. */
    9.  

    EDIT: Stupid me. The getLocale method no longer exists, try this: https://bukkit.org/threads/get-a-players-minecraft-language.172468/

    EDIT2: Stupid me (again). Turns out you can just use new LocaleLanguage().c("gui.yes"); Minecraft defaults to creating a LocaleLanguage in US English. You could create a new class with this code:

    Code:java
    1.  
    2. package net.minecraft.server.v1_12_R1;
    3.  
    4. import com.google.common.base.Splitter;
    5. import com.google.common.collect.Iterables;
    6. import com.google.common.collect.Maps;
    7. import java.io.IOException;
    8. import java.io.InputStream;
    9. import java.nio.charset.StandardCharsets;
    10. import java.util.IllegalFormatException;
    11. import java.util.Map;
    12. import java.util.regex.Matcher;
    13. import java.util.regex.Pattern;
    14. import org.apache.commons.io.IOUtils;
    15.  
    16. public class LocaleLanguage
    17. {
    18. private static final Pattern a = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
    19. private static final Splitter b = Splitter.on('=').limit(2);
    20.  
    21. private static final LocaleLanguage c = new LocaleLanguage();
    22.  
    23. private final Map<String, String> d = Maps.newHashMap();
    24. private long e;
    25.  
    26. public LocaleLanguage()
    27. {
    28. try
    29. {
    30. InputStream localInputStream = LocaleLanguage.class.getResourceAsStream("/assets/minecraft/lang/en_us.lang"); // YOU NEED TO CHANGE THE LANGUAGE HERE (soz for the caps)
    31. for (String str1 : IOUtils.readLines(localInputStream, StandardCharsets.UTF_8))
    32. {
    33. if ((!str1.isEmpty()) && (str1.charAt(0) != '#'))
    34. {
    35. String[] arrayOfString = (String[])Iterables.toArray(b.split(str1), String.class);
    36.  
    37. if ((arrayOfString != null) && (arrayOfString.length == 2))
    38. {
    39. String str2 = arrayOfString[0];
    40. String str3 = a.matcher(arrayOfString[1]).replaceAll("%$1s");
    41.  
    42. this.d.put(str2, str3);
    43. }
    44. }
    45. }
    46. this.e = System.currentTimeMillis();
    47. }
    48. catch (IOException localIOException)
    49. {
    50. }
    51. }
    52.  
    53. static LocaleLanguage a() {
    54. return c;
    55. }
    56.  
    57. public synchronized String a(String paramString)
    58. {
    59. return c(paramString);
    60. }
    61.  
    62. public synchronized String a(String paramString, Object[] paramArrayOfObject) {
    63. String str = c(paramString);
    64. try {
    65. return String.format(str, paramArrayOfObject); } catch (IllegalFormatException localIllegalFormatException) {
    66. }
    67. return "Format error: " + str;
    68. }
    69.  
    70. private String c(String paramString)
    71. {
    72. String str = (String)this.d.get(paramString);
    73. return str == null ? paramString : str;
    74. }
    75.  
    76. public synchronized boolean b(String paramString) {
    77. return this.d.containsKey(paramString);
    78. }
    79.  
    80. public long c() {
    81. return this.e;
    82. }
    83. }

    EDIT3: wow this is a lot of edits.. The above code is for 1.12, idk if it matters, but you might wanna use the 1.8 code instead.

    EDIT4: Umm I seem to have overlooked that minecraft doesn't have the same names as bukkit, oh well, it was a fun experiment, list of the translation for the player (in english). I'm not sure what the '[m' at the end is, but it can be safely ignored. You'll have to create a DB by yourself, or use something like WhatIsIt
     
    Last edited: Aug 21, 2017
Thread Status:
Not open for further replies.

Share This Page