Skip to content

Commit

Permalink
Allow opening tater boxes from an inventory slot (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Sep 7, 2023
1 parent 8212d30 commit 760d19f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/xyz/nucleoid/extras/lobby/item/TaterBoxItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.block.Block;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.StackReference;
import net.minecraft.item.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
Expand All @@ -14,6 +15,7 @@
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -67,7 +69,22 @@ private MutableText getTitle(ServerPlayerEntity player) {
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
this.openTaterBox(world, user, stack, hand);

return TypedActionResult.success(stack, world.isClient());
}

@Override
public boolean onClicked(ItemStack stack, ItemStack otherStack, Slot slot, ClickType clickType, PlayerEntity player, StackReference cursorStackReference) {
if (clickType == ClickType.RIGHT) {
this.openTaterBox(player.getWorld(), player, stack, null);
return true;
}

return false;
}

private void openTaterBox(World world, PlayerEntity user, ItemStack stack, Hand hand) {
if (!world.isClient()) {
if (stack.hasNbt() && stack.getNbt().contains(LEGACY_TATERS_KEY)) {
var data = PlayerLobbyState.get(user);
Expand Down Expand Up @@ -99,8 +116,6 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
ui.setTitle(this.getTitle((ServerPlayerEntity) user));
ui.open();
}

return TypedActionResult.success(stack, world.isClient());
}

private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEntity user, Hand hand, ItemConvertible icon, Text text, Identifier taterId, boolean found) {
Expand All @@ -109,7 +124,7 @@ private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEnti
guiElementBuilder.setFound(found);
guiElementBuilder.hideFlags();
guiElementBuilder.setCallback((index, type, action, gui) -> {
ItemStack newStack = user.getStackInHand(hand);
ItemStack newStack = hand == null ? stack : user.getStackInHand(hand);
if (found && this == newStack.getItem() && this.isOwner(newStack, user) != ActionResult.FAIL) {
TaterBoxItem.setSelectedTater(newStack, taterId);
gui.close();
Expand Down

0 comments on commit 760d19f

Please sign in to comment.