Solved Set motd (using reflection)

Discussion in 'Plugin Development' started by SeaMoon69, May 17, 2018.

  1. Offline

    SeaMoon69

    Plugin version : 1.8 1.9 1.10 1.11 1.12
    API : Spigot 1.8 - 1.9 - 1.10 - 1.11 - 1.12
    IDE : Eclipse Oxygene A3
    JDK : 10.0.1

    Hello bukkit,
    I have a error when y start this :
    Code:
    private static void setMotd(String str) {
         try {
           Class<?> craftserver = Class.forName("org.bukkit.craftbukkit." + getVersion() + ".CraftServer");
           Class<?> dedicatedPlayerList = craftserver.getMethod("getHandle").getReturnType();
           Class<?> dedicatedServer = dedicatedPlayerList.getMethod("getServer").getReturnType();
           dedicatedServer.getMethod("setMotd", String.class).invoke(Bukkit.getServer(), str);
         } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
           e.printStackTrace();
         }
       }
    
    If you can help me, thank you !

    Sorry, I'am french but bukkit.fr is down.
     
    Last edited: May 18, 2018
  2. Online

    timtower Ninja on the waves Moderator

    @SeaMoon69 Please post the error as well, because this doesn't tell a lot.
    And there is a bukkit.fr?
     
    RcExtract likes this.
  3. Offline

    SeaMoon69

    Yes but it's been a week since the site is in maintenance

    Error :
    Error (open)

    Code:
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    [17:07:43 WARN]:  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [17:07:43 WARN]:  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [17:07:43 WARN]:  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [17:07:43 WARN]:  at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    [17:07:43 WARN]:  at PACKAGE.setMotd(SetModtv1_8_R3.java:49)
    [17:07:43 WARN]:  at PACKAGE.actualise(SetModtv1_8_R3.java:34)
    [17:07:43 WARN]:  at PACKAGE.run(ActualseModt.java:13)
    [17:07:43 WARN]:  at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    [17:07:43 WARN]:  at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    [17:07:43 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723)
    [17:07:43 WARN]:  at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    [17:07:43 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    [17:07:43 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    [17:07:43 WARN]:  at java.base/java.lang.Thread.run(Unknown Source)
    


    Line 49 = dedicatedServer.getMethod("setMotd", String.class).invoke(Bukkit.getServer(), str);
     
  4. Online

    timtower Ninja on the waves Moderator

    @SeaMoon69 Bukkit.getServer() isn't the same type as dedicatedServer.
     
  5. Offline

    SeaMoon69

    So how to use this method by reflect ?
     
  6. Online

    timtower Ninja on the waves Moderator

    @SeaMoon69 You need to provide the correct object.
    Do it without reflection first, then you can build it back to use reflection.
     
  7. Offline

    SeaMoon69

    The code without reflect :
    ((CraftServer) Bukkit.getServer()).getHandle().getServer().setMotd(String);
    But I don't know how to convert to reflect...
     
  8. @SeaMoon69
    The only change you have to make to your original code is just replacing "craftserver" so that it is equal to Bukkit.getServer() casted to a CraftServer (instead of just getting the class object).
     
  9. Offline

    SeaMoon69

    Thanks @Zombie_Striker

    Solution (open)
    Code:
    Class<?> craftserver = Class.forName("org.bukkit.craftbukkit." + getVersion() + ".CraftServer");
                Class<?> dedicatedPlayerList = craftserver.getMethod("getHandle").getReturnType();
                Class<?> dedicatedServer = dedicatedPlayerList.getMethod("getServer").getReturnType();
                Object obj = dedicatedPlayerList.getMethod("getServer").invoke(craftserver.getMethod("getHandle").invoke(craftserver.cast(Bukkit.getServer())));
                dedicatedServer.getMethod("setMotd", String.class).invoke(obj, str);
     
    Last edited: May 18, 2018

Share This Page