Skip to content

Commit

Permalink
Update Descriptions the correct way
Browse files Browse the repository at this point in the history
  • Loading branch information
FireInstall committed Feb 10, 2024
1 parent 3a259b2 commit d4e4fde
Show file tree
Hide file tree
Showing 44 changed files with 464 additions and 584 deletions.
47 changes: 24 additions & 23 deletions Minigames/src/main/java/au/com/mineauz/minigames/MinigameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import au.com.mineauz.minigames.tool.MinigameTool;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.*;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -286,39 +287,39 @@ public static void saveShortLocation(ConfigurationSection section, Location loca
}

/**
* Limits the length of a string ignoring all colour codes within it
* Limits the length of a Component ignoring all default formats and styles within it
*
* @param string The string to limit
* @param component The string to limit
* @param maxLength The maximum number of characters to allow
* @return The string that is never longer than maxLength with chat colours stripped out
* @return The Component, where it's plain text part is never longer than maxLength
*/
public static String limitIgnoreCodes(String string, int maxLength) {
if (string.length() <= maxLength) {
return string;
public static Component limitIgnoreFormat(Component component, int maxLength) {
String formatted = MiniMessage.miniMessage().serialize(component);
String unformatted = MiniMessage.miniMessage().stripTags(formatted);

if (maxLength >= unformatted.length()) {
return component;
}

int size = 0;
int chompStart = -1;
for (int i = 0; i < string.length(); ++i) {
char c = string.charAt(i);
StringBuilder result = new StringBuilder();

if (c == ChatColor.COLOR_CHAR) {
++i;
continue;
}
int unformattedIndex = 0;
for (int formattedIndex = 0; formattedIndex < formatted.length(); formattedIndex++) {
char formattedChar = formatted.charAt(formattedIndex);

++size;
if (size > maxLength) {
chompStart = i;
break;
if (formattedChar == unformatted.charAt(unformattedIndex)) {
unformattedIndex++;

if (unformattedIndex > maxLength) {
break;
}
}
}

if (chompStart != -1) {
return string.substring(0, chompStart);
} else {
return string;
result.append(formattedChar);
}

// reassemble to component
return MiniMessage.miniMessage().deserialize(result.toString());
}

public static String sanitizeYamlString(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.text.WordUtils;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -62,17 +61,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Minigame miniga
if (mat != null && mat.isBlock()) {
minigame.getRecorderData().addWBBlock(mat);

Component addedMat;
if (mat.getBlockTranslationKey() != null) {
addedMat = Component.translatable(mat.getBlockTranslationKey());
} else if (mat.getItemTranslationKey() != null) {
addedMat = Component.translatable(mat.getItemTranslationKey());
} else {
addedMat = Component.text(WordUtils.capitalize(mat.toString().replace("_", " ").toLowerCase()));
}

MinigameMessageManager.sendMgMessage(sender, MinigameMessageType.INFO, MgCommandLangKey.COMMAND_SET_WHITELIST_ADDED,
Placeholder.component(MinigamePlaceHolderKey.MATERIAL.getKey(), addedMat),
Placeholder.component(MinigamePlaceHolderKey.MATERIAL.getKey(), Component.translatable(mat.translationKey())),
Placeholder.unparsed(MinigamePlaceHolderKey.MINIGAME.getKey(), minigame.getName(false)));
} else {
MinigameMessageManager.sendMgMessage(sender, MinigameMessageType.ERROR, MgCommandLangKey.COMMAND_ERROR_NOTMATERIAL,
Expand All @@ -84,17 +74,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Minigame miniga
if (mat != null) {
minigame.getRecorderData().removeWBBlock(mat);

Component removedMat;
if (mat.getBlockTranslationKey() != null) {
removedMat = Component.translatable(mat.getBlockTranslationKey());
} else if (mat.getItemTranslationKey() != null) {
removedMat = Component.translatable(mat.getItemTranslationKey());
} else {
removedMat = Component.text(WordUtils.capitalize(mat.toString().replace("_", " ").toLowerCase()));
}

MinigameMessageManager.sendMgMessage(sender, MinigameMessageType.INFO, MgCommandLangKey.COMMAND_SET_WHITELIST_REMOVE,
Placeholder.component(MinigamePlaceHolderKey.MATERIAL.getKey(), removedMat),
Placeholder.component(MinigamePlaceHolderKey.MATERIAL.getKey(), Component.translatable(mat.translationKey())),
Placeholder.unparsed(MinigamePlaceHolderKey.MINIGAME.getKey(), minigame.getName(false)));
} else {
MinigameMessageManager.sendMgMessage(sender, MinigameMessageType.ERROR, MgCommandLangKey.COMMAND_ERROR_NOTMATERIAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void loadValue(String path, FileConfiguration config) {
}

@Override
public MenuItem getMenuItem(@Nullable Material displayMaterial, @NotNull LangKey langKey) {
public MenuItemBoolean getMenuItem(@Nullable Material displayMaterial, @NotNull LangKey langKey) {
return new MenuItemBoolean(displayMaterial, langKey, new Callback<>() {

@Override
Expand All @@ -50,9 +50,9 @@ public void setValue(Boolean value) {
}

@Override
public MenuItem getMenuItem(@Nullable Material displayMat, @Nullable Component name,
public MenuItemBoolean getMenuItem(@Nullable Material displayMat, @Nullable Component name,
@Nullable List<@NotNull Component> description) {
return new MenuItemBoolean(name, description, displayMat, new Callback<>() {
return new MenuItemBoolean(displayMat, name, description, new Callback<>() {

@Override
public Boolean getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ public static String getUnformattedMgMessage(@NotNull LangKey key) throws Missin
return getMessageList(null, key, resolvers);
}

/**
* Reads a String from the ressource bundle, splits it at "{@code <newline>}" and then deserializes it via MiniMessage.
* This was first and formost written for Lore of ItemStacks, where newlines don't exist in the common way,
* but every line is an element of a list.
* This way a Component read by getMessage would look the same as getMessageList in different scenarios.
* And be hopefully intuitive how to make multiline lore in the translation files.
*/
public static @NotNull List<Component> getMessageList(@Nullable String identifier, @NotNull LangKey key, TagResolver... resolvers) {
MiniMessage miniMessage = MiniMessage.miniMessage();
MiniMessage miniMessage = MiniMessage.miniMessage(); // cached to use multiple times in stream

String unformatted = getUnformattedMessage(identifier, key);
//split at new line then deserialize to component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public enum MgMenuLangKey implements LangKey {
MENU_BLOCKDATA_CLICKBLOCK("menu.blockData.clickBlock"),
MENU_BLOCKDATA_DESCRIOPTION_EXTRA("menu.blockData.description.extra"),
MENU_BLOCKDATA_ERROR_INVALID("menu.blockData.error.invalid"),
MENU_DECIMAL_ENTERCHAT("menu.decimal.enterChat"),
MENU_DEFAULTWINNINGTEAM_NAME("menu.defaultWinningTeam.name"),
Expand All @@ -17,12 +18,16 @@ public enum MgMenuLangKey implements LangKey {
MENU_DISPLAYLOADOUT_NOTDELETE("menu.displayLoadout.notDelete"),
MENU_DISPLAYLOADOUT_SAVE_NAME("menu.displayLoadout.save.name"),
MENU_DISPLAYLOADOUT_SETTINGS_NAME("menu.displayLoadout.settings.name"),
MENU_ELEMENTNOTSET("menu.elementNotSet"),
MENU_ENUM_ERROR("menu.enum.error"),
MENU_FLAGADD_ENTERCHAT("menu.flagAdd.enterChat"),
MENU_FLAGADD_NAME("menu.flagAdd.name"),
MENU_HIERARCHY_ENTERCHAT("menu.hierarchy.enterChat"),
MENU_LOADOUT_SAVE("menu.displayLoadout.save.success"),
MENU_MATERIAL_DESCRIOPTION("menu.material.description"),
MENU_MONEYREWARD_ITEM_NAME("menu.moneyReward.item.name"),
MENU_MONEYREWARD_MENU_NAME("menu.moneyReward.menu.name"),
MENU_NUMBER_INFINITE("menu.number.infinite"),
MENU_PAGE_BACK("menu.page.back"),
MENU_PAGE_NEXT("menu.page.next"),
MENU_PAGE_PREVIOUS("menu.page.previous"),
Expand All @@ -34,6 +39,9 @@ public enum MgMenuLangKey implements LangKey {
MENU_POTIONADD_ENTERCHAT("menu.potionAdd.enterChat"),
MENU_POTIONADD_ERROR_SYNTAX("menu.potionAdd.error.syntax"),
MENU_POTIONADD_NAME("menu.potionAdd.name"),
MENU_POTION_DURATION("menu.potion.duration"),
MENU_POTION_LEVEL("menu.potion.level"),
MENU_REWARDPAIR_EDIT("menu.rewardPair.edit"),
MENU_TEAMADD_NAME("menu.teamAdd.name"),
MENU_TEAM_AUTOBALANCE("menu.team.autobalance"),
MENU_TEAM_DISPLAYNAME("menu.team.displayName"),
Expand All @@ -44,7 +52,8 @@ public enum MgMenuLangKey implements LangKey {
MENU_TEAM_NAMEVISIBILITY_NAME("menu.team.nameVisibility.name"),
MENU_TEAM_NAMEVISIBILITY_NEVERVISIBLE("menu.team.nameVisibility.neverVisible"),
MENU_WHITELIST_ENTERCHAT("menu.whitelist.enterChat"),
MENU_WHITELIST_ERROR_CONTAINS("menu.whitelist.error.contains");
MENU_WHITELIST_ERROR_CONTAINS("menu.whitelist.error.contains"),
MENU_WHITELIST_INTERACT("menu.whitelist.interact");

private final @NotNull String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum MinigameLangKey implements LangKey { //todo this gets rather big. Sl
MINIGAME_ERROR_NOTELEPORTALLOWED("minigame.error.noTeleportAllowed"),
MINIGAME_ERROR_NOTENABLED("minigame.error.notEnabled"),
MINIGAME_ERROR_NOTSTARTED("minigame.error.notStarted"),
MINIGAME_ERROR_NOTTEAMGAME("minigame.error.notTeamGame"),
MINIGAME_ERROR_REGENERATING("minigame.error.regenerating"),
MINIGAME_ERROR_STARTED("minigame.error.started"),
MINIGAME_FLAG_RETURNEDNEUTRAL("minigame.flag.returnedNeutral"),
Expand Down Expand Up @@ -153,6 +154,11 @@ public enum MinigameLangKey implements LangKey { //todo this gets rather big. Sl
REGION_REGENREGION_REMOVED("region.regenregion.removed"),
REGION_REGENREGION_UPDATED("region.regenRegion.updated"),
REGION_SELECT_POINT("region.select.point"),
REWARDRARITY_COMMON("rewardRarity.common"),
REWARDRARITY_NORMAL("rewardRarity.normal"),
REWARDRARITY_RARE("rewardRarity.rare"),
REWARDRARITY_VERYCOMMON("rewardRarity.veryCommon"),
REWARDRARITY_VERYRARE("rewardRarity.veryRare"),
REWARDSCHEME_ERROR_DUPLICATE("rewardscheme.error.duplicate"),
REWARDSCHEME_ERROR_INVALID("rewardscheme.error.invalid"),
REWARD_ERROR_NOVAULT("reward.error.noVault"),
Expand All @@ -178,6 +184,9 @@ public enum MinigameLangKey implements LangKey { //todo this gets rather big. Sl
SIGN_SCORE_ERROR_ALREADYUSEDTEAM("sign.score.error.alreadyUsedTeam"),
SIGN_TEAM_ERROR_UNBALANCE("sign.team.error.unbalance"),
SIGN_TELEPORT_INVALID("sign.teleport.invalid"),
TEAM_ADD("team.add"),
TEAM_ERROR_COLOR_INVALID("team.error.color.invalid"),
TEAM_ERROR_COLOR_TAKEN("team.error.color.taken"),
TIME_AND("time.and"),
TIME_DAYS_LONG("time.days.long"),
TIME_DAYS_SHORT("time.days.short"),
Expand Down Expand Up @@ -228,11 +237,7 @@ public enum MinigameLangKey implements LangKey { //todo this gets rather big. Sl
TOOL_SET_ENDLOCATION("tool.set.endLocation"),
TOOL_SET_LOBBYLOCATION("tool.set.lobbyLocation"),
TOOL_SET_QUITLOCATION("tool.set.quitLocation"),
TOOL_SET_SPECTATORLOCATION("tool.set.spectatorLocation"),
MINIGAME_ERROR_NOTTEAMGAME("minigame.error.notTeamGame"),
TEAM_ADD("team.add"),
TEAM_ERROR_COLOR_TAKEN("team.error.color.taken"),
TEAM_ERROR_COLOR_INVALID("team.error.color.invalid");
TOOL_SET_SPECTATORLOCATION("tool.set.spectatorLocation");

private final @NotNull String path;

Expand Down
43 changes: 31 additions & 12 deletions Minigames/src/main/java/au/com/mineauz/minigames/menu/MenuItem.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package au.com.mineauz.minigames.menu;

import au.com.mineauz.minigames.Minigames;
import au.com.mineauz.minigames.objects.IndexedMap;
import au.com.mineauz.minigames.objects.StrIntMapPersistentDataType;
import au.com.mineauz.minigames.managers.MinigameMessageManager;
import au.com.mineauz.minigames.managers.language.langkeys.LangKey;
import au.com.mineauz.minigames.objects.IndexedMap;
import au.com.mineauz.minigames.objects.StrIntMapPersistentDataType;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand All @@ -14,13 +14,15 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MenuItem {
public static final StrIntMapPersistentDataType STR_INT_MAP_TYPE = new StrIntMapPersistentDataType();
private static final StrIntMapPersistentDataType STR_INT_MAP_TYPE = new StrIntMapPersistentDataType();
private final static String BASE_DESCRIPTION_TOKEN = "Base_description";
private final static NamespacedKey DESCRIPTION_KEY = new NamespacedKey(Minigames.getPlugin(), "DescriptionOrder");
private @NotNull ItemStack displayItem;
private @Nullable Menu container = null;
Expand Down Expand Up @@ -48,8 +50,16 @@ public MenuItem(@Nullable Material displayMat, @Nullable Component name, @Nullab
this.displayItem = new ItemStack(displayMat);
ItemMeta meta = this.displayItem.getItemMeta();
meta.displayName(name);
if (description != null) meta.lore(description);
this.displayItem.setItemMeta(meta);

if (description == null) {
this.displayItem.setItemMeta(meta);
} else {
// clear automatically generated lore in case there was one
meta.lore(List.of());
this.displayItem.setItemMeta(meta);

setDescriptionPartAtIndex(BASE_DESCRIPTION_TOKEN, 0, description);
}
}

public MenuItem(@NotNull Component name, @NotNull ItemStack displayItem) {
Expand All @@ -67,7 +77,7 @@ public MenuItem(@NotNull Component name, @NotNull ItemStack displayItem) {
* @param descriptionPart the part of description to get written. If null the part will get removed.
*/
public void setDescriptionPartAtEnd(@NotNull String typeStr,
@Nullable List<@NotNull Component> descriptionPart) {
@Nullable List<@NotNull Component> descriptionPart) { // todo maybe instead of Persistent data container just use a map here; the menu will not get saved anyways
ItemMeta itemMeta = displayItem.getItemMeta();
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
IndexedMap<String, Integer> descriptionRegistry = container.get(DESCRIPTION_KEY, STR_INT_MAP_TYPE);
Expand Down Expand Up @@ -215,15 +225,19 @@ public List<Component> getDescription() {
return displayItem.getItemMeta().lore();
}

public void setDescription(List<Component> description) {
ItemMeta meta = displayItem.getItemMeta();

meta.lore(description);
displayItem.setItemMeta(meta);
/**
* Sets the description unter the base description token.
*
* @param description
* @see #setDescriptionPartAtEnd(String, List)
* @see #setDescriptionPartAtIndex(String, int, List)
*/
public void setBaseDescriptionPart(List<Component> description) {
setDescriptionPartAtIndex(BASE_DESCRIPTION_TOKEN, 0, description);
}

public @NotNull Component getName() {
return displayItem.getItemMeta().displayName();
return displayItem.displayName();
}

public @NotNull ItemStack getDisplayItem() {
Expand All @@ -236,6 +250,11 @@ public void setDisplayItem(@NotNull ItemStack item) {
ItemMeta nmeta = displayItem.getItemMeta();
nmeta.displayName(ometa.displayName());
nmeta.lore(nmeta.lore());
try {
nmeta.getPersistentDataContainer().readFromBytes(ometa.getPersistentDataContainer().serializeToBytes());
} catch (IOException e) {
Minigames.getCmpnntLogger().error("Could not carry over persistent data from one menuItem to another. Description Data might got lost!", e);
}
displayItem.setItemMeta(nmeta);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import java.util.List;

public class MenuItemAddWhitelistBlock extends MenuItem {
private final List<Material> whitelist;
protected final List<Material> whitelist;

public MenuItemAddWhitelistBlock(Component name, List<Material> whitelist) {
super(MenuUtility.getCreateMaterial(), name);
setDescription(List.of("Left Click with item to", "add to whitelist/blacklist", "Click without item to", "manually add item."));
super(MenuUtility.getCreateMaterial(), name,
MinigameMessageManager.getMgMessageList(MgMenuLangKey.MENU_WHITELIST_INTERACT));
this.whitelist = whitelist;
}

Expand Down
Loading

0 comments on commit d4e4fde

Please sign in to comment.