From 744dfe8bc20afd3667ee2ffc5464b8202cc40f17 Mon Sep 17 00:00:00 2001 From: TheEpicBlock Date: Sun, 17 Nov 2024 00:47:38 +0100 Subject: [PATCH] Use gamerule for entity mutability instead --- .../java/net/modfest/fireblanket/Fireblanket.java | 14 ++++++++++---- .../net/modfest/fireblanket/FireblanketMixin.java | 4 ---- .../modfest/fireblanket/config/ConfigSpecs.java | 6 ------ .../entity_immutability/MixinArmorStand.java | 15 +++++++++------ .../mixin/entity_immutability/MixinItemFrame.java | 7 +++++-- .../resources/assets/fireblanket/lang/en_us.json | 5 +++-- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/modfest/fireblanket/Fireblanket.java b/src/main/java/net/modfest/fireblanket/Fireblanket.java index 221d639..f22b4a2 100644 --- a/src/main/java/net/modfest/fireblanket/Fireblanket.java +++ b/src/main/java/net/modfest/fireblanket/Fireblanket.java @@ -7,8 +7,9 @@ import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; -import net.fabricmc.fabric.api.event.player.UseBlockCallback; import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; +import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; +import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents; @@ -28,6 +29,7 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.GameRules; import net.modfest.fireblanket.command.CmdFindReplaceCommand; import net.modfest.fireblanket.command.DumpCommand; import net.modfest.fireblanket.command.RegionCommand; @@ -49,9 +51,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; @@ -61,6 +60,13 @@ public class Fireblanket implements ModInitializer { public static final Identifier FULL_STREAM_COMPRESSION = Identifier.of("fireblanket", "full_stream_compression"); public static final Identifier REGIONS_UPDATE = Identifier.of("fireblanket", "regions_update"); + /** + * Whether new entities will be fixed. + * @see net.modfest.fireblanket.mixin.entity_immutability + */ + public static final GameRules.Key NEW_ENTITIES_IMMUTABLE = + GameRuleRegistry.register("newEntitiesImmutable", GameRules.Category.MOBS, GameRuleFactory.createBooleanRule(true)); + public static final Logger LOGGER = LoggerFactory.getLogger("Fireblanket"); public record QueuedPacket(ClientConnection conn, Packet packet, PacketCallbacks listener) { diff --git a/src/main/java/net/modfest/fireblanket/FireblanketMixin.java b/src/main/java/net/modfest/fireblanket/FireblanketMixin.java index 171d601..fe3ab53 100644 --- a/src/main/java/net/modfest/fireblanket/FireblanketMixin.java +++ b/src/main/java/net/modfest/fireblanket/FireblanketMixin.java @@ -56,10 +56,6 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { return FireblanketConfig.get(ConfigSpecs.FLATTEN_CHUNK_PALETTES); } - if (mixinClassName.contains("entity_immutability")) { - return FireblanketConfig.get(ConfigSpecs.ENTITY_IMMUTABILITY); - } - if (mixinClassName.contains("ai") || mixinClassName.contains("sounds")) { return FireblanketConfig.get(ConfigSpecs.GAMEPLAY_CHANGES); } diff --git a/src/main/java/net/modfest/fireblanket/config/ConfigSpecs.java b/src/main/java/net/modfest/fireblanket/config/ConfigSpecs.java index 20fe95b..4968e05 100644 --- a/src/main/java/net/modfest/fireblanket/config/ConfigSpecs.java +++ b/src/main/java/net/modfest/fireblanket/config/ConfigSpecs.java @@ -44,11 +44,6 @@ public final class ConfigSpecs { Disables the usage of zstd to save world data and compress network connections. """, "no", ConfigParsers.BOOLEAN); - public static final ConfigSpec ENTITY_IMMUTABILITY = new ConfigSpec<>("entity-immutability", "Entity Immutability by Default", - """ - Makes entities default to being immutable. For example, it will set frames to fixed by default. - """, "no", ConfigParsers.BOOLEAN); - public static final ConfigSpec GAMEPLAY_CHANGES = new ConfigSpec<>("gameplay-changes", "Gameplay Changes", """ Allow altering features in a way that impacts gameplay. @@ -72,7 +67,6 @@ private static void buildMap() { ALL_SPECS.add(FLATTEN_CHUNK_PALETTES); ALL_SPECS.add(ALLOW_FOOTGUNS); ALL_SPECS.add(AVOID_ZSTD); - ALL_SPECS.add(ENTITY_IMMUTABILITY); ALL_SPECS.add(GAMEPLAY_CHANGES); // We have registries at home: diff --git a/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinArmorStand.java b/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinArmorStand.java index 8bdf8e0..46bdef6 100644 --- a/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinArmorStand.java +++ b/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinArmorStand.java @@ -3,6 +3,7 @@ import net.minecraft.entity.EntityType; import net.minecraft.entity.decoration.ArmorStandEntity; import net.minecraft.world.World; +import net.modfest.fireblanket.Fireblanket; import net.modfest.fireblanket.mixinsupport.ImmmovableLivingEntity; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -16,12 +17,14 @@ public class MixinArmorStand { private int disabledSlots; @Inject(method = "(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At("RETURN")) - private void onInit(EntityType entityType, World world, CallbackInfo ci) { - // Disable all slots by default - this.disabledSlots = 4144959; - // Disable movement (prevents abuse of fishing rods) - if (this instanceof ImmmovableLivingEntity im) { - im.setNoMovement(true); + private void onInit(EntityType entityType, World world, CallbackInfo ci) { + if (world.getGameRules().getBoolean(Fireblanket.NEW_ENTITIES_IMMUTABLE)) { + // Disable all slots by default + this.disabledSlots = 4144959; + // Disable movement (prevents abuse of fishing rods) + if (this instanceof ImmmovableLivingEntity im) { + im.setNoMovement(true); + } } } } diff --git a/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinItemFrame.java b/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinItemFrame.java index 082a446..cba2e90 100644 --- a/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinItemFrame.java +++ b/src/main/java/net/modfest/fireblanket/mixin/entity_immutability/MixinItemFrame.java @@ -3,6 +3,7 @@ import net.minecraft.entity.EntityType; import net.minecraft.entity.decoration.ItemFrameEntity; import net.minecraft.world.World; +import net.modfest.fireblanket.Fireblanket; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -15,7 +16,9 @@ public class MixinItemFrame { private boolean fixed; @Inject(method = "(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At("RETURN")) - private void onInit(EntityType entityType, World world, CallbackInfo ci) { - this.fixed = true; + private void onInit(EntityType entityType, World world, CallbackInfo ci) { + if (world.getGameRules().getBoolean(Fireblanket.NEW_ENTITIES_IMMUTABLE)) { + this.fixed = true; + } } } diff --git a/src/main/resources/assets/fireblanket/lang/en_us.json b/src/main/resources/assets/fireblanket/lang/en_us.json index 861f722..b15e4a4 100644 --- a/src/main/resources/assets/fireblanket/lang/en_us.json +++ b/src/main/resources/assets/fireblanket/lang/en_us.json @@ -1,4 +1,5 @@ { "argument.entity.selector.limit.unforced": "Targeting too many entities with no distance filter; add `force=true` to the selector to bypass", - "argument.entity.options.force.description": "Force-allow more than 50 entities targeted with no distance filter" -} \ No newline at end of file + "argument.entity.options.force.description": "Force-allow more than 50 entities targeted with no distance filter", + "gamerule.newEntitiesImmutable": "Whether newly spawned entities should be immutable. (Item frames will be fixed for example" +}