Jannyboy11 / ScalaPluginLoader

PluginLoader for Bukkit that provides Scala runtime classes and enhanced APIs.
https://www.spigotmc.org/resources/scalaloader.59568/
GNU Lesser General Public License v3.0
26 stars 10 forks source link

It seems that Scala library is not accessable to ScalaPlugins. #12

Closed freeze-dolphin closed 3 years ago

freeze-dolphin commented 3 years ago

Problems

I use ScalaPluginLoader in order to avoid packaging Scala libraries into my plugins, but it still threw a NoSuchMethodError like this:

[12:18:36 WARN]: java.lang.reflect.InvocationTargetException
[12:18:36 WARN]:        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[12:18:36 WARN]:        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[12:18:36 WARN]:        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[12:18:36 WARN]:        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[12:18:36 WARN]:        at redempt.redlib.commandmanager.Command.execute(Command.java:818)
[12:18:36 WARN]:        at redempt.redlib.commandmanager.Command.execute(Command.java:844)
[12:18:36 WARN]:        at redempt.redlib.commandmanager.Command$1.execute(Command.java:503)
[12:18:36 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
[12:18:36 WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:826)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:2185)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:2000)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1953)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:49)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:7)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:35)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1266)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1259)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1333)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1130)
[12:18:36 WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291)
[12:18:36 WARN]:        at java.base/java.lang.Thread.run(Thread.java:834)
[12:18:36 WARN]: Caused by: java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)Ljava/lang/Object;
[12:18:36 WARN]:        at io.freeze_dolphin.plain_shop.guis.UIUtl$.create(UIUtl.scala:43)
[12:18:36 WARN]:        at io.freeze_dolphin.plain_shop.commands.threads.OpenThread.start(OpenThread.scala:19)
[12:18:36 WARN]:        at io.freeze_dolphin.plain_shop.commands.CommandBus.opn(CommandBus.scala:38)
[12:18:36 WARN]:        ... 26 more

Codes at UIUtl.scala:43:

for (f <- Objects.requireNonNull(
    new File(
        PlugGividado.pg.getDataFolder.getPath + sep + "storage" + sep + id
    ).listFiles(
        (_: File, name: String) => name.endsWith(".yml")
    )
)) {
    ...
} 
Jannyboy11 commented 3 years ago

Are you sure the version of Scala in your build.sbt/pom.xml is the same as the version used in the @Scala annotation on your main class? Because I'm not able to reproduce this. You're getting a NoSuchMethodError (indicating that a method doesn't exist), but you didn't get a NoClassDefFoundError (indicating that a class doesn't exist), so the Predef object has been found by the classloader.

It seems pretty much impossible that the ScalaPluginClassLoader cannot find classes from the Scala standardlibrary, because the parent classloader is the ScalaLibraryClassLoader which has access to those classes.

If you can give a minimal example of your code (either in source, or packaged as a jar) then I can debug this, but I have no place to start at this point. One other thing you can do is to execute the command /listscalaplugins which will output the version of Scala used by each plugin at runtime. For the ScalaExample en Scala3Example plugin this looks as follows: afbeelding This means that both plugins use the 'compatibility release version 3.0.x', (which is binary compatible with 2.13.x), and the versions of scala in their definitions are 2.13.6 and 3.0.1 respectively. ScalaLoader will always pick the latest version of the library to load plugins that use compatible versions, so in this case the ScalaExample plugin is getting the 3.0.1 library.

freeze-dolphin commented 3 years ago

I would appreciate it if you could help me with this problem!

Jannyboy11 commented 3 years ago

Alright I cloned it, replaced the ink.ptms.core dependency by the official spigot server dependency (you're not trying to supply-chain-attack me here, are you?) and got it to compile. I will investigate later tonight. Feel free to make your repo private again.

freeze-dolphin commented 3 years ago

Alright I cloned it, replaced the ink.ptms.core dependency by the official spigot server dependency (you're not trying to supply-chain-attack me here, are you?) and got it to compile. I will investigate later tonight. Feel free to make your repo private again.

Actually this is a modified paper core, I have asked the maintainer what's the differences between this and the official spigot core. And here is the original thread url: Note that you may need a translator to read this, which is in Chinese

Also, from his words, I may know the reason why it always shows me this error!

freeze-dolphin commented 3 years ago

Also, from his words, I may know the reason why it always shows me this error!

Well, it seems that this error still remains after replacing ink.ptms.core into the official spigot core.

Jannyboy11 commented 3 years ago

Well, it seems that this error still remains after replacing ink.ptms.core into the official spigot core.

Yes I know. I didn't say it was part of the solution, but I just didn't want to have a dependency from a http repository. A "supply chain attack" is an attack where a system gets compromised by compromomising trusted sources. I don't trust maven artifacts from a http repository because some man in the middle could change the bytecode in the jar file and potentially compromise my computer if I were to execute code from that custom fork of paper.

I'm really tired now actually, I'll have to dig into this some other time.

freeze-dolphin commented 3 years ago

But the way, this error occurs while performing command /plainshop opn.

If you want to try this plugin on your local server, you need to perform /plainshop crt to create a category first and then do opn.

Jannyboy11 commented 3 years ago

Alright, thanks!

freeze-dolphin commented 3 years ago

I have solved this by myself, thank you for your concern!

Jannyboy11 commented 3 years ago

If you did this by changing the code of ScalaLoader, then I would very much like to have your patch and include it in the project under the LGPL v3 license.