From 7ae96f861dcefe7386cc4fc0a8b8bc71a8e0c4b7 Mon Sep 17 00:00:00 2001 From: Chai <7232280+Chailotl@users.noreply.github.com> Date: Fri, 30 Aug 2024 23:51:24 -0400 Subject: [PATCH 1/7] Added lock item for #42 --- .../dev/hephaestus/glowcase/Glowcase.java | 4 ++ .../hephaestus/glowcase/item/LockItem.java | 43 ++++++++++++++++++ .../LockableContainerBlockEntityAccessor.java | 15 ++++++ .../glowcase/mixin/PlayerEntityMixin.java | 24 ++++++++++ .../resources/assets/glowcase/lang/en_us.json | 3 ++ .../assets/glowcase/models/item/lock.json | 6 +++ .../assets/glowcase/textures/item/lock.png | Bin 0 -> 196 bytes src/main/resources/fabric.mod.json | 5 +- src/main/resources/glowcase.mixins.json | 15 ++++++ 9 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/hephaestus/glowcase/item/LockItem.java create mode 100644 src/main/java/dev/hephaestus/glowcase/mixin/LockableContainerBlockEntityAccessor.java create mode 100644 src/main/java/dev/hephaestus/glowcase/mixin/PlayerEntityMixin.java create mode 100644 src/main/resources/assets/glowcase/models/item/lock.json create mode 100644 src/main/resources/assets/glowcase/textures/item/lock.png create mode 100644 src/main/resources/glowcase.mixins.json diff --git a/src/main/java/dev/hephaestus/glowcase/Glowcase.java b/src/main/java/dev/hephaestus/glowcase/Glowcase.java index 84476b4..8f32138 100644 --- a/src/main/java/dev/hephaestus/glowcase/Glowcase.java +++ b/src/main/java/dev/hephaestus/glowcase/Glowcase.java @@ -9,6 +9,7 @@ import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity; import dev.hephaestus.glowcase.block.entity.TextBlockEntity; import dev.hephaestus.glowcase.compat.PolydexCompatibility; +import dev.hephaestus.glowcase.item.LockItem; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.loader.api.FabricLoader; @@ -46,6 +47,8 @@ public class Glowcase implements ModInitializer { public static final Supplier TEXT_BLOCK_ITEM = registerItem("text_block", () -> new BlockItem(TEXT_BLOCK.get(), new Item.Settings())); public static final Supplier> TEXT_BLOCK_ENTITY = registerBlockEntity("text_block", () -> BlockEntityType.Builder.create(TextBlockEntity::new, TEXT_BLOCK.get()).build(null)); + public static final Supplier LOCK_ITEM = registerItem("lock", () -> new LockItem(new Item.Settings())); + public static final Supplier ITEM_GROUP = registerItemGroup("items", () -> FabricItemGroup.builder() .displayName(Text.translatable("itemGroup.glowcase.items")) .icon(() -> new ItemStack(Items.GLOWSTONE)) @@ -53,6 +56,7 @@ public class Glowcase implements ModInitializer { entries.add(HYPERLINK_BLOCK_ITEM.get()); entries.add(ITEM_DISPLAY_BLOCK_ITEM.get()); entries.add(TEXT_BLOCK_ITEM.get()); + entries.add(LOCK_ITEM.get()); }) .build() ); diff --git a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java new file mode 100644 index 0000000..a0b4d4e --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java @@ -0,0 +1,43 @@ +package dev.hephaestus.glowcase.item; + +import dev.hephaestus.glowcase.mixin.LockableContainerBlockEntityAccessor; +import net.minecraft.block.entity.LockableContainerBlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.ContainerLock; +import net.minecraft.item.Item; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; +import net.minecraft.world.World; + +public class LockItem extends Item { + public LockItem(Settings settings) { + super(settings); + } + + @Override + public ActionResult useOnBlock(ItemUsageContext context) { + World world = context.getWorld(); + if (world.isClient || !(world.getBlockEntity(context.getBlockPos()) instanceof LockableContainerBlockEntity be)) return ActionResult.PASS; + + var bea = (LockableContainerBlockEntityAccessor) be; + Text message; + + if (bea.getLock().equals(ContainerLock.EMPTY)) { + bea.setLock(new ContainerLock("glowcase")); + message = Text.translatable("gui.glowcase.locked_block", be.getDisplayName()); + } else { + bea.setLock(ContainerLock.EMPTY); + message = Text.translatable("gui.glowcase.unlocked_block", be.getDisplayName()); + } + + PlayerEntity player = context.getPlayer(); + player.sendMessage(message, true); + player.playSoundToPlayer(SoundEvents.BLOCK_CHEST_LOCKED, SoundCategory.BLOCKS, 1.0F, 1.0F); + be.markDirty(); + + return ActionResult.SUCCESS; + } +} diff --git a/src/main/java/dev/hephaestus/glowcase/mixin/LockableContainerBlockEntityAccessor.java b/src/main/java/dev/hephaestus/glowcase/mixin/LockableContainerBlockEntityAccessor.java new file mode 100644 index 0000000..8b95c09 --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/mixin/LockableContainerBlockEntityAccessor.java @@ -0,0 +1,15 @@ +package dev.hephaestus.glowcase.mixin; + +import net.minecraft.block.entity.LockableContainerBlockEntity; +import net.minecraft.inventory.ContainerLock; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(LockableContainerBlockEntity.class) +public interface LockableContainerBlockEntityAccessor { + @Accessor + ContainerLock getLock(); + + @Accessor + void setLock(ContainerLock lock); +} diff --git a/src/main/java/dev/hephaestus/glowcase/mixin/PlayerEntityMixin.java b/src/main/java/dev/hephaestus/glowcase/mixin/PlayerEntityMixin.java new file mode 100644 index 0000000..5d2879c --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/mixin/PlayerEntityMixin.java @@ -0,0 +1,24 @@ +package dev.hephaestus.glowcase.mixin; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import dev.hephaestus.glowcase.Glowcase; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(PlayerEntity.class) +public abstract class PlayerEntityMixin { + @Shadow + public abstract ItemStack getEquippedStack(EquipmentSlot slot); + + @ModifyReturnValue( + method = "shouldCancelInteraction", + at = @At(value = "RETURN") + ) + private boolean directLockInteraction(boolean original) { + return getEquippedStack(EquipmentSlot.MAINHAND).getItem().equals(Glowcase.LOCK_ITEM.get()) || original; + } +} diff --git a/src/main/resources/assets/glowcase/lang/en_us.json b/src/main/resources/assets/glowcase/lang/en_us.json index 8f81d17..c88df3f 100644 --- a/src/main/resources/assets/glowcase/lang/en_us.json +++ b/src/main/resources/assets/glowcase/lang/en_us.json @@ -13,6 +13,9 @@ "gui.glowcase.none": "(None)", "item.glowcase.text_block": "Text Block", "item.glowcase.hyperlink_block": "Hyperlink Block", + "item.glowcase.lock": "Lock", + "gui.glowcase.locked_block": "%s is now locked", + "gui.glowcase.unlocked_block": "%s is now unlocked", "gui.glowcase.title": "Title", "gui.glowcase.url": "URL", "block.glowcase.generic.tooltip": "Interact with a glowcase item to edit", diff --git a/src/main/resources/assets/glowcase/models/item/lock.json b/src/main/resources/assets/glowcase/models/item/lock.json new file mode 100644 index 0000000..1467ebc --- /dev/null +++ b/src/main/resources/assets/glowcase/models/item/lock.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "glowcase:item/lock" + } +} diff --git a/src/main/resources/assets/glowcase/textures/item/lock.png b/src/main/resources/assets/glowcase/textures/item/lock.png new file mode 100644 index 0000000000000000000000000000000000000000..02566b836422b02199bf094a3af90eac62040cbc GIT binary patch literal 196 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJV{wqX6T`Z5GB1G~mUKs7M+SzC z{oH>NS%G}c0G|-o|NsA+o0|i9l9#QH0V(E^Aiv=M{~4~I;svT@;4JWnEM{QfI{?Cr zS=Tx_fr6% Date: Sat, 31 Aug 2024 17:23:43 +1000 Subject: [PATCH 2/7] fmj consistency --- src/main/resources/fabric.mod.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 0af3d8e..0c0ad98 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,6 +17,9 @@ }, "license": "${license}", "icon": "assets/${modId}/icon.png", + "mixins": [ + "${modId}.mixins.json" + ], "depends": { "minecraft": ">=${mc}", "fabricloader": ">=${fl}", @@ -33,8 +36,5 @@ "client": [ "dev.hephaestus.glowcase.client.GlowcaseClient" ] - }, - "mixins": [ - "glowcase.mixins.json" - ] + } } From b3cdfa02a837a6a4d06fd9ffad3be73e4ee313c5 Mon Sep 17 00:00:00 2001 From: Chai <7232280+Chailotl@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:07:07 -0400 Subject: [PATCH 3/7] Added creative mode check --- src/main/java/dev/hephaestus/glowcase/item/LockItem.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java index a0b4d4e..8698696 100644 --- a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java +++ b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java @@ -20,7 +20,13 @@ public LockItem(Settings settings) { @Override public ActionResult useOnBlock(ItemUsageContext context) { World world = context.getWorld(); - if (world.isClient || !(world.getBlockEntity(context.getBlockPos()) instanceof LockableContainerBlockEntity be)) return ActionResult.PASS; + PlayerEntity player = context.getPlayer(); + if (world.isClient || + player == null || + !player.isCreative() || + !(world.getBlockEntity(context.getBlockPos()) instanceof LockableContainerBlockEntity be)) { + return ActionResult.PASS; + } var bea = (LockableContainerBlockEntityAccessor) be; Text message; @@ -33,7 +39,6 @@ public ActionResult useOnBlock(ItemUsageContext context) { message = Text.translatable("gui.glowcase.unlocked_block", be.getDisplayName()); } - PlayerEntity player = context.getPlayer(); player.sendMessage(message, true); player.playSoundToPlayer(SoundEvents.BLOCK_CHEST_LOCKED, SoundCategory.BLOCKS, 1.0F, 1.0F); be.markDirty(); From 735b3f9041a1b5c9c0e60bd342395839fa53eb42 Mon Sep 17 00:00:00 2001 From: Chai <7232280+Chailotl@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:36:32 -0400 Subject: [PATCH 4/7] Added tooltip to the lock item --- .../java/dev/hephaestus/glowcase/item/LockItem.java | 10 ++++++++++ src/main/resources/assets/glowcase/lang/en_us.json | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java index 8698696..36b9b68 100644 --- a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java +++ b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java @@ -5,13 +5,18 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.ContainerLock; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; +import net.minecraft.item.tooltip.TooltipType; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; +import net.minecraft.util.Formatting; import net.minecraft.world.World; +import java.util.List; + public class LockItem extends Item { public LockItem(Settings settings) { super(settings); @@ -45,4 +50,9 @@ public ActionResult useOnBlock(ItemUsageContext context) { return ActionResult.SUCCESS; } + + @Override + public void appendTooltip(ItemStack itemStack, Item.TooltipContext context, List tooltip, TooltipType options) { + tooltip.add(Text.translatable("item.glowcase.lock.tooltip.0").formatted(Formatting.GRAY)); + } } diff --git a/src/main/resources/assets/glowcase/lang/en_us.json b/src/main/resources/assets/glowcase/lang/en_us.json index c88df3f..1b98d38 100644 --- a/src/main/resources/assets/glowcase/lang/en_us.json +++ b/src/main/resources/assets/glowcase/lang/en_us.json @@ -24,5 +24,6 @@ "block.glowcase.hyperlink_block.tooltip.0": "Opens a URL when interacted", "block.glowcase.item_display_block.tooltip.0": "Gives an item stack when interacted", "block.glowcase.item_display_block.tooltip.1": "Interact with an item stack to set it", - "block.glowcase.item_display_block.tooltip.2": "Interact with the same item to edit" + "block.glowcase.item_display_block.tooltip.2": "Interact with the same item to edit", + "item.glowcase.lock.tooltip.0": "Locks and unlocks containers" } From 3d7a6650bb6ed948602be067d8548500ae0a3b5c Mon Sep 17 00:00:00 2001 From: Chai <7232280+Chailotl@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:43:08 -0400 Subject: [PATCH 5/7] Update lock.png --- .../assets/glowcase/textures/item/lock.png | Bin 196 -> 198 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/src/main/resources/assets/glowcase/textures/item/lock.png b/src/main/resources/assets/glowcase/textures/item/lock.png index 02566b836422b02199bf094a3af90eac62040cbc..d69f1e3096254abcccc594dfb949596287af2d92 100644 GIT binary patch delta 96 zcmV-m0H6QF0mcE4ZCSqn01m$aI0aKA0000tNklQ2*cR^|7dkgF&dBd z(2OPo4_+ZUUTKljH{jv`$Xh6DO|CE=e+6RUP>>C=Q~@rJ<9J#C0000 Date: Sat, 31 Aug 2024 11:45:13 -0400 Subject: [PATCH 6/7] Changed lock/unlock sounds and made them differentiable --- src/main/java/dev/hephaestus/glowcase/item/LockItem.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java index 36b9b68..f30487f 100644 --- a/src/main/java/dev/hephaestus/glowcase/item/LockItem.java +++ b/src/main/java/dev/hephaestus/glowcase/item/LockItem.java @@ -9,6 +9,7 @@ import net.minecraft.item.ItemUsageContext; import net.minecraft.item.tooltip.TooltipType; import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; @@ -35,17 +36,20 @@ public ActionResult useOnBlock(ItemUsageContext context) { var bea = (LockableContainerBlockEntityAccessor) be; Text message; + SoundEvent soundEvent; if (bea.getLock().equals(ContainerLock.EMPTY)) { bea.setLock(new ContainerLock("glowcase")); message = Text.translatable("gui.glowcase.locked_block", be.getDisplayName()); + soundEvent = SoundEvents.BLOCK_WOODEN_TRAPDOOR_CLOSE; } else { bea.setLock(ContainerLock.EMPTY); message = Text.translatable("gui.glowcase.unlocked_block", be.getDisplayName()); + soundEvent = SoundEvents.BLOCK_WOODEN_TRAPDOOR_OPEN; } player.sendMessage(message, true); - player.playSoundToPlayer(SoundEvents.BLOCK_CHEST_LOCKED, SoundCategory.BLOCKS, 1.0F, 1.0F); + player.playSoundToPlayer(soundEvent, SoundCategory.BLOCKS, 1.0F, 1.0F); be.markDirty(); return ActionResult.SUCCESS; From d351f7a0d65e4bd923e92e7a001b67f164b56371 Mon Sep 17 00:00:00 2001 From: Chai <7232280+Chailotl@users.noreply.github.com> Date: Sat, 31 Aug 2024 12:45:32 -0400 Subject: [PATCH 7/7] =?UTF-8?q?Added=20=F0=9F=94=92/=F0=9F=94=93=20icons?= =?UTF-8?q?=20to=20their=20respective=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/assets/glowcase/lang/en_us.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/assets/glowcase/lang/en_us.json b/src/main/resources/assets/glowcase/lang/en_us.json index 1b98d38..700e588 100644 --- a/src/main/resources/assets/glowcase/lang/en_us.json +++ b/src/main/resources/assets/glowcase/lang/en_us.json @@ -14,8 +14,8 @@ "item.glowcase.text_block": "Text Block", "item.glowcase.hyperlink_block": "Hyperlink Block", "item.glowcase.lock": "Lock", - "gui.glowcase.locked_block": "%s is now locked", - "gui.glowcase.unlocked_block": "%s is now unlocked", + "gui.glowcase.locked_block": "%s is now locked \uD83D\uDD12", + "gui.glowcase.unlocked_block": "%s is now unlocked \uD83D\uDD13", "gui.glowcase.title": "Title", "gui.glowcase.url": "URL", "block.glowcase.generic.tooltip": "Interact with a glowcase item to edit",