Solved Cast NBTBase to NBTTagString?

Discussion in 'Spigot Plugin Development' started by RockinChaos, Jan 14, 2018 at 4:48 AM.

  1. So I am working on converting a few things into reflections but I have ran into the following issue, I am trying to add a cast NBTBase to an NBTTagString but I keep getting various "no such method" errors. The issue is I am not sure how to cast an NBTBase to NBTTagString, in the original code it is; pages.add((NBTBase)new NBTTagString("JSON String Stuff Here"))

    The reason for this is that there is no method NBTTagList.add(NBTTagString);
    There is only NBTTagList.add(NBTBase);

    Original;
    Code (Text):
    NBTTagCompound tag = new NBTTagCompound();
    NBTTagList pages = new NBTTagList();
    pages.add((NBTBase)new NBTTagString("JSON String Stuff Here"));
    Reflection;
    Code (Text):
    Object pages = null;
     try { pages = Reflection.getNMS("NBTTagList").getConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); }

    Object tag = null;
    try { tag = Reflection.getNMS("NBTTagCompound").getConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); }

    Object tagconvert = null;
    try { tagconvert = Reflection.getNMS("NBTTagString").getConstructor(String.class).newInstance("JSON String Stuff Here"); } catch (Exception e) { e.printStackTrace(); }

    Object cast = null;
    try { cast = Reflection.getNMS("NBTBase").getConstructor(tagconvert.getClass()).newInstance(tagconvert); } catch (Exception e) { e.printStackTrace(); }

    try { pages.getClass().getMethod("add", Reflection.getNMS("NBTBase")).invoke(cast); } catch (Exception e) { e.printStackTrace(); }


    EDIT:


    This might make things a little more clear. I am trying to add objects to an NBTTagList, however the only method for (add) is NBTTagList.add(NBTBase). However if you do this without reflections you can easily add NBTTagCompound or NBTTagString even though it says only NBTBase.

    When I do;
    Code (Text):
    Object pages = null;
    try { pages = Reflection.getNMS("NBTTagList").getConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); }

    Object tagconvert = null;
    try { tagconvert = Reflection.getNMS("NBTTagString").getConstructor(String.class).newInstance(textBuilder.toString()); } catch (Exception e) { e.printStackTrace(); }


    try { pages.getClass().getMethod("add", Reflection.getNMS("NBTBase")).invoke(tagconvert); } catch (Exception e) { e.printStackTrace();
    I get the error (on the pages.getClass());
    Code (Text):
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    So somehow, I need to add a NBTBase cast to an NBTTagString.
     
    #1 RockinChaos, Jan 14, 2018 at 4:48 AM
    Last edited: Jan 14, 2018 at 6:43 PM
    • Informative Informative x 1
  2. That's actually something I looked into but it doesn't have any scenarios such as mine where I have to add a cast.
    Thanks for the info though.

    Specifically what I am trying to do is add pages to a book using NBTTags.
     
    • Creative Creative x 1
  3. I was originally using this but it is not implemented in versions of spigot/craftbukkit 1.9, 1.10, 1.11, etc. Only the latest.
    I'm trying to make everything universally work, if that makes any sense xD

    (Im meaning the TextComponent/Component, etc. I am working with JSON hover events btw.)
     
    #5 RockinChaos, Jan 14, 2018 at 5:39 AM
    Last edited: Jan 14, 2018 at 5:44 AM
  4. This might make things a little more clear. I am trying to add objects to an NBTTagList, however the only method for (add) is NBTTagList.add(NBTBase). However if you do this without reflections you can easily add NBTTagCompound or NBTTagString even though it says only NBTBase.

    When I do;
    Code (Text):
    Object pages = null;
    try { pages = Reflection.getNMS("NBTTagList").getConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); }

    Object tagconvert = null;
    try { tagconvert = Reflection.getNMS("NBTTagString").getConstructor(String.class).newInstance(textBuilder.toString()); } catch (Exception e) { e.printStackTrace(); }


    try { pages.getClass().getMethod("add", Reflection.getNMS("NBTBase")).invoke(tagconvert); } catch (Exception e) { e.printStackTrace();
    I get the error (on the pages.getClass());
    Code (Text):
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    So somehow, I need to add a NBTBase cast to an NBTTagString.
     
  5. Still haven't been able to figure this out. Anyone have any ideas?
     
  6. The first argument is the object you are calling the method of. The rest are the arguments.

    invoke(pages, tagconvert)
     
    • Winner Winner x 1
  7. Thank you!!
     

Share This Page