Skip to content

Mixin #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft

Mixin #299

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/mixins/PlayerListMixin.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.NetworkManager;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.server.management.PlayerList

@Mixin(value=PlayerList.class, remap=false)
class PlayerListMixin {

@Inject(method = "initializeConnectionToPlayer", at = @At(value = "INVOKE",
target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;firePlayerLoggedIn(Lnet/minecraft/entity/player/EntityPlayer;)V"))
void init(NetworkManager netManager, EntityPlayerMP playerIn, NetHandlerPlayServer nethandlerplayserver, CallbackInfo ci) {
playerIn.sendMessage(new TextComponentString("Player " + playerIn.getName() + " logged in"));
}

}
14 changes: 14 additions & 0 deletions examples/mixins/TestClassMixin.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import com.cleanroommc.groovyscript.TestClass
import com.cleanroommc.groovyscript.api.GroovyLog

@Mixin(value = TestClass.class, remap=false)
class TestClassMixin {

@Inject(method = "sayHello", at = @At("HEAD"), cancellable = true)
private static void sayBye(CallbackInfo ci) {
GroovyLog.get().info("Bye from TestClassMixin");
ci.cancel();
}

}
10 changes: 1 addition & 9 deletions examples/runConfig.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be changed?

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
"version": "1.0.0",
"debug": true,
"loaders": {
"preInit": [
"classes/",
"preInit/"
],
"init": [
"init/"
],
"postInit": [
"postInit/",
"recipes/"
"test/"
]
},
"packmode": {
Expand Down
4 changes: 4 additions & 0 deletions examples/test/main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

import com.cleanroommc.groovyscript.TestClass

TestClass.sayHello()
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void onConstruction(FMLConstructionEvent event) {
GroovyDeobfMapper.init();
LinkGeneratorHooks.init();
ReloadableRegistryManager.init();
((GroovyLogImpl) GroovyLog.get()).setPassedEarly();
GroovyScript.sandbox = new GroovyScriptSandbox();
ModSupport.INSTANCE.setup(event.getASMHarvestedData());

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/TestClass.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this stay in here? feels like test code

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cleanroommc.groovyscript;

import com.cleanroommc.groovyscript.api.GroovyLog;

public class TestClass {

static {
GroovyLog.get().info("Hello we are now initialising TestClass");
}

public static void sayHello() {
GroovyLog.get().info("Hello from TestClass");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public GSCommand() {
sender.sendMessage(new TextComponentString("Applied the default GameRules to the current world."));
}));


addSubcommand(new SimpleCommand("wiki", (server, sender, args) -> sender.sendMessage(getTextForUrl("GroovyScript wiki", "Click to open wiki in browser", new TextComponentString("https://cleanroommc.com/groovy-script/"))), "doc", "docs", "documentation"));

addSubcommand(new SimpleCommand("generateWiki", (server, sender, args) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.groovyscript.core;

import com.cleanroommc.groovyscript.sandbox.MixinSandbox;
import com.cleanroommc.groovyscript.sandbox.SandboxData;
import com.google.common.collect.ImmutableList;
import net.minecraftforge.common.ForgeVersion;
Expand Down Expand Up @@ -42,6 +43,7 @@ public void injectData(Map<String, Object> data) {
source = (File) data.getOrDefault("coremodLocation", null);
SandboxData.initialize((File) FMLInjectionData.data()[6], LOG);
SideOnlyConfig.init();
MixinSandbox.loadMixins();
}

@Override
Expand All @@ -51,6 +53,6 @@ public String getAccessTransformerClass() {

@Override
public List<String> getMixinConfigs() {
return ImmutableList.of("mixin.groovyscript.json");
return ImmutableList.of("mixin.groovyscript.groovy.json", "mixin.groovyscript.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class GroovyScriptTransformer implements IClassTransformer {
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null) return null;
if (transformedName.contains("PlayerLoggedInEvent") || transformedName.endsWith("PlayerList") || transformedName.endsWith("EntityPlayerMP") || transformedName.endsWith("NetworkManager") || transformedName.endsWith("NetHandlerPlayServer") || transformedName.endsWith("TextComponentString")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is debug code

GroovyScriptCore.LOG.info("Loading class {}", transformedName);
}
switch (name) {
case InvokerHelperVisitor.CLASS_NAME: {
ClassWriter classWriter = new ClassWriter(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void init(SourceUnit context, CallbackInfo ci) {
// inject correct package declaration into script
String packageName = rel.substring(0, i).replace('/', '.') + '.';
this.packageNode = new PackageNode(packageName);
this.packageNode.setSynthetic(true);
}
}

Expand Down
Loading