Skip to content

Commit

Permalink
add short lord titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Apr 3, 2024
1 parent f2a8e77 commit 36afd7a
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public interface IFactionVillage {

boolean isBanner(@NotNull ItemStack stack);

/**
* @deprecated internal use only
*/
@ApiStatus.Internal
default @NotNull IFactionVillage build() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import de.teamlapen.vampirism.api.entity.minion.IMinionData;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

Expand All @@ -21,15 +20,21 @@ public interface ILordPlayerBuilder<T extends IFactionPlayer<T>> {
*/
ILordPlayerBuilder<T> lordLevel(int highestLordLevel);

/**
* @deprecated use {@link #lordTitle(ILordTitleProvider)} instead
*/
@Deprecated
ILordPlayerBuilder<T> lordTitle(@NotNull BiFunction<Integer, IPlayableFaction.TitleGender, Component> lordTitleFunction);

/**
* Sets custom lord titles
* <br>
* Only relevant when lord level > 0
*
* @param lordTitleFunction a function that return the title for a lord player based on level and gender
* @param lordTitleFunction an object that provides the title for a lord player
* @return the builder
*/
ILordPlayerBuilder<T> lordTitle(@NotNull BiFunction<Integer, IPlayableFaction.TitleGender, Component> lordTitleFunction);
ILordPlayerBuilder<T> lordTitle(@NotNull ILordTitleProvider lordTitleFunction);

/**
* Enables this faction to have lord skills
Expand All @@ -41,4 +46,5 @@ public interface ILordPlayerBuilder<T extends IFactionPlayer<T>> {
<Z extends IMinionData> IMinionBuilder<T,Z> minion(ResourceLocation minionId, Supplier<Z> data);

IPlayableFactionBuilder<T> build();

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

import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

public interface ILordTitleProvider {

/**
* Get a lord title.
*
* @param level the lord level
* @param titleGender the gender of the title
* @return the lord title
*/
@Nullable
Component getLordTitle(int level, IPlayableFaction.TitleGender titleGender);

/**
* Get a short lord title.
* @param level the lord level
* @param titleGender the gender of the title
* @return the short lord title
*/
@Nullable
Component getShort(int level, IPlayableFaction.TitleGender titleGender);

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ public interface IPlayableFaction<T extends IFactionPlayer<T>> extends IFaction<
* @param level lord level
* @param female Female version
* @return A text component representing the title of the player at the given lord level. empty if level==0
* @deprecated Use {@link ILordTitleProvider#getLordTitle(int, TitleGender)} instead
*/
@Deprecated
@NotNull
Component getLordTitle(int level, TitleGender female);

/**
* Gets the lord title provider for this faction
*/
ILordTitleProvider lordTiles();

/**
* @return The "<? extends IFactionPlayer>" of this faction for the given player
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package de.teamlapen.vampirism.api.entity.player;

import de.teamlapen.vampirism.api.entity.factions.ILordTitleProvider;
import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
* Interface for the player lord related data.
*/
Expand All @@ -26,6 +29,12 @@ public interface ILordPlayer {
@Nullable
Component getLordTitle();

/**
* @return Null, if level ==0
*/
@Nullable
Component getLordTitleShort();

@NotNull
Player getPlayer();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/teamlapen/vampirism/VampirismMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private void prepareAPI() {
.namePlural("text.vampirism.vampires")
.hostileTowardsNeutral()
.highestLevel(REFERENCE.HIGHEST_VAMPIRE_LEVEL)
.lord().lordLevel(REFERENCE.HIGHEST_VAMPIRE_LORD).lordTitle(LordTitles::getVampireTitle).enableLordSkills()
.lord().lordLevel(REFERENCE.HIGHEST_VAMPIRE_LORD).lordTitle(new LordTitles.VampireTitles()).enableLordSkills()
.minion(VampireMinionEntity.VampireMinionData.ID, VampireMinionEntity.VampireMinionData::new)
.commandBuilder(ModEntities.VAMPIRE_MINION::get)
.with("name", "Vampire", StringArgumentType.string(), MinionData::setName, StringArgumentType::getString)
Expand All @@ -278,7 +278,7 @@ private void prepareAPI() {
.name("text.vampirism.hunter")
.namePlural("text.vampirism.hunters")
.highestLevel(REFERENCE.HIGHEST_HUNTER_LEVEL)
.lord().lordLevel(REFERENCE.HIGHEST_HUNTER_LORD).lordTitle(LordTitles::getHunterTitle).enableLordSkills()
.lord().lordLevel(REFERENCE.HIGHEST_HUNTER_LORD).lordTitle(new LordTitles.HunterTitles()).enableLordSkills()
.minion(HunterMinionEntity.HunterMinionData.ID, HunterMinionEntity.HunterMinionData::new)
.commandBuilder(ModEntities.HUNTER_MINION::get)
.with("name", "Hunter", StringArgumentType.string(), MinionData::setName, StringArgumentType::getString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import de.teamlapen.vampirism.entity.factions.FactionPlayerHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.client.gui.overlay.ExtendedGui;
import net.neoforged.neoforge.client.gui.overlay.IGuiOverlay;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public class FactionLevelOverlay implements IGuiOverlay {
private final Minecraft mc = Minecraft.getInstance();

Expand All @@ -23,13 +26,15 @@ public void render(@NotNull ExtendedGui gui, @NotNull GuiGraphics graphics, floa
// boolean flag1 = false;
int color = faction.getColor();
int lord = handler.getLordLevel();
String text;
String text = null;
if (lord > 0) {
String title = handler.getLordTitle().getString();
text = title.substring(0, Math.min(3, title.length()));
} else {
text = Optional.ofNullable(handler.getLordTitleShort()).map(Component::getString).map(x -> x.substring(0, Math.min(3, x.length()))).orElse(null);
}

if (text == null) {
text = String.valueOf(handler.getCurrentLevel());
}

int x = (this.mc.getWindow().getGuiScaledWidth() - this.mc.font.width(text)) / 2 + VampirismConfig.CLIENT.guiLevelOffsetX.get();
int y = this.mc.getWindow().getGuiScaledHeight() - VampirismConfig.CLIENT.guiLevelOffsetY.get();
graphics.drawString(this.mc.font, text, x + 1, y, 0, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class VampirismContainerScreen extends AbstractContainerScreen<VampirismMenu> implements ExtendedScreen {
Expand Down Expand Up @@ -135,12 +136,7 @@ protected void init() {
super.init();
if (factionPlayer.getLevel() > 0) {
FactionPlayerHandler handler = FactionPlayerHandler.get(factionPlayer.asEntity());
MutableComponent component;
if (handler.getLordLevel() > 0) {
component = handler.getLordTitle().copy().append(" (" + handler.getLordLevel() + ")");
} else {
component = Component.translatable("text.vampirism.level").append(" " + factionPlayer.getLevel());
}
MutableComponent component = Optional.of(handler).filter(x -> x.getLordLevel() > 0).map(FactionPlayerHandler::getLordTitle).map(x -> x.plainCopy().append(" (" + handler.getLordLevel() + ")")).orElseGet(() -> Component.translatable("text.vampirism.level").append(" " + factionPlayer.getLevel()));
this.level = component.withStyle(style -> style.withColor(factionPlayer.getFaction().getChatColor()));
} else {
this.level = Component.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.VampirismAPI;
import de.teamlapen.vampirism.api.VampirismRegistries;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.factions.IFactionPlayerHandler;
import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import de.teamlapen.vampirism.api.entity.factions.ISkillTree;
import de.teamlapen.vampirism.api.entity.factions.*;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.entity.player.actions.IAction;
import de.teamlapen.vampirism.api.event.PlayerFactionEvent;
Expand Down Expand Up @@ -166,9 +163,17 @@ public int getLordLevel() {
@Nullable
@Override
public Component getLordTitle() {
return currentLordLevel == 0 || currentFaction == null ? null : currentFaction.getLordTitle(currentLordLevel, titleGender);
return lordTitles().map(titles -> titles.getLordTitle(currentLordLevel, titleGender)).orElse(null);
}

@Override
public @Nullable Component getLordTitleShort() {
return lordTitles().map(titles -> titles.getShort(currentLordLevel, titleGender)).orElse(null);
}

public @NotNull Optional<ILordTitleProvider> lordTitles() {
return Optional.ofNullable(currentFaction).map(IPlayableFaction::lordTiles);
}

public int getMaxMinions() {
return currentLordLevel * VampirismConfig.BALANCE.miMinionPerLordLevel.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -25,7 +24,6 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.common.util.NonNullSupplier;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -352,9 +350,9 @@ public static class LordPlayerBuilder<T extends IFactionPlayer<T>> implements IL

protected final PlayableFactionBuilder<T> factionBuilder;
protected int maxLevel = 0;
protected BiFunction<Integer, IPlayableFaction.TitleGender, Component> lordTitleFunction = (a, b) -> Component.literal("Lord " + a);
protected ILordTitleProvider lordTitleFunction = (LordTitleProvider) (a, b) -> Component.literal("Lord " + a);
protected boolean lordSkillsEnabled;
protected List<MinionBuilder<T,?>> minions = new ArrayList<>();
protected List<MinionBuilder<T, ?>> minions = new ArrayList<>();

public LordPlayerBuilder(PlayableFactionBuilder<T> factionBuilder) {
this.factionBuilder = factionBuilder;
Expand All @@ -368,6 +366,12 @@ public LordPlayerBuilder(PlayableFactionBuilder<T> factionBuilder) {

@Override
public @NotNull LordPlayerBuilder<T> lordTitle(@NotNull BiFunction<Integer, IPlayableFaction.TitleGender, Component> lordTitleFunction) {
this.lordTitleFunction = (LordTitleProvider) lordTitleFunction::apply;
return this;
}

@Override
public ILordPlayerBuilder<T> lordTitle(@NotNull ILordTitleProvider lordTitleFunction) {
this.lordTitleFunction = lordTitleFunction;
return this;
}
Expand Down Expand Up @@ -447,6 +451,14 @@ public record CommandEntry<Z extends IMinionData,T>(String name, T defaultValue,
}
}
}

public interface LordTitleProvider extends ILordTitleProvider {

@Override
default Component getShort(int level, IPlayableFaction.TitleGender titleGender) {
return getLordTitle(level, titleGender);
}
}
}

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

import de.teamlapen.vampirism.api.entity.factions.ILordTitleProvider;
import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.items.IRefinementItem;
Expand All @@ -24,7 +25,7 @@ public class PlayableFaction<T extends IFactionPlayer<T>> extends Faction<T> imp
private final int highestLevel;
private final int highestLordLevel;
private final Supplier<AttachmentType<T>> playerCapabilitySupplier;
private final BiFunction<Integer, TitleGender, Component> lordTitleFunction;
private final ILordTitleProvider lordTitleFunction;
private final Function<IRefinementItem.AccessorySlotType, IRefinementItem> refinementItemBySlot;
private final boolean hasLordSkills;

Expand Down Expand Up @@ -61,8 +62,12 @@ public boolean hasLordSkills() {
@NotNull
@Override
public Component getLordTitle(int level, TitleGender female) {
assert level <= highestLordLevel;
return lordTitleFunction.apply(level, female);
return lordTiles().getLordTitle(level, female);
}

@Override
public ILordTitleProvider lordTiles() {
return this.lordTitleFunction;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,15 @@ public void onPlayerLeftClickedBlock(PlayerInteractEvent.@NotNull LeftClickBlock

@SubscribeEvent(priority = EventPriority.LOW)
public void onPlayerName(PlayerEvent.@NotNull NameFormat event) {
if (event.getEntity() != null && VampirismConfig.SERVER.factionColorInChat.get()) {
if (VampirismConfig.SERVER.factionColorInChat.get()) {
FactionPlayerHandler handler = FactionPlayerHandler.get(event.getEntity());
handler.getCurrentFactionPlayer().ifPresent(fp -> {
IFaction<?> f = fp.getDisguise().getViewedFaction(Optional.ofNullable(VampirismMod.proxy.getClientPlayer()).flatMap(FactionPlayerHandler::getOpt).map(FactionPlayerHandler::getCurrentFaction).orElse(null));
IFaction<?> f = fp.getDisguise().getViewedFaction(Optional.ofNullable(VampirismMod.proxy.getClientPlayer()).map(FactionPlayerHandler::get).map(FactionPlayerHandler::getCurrentFaction).orElse(null));
if (f != null) {
MutableComponent displayName;
if (handler.getLordLevel() > 0 && VampirismConfig.SERVER.lordPrefixInChat.get()) {
displayName = Component.literal("[").append(handler.getLordTitle()).append("] ").append(event.getDisplayname());
} else {
displayName = event.getDisplayname().copy();
}
displayName = Optional.of(handler).filter(h -> h.getLordLevel() > 0).filter(x -> VampirismConfig.SERVER.lordPrefixInChat.get()).map(FactionPlayerHandler::getLordTitle)
.map(x -> Component.literal("[").append(x).append("] ").append(event.getDisplayname()))
.orElseGet(() -> event.getDisplayname().copy());
event.setDisplayname(displayName.withStyle(style -> style.withColor((f.getChatColor()))));
}
});
Expand Down
Loading

0 comments on commit 36afd7a

Please sign in to comment.