From 6de010f71a65f3850cdf16e4570b68cfe4cfd05a Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Wed, 27 Jan 2021 15:14:42 +0200 Subject: [PATCH] Add game rule API --- build.gradle | 4 +- .../registry/GameRuleFactory.java | 53 +++++++++++++++++++ .../registry/GameRuleRegistry.java | 35 ++++++++++++ .../registry/fabric/GameRuleFactoryImpl.java | 44 +++++++++++++++ .../registry/fabric/GameRuleRegistryImpl.java | 29 ++++++++++ .../registry/forge/GameRuleFactoryImpl.java | 45 ++++++++++++++++ .../registry/forge/GameRuleRegistryImpl.java | 28 ++++++++++ .../resources/META-INF/accesstransformer.cfg | 6 ++- .../shedaniel/architectury/test/TestMod.java | 2 + .../architectury/test/events/DebugEvents.java | 2 +- .../test/gamerule/TestGameRules.java | 38 +++++++++++++ 11 files changed, 282 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/me/shedaniel/architectury/registry/GameRuleFactory.java create mode 100644 common/src/main/java/me/shedaniel/architectury/registry/GameRuleRegistry.java create mode 100644 fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleFactoryImpl.java create mode 100644 fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleRegistryImpl.java create mode 100644 forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleFactoryImpl.java create mode 100644 forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleRegistryImpl.java create mode 100644 testmod-common/src/main/java/me/shedaniel/architectury/test/gamerule/TestGameRules.java diff --git a/build.gradle b/build.gradle index ce3baa79..6ab5cccb 100644 --- a/build.gradle +++ b/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 diff --git a/common/src/main/java/me/shedaniel/architectury/registry/GameRuleFactory.java b/common/src/main/java/me/shedaniel/architectury/registry/GameRuleFactory.java new file mode 100644 index 00000000..a51fdc85 --- /dev/null +++ b/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 createBooleanRule(boolean defaultValue) { + throw new AssertionError(); + } + + @ExpectPlatform + public static GameRules.Type createBooleanRule(boolean defaultValue, BiConsumer changedCallback) { + throw new AssertionError(); + } + + @ExpectPlatform + public static GameRules.Type createIntRule(int defaultValue) { + throw new AssertionError(); + } + + @ExpectPlatform + public static GameRules.Type createIntRule(int defaultValue, BiConsumer changedCallback) { + throw new AssertionError(); + } +} diff --git a/common/src/main/java/me/shedaniel/architectury/registry/GameRuleRegistry.java b/common/src/main/java/me/shedaniel/architectury/registry/GameRuleRegistry.java new file mode 100644 index 00000000..f85744ea --- /dev/null +++ b/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 > GameRules.Key register(String name, GameRules.Category category, GameRules.Type type) { + throw new AssertionError(); + } +} diff --git a/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleFactoryImpl.java b/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleFactoryImpl.java new file mode 100644 index 00000000..ef125401 --- /dev/null +++ b/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 createBooleanRule(boolean defaultValue) { + return GameRuleFactory.createBooleanRule(defaultValue); + } + + public static GameRules.Type createBooleanRule(boolean defaultValue, BiConsumer changedCallback) { + return GameRuleFactory.createBooleanRule(defaultValue, changedCallback); + } + + public static GameRules.Type createIntRule(int defaultValue) { + return GameRuleFactory.createIntRule(defaultValue); + } + + public static GameRules.Type createIntRule(int defaultValue, BiConsumer changedCallback) { + return GameRuleFactory.createIntRule(defaultValue, changedCallback); + } +} diff --git a/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleRegistryImpl.java b/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/GameRuleRegistryImpl.java new file mode 100644 index 00000000..bed77e86 --- /dev/null +++ b/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 > GameRules.Key register(String name, GameRules.Category category, GameRules.Type type) { + return GameRuleRegistry.register(name, category, type); + } +} diff --git a/forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleFactoryImpl.java b/forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleFactoryImpl.java new file mode 100644 index 00000000..c925bf5f --- /dev/null +++ b/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 createBooleanRule(boolean defaultValue) { + return GameRules.BooleanValue.create(defaultValue); + } + + public static GameRules.Type createBooleanRule(boolean defaultValue, BiConsumer changedCallback) { + return GameRules.BooleanValue.create(defaultValue, changedCallback); + } + + public static GameRules.Type createIntRule(int defaultValue) { + return GameRules.IntegerValue.create(defaultValue); + } + + public static GameRules.Type createIntRule(int defaultValue, BiConsumer changedCallback) { + return GameRules.IntegerValue.create(defaultValue, changedCallback); + } +} diff --git a/forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleRegistryImpl.java b/forge/src/main/java/me/shedaniel/architectury/registry/forge/GameRuleRegistryImpl.java new file mode 100644 index 00000000..5fd5de13 --- /dev/null +++ b/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 > GameRules.Key register(String name, GameRules.Category category, GameRules.Type type) { + return GameRules.register(name, category, type); + } +} diff --git a/forge/src/main/resources/META-INF/accesstransformer.cfg b/forge/src/main/resources/META-INF/accesstransformer.cfg index d9108c0c..523978ec 100644 --- a/forge/src/main/resources/META-INF/accesstransformer.cfg +++ b/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 (Ljava/lang/String;)V \ No newline at end of file +public net.minecraft.world.storage.FolderName (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 diff --git a/testmod-common/src/main/java/me/shedaniel/architectury/test/TestMod.java b/testmod-common/src/main/java/me/shedaniel/architectury/test/TestMod.java index 732322eb..7a53cd3c 100644 --- a/testmod-common/src/main/java/me/shedaniel/architectury/test/TestMod.java +++ b/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(); } diff --git a/testmod-common/src/main/java/me/shedaniel/architectury/test/events/DebugEvents.java b/testmod-common/src/main/java/me/shedaniel/architectury/test/events/DebugEvents.java index 3f8e67b4..31bd02a9 100644 --- a/testmod-common/src/main/java/me/shedaniel/architectury/test/events/DebugEvents.java +++ b/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()); } } diff --git a/testmod-common/src/main/java/me/shedaniel/architectury/test/gamerule/TestGameRules.java b/testmod-common/src/main/java/me/shedaniel/architectury/test/gamerule/TestGameRules.java new file mode 100644 index 00000000..d20aad5f --- /dev/null +++ b/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 SIMPLE_BOOL = register("simpleBool", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true)); + public static final GameRules.Key SIMPLE_INT = register("simpleInt", GameRules.Category.MISC, GameRuleFactory.createIntRule(10)); + public static final GameRules.Key CALLBACK_BOOL = register("callbackBool", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true, (server, value) -> LOGGER.info("changed to {}", value.get()))); + public static final GameRules.Key CALLBACK_INT = register("callbackInt", GameRules.Category.MISC, GameRuleFactory.createIntRule(10, (server, value) -> LOGGER.info("changed to {}", value.get()))); + + public static void init() {} +}