From 64f5727f1a070ea842f3fd660064c32d270bc05b Mon Sep 17 00:00:00 2001 From: cheaterpaul Date: Thu, 3 Oct 2024 15:51:59 +0200 Subject: [PATCH] move default tags back to faction builder --- .../api/entity/factions/IFactionBuilder.java | 10 ++++++ .../factions/IPlayableFactionBuilder.java | 9 +++++ .../api/event/AddFactionTagEvent.java | 6 ++-- .../teamlapen/vampirism/core/ModFactions.java | 34 ++++++++----------- .../vampirism/core/RegistryManager.java | 1 - .../entity/factions/FactionBuilder.java | 23 ++++++++++++- .../entity/factions/FactionTags.java | 13 ++++++- .../factions/PlayableFactionBuilder.java | 17 +++++++++- 8 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/api/java/de/teamlapen/vampirism/api/entity/factions/IFactionBuilder.java b/src/api/java/de/teamlapen/vampirism/api/entity/factions/IFactionBuilder.java index 1ef493d97..b066e3ea1 100644 --- a/src/api/java/de/teamlapen/vampirism/api/entity/factions/IFactionBuilder.java +++ b/src/api/java/de/teamlapen/vampirism/api/entity/factions/IFactionBuilder.java @@ -1,7 +1,10 @@ package de.teamlapen.vampirism.api.entity.factions; import net.minecraft.ChatFormatting; +import net.minecraft.core.Registry; import net.minecraft.network.chat.TextColor; +import net.minecraft.resources.ResourceKey; +import net.minecraft.tags.TagKey; import org.jetbrains.annotations.NotNull; public interface IFactionBuilder { @@ -60,6 +63,13 @@ public interface IFactionBuilder { */ IFactionBuilder namePlural(@NotNull String namePluralKey); + IFactionBuilder addTag(ResourceKey key, TagKey tag); + + /** + * Adds a tag for the given registry + */ + IFactionBuilder addRegistryTag(ResourceKey> key, TagKey tag); + /** * finish the building and registers the faction with values from the builder * diff --git a/src/api/java/de/teamlapen/vampirism/api/entity/factions/IPlayableFactionBuilder.java b/src/api/java/de/teamlapen/vampirism/api/entity/factions/IPlayableFactionBuilder.java index f985b0852..b812c1b4c 100644 --- a/src/api/java/de/teamlapen/vampirism/api/entity/factions/IPlayableFactionBuilder.java +++ b/src/api/java/de/teamlapen/vampirism/api/entity/factions/IPlayableFactionBuilder.java @@ -3,7 +3,10 @@ import de.teamlapen.vampirism.api.entity.player.IFactionPlayer; import de.teamlapen.vampirism.api.items.IRefinementItem; import net.minecraft.ChatFormatting; +import net.minecraft.core.Registry; import net.minecraft.network.chat.TextColor; +import net.minecraft.resources.ResourceKey; +import net.minecraft.tags.TagKey; import org.jetbrains.annotations.NotNull; import java.util.function.Supplier; @@ -46,6 +49,12 @@ public interface IPlayableFactionBuilder> extends IF IPlayableFactionBuilder lord(ILordPlayerEntry lordPlayerBuilder); + @Override + IPlayableFactionBuilder addTag(ResourceKey key, TagKey tag); + + @Override + IPlayableFactionBuilder addRegistryTag(ResourceKey> key, TagKey tag); + @Override IPlayableFaction build(); } diff --git a/src/api/java/de/teamlapen/vampirism/api/event/AddFactionTagEvent.java b/src/api/java/de/teamlapen/vampirism/api/event/AddFactionTagEvent.java index 8a42c7e8a..f423061f2 100644 --- a/src/api/java/de/teamlapen/vampirism/api/event/AddFactionTagEvent.java +++ b/src/api/java/de/teamlapen/vampirism/api/event/AddFactionTagEvent.java @@ -12,7 +12,6 @@ import org.apache.logging.log4j.Logger; import java.util.Collections; -import java.util.HashMap; import java.util.Map; /** @@ -25,10 +24,11 @@ public class AddFactionTagEvent extends Event implements IModBusEvent { private static final Logger LOGGER = LogManager.getLogger(); private final Holder> faction; - private final Map, TagKey> tags = new HashMap<>(); + private final Map, TagKey> tags; - public AddFactionTagEvent(Holder> faction) { + public AddFactionTagEvent(Holder> faction, Map, TagKey> defaultTags) { this.faction = faction; + this.tags = defaultTags; } /** diff --git a/src/main/java/de/teamlapen/vampirism/core/ModFactions.java b/src/main/java/de/teamlapen/vampirism/core/ModFactions.java index ad92d6be8..f1d940527 100644 --- a/src/main/java/de/teamlapen/vampirism/core/ModFactions.java +++ b/src/main/java/de/teamlapen/vampirism/core/ModFactions.java @@ -7,6 +7,7 @@ import de.teamlapen.vampirism.REFERENCE; import de.teamlapen.vampirism.api.VampirismFactions; import de.teamlapen.vampirism.api.VampirismRegistries; +import de.teamlapen.vampirism.api.VampirismTags; import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction; import de.teamlapen.vampirism.api.entity.hunter.IBasicHunter; import de.teamlapen.vampirism.api.entity.minion.IMinionEntry; @@ -14,7 +15,6 @@ import de.teamlapen.vampirism.api.entity.player.neutral.INeutralPlayer; import de.teamlapen.vampirism.api.entity.player.vampire.IVampirePlayer; import de.teamlapen.vampirism.api.entity.vampire.IBasicVampire; -import de.teamlapen.vampirism.api.event.AddFactionTagEvent; import de.teamlapen.vampirism.api.items.IRefinementItem; import de.teamlapen.vampirism.api.registries.DeferredFaction; import de.teamlapen.vampirism.api.registries.DeferredFactionRegister; @@ -53,6 +53,13 @@ public class ModFactions { .refinementItem(IRefinementItem.AccessorySlotType.RING, ModItems.RING::get) .refinementItem(IRefinementItem.AccessorySlotType.OBI_BELT, ModItems.OBI_BELT::get) .village(VampireVillage.vampireVillage().build()) + .addRegistryTag(Registries.BIOME, ModBiomeTags.HasFaction.IS_VAMPIRE_BIOME) + .addRegistryTag(Registries.POINT_OF_INTEREST_TYPE, ModPoiTypeTags.IS_VAMPIRE) + .addRegistryTag(Registries.VILLAGER_PROFESSION, ModProfessionTags.IS_VAMPIRE) + .addRegistryTag(Registries.ENTITY_TYPE, ModEntityTags.VAMPIRE) + .addRegistryTag(VampirismRegistries.Keys.TASK, ModTaskTags.IS_VAMPIRE) + .addRegistryTag(VampirismRegistries.Keys.FACTION, ModFactionTags.IS_VAMPIRE) + .addTag(VampirismTags.CommonKeys.ACTION_DISABLES, ModEffectTags.DISABLES_ACTIONS_VAMPIRE) .lord(new LordPlayerBuilder() .lordTitle(new LordTitles.VampireTitles()).lordLevel(REFERENCE.HIGHEST_VAMPIRE_LORD).build()) .build()); @@ -66,6 +73,12 @@ public class ModFactions { .village(HunterVillage.hunterVillage().build()) .lord(new LordPlayerBuilder() .lordTitle(new LordTitles.HunterTitles()).lordLevel(REFERENCE.HIGHEST_HUNTER_LORD).build()) + .addRegistryTag(Registries.BIOME, ModBiomeTags.HasFaction.IS_HUNTER_BIOME) + .addRegistryTag(Registries.POINT_OF_INTEREST_TYPE, ModPoiTypeTags.IS_HUNTER) + .addRegistryTag(Registries.VILLAGER_PROFESSION, ModProfessionTags.IS_HUNTER) + .addRegistryTag(Registries.ENTITY_TYPE, ModEntityTags.HUNTER) + .addRegistryTag(VampirismRegistries.Keys.TASK, ModTaskTags.IS_HUNTER) + .addRegistryTag(VampirismRegistries.Keys.FACTION, ModFactionTags.IS_HUNTER) .build()); public static final DeferredFaction> NEUTRAL = FACTIONS.registerFaction(VampirismFactions.Keys.NEUTRAL.getPath(), () -> new PlayableFactionBuilder<>((Supplier>) (Object) ModAttachments.NEUTRAL_PLAYER) @@ -93,23 +106,4 @@ static void register(IEventBus bus) { MINIONS.register(bus); } - static void onFactionTags(AddFactionTagEvent event) { - if (event.getFaction().is(ModFactionTags.IS_VAMPIRE)) { - event.addRegistryTag(Registries.BIOME, ModBiomeTags.HasFaction.IS_VAMPIRE_BIOME); - event.addRegistryTag(Registries.POINT_OF_INTEREST_TYPE, ModPoiTypeTags.IS_VAMPIRE); - event.addRegistryTag(Registries.VILLAGER_PROFESSION, ModProfessionTags.IS_VAMPIRE); - event.addRegistryTag(Registries.ENTITY_TYPE, ModEntityTags.VAMPIRE); - event.addRegistryTag(VampirismRegistries.Keys.TASK, ModTaskTags.IS_VAMPIRE); - event.addRegistryTag(VampirismRegistries.Keys.FACTION, ModFactionTags.IS_VAMPIRE); - } - if (event.getFaction().is(ModFactionTags.IS_HUNTER)) { - event.addRegistryTag(Registries.BIOME, ModBiomeTags.HasFaction.IS_HUNTER_BIOME); - event.addRegistryTag(Registries.POINT_OF_INTEREST_TYPE, ModPoiTypeTags.IS_HUNTER); - event.addRegistryTag(Registries.VILLAGER_PROFESSION, ModProfessionTags.IS_HUNTER); - event.addRegistryTag(Registries.ENTITY_TYPE, ModEntityTags.HUNTER); - event.addRegistryTag(VampirismRegistries.Keys.TASK, ModTaskTags.IS_HUNTER); - event.addRegistryTag(VampirismRegistries.Keys.FACTION, ModFactionTags.IS_HUNTER); - } - } - } diff --git a/src/main/java/de/teamlapen/vampirism/core/RegistryManager.java b/src/main/java/de/teamlapen/vampirism/core/RegistryManager.java index 12610dfb0..2d2338dc4 100644 --- a/src/main/java/de/teamlapen/vampirism/core/RegistryManager.java +++ b/src/main/java/de/teamlapen/vampirism/core/RegistryManager.java @@ -90,7 +90,6 @@ public void registerModEventHandler() { this.eventBus.addListener(PackRepositories::registerPackRepository); this.eventBus.addListener(ModRecipes::registerCategories); this.eventBus.addListener(ModTiles::registerTileExtensions); - this.eventBus.addListener(ModFactions::onFactionTags); } public void registerForgeEventHandler() { diff --git a/src/main/java/de/teamlapen/vampirism/entity/factions/FactionBuilder.java b/src/main/java/de/teamlapen/vampirism/entity/factions/FactionBuilder.java index 868d39a93..3700e1fe4 100644 --- a/src/main/java/de/teamlapen/vampirism/entity/factions/FactionBuilder.java +++ b/src/main/java/de/teamlapen/vampirism/entity/factions/FactionBuilder.java @@ -6,10 +6,16 @@ import de.teamlapen.vampirism.api.entity.factions.IFactionEntity; import de.teamlapen.vampirism.api.entity.factions.IFactionVillage; import net.minecraft.ChatFormatting; +import net.minecraft.core.Registry; import net.minecraft.network.chat.TextColor; +import net.minecraft.resources.ResourceKey; +import net.minecraft.tags.TagKey; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; + public class FactionBuilder implements IFactionBuilder { protected int color = Color.WHITE.getRGB(); @@ -17,6 +23,7 @@ public class FactionBuilder implements IFactionBuilder protected @Nullable TextColor chatColor; protected String name; protected String namePlural; + protected final Map, TagKey> tags = new HashMap<>(); @Override public IFactionBuilder color(int color) { @@ -57,9 +64,23 @@ public IFactionBuilder namePlural(@NotNull String namePluralKey) { return this; } + @Override + public IFactionBuilder addTag(ResourceKey key, TagKey tag) { + this.tags.put(key, tag); + return this; + } + + @Override + public IFactionBuilder addRegistryTag(ResourceKey> key, TagKey tag) { + this.tags.put(key, tag); + return this; + } + @Override public @NotNull IFaction build() { - return new Faction<>(this); + var faction = new Faction<>(this); + FactionTags.addFaction(faction, this.tags); + return faction; } } diff --git a/src/main/java/de/teamlapen/vampirism/entity/factions/FactionTags.java b/src/main/java/de/teamlapen/vampirism/entity/factions/FactionTags.java index 1c9b1041a..f3a5e7ecb 100644 --- a/src/main/java/de/teamlapen/vampirism/entity/factions/FactionTags.java +++ b/src/main/java/de/teamlapen/vampirism/entity/factions/FactionTags.java @@ -12,6 +12,8 @@ import net.neoforged.fml.ModLoader; import org.jetbrains.annotations.ApiStatus; +import java.util.Collections; +import java.util.Map; import java.util.Optional; public class FactionTags { @@ -24,7 +26,8 @@ public static void collectTags() { } private static void addFaction(Holder> faction) { - ModLoader.postEventWithReturn(new AddFactionTagEvent(faction)).getTags().forEach((key, tag) -> registeredTags.put(faction.value(), key, tag)); + Map, TagKey> row = registeredTags.row(faction.value()); + ModLoader.postEventWithReturn(new AddFactionTagEvent(faction, Collections.unmodifiableMap(row))).getTags().forEach(row::putIfAbsent); } @SuppressWarnings("unchecked") @@ -46,4 +49,12 @@ public static Optional> getTag(IFaction faction, ResourceKey public static Optional> getRegistryTag(IFaction faction, ResourceKey> key) { return Optional.ofNullable((TagKey) registeredTags.get(faction, key)); } + + @ApiStatus.Internal + public static void addFaction(IFaction faction, Map, TagKey> defaultTags) { + if (registeredTags.containsRow(faction)) { + throw new IllegalStateException("Faction " + faction + " already registered. Use the AddFactionTagEvent to add tags to a faction"); + } + registeredTags.row(faction).putAll(defaultTags); + } } diff --git a/src/main/java/de/teamlapen/vampirism/entity/factions/PlayableFactionBuilder.java b/src/main/java/de/teamlapen/vampirism/entity/factions/PlayableFactionBuilder.java index 522b0c38e..065095907 100644 --- a/src/main/java/de/teamlapen/vampirism/entity/factions/PlayableFactionBuilder.java +++ b/src/main/java/de/teamlapen/vampirism/entity/factions/PlayableFactionBuilder.java @@ -6,7 +6,10 @@ import de.teamlapen.vampirism.api.entity.player.IFactionPlayer; import de.teamlapen.vampirism.api.items.IRefinementItem; import net.minecraft.ChatFormatting; +import net.minecraft.core.Registry; import net.minecraft.network.chat.TextColor; +import net.minecraft.resources.ResourceKey; +import net.minecraft.tags.TagKey; import net.neoforged.neoforge.attachment.AttachmentType; import org.jetbrains.annotations.NotNull; @@ -75,8 +78,20 @@ public PlayableFactionBuilder namePlural(@NotNull String namePluralKey) { return this; } + @Override + public PlayableFactionBuilder addTag(ResourceKey key, TagKey tag) { + return (PlayableFactionBuilder) super.addTag(key, tag); + } + + @Override + public PlayableFactionBuilder addRegistryTag(ResourceKey> key, TagKey tag) { + return (PlayableFactionBuilder) super.addRegistryTag(key, tag); + } + @Override public @NotNull PlayableFaction build() { - return new PlayableFaction<>(this); + var faction = new PlayableFaction<>(this); + FactionTags.addFaction(faction, this.tags); + return faction; } }