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

Add a tag for collectable tater blocks #195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static Advancement.Builder requiringTatersCollected(int count) {
private static Advancement.Builder requiringTatersCollected(TaterCount count) {
var builder = Advancement.Builder.createUntelemetered();

var name = "get_" + count.count() + "_tater" + (count.count() == 1 ? "" : "s");
var name = "get_" + count.count(null) + "_tater" + (count.count(null) == 1 ? "" : "s");
var conditions = new TaterCollectedCriterion.Conditions(Optional.empty(), Optional.of(count));

builder.criterion(name, NECriteria.TATER_COLLECTED.create(conditions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.registry.RegistryWrapper.WrapperLookup;
import net.minecraft.registry.tag.BlockTags;
import xyz.nucleoid.extras.lobby.NEBlocks;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.tag.NEBlockTags;

public class NEBlockTagProvider extends FabricTagProvider.BlockTagProvider {
Expand All @@ -17,6 +18,10 @@ public NEBlockTagProvider(FabricDataOutput dataOutput, CompletableFuture<Registr

@Override
protected void configure(WrapperLookup lookup) {
for (var block : TinyPotatoBlock.TATERS) {
this.getOrCreateTagBuilder(NEBlockTags.COLLECTABLE_TATERS).add(block);
}

this.getOrCreateTagBuilder(BlockTags.DOORS)
.add(NEBlocks.TRANSIENT_IRON_DOOR)
.add(NEBlocks.TRANSIENT_COPPER_DOOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private ActionResult collectTaterFromSlot(ItemStack slotStack, ItemStack stack,
}

private ActionResult collectTater(Block block, ItemStack stack, ServerPlayerEntity player) {
if (!NEItems.canUseTaters(player) || !(block instanceof TinyPotatoBlock tater)) return ActionResult.PASS;
if (!NEItems.canUseTaters(player) || !(block instanceof TinyPotatoBlock tater) || !tater.isCollectable()) return ActionResult.PASS;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import xyz.nucleoid.extras.tag.NEBlockTags;
import xyz.nucleoid.extras.util.SkinEncoder;

import java.util.ArrayList;
Expand Down Expand Up @@ -95,6 +96,9 @@ public boolean isFickle() {
return false;
}

public boolean isCollectable() {
return this.getDefaultState().isIn(NEBlockTags.COLLECTABLE_TATERS);
}

public final String getItemTexture() {
return this.texture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class TaterCollectedCriterion extends AbstractCriterion<TaterCollectedCriterion.Conditions> {
public void trigger(ServerPlayerEntity player, TinyPotatoBlock tater, int count) {
this.trigger(player, conditions -> conditions.matches(tater, count));
this.trigger(player, conditions -> conditions.matches(player, tater, count));
}

@Override
Expand All @@ -36,9 +36,9 @@ public Conditions(Optional<RegistryEntry<Block>> tater, Optional<TaterCount> cou
this.count = count;
}

public boolean matches(TinyPotatoBlock tater, int count) {
public boolean matches(ServerPlayerEntity player, TinyPotatoBlock tater, int count) {
boolean taterMatches = this.tater.isEmpty() || this.tater.get().value() == tater;
boolean countMatches = this.count.isEmpty() || this.count.get().matches(count);
boolean countMatches = this.count.isEmpty() || this.count.get().matches(player.getRegistryManager(), count);
return taterMatches && countMatches;
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/xyz/nucleoid/extras/lobby/criterion/TaterCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.dynamic.Codecs;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;

import java.util.function.Function;

Expand All @@ -15,10 +18,10 @@ public sealed interface TaterCount {
return count instanceof Value ? Either.left((Value) count) : Either.right((All) count);
});

int count();
int count(@Nullable RegistryWrapper.WrapperLookup registries);

default boolean matches(int count) {
return this.count() <= count;
default boolean matches(RegistryWrapper.WrapperLookup registries, int count) {
return this.count(registries) <= count;
}

record Value(int count) implements TaterCount {
Expand All @@ -29,6 +32,11 @@ record Value(int count) implements TaterCount {
throw new IllegalArgumentException("Count must be non-negative: " + count);
}
}

@Override
public int count(@Nullable RegistryWrapper.WrapperLookup registries) {
return this.count;
}
}

record All() implements TaterCount {
Expand All @@ -44,8 +52,13 @@ record All() implements TaterCount {
}, string -> STRING);

@Override
public int count() {
return TinyPotatoBlock.TATERS.size();
public int count(@Nullable RegistryWrapper.WrapperLookup registries) {
if (registries == null) {
return TinyPotatoBlock.TATERS.size();
}

long count = TaterBoxItem.getCollectableTaterCount(registries);
return count > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) count;
}
}
}
44 changes: 32 additions & 12 deletions src/main/java/xyz/nucleoid/extras/lobby/gui/TaterBoxGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.util.Formatting;
import xyz.nucleoid.extras.lobby.PlayerLobbyState;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;
import xyz.nucleoid.extras.util.PagedGui;

import java.util.List;
Expand Down Expand Up @@ -74,12 +75,13 @@ protected DisplayElement getNavElement(int id) {
public List<GuiElementInterface> getList() {
List<GuiElementInterface> all = super.getList();

if(this.shouldHideUnfound()) {
return all.stream().filter(element -> {
if(element instanceof TaterGuiElement taterGuiElement) return taterGuiElement.isFound();
else return true;
}).toList();
} else return all;
return all.stream().filter(element -> {
if (element instanceof TaterGuiElement taterGuiElement) {
return taterGuiElement.shouldShow(this.shouldHideUnfound());
}

return true;
}).toList();
}

public static DisplayElement hideUnfoundButton(TaterBoxGui gui) {
Expand All @@ -101,9 +103,15 @@ public static DisplayElement collectAllButton(TaterBoxGui gui) {
GuiElementBuilder builder = new GuiElementBuilder(COLLECT_ALL_ICON)
.setItemName(COLLECT_ALL_TEXT)
.hideDefaultTooltip()
.setCallback(() -> {
.setCallback(clickType -> {
var player = gui.getPlayer();
var state = PlayerLobbyState.get(gui.getPlayer());
state.collectedTaters.addAll(TinyPotatoBlock.TATERS);

if (clickType.shift) {
state.collectedTaters.addAll(TinyPotatoBlock.TATERS);
} else {
TaterBoxItem.getCollectableTaters(player.getRegistryManager()).forEach(state.collectedTaters::add);
}

playSound(gui.player, SoundEvents.ENTITY_PLAYER_LEVELUP);
gui.close();
Expand All @@ -129,14 +137,20 @@ public static DisplayElement resetButton(TaterBoxGui gui) {

public static class TaterGuiElement extends GuiElement {
protected final boolean found;
protected final boolean collectable;

public TaterGuiElement(ItemStack item, ClickCallback callback, boolean found) {
public TaterGuiElement(ItemStack item, ClickCallback callback, boolean found, boolean collectable) {
super(item, callback);
this.found = found;
this.collectable = collectable;
}

public boolean isFound() {
return found;
public boolean shouldShow(boolean hideUnfound) {
if (this.found) {
return true;
}

return !hideUnfound && this.collectable;
}
}

Expand All @@ -145,6 +159,7 @@ public static class TaterGuiElementBuilder extends GuiElementBuilder {
protected static final Item UNFOUND_ICON = Items.POTATO;

protected boolean found;
protected boolean collectable;

public TaterGuiElementBuilder(Item item) {
super(item);
Expand All @@ -159,9 +174,14 @@ public TaterGuiElementBuilder setFound(boolean found) {
return this;
}

public TaterGuiElementBuilder setCollectable(boolean collectable) {
this.collectable = collectable;
return this;
}

@Override
public TaterGuiElement build() {
return new TaterGuiElement(asStack(), callback, found);
return new TaterGuiElement(asStack(), callback, found, collectable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
Expand All @@ -32,9 +35,9 @@
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;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.lobby.gui.TaterBoxGui;
import xyz.nucleoid.extras.tag.NEBlockTags;
import xyz.nucleoid.packettweaker.PacketContext;
import xyz.nucleoid.server.translations.api.Localization;

Expand All @@ -57,7 +60,7 @@ public TaterBoxItem(Settings settings) {
private MutableText getTitle(ServerPlayerEntity player) {
Text name = this.getName();
int count = PlayerLobbyState.get(player).collectedTaters.size();
int max = TinyPotatoBlock.TATERS.size();
long max = getCollectableTaterCount(player.getRegistryManager());

return Text.translatable("text.nucleoid_extras.tater_box.title", name, count, max);
}
Expand Down Expand Up @@ -120,13 +123,13 @@ private void openTaterBoxUi(ServerPlayerEntity user, ItemStack stack, Hand hand)
var state = PlayerLobbyState.get(user);
List<GuiElementInterface> taters = new ArrayList<>();

taters.add(createGuiElement(stack, user, hand, Items.BARRIER, NONE_TEXT, null, true));
taters.add(createGuiElement(stack, user, hand, Items.BARRIER, NONE_TEXT, null, true, true));

getSortedTaterStream(user)
.map(tater -> {
boolean found = state.collectedTaters.contains(tater);

return createGuiElement(stack, user, hand, tater, tater.getName(), tater.getRegistryEntry(), found);
return createGuiElement(stack, user, hand, tater, tater.getName(), tater.getRegistryEntry(), found, tater.isCollectable());
})
.forEachOrdered(taters::add);

Expand All @@ -143,11 +146,12 @@ private void openTaterBoxUi(ServerPlayerEntity user, ItemStack stack, Hand hand)
}
}

private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEntity user, Hand hand, ItemConvertible icon, Text text, RegistryEntry<Block> tater, boolean found) {
private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEntity user, Hand hand, ItemConvertible icon, Text text, RegistryEntry<Block> tater, boolean found, boolean collectable) {
var guiElementBuilder = new TaterBoxGui.TaterGuiElementBuilder(icon.asItem());
guiElementBuilder.setName(text);
guiElementBuilder.setRarity(Rarity.COMMON);
guiElementBuilder.setFound(found);
guiElementBuilder.setCollectable(collectable);
guiElementBuilder.hideDefaultTooltip();
guiElementBuilder.setCallback((index, type, action, gui) -> {
ItemStack newStack = hand == null ? stack : user.getStackInHand(hand);
Expand Down Expand Up @@ -226,12 +230,27 @@ public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> to
tooltip.add(Text.translatable("text.nucleoid_extras.tater_box.selected", selectedName).formatted(Formatting.GRAY));

int count = owner != null && owner.getPlayer() != null ? PlayerLobbyState.get(owner.getPlayer()).collectedTaters.size() : 0;
int max = CubicPotatoBlock.TATERS.size();
String percent = String.format("%.2f", count / (double) max * 100);
long max = getCollectableTaterCount(context.getRegistryLookup());
String percent = String.format("%.2f", max == 0 ? 0 : count / (double) max * 100);

tooltip.add(Text.translatable("text.nucleoid_extras.tater_box.completion", count, max, percent).formatted(Formatting.GRAY));
}

public static Stream<TinyPotatoBlock> getCollectableTaters(RegistryWrapper.WrapperLookup registries) {
return registries
.getOrThrow(RegistryKeys.BLOCK)
.getOptional(NEBlockTags.COLLECTABLE_TATERS)
.map(RegistryEntryList::stream)
.orElseGet(Stream::empty)
.map(RegistryEntry::value)
.filter(block -> block instanceof TinyPotatoBlock)
.map(block -> (TinyPotatoBlock) block);
}

public static long getCollectableTaterCount(RegistryWrapper.WrapperLookup registries) {
return getCollectableTaters(registries).count();
}

public static Stream<TinyPotatoBlock> getSortedTaterStream(ServerPlayerEntity player) {
return TinyPotatoBlock.TATERS.stream()
.sorted(Comparator.comparing(tater -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/xyz/nucleoid/extras/tag/NEBlockTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xyz.nucleoid.extras.NucleoidExtras;

public final class NEBlockTags {
public static final TagKey<Block> COLLECTABLE_TATERS = of("collectable_taters");
public static final TagKey<Block> LUCKY_TATER_DROPS = of("lucky_tater_drops");
public static final TagKey<Block> NON_VIBRATING_TATERS = of("non_vibrating_taters");
public static final TagKey<Block> VIRAL_TATERS = of("viral_taters");
Expand Down
Loading