From eff1565fbab14585bd03f285d969ba5d5b959c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=82=A6=E8=A7=A3=E8=AF=B4?= Date: Sun, 4 Aug 2024 11:14:39 +0800 Subject: [PATCH] Prevent block falling --- .../java/ziyue/tjmetro/fabric/MainFabric.java | 2 - .../tjmetro/fabric/MainFabricClient.java | 2 + .../fabric/mixin/FallingBlockMixin.java | 64 +++++++++---------- .../java/ziyue/tjmetro/mod/TianjinMetro.java | 4 +- .../ziyue/tjmetro/mod/data/IGuiExtension.java | 2 +- fabric/src/main/resources/fabric.mod.json | 3 +- forge/build.gradle | 2 + .../forge/mixin/ATGameRuleBooleanMixin.java | 47 ++++++++++++++ .../forge/mixin/FallingBlockMixin.java | 56 ++++++++++++++++ .../PropertyCreateBooleanGameRule.java | 17 +++++ .../tjmetro/mapping/BooleanGameRule.java | 32 ++++++++++ .../ziyue/tjmetro/mapping/FilterBuilder.java | 7 +- .../tjmetro/mapping/GameRuleRegistry.java | 16 +++++ .../main/resources/tjmetro-forge.mixins.json | 16 +++++ gradle.properties | 2 +- 15 files changed, 230 insertions(+), 42 deletions(-) create mode 100644 forge/src/main/java/ziyue/tjmetro/forge/mixin/ATGameRuleBooleanMixin.java create mode 100644 forge/src/main/java/ziyue/tjmetro/forge/mixin/FallingBlockMixin.java create mode 100644 forge/src/main/java/ziyue/tjmetro/forge/property/PropertyCreateBooleanGameRule.java create mode 100644 forge/src/main/java/ziyue/tjmetro/mapping/BooleanGameRule.java create mode 100644 forge/src/main/java/ziyue/tjmetro/mapping/GameRuleRegistry.java create mode 100644 forge/src/main/resources/tjmetro-forge.mixins.json diff --git a/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabric.java b/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabric.java index d91db18..bafbdcc 100644 --- a/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabric.java +++ b/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabric.java @@ -9,7 +9,5 @@ public final class MainFabric implements ModInitializer { @Override public void onInitialize() { TianjinMetro.init(); - Registry.FILTERS_REGISTRY_ITEM.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().data)); - Registry.FILTERS_REGISTRY_BLOCK.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().asItem().data)); } } diff --git a/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabricClient.java b/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabricClient.java index 4c5665b..96605d7 100644 --- a/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabricClient.java +++ b/fabric/src/main/java/ziyue/tjmetro/fabric/MainFabricClient.java @@ -9,5 +9,7 @@ public final class MainFabricClient implements ClientModInitializer @Override public void onInitializeClient() { TianjinMetroClient.init(); + Registry.FILTERS_REGISTRY_ITEM.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().data)); + Registry.FILTERS_REGISTRY_BLOCK.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().asItem().data)); } } diff --git a/fabric/src/main/java/ziyue/tjmetro/fabric/mixin/FallingBlockMixin.java b/fabric/src/main/java/ziyue/tjmetro/fabric/mixin/FallingBlockMixin.java index 1355eea..d59e658 100644 --- a/fabric/src/main/java/ziyue/tjmetro/fabric/mixin/FallingBlockMixin.java +++ b/fabric/src/main/java/ziyue/tjmetro/fabric/mixin/FallingBlockMixin.java @@ -1,32 +1,32 @@ -//package ziyue.tjmetro.fabric.mixin; -// -//import net.minecraft.block.Block; -//import net.minecraft.block.BlockState; -//import net.minecraft.block.FallingBlock; -//import net.minecraft.server.world.ServerWorld; -//import net.minecraft.util.math.BlockPos; -//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 ziyue.tjmetro.mapping.BooleanGameRule; -//import ziyue.tjmetro.mod.TianjinMetro; -// -//@Mixin(FallingBlock.class) -//public abstract class FallingBlockMixin extends Block -//{ -// public FallingBlockMixin(Settings settings) { -// super(settings); -// } -// -// @Inject(at = @At("HEAD"), method = "scheduledTick", cancellable = true) -//#if MC_VERSION < "11904" -// private void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, java.util.Random random, CallbackInfo ci) -//#else -// private void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, net.minecraft.util.math.random.Random random, CallbackInfo ci) -//#endif -// { -// if (BooleanGameRule.getValue(new org.mtr.mapping.holder.ServerWorld(world), TianjinMetro.NO_FALLING_BLOCK)) -// ci.cancel(); -// } -//} +package ziyue.tjmetro.fabric.mixin; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.FallingBlock; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; +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 ziyue.tjmetro.mapping.BooleanGameRule; +import ziyue.tjmetro.mod.TianjinMetro; + +@Mixin(FallingBlock.class) +public abstract class FallingBlockMixin extends Block +{ + public FallingBlockMixin(Settings settings) { + super(settings); + } + + @Inject(at = @At("HEAD"), method = "scheduledTick", cancellable = true) +#if MC_VERSION < "11904" + private void beforeScheduledTick(BlockState state, ServerWorld world, BlockPos pos, java.util.Random random, CallbackInfo ci) +#else + private void beforeScheduledTick(BlockState state, ServerWorld world, BlockPos pos, net.minecraft.util.math.random.Random random, CallbackInfo ci) +#endif + { + if (BooleanGameRule.getValue(new org.mtr.mapping.holder.ServerWorld(world), TianjinMetro.NO_FALLING_BLOCK)) + ci.cancel(); + } +} diff --git a/fabric/src/main/java/ziyue/tjmetro/mod/TianjinMetro.java b/fabric/src/main/java/ziyue/tjmetro/mod/TianjinMetro.java index 36924b2..79871a5 100644 --- a/fabric/src/main/java/ziyue/tjmetro/mod/TianjinMetro.java +++ b/fabric/src/main/java/ziyue/tjmetro/mod/TianjinMetro.java @@ -5,13 +5,15 @@ import org.mtr.mapping.holder.ItemConvertible; import org.mtr.mapping.holder.ItemStack; import org.mtr.mapping.registry.CreativeModeTabHolder; +import ziyue.tjmetro.mapping.BooleanGameRule; +import ziyue.tjmetro.mapping.GameRuleRegistry; import ziyue.tjmetro.mod.packet.*; public final class TianjinMetro { public static final Logger LOGGER = LogManager.getLogger(Reference.NAME); - //public static final BooleanGameRule NO_FALLING_BLOCK = GameRuleRegistry.registerBoolean("preventBlockFalling", false); + public static final BooleanGameRule NO_FALLING_BLOCK = GameRuleRegistry.registerBoolean("preventBlockFalling", false); public static final CreativeModeTabHolder CREATIVE_MODE_TAB = Registry.createCreativeModeTabHolder("tjmetro_tab", () -> new ItemStack(new ItemConvertible(BlockList.LOGO.get().data))); public static void init() { diff --git a/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java b/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java index 83384fe..924371f 100644 --- a/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java +++ b/fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java @@ -70,7 +70,7 @@ static String mergeTranslation(String keyCJK, String key) { * @since 1.0.0-beta-1 */ static Pair splitTranslation(String text) { - final int separatorIndex = text.lastIndexOf("|"); + final int separatorIndex = text.indexOf("|"); return new Pair<>(text.substring(0, separatorIndex), text.substring(separatorIndex + 1)); } } diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index d6ea19f..c53d9c6 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -39,7 +39,8 @@ ] }, "mixins": [ - "tjmetro.mixins.json" + "tjmetro.mixins.json", + "tjmetro-fabric.mixins.json" ], "depends": { "fabric": "*", diff --git a/forge/build.gradle b/forge/build.gradle index 00e5ed7..7deae62 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -31,7 +31,9 @@ dependencies { mixin { add sourceSets.main, "tjmetro.refmap.json" + add sourceSets.main, "tjmetro-forge.refmap.json" config "tjmetro.mixins.json" + config "tjmetro-forge.mixins.json" debug.verbose = true debug.export = true } diff --git a/forge/src/main/java/ziyue/tjmetro/forge/mixin/ATGameRuleBooleanMixin.java b/forge/src/main/java/ziyue/tjmetro/forge/mixin/ATGameRuleBooleanMixin.java new file mode 100644 index 0000000..b77b138 --- /dev/null +++ b/forge/src/main/java/ziyue/tjmetro/forge/mixin/ATGameRuleBooleanMixin.java @@ -0,0 +1,47 @@ +package ziyue.tjmetro.forge.mixin; + +// Dumb accessTransformer can't do anything. + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import ziyue.tjmetro.forge.property.PropertyCreateBooleanGameRule; + +#if MC_VERSION <= "11605" +import net.minecraft.world.GameRules; +@Mixin(GameRules.BooleanValue.class) +public abstract class ATGameRuleBooleanMixin extends GameRules.RuleValue implements PropertyCreateBooleanGameRule +{ + @Shadow + private static GameRules.RuleType create(boolean p_223568_0_) { + return null; + } + + public ATGameRuleBooleanMixin(GameRules.RuleType p_i51530_1_) { + super(p_i51530_1_); + } + + @Override + public GameRules.RuleType createBoolean(boolean value) { + return create(value); + } +} +#else +import net.minecraft.world.level.GameRules; +@Mixin(GameRules.BooleanValue.class) +public abstract class ATGameRuleBooleanMixin extends GameRules.Value implements PropertyCreateBooleanGameRule +{ + @Shadow + static GameRules.Type create(boolean p_46251_) { + return null; + } + + public ATGameRuleBooleanMixin(GameRules.Type p_46362_) { + super(p_46362_); + } + + @Override + public GameRules.Type createBoolean(boolean value) { + return create(value); + } +} +#endif \ No newline at end of file diff --git a/forge/src/main/java/ziyue/tjmetro/forge/mixin/FallingBlockMixin.java b/forge/src/main/java/ziyue/tjmetro/forge/mixin/FallingBlockMixin.java new file mode 100644 index 0000000..d729521 --- /dev/null +++ b/forge/src/main/java/ziyue/tjmetro/forge/mixin/FallingBlockMixin.java @@ -0,0 +1,56 @@ +package ziyue.tjmetro.forge.mixin; + +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 ziyue.tjmetro.mod.TianjinMetro; + +#if MC_VERSION <= "11605" +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.FallingBlock; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.server.ServerWorld; +import java.util.Random; + +@Mixin(FallingBlock.class) +public abstract class FallingBlockMixin extends Block +{ + public FallingBlockMixin(Properties p_i48440_1_) { + super(p_i48440_1_); + } + + @Inject(at = @At("HEAD"), method = "tick", cancellable = true) + private void beforeTick(BlockState p_225534_1_, ServerWorld world, BlockPos p_225534_3_, Random p_225534_4_, CallbackInfo ci) { + if (world.getGameRules().getBoolean(TianjinMetro.NO_FALLING_BLOCK.data)) ci.cancel(); + } +} +#else +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Fallable; +import net.minecraft.world.level.block.FallingBlock; +import net.minecraft.world.level.block.state.BlockState; + +import java.util.Random; + +@Mixin(FallingBlock.class) +public abstract class FallingBlockMixin extends Block implements Fallable +{ + public FallingBlockMixin(Properties p_49795_) { + super(p_49795_); + } + + @Inject(at = @At("HEAD"), method = "tick", cancellable = true) +#if MC_VERSION >= "11802" + private void beforeTick(BlockState p_221124_, ServerLevel world, BlockPos p_221126_, net.minecraft.util.RandomSource p_221127_, CallbackInfo ci) +#else + private void beforeTick(BlockState p_53216_, ServerLevel world, BlockPos p_53218_, Random p_53219_, CallbackInfo ci) +#endif + { + if (world.getGameRules().getBoolean(TianjinMetro.NO_FALLING_BLOCK.data)) ci.cancel(); + } +} +#endif diff --git a/forge/src/main/java/ziyue/tjmetro/forge/property/PropertyCreateBooleanGameRule.java b/forge/src/main/java/ziyue/tjmetro/forge/property/PropertyCreateBooleanGameRule.java new file mode 100644 index 0000000..329b594 --- /dev/null +++ b/forge/src/main/java/ziyue/tjmetro/forge/property/PropertyCreateBooleanGameRule.java @@ -0,0 +1,17 @@ +package ziyue.tjmetro.forge.property; + +#if MC_VERSION <= "11605" +import net.minecraft.world.GameRules; + +public interface PropertyCreateBooleanGameRule +{ + GameRules.RuleType createBoolean(boolean value); +} +#else +import net.minecraft.world.level.GameRules; + +public interface PropertyCreateBooleanGameRule +{ + GameRules.Type createBoolean(boolean value); +} +#endif \ No newline at end of file diff --git a/forge/src/main/java/ziyue/tjmetro/mapping/BooleanGameRule.java b/forge/src/main/java/ziyue/tjmetro/mapping/BooleanGameRule.java new file mode 100644 index 0000000..d1c630e --- /dev/null +++ b/forge/src/main/java/ziyue/tjmetro/mapping/BooleanGameRule.java @@ -0,0 +1,32 @@ +package ziyue.tjmetro.mapping; + +import org.mtr.mapping.holder.ServerWorld; +import org.mtr.mapping.tool.HolderBase; + +#if MC_VERSION <= "11605" +import net.minecraft.world.GameRules; + +public class BooleanGameRule extends HolderBase> +{ + public BooleanGameRule(GameRules.RuleKey data) { + super(data); + } + + public static boolean getValue(ServerWorld world, BooleanGameRule rule) { + return world.data.getGameRules().getBoolean(rule.data); + } +} +#else +import net.minecraft.world.level.GameRules; + +public class BooleanGameRule extends HolderBase> +{ + public BooleanGameRule(GameRules.Key data) { + super(data); + } + + public static boolean getValue(ServerWorld world, BooleanGameRule rule) { + return world.data.getGameRules().getBoolean(rule.data); + } +} +#endif diff --git a/forge/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java b/forge/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java index e3aa32a..5ddcf55 100644 --- a/forge/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java +++ b/forge/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java @@ -1,6 +1,5 @@ package ziyue.tjmetro.mapping; -import net.minecraft.world.item.CreativeModeTab; import org.mtr.mapping.holder.ItemStack; import org.mtr.mapping.holder.MutableText; import org.mtr.mapping.holder.PressAction; @@ -29,15 +28,15 @@ static void setReservedButton(CreativeModeTabHolder creativeModeTab, MutableText } #else static Filter registerFilter(CreativeModeTabHolder creativeModeTab, MutableText filterName, Supplier filterIcon) { - return ziyue.filters.FilterBuilder.registerFilter((CreativeModeTab) null, filterName.data, () -> filterIcon.get().data); + return ziyue.filters.FilterBuilder.registerFilter((net.minecraft.world.item.CreativeModeTab) null, filterName.data, () -> filterIcon.get().data); } static Filter registerUncategorizedItemsFilter(CreativeModeTabHolder creativeModeTab) { - return ziyue.filters.FilterBuilder.registerUncategorizedItemsFilter((CreativeModeTab) null); + return ziyue.filters.FilterBuilder.registerUncategorizedItemsFilter((net.minecraft.world.item.CreativeModeTab) null); } static void filtersVisibility(CreativeModeTabHolder creativeModeTab, boolean visible) { - ziyue.filters.FilterBuilder.filtersVisibility((CreativeModeTab) null, visible); + ziyue.filters.FilterBuilder.filtersVisibility((net.minecraft.world.item.CreativeModeTab) null, visible); } static void setReservedButton(CreativeModeTabHolder creativeModeTab, MutableText tooltip, PressAction onPress) { diff --git a/forge/src/main/java/ziyue/tjmetro/mapping/GameRuleRegistry.java b/forge/src/main/java/ziyue/tjmetro/mapping/GameRuleRegistry.java new file mode 100644 index 0000000..9bb5ce6 --- /dev/null +++ b/forge/src/main/java/ziyue/tjmetro/mapping/GameRuleRegistry.java @@ -0,0 +1,16 @@ +package ziyue.tjmetro.mapping; + +#if MC_VERSION <= "11605" + import net.minecraft.world.GameRules; +#else +import net.minecraft.world.level.GameRules; +#endif +import net.minecraft.world.GameRules; +import ziyue.tjmetro.forge.property.PropertyCreateBooleanGameRule; + +public class GameRuleRegistry +{ + public static BooleanGameRule registerBoolean(String name, boolean defaultValue) { + return new BooleanGameRule(GameRules.register(name, GameRules.Category.MISC, ((PropertyCreateBooleanGameRule)new GameRules.BooleanValue(null, false)).createBoolean(defaultValue))); + } +} diff --git a/forge/src/main/resources/tjmetro-forge.mixins.json b/forge/src/main/resources/tjmetro-forge.mixins.json new file mode 100644 index 0000000..a48f885 --- /dev/null +++ b/forge/src/main/resources/tjmetro-forge.mixins.json @@ -0,0 +1,16 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "ziyue.tjmetro.forge.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "ATGameRuleBooleanMixin", + "FallingBlockMixin" + ], + "injectors": { + "defaultRequire": 1 + }, + "client": [ + ], + "refmap": "tjmetro-forge.refmap.json" +} diff --git a/gradle.properties b/gradle.properties index 9f11618..fc531cf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G org.gradle.parallel=true org.gradle.daemon=false -minecraft_version=1.20.4 +minecraft_version=1.16.5 # Mod Properties mod_version=1.0.0-beta-1