Skip to content

Commit 2de715d

Browse files
authored
add Default GameRules (#279)
* add Default GameRules * add command to apply gamerules * add some debug logs for the default gamerules * restore boolean to default value * fix use_examples_folder server using `run/examples` * fastutil the map
1 parent c9eb5d9 commit 2de715d

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

examples/postInit/custom/vanilla.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,8 @@ eventManager.listen(EnderTeleportEvent) { event ->
260260
command.registerCommand('groovy_test') { server, sender, args ->
261261
sender.sendMessage('Hello from GroovyScript')
262262
}
263+
264+
// Default GameRules
265+
gameRule.add('doDaylightCycle', 'false')
266+
gameRule.add(['mobGriefing': 'false', 'keepInventory': 'true'])
267+
gameRule.setWarnNewGameRule(true)

src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cleanroommc.groovyscript.api.GroovyLog;
55
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
66
import com.cleanroommc.groovyscript.compat.mods.jei.JeiPlugin;
7+
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
78
import com.cleanroommc.groovyscript.documentation.Documentation;
89
import com.cleanroommc.groovyscript.helper.StyleConstant;
910
import com.cleanroommc.groovyscript.network.NetworkHandler;
@@ -25,6 +26,7 @@
2526
import java.io.File;
2627
import java.util.Arrays;
2728
import java.util.List;
29+
import java.util.Objects;
2830

2931
public class GSCommand extends CommandTreeBase {
3032

@@ -58,6 +60,11 @@ public GSCommand() {
5860
addSubcommand(new InfoLookingCommand());
5961
addSubcommand(new InfoSelfCommand());
6062

63+
addSubcommand(new SimpleCommand("applyDefaultGameRules", (server, sender, args) -> {
64+
VanillaModule.gameRule.setDefaultGameRules(Objects.requireNonNull(server.getServer()).getEntityWorld().getGameRules());
65+
sender.sendMessage(new TextComponentString("Applied the default GameRules to the current world."));
66+
}));
67+
6168

6269
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"));
6370

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.cleanroommc.groovyscript.compat.vanilla;
2+
3+
import com.cleanroommc.groovyscript.api.GroovyBlacklist;
4+
import com.cleanroommc.groovyscript.api.GroovyLog;
5+
import com.cleanroommc.groovyscript.api.IScriptReloadable;
6+
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
7+
import com.cleanroommc.groovyscript.registry.NamedRegistry;
8+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
9+
import net.minecraft.world.GameRules;
10+
11+
import java.util.Map;
12+
13+
public class GameRule extends NamedRegistry implements IScriptReloadable {
14+
15+
private static final String LOG_MESSAGE = "Could not find an already existing rule with the name {}. This may be intentional! If it is, you can disable this via `gameRule.setWarnNewGameRule(false)`";
16+
private final Map<String, String> defaultGameRules = new Object2ObjectOpenHashMap<>();
17+
private boolean warnNewGameRule;
18+
19+
@GroovyBlacklist
20+
public void setDefaultGameRules(GameRules gameRules) {
21+
defaultGameRules.forEach((k, v) -> {
22+
if (warnNewGameRule && !gameRules.hasRule(k)) GroovyLog.get().warn(LOG_MESSAGE, k);
23+
GroovyLog.get().debug("Setting the GameRule '{}' to the value '{}'", k, v);
24+
gameRules.setOrCreateGameRule(k, v);
25+
});
26+
GroovyLog.get().debug("Set or created {} GameRules", defaultGameRules.size());
27+
}
28+
29+
@MethodDescription
30+
public void add(String gameRule, String value) {
31+
defaultGameRules.put(gameRule, value);
32+
}
33+
34+
@MethodDescription
35+
public void add(Map<String, String> gameRules) {
36+
defaultGameRules.putAll(gameRules);
37+
}
38+
39+
@MethodDescription
40+
public void setWarnNewGameRule(boolean value) {
41+
warnNewGameRule = value;
42+
}
43+
44+
@Override
45+
@GroovyBlacklist
46+
public void onReload() {
47+
defaultGameRules.clear();
48+
warnNewGameRule = false;
49+
}
50+
51+
@Override
52+
@GroovyBlacklist
53+
public void afterScriptLoad() {}
54+
}

src/main/java/com/cleanroommc/groovyscript/compat/vanilla/VanillaModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class VanillaModule extends GroovyPropertyContainer implements IScriptRel
2727
public static final Rarity rarity = new Rarity();
2828
public static final InWorldCrafting inWorldCrafting = new InWorldCrafting();
2929
public static final Command command = new Command();
30+
public static final GameRule gameRule = new GameRule();
3031

3132
public static void initializeBinding() {
3233
GroovyScript.getSandbox().registerBinding(crafting);
@@ -38,6 +39,7 @@ public static void initializeBinding() {
3839
GroovyScript.getSandbox().registerBinding(rarity);
3940
GroovyScript.getSandbox().registerBinding(inWorldCrafting);
4041
GroovyScript.getSandbox().registerBinding(command);
42+
GroovyScript.getSandbox().registerBinding(gameRule);
4143

4244
ExpansionHelper.mixinClass(ItemStack.class, ItemStackExpansion.class);
4345
ExpansionHelper.mixinClass(ICommandSender.class, CommandSenderExpansion.class);
@@ -54,6 +56,7 @@ public void onReload() {
5456
player.onReload();
5557
inWorldCrafting.onReload();
5658
command.onReload();
59+
gameRule.onReload();
5760
}
5861

5962
@Override

src/main/java/com/cleanroommc/groovyscript/event/EventHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import net.minecraftforge.common.config.ConfigManager;
3838
import net.minecraftforge.event.RegistryEvent;
3939
import net.minecraftforge.event.world.ExplosionEvent;
40+
import net.minecraftforge.event.world.WorldEvent;
4041
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
4142
import net.minecraftforge.fml.common.Loader;
4243
import net.minecraftforge.fml.common.eventhandler.EventPriority;
@@ -78,6 +79,11 @@ public static void registerTextures(TextureStitchEvent.Post event) {
7879
GroovyFluid.initTextures(event.getMap());
7980
}
8081

82+
@SubscribeEvent
83+
public static void createSpawnPosition(WorldEvent.CreateSpawnPosition event) {
84+
VanillaModule.gameRule.setDefaultGameRules(event.getWorld().getGameRules());
85+
}
86+
8187
@SubscribeEvent
8288
public static void playerLogin(PlayerEvent.PlayerLoggedInEvent event) {
8389
// clear all errors and post

src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public static void initialize(File minecraftHome, Logger log) {
5151
cachePath = new File(SandboxData.minecraftHome, "cache" + File.separator + "groovy");
5252
// If we are launching with the environment variable set to use the examples folder, use the examples folder for easy and consistent testing.
5353
if (Boolean.parseBoolean(System.getProperty("groovyscript.use_examples_folder"))) {
54-
scriptPath = new File(minecraftHome.getParentFile(), "examples");
54+
scriptPath = new File(SandboxData.minecraftHome.getParentFile(), "examples");
5555
} else {
56-
scriptPath = new File(minecraftHome, "groovy");
56+
scriptPath = new File(SandboxData.minecraftHome, "groovy");
5757
}
5858
try {
5959
scriptPath = scriptPath.getCanonicalFile();

0 commit comments

Comments
 (0)