Skip to content

Commit

Permalink
fixes & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Nov 24, 2023
1 parent da4f030 commit b66d027
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 11 deletions.
8 changes: 8 additions & 0 deletions examples/postInit/hard_mode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// PACKMODE: hard

crafting.shapedBuilder()
.output(item('placeholdername:clay_2'))
.shape([[null, item('minecraft:diamond'), null],
[item('minecraft:diamond'), null, item('minecraft:diamond')],
[null, item('minecraft:diamond'), null]])
.register()
10 changes: 10 additions & 0 deletions examples/postInit/normal_mode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

// PACKMODE: normal

crafting.removeByOutput(item('minecraft:furnace'))
crafting.shapedBuilder()
.output(item('placeholdername:clay_2'))
.shape([[null, item('minecraft:iron_ingot'), null],
[item('minecraft:iron_ingot'), null, item('minecraft:iron_ingot')],
[null, item('minecraft:iron_ingot'), null]])
.register()
2 changes: 1 addition & 1 deletion examples/postInit/vanilla.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ crafting.shapedBuilder()
crafting.remove('minecraft:mossy_stonebrick') // Remove the entry with the recipe ID
crafting.remove(resource('minecraft:stonebrick'))
crafting.removeByOutput(item('minecraft:gold_ingot')) // Remove all recipes with the output
crafting.removeByInput(item('minecraft:iron_ingot')) // Remove all recipes containing the ingredient as an input
//crafting.removeByInput(item('minecraft:iron_ingot')) // Remove all recipes containing the ingredient as an input
//crafting.removeAll()


Expand Down
11 changes: 9 additions & 2 deletions examples/runConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
"preInit": [
"preInit/"
],
"init": [
],
"init": [],
"postInit": [
"postInit/"
]
},
"packmode": {
"values": [
"normal",
"hard"
],
"default": "normal",
"current": "hard"
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ public static File getResourcesFile() {
return resourcesFile;
}

@NotNull
public static File getRunConfigFile() {
if (runConfigFile == null) {
throw new IllegalStateException("GroovyScript is not yet loaded!");
}
return runConfigFile;
}

@NotNull
public static GroovyScriptSandbox getSandbox() {
if (sandbox == null) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/cleanroommc/groovyscript/Packmode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cleanroommc.groovyscript;

import com.cleanroommc.groovyscript.helper.Alias;
import com.google.common.base.CaseFormat;
import net.minecraftforge.fml.common.eventhandler.Event;

public class Packmode {
Expand All @@ -11,7 +13,13 @@ public static String getPackmode() {
}

public static void updatePackmode(String packmode) {
Packmode.packmode = packmode;
if (!isValidPackmode(packmode)) throw new IllegalArgumentException("Undefined packmode '" + packmode + "'");
Packmode.packmode = Alias.autoConvertTo(packmode, CaseFormat.LOWER_UNDERSCORE);
GroovyScript.getRunConfig().writeAndSavePackmode(Packmode.packmode);
}

public static boolean isValidPackmode(String mode) {
return GroovyScript.getRunConfig().isValidPackmode(mode);
}

public static class ChangeEvent extends Event {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.Packmode;
import com.cleanroommc.groovyscript.network.IPacket;
import com.cleanroommc.groovyscript.network.NetworkHandler;
import com.cleanroommc.groovyscript.network.SReloadJei;
import com.cleanroommc.groovyscript.sandbox.LoadStage;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
Expand All @@ -28,11 +31,12 @@ public class PackmodeCommand extends CommandBase {
public void execute(@NotNull MinecraftServer server, @NotNull ICommandSender sender, String @NotNull [] args) throws CommandException {
if (args.length == 0) throw new CommandException("Missing packmode");
String packmode = args[0];
if (!GroovyScript.getRunConfig().isValidPackmode(packmode)) throw new CommandException("Invalid packmode: " + packmode);
if (!Packmode.isValidPackmode(packmode)) throw new CommandException("Invalid packmode: " + packmode);
Packmode.updatePackmode(packmode);
sender.sendMessage(new TextComponentString("Changing packmode to " + packmode + ". This might take a minute."));
long time = GroovyScript.runGroovyScriptsInLoader(LoadStage.POST_INIT);
GroovyScript.postScriptRunResult((EntityPlayerMP) sender, false, true, true, time);
NetworkHandler.sendToPlayer(new SReloadJei(), (EntityPlayerMP) sender);
MinecraftForge.EVENT_BUS.post(new Packmode.ChangeEvent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public void removeEntry(ResourceLocation name) {
}
Integer id = this.ids.inverse().remove(entry);
Object ownerOverride = this.owners.inverse().remove(entry);
this.backups.add(Triple.of(entry, id, ownerOverride));
if (this.scripted == null || !this.scripted.contains(entry)) {
this.backups.add(Triple.of(entry, id, ownerOverride));
}
groovyscript$putDummy(entry, name, id, ownerOverride);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/helper/Alias.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.cleanroommc.groovyscript.helper;

import com.google.common.base.CaseFormat;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.regex.Pattern;

public class Alias extends ArrayList<String> {

Expand Down Expand Up @@ -96,4 +98,25 @@ public static <T> void putAll(String name, CaseFormat caseFormat, T object, Map<
map.put(alias, object);
}
}

private static final Pattern lowerCamel = Pattern.compile("[a-z]+[a-zA-Z]*");
private static final Pattern upperCamel = Pattern.compile("[A-Z][a-zA-Z]*");
private static final Pattern lowerUnderscore = Pattern.compile("[a-z]+(_[a-z]+)*");
private static final Pattern upperUnderscore = Pattern.compile("[A-Z]+(_[A-Z]+)*");

@NotNull
public static CaseFormat detectCaseFormat(String s) {
if (s == null || s.isEmpty()) throw new IllegalArgumentException("String must not be empty");
if (lowerCamel.matcher(s).matches()) return CaseFormat.LOWER_CAMEL;
if (upperCamel.matcher(s).matches()) return CaseFormat.UPPER_CAMEL;
if (lowerUnderscore.matcher(s).matches()) return CaseFormat.LOWER_UNDERSCORE;
if (upperUnderscore.matcher(s).matches()) return CaseFormat.UPPER_UNDERSCORE;
throw new IllegalArgumentException("String has invalid format!");
}

public static String autoConvertTo(String s, CaseFormat format) {
CaseFormat fromFormat = detectCaseFormat(s);
if (fromFormat == format) return s;
return fromFormat.to(format, s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public static String getString(JsonObject json, String defaultValue, String... k
for (String key : keys) {
if (json.has(key)) {
JsonElement jsonElement = json.get(key);
return jsonElement.getAsString();
if (jsonElement.isJsonPrimitive()) {
return jsonElement.getAsString();
}
}
}
return defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.Packmode;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.helper.Alias;
import com.cleanroommc.groovyscript.registry.ReloadableRegistryManager;
import com.google.common.base.CaseFormat;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
Expand Down Expand Up @@ -72,6 +74,7 @@ private static boolean processPreprocessor(String line) {
String[] parts = line.split(":", 2);
String[] args = NO_ARGS;
if (parts.length > 1) {
args = parts[1].split(",");
for (int i = 0; i < args.length; i++) {
args[i] = args[i].trim();
}
Expand Down Expand Up @@ -113,7 +116,7 @@ private static boolean checkSide(String[] sides) {

private static boolean checkPackmode(String[] modes) {
for (String mode : modes) {
if (Packmode.getPackmode().equalsIgnoreCase(mode)) {
if (Packmode.getPackmode().equals(Alias.autoConvertTo(mode, CaseFormat.LOWER_UNDERSCORE))) {
return true;
}
}
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/com/cleanroommc/groovyscript/sandbox/RunConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.Packmode;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.helper.Alias;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.google.common.base.CaseFormat;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
Expand Down Expand Up @@ -45,6 +47,8 @@ public static JsonObject createDefaultJson() {
JsonArray postInit = new JsonArray();
loaders.add("postInit", postInit);
postInit.add("postInit/");
JsonObject packmode = new JsonObject();
packmode.add("values", new JsonArray());
return json;
}

Expand All @@ -56,12 +60,12 @@ public static JsonObject createDefaultJson() {
modMetadata.version = "0.0.0";
}

private JsonObject rawRunConfig;
private final String packName;
private final String packId;
private final String version;
private final Map<String, List<String>> classes = new Object2ObjectOpenHashMap<>();
private final Map<String, List<String>> loaderPaths = new Object2ObjectOpenHashMap<>();
// TODO pack modes
private final Set<String> packmodes = new ObjectOpenHashSet<>();
private final Map<String, List<String>> packmodePaths = new Object2ObjectOpenHashMap<>();
// TODO asm
Expand All @@ -83,6 +87,7 @@ public static boolean isGroovyFile(String path) {
}

public RunConfig(JsonObject json) {
this.rawRunConfig = json;
String name = JsonHelper.getString(json, "", "packName", "name");
String id = JsonHelper.getString(json, "", "packId", "id");
Pattern idPattern = Pattern.compile("[a-z_]+");
Expand All @@ -104,6 +109,7 @@ public void reload(JsonObject json) {
if (GroovyScript.isSandboxLoaded() && GroovyScript.getSandbox().isRunning()) {
throw new RuntimeException();
}
this.rawRunConfig = json;
this.debug = JsonHelper.getBoolean(json, false, "debug");
this.classes.clear();
this.loaderPaths.clear();
Expand Down Expand Up @@ -168,10 +174,13 @@ public void reload(JsonObject json) {
JsonArray modes = JsonHelper.getJsonArray(jsonPackmode, "values", "types");
for (JsonElement je : modes) {
if (je.isJsonPrimitive()) {
this.packmodes.add(je.getAsString());
String pm = Alias.autoConvertTo(je.getAsString(), CaseFormat.UPPER_CAMEL);
Alias.generateAliases(this.packmodes, pm, CaseFormat.UPPER_CAMEL);
}
}
Packmode.updatePackmode(JsonHelper.getString(jsonPackmode, "", "current", "default"));
if (Packmode.getPackmode() == null || Packmode.getPackmode().isEmpty()) {
Packmode.updatePackmode(JsonHelper.getString(jsonPackmode, "", "current", "default"));
}
}

public String getPackName() {
Expand Down Expand Up @@ -215,6 +224,19 @@ public boolean isValidPackmode(String packmode) {
return this.packmodes.contains(packmode);
}

@ApiStatus.Internal
public void writeAndSavePackmode(String mode) {
JsonObject packmode = JsonHelper.getJsonObject(this.rawRunConfig, (JsonObject) null, "packmode");
if (packmode == null) {
packmode = new JsonObject();
this.rawRunConfig.add("packmode", packmode);
}
String current = JsonHelper.getString(packmode, null, "current");
if (current != null && current.equals(mode)) return;
packmode.addProperty("current", mode);
JsonHelper.saveJson(GroovyScript.getRunConfigFile(), this.rawRunConfig);
}

public ResourceLocation makeLoc(String name) {
return new ResourceLocation(getPackId(), name);
}
Expand Down

0 comments on commit b66d027

Please sign in to comment.