Skip to content

Commit

Permalink
Use player context to create tater box tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Gegy committed Jun 6, 2024
1 parent ac8e0ae commit fd03413
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eu.pb4.playerdata.api.PlayerDataApi;
import eu.pb4.playerdata.api.storage.JsonDataStorage;
import eu.pb4.playerdata.api.storage.PlayerDataStorage;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -82,10 +83,6 @@ private ActionResult collectTaterFromSlot(ItemStack slotStack, ItemStack stack,
private ActionResult collectTater(Block block, ItemStack stack, PlayerEntity player) {
if (!(block instanceof TinyPotatoBlock tater)) return ActionResult.PASS;

if (stack.getItem() instanceof TaterBoxItem) {
stack.getOrCreateNbt().putUuid(TaterBoxItem.OWNER_KEY, player.getUuid());
}

boolean alreadyAdded = this.collectedTaters.contains(tater);
Text message;

Expand All @@ -94,6 +91,9 @@ private ActionResult collectTater(Block block, ItemStack stack, PlayerEntity pla
} else {
this.collectedTaters.add(tater);

// Update the tooltip of tater boxes in player's inventory
PolymerUtils.reloadInventory((ServerPlayerEntity) player);

message = Text.translatable("text.nucleoid_extras.tater_box.added", block.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class TaterBoxItem extends Item implements PolymerItem, Equipment {
private static final Text NOT_OWNER_MESSAGE = Text.translatable("text.nucleoid_extras.tater_box.not_owner").formatted(Formatting.RED);
public static final Text NONE_TEXT = Text.translatable("text.nucleoid_extras.tater_box.none");

public static final String OWNER_KEY = "Owner";
private static final String LEGACY_TATERS_KEY = "Taters";
private static final String SELECTED_TATER_KEY = "SelectedTater";
private static final int COLOR = 0xCEADAA;
Expand All @@ -48,17 +47,6 @@ public TaterBoxItem(Settings settings) {
super(settings);
}

private ActionResult isOwner(ItemStack stack, PlayerEntity player) {
NbtCompound tag = stack.getNbt();
if (tag == null) return ActionResult.PASS;

if (!tag.contains(OWNER_KEY, NbtElement.LIST_TYPE)) return ActionResult.PASS;
UUID uuid = tag.getUuid(OWNER_KEY);
if (uuid == null) return ActionResult.PASS;

return player.getUuid().equals(uuid) ? ActionResult.SUCCESS : ActionResult.FAIL;
}

private MutableText getTitle(ServerPlayerEntity player) {
Text name = this.getName();
int count = PlayerLobbyState.get(player).collectedTaters.size();
Expand Down Expand Up @@ -144,7 +132,7 @@ private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEnti
guiElementBuilder.hideFlags();
guiElementBuilder.setCallback((index, type, action, gui) -> {
ItemStack newStack = hand == null ? stack : user.getStackInHand(hand);
if (found && this == newStack.getItem() && this.isOwner(newStack, user) != ActionResult.FAIL) {
if (found && this == newStack.getItem()) {
TaterBoxItem.setSelectedTater(newStack, taterId);
gui.close();
}
Expand Down Expand Up @@ -198,7 +186,7 @@ public EquipmentSlot getSlotType() {
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

var owner = getOwnerPlayer(stack, world);
var owner = PolymerUtils.getPlayerContext();

Block selectedBlock = getSelectedTater(stack);
Text selectedName;
Expand All @@ -218,16 +206,6 @@ public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, Tool
tooltip.add(Text.translatable("text.nucleoid_extras.tater_box.completion", count, max, percent).formatted(Formatting.GRAY));
}

private PlayerEntity getOwnerPlayer(ItemStack stack, World world) {
if (!stack.hasNbt() || !stack.getNbt().contains(OWNER_KEY)) {
return null;
}

var owner = stack.getNbt().getUuid(OWNER_KEY);

return world.getPlayerByUuid(owner);
}

@Nullable
public static Identifier getSelectedTaterId(ItemStack stack) {
NbtCompound tag = stack.getNbt();
Expand Down

0 comments on commit fd03413

Please sign in to comment.