本帖最后由 无敌三脚猫 于 2023-3-2 22:57 编辑 
https://www.mcbbs.net/thread-874131-1-1.html
这边我看过很多次了,最近才发现一个问题
跟没接触过cnpc的人解释一下,这个mod有一个脚本系统,可以用Nashorn引擎写js脚本
上述脚本中出现了一个奇怪的东西复制代码咦?这哪儿来的无参构造?
如果在java里写new net.minecraftforge.event.ServerChatEvent(),会显示构造函数 ServerChatEvent()未定义
因为ServerChatEvent真的只有这一个构造函数复制代码可能不同版本具体的参数不一样,不过这不重要,这个类确实是在每个版本都没有无参构造,所以Nashorn是怎么把它new出来的?而且没有报错(我写过了)
不关nashorn的事,是forge干的
https://www.mcbbs.net/thread-874131-1-1.html
这边我看过很多次了,最近才发现一个问题
跟没接触过cnpc的人解释一下,这个mod有一个脚本系统,可以用Nashorn引擎写js脚本
上述脚本中出现了一个奇怪的东西
- var Event=Java.type("net.minecraftforge.event.ServerChatEvent");
 
- var event=new Event();
如果在java里写new net.minecraftforge.event.ServerChatEvent(),会显示构造函数 ServerChatEvent()未定义
因为ServerChatEvent真的只有这一个构造函数
-     public ServerChatEvent(EntityPlayerMP player, String message, ITextComponent component)
 
-     {
 
-         super();
 
-         this.message = message;
 
-         this.player = player;
 
-         this.username = player.getGameProfile().getName();
 
-         this.component = component;
 
- }
不关nashorn的事,是forge干的
Unsafe 的 allocateInstance 方法?没用过 Nashorn 盲猜的。
 本帖最后由 Neige 于 2023-3-2 13:23 编辑 
我拿bukkit的AsyncPlayerChatEvent试了一下,不行,会报they do not match any of its method signatures
你这很怪
我拿bukkit的AsyncPlayerChatEvent试了一下,不行,会报they do not match any of its method signatures
你这很怪
Nashorn 调用无参构造会报出
TypeError: Can not create new object with constructor ClassA with the passed arguments; they do not match any of its method signatures.
Unsafe.allocateInstance 可以做到强制实例 但从我这里来看Nashorn 没有采用这个
也许是版本不同
TypeError: Can not create new object with constructor ClassA with the passed arguments; they do not match any of its method signatures.
Unsafe.allocateInstance 可以做到强制实例 但从我这里来看Nashorn 没有采用这个
也许是版本不同