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

Fix advancement criterion format regressions #176

Merged
merged 1 commit into from
Jun 9, 2024
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
6 changes: 3 additions & 3 deletions src/main/java/xyz/nucleoid/extras/lobby/PlayerLobbyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ private ActionResult collectTater(Block block, ItemStack stack, PlayerEntity pla
}

player.sendMessage(message, true);
triggerCollectCriterion((ServerPlayerEntity) player, Registries.BLOCK.getId(tater), this.collectedTaters.size());
triggerCollectCriterion((ServerPlayerEntity) player, tater, this.collectedTaters.size());

return alreadyAdded ? ActionResult.FAIL : ActionResult.SUCCESS;
}

private static void triggerCollectCriterion(ServerPlayerEntity player, Identifier taterId, int count) {
NECriteria.TATER_COLLECTED.trigger(player, taterId, count);
private static void triggerCollectCriterion(ServerPlayerEntity player, TinyPotatoBlock tater, int count) {
NECriteria.TATER_COLLECTED.trigger(player, tater, count);
}

private static boolean isFickle(ActionResult result, Block block, PlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package xyz.nucleoid.extras.lobby.block.tater;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import eu.pb4.polymer.core.api.block.PolymerBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.dynamic.Codecs;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
Expand All @@ -21,6 +24,14 @@
import java.util.List;

public abstract class TinyPotatoBlock extends Block implements PolymerBlock {
public static final Codec<RegistryEntry<Block>> ENTRY_CODEC = Codecs.validate(Registries.BLOCK.createEntryCodec(), block -> {
if (block.value() instanceof TinyPotatoBlock) {
return DataResult.success(block);
}

return DataResult.error(() -> "Not a tater: " + block);
});

public static final List<TinyPotatoBlock> TATERS = new ArrayList<>();

private final ParticleEffect particleEffect;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package xyz.nucleoid.extras.lobby.criterion;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancement.criterion.AbstractCriterion;
import net.minecraft.block.Block;
import net.minecraft.predicate.entity.LootContextPredicate;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import xyz.nucleoid.extras.lobby.block.tater.CubicPotatoBlock;
import net.minecraft.util.dynamic.Codecs;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;

import java.util.Optional;

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

Expand All @@ -27,33 +25,23 @@ public Codec<Conditions> getConditionsCodec() {
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static class Conditions implements AbstractCriterion.Conditions {
public static final Codec<Conditions> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Identifier.CODEC.fieldOf("tater").forGetter(Conditions::getTater),
Codec.INT.optionalFieldOf("count").forGetter(i -> i.optionalCount)
Codecs.createStrictOptionalFieldCodec(TinyPotatoBlock.ENTRY_CODEC, "tater").forGetter(i -> i.tater),
Codecs.createStrictOptionalFieldCodec(TaterCount.CODEC, "count").forGetter(i -> i.count)
).apply(instance, Conditions::new));

private final Identifier tater;
private final Integer count;
private final Optional<Integer> optionalCount;

public Conditions(Identifier tater, Optional<Integer> count) {
this.tater = tater;
this.count = count.orElse(TinyPotatoBlock.TATERS.size());
this.optionalCount = count;
}

public Identifier getTater() {
return tater;
}

public Integer getCount() {
return count;
}

public boolean matches(Identifier tater, int count) {
boolean taterMatches = getTater() == null || getTater().equals(tater);
boolean countMatches = getCount() == null || getCount() <= count;
return taterMatches && countMatches;
}
private final Optional<RegistryEntry<Block>> tater;
private final Optional<TaterCount> count;

public Conditions(Optional<RegistryEntry<Block>> tater, Optional<TaterCount> count) {
this.tater = tater;
this.count = count;
}

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

@Override
public Optional<LootContextPredicate> player() {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/xyz/nucleoid/extras/lobby/criterion/TaterCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xyz.nucleoid.extras.lobby.criterion;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import net.minecraft.util.dynamic.Codecs;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;

import java.util.function.Function;

public sealed interface TaterCount {
static final Codec<TaterCount> CODEC = Codecs.either(Value.CODEC, All.CODEC).xmap(either -> {
return either.map(Function.identity(), Function.identity());
}, count -> {
return count instanceof Value ? Either.left((Value) count) : Either.right((All) count);
});

int count();

default boolean matches(int count) {
return this.count() <= count;
}

record Value(int count) implements TaterCount {
private static final Codec<Value> CODEC = Codecs.NONNEGATIVE_INT.xmap(Value::new, Value::count);

public Value {
if (count < 0) {
throw new IllegalArgumentException("Count must be non-negative: " + count);
}
}
}

record All() implements TaterCount {
private static final String STRING = "all";
private static final All INSTANCE = new All();

private static final Codec<All> CODEC = Codec.STRING.comapFlatMap(string -> {
if (STRING.equals(string)) {
return DataResult.success(All.INSTANCE);
}

return DataResult.error(() -> "Not an 'all' count");
}, string -> STRING);

@Override
public int count() {
return TinyPotatoBlock.TATERS.size();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package xyz.nucleoid.extras.lobby.criterion;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancement.criterion.AbstractCriterion;
import net.minecraft.block.Block;
import net.minecraft.predicate.entity.LootContextPredicate;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;

import java.util.Calendar;
import java.util.Date;
Expand All @@ -19,7 +18,7 @@
public class WearTaterCriterion extends AbstractCriterion<WearTaterCriterion.Conditions> {
public static final Calendar CALENDAR = Calendar.getInstance();

public void trigger(ServerPlayerEntity player, Identifier tater) {
public void trigger(ServerPlayerEntity player, TinyPotatoBlock tater) {
CALENDAR.setTime(new Date());
this.trigger(player, conditions -> conditions.matches(tater, CALENDAR.get(Calendar.DAY_OF_WEEK)));
}
Expand Down Expand Up @@ -55,16 +54,17 @@ public Codec<Conditions> getConditionsCodec() {
return Conditions.CODEC;
}

public record Conditions(Identifier tater, int dayOfWeek) implements AbstractCriterion.Conditions {
public record Conditions(Optional<RegistryEntry<Block>> tater, Optional<Integer> dayOfWeek) implements AbstractCriterion.Conditions {
private static final Codec<Integer> DAY_OF_WEEK_CODEC = Codec.STRING.xmap(WearTaterCriterion::dayOfWeekToInt, WearTaterCriterion::dayOfWeekToString);

public static final Codec<Conditions> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Identifier.CODEC.fieldOf("tater").forGetter(Conditions::tater),
Codec.STRING.xmap(WearTaterCriterion::dayOfWeekToInt, WearTaterCriterion::dayOfWeekToString)
.fieldOf("day_of_week").forGetter(Conditions::dayOfWeek)
Codecs.createStrictOptionalFieldCodec(TinyPotatoBlock.ENTRY_CODEC, "tater").forGetter(Conditions::tater),
Codecs.createStrictOptionalFieldCodec(DAY_OF_WEEK_CODEC, "day_of_week").forGetter(Conditions::dayOfWeek)
).apply(instance, Conditions::new));

public boolean matches(Identifier tater, int dayOfWeek) {
boolean taterMatches = tater() == null || tater().equals(tater);
boolean dayOfWeekMatches = dayOfWeek() == dayOfWeek;
public boolean matches(TinyPotatoBlock tater, int dayOfWeek) {
boolean taterMatches = this.tater.isEmpty() || this.tater.get().value() == tater;
boolean dayOfWeekMatches = this.dayOfWeek.isEmpty() || this.dayOfWeek.get() == dayOfWeek;
return taterMatches && dayOfWeekMatches;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile
if (helmet.getItem() instanceof TaterBoxItem) {
if (TaterBoxItem.getSelectedTater(helmet) instanceof CubicPotatoBlock tinyPotatoBlock) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
NECriteria.WEAR_TATER.trigger(player, TaterBoxItem.getSelectedTaterId(helmet));
NECriteria.TATER_COLLECTED.trigger(player, TaterBoxItem.getSelectedTaterId(helmet), PlayerLobbyState.get(this).collectedTaters.size());
NECriteria.WEAR_TATER.trigger(player, tinyPotatoBlock);
NECriteria.TATER_COLLECTED.trigger(player, tinyPotatoBlock, PlayerLobbyState.get(this).collectedTaters.size());
if (this.age % tinyPotatoBlock.getPlayerParticleRate(player) == 0) {
tinyPotatoBlock.spawnPlayerParticles(player);
}
Expand Down
Loading