Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent players from collecting taters within games #180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading