Skip to content

Commit

Permalink
create a neoforge registry for factions
Browse files Browse the repository at this point in the history
- adapt faction holders
- add faction tags
  • Loading branch information
Cheaterpaul committed Jun 9, 2024
1 parent 3eeef36 commit 725f46c
Show file tree
Hide file tree
Showing 217 changed files with 1,499 additions and 1,105 deletions.
12 changes: 9 additions & 3 deletions src/api/java/de/teamlapen/vampirism/api/APIUtil.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package de.teamlapen.vampirism.api;

import com.google.common.base.Suppliers;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.entity.player.actions.IAction;
import de.teamlapen.vampirism.api.entity.player.actions.ILastingAction;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.factions.IFactionEntity;
import de.teamlapen.vampirism.api.registries.DeferredFaction;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.RegistryManager;
import net.neoforged.neoforge.registries.datamaps.DataMapType;

Expand All @@ -36,4 +37,9 @@ static <T> Supplier<T> supplyRegistry(ResourceKey<T> key) {
static <T> ResourceKey<Registry<T>> registryKey(String name) {
return ResourceKey.createRegistryKey(new ResourceLocation(VReference.MODID, name));
}

@SuppressWarnings("unchecked")
static <Z extends IFactionEntity, L extends IFaction<Z>> DeferredFaction<Z, L> factionHolder(ResourceLocation key) {
return DeferredFaction.createFaction((ResourceKey<L>) ResourceKey.create(VampirismRegistries.Keys.FACTION, key));
}
}
8 changes: 4 additions & 4 deletions src/api/java/de/teamlapen/vampirism/api/VReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public class VReference {
*/
public static MobCategory VAMPIRE_CREATURE_TYPE = MobCategory.create("vampirism_vampire", "vampirism_vampire", 30, false, false, 128);
/**
* Vampire Player Faction
* Filled during pre-init.
* @deprecated use {@link de.teamlapen.vampirism.api.VampirismFactions#VAMPIRE}
*/
@Deprecated
public static IPlayableFaction<IVampirePlayer> VAMPIRE_FACTION;
/**
* Hunter Player Faction
* Filled during pre-init.
* @deprecated use {@link de.teamlapen.vampirism.api.VampirismFactions#HUNTER}
*/
@Deprecated
public static IPlayableFaction<IHunterPlayer> HUNTER_FACTION;
public static IVampireVision vision_nightVision;
public static IVampireVision vision_bloodVision;
Expand Down
19 changes: 19 additions & 0 deletions src/api/java/de/teamlapen/vampirism/api/VampirismFactions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.teamlapen.vampirism.api;

import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import de.teamlapen.vampirism.api.entity.player.hunter.IHunterPlayer;
import de.teamlapen.vampirism.api.entity.player.vampire.IVampirePlayer;
import de.teamlapen.vampirism.api.registries.DeferredFaction;
import net.minecraft.resources.ResourceLocation;

import static de.teamlapen.vampirism.api.APIUtil.*;

public class VampirismFactions {
public static final DeferredFaction<IVampirePlayer, IPlayableFaction<IVampirePlayer>> VAMPIRE = factionHolder(Keys.VAMPIRE);
public static final DeferredFaction<IHunterPlayer, IPlayableFaction<IHunterPlayer>> HUNTER = factionHolder(Keys.HUNTER);

public static class Keys {
public static final ResourceLocation VAMPIRE = new ResourceLocation(VReference.MODID, "vampire");
public static final ResourceLocation HUNTER = new ResourceLocation(VReference.MODID, "hunter");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.serialization.MapCodec;
import de.teamlapen.vampirism.api.entity.convertible.Converter;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.factions.ISkillNode;
import de.teamlapen.vampirism.api.entity.factions.ISkillTree;
import de.teamlapen.vampirism.api.entity.minion.IMinionTask;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class VampirismRegistries {
public static final Supplier<Registry<MapCodec<? extends TaskRequirement.Requirement<?>>>> TASK_REQUIREMENT = supplyRegistry(Keys.TASK_REQUIREMENT);
public static final Supplier<Registry<MapCodec<? extends ITaskRewardInstance>>> TASK_REWARD_INSTANCE = supplyRegistry(Keys.TASK_REWARD_INSTANCE);
public static final Supplier<Registry<MapCodec<? extends Converter>>> ENTITY_CONVERTER = supplyRegistry(Keys.ENTITY_CONVERTER);
public static final Supplier<Registry<IFaction<?>>> FACTION = supplyRegistry(Keys.FACTION);


public static class Keys {
Expand All @@ -53,6 +55,7 @@ public static class Keys {
public static final ResourceKey<Registry<MapCodec<? extends TaskRequirement.Requirement<?>>>> TASK_REQUIREMENT = registryKey("task_requirement");
public static final ResourceKey<Registry<MapCodec<? extends ITaskRewardInstance>>> TASK_REWARD_INSTANCE = registryKey("task_reward_instance");
public static final ResourceKey<Registry<MapCodec<? extends Converter>>> ENTITY_CONVERTER = registryKey("converting_handler");
public static final ResourceKey<Registry<IFaction<?>>> FACTION = registryKey("faction");

// data pack registries
public static final ResourceKey<Registry<Task>> TASK = registryKey("tasks");
Expand Down
20 changes: 20 additions & 0 deletions src/api/java/de/teamlapen/vampirism/api/VampirismTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.teamlapen.vampirism.api;

import de.teamlapen.vampirism.api.entity.factions.IFaction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import org.jetbrains.annotations.NotNull;

public class VampirismTags {

public static class Factions {
public static final TagKey<IFaction<?>> ALL_FACTIONS = tag("all");
public static final TagKey<IFaction<?>> IS_HUNTER = tag("is_hunter");
public static final TagKey<IFaction<?>> IS_VAMPIRE = tag("is_vampire");

private static @NotNull TagKey<IFaction<?>> tag(@NotNull String name) {
return TagKey.create(VampirismRegistries.Keys.FACTION, new ResourceLocation(VReference.MODID, name));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package de.teamlapen.vampirism.api.entity.factions;

import net.minecraft.core.Holder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface IDisguise {

@NotNull
IPlayableFaction<?> getOriginalFaction();
Holder<? extends IPlayableFaction<?>> getOriginalFaction();

@Nullable
IPlayableFaction<?> getViewedFaction(@Nullable IFaction<?> viewerFaction);
Holder<? extends IFaction<?>> getViewedFaction(@Nullable Holder<? extends IFaction<?>> viewerFaction);

@Nullable
default IPlayableFaction<?> getViewedFaction(@Nullable IFaction<?> viewerFaction, boolean ignoreDisguise) {
default Holder<? extends IFaction<?>> getViewedFaction(@Nullable Holder<? extends IFaction<?>> viewerFaction, boolean ignoreDisguise) {
return ignoreDisguise ? getOriginalFaction() : getViewedFaction(viewerFaction);
}

void disguiseAs(@Nullable IPlayableFaction<?> faction);
void disguiseAs(@Nullable Holder<? extends IFaction<?>> faction);

void unDisguise();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.teamlapen.vampirism.api.VampirismAPI;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
Expand All @@ -11,6 +12,7 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.PathfinderMob;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

Expand All @@ -19,8 +21,6 @@
*/
public interface IFaction<T extends IFactionEntity> {

Codec<IFaction<?>> CODEC = RecordCodecBuilder.create(ins -> ins.group(ResourceLocation.CODEC.fieldOf("id").forGetter(IFaction::getID)).apply(ins, (id) -> VampirismAPI.factionRegistry().getFactionByID(id)));

/**
* If not set returns white
*/
Expand Down Expand Up @@ -61,9 +61,30 @@ public interface IFaction<T extends IFactionEntity> {

/**
* @return Whether entities of this faction are hostile towards neutral entities
* @deprecated use HostileTowardsNeutral tag instead
*/
@Deprecated
boolean isHostileTowardsNeutral();

<Z> Optional<TagKey<Z>> getTag(ResourceKey<? extends Registry<Z>> registryKey);

@SuppressWarnings({"deprecation", "unchecked", "rawtypes"})
static boolean is(@Nullable Holder<? extends IFaction<?>> first, @Nullable Holder<? extends IFaction<?>> second) {
if (first == null) {
return second == null;
}
return second != null && first.is((Holder) second);
}

@SuppressWarnings({"unchecked", "rawtypes"})
static <T extends IFaction<?>> boolean is(@Nullable Holder<? extends IFaction<?>> first, @Nullable TagKey<T> second) {
if (first == null) {
return second == null;
}
return second != null && first.is((TagKey) second);
}

static <T extends IFaction<?>, Z extends IFaction<?>> boolean is(TagKey<Z> first, TagKey<T> second) {
return first.location().equals(second.location());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.teamlapen.vampirism.api.entity.factions;

import de.teamlapen.vampirism.api.extensions.IEntity;
import net.minecraft.core.Holder;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,7 +13,7 @@ public interface IFactionEntity extends IEntity {
* @return the faction this entity belongs to
*/
@NotNull
IFaction<?> getFaction();
Holder<? extends IFaction<?>> getFaction();

/**
* Can be the same object or for Player Capabilities the player object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.entity.player.ILordPlayer;
import de.teamlapen.vampirism.api.extensions.IPlayer;
import net.minecraft.core.Holder;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,6 +24,8 @@ public interface IFactionPlayerHandler extends ILordPlayer, IPlayer {
*/
boolean canJoin(IPlayableFaction<?> faction);

boolean canJoin(Holder<? extends IPlayableFaction<?>> faction);

/**
* Checks currents factions {@link IFactionPlayer#canLeaveFaction()}
*/
Expand All @@ -34,6 +37,9 @@ public interface IFactionPlayerHandler extends ILordPlayer, IPlayer {
@Nullable
<T extends IFactionPlayer<T>> IPlayableFaction<T> getCurrentFaction();

@Nullable
Holder<? extends IPlayableFaction<?>> getFaction();

/**
* @return The currently active faction player. Can be null
*/
Expand All @@ -56,6 +62,8 @@ public interface IFactionPlayerHandler extends ILordPlayer, IPlayer {
*/
int getCurrentLevel(IPlayableFaction<?> f);

int getCurrentLevel(Holder<? extends IPlayableFaction<?>> f);

/**
* If not in faction returns 0f
*
Expand All @@ -76,12 +84,17 @@ public interface IFactionPlayerHandler extends ILordPlayer, IPlayer {
*/
boolean isInFaction(IFaction<?> f);

<T extends IFaction<?>> boolean isInFaction(Holder<T> f);

/**
* Join the given faction and set the faction level to 1.
* Only successful if {@link IFactionPlayerHandler#canJoin(IPlayableFaction)}
*/
void joinFaction(@NotNull IPlayableFaction<?> faction);


void joinFaction(@NotNull Holder<? extends IPlayableFaction<?>> faction);

/**
* Should be called if the entity attacked.
* If this returns false the attack should be canceled
Expand All @@ -97,12 +110,17 @@ public interface IFactionPlayerHandler extends ILordPlayer, IPlayer {
*/
boolean setFactionAndLevel(@Nullable IPlayableFaction<?> faction, int level);

boolean setFactionAndLevel(@Nullable Holder<? extends IPlayableFaction<?>> faction, int level);

/**
* Set the level for a faction. Only works if the player already is in the given faction.
* Use {@link IFactionPlayerHandler#joinFaction(IPlayableFaction)} to join a faction first or {@link IFactionPlayerHandler#setFactionAndLevel(IPlayableFaction, int)} if you are sure what you do
*
* @return If successful
*/
boolean setFactionLevel(@NotNull Holder<? extends IPlayableFaction<?>> faction, int level);


boolean setFactionLevel(@NotNull IPlayableFaction<?> faction, int level);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import de.teamlapen.vampirism.api.entity.minion.IMinionData;
import de.teamlapen.vampirism.api.entity.minion.IMinionEntity;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.util.RegUtil;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand All @@ -29,31 +31,44 @@ public interface IFactionRegistry {
* @param entity Can check any entity
* @return The faction of the given entity or null
*/
@Deprecated
@Nullable
IFaction<?> getFaction(Entity entity);

Holder<? extends IFaction<?>> getFactionHolder(Entity entity);

/**
* Get a faction by its name
*/
@Deprecated
@Nullable
IFaction<?> getFactionByID(ResourceLocation id);

/**
* @return All factions after post init
*/
@Deprecated
IFaction<?>[] getFactions();

/**
* @return All playable factions after post init
*/
@Deprecated
IPlayableFaction<?>[] getPlayableFactions();

List<Holder<? extends IPlayableFaction<?>>> playableFactions();

/**
* Get a cached or create a predicate which selects all other faction entities
*
* @param ignoreDisguise If disguised players should still be counted for their actual faction (disguised vampires will still be detected as vampires)
*/
Predicate<LivingEntity> getPredicate(IFaction<?> thisFaction, boolean ignoreDisguise);
@Deprecated
default Predicate<LivingEntity> getPredicate(IFaction<?> thisFaction, boolean ignoreDisguise) {
return getPredicate(RegUtil.holder(thisFaction), ignoreDisguise);
}

Predicate<LivingEntity> getPredicate(Holder<? extends IFaction<?>> thisFaction, boolean ignoreDisguise);

/**
* Get a cached or create a predicate which selects entities from other factions.
Expand All @@ -65,8 +80,11 @@ public interface IFactionRegistry {
* @param ignoreDisguise If disguised players should still be counted for their actual faction (disguised vampires will still be detected as vampires)
* @param otherFaction If this is not null, only entities of this faction are selected.
*/
@Deprecated
Predicate<LivingEntity> getPredicate(IFaction<?> thisFaction, boolean player, boolean mob, boolean neutralPlayer, boolean ignoreDisguise, @Nullable IFaction<?> otherFaction);

Predicate<LivingEntity> getPredicate(Holder<? extends IFaction<?>> thisFaction, boolean player, boolean mob, boolean neutralPlayer, boolean ignoreDisguise, @Nullable Holder<? extends IFaction<?>> otherFaction);

/**
* Creates a faction builder.
*
Expand All @@ -75,6 +93,7 @@ public interface IFactionRegistry {
* @param <T> Interface all entities implement
* @return a faction builder
*/
@Deprecated
@ThreadSafeAPI
<T extends IFactionEntity> IFactionBuilder<T> createFaction(ResourceLocation id, Class<T> entityInterface);

Expand All @@ -87,6 +106,7 @@ public interface IFactionRegistry {
* @param <T> Interface all entities or (the given capability for players) implement
* @return a player faction builder
*/
@Deprecated
@ThreadSafeAPI
<T extends IFactionPlayer<T>> IPlayableFactionBuilder<T> createPlayableFaction(ResourceLocation id, Class<T> entityInterface, Supplier<AttachmentType<T>> playerCapabilitySupplier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public interface ILordPlayerBuilder<T extends IFactionPlayer<T>> {
* Enables this faction to have lord skills
*
* @return the builder
* @deprecated use HasLordSkills tag instead
*/
@Deprecated
ILordPlayerBuilder<T> enableLordSkills();

<Z extends IMinionData> IMinionBuilder<T,Z> minion(ResourceLocation minionId, Supplier<Z> data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
public interface IPlayableFaction<T extends IFactionPlayer<T>> extends IFaction<T> {

Codec<IPlayableFaction<?>> CODEC = RecordCodecBuilder.create(ins -> ins.group(ResourceLocation.CODEC.fieldOf("id").forGetter(IPlayableFaction::getID)).apply(ins, (id) -> (IPlayableFaction<?>) VampirismAPI.factionRegistry().getFactionByID(id)));

Class<T> getFactionPlayerInterface();

/**
Expand Down Expand Up @@ -69,6 +67,7 @@ public interface IPlayableFaction<T extends IFactionPlayer<T>> extends IFaction<
/**
* @return If the faction has lord skills
*/
@Deprecated
boolean hasLordSkills();

enum TitleGender implements StringRepresentable {
Expand Down
Loading

0 comments on commit 725f46c

Please sign in to comment.