mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Initial gamerule API (#159)
Co-authored-by: Alexdoru <[email protected]>
This commit is contained in:
1 parent
1b8d921810
commit
19e6ef05e1
4 files changed
+145
-2
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package com.gtnewhorizon.gtnhlib.gamerules;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.world.GameRules;
|
||||
|
||||
public class GameRuleRegistry {
|
||||
|
||||
private static final Map<String, IGameRule> gameRulesMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Registers a new {@link IGameRule} into the registry. New rules should be registered during the mod's
|
||||
* pre-initialization or initialization phase.
|
||||
*
|
||||
* @param rule The rule to be registered
|
||||
*/
|
||||
public static void registerGameRule(IGameRule rule) {
|
||||
if (gameRulesMap.containsKey(rule.getName())) {
|
||||
throw new RuntimeException("Duplicate GameRule Name: " + rule.getName());
|
||||
}
|
||||
gameRulesMap.put(rule.getName(), rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method used by the registry. This should not be called from dependent mods. This method is triggered by
|
||||
* the underlying MC game rules system via a mixin to notify the registered {@link IGameRule} of updates to the
|
||||
* value.
|
||||
*
|
||||
* @param name The name of the GameRule that has been updated.
|
||||
* @param value The new value of the GameRule.
|
||||
* @param gameRules The underlying MC GameRules instance that was updated.
|
||||
*/
|
||||
public static void notifyGameRuleUpdate(String name, String value, GameRules gameRules) {
|
||||
IGameRule rule = gameRulesMap.get(name);
|
||||
if (rule != null) {
|
||||
rule.onValueUpdated(value, Boolean.parseBoolean(value), gameRules);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method used by the registry. This should be called from dependent mods. This method is triggered by the
|
||||
* underlying MC game rules system via a mixin to add all of the game rules from the registry into the underlying
|
||||
* system.
|
||||
*
|
||||
* @param gameRules The GameRules instance for our rules to be added to.
|
||||
*/
|
||||
public static void injectGameRules(GameRules gameRules) {
|
||||
for (IGameRule rule : GameRuleRegistry.gameRulesMap.values()) {
|
||||
gameRules.addGameRule(rule.getName(), rule.defaultValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.gtnewhorizon.gtnhlib.gamerules;
|
||||
|
||||
import net.minecraft.world.GameRules;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* This interface can be implemented and then registered with {@link GameRuleRegistry} to easily create a new game rule.
|
||||
*/
|
||||
public interface IGameRule {
|
||||
|
||||
/**
|
||||
* @return The name of this game rule.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* @return The default value for this game rule. The rule will be set to this upon creation.
|
||||
*/
|
||||
String defaultValue();
|
||||
|
||||
/**
|
||||
* This method will be called when the underlying value of a GameRule is updated. This can be used to update a
|
||||
* locally cached value. By default this method will do nothing, in which case to know the value of the game rule
|
||||
* you will need to query it through traditional Minecraft/Forge means.
|
||||
*
|
||||
* @param value The newly updated raw String value.
|
||||
* @param boolValue The String value parsed as a boolean.
|
||||
* @param gameRules The underlying MC GameRules instance was updated.
|
||||
*/
|
||||
default void onValueUpdated(String value, boolean boolValue, GameRules gameRules) {}
|
||||
|
||||
/**
|
||||
* Queries a value from the underlying MC GameRule's system. This is guaranteed to be the currently set value.
|
||||
*
|
||||
* @param world The world in which to make the GameRule query.
|
||||
* @return The current String value of the game rule.
|
||||
*/
|
||||
default String queryStringValue(World world) {
|
||||
return world.getGameRules().getGameRuleStringValue(getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries a value from the underlying MC GameRule's system. This is guaranteed to be the currently set value.
|
||||
*
|
||||
* @param world The world in which to make the GameRule query.
|
||||
* @return The current value of the game rule as a boolean.
|
||||
*/
|
||||
default boolean queryBooleanValue(World world) {
|
||||
return world.getGameRules().getGameRuleBooleanValue(getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,12 @@ package com.gtnewhorizon.gtnhlib.mixins;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.mixin.*;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.IMixins;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.ITargetedMod;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.MixinBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.Phase;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.Side;
|
||||
import com.gtnewhorizon.gtnhlib.mixin.TargetedMod;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -27,7 +32,10 @@ public enum Mixins implements IMixins {
|
||||
.setApplyIf(() -> Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false")))
|
||||
.addMixinClasses("debug.MixinDynamicTexture", "debug.MixinTextureAtlasSprite")),
|
||||
SERVER_TICKING(new MixinBuilder("Backport MinecraftServer ticking methods").addTargetedMod(TargetedMod.VANILLA)
|
||||
.setSide(Side.BOTH).setPhase(Phase.EARLY).addMixinClasses("MixinMinecraftServer").setApplyIf(() -> true));
|
||||
.setSide(Side.BOTH).setPhase(Phase.EARLY).addMixinClasses("MixinMinecraftServer").setApplyIf(() -> true)),
|
||||
GAME_RULES(
|
||||
new MixinBuilder("Mixins to support the IGameRule/GameRuleRegistry API").addTargetedMod(TargetedMod.VANILLA)
|
||||
.setSide(Side.BOTH).setPhase(Phase.EARLY).setApplyIf(() -> true).addMixinClasses("MixinGameRules"));
|
||||
|
||||
private final List<String> mixinClasses;
|
||||
private final Supplier<Boolean> applyIf;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import net.minecraft.world.GameRules;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.gamerules.GameRuleRegistry;
|
||||
|
||||
@Mixin(GameRules.class)
|
||||
public abstract class MixinGameRules {
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
private void gtnhlib$initGameRules(CallbackInfo ci) {
|
||||
GameRuleRegistry.injectGameRules((GameRules) (Object) this);
|
||||
}
|
||||
|
||||
@Inject(
|
||||
method = "setOrCreateGameRule",
|
||||
at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/world/GameRules$Value;setValue(Ljava/lang/String;)V",
|
||||
shift = At.Shift.AFTER))
|
||||
private void GTNHLib$onGameRuleChanged(String ruleName, String value, CallbackInfo ci) {
|
||||
GameRuleRegistry.notifyGameRuleUpdate(ruleName, value, (GameRules) (Object) this);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user