Skip to content

Commit

Permalink
misc: 使动作选择界面的物品支持Minimessage,并补齐界面中的空位
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Oct 10, 2024
1 parent f1b341a commit 87b6f20
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/main/java/xyz/nifeather/morph/messages/EmoteStrings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nifeather.morph.messages;

import org.jetbrains.annotations.NotNull;
import xyz.nifeather.morph.MorphPlugin;
import xiamomc.pluginbase.Messages.FormattableMessage;

Expand All @@ -12,6 +13,10 @@ public static FormattableMessage notAvailable()
{
return getFormattable(getKey("not_available"), "当前不能使用动作");
}
public static FormattableMessage none()
{
return getFormattable(getKey("none"), "<gray>[Fallback] 无");
}

private static final Map<String, FormattableMessage> map = new ConcurrentHashMap<>();

Expand All @@ -28,7 +33,7 @@ public static FormattableMessage Unknown()
return getFormattable("unknown", "animation:unknown");
}

public static FormattableMessage get(String animationId)
public static FormattableMessage get(@NotNull String animationId)
{
var value = map.getOrDefault(animationId, Unknown());

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/xyz/nifeather/morph/messages/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.nifeather.morph.messages;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -129,4 +130,12 @@ public static String getLocale(CommandSender sender)
else
return getServerLocale();
}

public static String asParsedMiniMessageString(String locale, FormattableMessage formattable)
{
if (formattable.getLocale() == null)
formattable.withLocale(locale);

return MiniMessage.miniMessage().serialize(formattable.toComponent());
}
}
2 changes: 1 addition & 1 deletion src/main/java/xyz/nifeather/morph/misc/DisguiseState.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public boolean canScheduleSequence()
/**
* @return Whether success.
*/
public boolean tryScheduleSequence(String sequenceIdentifier,
public boolean tryScheduleSequence(@NotNull String sequenceIdentifier,
List<SingleAnimation> sequence,
boolean persistent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@
import de.themoep.inventorygui.DynamicGuiElement;
import de.themoep.inventorygui.InventoryGui;
import de.themoep.inventorygui.StaticGuiElement;
import io.papermc.paper.adventure.providers.MiniMessageProviderImpl;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
import org.bukkit.Material;
import org.bukkit.block.data.Levelled;
import org.bukkit.inventory.ItemRarity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockDataMeta;
import org.jetbrains.annotations.Nullable;
import xiamomc.pluginbase.Bindables.BindableList;
import xyz.nifeather.morph.messages.EmoteStrings;
import xyz.nifeather.morph.messages.GuiStrings;
import xyz.nifeather.morph.messages.MessageUtils;
import xyz.nifeather.morph.messages.MorphStrings;
import xyz.nifeather.morph.misc.DisguiseState;

import java.util.List;
import java.util.function.Consumer;

public class AnimSelectScreenWrapper extends ScreenWrapper
{
/*
// 如果要启用这个,记得手动设置 capacity
private final BindableList<String> pattern = new BindableList<>(
List.of(
" " + getCurrentIndexChar(0) + " ",
getCurrentIndexChar(3) + "E" + getCurrentIndexChar(1),
" " + getCurrentIndexChar(2) + " "
));*/

private final BindableList<String> pattern = new BindableList<>(List.of(
"XXXXE"
));
Expand Down Expand Up @@ -98,22 +115,32 @@ private InventoryGui preparePage()
capacity += lineCapacity;
}

return new InventoryGui(plugin, GuiStrings.selectAnimation().toString(playerLocale), rows.toArray(new String[]{}));
var gui = new InventoryGui(plugin,
GuiStrings.selectAnimation().toString(playerLocale),
rows.toArray(new String[]{}));

gui.setItemNameSetter((meta, string) -> meta.itemName(defaultMiniMessage.deserialize(string)));

return gui;
}

private void initElements(InventoryGui gui)
{
var actionItemBase = IconLookup.instance().lookup(state.getDisguiseIdentifier()); //new ItemStack(Material.LIGHT);
var defaultIcon = IconLookup.instance().lookup(state.getDisguiseIdentifier()); //new ItemStack(Material.LIGHT);

if (IconLookup.instance().lookup(state.getDisguiseIdentifier()).getType() == Material.PLAYER_HEAD)
this.isDynamic.set(true);

for (int i = 0; i < Math.min(capacity, availableSequences.size()); i++)
for (int i = 0; i < capacity; i++)
{
@Nullable
var sequenceId = i >= availableSequences.size() ? null : availableSequences.get(i);

var guiChar = this.getCurrentIndexChar(i);
var itemClone = actionItemBase.clone();
var icon = sequenceId == null ? new ItemStack(Material.GRAY_STAINED_GLASS_PANE) : defaultIcon.clone();
int finalIndex = i;
itemClone.editMeta(meta ->

icon.editMeta(meta ->
{
meta.setRarity(ItemRarity.COMMON);

Expand All @@ -126,12 +153,18 @@ private void initElements(InventoryGui gui)
}
});

var sequenceId = availableSequences.get(i);
// String(Raw) -> Component(Parsed) -> String(Parsed MiniMessage format) -> Component(Parsed again)
// So terrible, couldn't we just use Component instead String to set the item name?
Component sequenceDisplayNameComponent = sequenceId == null
? EmoteStrings.none().toComponent(playerLocale)
: EmoteStrings.get(sequenceId).toComponent(playerLocale);

var sequenceDisplayName = EmoteStrings.get(sequenceId).withLocale(playerLocale).toString();
String sequenceDisplayName = defaultMiniMessage.serialize(sequenceDisplayNameComponent);

var element = new StaticGuiElement(guiChar, itemClone, 1 + i, click ->
var element = new StaticGuiElement(guiChar, icon, 1 + i, click ->
{
if (sequenceId == null) return true;

var animationSet = state.getProvider()
.getAnimationProvider()
.getAnimationSetFor(state.getDisguiseIdentifier());
Expand All @@ -141,9 +174,10 @@ private void initElements(InventoryGui gui)
getBindingPlayer().playSound(clickSound);
state.tryScheduleSequence(sequenceId, sequencePair.left(), sequencePair.right());
guiInstance.close();

return true;
},
"§r" + sequenceDisplayName);
"<italic:false>" + sequenceDisplayName);

gui.addElement(element);
}
Expand All @@ -152,9 +186,9 @@ private void initElements(InventoryGui gui)
new ItemStack(Material.PINK_STAINED_GLASS_PANE),
1,
click -> true,
"§r"));
"<italic:false>"));

var closeElementItem = new ItemStack(Material.BARRIER);
var closeElementItem = new ItemStack(Material.MAGENTA_GLAZED_TERRACOTTA);
closeElementItem.editMeta(meta -> meta.setRarity(ItemRarity.COMMON));

gui.addElement(new StaticGuiElement('E',
Expand All @@ -166,13 +200,13 @@ private void initElements(InventoryGui gui)
guiInstance.close();
return true;
},
"§r" + GuiStrings.close().toString(playerLocale)));
"<italic:false>" + GuiStrings.close().toString(playerLocale)));

var disguiseElement = new StaticGuiElement('D',
IconLookup.instance().lookup(state.getDisguiseIdentifier()),
1,
click -> true,
"§r" + MorphStrings.disguisingAsString().resolve("what", state.getPlayerDisplay())
"<italic:false>" + MorphStrings.disguisingAsString().resolve("what", state.getPlayerDisplay())
.toString(playerLocale));

if (isDynamic.get())
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/xyz/nifeather/morph/misc/gui/ScreenWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.themoep.inventorygui.InventoryGui;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player;
import xiamomc.pluginbase.Bindables.Bindable;
import xiamomc.pluginbase.ScheduleInfo;
Expand All @@ -20,6 +21,8 @@ public class ScreenWrapper extends MorphPluginObject
public static final Sound clickSound = Sound.sound().type(Key.key("ui.button.click")).volume(0.45f).build();
public static final Sound openSound = Sound.sound().type(Key.key("entity.experience_orb.pickup")).volume(0.55f).build();

protected static final MiniMessage defaultMiniMessage = MiniMessage.miniMessage();

protected Player getBindingPlayer()
{
return bindingPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void register(String sequenceID, List<SingleAnimation> sequence, boole
* @return A pair, left is the sequence, right is whether the sequence is persistent
*/
@NotNull
public Pair<List<SingleAnimation>, Boolean> sequenceOf(String animationId)
public Pair<List<SingleAnimation>, Boolean> sequenceOf(@NotNull String animationId)
{
return animationMap.getOrDefault(animationId, Pair.of(List.of(), false));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/feathermorph/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"emote.morphclient.crawl": "Crawl",
"emote.morphclient.digdown": "Dig Down",
"emote.morphclient.appear": "Appear",
"emote.morphclient.none": "<gray>None</gray>",
"emote.morphclient.not_available": "Can't play action while the skill is still in cooldown",
"emote.morphclient.unknown": "Unknown",
"chestui.next_page": "Next page",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/feathermorph/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"emote.morphclient.crawl": "趴下",
"emote.morphclient.digdown": "掘地",
"emote.morphclient.appear": "出现",
"emote.morphclient.none": "<gray>无</gray>",
"emote.morphclient.not_available": "技能冷却时不能播放动作",
"emote.morphclient.unknown": "未知",
"chestui.next_page": "下一页",
Expand Down

0 comments on commit 87b6f20

Please sign in to comment.