Skip to content

Commit 8085e8c

Browse files
committed
final refactoring
1 parent 0283a3d commit 8085e8c

File tree

14 files changed

+252
-349
lines changed

14 files changed

+252
-349
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package simplexity.simplefly;
22

3-
import net.kyori.adventure.text.minimessage.MiniMessage;
43
import org.bukkit.NamespacedKey;
4+
import org.bukkit.permissions.Permission;
5+
import org.bukkit.permissions.PermissionDefault;
56

67
public class Constants {
78

89

910

10-
public static final NamespacedKey FLY_STATUS = new NamespacedKey(SimpleFly.getInstance(), "flystatus");
11-
public static final String FLY_PERMISSION = "simplefly.fly";
12-
public static final String FLY_SPEED_PERMISSION = "simplefly.flyspeed";
13-
public static final String FLY_OTHERS_PERMISSION = "simplefly.others.fly";
14-
public static final String FLY_SPEED_OTHERS_PERMISSION = "simplefly.others.flyspeed";
15-
public static final String FLY_RELOAD = "simplefly.reload";
11+
public static final NamespacedKey FLY_STATUS_KEY = new NamespacedKey(SimpleFly.getInstance(), "flystatus");
12+
public static final Permission FLY_PERMISSION = new Permission("simplefly.fly", "Allows a player to toggle their own flight", PermissionDefault.OP);
13+
public static final Permission FLY_SPEED_PERMISSION = new Permission("simplefly.flyspeed", "Allows a player to set their own flight speed", PermissionDefault.OP);
14+
public static final Permission FLY_OTHERS_PERMISSION = new Permission("simplefly.others.fly", "Allows a player to toggle other players' flight", PermissionDefault.OP);
15+
public static final Permission FLY_SPEED_OTHERS_PERMISSION = new Permission("simplefly.others.flyspeed", "Allows a player to set other players' flight speed", PermissionDefault.OP);
16+
public static final Permission FLY_RELOAD_PERMISSION = new Permission("simplefly.reload", "Allows a player to reload the SimpleFly configuration", PermissionDefault.OP);
1617

1718
}

src/main/java/simplexity/simplefly/FlyListeners.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
public class FlyListeners implements Listener {
1919

20-
private static final NamespacedKey flyStatus = Constants.FLY_STATUS;
20+
private static final NamespacedKey flyStatus = Constants.FLY_STATUS_KEY;
2121

2222
@EventHandler
2323
public void onPlayerLogin(PlayerJoinEvent joinEvent) {
24-
if (!ConfigHandler.sessionPersistentFlight) {
24+
if (!ConfigHandler.getInstance().isFlyPersistsSessions()) {
2525
return;
2626
}
2727
Player player = joinEvent.getPlayer();
@@ -42,7 +42,7 @@ public void onPlayerLogin(PlayerJoinEvent joinEvent) {
4242

4343
@EventHandler
4444
public void onWorldChange(PlayerChangedWorldEvent worldEvent) {
45-
if (!ConfigHandler.worldChangePersistentFlight) return;
45+
if (!ConfigHandler.getInstance().isFlyPersistsWorldChange()) return;
4646
Player player = worldEvent.getPlayer();
4747
PersistentDataContainer playerPDC = player.getPersistentDataContainer();
4848
Boolean flyEnabled = playerPDC.getOrDefault(flyStatus, PersistentDataType.BOOLEAN, false);
@@ -53,7 +53,7 @@ public void onWorldChange(PlayerChangedWorldEvent worldEvent) {
5353

5454
@EventHandler
5555
public void onRespawn(PlayerRespawnEvent respawnEvent) {
56-
if (!ConfigHandler.respawnPersistentFlight) return;
56+
if (!ConfigHandler.getInstance().isFlyPersistsRespawn()) return;
5757
Player player = respawnEvent.getPlayer();
5858
PersistentDataContainer playerPDC = player.getPersistentDataContainer();
5959
Boolean flyEnabled = playerPDC.getOrDefault(flyStatus, PersistentDataType.BOOLEAN, false);
@@ -63,8 +63,8 @@ public void onRespawn(PlayerRespawnEvent respawnEvent) {
6363
}
6464

6565
@EventHandler
66-
public void onGamemodeChange(PlayerGameModeChangeEvent gameModeChangeEvent) {
67-
if (!ConfigHandler.gamemodeChangePersistentFlight) return;
66+
public void onGameModeChange(PlayerGameModeChangeEvent gameModeChangeEvent) {
67+
if (!ConfigHandler.getInstance().isFlyPersistsGameMode()) return;
6868
Player player = gameModeChangeEvent.getPlayer();
6969
PersistentDataContainer playerPDC = player.getPersistentDataContainer();
7070
Bukkit.getScheduler().runTaskLater(SimpleFly.getInstance(), () -> {

src/main/java/simplexity/simplefly/FlyLogic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class FlyLogic {
1111

12-
private static final NamespacedKey flyStatus = Constants.FLY_STATUS;
12+
private static final NamespacedKey flyStatus = Constants.FLY_STATUS_KEY;
1313

1414
public static boolean flyToggle(Player player) {
1515
PersistentDataContainer playerPDC = player.getPersistentDataContainer();

src/main/java/simplexity/simplefly/SimpleFly.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,42 @@
22

33
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
44
import net.kyori.adventure.text.minimessage.MiniMessage;
5-
import org.bukkit.Server;
65
import org.bukkit.plugin.java.JavaPlugin;
76
import simplexity.simplefly.commands.Fly;
87
import simplexity.simplefly.commands.FlyReload;
8+
import simplexity.simplefly.commands.FlySpeed;
99
import simplexity.simplefly.config.ConfigHandler;
1010

11+
@SuppressWarnings("UnstableApiUsage")
1112
public final class SimpleFly extends JavaPlugin {
12-
13+
1314
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
1415
private static SimpleFly instance;
15-
private static Server flyServer;
16-
16+
1717
public static SimpleFly getInstance() {
1818
return instance;
1919
}
20-
20+
2121
public static MiniMessage getMiniMessage() {
2222
return miniMessage;
2323
}
24-
25-
public static Server getFlyServer() {
26-
return flyServer;
27-
}
28-
29-
24+
3025
@Override
3126
public void onEnable() {
3227
instance = this;
33-
flyServer = this.getServer();
3428
this.saveDefaultConfig();
35-
ConfigHandler.reloadConfigValues();
29+
ConfigHandler.getInstance().reloadConfigValues();
3630
this.getServer().getPluginManager().registerEvents(new simplexity.simplefly.FlyListeners(), this);
3731
this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, commands -> {
3832
commands.registrar().register(Fly.createCommand().build());
3933
commands.registrar().register(FlyReload.createCommand().build());
34+
commands.registrar().register(FlySpeed.createCommand().build());
4035
});
36+
this.getServer().getPluginManager().addPermission(Constants.FLY_PERMISSION);
37+
this.getServer().getPluginManager().addPermission(Constants.FLY_OTHERS_PERMISSION);
38+
this.getServer().getPluginManager().addPermission(Constants.FLY_SPEED_PERMISSION);
39+
this.getServer().getPluginManager().addPermission(Constants.FLY_SPEED_OTHERS_PERMISSION);
40+
this.getServer().getPluginManager().addPermission(Constants.FLY_RELOAD_PERMISSION);
4141
}
42-
42+
4343
}

src/main/java/simplexity/simplefly/Util.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/simplexity/simplefly/commands/Exceptions.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package simplexity.simplefly.commands;
22

33
import com.mojang.brigadier.Message;
4-
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
54
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
65
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
76
import net.kyori.adventure.text.minimessage.MiniMessage;
87
import simplexity.simplefly.SimpleFly;
98
import simplexity.simplefly.config.LocaleMessage;
109

10+
@SuppressWarnings("UnstableApiUsage")
1111
public class Exceptions {
1212
private static final MiniMessage miniMessage = SimpleFly.getMiniMessage();
1313

@@ -17,10 +17,6 @@ public class Exceptions {
1717
public static final SimpleCommandExceptionType NO_USERS_FOUND = new SimpleCommandExceptionType(
1818
parseMessage(LocaleMessage.ERROR_INVALID_PLAYER));
1919

20-
public static final SimpleCommandExceptionType MUST_SPECIFY_FLY_STATE = new SimpleCommandExceptionType(
21-
parseMessage(LocaleMessage.ERROR_MUST_PROVIDE_STATE)
22-
);
23-
2420

2521
private static Message parseMessage(LocaleMessage message) {
2622
return MessageComponentSerializer.message().serialize(

src/main/java/simplexity/simplefly/commands/Fly.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,21 @@ public class Fly {
2727

2828
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
2929
return Commands.literal("fly")
30-
.requires(Fly::canExecute)
30+
.requires(css -> css.getSender().hasPermission(Constants.FLY_PERMISSION))
3131
.executes(Fly::execute)
32-
.then(
33-
Commands.literal("enable").executes(
34-
ctx -> executeWithArg(ctx, true)
35-
))
32+
.then(Commands.literal("enable").executes(
33+
ctx -> executeWithArg(ctx, true))
34+
.then(Commands.argument("player", ArgumentTypes.players())
35+
.requires(ctx -> ctx.getSender().hasPermission(Constants.FLY_OTHERS_PERMISSION))
36+
.suggests(SuggestionUtils::suggestPlayers)
37+
.executes(ctx-> executeOnOtherWithArg(ctx, true)))
38+
)
3639
.then(Commands.literal("disable").executes(
37-
ctx -> executeWithArg(ctx, false)
38-
))
39-
.then(Commands.literal("admin")
40-
.requires(ctx -> ctx.getSender().hasPermission(Constants.FLY_OTHERS_PERMISSION))
41-
.then(
42-
Commands.argument("player", ArgumentTypes.player())
43-
.suggests(SuggestionUtils::suggestPlayers)
44-
.executes(Fly::executeOnOther)
45-
.then(Commands.literal("enable").executes(ctx -> executeOnOtherWithArg(ctx, true)))
46-
.then(Commands.literal("disable").executes(ctx -> executeOnOtherWithArg(ctx, false)))));
47-
}
48-
49-
private static boolean canExecute(CommandSourceStack css) {
50-
if (!(css.getSender() instanceof Player player)) return false;
51-
return player.hasPermission(Constants.FLY_PERMISSION);
40+
ctx -> executeWithArg(ctx, false))
41+
.then(Commands.argument("player", ArgumentTypes.players())
42+
.requires(ctx -> ctx.getSender().hasPermission(Constants.FLY_OTHERS_PERMISSION))
43+
.suggests(SuggestionUtils::suggestPlayers)
44+
.executes(ctx-> executeOnOtherWithArg(ctx, false))));
5245
}
5346

5447
private static int execute(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
@@ -67,23 +60,6 @@ private static int executeWithArg(CommandContext<CommandSourceStack> ctx, boolea
6760
return Command.SINGLE_SUCCESS;
6861
}
6962

70-
private static int executeOnOther(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
71-
CommandSender sender = ctx.getSource().getSender();
72-
PlayerSelectorArgumentResolver playerArg = ctx.getArgument("player", PlayerSelectorArgumentResolver.class);
73-
List<Player> targets = playerArg.resolve(ctx.getSource());
74-
int size = targets.size();
75-
if (size == 0) {
76-
throw Exceptions.NO_USERS_FOUND.create();
77-
} else if (size > 1) {
78-
throw Exceptions.MUST_SPECIFY_FLY_STATE.create();
79-
}
80-
Player player = targets.getFirst();
81-
boolean flyEnabled = FlyLogic.flyToggle(player);
82-
player.sendMessage(getParsedComponent(flyEnabled, player, sender, LocaleMessage.FLY_SET_BY_OTHER.getMessage()));
83-
sender.sendMessage(getParsedComponent(flyEnabled, player, sender, LocaleMessage.FLY_SET_OTHER.getMessage()));
84-
return Command.SINGLE_SUCCESS;
85-
}
86-
8763
@SuppressWarnings("SameReturnValue")
8864
private static int executeOnOtherWithArg(CommandContext<CommandSourceStack> ctx, boolean shouldEnable) throws CommandSyntaxException {
8965
CommandSender sender = ctx.getSource().getSender();

src/main/java/simplexity/simplefly/commands/FlyReload.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import simplexity.simplefly.config.ConfigHandler;
1010
import simplexity.simplefly.config.LocaleMessage;
1111

12+
@SuppressWarnings("UnstableApiUsage")
1213
public class FlyReload {
1314

1415
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
15-
return Commands.literal("flyreload").requires(ctx -> ctx.getSender().hasPermission(Constants.FLY_RELOAD))
16+
return Commands.literal("flyreload").requires(ctx -> ctx.getSender().hasPermission(Constants.FLY_RELOAD_PERMISSION))
1617
.executes(ctx -> {
17-
ConfigHandler.reloadConfigValues();
18+
ConfigHandler.getInstance().reloadConfigValues();
1819
ctx.getSource().getSender().sendRichMessage(LocaleMessage.FEEDBACK_CONFIG_RELOADED.getMessage());
1920
return Command.SINGLE_SUCCESS;
2021
});

0 commit comments

Comments
 (0)