11package com .nisovin .magicspells ;
22
3+ import net .kyori .adventure .text .format .Style ;
4+ import net .kyori .adventure .text .format .TextDecoration ;
5+ import org .jetbrains .annotations .NotNull ;
6+
37import java .io .*;
48
59import java .util .*;
2529import com .google .common .collect .SetMultimap ;
2630import com .google .common .collect .LinkedHashMultimap ;
2731
32+ import net .kyori .adventure .text .format .TextColor ;
33+ import net .kyori .adventure .text .format .NamedTextColor ;
34+
2835import de .slikey .effectlib .EffectManager ;
2936
3037import org .bstats .bukkit .Metrics ;
3138import org .bstats .charts .SimplePie ;
3239import org .bstats .charts .AdvancedPie ;
3340import org .bstats .charts .DrilldownPie ;
3441
35- import org .jetbrains .annotations .NotNull ;
36-
37- import co .aikar .commands .PaperCommandManager ;
38-
3942import org .bukkit .*;
4043import org .bukkit .event .Event ;
4144import org .bukkit .entity .Entity ;
5356import org .bukkit .permissions .PermissionDefault ;
5457import org .bukkit .configuration .ConfigurationSection ;
5558
59+ import io .papermc .paper .command .brigadier .CommandSourceStack ;
60+
61+ import org .incendo .cloud .paper .PaperCommandManager ;
62+ import org .incendo .cloud .execution .ExecutionCoordinator ;
63+
5664import me .clip .placeholderapi .PlaceholderAPI ;
5765
5866import com .nisovin .magicspells .util .*;
6472import com .nisovin .magicspells .mana .ManaHandler ;
6573import com .nisovin .magicspells .variables .Variable ;
6674import com .nisovin .magicspells .spells .PassiveSpell ;
67- import com .nisovin .magicspells .commands .MagicCommand ;
6875import com .nisovin .magicspells .util .compat .EventUtil ;
76+ import com .nisovin .magicspells .commands .MagicCommands ;
6977import com .nisovin .magicspells .storage .StorageHandler ;
7078import com .nisovin .magicspells .util .prompt .PromptType ;
7179import com .nisovin .magicspells .util .compat .CompatBasics ;
7280import com .nisovin .magicspells .zones .NoMagicZoneManager ;
7381import com .nisovin .magicspells .spelleffects .SpellEffect ;
7482import com .nisovin .magicspells .util .magicitems .MagicItem ;
7583import com .nisovin .magicspells .castmodifiers .ModifierSet ;
76- import com .nisovin .magicspells .commands .CommandHelpFilter ;
7784import com .nisovin .magicspells .util .magicitems .MagicItems ;
7885import com .nisovin .magicspells .util .recipes .CustomRecipes ;
7986import com .nisovin .magicspells .util .ai .CustomGoalsManager ;
@@ -140,7 +147,8 @@ public class MagicSpells extends JavaPlugin {
140147 private NoMagicZoneManager zoneManager ;
141148 private CleanserManager cleanserManager ;
142149 private CustomGoalsManager customGoalsManager ;
143- private PaperCommandManager commandManager ;
150+ @ SuppressWarnings ("UnstableApiUsage" )
151+ private PaperCommandManager <CommandSourceStack > commandManager ;
144152 private ExperienceBarManager expBarManager ;
145153
146154 private MagicConfig config ;
@@ -209,7 +217,7 @@ public class MagicSpells extends JavaPlugin {
209217
210218 private long lastReloadTime = 0 ;
211219
212- private ChatColor textColor ;
220+ private Style textStyle ;
213221
214222 private double losRaySize ;
215223 private boolean losIgnorePassableBlocks ;
@@ -235,45 +243,8 @@ public class MagicSpells extends JavaPlugin {
235243 @ Override
236244 public void onEnable () {
237245 load ();
238-
239- Metrics metrics = new Metrics (this , 892 );
240-
241- metrics .addCustomChart (new DrilldownPie ("spells" , () -> {
242- Map <String , Map <String , Integer >> map = new HashMap <>();
243- if (spells == null ) return map ;
244-
245- for (Spell spell : spells .values ()) {
246- String name = spell .getClass ().getName ();
247- if (!name .startsWith ("com.nisovin.magicspells.spells" )) continue ;
248- name = name .replace ("com.nisovin.magicspells.spells." , "" );
249-
250- String [] typeSplit = name .split ("\\ ." , 2 );
251- String formalPackage = typeSplit [0 ].substring (0 , 1 ).toUpperCase () + typeSplit [0 ].substring (1 );
252-
253- String spellPackage = (typeSplit .length == 1 ? "General" : formalPackage ) + " Spells" ;
254- String spellClass = typeSplit .length == 1 ? typeSplit [0 ] : typeSplit [1 ];
255-
256- map .computeIfAbsent (spellPackage , key -> new HashMap <>());
257- map .get (spellPackage ).compute (spellClass , (k , v ) -> (v == null ? 0 : v ) + 1 );
258- }
259- return map ;
260- }));
261- metrics .addCustomChart (new AdvancedPie ("passive_listeners" , () -> {
262- IntMap <String > map = new IntMap <>();
263- if (spells == null ) return map ;
264-
265- for (Spell spell : spells .values ()) {
266- if (!spell .getClass ().getName ().startsWith ("com.nisovin.magicspells.spells" )) continue ;
267- if (!(spell instanceof PassiveSpell passiveSpell )) continue ;
268-
269- for (PassiveListener listener : passiveSpell .getPassiveListeners ()) {
270- String name = listener .getClass ().getSimpleName ();
271- map .increment (name .substring (0 , name .lastIndexOf ("Listener" )));
272- }
273- }
274- return map ;
275- }));
276- metrics .addCustomChart (new SimplePie ("reload_time" , () -> "<" + (lastReloadTime - lastReloadTime % 500 + 500 ) + " ms" ));
246+ initMetrics ();
247+ initCommands ();
277248 }
278249
279250 public void load () {
@@ -284,8 +255,6 @@ public void load() {
284255 effectManager = new EffectManager (this );
285256 effectManager .enableDebug (debug );
286257
287- commandManager = new PaperCommandManager (plugin );
288-
289258 // Create storage stuff
290259 spells = new HashMap <>();
291260 spellNames = new HashMap <>();
@@ -331,7 +300,6 @@ public void load() {
331300 enableErrorLogging = config .getBoolean (path + "enable-error-logging" , true );
332301 errorLogLimit = config .getInt (path + "error-log-limit" , -1 );
333302 enableProfiling = config .getBoolean (path + "enable-profiling" , false );
334- textColor = ChatColor .getByChar (config .getString (path + "text-color" , ChatColor .DARK_AQUA .getChar () + "" ));
335303 broadcastRange = config .getInt (path + "broadcast-range" , 20 );
336304 effectlibInstanceLimit = config .getInt (path + "effectlib-instance-limit" , 20000 );
337305
@@ -402,6 +370,8 @@ public void load() {
402370 }
403371 }
404372
373+ textStyle = Util .getStyle (config .getString (path + "text-style" , config .getString ("text-color" , null )), Style .style (NamedTextColor .DARK_AQUA ));
374+
405375 soundFailOnCooldown = config .getString (path + "sound-on-cooldown" , null );
406376 soundFailMissingReagents = config .getString (path + "sound-missing-reagents" , null );
407377
@@ -574,12 +544,6 @@ public void load() {
574544 magicLogger = new MagicLogger (this );
575545 }
576546
577- // Register commands
578- commandManager .enableUnstableAPI ("help" );
579- commandManager .registerCommand (new MagicCommand ());
580- commandManager .setValidNamePredicate (string -> true );
581- CommandHelpFilter .mapPerms ();
582-
583547 // Setup profiling
584548 if (enableProfiling ) {
585549 profilingTotalTime = new HashMap <>();
@@ -592,6 +556,56 @@ public void load() {
592556 Bukkit .getScheduler ().runTaskLater (this , this ::loadExternalData , 1 );
593557 }
594558
559+ private void initMetrics () {
560+ Metrics metrics = new Metrics (this , 892 );
561+
562+ metrics .addCustomChart (new DrilldownPie ("spells" , () -> {
563+ Map <String , Map <String , Integer >> map = new HashMap <>();
564+ if (spells == null ) return map ;
565+
566+ for (Spell spell : spells .values ()) {
567+ String name = spell .getClass ().getName ();
568+ if (!name .startsWith ("com.nisovin.magicspells.spells" )) continue ;
569+ name = name .replace ("com.nisovin.magicspells.spells." , "" );
570+
571+ String [] typeSplit = name .split ("\\ ." , 2 );
572+ String formalPackage = typeSplit [0 ].substring (0 , 1 ).toUpperCase () + typeSplit [0 ].substring (1 );
573+
574+ String spellPackage = (typeSplit .length == 1 ? "General" : formalPackage ) + " Spells" ;
575+ String spellClass = typeSplit .length == 1 ? typeSplit [0 ] : typeSplit [1 ];
576+
577+ map .computeIfAbsent (spellPackage , key -> new HashMap <>());
578+ map .get (spellPackage ).compute (spellClass , (k , v ) -> (v == null ? 0 : v ) + 1 );
579+ }
580+ return map ;
581+ }));
582+ metrics .addCustomChart (new AdvancedPie ("passive_listeners" , () -> {
583+ IntMap <String > map = new IntMap <>();
584+ if (spells == null ) return map ;
585+
586+ for (Spell spell : spells .values ()) {
587+ if (!spell .getClass ().getName ().startsWith ("com.nisovin.magicspells.spells" )) continue ;
588+ if (!(spell instanceof PassiveSpell passiveSpell )) continue ;
589+
590+ for (PassiveListener listener : passiveSpell .getPassiveListeners ()) {
591+ String name = listener .getClass ().getSimpleName ();
592+ map .increment (name .substring (0 , name .lastIndexOf ("Listener" )));
593+ }
594+ }
595+ return map ;
596+ }));
597+ metrics .addCustomChart (new SimplePie ("reload_time" , () -> "<" + (lastReloadTime - lastReloadTime % 500 + 500 ) + " ms" ));
598+ }
599+
600+ @ SuppressWarnings ("UnstableApiUsage" )
601+ private void initCommands () {
602+ commandManager = PaperCommandManager .builder ()
603+ .executionCoordinator (ExecutionCoordinator .simpleCoordinator ())
604+ .buildOnEnable (this );
605+
606+ MagicCommands .register (commandManager );
607+ }
608+
595609 private void initializeSpells () {
596610 log ("Initializing spells..." );
597611
@@ -1081,8 +1095,31 @@ public static Spellbook getSpellbook(Player player) {
10811095 return plugin .spellbooks .computeIfAbsent (player .getName (), playerName -> new Spellbook (player ));
10821096 }
10831097
1098+ @ Deprecated (forRemoval = true )
10841099 public static ChatColor getTextColor () {
1085- return plugin .textColor ;
1100+ TextColor color = plugin .textStyle .color ();
1101+ if (color != null ) {
1102+ NamedTextColor nearest = NamedTextColor .nearestTo (color );
1103+ return ChatColor .valueOf (nearest .toString ().toUpperCase ());
1104+ }
1105+
1106+ return plugin .textStyle .decorations ().entrySet ().stream ()
1107+ .filter (entry -> entry .getValue () == TextDecoration .State .TRUE )
1108+ .map (Map .Entry ::getKey )
1109+ .sorted ()
1110+ .findFirst ()
1111+ .map (decoration -> switch (decoration ) {
1112+ case OBFUSCATED -> ChatColor .MAGIC ;
1113+ case BOLD -> ChatColor .BOLD ;
1114+ case STRIKETHROUGH -> ChatColor .STRIKETHROUGH ;
1115+ case UNDERLINED -> ChatColor .UNDERLINE ;
1116+ case ITALIC -> ChatColor .ITALIC ;
1117+ })
1118+ .orElse (ChatColor .DARK_AQUA );
1119+ }
1120+
1121+ public static Style getTextStyle () {
1122+ return plugin .textStyle ;
10861123 }
10871124
10881125 /**
@@ -1250,6 +1287,10 @@ public static int getDebugLevelOriginal() {
12501287 return plugin .debugLevelOriginal ;
12511288 }
12521289
1290+ public static int getDebugLevel () {
1291+ return plugin .debugLevel ;
1292+ }
1293+
12531294 public static int getErrorLogLimit () {
12541295 return plugin .errorLogLimit ;
12551296 }
@@ -1584,7 +1625,7 @@ public static void sendMessage(String message, LivingEntity recipient, SpellData
15841625
15851626 message = doReplacements (message , recipient , data , replacements );
15861627
1587- recipient .sendMessage (Util .getMiniMessage (getTextColor () + message ));
1628+ recipient .sendMessage (Util .getMiniMessage (message ). applyFallbackStyle ( MagicSpells . getTextStyle () ));
15881629 }
15891630
15901631 private static final Pattern chatVarMatchPattern = Pattern .compile ("%var:(\\ w+)(?::(\\ d+))?%" , Pattern .CASE_INSENSITIVE | Pattern .MULTILINE );
@@ -1889,13 +1930,13 @@ public static boolean requireReplacement(String message) {
18891930 public static String getTargetName (Entity target ) {
18901931 if (target instanceof Player ) return target .getName ();
18911932
1892- if (target .customName () != null ) return Util .getStrictStringFromComponent (target .customName ());
1933+ if (target .customName () != null ) return Util .getStrictString (target .customName ());
18931934
18941935 EntityType type = target .getType ();
18951936 String name = plugin .entityNames .get (type );
18961937 if (name != null ) return name ;
18971938
1898- return Util .getStrictStringFromComponent (target .name ());
1939+ return Util .getStrictString (target .name ());
18991940 }
19001941
19011942 public static void registerEvents (final Listener listener ) {
0 commit comments