33import com .sk89q .worldedit .regions .Region ;
44import fr .mrmicky .worldeditselectionvisualizer .commands .CommandWesv ;
55import fr .mrmicky .worldeditselectionvisualizer .compat .CompatibilityHelper ;
6+ import fr .mrmicky .worldeditselectionvisualizer .compat .ScheduledTask ;
7+ import fr .mrmicky .worldeditselectionvisualizer .compat .SchedulerAdapter ;
68import fr .mrmicky .worldeditselectionvisualizer .config .ConfigurationManager ;
79import fr .mrmicky .worldeditselectionvisualizer .config .GlobalSelectionConfig ;
810import fr .mrmicky .worldeditselectionvisualizer .display .DisplayType ;
1618import fr .mrmicky .worldeditselectionvisualizer .utils .ChatUtils ;
1719import org .bukkit .entity .Player ;
1820import org .bukkit .plugin .java .JavaPlugin ;
19- import org .bukkit .scheduler .BukkitTask ;
20- import org .jspecify .annotations .NonNull ;
21+ import org .jspecify .annotations .NullMarked ;
2122
2223import java .io .BufferedReader ;
23- import java .io .File ;
2424import java .io .IOException ;
2525import java .io .InputStreamReader ;
2626import java .net .URI ;
2727import java .net .URL ;
2828import java .util .*;
29+ import java .util .concurrent .ConcurrentHashMap ;
2930
31+ @ NullMarked
3032public final class WorldEditSelectionVisualizer extends JavaPlugin {
3133
3234 private final Map <SelectionType , GlobalSelectionConfig > configurations = new EnumMap <>(SelectionType .class );
33- private final Map <UUID , PlayerVisualizerData > players = new HashMap <>();
34- private final List <BukkitTask > particlesTasks = new ArrayList <>(6 );
35+ private final Map <UUID , PlayerVisualizerData > players = new ConcurrentHashMap <>();
36+ private final List <ScheduledTask > particlesTasks = new ArrayList <>(6 );
3537
36- private SelectionManager selectionManager ;
37- private StorageManager storageManager ;
38- private ConfigurationManager configurationManager ;
39- private CompatibilityHelper compatibilityHelper ;
38+ private final SelectionManager selectionManager = new SelectionManager ( this ) ;
39+ private final StorageManager storageManager = new StorageManager ( this ) ;
40+ private final ConfigurationManager configurationManager = new ConfigurationManager ( this ) ;
41+ private final CompatibilityHelper compatibilityHelper = new CompatibilityHelper ( this ) ;
4042
4143 @ Override
4244 public void onEnable () {
4345 saveDefaultConfig ();
44- migrateV1Config ();
4546
46- this .compatibilityHelper = new CompatibilityHelper (this );
47- this .storageManager = new StorageManager (this );
48- this .configurationManager = new ConfigurationManager (this );
49- this .selectionManager = new SelectionManager (this );
47+ this .compatibilityHelper .init ();
48+ this .storageManager .init ();
49+ this .selectionManager .init ();
50+
51+ getLogger ().info ("Using WorldEdit " + this .compatibilityHelper .getWorldEditVersion () + " api" );
52+ getLogger ().info ("Using " + this .getScheduler ().name () + " scheduler" );
5053
5154 if (this .compatibilityHelper .getWorldEditVersion () == 7 ) {
5255 try {
5356 Region .class .getMethod ("getVolume" );
5457 } catch (NoSuchMethodException e ) {
55- getLogger ().severe ("**********************************" );
56- getLogger ().severe ("You are using an unsupported WorldEdit version (7.0.x or 7.1.x)!" );
57- getLogger ().severe ("WorldEditSelection visualizer doesn't works with outdated WorldEdit version." );
58- getLogger ().severe ("You can download the latest WorldEdit version here: https://dev.bukkit.org/projects/worldedit/files" );
59- getLogger ().severe ("**********************************" );
58+ getLogger ().severe ("You are using an unsupported WorldEdit version (7.0.x or 7.1.x)" );
59+ getLogger ().severe ("Please update WorldEdit: https://dev.bukkit.org/projects/worldedit/files" );
6060 getServer ().getPluginManager ().disablePlugin (this );
6161 return ;
6262 }
@@ -74,25 +74,21 @@ public void onEnable() {
7474 }
7575
7676 if (getConfig ().getBoolean ("check-updates" )) {
77- getServer (). getScheduler ().runTaskAsynchronously ( this , this ::checkUpdate );
77+ getScheduler ().executeAsync ( this ::checkUpdate );
7878 }
7979 }
8080
8181 @ Override
8282 public void reloadConfig () {
8383 super .reloadConfig ();
8484
85- if (this .configurationManager != null ) {
86- loadConfig ();
87- }
85+ loadConfig ();
8886
89- if (this .compatibilityHelper != null ) {
90- this .compatibilityHelper .init ();
91- }
87+ this .compatibilityHelper .init ();
9288 }
9389
9490 private void loadConfig () {
95- this .particlesTasks .forEach (BukkitTask ::cancel );
91+ this .particlesTasks .forEach (ScheduledTask ::cancel );
9692 this .particlesTasks .clear ();
9793
9894 for (SelectionType type : SelectionType .values ()) {
@@ -107,15 +103,11 @@ private void loadConfig() {
107103 }
108104 }
109105
110- public void updateHoldingSelectionItem (@ NonNull PlayerVisualizerData playerData ) {
106+ public void updateHoldingSelectionItem (PlayerVisualizerData playerData ) {
111107 playerData .setHoldingSelectionItem (this .compatibilityHelper .isHoldingSelectionItem (playerData .getPlayer ()));
112108 }
113109
114- public void loadPlayer (@ NonNull Player player ) {
115- if (this .players .containsKey (player .getUniqueId ())) {
116- return ;
117- }
118-
110+ public void loadPlayer (Player player ) {
119111 PlayerVisualizerData playerData = new PlayerVisualizerData (player );
120112
121113 for (SelectionType type : SelectionType .values ()) {
@@ -125,14 +117,14 @@ public void loadPlayer(@NonNull Player player) {
125117
126118 updateHoldingSelectionItem (playerData );
127119
128- this .players .put (player .getUniqueId (), playerData );
120+ this .players .putIfAbsent (player .getUniqueId (), playerData );
129121 }
130122
131- public void unloadPlayer (@ NonNull Player player ) {
123+ public void unloadPlayer (Player player ) {
132124 this .players .remove (player .getUniqueId ());
133125 }
134126
135- public @ NonNull PlayerVisualizerData getPlayerData (Player player ) {
127+ public PlayerVisualizerData getPlayerData (Player player ) {
136128 PlayerVisualizerData playerData = this .players .get (player .getUniqueId ());
137129
138130 if (playerData == null ) {
@@ -142,11 +134,11 @@ public void unloadPlayer(@NonNull Player player) {
142134 return playerData ;
143135 }
144136
145- public @ NonNull Optional <PlayerVisualizerData > getOptionalPlayerData (Player player ) {
137+ public Optional <PlayerVisualizerData > getOptionalPlayerData (Player player ) {
146138 return Optional .ofNullable (this .players .get (player .getUniqueId ()));
147139 }
148140
149- public @ NonNull Collection <PlayerVisualizerData > getPlayers () {
141+ public Collection <PlayerVisualizerData > getPlayers () {
150142 return this .players .values ();
151143 }
152144
@@ -170,35 +162,27 @@ public String getMessage(String path) {
170162 return ChatUtils .color (getConfig ().getString ("messages." + path ));
171163 }
172164
165+ public SchedulerAdapter getScheduler () {
166+ return this .compatibilityHelper .getScheduler ();
167+ }
168+
169+ @ SuppressWarnings ("deprecation" ) // Ignore Paper-specific deprecations (for compatibility)
170+ public String getPluginVersion () {
171+ return getDescription ().getVersion ();
172+ }
173+
173174 private void checkUpdate () {
174175 try {
175176 URL url = URI .create ("https://api.spigotmc.org/legacy/update.php?resource=17311" ).toURL ();
176177 try (BufferedReader reader = new BufferedReader (new InputStreamReader (url .openStream ()))) {
177178 String lastVersion = reader .readLine ();
178- if (!getDescription (). getVersion ().equalsIgnoreCase (lastVersion )) {
179- getLogger ().warning ("A new version is available. Last version is " + lastVersion + " and you are on " + getDescription (). getVersion ());
180- getLogger ().warning ("You can download it on " + getDescription (). getWebsite () );
179+ if (!getPluginVersion ().equalsIgnoreCase (lastVersion )) {
180+ getLogger ().warning ("A new version is available. Last version is " + lastVersion + " and you are on " + getPluginVersion ());
181+ getLogger ().warning ("You can download it on https://www.spigotmc.org/resources/worldeditselectionvisualizer.17311/" );
181182 }
182183 }
183184 } catch (IOException e ) {
184185 getLogger ().warning ("Unable to check for updates: " + e .getMessage ());
185186 }
186187 }
187-
188- private void migrateV1Config () {
189- if (getConfig ().get ("lang" ) == null ) {
190- return ;
191- }
192-
193- // Rename the config from WorldEditSelectionVisualizer v1.x to prevent any issues
194- File configFile = new File (getDataFolder (), "config.yml" );
195- File configBackupFile = new File (getDataFolder (), "config-old.yml" );
196-
197- if (!configFile .renameTo (configBackupFile )) {
198- getLogger ().warning ("Unable to rename old config.yml file." );
199- }
200-
201- saveDefaultConfig ();
202- reloadConfig ();
203- }
204188}
0 commit comments