Solved Enchantment Glow w/o actual enchantment

Discussion in 'Plugin Development' started by Unica, Sep 22, 2014.

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

    Unica

    Hello!

    Im creating a minigame plugin, and im trying to add a purple enchantment glow to an itemstack.
    I can add it perfectly (w/o errors). But when I interact with this itemstack I get disconnected from the server.

    Any ideas ?

    Error
    Code:
    [19:51:09 INFO]: HeyImFat lost connection: Internal Exception: net.minecraft.util.
    io.netty.handler.codec.DecoderException: java.lang.NullPointerException
    EnchantGlow class (source not by me)
    Code:java
    1. public class EnchantGlow extends EnchantmentWrapper {
    2.  
    3. public EnchantGlow(int id) {
    4. super(id);
    5. }
    6.  
    7. @Override
    8. public boolean canEnchantItem(ItemStack item) {
    9. return true;
    10. }
    11.  
    12. @Override
    13. public boolean conflictsWith(Enchantment other) {
    14. return false;
    15. }
    16.  
    17. @Override
    18. public EnchantmentTarget getItemTarget() {
    19. return null;
    20. }
    21.  
    22. @Override
    23. public int getMaxLevel() {
    24. return 10;
    25. }
    26.  
    27. @Override
    28. public String getName() {
    29. return "Glow";
    30. }
    31.  
    32. @Override
    33. public int getStartLevel() {
    34. return 1;
    35. }
    36.  
    37. }



    Adding to itemstack
    Code:java
    1. private ItemStack convertWithEnchantment(ItemStack stack) {
    2. EnchantGlow glow = new EnchantGlow(120);
    3. try {
    4. stack.addUnsafeEnchantment(glow, 1);
    5. } catch (Exception e) {
    6. e.printStackTrace();
    7. }
    8. return stack;
    9. }
     
  2. Offline

    mine-care

    Okay there is a way through protocollib but I don't know it, can we see full error?
     
  3. Offline

    97WaterPolo

    Unica Try this.
    Code:java
    1. import org.bukkit.enchantments.Enchantment;
    2. import org.bukkit.enchantments.EnchantmentTarget;
    3. import org.bukkit.enchantments.EnchantmentWrapper;
    4. import org.bukkit.inventory.ItemStack;
    5.  
    6. import java.lang.reflect.Field;
    7.  
    8. public class Glow extends EnchantmentWrapper {
    9.  
    10. private static Enchantment glow;
    11.  
    12. public Glow(int id) {
    13. super(id);
    14. }
    15.  
    16. @Override
    17. public boolean canEnchantItem(ItemStack item) {
    18. return true;
    19. }
    20.  
    21. @Override
    22. public boolean conflictsWith(Enchantment other) {
    23. return false;
    24. }
    25.  
    26. @Override
    27. public EnchantmentTarget getItemTarget() {
    28. return null;
    29. }
    30.  
    31. @Override
    32. public int getMaxLevel() {
    33. return 10;
    34. }
    35.  
    36. @Override
    37. public String getName() {
    38. return "Glow";
    39. }
    40.  
    41. @Override
    42. public int getStartLevel() {
    43. return 1;
    44. }
    45.  
    46. public static Enchantment getGlow() {
    47. if(glow != null)
    48. return glow;
    49. try {
    50. Field f = Enchantment.class.getDeclaredField("acceptingNew");
    51. f.setAccessible(true);
    52. f.set(null, true);
    53. } catch(Exception e) {
    54. e.printStackTrace();
    55. }
    56. glow = new Glow(255);
    57. Enchantment.registerEnchantment(glow);
    58. return glow;
    59. }
    60.  
    61. public static void addGlow(ItemStack item) {
    62. Enchantment glow = getGlow();
    63. item.addEnchantment(glow, 1);
    64. }
    65. }


    Code:java
    1.  
    2. //Made without IDE, probably some error here, but you get the gist of it
    3. ItemStack is = new ItemStack(Material.STONE);
    4. Glow.addGlow(is);
    5.  
    6. if (is.containsEnchantment(Glow.getGlow())
    7. is.removeEnchantment(Glow.getGlow());
     
  4. Offline

    Unica

    97WaterPolo

    Thanks! Will try once I get home

    mine-care
    Haha, the funny thing is that this is the entire stacktrace ;)

    97WaterPolo

    Works like a charm!
    Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
    mine-care likes this.
  5. Offline

    97WaterPolo

    Unica
    Glad to see that it works! :) Thank SoThatsIt for that awesome class!
     
  6. Offline

    Unica

    97WaterPolo

    Hey, sorry for bothering you again, but there seems to occur a error.
    Code:
    Caused by: java.lang.IllegalArgumentException: Cannot set already-set enchantmen
    t
            at org.bukkit.enchantments.Enchantment.registerEnchantment(Enchantment.j
    ava:236) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at me.dubehh.Mech.Glow.getGlow(Glow.java:59) ~[?:?]
            at me.dubehh.GamePlay.GUI.MeleeMenu.convertWithEnchantment(MeleeMenu.jav
    a:64) ~[?:?]
            at me.dubehh.GamePlay.GUI.MeleeMenu.openMeleeInventory(MeleeMenu.java:48
    ) ~[?:?]
            at me.dubehh.Listeners.InventoryClick.onClick(InventoryClick.java:61) ~[
    ?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _67]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _67]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            ... 13 more
    ConvertWithEnchantment
    Code:java
    1. private ItemStack convertWithEnchantment(ItemStack stack) {
    2. if(stack.containsEnchantment(Glow.getGlow())){
    3. stack.removeEnchantment(Glow.getGlow());
    4. }
    5. Glow.addGlow(stack);
    6. return stack;
    7. }


    I did not change a thing on that other class,
    and I understand the error Cannot set already-set enchantmen
    t , but I thought removing it first, then adding it would fix that
     
  7. Offline

    97WaterPolo

    Unica
    Um what you did is correct I believe, try recompiling it, that method should work. Is there another place you are calling it and just happened to forget to check if it has a glow already?
     
  8. Offline

    Unica

    97WaterPolo

    Hey, This is the only place where I actually use the Glow thing.
    I've found when the error appears,
    it seems that when the first time my inventory menu get's opened (after a reload / restart) the error appears, after that the error only appears after a new reload.
     
  9. Offline

    fireblast709

    Unica check if the Enchantment is registered before attempting to register it. After the != null check, try getting the enchantment from Enchantment. If that returns null, register it.
     
  10. Offline

    Unica

    fireblast709
    Hey, Could u be a little bit more specific? Maybe with some pseudo code? I don't know exactly what u mean
     
  11. Offline

    fireblast709

    Unica Assuming we know 'id' (basically the id of your enchantment)
    Code:
    ench = getById(id)
    if(ench != null) // Thus, it was registered already!
        return;
    // Here you register your enchantment
     
  12. Offline

    Unica

    fireblast709

    Should this be called in the onEnable, ur when I call my method?
     
  13. Offline

    fireblast709

    Unica this is basically the behaviour of getGlow(). The fundamental difference is that you check if your static field is set, whereas I check if the Enchantment is actually registered.

    When you reload, that static field is garbage collected (and thus null), but the Enchantment is not unregistered. Then later you try to register it again (because that static field is null). But wait, it was already registered... this leads to the error that you are getting.
     
  14. Offline

    Unica

    fireblast709
    Hello sir, thanks for your reply and explanation, the bad thing is that still fail to create this correctly,

    Method
    Code:java
    1. @SuppressWarnings("deprecation")
    2. private ItemStack convertWithEnchantment(ItemStack stack) {
    3. Enchantment ench = Enchantment.getById(255); //Id is 255 which is set in the Glow class?
    4. if (ench != null) {
    5. System.out.println("Not null ;)");
    6. return stack;
    7. }
    8. Enchantment.registerEnchantment(Enchantment.getById(255));
    9. if (stack.containsEnchantment(Glow.getGlow())) {
    10. stack.removeEnchantment(Glow.getGlow());
    11. }
    12. Glow.addGlow(stack);
    13. return stack;
    14. }


    Error after fresh server startup
    Code:
    [22:52:49 ERROR]: Could not pass event InventoryClickEvent to Strikerz v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :1363) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
    Caused by: java.lang.NullPointerException
            at org.bukkit.enchantments.Enchantment.registerEnchantment(Enchantment.j
    ava:235) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at me.dubehh.GamePlay.GUI.MeleeMenu.convertWithEnchantment(MeleeMenu.jav
    a:70) ~[?:?]
            at me.dubehh.GamePlay.GUI.MeleeMenu.openMeleeInventory(MeleeMenu.java:49
    ) ~[?:?]
            at me.dubehh.Listeners.InventoryClick.onClick(InventoryClick.java:61) ~[
    ?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _67]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _67]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            ... 13 more
    Problem after /reload
    The debug message (not null ;)) gets fired, which means java assumes it's not null, I am certainly missing something


     
  15. Offline

    fireblast709

    Unica you try tot register null. (Since you know that Enchantment.getById(255) is null after line 7). Instead, create your EnchantmentWrapper there
     
  16. Offline

    Unica

    fireblast709

    Hello sir,
    Im really trying to understand this,

    Current problem: After a fresh restart, the enchantment works, but when I re-open my inventory menu, the enchantment is gone.
    Known cause: After a restart (not /reload) the 'ench' is no longer null, which means it breaks out of the code.
    So it returns the normal itemstack because it's not null. I tried different things inside this method, and I would really appreciate some pseudo code / push in the right direction.

    I am (decent) experienced in java / bukkit developing, it's just that I've never used this kind of stuff.

    EDIT:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. private ItemStack convertWithEnchantment(ItemStack stack) {
    3. Enchantment ench = Enchantment.getById(255);
    4. if (ench != null) {
    5. return stack;
    6. }
    7. if (stack.containsEnchantment(Glow.getGlow())) {
    8. stack.removeEnchantment(Glow.getGlow());
    9. }
    10. Glow.addGlow(stack);
    11. return stack;
    12. }

     
  17. Offline

    fireblast709

    Unica I just noticed you were editing convertWithEnchantment instead of getGlow
     
  18. Offline

    Unica

    fireblast709
    aah lol ;)
    So where should I put your earlier fix?
     
  19. Offline

    fireblast709

    Unica in getGlow() (since you were initializing it there)
     
  20. Offline

    Unica

    fireblast709
    Hey,
    I think all checks are done right?
    Im still getting the IllegalArgumentException

    Code:java
    1. public static Enchantment getGlow() {
    2. if(glow != null)
    3. return glow;
    4. try {
    5. Field f = Enchantment.class.getDeclaredField("acceptingNew");
    6. f.setAccessible(true);
    7. f.set(null, true);
    8. } catch(Exception e) {
    9. e.printStackTrace();
    10. }
    11. glow = new Glow(255);
    12. Enchantment.registerEnchantment(glow); //Glow is null, can't register (obviously)
    13. return glow;
    14. }


    Im not sure how to make that glow NOT null..
     
  21. Offline

    fireblast709

    Unica before the if statement, add
    Code:java
    1. if(glow != null)
    2. return glow;
    3. Enchantment g = Enchantment.getById(255);
    4. if(g instanceof Glow)
    5. {
    6. glow = (Glow) g;
    7. return glow;
    8. }
    9. // here you add the rest of your code, starting with the try-catch
    (Though note, if g isn't an instance of Glow, you need to handle that accordingly :p)
     
  22. Offline

    Unica

    fireblast709
    Hey, thanks for all your replies, but it seems it doesn't work.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public static Enchantment getGlow() {
    3. Enchantment g = Enchantment.getById(255);
    4. if (g instanceof Glow) {
    5. glow = (Glow) g;
    6. if (glow != null)
    7. return glow;
    8. try {
    9. Field f = Enchantment.class.getDeclaredField("acceptingNew");
    10. f.setAccessible(true);
    11. f.set(null, true);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. glow = new Glow(255);
    16. Enchantment.registerEnchantment(glow);
    17. return glow;
    18. } else {
    19. return Enchantment.getById(255); //Is null on first ever start so yea.
    20. }
    21. }


    On reloads, this works fine, but on the first ever start of the server the enchantment is null so I keep getting a "Enchantment cannot be null error".
    Code:
    [17:14:44 ERROR]: Could not pass event InventoryClickEvent to Strikerz v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :1363) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32)
     [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
    Caused by: java.lang.IllegalArgumentException: Enchantment cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at org.bukkit.inventory.ItemStack.addEnchantment(ItemStack.java:428) ~[c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            at me.dubehh.Mech.Glow.addGlow(Glow.java:75) ~[?:?]
            at me.dubehh.GamePlay.GUI.MeleeMenu.convertWithEnchantment(MeleeMenu.jav
    a:63) ~[?:?]
            at me.dubehh.GamePlay.GUI.MeleeMenu.openMeleeInventory(MeleeMenu.java:48
    ) ~[?:?]
            at me.dubehh.Listeners.InventoryClick.onClick(InventoryClick.java:66) ~[
    ?:?]
            at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-56-g3779cff-b3072jnks]
            ... 13 more
    >
     
  23. Offline

    fireblast709

    Unica nonono xD. Let me add some braces for clarification (so check my previous post in a minute or two)
     
  24. Offline

    Marten Mooij

    Do you even Glow?
     
  25. Offline

    Unica

    Marten Mooij
    Baby don't hurt me. No more.

    fireblast709
    The error appeared again :p
    I looked through jd.bukkit.org for possible fixes and I found Enchantment.values, which returns a list of all enchantments.

    Using that I did
    Code:java
    1. for(Enchantment e : Enchantment.values()){
    2. if(e.equals(glow))
    3. return glow;
    4. }

    Before I registered which worked :D
     
Thread Status:
Not open for further replies.

Share This Page