Skip to content

Commit

Permalink
Some misc migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Apr 19, 2024
1 parent c9edb54 commit 2506af7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ private static int startGame(CommandContext<CommandSourceStack> context) throws
requiredGoals,
excludedTags,
requireClient,
server.getLootData()
server.reloadableRegistries().lookup()
);
} catch (Exception e) {
Bingo.LOGGER.error("Error generating bingo board", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
package io.github.gaming32.bingo.data.icons;

import com.mojang.serialization.MapCodec;
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.effect.MobEffectInstance;
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.PotionUtils;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.alchemy.Potions;

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

public static EffectIcon of(MobEffect effect) {
Potion potion = Potions.EMPTY;
outer: for (Potion p : BuiltInRegistries.POTION) {
for (MobEffectInstance e : p.getEffects()) {
if (e.getEffect() == effect) {
potion = p;
break outer;
}
}
}

return new EffectIcon(effect, potion);
return new EffectIcon(
effect, BuiltInRegistries.POTION.holders()
.filter(p -> p.value().getEffects().stream().anyMatch(e -> e.getEffect() == effect))
.findFirst()
.map(h -> (Holder<Potion>)h)
.orElse(Potions.WATER)
);
}

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

@Override
Expand Down
19 changes: 8 additions & 11 deletions common/src/main/java/io/github/gaming32/bingo/game/ActiveGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import io.github.gaming32.bingo.data.icons.GoalIcon;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.CriterionValidator;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.LootDataResolver;
import net.minecraft.world.item.component.ItemLore;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -45,16 +46,12 @@ public boolean hasProgress() {

public ItemStack toSingleStack() {
final ItemStack result = icon.item().copy();
result.setHoverName(name);
tooltip.ifPresent(component -> {
final ListTag lore = new ListTag();
lore.add(StringTag.valueOf(Component.Serializer.toJson(component)));
result.getOrCreateTagElement("display").put("Lore", lore);
});
result.set(DataComponents.ITEM_NAME, name);
tooltip.ifPresent(component -> result.set(DataComponents.LORE, new ItemLore(List.of(component))));
return result;
}

public void validateAndLog(LootDataResolver lootData) {
public void validateAndLog(HolderGetter.Provider lootData) {
final ProblemReporter.Collector collector = new ProblemReporter.Collector();
validate(collector, lootData);
final Multimap<String, String> errors = collector.get();
Expand All @@ -68,7 +65,7 @@ public void validateAndLog(LootDataResolver lootData) {
}
}

public void validate(ProblemReporter reporter, LootDataResolver lootData) {
public void validate(ProblemReporter reporter, HolderGetter.Provider lootData) {
criteria.forEach((key, criterion) -> {
final CriterionValidator validator = new CriterionValidator(reporter.forChild(key), lootData);
criterion.triggerInstance().validate(validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.core.HolderGetter;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.storage.loot.LootDataResolver;
import org.apache.commons.lang3.text.WordUtils;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -79,7 +79,7 @@ public static BingoBoard generate(
List<BingoGoal.Holder> requiredGoals,
Set<BingoTag.Holder> excludedTags,
boolean allowsClientRequired,
@Nullable LootDataResolver lootData
@Nullable HolderGetter.Provider lootData
) {
final BingoBoard board = new BingoBoard(size);
final BingoGoal.Holder[] generatedSheet = generateGoals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
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;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PotionItem;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.item.alchemy.PotionContents;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
Expand Down Expand Up @@ -43,7 +44,7 @@ public record TriggerInstance(
) implements SimpleInstance {
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player),
EntityPredicate.ADVANCEMENT_CODEC.optionalFieldOf("player").forGetter(TriggerInstance::player),
ExtraCodecs.POSITIVE_INT.fieldOf("min_count").forGetter(TriggerInstance::minCount)
).apply(instance, TriggerInstance::new)
);
Expand All @@ -55,12 +56,13 @@ public static Criterion<TriggerInstance> differentPotions(int minCount) {
}

public boolean matches(Inventory inventory, ProgressListener<TriggerInstance> progressListener) {
final Set<String> discovered = new HashSet<>();
final Set<Holder<Potion>> 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 Potion potion = PotionUtils.getPotion(item);
if (potion != Potions.EMPTY && discovered.add(potion.getName("")) && discovered.size() >= minCount) {
final PotionContents potion = item.get(DataComponents.POTION_CONTENTS);
if (potion == null || potion.potion().isEmpty()) continue;
if (discovered.add(potion.potion().get()) && discovered.size() >= minCount) {
progressListener.update(this, minCount, minCount);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.gaming32.bingo.event.InventoryChangedCallback;
import io.github.gaming32.bingo.triggers.progress.SimpleProgressibleCriterionTrigger;
import io.github.gaming32.bingo.util.BingoUtil;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.DyeableArmorItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.DyedItemColor;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down Expand Up @@ -47,26 +49,26 @@ public record TriggerInstance(
) implements SimpleInstance {
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player),
ExtraCodecs.strictOptionalField(Codec.BOOL, "allow_uncolored", true).forGetter(TriggerInstance::allowUncolored),
EntityPredicate.ADVANCEMENT_CODEC.optionalFieldOf("player").forGetter(TriggerInstance::player),
Codec.BOOL.optionalFieldOf("allow_uncolored", true).forGetter(TriggerInstance::allowUncolored),
ExtraCodecs.POSITIVE_INT.fieldOf("min_count").forGetter(TriggerInstance::minCount),
ExtraCodecs.strictOptionalField(ItemPredicate.CODEC, "item_predicate").forGetter(TriggerInstance::itemPredicate)
ItemPredicate.CODEC.optionalFieldOf("item_predicate").forGetter(TriggerInstance::itemPredicate)
).apply(instance, TriggerInstance::new)
);

public boolean matches(Inventory inventory, ProgressListener<TriggerInstance> progressListener) {
final IntSet discovered = new IntOpenHashSet();
for (int i : Inventory.ALL_ARMOR_SLOTS) {
final ItemStack item = inventory.getArmor(i);
if (itemPredicate.isPresent() && !itemPredicate.get().matches(item)) {
if (itemPredicate.isPresent() && !itemPredicate.get().test(item)) {
continue;
}
if (item.getItem() instanceof DyeableArmorItem dyeableItem) {
final int color = dyeableItem.getColor(item);
if (!allowUncolored && color == dyeableItem.getColor(new ItemStack(dyeableItem))) {
if (BingoUtil.isDyeableArmor(item.getItem())) {
final DyedItemColor color = item.get(DataComponents.DYED_COLOR);
if (!allowUncolored && color == null) {
continue;
}
if (discovered.add(color) && discovered.size() >= minCount) {
if (discovered.add(color != null ? color.rgb() : -1) && discovered.size() >= minCount) {
progressListener.update(this, minCount, minCount);
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/io/github/gaming32/bingo/util/BingoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.Item;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -186,6 +189,14 @@ public static Component getDisplayName(PlayerTeam team, PlayerList playerList) {
return team.getDisplayName();
}

public static boolean isDyeableArmor(Item item) {
return item instanceof ArmorItem armor && armor.getMaterial()
.value()
.layers()
.stream()
.anyMatch(ArmorMaterial.Layer::dyeable);
}

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

0 comments on commit 2506af7

Please sign in to comment.