-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
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
12 changes: 12 additions & 0 deletions
12
src/main/java/xyz/nucleoid/extras/lobby/block/ContainerLockAccess.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,12 @@ | ||
package xyz.nucleoid.extras.lobby.block; | ||
|
||
import net.minecraft.inventory.ContainerLock; | ||
import net.minecraft.text.Text; | ||
|
||
public interface ContainerLockAccess { | ||
ContainerLock getContainerLock(); | ||
|
||
void setContainerLock(ContainerLock lock); | ||
|
||
Text getContainerLockName(); | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/xyz/nucleoid/extras/lobby/item/LockSetterItem.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,71 @@ | ||
package xyz.nucleoid.extras.lobby.item; | ||
|
||
import eu.pb4.polymer.core.api.item.SimplePolymerItem; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.ContainerLock; | ||
import net.minecraft.item.ItemUsageContext; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.predicate.NumberRange.IntRange; | ||
import net.minecraft.predicate.item.ItemPredicate; | ||
import net.minecraft.sound.SoundCategory; | ||
import net.minecraft.sound.SoundEvents; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.ActionResult; | ||
import xyz.nucleoid.extras.lobby.block.ContainerLockAccess; | ||
|
||
public class LockSetterItem extends SimplePolymerItem { | ||
public LockSetterItem(Settings settings) { | ||
super(settings, Items.TRIAL_KEY, false); | ||
} | ||
|
||
@Override | ||
public ActionResult useOnBlock(ItemUsageContext context) { | ||
var world = context.getWorld(); | ||
var user = context.getPlayer(); | ||
|
||
if (!world.isClient() && user.isCreativeLevelTwoOp()) { | ||
var stack = context.getStack(); | ||
var newLock = stack.get(DataComponentTypes.LOCK); | ||
|
||
if (newLock != null) { | ||
var pos = context.getBlockPos(); | ||
var blockEntity = world.getBlockEntity(pos); | ||
|
||
if (blockEntity instanceof ContainerLockAccess access) { | ||
var currentLock = access.getContainerLock(); | ||
|
||
if (currentLock == ContainerLock.EMPTY) { | ||
access.setContainerLock(newLock); | ||
sendFeedback(user, access, "locked"); | ||
} else if (!newLock.equals(currentLock)) { | ||
sendFeedback(user, access, "already_locked"); | ||
return ActionResult.FAIL; | ||
} else { | ||
access.setContainerLock(ContainerLock.EMPTY); | ||
sendFeedback(user, access, "unlocked"); | ||
} | ||
|
||
return ActionResult.SUCCESS_SERVER; | ||
} | ||
} | ||
} | ||
|
||
return ActionResult.PASS; | ||
} | ||
|
||
private static void sendFeedback(PlayerEntity player, ContainerLockAccess access, String suffix) { | ||
var text = Text.translatable("text.nucleoid_extras.lock_setter." + suffix, access.getContainerLockName()); | ||
player.sendMessage(text, true); | ||
|
||
player.playSoundToPlayer(SoundEvents.BLOCK_CHEST_LOCKED, SoundCategory.BLOCKS, 1, 1); | ||
} | ||
|
||
public static ContainerLock createUnlockableLock() { | ||
var predicate = ItemPredicate.Builder.create() | ||
.count(IntRange.exactly(-1)) | ||
.build(); | ||
|
||
return new ContainerLock(predicate); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/xyz/nucleoid/extras/mixin/lobby/BeaconBlockEntityMixin.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,32 @@ | ||
package xyz.nucleoid.extras.mixin.lobby; | ||
|
||
import net.minecraft.block.entity.BeaconBlockEntity; | ||
import net.minecraft.inventory.ContainerLock; | ||
import net.minecraft.text.Text; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import xyz.nucleoid.extras.lobby.block.ContainerLockAccess; | ||
|
||
@Mixin(BeaconBlockEntity.class) | ||
public abstract class BeaconBlockEntityMixin implements ContainerLockAccess { | ||
@Shadow | ||
private ContainerLock lock; | ||
|
||
@Shadow | ||
public abstract Text getDisplayName(); | ||
|
||
@Override | ||
public ContainerLock getContainerLock() { | ||
return this.lock; | ||
} | ||
|
||
@Override | ||
public void setContainerLock(ContainerLock lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public Text getContainerLockName() { | ||
return this.getDisplayName(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/xyz/nucleoid/extras/mixin/lobby/LockableContainerBlockEntityMixin.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,32 @@ | ||
package xyz.nucleoid.extras.mixin.lobby; | ||
|
||
import net.minecraft.block.entity.LockableContainerBlockEntity; | ||
import net.minecraft.inventory.ContainerLock; | ||
import net.minecraft.text.Text; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import xyz.nucleoid.extras.lobby.block.ContainerLockAccess; | ||
|
||
@Mixin(LockableContainerBlockEntity.class) | ||
public abstract class LockableContainerBlockEntityMixin implements ContainerLockAccess { | ||
@Shadow | ||
private ContainerLock lock; | ||
|
||
@Shadow | ||
public abstract Text getDisplayName(); | ||
|
||
@Override | ||
public ContainerLock getContainerLock() { | ||
return this.lock; | ||
} | ||
|
||
@Override | ||
public void setContainerLock(ContainerLock lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public Text getContainerLockName() { | ||
return this.getDisplayName(); | ||
} | ||
} |
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