Skip to content

Commit

Permalink
Add lock setters
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Patbox committed Dec 8, 2024
1 parent 4cda940 commit d5d0c7b
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/xyz/nucleoid/extras/lobby/NEItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import xyz.nucleoid.extras.lobby.item.LobbyBlockItem;
import xyz.nucleoid.extras.lobby.item.LobbyHeadItem;
import xyz.nucleoid.extras.lobby.item.LobbyTallBlockItem;
import xyz.nucleoid.extras.lobby.item.LockSetterItem;
import xyz.nucleoid.extras.lobby.item.QuickArmorStandItem;
import xyz.nucleoid.extras.lobby.item.RuleBookItem;
import xyz.nucleoid.extras.lobby.item.tater.CreativeTaterBoxItem;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class NEItems {
entries.add(NEItems.GOLD_LAUNCH_PAD);
entries.add(NEItems.IRON_LAUNCH_PAD);
entries.add(NEItems.CONTRIBUTOR_STATUE);
entries.add(NEItems.LOCK_SETTER);
entries.add(NEItems.INFINITE_DISPENSER);
entries.add(NEItems.INFINITE_DROPPER);
entries.add(NEItems.SNAKE_BLOCK);
Expand Down Expand Up @@ -493,6 +495,11 @@ public class NEItems {
.component(NEDataComponentTypes.LAUNCHER, LauncherComponent.DEFAULT)
.maxCount(1), settings -> new LaunchFeatherItem(settings));

public static final Item LOCK_SETTER = register("lock_setter", new Item.Settings()
.component(DataComponentTypes.LOCK, LockSetterItem.createUnlockableLock())
.component(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true)
.maxCount(1), LockSetterItem::new);

public static final Item RULE_BOOK = register("rule_book", new Item.Settings()
.rarity(Rarity.EPIC)
.component(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true), settings -> new RuleBookItem(settings));
Expand Down
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 src/main/java/xyz/nucleoid/extras/lobby/item/LockSetterItem.java
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);
}
}
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();
}
}
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();
}
}
5 changes: 5 additions & 0 deletions src/main/resources/data/nucleoid_extras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@

"item.nucleoid_extras.game_portal_opener": "Game Portal Opener",
"item.nucleoid_extras.launch_feather": "Launch Feather",
"item.nucleoid_extras.lock_setter": "Lock Setter",
"item.nucleoid_extras.tater_box": "Tater Box",
"item.nucleoid_extras.creative_tater_box": "Creative Tater Box",
"item.nucleoid_extras.tater_guidebook": "Tater Guidebook",
Expand Down Expand Up @@ -399,6 +400,10 @@
"text.nucleoid_extras.lobby_items": "Do not use this block outside of lobby!",
"text.nucleoid_extras.lobby_only": "(Lobby Only!)",

"text.nucleoid_extras.lock_setter.already_locked": "%s has already has a different lock!",
"text.nucleoid_extras.lock_setter.locked": "%s is now locked!",
"text.nucleoid_extras.lock_setter.unlocked": "%s has been unlocked!",

"text.nucleoid_extras.statistics.web_url": "Click here to check out all the stats for this game!",
"text.nucleoid_extras.statistics.bundle_header": "Statistics for %s",
"text.nucleoid_extras.statistics.stat": "%s: %s",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/extras.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"datafixer.Schema1460Mixin",
"debug.EntityMixin",
"lobby.ArmorStandEntityAccessor",
"lobby.BeaconBlockEntityMixin",
"lobby.LivingEntityAccessor",
"lobby.LockableContainerBlockEntityMixin",
"lobby.ServerChunkLoadingManagerAccessor",
"lobby.ServerPlayerEntityMixin",
"lobby.SkullBlockEntityAccessor",
Expand Down

0 comments on commit d5d0c7b

Please sign in to comment.