From ba8769be52f890cc3d0a8c262bb8456710579973 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:51:27 -0500 Subject: [PATCH] Add the spread container loot rule type --- .../plasmid/api/game/rule/GameRuleType.java | 1 + .../mixin/game/rule/LootTableMixin.java | 34 +++++++++++++++++++ src/main/resources/plasmid.mixins.json | 1 + 3 files changed, 36 insertions(+) create mode 100644 src/main/java/xyz/nucleoid/plasmid/mixin/game/rule/LootTableMixin.java diff --git a/src/main/java/xyz/nucleoid/plasmid/api/game/rule/GameRuleType.java b/src/main/java/xyz/nucleoid/plasmid/api/game/rule/GameRuleType.java index aae7ef6e..cfb1a91c 100644 --- a/src/main/java/xyz/nucleoid/plasmid/api/game/rule/GameRuleType.java +++ b/src/main/java/xyz/nucleoid/plasmid/api/game/rule/GameRuleType.java @@ -114,6 +114,7 @@ public final class GameRuleType { public static final GameRuleType STOP_SPECTATING_ENTITY = GameRuleType.create(); public static final GameRuleType PLAYER_PROJECTILE_KNOCKBACK = GameRuleType.create(); public static final GameRuleType TRIDENTS_LOYAL_IN_VOID = GameRuleType.create(); + public static final GameRuleType SPREAD_CONTAINER_LOOT = GameRuleType.create(); public static final GameRuleType MODIFY_INVENTORY = GameRuleType.create(); public static final GameRuleType MODIFY_ARMOR = GameRuleType.create(); public static final GameRuleType SWAP_OFFHAND = GameRuleType.create().enforces(PlayerSwapWithOffhandEvent.EVENT, result -> (player) -> result); diff --git a/src/main/java/xyz/nucleoid/plasmid/mixin/game/rule/LootTableMixin.java b/src/main/java/xyz/nucleoid/plasmid/mixin/game/rule/LootTableMixin.java new file mode 100644 index 00000000..ec008cf1 --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/mixin/game/rule/LootTableMixin.java @@ -0,0 +1,34 @@ +package xyz.nucleoid.plasmid.mixin.game.rule; + +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; +import com.llamalad7.mixinextras.sugar.Local; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import net.minecraft.item.ItemStack; +import net.minecraft.loot.LootTable; +import net.minecraft.loot.context.LootContext; +import net.minecraft.loot.context.LootContextParameters; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.random.Random; +import xyz.nucleoid.plasmid.api.game.rule.GameRuleType; +import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl; +import xyz.nucleoid.stimuli.event.EventResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(LootTable.class) +public class LootTableMixin { + @WrapWithCondition( + method = "supplyInventory", + at = @At(value = "INVOKE", target = "Lnet/minecraft/loot/LootTable;shuffle(Lit/unimi/dsi/fastutil/objects/ObjectArrayList;ILnet/minecraft/util/math/random/Random;)V") + ) + public boolean preventContainerLootShuffling(LootTable lootTable, ObjectArrayList stacks, int freeSlots, Random random, @Local LootContext context) { + var entity = context.get(LootContextParameters.THIS_ENTITY); + var gameSpace = entity instanceof ServerPlayerEntity player ? GameSpaceManagerImpl.get().byPlayer(player) : GameSpaceManagerImpl.get().byWorld(context.getWorld()); + + if (gameSpace != null && gameSpace.getBehavior().testRule(GameRuleType.SPREAD_CONTAINER_LOOT) == EventResult.DENY) { + return false; + } + + return true; + } +} diff --git a/src/main/resources/plasmid.mixins.json b/src/main/resources/plasmid.mixins.json index 30ec7345..abb31506 100644 --- a/src/main/resources/plasmid.mixins.json +++ b/src/main/resources/plasmid.mixins.json @@ -12,6 +12,7 @@ "game.portal.SignBlockEntityMixin", "game.rule.EquippableComponentMixin", "game.rule.HungerManagerMixin", + "game.rule.LootTableMixin", "game.rule.PlayerEntityMixin", "game.rule.ServerPlayerEntityMixin", "game.rule.ServerPlayNetworkHandlerMixin",