Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/fr/korastrip/paperplugin/SharedStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/fr/korastrip/paperplugin/SynchronizationConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public LiteralCommandNode<CommandSourceStack> 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();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <health|food|potions>");
}

@Override
public LiteralArgumentBuilder<CommandSourceStack> 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("<yellow>" + stat + " synchronization is already disabled.</yellow>");
return Command.SINGLE_SUCCESS;
}

if (!config.setEnabled(stat, false)) {
source.getSender().sendRichMessage("<red>Could not save the configuration.</red>");
return 0;
}

source.getSender().sendRichMessage("<green>Disabled " + stat + " synchronization.</green>");
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -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 <health|food|potions>");
}

@Override
public LiteralArgumentBuilder<CommandSourceStack> 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("<yellow>" + stat + " synchronization is already enabled.</yellow>");
return Command.SINGLE_SUCCESS;
}

if (!config.setEnabled(stat, true)) {
source.getSender().sendRichMessage("<red>Could not save the configuration.</red>");
return 0;
}

source.getSender().sendRichMessage("<green>Enabled " + stat + " synchronization.</green>");
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public LiteralArgumentBuilder<CommandSourceStack> 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("<gold><usage></gold><white>: <description></white>", Placeholder.unparsed("description", command.getDescription()), Placeholder.unparsed("usage", command.getUsage())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
synchronization:
health: true
food: true
potions: true