Skip to content

Commit

Permalink
It compiles!
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Apr 20, 2024
1 parent a7e1285 commit ee57a64
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 252 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package io.github.gaming32.bingo.data.icons;

import com.mojang.serialization.MapCodec;
import io.github.gaming32.bingo.util.BingoUtil;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.alchemy.Potions;

public record EffectIcon(MobEffect effect, Holder<Potion> potion) implements GoalIcon {
public record EffectIcon(Holder<MobEffect> effect, Holder<Potion> potion) implements GoalIcon {
public static final MapCodec<EffectIcon> CODEC = BuiltInRegistries.MOB_EFFECT
.byNameCodec()
.holderByNameCodec()
.fieldOf("effect")
.xmap(EffectIcon::of, EffectIcon::effect);

public static EffectIcon of(MobEffect effect) {
public static EffectIcon of(Holder<MobEffect> effect) {
return new EffectIcon(
effect, BuiltInRegistries.POTION.holders()
.filter(p -> p.value().getEffects().stream().anyMatch(e -> e.getEffect() == effect))
Expand All @@ -29,9 +28,7 @@ public static EffectIcon of(MobEffect effect) {

@Override
public ItemStack item() {
final ItemStack result = new ItemStack(Items.POTION);
result.set(DataComponents.POTION_CONTENTS, new PotionContents(potion));
return result;
return BingoUtil.setPotion(new ItemStack(Items.POTION), potion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ProgressTracker {
Codec<ProgressTracker> CODEC = ProgressTrackerType.REGISTER
.registry()
.byNameCodec()
.dispatch(ProgressTracker::type, type -> type.codec().codec());
.dispatch(ProgressTracker::type, ProgressTrackerType::codec);

default DataResult<ProgressTracker> validate(BingoGoal goal) {
return DataResult.success(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@ private BingoItemTags() {
public static final TagKey<Item> CONCRETE = create("concrete");
public static final TagKey<Item> DEAD_CORAL_BLOCKS = create("dead_coral_blocks");
public static final TagKey<Item> DIAMOND_IN_NAME = create("diamond_in_name");
public static final TagKey<Item> DYES = create("dyes");
public static final TagKey<Item> FISHING_JUNK = create("fishing_junk");
public static final TagKey<Item> FISHING_TREASURE = create("fishing_treasure");
public static final TagKey<Item> FISH_BUCKETS = create("fish_buckets");
public static final TagKey<Item> FLOWERS = create("flowers");
public static final TagKey<Item> FOOD = create("food");
public static final TagKey<Item> GLAZED_TERRACOTTA = create("glazed_terracotta");
public static final TagKey<Item> GOLD_IN_NAME = create("gold_in_name");
public static final TagKey<Item> LIVING_CORAL_BLOCKS = create("living_coral_blocks");
public static final TagKey<Item> MEAT = create("meat");
public static final TagKey<Item> NOT_MEAT = create("not_meat");
public static final TagKey<Item> ORES = create("ores");
public static final TagKey<Item> SHIELDS = create("shields");
public static final TagKey<Item> SLABS = create("slabs");
public static final TagKey<Item> STAIRS = create("stairs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;
Expand Down Expand Up @@ -56,13 +55,13 @@ public static Criterion<TriggerInstance> differentPotions(int minCount) {
}

public boolean matches(Inventory inventory, ProgressListener<TriggerInstance> progressListener) {
final Set<Holder<Potion>> discovered = new HashSet<>();
final Set<String> discovered = new HashSet<>();
for (int i = 0, l = inventory.getContainerSize(); i < l; i++) {
final ItemStack item = inventory.getItem(i);
if (item.getItem() instanceof PotionItem) {
final PotionContents potion = item.get(DataComponents.POTION_CONTENTS);
if (potion == null || potion.potion().isEmpty()) continue;
if (discovered.add(potion.potion().get()) && discovered.size() >= minCount) {
if (discovered.add(Potion.getName(potion.potion(), "")) && discovered.size() >= minCount) {
progressListener.update(this, minCount, minCount);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Optional;
import java.util.Set;

@Deprecated(forRemoval = true)
public class HasSomeFoodItemsTrigger extends SimpleProgressibleCriterionTrigger<HasSomeFoodItemsTrigger.TriggerInstance> {
static {
InventoryChangedCallback.HANDLERS.add((player, inventory) -> BingoTriggers.HAS_SOME_FOOD_ITEMS.get().trigger(player, inventory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.util.ExtraCodecs;

import java.lang.reflect.Array;
import java.util.Objects;
Expand Down Expand Up @@ -69,8 +68,7 @@ public static <T> Codec<Optional<T>> optional(Codec<T> codec) {
}

public static Codec<Integer> atLeast(int minInclusive) {
return ExtraCodecs.validate(
Codec.INT,
return Codec.INT.validate(
value -> value >= minInclusive
? DataResult.success(value)
: DataResult.error(() -> "Value must be greater than " + minInclusive + ": " + value)
Expand Down Expand Up @@ -99,8 +97,7 @@ public static <A extends Enum<A>> Codec<Set<A>> enumSetOf(Codec<A> elementCodec)
}

public static <A> Codec<A> exactly(A value, Codec<A> codec) {
return ExtraCodecs.validate(
codec,
return codec.validate(
a -> Objects.equals(a, value)
? DataResult.success(a)
: DataResult.error(() -> "Value must equal " + value + ". Got " + a));
Expand All @@ -111,8 +108,8 @@ public static Codec<Integer> exactly(int value) {
}

/**
* {@link ExtraCodecs#withAlternative(Codec, Codec)} will always encode with {@code first}, which will trip any
* {@link ExtraCodecs#validate(Codec, Function)}. This codec will try to encode with both, and will return the
* {@link Codec#withAlternative(Codec, Codec)} will always encode with {@code first}, which will trip any
* {@link Codec#validate(Function)}. This codec will try to encode with both, and will return the
* first successful encoding.
*/
public static <A> Codec<A> firstValid(Codec<A> first, Codec<A> second) {
Expand All @@ -127,25 +124,25 @@ public static <A> Codec<Set<A>> minifiedSet(Codec<A> elementCodec) {
}

public static <A> MapCodec<Set<A>> minifiedSetField(Codec<A> elementCodec, String name) {
return ExtraCodecs.strictOptionalField(minifiedSet(elementCodec), name, ImmutableSet.of());
return minifiedSet(elementCodec).optionalFieldOf(name, ImmutableSet.of());
}

public static MapCodec<OptionalInt> optionalInt(String name) {
return ExtraCodecs.strictOptionalField(Codec.INT, name).xmap(
return Codec.INT.optionalFieldOf(name).xmap(
opt -> opt.map(OptionalInt::of).orElseGet(OptionalInt::empty),
opt -> opt.isPresent() ? Optional.of(opt.getAsInt()) : Optional.empty()
);
}

public static MapCodec<Dynamic<?>> optionalDynamicField(String name) {
return ExtraCodecs.strictOptionalField(Codec.PASSTHROUGH, name).xmap(
return Codec.PASSTHROUGH.optionalFieldOf(name).xmap(
opt -> opt.orElse(EMPTY_DYNAMIC),
dyn -> dyn.getValue() == dyn.getOps().empty() ? Optional.empty() : Optional.of(dyn)
);
}

public static MapCodec<Dynamic<?>> optionalDynamicField(String name, Dynamic<?> defaultValue) {
return ExtraCodecs.strictOptionalField(Codec.PASSTHROUGH, name).xmap(
return Codec.PASSTHROUGH.optionalFieldOf(name).xmap(
opt -> opt.orElse(defaultValue),
dyn -> dyn.convert(defaultValue.getOps()).getValue().equals(defaultValue.getValue()) ? Optional.empty() : Optional.of(dyn)
);
Expand Down
22 changes: 15 additions & 7 deletions common/src/main/java/io/github/gaming32/bingo/util/BingoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;
import it.unimi.dsi.fastutil.Hash;
import net.minecraft.Util;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
Expand All @@ -25,6 +25,9 @@
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -119,19 +122,19 @@ public static ListTag list(List<? extends Tag> nbt) {
}

public static <T> JsonElement toJsonElement(Codec<T> codec, T obj) {
return Util.getOrThrow(codec.encodeStart(JsonOps.INSTANCE, obj), IllegalStateException::new);
return codec.encodeStart(JsonOps.INSTANCE, obj).getOrThrow(IllegalStateException::new);
}

public static <T> JsonObject toJsonObject(Codec<T> codec, T obj) {
return toJsonElement(codec, obj).getAsJsonObject();
}

public static <T> T fromJsonElement(Codec<T> codec, JsonElement element) throws JsonParseException {
return Util.getOrThrow(codec.parse(JsonOps.INSTANCE, element), JsonParseException::new);
return codec.parse(JsonOps.INSTANCE, element).getOrThrow(JsonParseException::new);
}

public static <T> Tag toTag(Codec<T> codec, T obj) {
return Util.getOrThrow(codec.encodeStart(NbtOps.INSTANCE, obj), IllegalStateException::new);
return codec.encodeStart(NbtOps.INSTANCE, obj).getOrThrow(IllegalStateException::new);
}

public static <T> CompoundTag toCompoundTag(Codec<T> codec, T obj) {
Expand All @@ -143,19 +146,19 @@ public static <T> CompoundTag toCompoundTag(Codec<T> codec, T obj) {
}

public static <T> T fromTag(Codec<T> codec, Tag tag) {
return Util.getOrThrow(codec.parse(NbtOps.INSTANCE, tag), IllegalArgumentException::new);
return codec.parse(NbtOps.INSTANCE, tag).getOrThrow(IllegalArgumentException::new);
}

public static <T> Dynamic<?> toDynamic(Codec<T> codec, T obj) {
return toDynamic(codec, obj, BingoCodecs.DEFAULT_OPS);
}

public static <T, O> Dynamic<O> toDynamic(Codec<T> codec, T obj, DynamicOps<O> ops) {
return new Dynamic<>(ops, Util.getOrThrow(codec.encodeStart(ops, obj), IllegalStateException::new));
return new Dynamic<>(ops, codec.encodeStart(ops, obj).getOrThrow(IllegalStateException::new));
}

public static <T> T fromDynamic(Codec<T> codec, Dynamic<?> dynamic) throws IllegalArgumentException {
return Util.getOrThrow(codec.parse(dynamic), IllegalArgumentException::new);
return codec.parse(dynamic).getOrThrow(IllegalArgumentException::new);
}

public static <T> List<T> addToList(List<T> a, T b) {
Expand Down Expand Up @@ -197,6 +200,11 @@ public static boolean isDyeableArmor(Item item) {
.anyMatch(ArmorMaterial.Layer::dyeable);
}

public static ItemStack setPotion(ItemStack stack, Holder<Potion> potion) {
stack.set(DataComponents.POTION_CONTENTS, new PotionContents(potion));
return stack;
}

public static boolean collidesWithProjectedBox(Vec3 entityOrigin, Vec3 boxNormal, double boxWidth) {
// Distance from origin to the closest point on the boxNormal line
final double pointDistance = entityOrigin.dot(boxNormal) / boxNormal.lengthSqr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.gaming32.bingo.data.BingoGoal;
import io.github.gaming32.bingo.util.BingoUtil;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
Expand All @@ -19,29 +20,33 @@

public class BingoGoalProvider implements DataProvider {
private final PackOutput.PathProvider pathProvider;
private final CompletableFuture<HolderLookup.Provider> registriesFuture;

public BingoGoalProvider(FabricDataOutput output) {
public BingoGoalProvider(FabricDataOutput output, CompletableFuture<HolderLookup.Provider> registriesFuture) {
this.pathProvider = output.createPathProvider(PackOutput.Target.DATA_PACK, "bingo/goals");
this.registriesFuture = registriesFuture;
}

@Override
@NotNull
public CompletableFuture<?> run(CachedOutput output) {
Set<ResourceLocation> existingGoals = new HashSet<>();
List<CompletableFuture<?>> generators = new ArrayList<>();
return registriesFuture.thenCompose(registries -> {
Set<ResourceLocation> existingGoals = new HashSet<>();
List<CompletableFuture<?>> generators = new ArrayList<>();

Consumer<BingoGoal.Holder> goalAdder = goal -> {
if (!existingGoals.add(goal.id())) {
throw new IllegalArgumentException("Duplicate goal " + goal.id());
} else {
Path path = pathProvider.json(goal.id());
generators.add(DataProvider.saveStable(output, BingoUtil.toJsonElement(BingoGoal.CODEC, goal.goal()), path));
}
};
Consumer<BingoGoal.Holder> goalAdder = goal -> {
if (!existingGoals.add(goal.id())) {
throw new IllegalArgumentException("Duplicate goal " + goal.id());
} else {
Path path = pathProvider.json(goal.id());
generators.add(DataProvider.saveStable(output, BingoUtil.toJsonElement(BingoGoal.CODEC, goal.goal()), path));
}
};

addGoals(goalAdder);
addGoals(goalAdder, registries);

return CompletableFuture.allOf(generators.toArray(CompletableFuture[]::new));
return CompletableFuture.allOf(generators.toArray(CompletableFuture[]::new));
});
}

@Override
Expand All @@ -50,11 +55,11 @@ public String getName() {
return "Bingo goals";
}

private void addGoals(Consumer<BingoGoal.Holder> goalAdder) {
new VeryEasyGoalProvider(goalAdder).addGoals();
new EasyGoalProvider(goalAdder).addGoals();
new MediumGoalProvider(goalAdder).addGoals();
new HardGoalProvider(goalAdder).addGoals();
new VeryHardGoalProvider(goalAdder).addGoals();
private void addGoals(Consumer<BingoGoal.Holder> goalAdder, HolderLookup.Provider registries) {
new VeryEasyGoalProvider(goalAdder).addGoals(registries);
new EasyGoalProvider(goalAdder).addGoals(registries);
new MediumGoalProvider(goalAdder).addGoals(registries);
new HardGoalProvider(goalAdder).addGoals(registries);
new VeryHardGoalProvider(goalAdder).addGoals(registries);
}
}
Loading

0 comments on commit ee57a64

Please sign in to comment.