Rename internal CMagicGem implementation
This commit is contained in:
1 parent
953c15e380
commit
ff9b6ce4f1
68 files changed
+298
-306
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package cn.infstar.magicgem.util;
|
||||
|
||||
import cn.infstar.magicgem.config.LanguageManager;
|
||||
import cn.infstar.magicgem.dsl.Directive;
|
||||
import cn.infstar.magicgem.dsl.DirectiveParser;
|
||||
import cn.infstar.magicgem.integration.PlaceholderBridge;
|
||||
import cn.infstar.magicgem.item.ItemCodec;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class MessageDispatcher {
|
||||
private final JavaPlugin plugin;
|
||||
private final ItemCodec codec;
|
||||
private final DirectiveParser parser;
|
||||
private final PlaceholderBridge placeholders;
|
||||
private final LanguageManager languages;
|
||||
|
||||
public MessageDispatcher(JavaPlugin plugin, ItemCodec codec, DirectiveParser parser, PlaceholderBridge placeholders,
|
||||
LanguageManager languages) {
|
||||
this.plugin = plugin;
|
||||
this.codec = codec;
|
||||
this.parser = parser;
|
||||
this.placeholders = placeholders;
|
||||
this.languages = languages;
|
||||
}
|
||||
|
||||
public void send(CommandSender receiver, String source) {
|
||||
if (source == null || source.isBlank() || source.equalsIgnoreCase("none")) return;
|
||||
if (!(receiver instanceof Player player) || !source.contains("{")) {
|
||||
receiver.sendMessage(codec.color(source));
|
||||
return;
|
||||
}
|
||||
Directive directive;
|
||||
try { directive = parser.parse(source); }
|
||||
catch (RuntimeException ignored) { player.sendMessage(codec.color(source)); return; }
|
||||
String type = directive.type().toLowerCase(Locale.ROOT);
|
||||
List<String> messages = parser.splitTopLevel(directive.argument("", "messages", "message", "m"), ';');
|
||||
int interval = Math.max(0, directive.intArgument(0, "interval"));
|
||||
if (type.equals("title")) {
|
||||
for (int index = 0; index < messages.size(); index += 2) {
|
||||
String title = resolve(player, messages.get(index));
|
||||
String subtitle = resolve(player, index + 1 < messages.size() ? messages.get(index + 1) : "");
|
||||
int delay = interval * (index / 2);
|
||||
later(delay, () -> player.showTitle(Title.title(component(title), component(subtitle), Title.Times.times(
|
||||
Duration.ofMillis(directive.intArgument(10, "fadein") * 50L),
|
||||
Duration.ofMillis(directive.intArgument(70, "stay") * 50L),
|
||||
Duration.ofMillis(directive.intArgument(20, "fadeout") * 50L)))));
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (int index = 0; index < messages.size(); index++) {
|
||||
String message = resolve(player, messages.get(index));
|
||||
int delay = interval * index;
|
||||
later(delay, () -> {
|
||||
if (type.equals("actionbar")) player.sendActionBar(component(message));
|
||||
else player.sendMessage(codec.color(message));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void cooldown(Player player, String configured, double remaining, double total) {
|
||||
if (configured == null || configured.isBlank()) {
|
||||
send(player, languages.text("cmagic.cooldown", "&e冷却中,剩余 {seconds} 秒",
|
||||
"seconds", String.format(Locale.ROOT, "%.2f", remaining)));
|
||||
return;
|
||||
}
|
||||
int open = configured.indexOf('{');
|
||||
int close = configured.lastIndexOf('}');
|
||||
if (open < 0 || close < open) { send(player, configured); return; }
|
||||
Map<String, String> arguments = new LinkedHashMap<>();
|
||||
for (String token : configured.substring(open + 1, close).split(";")) {
|
||||
int equals = token.indexOf('=');
|
||||
if (equals > 0) arguments.put(token.substring(0, equals).trim().toLowerCase(Locale.ROOT), token.substring(equals + 1));
|
||||
}
|
||||
int length;
|
||||
try { length = Math.max(0, Integer.parseInt(arguments.getOrDefault("length", "0"))); }
|
||||
catch (NumberFormatException ignored) { length = 0; }
|
||||
int full = total <= 0 ? 0 : (int) Math.round(length * Math.max(0, Math.min(1, 1 - remaining / total)));
|
||||
String suffix;
|
||||
try { suffix = String.format(Locale.ROOT, arguments.getOrDefault("s", "%.2fs"), remaining); }
|
||||
catch (RuntimeException ignored) { suffix = String.format(Locale.ROOT, "%.2fs", remaining); }
|
||||
String message = arguments.getOrDefault("p", "") + arguments.getOrDefault("full", "|").repeat(full)
|
||||
+ arguments.getOrDefault("empty", "|").repeat(Math.max(0, length - full)) + suffix;
|
||||
send(player, configured.substring(0, open) + "{m=" + message + '}');
|
||||
}
|
||||
|
||||
private String resolve(Player player, String message) {
|
||||
return placeholders.resolve(player, message);
|
||||
}
|
||||
|
||||
private void later(int delay, Runnable action) {
|
||||
if (delay <= 0) action.run(); else Bukkit.getScheduler().runTaskLater(plugin, action, delay);
|
||||
}
|
||||
|
||||
private Component component(String message) {
|
||||
return LegacyComponentSerializer.legacyAmpersand().deserialize(message == null ? "" : message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user