diff --git a/src/main/java/fr/korastrip/paperplugin/SharedStats.java b/src/main/java/fr/korastrip/paperplugin/SharedStats.java index f1c6bd5..e4d7b6f 100644 --- a/src/main/java/fr/korastrip/paperplugin/SharedStats.java +++ b/src/main/java/fr/korastrip/paperplugin/SharedStats.java @@ -13,21 +13,27 @@ public final class SharedStats extends JavaPlugin implements Listener { private final SharedGroup group = new SharedGroup(); GroupCommand groupCommand = new GroupCommand(this); + private SynchronizationConfig synchronizationConfig; public SharedGroup getGroup() { return group; } + public SynchronizationConfig getSynchronizationConfig() { + return synchronizationConfig; + } + + + @Override public void onEnable() { - + synchronizationConfig = new SynchronizationConfig(this); this.getServer().getPluginManager().registerEvents(this, this); this.getServer().getPluginManager().registerEvents(new DamageListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); this.getServer().getPluginManager().registerEvents(new RegenerationListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerRespawnListener(this), this); - this.getLifecycleManager().registerEventHandler( LifecycleEvents.COMMANDS, event -> event.registrar().register(groupCommand.create()) diff --git a/src/main/java/fr/korastrip/paperplugin/SynchronizationConfig.java b/src/main/java/fr/korastrip/paperplugin/SynchronizationConfig.java new file mode 100644 index 0000000..0f98c05 --- /dev/null +++ b/src/main/java/fr/korastrip/paperplugin/SynchronizationConfig.java @@ -0,0 +1,77 @@ +package fr.korastrip.paperplugin; + +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + +public class SynchronizationConfig { + + private final SharedStats plugin; + private final File file; + private YamlConfiguration config; + + public SynchronizationConfig(SharedStats plugin) { + this.plugin = plugin; + this.file = new File(plugin.getDataFolder(), "configuration.yml"); + + if (!file.exists()) { + plugin.saveResource("configuration.yml", false); + } + + reload(); + } + + public void reload() { + config = YamlConfiguration.loadConfiguration(file); + + config.addDefault("synchronization.health", true); + config.addDefault("synchronization.food", true); + config.addDefault("synchronization.potions", true); + config.options().copyDefaults(true); + + try { + config.save(file); + } catch (IOException exception) { + plugin.getLogger().severe( + "Failed to save default configuration: " + exception.getMessage() + ); + } + } + + public boolean isEnabled(String stat) { + String key = getKey(stat); + return config.getBoolean(key); + } + + public boolean setEnabled(String stat, boolean enabled) { + String key = getKey(stat); + boolean previousValue = config.getBoolean(key); + + config.set(key, enabled); + + try { + config.save(file); + return true; + } catch (IOException exception) { + config.set(key, previousValue); + plugin.getLogger().severe( + "Failed to save configuration.yml: " + exception.getMessage() + ); + return false; + } + } + + private String getKey(String stat) { + String normalizedStat = stat.toLowerCase(Locale.ROOT); + + if (!normalizedStat.equals("health") + && !normalizedStat.equals("food") + && !normalizedStat.equals("potions")) { + throw new IllegalArgumentException("Unknown synchronization stat: " + stat); + } + + return "synchronization." + normalizedStat; + } +} diff --git a/src/main/java/fr/korastrip/paperplugin/commands/group/GroupCommand.java b/src/main/java/fr/korastrip/paperplugin/commands/group/GroupCommand.java index 4eec59f..ec3081d 100644 --- a/src/main/java/fr/korastrip/paperplugin/commands/group/GroupCommand.java +++ b/src/main/java/fr/korastrip/paperplugin/commands/group/GroupCommand.java @@ -25,6 +25,8 @@ public LiteralCommandNode create() { .then(new Remove(plugin).build()) .then(new Remove(plugin).build()) .then(new Players(plugin).build()) + .then(new Enable(plugin).build()) + .then(new Disable(plugin).build()) .build(); } diff --git a/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Disable.java b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Disable.java new file mode 100644 index 0000000..f3149d1 --- /dev/null +++ b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Disable.java @@ -0,0 +1,45 @@ + +package fr.korastrip.paperplugin.commands.group.sub; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import fr.korastrip.paperplugin.SharedStats; +import fr.korastrip.paperplugin.commands.group.GroupSubCommand; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; + +public class Disable extends GroupSubCommand { + + public Disable(SharedStats plugin) { + super(plugin, "disable", "Disable synchronization for a stat", "disable "); + } + + @Override + public LiteralArgumentBuilder build() { + return Commands.literal("disable") + .requires(source -> source.getSender().isOp()) + .then(Commands.literal("health") + .executes(context -> disable("health", context.getSource()))) + .then(Commands.literal("food") + .executes(context -> disable("food", context.getSource()))) + .then(Commands.literal("potions") + .executes(context -> disable("potions", context.getSource()))); + } + + private int disable(String stat, CommandSourceStack source) { + var config = plugin.getSynchronizationConfig(); + + if (!config.isEnabled(stat)) { + source.getSender().sendRichMessage("" + stat + " synchronization is already disabled."); + return Command.SINGLE_SUCCESS; + } + + if (!config.setEnabled(stat, false)) { + source.getSender().sendRichMessage("Could not save the configuration."); + return 0; + } + + source.getSender().sendRichMessage("Disabled " + stat + " synchronization."); + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Enable.java b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Enable.java new file mode 100644 index 0000000..11ec7d8 --- /dev/null +++ b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Enable.java @@ -0,0 +1,47 @@ + +package fr.korastrip.paperplugin.commands.group.sub; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import fr.korastrip.paperplugin.SharedStats; +import fr.korastrip.paperplugin.commands.group.GroupSubCommand; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; + +import java.util.Locale; +import java.util.Set; + +public class Enable extends GroupSubCommand { + public Enable(SharedStats plugin) { + super(plugin, "enable", "Enable synchronization for a stat", "enable "); + } + + @Override + public LiteralArgumentBuilder build() { + return Commands.literal("enable") + .requires(source -> source.getSender().isOp()) + .then(Commands.literal("health") + .executes(context -> enable("health", context.getSource()))) + .then(Commands.literal("food") + .executes(context -> enable("food", context.getSource()))) + .then(Commands.literal("potions") + .executes(context -> enable("potions", context.getSource()))); + } + + private int enable(String stat, CommandSourceStack source) { + var config = plugin.getSynchronizationConfig(); + + if (config.isEnabled(stat)) { + source.getSender().sendRichMessage("" + stat + " synchronization is already enabled."); + return Command.SINGLE_SUCCESS; + } + + if (!config.setEnabled(stat, true)) { + source.getSender().sendRichMessage("Could not save the configuration."); + return 0; + } + + source.getSender().sendRichMessage("Enabled " + stat + " synchronization."); + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Help.java b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Help.java index 36a95d2..2e96031 100644 --- a/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Help.java +++ b/src/main/java/fr/korastrip/paperplugin/commands/group/sub/Help.java @@ -39,7 +39,9 @@ public LiteralArgumentBuilder build() { new Start(plugin), new Stop(plugin), new Players(plugin), - new Version(plugin) + new Version(plugin), + new Enable(plugin), + new Disable(plugin) ); commands.forEach(command -> sender.sendRichMessage(": ", Placeholder.unparsed("description", command.getDescription()), Placeholder.unparsed("usage", command.getUsage()))); diff --git a/src/main/java/fr/korastrip/paperplugin/listener/DamageListener.java b/src/main/java/fr/korastrip/paperplugin/listener/DamageListener.java index f40f1e1..50fb84a 100644 --- a/src/main/java/fr/korastrip/paperplugin/listener/DamageListener.java +++ b/src/main/java/fr/korastrip/paperplugin/listener/DamageListener.java @@ -27,7 +27,7 @@ public void onDamageReceive(EntityDamageEvent event) { if (event.getEntity() instanceof Player player) { SharedGroup group = this.plugin.getGroup(); - + if (!plugin.getSynchronizationConfig().isEnabled("health")) return; if (!group.getIsPlaying()) return; if (!group.getPlayersId().contains(player.getUniqueId())) return; diff --git a/src/main/java/fr/korastrip/paperplugin/listener/PlayerRespawnListener.java b/src/main/java/fr/korastrip/paperplugin/listener/PlayerRespawnListener.java index f508857..1595c64 100644 --- a/src/main/java/fr/korastrip/paperplugin/listener/PlayerRespawnListener.java +++ b/src/main/java/fr/korastrip/paperplugin/listener/PlayerRespawnListener.java @@ -28,8 +28,10 @@ public void onPlayerRespawn(PlayerRespawnEvent event) { plugin, () -> { if (group.getOnlinePlayers().size() == group.getRespawnedPlayers().size()) { - group.setHealth(20); - group.syncHealth(); + if (plugin.getSynchronizationConfig().isEnabled("health")) { + group.setHealth(20); + group.syncHealth(); + } group.clearRespawnedPlayers(); group.isGroupDying = false; } diff --git a/src/main/java/fr/korastrip/paperplugin/listener/RegenerationListener.java b/src/main/java/fr/korastrip/paperplugin/listener/RegenerationListener.java index 28b44c5..add443d 100644 --- a/src/main/java/fr/korastrip/paperplugin/listener/RegenerationListener.java +++ b/src/main/java/fr/korastrip/paperplugin/listener/RegenerationListener.java @@ -19,6 +19,8 @@ public RegenerationListener(SharedStats plugin) { public void onRegeneration(EntityRegainHealthEvent event) { SharedGroup group = this.plugin.getGroup(); + if (!plugin.getSynchronizationConfig().isEnabled("health")) return; + if (event.getEntity() instanceof Player player) { if(group.getPlayersId().contains(player.getUniqueId()) && group.getIsPlaying()) { diff --git a/src/main/resources/configuration.yml b/src/main/resources/configuration.yml new file mode 100644 index 0000000..1a243ce --- /dev/null +++ b/src/main/resources/configuration.yml @@ -0,0 +1,4 @@ +synchronization: + health: true + food: true + potions: true \ No newline at end of file