forked from Haven-King/glowcase
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Chailotl/lock-item
Added the lock item
- Loading branch information
Showing
9 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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.ItemStack; | ||
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; | ||
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); | ||
} | ||
|
||
@Override | ||
public ActionResult useOnBlock(ItemUsageContext context) { | ||
World world = context.getWorld(); | ||
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; | ||
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(soundEvent, SoundCategory.BLOCKS, 1.0F, 1.0F); | ||
be.markDirty(); | ||
|
||
return ActionResult.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void appendTooltip(ItemStack itemStack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) { | ||
tooltip.add(Text.translatable("item.glowcase.lock.tooltip.0").formatted(Formatting.GRAY)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/dev/hephaestus/glowcase/mixin/LockableContainerBlockEntityAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/dev/hephaestus/glowcase/mixin/PlayerEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"parent": "item/generated", | ||
"textures": { | ||
"layer0": "glowcase:item/lock" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "dev.hephaestus.glowcase.mixin", | ||
"compatibilityLevel": "JAVA_21", | ||
"mixins": [ | ||
"LockableContainerBlockEntityAccessor", | ||
"PlayerEntityMixin" | ||
], | ||
"client": [], | ||
"server": [], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |