Skip to content

Commit

Permalink
update everything for new NT-RPG snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
GlassSpirit committed May 16, 2020
1 parent 092de7f commit 5e538ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
26 changes: 13 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ buildscript {
url = "https://files.minecraftforge.net/maven"
}
maven {
name = 'sponge'
url = 'https://repo.spongepowered.org/maven'
name = "sponge"
url = "https://repo.spongepowered.org/maven"
}
maven {
url = "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT"
classpath 'org.spongepowered:mixingradle:0.6-SNAPSHOT'
classpath "org.spongepowered:mixingradle:0.6-SNAPSHOT"
classpath "gradle.plugin.org.spongepowered:spongegradle:0.8.1"
}
}

apply plugin: "net.minecraftforge.gradle.forge"
apply plugin: "org.spongepowered.plugin"
apply plugin: 'org.spongepowered.mixin'
apply plugin: "org.spongepowered.mixin"

version = modVersion
group = modGroup
archivesBaseName = modBaseName

sourceCompatibility = targetCompatibility = 1.8
compileJava.options.encoding = compileTestJava.options.encoding = 'UTF-8'
compileJava.options.encoding = compileTestJava.options.encoding = "UTF-8"

sponge {
plugin {
meta {
id = 'cnpc-ntrpg'
id = "cnpc-ntrpg"
}
}
}
Expand All @@ -47,25 +47,25 @@ minecraft {

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url = "https://jitpack.io" }
maven { url = "https://repo.aikar.co/content/groups/aikar/" }
}

dependencies {
implementation('org.spongepowered:spongeforge:' + project.spongeForgeVersion + ':dev') {
exclude module: 'testmods'
implementation("org.spongepowered:spongeforge:" + project.spongeForgeVersion + ":dev") {
exclude module: "testmods"
}
implementation 'com.github.Sponge-RPG-dev.NT-RPG:Sponge-7:5.1.2020Sponge2'
implementation "com.github.Sponge-RPG-dev.NT-RPG:Sponge-7:15.5.2020SpongeSpigot2"
}

configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
resolutionStrategy.cacheDynamicVersionsFor 10, "minutes"
resolutionStrategy.cacheChangingModulesFor 10, "minutes"
}

sourceSets {
main {
ext.refMap = 'mixins.cnpcntrpg.refmap.json'
ext.refMap = "mixins.cnpcntrpg.refmap.json"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ modGroup=ru.glassspirit
modVersion=1.5
modBaseName=CNPC-NTRPG
forgeVersion=1.12.2-14.23.5.2847
spongeForgeVersion=1.12.2-2838-7.1.7-RC3928
spongeForgeVersion=1.12.2-2838-7.2.1
mcpVersion=stable_39
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ru.glassspirit.cnpcntrpg.forge;

import cz.neumimto.rpg.api.logging.Log;
import cz.neumimto.rpg.sponge.scripting.SpongeClassGenerator;
import cz.neumimto.rpg.common.bytecode.ClassGenerator;
import jdk.internal.dynalink.beans.StaticClass;
import jdk.nashorn.api.scripting.JSObject;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.annotation.AnnotationDescription;
Expand All @@ -17,10 +18,11 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
import java.util.function.Consumer;

public class ForgeEventListenerGenerator {
public class ForgeEventListenerGenerator extends ClassGenerator {

private static ClassLoader asmClassLoader;
private static Class forgeEventListener;
Expand All @@ -35,7 +37,7 @@ public class ForgeEventListenerGenerator {
}
}

public static void registerDynamicListener(List<ScriptObjectMirror> list) {
public void registerDynamicListener(List<JSObject> list) {
if (forgeEventListener != null) {
Log.info("Found Forge JS listener: " + forgeEventListener.getSimpleName() + " Unregistering");
MinecraftForge.EVENT_BUS.unregister(forgeEventListener);
Expand All @@ -46,7 +48,7 @@ public static void registerDynamicListener(List<ScriptObjectMirror> list) {
MinecraftForge.EVENT_BUS.register(forgeEventListener);
}

private static Class generateForgeListenerClass(List<ScriptObjectMirror> list) {
private Class generateForgeListenerClass(List<JSObject> list) {
Class c = null;
try {
String name = "DynamicForgeListener" + System.currentTimeMillis();
Expand All @@ -57,20 +59,47 @@ private static Class generateForgeListenerClass(List<ScriptObjectMirror> list) {
.name(name);

int i = 0;
for (ScriptObjectMirror obj : list) {
Class<?> type = ((StaticClass) obj.get("type")).getRepresentedClass();
Consumer consumer = (event) -> obj.callMember("consumer", event);
for (JSObject object : list) {
if (!object.hasMember("consumer") || !((JSObject) object.getMember("consumer")).isFunction()) {
Log.warn("JS event listener missing function consumer, skipping");
continue;
}
ScriptObjectMirror mirror = (ScriptObjectMirror) object.getMember("consumer");
Consumer consumer = (event) -> mirror.call(mirror, event);

EventPriority priority = EventPriority.valueOf(extract(obj, "priority", "NORMAL"));
String methodName = extract(obj, "methodName", "e" + i);
if (!object.hasMember("type")) {
Log.warn("Js event listener missing node type, skipping");
continue;
}
String className = "";
Object typeObj = object.getMember("type");
StaticClass staticClass;
if (typeObj instanceof StaticClass) {
staticClass = (StaticClass) typeObj;
className = staticClass.getRepresentedClass().getName();
Log.warn("JS event listener for the event " + className + ", it's no longer needed to reference the class (Java.type(...)), use only the wrapped string");
} else {
className = (String) typeObj;
}

Class type;
try {
type = Class.forName(className);
} catch (ClassNotFoundException e) {
Log.warn("JS event listener - unknown event " + className);
continue;
}

EventPriority priority = EventPriority.valueOf(extract(object, "priority", "NORMAL"));
String methodName = extract(object, "methodName", "e" + i);

AnnotationDescription annotation = AnnotationDescription.Builder.ofType(SubscribeEvent.class)
.define("priority", priority)
.build();

classBuilder = classBuilder.defineMethod(methodName, void.class, Visibility.PUBLIC, Ownership.STATIC)
.withParameter(type, "event")
.intercept(MethodDelegation.to(new SpongeClassGenerator.EventHandlerInterceptor(consumer)))
.intercept(MethodDelegation.to(new EventHandlerInterceptor(consumer)))
.annotateMethod(annotation);

i++;
Expand All @@ -83,7 +112,16 @@ private static Class generateForgeListenerClass(List<ScriptObjectMirror> list) {
return c;
}

private static <T> T extract(ScriptObjectMirror obj, String key, T def) {
return obj.hasMember(key) ? (T) obj.get(key) : def;
@Override
protected Type getListenerSubclass() {
// NOOP
return Object.class;
}

@Override
protected DynamicType.Builder<Object> visitImplSpecAnnListener(DynamicType.Builder.MethodDefinition.ReceiverTypeDefinition<Object> classBuilder, JSObject object) {
// NOOP
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import noppes.npcs.constants.EnumPacketServer;
import noppes.npcs.controllers.LinkedNpcController;
import noppes.npcs.entity.EntityNPCInterface;
import org.spongepowered.asm.lib.Opcodes;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cz.neumimto.rpg.api.Rpg;
import noppes.npcs.controllers.ScriptContainer;
import org.spongepowered.asm.lib.Opcodes;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down
2 changes: 1 addition & 1 deletion useful_scripts/!!ForgeEventListenerMagic.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/*
* IMPORTS
*/
const FORGE_EVENT_LISTENER_GENERATOR = Java.type("ru.glassspirit.cnpcntrpg.forge.ForgeEventListenerGenerator");
const FORGE_EVENT_LISTENER_GENERATOR = new (Java.type("ru.glassspirit.cnpcntrpg.forge.ForgeEventListenerGenerator"));

/*
* CONSTANTS
Expand Down

0 comments on commit 5e538ec

Please sign in to comment.