From 7c0c8444e483d171e5e34a09eeef5bb4c4326d7f Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Sat, 29 Jun 2024 14:24:30 -0400 Subject: [PATCH] Prevent players from collecting taters within games --- .../java/xyz/nucleoid/extras/lobby/NEItems.java | 9 +++++++-- .../nucleoid/extras/lobby/PlayerLobbyState.java | 16 ++++++++-------- .../extras/lobby/item/tater/TaterBoxItem.java | 9 +++++---- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java b/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java index ff12724..1f4ab1b 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java @@ -41,6 +41,7 @@ import xyz.nucleoid.extras.lobby.item.tater.CreativeTaterBoxItem; import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem; import xyz.nucleoid.extras.lobby.item.tater.TaterGuidebookItem; +import xyz.nucleoid.plasmid.game.manager.GameSpaceManager; import java.util.ArrayList; import java.util.Collections; @@ -866,13 +867,17 @@ public static void giveLobbyItems(ServerPlayerEntity player) { }); } + public static boolean canUseTaters(ServerPlayerEntity player) { + return !GameSpaceManager.get().inGame(player); + } + private static ActionResult onUseBlock(PlayerEntity player, World world, Hand hand, BlockHitResult hitResult) { if (!player.getWorld().isClient() && hitResult != null && hand == Hand.MAIN_HAND) { ItemStack stack = player.getStackInHand(hand); BlockPos pos = hitResult.getBlockPos(); PlayerLobbyState state = PlayerLobbyState.get(player); - state.collectTaterFromBlock(world, pos, stack, player); + state.collectTaterFromBlock(world, pos, stack, (ServerPlayerEntity) player); } return ActionResult.PASS; @@ -884,7 +889,7 @@ private static ActionResult onUseEntity(PlayerEntity player, World world, Hand h Vec3d hitPos = hitResult.getPos().subtract(entity.getPos()); PlayerLobbyState state = PlayerLobbyState.get(player); - state.collectTaterFromEntity(entity, hitPos, stack, player); + state.collectTaterFromEntity(entity, hitPos, stack, (ServerPlayerEntity) player); } return ActionResult.PASS; diff --git a/src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java b/src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java index bcaf5cc..9fdb790 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java @@ -34,7 +34,7 @@ public class PlayerLobbyState { public static final PlayerDataStorage STORAGE = new JsonDataStorage<>("nucleoid_extras", PlayerLobbyState.class); public final Set collectedTaters = new HashSet<>(); - public ActionResult collectTaterFromBlock(World world, BlockPos pos, ItemStack stack, PlayerEntity player) { + public ActionResult collectTaterFromBlock(World world, BlockPos pos, ItemStack stack, ServerPlayerEntity player) { BlockState state = world.getBlockState(pos); Block block = state.getBlock(); @@ -47,7 +47,7 @@ public ActionResult collectTaterFromBlock(World world, BlockPos pos, ItemStack s return result; } - public ActionResult collectTaterFromEntity(Entity entity, Vec3d hitPos, ItemStack stack, PlayerEntity player) { + public ActionResult collectTaterFromEntity(Entity entity, Vec3d hitPos, ItemStack stack, ServerPlayerEntity player) { if (entity instanceof ArmorStandEntity armorStand) { EquipmentSlot slot = ((ArmorStandEntityAccessor) (Object) armorStand).callSlotFromPosition(hitPos); return this.collectTaterFromSlot(armorStand.getEquippedStack(slot), stack, player); @@ -66,7 +66,7 @@ public ActionResult collectTaterFromEntity(Entity entity, Vec3d hitPos, ItemStac return ActionResult.PASS; } - private ActionResult collectTaterFromSlot(ItemStack slotStack, ItemStack stack, PlayerEntity player) { + private ActionResult collectTaterFromSlot(ItemStack slotStack, ItemStack stack, ServerPlayerEntity player) { if (!slotStack.isEmpty() && slotStack.getItem() instanceof BlockItem slotItem) { Block block = slotItem.getBlock(); ActionResult result = this.collectTater(block, stack, player); @@ -81,8 +81,8 @@ private ActionResult collectTaterFromSlot(ItemStack slotStack, ItemStack stack, return ActionResult.PASS; } - private ActionResult collectTater(Block block, ItemStack stack, PlayerEntity player) { - if (!(block instanceof TinyPotatoBlock tater)) return ActionResult.PASS; + private ActionResult collectTater(Block block, ItemStack stack, ServerPlayerEntity player) { + if (!NEItems.canUseTaters(player) || !(block instanceof TinyPotatoBlock tater)) return ActionResult.PASS; boolean alreadyAdded = this.collectedTaters.contains(tater); Text message; @@ -93,13 +93,13 @@ private ActionResult collectTater(Block block, ItemStack stack, PlayerEntity pla this.collectedTaters.add(tater); // Update the tooltip of tater boxes in player's inventory - PolymerUtils.reloadInventory((ServerPlayerEntity) player); + PolymerUtils.reloadInventory(player); message = Text.translatable("text.nucleoid_extras.tater_box.added", block.getName()); } player.sendMessage(message, true); - triggerCollectCriterion((ServerPlayerEntity) player, tater, this.collectedTaters.size()); + triggerCollectCriterion(player, tater, this.collectedTaters.size()); return alreadyAdded ? ActionResult.FAIL : ActionResult.SUCCESS; } @@ -108,7 +108,7 @@ private static void triggerCollectCriterion(ServerPlayerEntity player, TinyPotat NECriteria.TATER_COLLECTED.trigger(player, tater, count); } - private static boolean isFickle(ActionResult result, Block block, PlayerEntity player) { + private static boolean isFickle(ActionResult result, Block block, ServerPlayerEntity player) { return result.isAccepted() && block instanceof TinyPotatoBlock tater && tater.isFickle() && !player.isCreative(); } diff --git a/src/main/java/xyz/nucleoid/extras/lobby/item/tater/TaterBoxItem.java b/src/main/java/xyz/nucleoid/extras/lobby/item/tater/TaterBoxItem.java index ec16b5c..84fae97 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/item/tater/TaterBoxItem.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/item/tater/TaterBoxItem.java @@ -20,6 +20,7 @@ import net.minecraft.util.*; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.extras.lobby.NEItems; import xyz.nucleoid.extras.lobby.PlayerLobbyState; import xyz.nucleoid.extras.lobby.block.tater.CorruptaterBlock; import xyz.nucleoid.extras.lobby.block.tater.CubicPotatoBlock; @@ -72,7 +73,7 @@ public boolean onClicked(ItemStack stack, ItemStack otherStack, Slot slot, Click } private void openTaterBox(World world, ServerPlayerEntity user, ItemStack stack, Hand hand) { - if (!world.isClient()) { + if (NEItems.canUseTaters(user)) { if (stack.hasNbt() && stack.getNbt().contains(LEGACY_TATERS_KEY)) { var data = PlayerLobbyState.get(user); @@ -100,12 +101,12 @@ private void openTaterBox(World world, ServerPlayerEntity user, ItemStack stack, }) .forEachOrdered(taters::add); - var ui = TaterBoxGui.of((ServerPlayerEntity) user, taters, this.isCreative()); + var ui = TaterBoxGui.of(user, taters, this.isCreative()); ui.setHideUnfound(true); - ui.setTitle(this.getTitle((ServerPlayerEntity) user)); + ui.setTitle(this.getTitle(user)); ui.open(); - ((ServerPlayerEntity) user).playSound(this.getEquipSound(), SoundCategory.PLAYERS, 0.8f, 1); + user.playSound(this.getEquipSound(), SoundCategory.PLAYERS, 0.8f, 1); } }