Skip to content

Commit

Permalink
move default tags back to faction builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Oct 3, 2024
1 parent d0429b6 commit 64f5727
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<T extends IFactionEntity> {
Expand Down Expand Up @@ -60,6 +63,13 @@ public interface IFactionBuilder<T extends IFactionEntity> {
*/
IFactionBuilder<T> namePlural(@NotNull String namePluralKey);

<Z> IFactionBuilder<T> addTag(ResourceKey<Z> key, TagKey<Z> tag);

/**
* Adds a tag for the given registry
*/
<Z> IFactionBuilder<T> addRegistryTag(ResourceKey<? extends Registry<Z>> key, TagKey<Z> tag);

/**
* finish the building and registers the faction with values from the builder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +49,12 @@ public interface IPlayableFactionBuilder<T extends IFactionPlayer<T>> extends IF

IPlayableFactionBuilder<T> lord(ILordPlayerEntry lordPlayerBuilder);

@Override
<Z> IPlayableFactionBuilder<T> addTag(ResourceKey<Z> key, TagKey<Z> tag);

@Override
<Z> IPlayableFactionBuilder<T> addRegistryTag(ResourceKey<? extends Registry<Z>> key, TagKey<Z> tag);

@Override
IPlayableFaction<T> build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.apache.logging.log4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -25,10 +24,11 @@ public class AddFactionTagEvent extends Event implements IModBusEvent {
private static final Logger LOGGER = LogManager.getLogger();

private final Holder<IFaction<?>> faction;
private final Map<ResourceKey<?>, TagKey<?>> tags = new HashMap<>();
private final Map<ResourceKey<?>, TagKey<?>> tags;

public AddFactionTagEvent(Holder<IFaction<?>> faction) {
public AddFactionTagEvent(Holder<IFaction<?>> faction, Map<ResourceKey<?>, TagKey<?>> defaultTags) {
this.faction = faction;
this.tags = defaultTags;
}

/**
Expand Down
34 changes: 14 additions & 20 deletions src/main/java/de/teamlapen/vampirism/core/ModFactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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;
import de.teamlapen.vampirism.api.entity.player.hunter.IHunterPlayer;
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;
Expand Down Expand Up @@ -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<IVampirePlayer>()
.lordTitle(new LordTitles.VampireTitles()).lordLevel(REFERENCE.HIGHEST_VAMPIRE_LORD).build())
.build());
Expand All @@ -66,6 +73,12 @@ public class ModFactions {
.village(HunterVillage.hunterVillage().build())
.lord(new LordPlayerBuilder<IHunterPlayer>()
.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<INeutralPlayer, IPlayableFaction<INeutralPlayer>> NEUTRAL = FACTIONS.registerFaction(VampirismFactions.Keys.NEUTRAL.getPath(), () -> new PlayableFactionBuilder<>((Supplier<AttachmentType<INeutralPlayer>>) (Object) ModAttachments.NEUTRAL_PLAYER)
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
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<T extends IFactionEntity> implements IFactionBuilder<T> {

protected int color = Color.WHITE.getRGB();
protected IFactionVillage villageFactionData;
protected @Nullable TextColor chatColor;
protected String name;
protected String namePlural;
protected final Map<ResourceKey<?>, TagKey<?>> tags = new HashMap<>();

@Override
public IFactionBuilder<T> color(int color) {
Expand Down Expand Up @@ -57,9 +64,23 @@ public IFactionBuilder<T> namePlural(@NotNull String namePluralKey) {
return this;
}

@Override
public <Z> IFactionBuilder<T> addTag(ResourceKey<Z> key, TagKey<Z> tag) {
this.tags.put(key, tag);
return this;
}

@Override
public <Z> IFactionBuilder<T> addRegistryTag(ResourceKey<? extends Registry<Z>> key, TagKey<Z> tag) {
this.tags.put(key, tag);
return this;
}

@Override
public @NotNull IFaction<T> build() {
return new Faction<>(this);
var faction = new Faction<>(this);
FactionTags.addFaction(faction, this.tags);
return faction;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,7 +26,8 @@ public static void collectTags() {
}

private static void addFaction(Holder<IFaction<?>> faction) {
ModLoader.postEventWithReturn(new AddFactionTagEvent(faction)).getTags().forEach((key, tag) -> registeredTags.put(faction.value(), key, tag));
Map<ResourceKey<?>, TagKey<?>> row = registeredTags.row(faction.value());
ModLoader.postEventWithReturn(new AddFactionTagEvent(faction, Collections.unmodifiableMap(row))).getTags().forEach(row::putIfAbsent);
}

@SuppressWarnings("unchecked")
Expand All @@ -46,4 +49,12 @@ public static <T> Optional<TagKey<T>> getTag(IFaction<?> faction, ResourceKey<T>
public static <T> Optional<TagKey<T>> getRegistryTag(IFaction<?> faction, ResourceKey<? extends Registry<T>> key) {
return Optional.ofNullable((TagKey<T>) registeredTags.get(faction, key));
}

@ApiStatus.Internal
public static void addFaction(IFaction<?> faction, Map<ResourceKey<?>, 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -75,8 +78,20 @@ public PlayableFactionBuilder<T> namePlural(@NotNull String namePluralKey) {
return this;
}

@Override
public <Z> PlayableFactionBuilder<T> addTag(ResourceKey<Z> key, TagKey<Z> tag) {
return (PlayableFactionBuilder<T>) super.addTag(key, tag);
}

@Override
public <Z> PlayableFactionBuilder<T> addRegistryTag(ResourceKey<? extends Registry<Z>> key, TagKey<Z> tag) {
return (PlayableFactionBuilder<T>) super.addRegistryTag(key, tag);
}

@Override
public @NotNull PlayableFaction<T> build() {
return new PlayableFaction<>(this);
var faction = new PlayableFaction<>(this);
FactionTags.addFaction(faction, this.tags);
return faction;
}
}

0 comments on commit 64f5727

Please sign in to comment.