Skip to content

Commit

Permalink
Prevent players from collecting taters within games (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Aug 2, 2024
1 parent 137edfe commit f8f31a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/main/java/xyz/nucleoid/extras/lobby/NEItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PlayerLobbyState {
public static final PlayerDataStorage<PlayerLobbyState> STORAGE = new JsonDataStorage<>("nucleoid_extras", PlayerLobbyState.class);
public final Set<TinyPotatoBlock> 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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit f8f31a8

Please sign in to comment.