Skip to content

Commit

Permalink
Prevent block falling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiYueCommentary committed Aug 4, 2024
1 parent d2c28bf commit eff1565
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 42 deletions.
2 changes: 0 additions & 2 deletions fabric/src/main/java/ziyue/tjmetro/fabric/MainFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 3 additions & 1 deletion fabric/src/main/java/ziyue/tjmetro/mod/TianjinMetro.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static String mergeTranslation(String keyCJK, String key) {
* @since 1.0.0-beta-1
*/
static Pair<String, String> splitTranslation(String text) {
final int separatorIndex = text.lastIndexOf("|");
final int separatorIndex = text.indexOf("|");
return new Pair<>(text.substring(0, separatorIndex), text.substring(separatorIndex + 1));
}
}
3 changes: 2 additions & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
]
},
"mixins": [
"tjmetro.mixins.json"
"tjmetro.mixins.json",
"tjmetro-fabric.mixins.json"
],
"depends": {
"fabric": "*",
Expand Down
2 changes: 2 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GameRules.BooleanValue> implements PropertyCreateBooleanGameRule
{
@Shadow
private static GameRules.RuleType<GameRules.BooleanValue> create(boolean p_223568_0_) {
return null;
}

public ATGameRuleBooleanMixin(GameRules.RuleType<GameRules.BooleanValue> p_i51530_1_) {
super(p_i51530_1_);
}

@Override
public GameRules.RuleType<GameRules.BooleanValue> createBoolean(boolean value) {
return create(value);
}
}
#else
import net.minecraft.world.level.GameRules;
@Mixin(GameRules.BooleanValue.class)
public abstract class ATGameRuleBooleanMixin extends GameRules.Value<GameRules.BooleanValue> implements PropertyCreateBooleanGameRule
{
@Shadow
static GameRules.Type<GameRules.BooleanValue> create(boolean p_46251_) {
return null;
}

public ATGameRuleBooleanMixin(GameRules.Type<GameRules.BooleanValue> p_46362_) {
super(p_46362_);
}

@Override
public GameRules.Type<GameRules.BooleanValue> createBoolean(boolean value) {
return create(value);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ziyue.tjmetro.forge.property;

#if MC_VERSION <= "11605"
import net.minecraft.world.GameRules;

public interface PropertyCreateBooleanGameRule
{
GameRules.RuleType<GameRules.BooleanValue> createBoolean(boolean value);
}
#else
import net.minecraft.world.level.GameRules;

public interface PropertyCreateBooleanGameRule
{
GameRules.Type<GameRules.BooleanValue> createBoolean(boolean value);
}
#endif
32 changes: 32 additions & 0 deletions forge/src/main/java/ziyue/tjmetro/mapping/BooleanGameRule.java
Original file line number Diff line number Diff line change
@@ -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<GameRules.RuleKey<GameRules.BooleanValue>>
{
public BooleanGameRule(GameRules.RuleKey<GameRules.BooleanValue> 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<GameRules.Key<GameRules.BooleanValue>>
{
public BooleanGameRule(GameRules.Key<GameRules.BooleanValue> data) {
super(data);
}

public static boolean getValue(ServerWorld world, BooleanGameRule rule) {
return world.data.getGameRules().getBoolean(rule.data);
}
}
#endif
7 changes: 3 additions & 4 deletions forge/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,15 +28,15 @@ static void setReservedButton(CreativeModeTabHolder creativeModeTab, MutableText
}
#else
static Filter registerFilter(CreativeModeTabHolder creativeModeTab, MutableText filterName, Supplier<ItemStack> 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) {
Expand Down
16 changes: 16 additions & 0 deletions forge/src/main/java/ziyue/tjmetro/mapping/GameRuleRegistry.java
Original file line number Diff line number Diff line change
@@ -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)));
}
}
16 changes: 16 additions & 0 deletions forge/src/main/resources/tjmetro-forge.mixins.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eff1565

Please sign in to comment.