diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 02e7892..df4c511 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: matrix: # Use these Java versions java: [ - 17 # Minimum supported by Minecraft + 21 # Minimum supported by Minecraft ] # and run on both Linux and Windows os: [ubuntu-20.04, windows-latest] diff --git a/README.md b/README.md index 27fca44..bc4aefd 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ if (Permissions.check(player, "locki.access.inventory.armor.head", true)) { // nothing is locking the head slot } ``` -For a list of default inventory nodes, see [DefaultInventoryNodes.java](src/main/java/io/github/ladysnake/locki/DefaultInventoryNodes.java). +For a list of default inventory nodes, see [DefaultInventoryNodes.java](src/main/java/org/ladysnake/locki/DefaultInventoryNodes.java). Simply prepend the desired node's full path with `locki.access.` before checking. ## Adding Locki to your project @@ -69,8 +69,8 @@ dependencies { include "org.ladysnake:locki:${locki_version}" // locki dependencies include "me.lucko:fabric-permissions-api:${fpa_version}" - include "dev.onyxstudios.cardinal-components-api:cardinal-components-base:${cca_version}" - include "dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${cca_version}" + include "org.ladysnake.cardinal-components-api:cardinal-components-base:${cca_version}" + include "org.ladysnake.cardinal-components-api:cardinal-components-entity:${cca_version}" } ``` diff --git a/build.gradle b/build.gradle index 02f8e82..949c883 100644 --- a/build.gradle +++ b/build.gradle @@ -20,14 +20,14 @@ chenille { } license = 'LGPL' - javaVersion = 17 + javaVersion = 21 } repositories { mavenCentral() mavenLocal() chenille.repositories { - cursemaven() + modrinth() ladysnake() lucko() jitpack() @@ -54,13 +54,13 @@ dependencies { modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}" - modIncludeImplementation ("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${cca_version}") { transitive = false } - modIncludeImplementation ("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${cca_version}") { transitive = false } + modIncludeImplementation ("org.ladysnake.cardinal-components-api:cardinal-components-base:${cca_version}") { transitive = false } + modIncludeImplementation ("org.ladysnake.cardinal-components-api:cardinal-components-entity:${cca_version}") { transitive = false } modIncludeImplementation "me.lucko:fabric-permissions-api:${fpa_version}" compileOnly "com.demonwav.mcdev:annotations:1.0" - modCompileOnly "curse.maven:backslot-402739:${backslot_version}" // not updated yet + modCompileOnly "maven.modrinth:backslot:${backslot_version}" // not updated yet modLocalRuntime ("me.shedaniel.cloth:cloth-config-fabric:${cloth_config_version}") { transitive = false } modLocalRuntime ("com.terraformersmc:modmenu:${modmenu_version}") { transitive = false } diff --git a/changelog.md b/changelog.md index 377e90d..c7968b4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,8 @@ +------------------------------------------------------ +Version 0.13.0 +------------------------------------------------------ +Updated to 1.21 + ------------------------------------------------------ Version 0.12.0 ------------------------------------------------------ diff --git a/gradle.properties b/gradle.properties index 4c2b8ef..8d9f9ff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,24 +3,24 @@ org.gradle.jvmargs=-Xmx3G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version = 1.20.4 - quilt_mappings = 3 + minecraft_version = 1.21 + quilt_mappings = 1 loader_version=0.26.0 # Mod Properties - mod_version = 0.12.0 + mod_version = 0.13.0 maven_group = org.ladysnake archives_base_name = locki # Dependencies - fabric_api_version = 0.97.1+1.20.4 - cca_version = 5.4.0 + fabric_api_version = 0.100.1+1.21 + cca_version = 6.1.0 fpa_version=0.2-SNAPSHOT # V1.2.12 - backslot_version=4178145 - cloth_config_version=11.1.118 - modmenu_version=9.2.0-beta.2 - elmendorf_version=0.12.0 + backslot_version=1.2.15+1.20.6 + cloth_config_version=15.0.127 + modmenu_version=11.0.0-rc.2 + elmendorf_version=0.13.0 # Publishing owners = Ladysnake display_name = Locki @@ -28,7 +28,7 @@ org.gradle.jvmargs=-Xmx3G gpl_version = 3 curseforge_id = 483059 modrinth_id = qS8U15sj - curseforge_versions = 1.20.4 + curseforge_versions = 1.21 cf_requirements = qsl cf_embeddeds = cardinal-components-api release_type = beta diff --git a/src/main/java/org/ladysnake/locki/DefaultInventoryNodes.java b/src/main/java/org/ladysnake/locki/DefaultInventoryNodes.java index d1e7443..fdddf78 100644 --- a/src/main/java/org/ladysnake/locki/DefaultInventoryNodes.java +++ b/src/main/java/org/ladysnake/locki/DefaultInventoryNodes.java @@ -62,6 +62,7 @@ public static InventoryNode get(EquipmentSlot equipmentSlot) { case LEGS -> LEGS; case CHEST -> CHEST; case HEAD -> HEAD; + case BODY -> MAIN_INVENTORY; }; } diff --git a/src/main/java/org/ladysnake/locki/Locki.java b/src/main/java/org/ladysnake/locki/Locki.java index 7bb6e71..eb307e7 100644 --- a/src/main/java/org/ladysnake/locki/Locki.java +++ b/src/main/java/org/ladysnake/locki/Locki.java @@ -19,13 +19,11 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.mojang.brigadier.arguments.StringArgumentType; import me.lucko.fabric.api.permissions.v0.PermissionCheckEvent; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.util.TriState; -import net.minecraft.command.argument.IdentifierArgumentType; import net.minecraft.command.argument.SingletonArgumentInfo; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; @@ -52,9 +50,14 @@ * @see #registerNode(InventoryNode, String) */ public final class Locki implements ModInitializer { + public static final String MOD_ID = "locki"; public static final Logger LOGGER = LogManager.getLogger("Locki"); public static final Pattern NODE_NAME_PART = Pattern.compile("[a-z0-9_-]+"); + public static Identifier id(String path) { + return Identifier.of(MOD_ID, path); + } + private static final Map locks = new HashMap<>(); private static final Map nodes = new HashMap<>(); private static int nextId; @@ -154,8 +157,8 @@ public static Stream streamNodeNames() { @Override public void onInitialize() { this.baseInit(); - ArgumentTypeRegistry.registerArgumentType(new Identifier("locki", "inventory_lock"), InventoryLockArgumentType.class, SingletonArgumentInfo.contextFree(InventoryLockArgumentType::inventoryLock)); - ArgumentTypeRegistry.registerArgumentType(new Identifier("locki", "inventory_node"), InventoryNodeArgumentType.class, SingletonArgumentInfo.contextFree(InventoryNodeArgumentType::inventoryNode)); + ArgumentTypeRegistry.registerArgumentType(id("inventory_lock"), InventoryLockArgumentType.class, SingletonArgumentInfo.contextFree(InventoryLockArgumentType::inventoryLock)); + ArgumentTypeRegistry.registerArgumentType(id("inventory_node"), InventoryNodeArgumentType.class, SingletonArgumentInfo.contextFree(InventoryNodeArgumentType::inventoryNode)); } void baseInit() { diff --git a/src/main/java/org/ladysnake/locki/impl/InventoryKeeperBase.java b/src/main/java/org/ladysnake/locki/impl/InventoryKeeperBase.java index c2b496b..dee319f 100644 --- a/src/main/java/org/ladysnake/locki/impl/InventoryKeeperBase.java +++ b/src/main/java/org/ladysnake/locki/impl/InventoryKeeperBase.java @@ -17,14 +17,15 @@ */ package org.ladysnake.locki.impl; -import dev.onyxstudios.cca.api.v3.component.Component; import it.unimi.dsi.fastutil.objects.Reference2BooleanMap; import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtList; +import net.minecraft.registry.HolderLookup; import net.minecraft.util.Identifier; +import org.ladysnake.cca.api.v3.component.Component; import org.ladysnake.locki.InventoryKeeper; import org.ladysnake.locki.InventoryLock; import org.ladysnake.locki.InventoryNode; @@ -142,7 +143,7 @@ protected BitSet lookup(InventoryNode inventoryNode) { } @Override - public void readFromNbt(NbtCompound tag) { + public void readFromNbt(NbtCompound tag, HolderLookup.Provider registryLookup) { if (tag.contains("locks", NbtElement.COMPOUND_TYPE)) { this.clearCache(); this.getLocks().clear(); @@ -169,7 +170,7 @@ public void readFromNbt(NbtCompound tag) { } @Override - public void writeToNbt(NbtCompound tag) { + public void writeToNbt(NbtCompound tag, HolderLookup.Provider registryLookup) { NbtCompound dict = new NbtCompound(); for (Map.Entry> nodeEntry : this.getLocks().entrySet()) { NbtList list = new NbtList(); diff --git a/src/main/java/org/ladysnake/locki/impl/LockiClient.java b/src/main/java/org/ladysnake/locki/impl/LockiClient.java index 4f0f2e4..8d15c36 100644 --- a/src/main/java/org/ladysnake/locki/impl/LockiClient.java +++ b/src/main/java/org/ladysnake/locki/impl/LockiClient.java @@ -27,10 +27,10 @@ import org.ladysnake.locki.DefaultInventoryNodes; import org.ladysnake.locki.InventoryLockingChangeCallback; import org.ladysnake.locki.InventoryNode; -import org.quiltmc.loader.api.ModContainer; +import org.ladysnake.locki.Locki; public class LockiClient implements ClientModInitializer { - public static final Identifier LOCKED_SLOT_SPRITE = new Identifier("locki", "gui/locked_slot"); + public static final Identifier LOCKED_SLOT_SPRITE = Locki.id("gui/locked_slot"); private static void updateCraftingBookVisibility(PlayerEntity player, InventoryNode invNode, boolean locked) { MinecraftClient mc = MinecraftClient.getInstance(); diff --git a/src/main/java/org/ladysnake/locki/impl/LockiCommand.java b/src/main/java/org/ladysnake/locki/impl/LockiCommand.java index b848bb2..4afb0f6 100644 --- a/src/main/java/org/ladysnake/locki/impl/LockiCommand.java +++ b/src/main/java/org/ladysnake/locki/impl/LockiCommand.java @@ -22,7 +22,6 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import org.ladysnake.locki.InventoryLock; import org.ladysnake.locki.InventoryNode; import org.ladysnake.locki.Locki; @@ -38,7 +37,7 @@ import static org.ladysnake.locki.impl.InventoryLockArgumentType.inventoryLock; public final class LockiCommand { - public static final InventoryLock COMMAND_LOCK = Locki.registerLock(new Identifier("locki", "commands"), true); + public static final InventoryLock COMMAND_LOCK = Locki.registerLock(Locki.id("commands"), true); public static void register(CommandDispatcher dispatcher) { dispatcher.register(literal("locki") diff --git a/src/main/java/org/ladysnake/locki/impl/LockiComponents.java b/src/main/java/org/ladysnake/locki/impl/LockiComponents.java index 06db44d..1f7d63c 100644 --- a/src/main/java/org/ladysnake/locki/impl/LockiComponents.java +++ b/src/main/java/org/ladysnake/locki/impl/LockiComponents.java @@ -17,15 +17,15 @@ */ package org.ladysnake.locki.impl; -import dev.onyxstudios.cca.api.v3.component.ComponentKey; -import dev.onyxstudios.cca.api.v3.component.ComponentRegistry; -import dev.onyxstudios.cca.api.v3.entity.EntityComponentFactoryRegistry; -import dev.onyxstudios.cca.api.v3.entity.EntityComponentInitializer; -import dev.onyxstudios.cca.api.v3.entity.RespawnCopyStrategy; -import net.minecraft.util.Identifier; +import org.ladysnake.cca.api.v3.component.ComponentKey; +import org.ladysnake.cca.api.v3.component.ComponentRegistry; +import org.ladysnake.cca.api.v3.entity.EntityComponentFactoryRegistry; +import org.ladysnake.cca.api.v3.entity.EntityComponentInitializer; +import org.ladysnake.cca.api.v3.entity.RespawnCopyStrategy; +import org.ladysnake.locki.Locki; public final class LockiComponents implements EntityComponentInitializer { - public static final ComponentKey INVENTORY_KEEPER = ComponentRegistry.getOrCreate(new Identifier("locki", "inventory_keeper"), InventoryKeeperBase.class); + public static final ComponentKey INVENTORY_KEEPER = ComponentRegistry.getOrCreate(Locki.id("inventory_keeper"), InventoryKeeperBase.class); @Override public void registerEntityComponentFactories(EntityComponentFactoryRegistry registry) { diff --git a/src/main/java/org/ladysnake/locki/impl/PlayerInventoryKeeper.java b/src/main/java/org/ladysnake/locki/impl/PlayerInventoryKeeper.java index 2a93c92..e2f8736 100644 --- a/src/main/java/org/ladysnake/locki/impl/PlayerInventoryKeeper.java +++ b/src/main/java/org/ladysnake/locki/impl/PlayerInventoryKeeper.java @@ -19,12 +19,12 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; -import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent; import it.unimi.dsi.fastutil.objects.Reference2BooleanMap; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.server.network.ServerPlayerEntity; +import org.ladysnake.cca.api.v3.component.sync.AutoSyncedComponent; import org.ladysnake.locki.DefaultInventoryNodes; import org.ladysnake.locki.InventoryKeeper; import org.ladysnake.locki.InventoryLock; @@ -74,7 +74,7 @@ public boolean shouldSyncWith(ServerPlayerEntity player) { } @Override - public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) { + public void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) { buf.writeVarInt(this.cache.size()); for (Map.Entry entry : this.getCache().entrySet()) { // Could compress this by not repeating ancestors? @@ -84,7 +84,7 @@ public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) { } @Override - public void applySyncPacket(PacketByteBuf buf) { + public void applySyncPacket(RegistryByteBuf buf) { int size = buf.readVarInt(); this.clearCache(); for (int i = 0; i < size; i++) { diff --git a/src/main/java/org/ladysnake/locki/impl/mixin/InputSlotFillerMixin.java b/src/main/java/org/ladysnake/locki/impl/mixin/InputSlotFillerMixin.java index 243dceb..ee37af7 100644 --- a/src/main/java/org/ladysnake/locki/impl/mixin/InputSlotFillerMixin.java +++ b/src/main/java/org/ladysnake/locki/impl/mixin/InputSlotFillerMixin.java @@ -33,7 +33,7 @@ @Mixin(InputSlotFiller.class) public abstract class InputSlotFillerMixin { - @Shadow protected AbstractRecipeScreenHandler handler; + @Shadow protected AbstractRecipeScreenHandler handler; @WrapOperation(method = "fillInputSlots(Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/recipe/RecipeHolder;Z)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/RecipeMatcher;match(Lnet/minecraft/recipe/Recipe;Lit/unimi/dsi/fastutil/ints/IntList;)Z")) private boolean blockInventoryCrafting(RecipeMatcher instance, Recipe recipe, IntList output, Operation original, ServerPlayerEntity entity) { diff --git a/src/main/java/org/ladysnake/locki/impl/mixin/client/InGameHudMixin.java b/src/main/java/org/ladysnake/locki/impl/mixin/client/InGameHudMixin.java index 13a549f..c9b979a 100644 --- a/src/main/java/org/ladysnake/locki/impl/mixin/client/InGameHudMixin.java +++ b/src/main/java/org/ladysnake/locki/impl/mixin/client/InGameHudMixin.java @@ -19,6 +19,7 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.class_9779; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.hud.in_game.InGameHud; import net.minecraft.entity.player.PlayerEntity; @@ -54,12 +55,12 @@ public abstract class InGameHudMixin { protected abstract PlayerEntity getCameraPlayer(); @Inject( - method = "renderHotbar", + method = "renderSurvivalHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;push()V"), allow = 1, cancellable = true ) - private void checkInventoryLimit(float tickDelta, GuiGraphics graphics, CallbackInfo ci) { + private void checkInventoryLimit(GuiGraphics graphics, class_9779 arg, CallbackInfo ci) { this.renderMainHandOnly = false; InventoryKeeper inventoryKeeper = InventoryKeeper.get(this.getCameraPlayer()); if (inventoryKeeper.isLocked(DefaultInventoryNodes.MAIN_HAND)) { @@ -69,7 +70,7 @@ private void checkInventoryLimit(float tickDelta, GuiGraphics graphics, Callback } } - @ModifyVariable(method = "renderHotbar", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/entity/player/PlayerEntity;getOffHandStack()Lnet/minecraft/item/ItemStack;")) + @ModifyVariable(method = "renderSurvivalHotbar", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/entity/player/PlayerEntity;getOffHandStack()Lnet/minecraft/item/ItemStack;")) private ItemStack lockOffHand(ItemStack base) { if (InventoryKeeper.get(this.getCameraPlayer()).isLocked(DefaultInventoryNodes.OFF_HAND)) { return ItemStack.EMPTY; @@ -78,7 +79,7 @@ private ItemStack lockOffHand(ItemStack base) { } @WrapOperation( - method = "renderHotbar", + method = "renderSurvivalHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;drawGuiTexture(Lnet/minecraft/util/Identifier;IIII)V", ordinal = 0) ) private void centerCroppedHotbar(GuiGraphics instance, Identifier texture, int x, int y, int width, int height, Operation original) { @@ -93,7 +94,7 @@ private void centerCroppedHotbar(GuiGraphics instance, Identifier texture, int x } @ModifyArg( - method = "renderHotbar", + method = "renderSurvivalHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;drawGuiTexture(Lnet/minecraft/util/Identifier;IIII)V", ordinal = 1), index = 1 ) @@ -105,7 +106,7 @@ private int centerSelectedSlot(int x) { } @ModifyArg( - method = "renderHotbar", + method = "renderSurvivalHotbar", slice = @Slice( from = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;pop()V"), to = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 1) @@ -118,12 +119,12 @@ private int cancelLockedItemRender(int index) { } @ModifyArg( - method = "renderHotbar", + method = "renderSurvivalHotbar", slice = @Slice( from = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;pop()V"), to = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 1) ), - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/in_game/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/GuiGraphics;IIFLnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V") + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/in_game/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/GuiGraphics;IILnet/minecraft/class_9779;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V") ) private ItemStack cancelLockedItemRender(ItemStack stack) { if (this.cancelNextItem) { @@ -133,12 +134,12 @@ private ItemStack cancelLockedItemRender(ItemStack stack) { } @ModifyArg( - method = "renderHotbar", + method = "renderSurvivalHotbar", slice = @Slice( from = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;pop()V"), to = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 1) ), - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/in_game/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/GuiGraphics;IIFLnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V"), + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/in_game/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/GuiGraphics;IILnet/minecraft/class_9779;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V"), index = 1 ) private int shiftMainHandItem(int x) { diff --git a/src/testmod/java/org/ladysnake/lockii/InventoryLockComponent.java b/src/testmod/java/org/ladysnake/lockii/InventoryLockComponent.java new file mode 100644 index 0000000..b906f39 --- /dev/null +++ b/src/testmod/java/org/ladysnake/lockii/InventoryLockComponent.java @@ -0,0 +1,26 @@ +package org.ladysnake.lockii; + +import com.mojang.serialization.Codec; +import io.netty.buffer.ByteBuf; +import net.minecraft.component.DataComponentType; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import org.jetbrains.annotations.NotNull; +import org.ladysnake.locki.InventoryNode; +import org.ladysnake.locki.Locki; + +public record InventoryLockComponent(int debugMode) { + public static final Codec CODEC = Codec.INT.xmap(InventoryLockComponent::new, InventoryLockComponent::debugMode); + public static final PacketCodec PACKET_CODEC = PacketCodecs.VAR_INT.map(InventoryLockComponent::new, InventoryLockComponent::debugMode); + public static final DataComponentType TYPE = new DataComponentType.Builder().codec(CODEC).packetCodec(PACKET_CODEC).build(); + private static final InventoryNode[] ALL_DEFAULT_NODES = Locki.streamNodeNames().map(Locki::getNode).toArray(InventoryNode[]::new); + public static final InventoryLockComponent DEFAULT = new InventoryLockComponent(0); + + public @NotNull InventoryLockComponent cycle() { + return new InventoryLockComponent((debugMode + 1) % ALL_DEFAULT_NODES.length); + } + + public InventoryNode node() { + return ALL_DEFAULT_NODES[debugMode]; + } +} diff --git a/src/testmod/java/org/ladysnake/lockii/InventoryLockItem.java b/src/testmod/java/org/ladysnake/lockii/InventoryLockItem.java index 1f40563..31b9d83 100644 --- a/src/testmod/java/org/ladysnake/lockii/InventoryLockItem.java +++ b/src/testmod/java/org/ladysnake/lockii/InventoryLockItem.java @@ -20,18 +20,15 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.world.World; import org.ladysnake.locki.InventoryLock; -import org.ladysnake.locki.InventoryNode; import org.ladysnake.locki.Locki; public class InventoryLockItem extends Item { public static final InventoryLock LOCK = Locki.registerLock(Lockii.id("test_item")); - private static final InventoryNode[] ALL_DEFAULT_NODES = Locki.streamNodeNames().map(Locki::getNode).toArray(InventoryNode[]::new); public InventoryLockItem(Settings settings) { super(settings); @@ -42,15 +39,14 @@ public TypedActionResult use(World world, PlayerEntity user, Hand han ItemStack heldStack = user.getStackInHand(hand); if (!world.isClient) { - NbtCompound data = heldStack.getOrCreateSubNbt("lockii"); - int currentDebug = data.getInt("debug"); + InventoryLockComponent currentDebug = heldStack.getOrDefault(InventoryLockComponent.TYPE, InventoryLockComponent.DEFAULT); if (user.isSneaking()) { - int newDebug = (currentDebug + 1) % ALL_DEFAULT_NODES.length; - data.putInt("debug", newDebug); - user.sendMessage(Text.literal("Now managing locking for " + ALL_DEFAULT_NODES[newDebug]), true); + InventoryLockComponent newDebug = currentDebug.cycle(); + heldStack.set(InventoryLockComponent.TYPE, newDebug); + user.sendMessage(Text.literal("Now managing locking for " + newDebug.node()), true); } else { - user.getInventory().toggleLock(LOCK, ALL_DEFAULT_NODES[currentDebug]); + user.getInventory().toggleLock(LOCK, currentDebug.node()); } } return TypedActionResult.success(heldStack); diff --git a/src/testmod/java/org/ladysnake/lockii/Lockii.java b/src/testmod/java/org/ladysnake/lockii/Lockii.java index 069186e..43deb1b 100644 --- a/src/testmod/java/org/ladysnake/lockii/Lockii.java +++ b/src/testmod/java/org/ladysnake/lockii/Lockii.java @@ -25,11 +25,12 @@ public final class Lockii implements ModInitializer { public static Identifier id(String path) { - return new Identifier("lockii", path); + return Identifier.of("lockii", path); } @Override public void onInitialize() { - Registry.register(Registries.ITEM, id("inventory_lock"), new InventoryLockItem(new Item.Settings())); + Registry.register(Registries.DATA_COMPONENT_TYPE, id("debug"), InventoryLockComponent.TYPE); + Registry.register(Registries.ITEM, id("inventory_lock"), new InventoryLockItem(new Item.Settings().component(InventoryLockComponent.TYPE, InventoryLockComponent.DEFAULT))); } } diff --git a/src/testmod/java/org/ladysnake/lockii/tests/LockiTestSuite.java b/src/testmod/java/org/ladysnake/lockii/tests/LockiTestSuite.java index 9e05906..992c459 100644 --- a/src/testmod/java/org/ladysnake/lockii/tests/LockiTestSuite.java +++ b/src/testmod/java/org/ladysnake/lockii/tests/LockiTestSuite.java @@ -34,6 +34,7 @@ import org.ladysnake.locki.Locki; import org.ladysnake.locki.impl.LockiComponents; import org.ladysnake.lockii.Lockii; + import static org.ladysnake.elmendorf.ByteBufChecker.any; public class LockiTestSuite implements FabricGameTest { @@ -47,7 +48,7 @@ public void checkPermission(TestContext ctx) { "New player can access their whole inventory", Permissions.check(player, "locki.access.inventory", true) ); - InventoryLock lock = Locki.registerLock(new Identifier("test", "test")); + InventoryLock lock = Locki.registerLock(Identifier.of("test", "test")); InventoryNode node = Locki.registerNode(InventoryNode.ROOT, "test"); Locki.registerNode(node, "child"); Locki.registerNode(InventoryNode.ROOT, "test-foo");