Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
| `/sharedgroup stop` | Stop an active game without deleting the group | Group owner |
| `/sharedgroup players` | List the online players in your group | Group member |
| `/sharedgroup leaderboard` | Show the group's death leaderboard during a game | Group member |
| `/sharedgroup enable <health\|food\|potions>` | Enable synchronization for a stat, outside a game | OP |
| `/sharedgroup disable <health\|food\|potions>` | Disable synchronization for a stat, outside a game | OP |
| `/sharedgroup version` | Display the plugin version | None |

## What remains to be done
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/fr/korastrip/paperplugin/SharedStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ 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);
Expand All @@ -34,13 +38,4 @@ public void onEnable() {
event -> event.registrar().register(groupCommand.create())
);
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendPlainMessage("Hello, " + event.getPlayer().getName());
this.getLogger().info("Health: " + event.getPlayer().getHealth());
this.getLogger().info("Food Level: " + event.getPlayer().getFoodLevel());
this.getLogger().info("Saturation: " + event.getPlayer().getSaturation());
}

}
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 @@ -26,6 +26,8 @@ public LiteralCommandNode<CommandSourceStack> create() {
.then(new Revoke(plugin).build())
.then(new Players(plugin).build())
.then(new Stats(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
Expand Up @@ -45,8 +45,8 @@ public int generateError(String message, CommandSender player, TagResolver... re
return 0;
}

public int generateError(ErrorMessage error, CommandSender player) {
return this.generateError(error.getMessage(), player);
public int generateError(ErrorMessage error, CommandSender player, TagResolver... resolvers) {
return this.generateError(error.getMessage(), player, resolvers);
}

public String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

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 fr.korastrip.paperplugin.enums.ErrorMessage;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.entity.Player;

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();
var group = plugin.getGroup();

if (source.getSender() instanceof Player player) {

if (group.getIsPlaying()) {
return this.generateError(ErrorMessage.GAME_RUNNING, player);
}

if (!config.isEnabled(stat)) {
return this.generateError(ErrorMessage.SYNC_ALREADY_DISABLED, player, Placeholder.unparsed("stat", stat));
}

if (!config.setEnabled(stat, false)) {
return this.generateError(ErrorMessage.CONFIG_SAVE_FAILED, player);
}

player.sendRichMessage("<green>Disabled " + stat + " synchronization.</green>");
return Command.SINGLE_SUCCESS;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

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 fr.korastrip.paperplugin.enums.ErrorMessage;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.entity.Player;

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();
var group = plugin.getGroup();

if (source.getSender() instanceof Player player) {

if (group.getIsPlaying()) {
return this.generateError(ErrorMessage.GAME_RUNNING, player);
}

if (config.isEnabled(stat)) {
return this.generateError(ErrorMessage.SYNC_ALREADY_ENABLED, player, Placeholder.unparsed("stat", stat));
}

if (!config.setEnabled(stat, true)) {
return this.generateError(ErrorMessage.CONFIG_SAVE_FAILED, player);
}

player.sendRichMessage("<green>Enabled " + stat + " synchronization.</green>");
return Command.SINGLE_SUCCESS;
}
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public LiteralArgumentBuilder<CommandSourceStack> build() {
new Stop(plugin),
new Players(plugin),
new Version(plugin),
new Stats(plugin)
new Stats(plugin),
new Enable(plugin),
new Disable(plugin)
);

commands.forEach(command -> ctx.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 @@ -13,7 +13,10 @@ public enum ErrorMessage {
PLAYER_ALREADY_IN_GROUP("This player is already in the group."),
PLAYER_NOT_IN_GROUP("This player is not in the group."),
CANNOT_REMOVE_YOURSELF("You cannot remove yourself. Use <gold>/" + Config.PREFIX + " revoke</gold> instead."),
NOT_ENOUGH_PLAYERS("You cannot start a game alone. Invite at least one player.");
NOT_ENOUGH_PLAYERS("You cannot start a game alone. Invite at least one player."),
SYNC_ALREADY_ENABLED("<stat> synchronization is already enabled."),
SYNC_ALREADY_DISABLED("<stat> synchronization is already disabled."),
CONFIG_SAVE_FAILED("Could not save the configuration.");

private final String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 @@ -20,6 +20,8 @@ public EntityResurrectListener(SharedStats plugin) {

@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityResurrect(EntityResurrectEvent e) {
if(!this.plugin.getSynchronizationConfig().isEnabled("health")) return;

if (e.getEntity() instanceof Player player) {

TotemService totemService = new TotemService(this.plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onPlayerDeath(PlayerDeathEvent event) {
if (!group.getIsPlaying()) return;
if (!group.getPlayersId().contains(player.getUniqueId())) return;

if(!group.isGroupDying) {
if(!group.isGroupDying && this.plugin.getSynchronizationConfig().isEnabled("health")) {
SharedHealthService sharedHealthService = new SharedHealthService(this.plugin);
sharedHealthService.groupDeath(player);
}
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 @@ -20,6 +20,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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void groupDeath(Player deathPlayer) {

public void shareLoss() {
SharedGroup group = this.plugin.getGroup();

if(!plugin.getSynchronizationConfig().isEnabled("health")) return;
if (!group.getIsPlaying() || group.isGroupDying) return;

double totalLost = 0;
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
Loading