Browse Source

Add game rule API

Juuxel 4 years ago
parent
commit
6de010f71a

+ 2 - 2
build.gradle

@@ -1,6 +1,6 @@
 plugins {
-    id "architectury-plugin" version "2.0.64"
-    id "forgified-fabric-loom" version "0.6.53" apply false
+    id "architectury-plugin" version "2.0.65"
+    id "forgified-fabric-loom" version "0.6.54" apply false
     id "org.cadixdev.licenser" version "0.5.0"
     id "com.jfrog.bintray" version "1.8.4"
     id "com.matthewprenger.cursegradle" version "1.4.0" apply false

+ 53 - 0
common/src/main/java/me/shedaniel/architectury/registry/GameRuleFactory.java

@@ -0,0 +1,53 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry;
+
+import me.shedaniel.architectury.annotations.ExpectPlatform;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.level.GameRules;
+
+import java.util.function.BiConsumer;
+
+/**
+ * A utility class for creating game rule types.
+ */
+public final class GameRuleFactory {
+    private GameRuleFactory() {}
+
+    @ExpectPlatform
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue) {
+        throw new AssertionError();
+    }
+
+    @ExpectPlatform
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue, BiConsumer<MinecraftServer, GameRules.BooleanValue> changedCallback) {
+        throw new AssertionError();
+    }
+
+    @ExpectPlatform
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue) {
+        throw new AssertionError();
+    }
+
+    @ExpectPlatform
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue, BiConsumer<MinecraftServer, GameRules.IntegerValue> changedCallback) {
+        throw new AssertionError();
+    }
+}

+ 35 - 0
common/src/main/java/me/shedaniel/architectury/registry/GameRuleRegistry.java

@@ -0,0 +1,35 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry;
+
+import me.shedaniel.architectury.annotations.ExpectPlatform;
+import net.minecraft.world.level.GameRules;
+
+/**
+ * A registry for registering game rules.
+ */
+public final class GameRuleRegistry {
+    private GameRuleRegistry() {}
+
+    @ExpectPlatform
+    public static <T extends GameRules.Value<T>> GameRules.Key<T> register(String name, GameRules.Category category, GameRules.Type<T> type) {
+        throw new AssertionError();
+    }
+}

+ 44 - 0
fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleFactoryImpl.java

@@ -0,0 +1,44 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry.fabric;
+
+import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.level.GameRules;
+
+import java.util.function.BiConsumer;
+
+public class GameRuleFactoryImpl {
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue) {
+        return GameRuleFactory.createBooleanRule(defaultValue);
+    }
+
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue, BiConsumer<MinecraftServer, GameRules.BooleanValue> changedCallback) {
+        return GameRuleFactory.createBooleanRule(defaultValue, changedCallback);
+    }
+
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue) {
+        return GameRuleFactory.createIntRule(defaultValue);
+    }
+
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue, BiConsumer<MinecraftServer, GameRules.IntegerValue> changedCallback) {
+        return GameRuleFactory.createIntRule(defaultValue, changedCallback);
+    }
+}

+ 29 - 0
fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleRegistryImpl.java

@@ -0,0 +1,29 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry.fabric;
+
+import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;
+import net.minecraft.world.level.GameRules;
+
+public class GameRuleRegistryImpl {
+    public static <T extends GameRules.Value<T>> GameRules.Key<T> register(String name, GameRules.Category category, GameRules.Type<T> type) {
+        return GameRuleRegistry.register(name, category, type);
+    }
+}

+ 45 - 0
forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleFactoryImpl.java

@@ -0,0 +1,45 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry.forge;
+
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.level.GameRules;
+
+import java.util.function.BiConsumer;
+
+public class GameRuleFactoryImpl {
+    private GameRuleFactoryImpl() {}
+
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue) {
+        return GameRules.BooleanValue.create(defaultValue);
+    }
+
+    public static GameRules.Type<GameRules.BooleanValue> createBooleanRule(boolean defaultValue, BiConsumer<MinecraftServer, GameRules.BooleanValue> changedCallback) {
+        return GameRules.BooleanValue.create(defaultValue, changedCallback);
+    }
+
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue) {
+        return GameRules.IntegerValue.create(defaultValue);
+    }
+
+    public static GameRules.Type<GameRules.IntegerValue> createIntRule(int defaultValue, BiConsumer<MinecraftServer, GameRules.IntegerValue> changedCallback) {
+        return GameRules.IntegerValue.create(defaultValue, changedCallback);
+    }
+}

+ 28 - 0
forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleRegistryImpl.java

@@ -0,0 +1,28 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.registry.forge;
+
+import net.minecraft.world.level.GameRules;
+
+public class GameRuleRegistryImpl {
+    public static <T extends GameRules.Value<T>> GameRules.Key<T> register(String name, GameRules.Category category, GameRules.Type<T> type) {
+        return GameRules.register(name, category, type);
+    }
+}

+ 5 - 1
forge/src/main/resources/META-INF/accesstransformer.cfg

@@ -32,4 +32,8 @@ public-f net.minecraft.world.biome.BiomeAmbience field_242523_e # skyColor
 public-f net.minecraft.world.biome.BiomeAmbience field_242524_f # foliageColor
 public-f net.minecraft.world.biome.BiomeAmbience field_242525_g # grassColor
 public-f net.minecraft.world.biome.BiomeAmbience field_242526_h # grassColorModifier
-public net.minecraft.world.storage.FolderName <init>(Ljava/lang/String;)V
+public net.minecraft.world.storage.FolderName <init>(Ljava/lang/String;)V
+public net.minecraft.world.GameRules$BooleanValue func_223567_b(ZLjava/util/function/BiConsumer;)Lnet/minecraft/world/GameRules$RuleType; # create
+public net.minecraft.world.GameRules$BooleanValue func_223568_b(Z)Lnet/minecraft/world/GameRules$RuleType; # create
+public net.minecraft.world.GameRules$IntegerValue func_223564_a(ILjava/util/function/BiConsumer;)Lnet/minecraft/world/GameRules$RuleType; # create
+public net.minecraft.world.GameRules$IntegerValue func_223559_b(I)Lnet/minecraft/world/GameRules$RuleType; # create

+ 2 - 0
testmod-common/src/main/java/me/shedaniel/architectury/test/TestMod.java

@@ -24,6 +24,7 @@ import me.shedaniel.architectury.test.debug.ConsoleMessageSink;
 import me.shedaniel.architectury.test.events.DebugEvents;
 import me.shedaniel.architectury.test.debug.MessageSink;
 import me.shedaniel.architectury.test.debug.client.ClientOverlayMessageSink;
+import me.shedaniel.architectury.test.gamerule.TestGameRules;
 import me.shedaniel.architectury.test.registry.TestRegistries;
 import me.shedaniel.architectury.test.registry.client.TestKeybinds;
 import me.shedaniel.architectury.utils.Env;
@@ -36,6 +37,7 @@ public class TestMod {
     public static void initialize() {
         DebugEvents.initialize();
         TestRegistries.initialize();
+        TestGameRules.init();
         if (Platform.getEnvironment() == Env.CLIENT)
             TestKeybinds.initialize();
     }

+ 1 - 1
testmod-common/src/main/java/me/shedaniel/architectury/test/events/DebugEvents.java

@@ -271,6 +271,6 @@ public class DebugEvents {
     }
     
     private static String toSimpleName(Object o) {
-        return o.getClass().getSimpleName() + "@" + Integer.toHexString(o.hashCode());
+        return o == null ? "null" : o.getClass().getSimpleName() + "@" + Integer.toHexString(o.hashCode());
     }
 }

+ 38 - 0
testmod-common/src/main/java/me/shedaniel/architectury/test/gamerule/TestGameRules.java

@@ -0,0 +1,38 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021 shedaniel
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.test.gamerule;
+
+import me.shedaniel.architectury.registry.GameRuleFactory;
+import net.minecraft.world.level.GameRules;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import static me.shedaniel.architectury.registry.GameRuleRegistry.register;
+
+public class TestGameRules {
+    private static final Logger LOGGER = LogManager.getLogger();
+
+    public static final GameRules.Key<GameRules.BooleanValue> SIMPLE_BOOL = register("simpleBool", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true));
+    public static final GameRules.Key<GameRules.IntegerValue> SIMPLE_INT = register("simpleInt", GameRules.Category.MISC, GameRuleFactory.createIntRule(10));
+    public static final GameRules.Key<GameRules.BooleanValue> CALLBACK_BOOL = register("callbackBool", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true, (server, value) -> LOGGER.info("changed to {}", value.get())));
+    public static final GameRules.Key<GameRules.IntegerValue> CALLBACK_INT = register("callbackInt", GameRules.Category.MISC, GameRuleFactory.createIntRule(10, (server, value) -> LOGGER.info("changed to {}", value.get())));
+
+    public static void init() {}
+}