MyEssentials / MyTown2

Version 2 of MyTown - A Minecraft Server Protection Mod
The Unlicense
35 stars 22 forks source link

PermissionsEx Chat Handling #521

Open Andrew2070 opened 6 years ago

Andrew2070 commented 6 years ago

Hi again,

I created a class called ChatHandler which overrides the ChatManager class.

It basically does this: [CitizenRank][EmpireName] Username: message example: [L][Rome] Player123: Hello World

I want it to do this: [PexPrefix][CitizenRank][EmpireName][PexSuffix] Username: message example: [Admin][L][Rome] Username: message **No suffix in this example ^

Was wondering if you could help me set this up, because i get this error:

Caused by: java.lang.ClassNotFoundException: ru.tehkode.permissions.bukkit.PermissionsEx
    at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:101) ~[launchwrapper-1.12.jar:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_73]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_73]

So i modified the class to include:

    empire = CommandsEMP.getEmpireFromCitizen(res);
                    rank = CommandsEMP.getRankFromCitizen(res);
                    // TODO: Add herochat support?

                    String rankChat = "[" + rank.getChatName() + "]";
                    String empireChat = "[" + empire + "]";
                    if (ClassUtils.isBukkitLoaded() == true) {

                    if (Bukkit.getPluginManager().getPlugin("PermissionsEx").isEnabled() == true) { 
                        Empires.instance.LOG.warn("[Empires Mod] PermissiosnEX detected.. Applying hooks");
                        //get pex prefix below                  
                    Player bukkitplayer = Bukkit.getPlayer(target.getUniqueID());
                    String pexPrefix = ChatComponentPEX.getPrefix(bukkitplayer);
                    String pexSuffix = ChatComponentPEX.getSuffix(bukkitplayer);

                    Method method = ChatComponentPEX.class.getDeclaredMethod("getPrefix", Player.class);
                    String chat = pexPrefix + EnumChatFormatting.RED + rankChat + EnumChatFormatting.GOLD + empireChat + " "
                                + EnumChatFormatting.WHITE + player.getDisplayName() + pexSuffix + ": " + event.message;
                        //add pex prefix here ^

                        target.addChatMessage(new ChatComponentTranslation(chat));
                    }
Andrew2070 commented 6 years ago

ChatHandler:

Not sure if have to use reflection here... if so i don't know how to do that properly.

jgoett154 commented 6 years ago

Interacting with Bukkit plugins from Minecraft mods requires a good bit of work. It's been a while since I last attempted it but there were hoops that I had to jump though to get it working. If memory serves me the classpaths for mods and plugins are split or requires a bit of finessing. I'll look into it again and see how it should be done. The error you show specifically denotes PEX not being in the classpath the mods are looking in typically. Is it installed properly as a plug-in? Or is that error occurring during build time?-- ~ Joe Goett

Andrew2070 commented 6 years ago

Yeah. Bukkit plugins are on another classpath. Forge mods are on another classpath.

The error comes from calling the PermissionsEx.user().getPrefix()/getSuffix(). This happens while the server is running (with PEX installed), and as soon as i send a chat.

I set up the deploader to download PEX and the gradle script to compile it.

I built a fresh build with ./gradlew clean build -i

I'm pretty sure this is going to require reflection, but my reflection code hasn't fixed it.

Andrew2070 commented 6 years ago

I got the reflection part nailed: Now i get a different error, when I chat, and the chat message doesn't get printed.

[00:40:57 INFO]: [Empires Mod] PermissionsEX detected.. Applying hooks
[00:40:57 WARN]: java.lang.IllegalArgumentException: argument type mismatch
[00:40:57 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[00:40:57 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[00:40:57 WARN]:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[00:40:57 WARN]:    at java.lang.reflect.Method.invoke(Method.java:497)
[00:40:57 WARN]:    at EmpiresMod.Handlers.ChatHandler.onServerChatReceivedEvent(ChatHandler.java:68)
[00:40:57 WARN]:    at cpw.mods.fml.common.eventhandler.ASMEventHandler_7_ChatHandler_onServerChatReceivedEvent_ServerChatEvent.invoke(.dynamic)
[00:40:57 WARN]:    at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54)
[00:40:57 WARN]:    at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:140)
[00:40:57 WARN]:    at net.minecraftforge.common.ForgeHooks.onServerChatEvent(ForgeHooks.java:394)
jgoett154 commented 6 years ago

For the invoke calls, replace instance with null and Player.class with the actual Player instance. Doing that should fix the IllegalArgumentException.

Andrew2070 commented 6 years ago

Yeah that fixed IllegalArgumentException but caused: btw, do you have a discord?

13:22:11 WARN]: java.lang.reflect.InvocationTargetException
[13:22:11 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[13:22:11 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[13:22:11 WARN]:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[13:22:11 WARN]:    at java.lang.reflect.Method.invoke(Method.java:497)

[13:22:11 WARN]:    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
[13:22:11 WARN]:    at java.lang.Thread.run(Thread.java:745)
[13:22:11 WARN]: Caused by: java.lang.NoClassDefFoundError: ru/tehkode/permissions/bukkit/PermissionsEx
[13:22:11 WARN]:    at EmpiresMod.API.Chat.ChatComponentPEX.getPrefix(ChatComponentPEX.java:19)
[13:22:11 WARN]:    ... 39 more
[13:22:11 WARN]: Caused by: java.lang.ClassNotFoundException: ru.tehkode.permissions.bukkit.PermissionsEx
[13:22:11 WARN]:    at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:101)
[13:22:11 WARN]:    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[13:22:11 WARN]:    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[13:22:11 WARN]:    ... 40 more
[13:22:15 INFO]: [Empires Mod] PermissionsEX detected.. Applying hooks
[13:22:15 WARN]: java.lang.reflect.InvocationTargetException
[13:22:15 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[13:22:15 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[13:22:15 WARN]:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[13:22:15 WARN]:    at java.lang.reflect.Method.invoke(Method.java:497)
[13:22:15 WARN]:    at EmpiresMod.Handlers.ChatHandler.onServerChatReceivedEvent(ChatHandler.java:79)
[13:22:15 WARN]:    at cpw.mods.fml.common.eventhandler.ASMEventHandler_7_ChatHandler_onServerChatReceivedEvent_ServerChatEvent.invoke(.dynamic)
[13:22:15 WARN]:    at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54)
jgoett154 commented 6 years ago

That is likely caused by the separate classpaths for mods and plugins. If I remember right the way I resolved it was to create a Bukkit plugin that hooked into MyTown. It might have changed since the last time I did that but that's what I remember.-- ~ Joe Goett

Andrew2070 commented 6 years ago

Hmm. Well by making a plugin you still have to solve the classpath problem but from another perspective.

I think you added a modified bukkit.jar to the dependencies to fix the problem because i can't find any reflection in the MT2 src.

I'm at a loss here, don't know what to do.

jgoett154 commented 6 years ago

I know for certain it's not a modified Bukkit jar. If you look at GitHub.com/MyEssentials/ForgePermsCBBridge and GitHub.com/MyEssentials/ForgePerms you can see how we tackled it in the past.-- ~ Joe Goett

jgoett154 commented 6 years ago

You may also see how it is handled more recently in GitHub.com/MyEssentials/mypermissions-- ~ Joe Goett

jgoett154 commented 6 years ago

I appologize, I just saw we do indeed have a modified Bukkit jar, which I find odd. -- ~ Joe Goett

Andrew2070 commented 6 years ago

Ahahahaha, Yeah the modified bukkit jar is a splendid way to go about it. :P

The code is already setup to work with PermissionsEx, although i can't find explicit references to it's API.

However, i can't seem to use reflection to import its methods.

I tried to go another route, and create two chat handler classes, one for forge and the other for bukkit, where the latter gets triggered if it's installation is detected.

For some reason, i can't import methods from bukkitchathandler.instance into forgechathandler.instance.

So the crude, manual way of doing this doesn't work either.

Guess i'll have to go the reflection route, and if you have time it would be easier to work together.

Andrew2070 commented 6 years ago

Is this information fine for the deps.info file?

    {
        "repo": "http://pex-repo.aoeu.xyz/ru/tehkode/PermissionsEx/1.22.7/PermissionsEx-1.22.7.jar"
        "file": "PermissionsEx-1.22.7.jar"
        "class": "ru.tehkode.permissions.bukkit.PermissionsEx"

    }