Skip to content

Commit

Permalink
move faction restrictions from item into item data components
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Jan 12, 2025
1 parent 6972b27 commit 752884f
Show file tree
Hide file tree
Showing 35 changed files with 400 additions and 481 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public static class Keys {
public static final ResourceLocation FRUGALITY = VResourceLocation.mod( "frugality");
public static final ResourceLocation IS_FACTION_BANNER = VResourceLocation.mod("is_faction_banner");
public static final ResourceLocation VAMPIRE_FOOD = VResourceLocation.mod("vampire_food");
public static final ResourceLocation FACTION_RESTRICTION = VResourceLocation.mod("faction_restriction");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.player.Player;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

Expand All @@ -18,10 +19,10 @@

public interface IFactionRegistry {

@Nullable
@NotNull
Holder<? extends IFaction<?>> getFaction(Entity entity);

@Nullable
@NotNull
Holder<? extends IFaction<?>> getFaction(Player entity);

boolean isEntityOfFaction(Entity entity, Holder<? extends IFaction<?>> faction);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.tags.TagKey;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -13,7 +14,7 @@
*
* @implSpec should only be implemented by {@link net.minecraft.world.item.Item}s
*/
public interface IRefinementItem extends IFactionExclusiveItem {
public interface IRefinementItem extends ItemLike {

/**
* Gets the refinement set that is applied to this refinement item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public interface IVampirismCrossbowArrow<T extends AbstractArrow & IEntityCrossbowArrow> extends IFactionExclusiveItem {

@Override
default @NotNull TagKey<IFaction<?>> getExclusiveFaction(@NotNull ItemStack stack) {
return VampirismTags.Factions.IS_HUNTER;
}
public interface IVampirismCrossbowArrow<T extends AbstractArrow & IEntityCrossbowArrow> extends ItemLike {

/**
* @return If an arrow of this type can be used in an infinite crossbow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package de.teamlapen.vampirism.client.core;

import com.google.common.collect.Lists;
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.api.items.IFactionExclusiveItem;
import de.teamlapen.vampirism.api.util.VResourceLocation;
import de.teamlapen.vampirism.client.VampirismModClient;
import de.teamlapen.vampirism.config.VampirismConfig;
import de.teamlapen.vampirism.core.ModBlocks;
import de.teamlapen.vampirism.core.ModFactions;
import de.teamlapen.vampirism.data.ClientSkillTreeData;
import de.teamlapen.vampirism.effects.VampirismPotion;
import de.teamlapen.vampirism.entity.player.LevelAttributeModifier;
import de.teamlapen.vampirism.items.component.AppliedOilContent;
import de.teamlapen.vampirism.items.component.FactionRestriction;
import de.teamlapen.vampirism.util.Helper;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
Expand All @@ -35,11 +27,6 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Handle general client side events
*/
Expand Down Expand Up @@ -86,24 +73,8 @@ static void onModelRegistry(@NotNull ModelEvent.RegisterAdditional event) {
*/
@SubscribeEvent
public void onItemToolTip(@NotNull ItemTooltipEvent event) {
if (event.getItemStack().getItem() instanceof IFactionExclusiveItem) return;
AppliedOilContent.getAppliedOil(event.getItemStack()).ifPresent(oil -> {
List<Component> toolTips = event.getToolTip();
int position = 1;
if (!event.getItemStack().has(DataComponents.HIDE_ADDITIONAL_TOOLTIP)) {
ArrayList<Component> additionalComponents = new ArrayList<>();
event.getItemStack().getItem().appendHoverText(event.getItemStack(), event.getContext(), additionalComponents, event.getFlags());
position += additionalComponents.size();
Optional<Component> oilTooltip = oil.oil().value().getToolTipLine(event.getItemStack(), oil.oil().value(), oil.duration(), event.getFlags());
if (oilTooltip.isPresent()) {
toolTips.add(position++, oilTooltip.get());
}
}
List<Component> factionToolTips = new ArrayList<>();
factionToolTips.add(Component.empty());
factionToolTips.add(Component.translatable("text.vampirism.faction_exclusive", ModFactions.HUNTER.get().getName().plainCopy().withStyle(Minecraft.getInstance().player != null ? Helper.isHunter(Minecraft.getInstance().player) ? ChatFormatting.DARK_GREEN : ChatFormatting.DARK_RED : ChatFormatting.GRAY)).withStyle(ChatFormatting.ITALIC).withStyle(ChatFormatting.GRAY));
toolTips.addAll(Math.min(event.getToolTip().size(), position), factionToolTips);
});
AppliedOilContent.addTooltipIfExist(event.getEntity(), event.getItemStack(), event.getToolTip(), event.getFlags());
FactionRestriction.addTooltipIfExist(event.getEntity(), event.getItemStack(), event.getToolTip());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ModDataComponents {
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> CROSSBOW_FRUGALITY_TRIGGERED = ITEM_DATA_COMPONENTS.registerComponentType("crossbow_frugality_triggered", (builder) -> builder.persistent(Codec.unit(Unit.INSTANCE)).networkSynchronized(StreamCodec.unit(Unit.INSTANCE)));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> IS_FACTION_BANNER = ITEM_DATA_COMPONENTS.registerComponentType(VampirismDataComponents.Keys.IS_FACTION_BANNER.getPath(), (builder) -> builder.persistent(Unit.CODEC).networkSynchronized(StreamCodec.unit(Unit.INSTANCE)));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<BloodFoodProperties>> VAMPIRE_FOOD = ITEM_DATA_COMPONENTS.registerComponentType(VampirismDataComponents.Keys.VAMPIRE_FOOD.getPath(), (builder) -> builder.persistent(BloodFoodProperties.DIRECT_CODEC).networkSynchronized(BloodFoodProperties.DIRECT_STREAM_CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<FactionRestriction>> FACTION_RESTRICTION = ITEM_DATA_COMPONENTS.registerComponentType(VampirismDataComponents.Keys.FACTION_RESTRICTION.getPath(), builder -> builder.persistent(FactionRestriction.CODEC).networkSynchronized(FactionRestriction.STREAM_CODEC));

static void register(IEventBus eventBus) {
ITEM_DATA_COMPONENTS.register(eventBus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.teamlapen.vampirism.api.entity.factions.IFactionRegistry;
import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import de.teamlapen.vampirism.api.entity.minion.IMinionEntry;
import de.teamlapen.vampirism.core.ModFactions;
import de.teamlapen.vampirism.core.ModRegistries;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -35,17 +36,18 @@ public class FactionRegistry implements IFactionRegistry {

private final MinionEntryCallbacks minionEntryCallbacks = new MinionEntryCallbacks();

@NotNull
@Override
public Holder<? extends IFaction<?>> getFaction(Entity entity) {
if (entity instanceof Player player) {
return getFaction(player);
} else if (entity instanceof IFactionEntity factionEntity) {
return factionEntity.getFaction();
}
return ModRegistries.FACTIONS.listElements().filter(s -> s.value().getTag(Registries.ENTITY_TYPE).flatMap(BuiltInRegistries.ENTITY_TYPE::get).filter(tag -> entity.getType().is(tag)).isPresent()).findFirst().orElse(null);
return ModRegistries.FACTIONS.listElements().map(s -> (Holder<IFaction<?>>)s).filter(s -> s.value().getTag(Registries.ENTITY_TYPE).flatMap(BuiltInRegistries.ENTITY_TYPE::get).filter(tag -> entity.getType().is(tag)).isPresent()).findFirst().orElse((Holder<IFaction<?>>) (Object) ModFactions.NEUTRAL);
}

@Nullable
@NotNull
public Holder<? extends IFaction<?>> getFaction(Player player) {
return FactionPlayerHandler.get(player).getFaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import de.teamlapen.lib.lib.storage.ISyncable;
import de.teamlapen.lib.lib.storage.UpdateParams;
import de.teamlapen.vampirism.VampirismMod;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.minion.IMinionEntity;
import de.teamlapen.vampirism.api.entity.minion.IMinionInventory;
import de.teamlapen.vampirism.api.entity.minion.IMinionTask;
import de.teamlapen.vampirism.api.entity.player.ILordPlayer;
import de.teamlapen.vampirism.api.items.IFactionExclusiveItem;
import de.teamlapen.vampirism.api.util.VResourceLocation;
import de.teamlapen.vampirism.entity.VampirismEntity;
import de.teamlapen.vampirism.entity.ai.goals.ForceLookEntityGoal;
Expand All @@ -24,6 +22,7 @@
import de.teamlapen.vampirism.entity.minion.management.MinionTasks;
import de.teamlapen.vampirism.entity.minion.management.PlayerMinionController;
import de.teamlapen.vampirism.inventory.MinionContainer;
import de.teamlapen.vampirism.items.component.FactionRestriction;
import de.teamlapen.vampirism.util.IPlayerOverlay;
import de.teamlapen.vampirism.util.Permissions;
import de.teamlapen.vampirism.util.PlayerModelType;
Expand Down Expand Up @@ -452,7 +451,7 @@ public void setItemSlot(@NotNull EquipmentSlot slotIn, @NotNull ItemStack stack)
}

public @NotNull Predicate<ItemStack> getEquipmentPredicate(EquipmentSlot slotType) {
return itemStack -> !(itemStack.getItem() instanceof IFactionExclusiveItem) || IFaction.is(this.getFaction(), ((IFactionExclusiveItem) itemStack.getItem()).getExclusiveFaction(itemStack));
return itemStack -> FactionRestriction.matchFaction(itemStack, getFaction());

}

Expand Down
Loading

0 comments on commit 752884f

Please sign in to comment.