Skip to content

Commit

Permalink
Commandes : /skylladmin setsize <player/uuid> <number> confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya committed Jan 29, 2024
1 parent 64baa8e commit 4c92c02
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 21 deletions.
6 changes: 0 additions & 6 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ public abstract class Island {

public abstract int getMaxMembers();

public abstract void setMaxMembers(int maxMembers);
public abstract boolean setMaxMembers(int maxMembers);
}
3 changes: 2 additions & 1 deletion plugin/src/main/java/fr/euphyllia/skyllia/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import fr.euphyllia.skyllia.commands.admin.SkylliaAdminCommand;
import fr.euphyllia.skyllia.commands.common.SkylliaCommand;
import fr.euphyllia.skyllia.configuration.ConfigToml;
import fr.euphyllia.skyllia.listeners.bukkitevents.*;
import fr.euphyllia.skyllia.listeners.bukkitevents.BlockEvent;
import fr.euphyllia.skyllia.listeners.bukkitevents.PortailAlternativeFoliaEvent;
import fr.euphyllia.skyllia.listeners.bukkitevents.entity.DamageEvent;
import fr.euphyllia.skyllia.listeners.bukkitevents.player.InventoryEvent;
import fr.euphyllia.skyllia.listeners.bukkitevents.player.JoinEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,88 @@
package fr.euphyllia.skyllia.commands.admin.subcommands;

import fr.euphyllia.skyllia.Main;
import fr.euphyllia.skyllia.api.skyblock.Island;
import fr.euphyllia.skyllia.commands.SubCommandInterface;
import fr.euphyllia.skyllia.configuration.LanguageToml;
import fr.euphyllia.skyllia.managers.skyblock.SkyblockManager;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class SetIslandTypeSubCommands implements SubCommandInterface {

private final Logger logger = LogManager.getLogger(SetIslandTypeSubCommands.class);

@Override
public boolean onCommand(@NotNull Main plugin, @NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return false;
if (!(sender instanceof Player player)) {
return true;
}
if (!player.hasPermission("skyllia.admins.commands.island.setsize")) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerPermissionDenied);
return true;
}

if (args.length < 3) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeCommandNotEnoughArgs);
return true;
}
String playerName = args[0];
String changeValue = args[1];
String confirm = args[2];
if (confirm.equalsIgnoreCase("confirm")) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeNotConfirmedArgs);
return true;
}
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
try {
executor.execute(() -> {
try {
UUID playerId;
try {
playerId = UUID.fromString(playerName);
} catch (IllegalArgumentException ignored) {
playerId = Bukkit.getPlayerUniqueId(playerName);
}
SkyblockManager skyblockManager = plugin.getInterneAPI().getSkyblockManager();
Island island = skyblockManager.getIslandByOwner(playerId).join();
if (island == null) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerHasNotIsland);
return;
}

int members = Integer.parseInt(changeValue);
boolean updated = island.setMaxMembers(members);
if (updated) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeSuccess);
} else {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeFailed);
}

} catch (Exception e) {
if (e instanceof NumberFormatException ignored) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeNAN);
} else {
logger.log(Level.FATAL, e.getMessage(), e);
LanguageToml.sendMessage(plugin, player, LanguageToml.messageError);
}
}
});
} finally {
executor.shutdown();
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class LanguageToml {
public static String messageUnbanPlayerFailed = "Le joueur n'a pas été banni pour une raison inconnue.";
public static String messageADeleteCommandNotEnoughArgs = "Il manque le nom du joueur.";
public static String messageADeleteNotConfirmedArgs = "Vous devez ajouter \"confirm\" à la fin de votre commandes";
public static String messageASetSizeCommandNotEnoughArgs = "La commande est incomplete : /skylladmin setsize <player/uuid> <number> confirm";
public static String messageASetSizeNotConfirmedArgs = "Vous devez ajouter l'argument \"confirm\" à la fin de votre commande.";
public static String messageASetSizeNAN = "Vous n'avez pas choisi un nombre entier.";
public static String messageASetSizeFailed = "Le changement n'a pas pu s'effectuer.";
public static String messageASetSizeSuccess = "Le nombre de personne sur l'ile a bien été changé.";
private static boolean verbose;

public static void init(File configFile) {
Expand Down Expand Up @@ -173,6 +178,12 @@ private static void adminLanguage() {
// forcedelete
messageADeleteCommandNotEnoughArgs = getString("admin.commands.island.delete.not-enough-args", messageADeleteCommandNotEnoughArgs);
messageADeleteNotConfirmedArgs = getString("admin.commands.island.delete.no-confirm", messageADeleteNotConfirmedArgs);
// setMember
messageASetSizeCommandNotEnoughArgs = getString("admin.commands.island.setsize.not-enough-args", messageASetSizeCommandNotEnoughArgs);
messageASetSizeSuccess = getString("admin.commands.island.setsize.success", messageASetSizeSuccess);
messageASetSizeFailed = getString("admin.commands.island.setsize.failed", messageASetSizeFailed);
messageASetSizeNAN = getString("admin.commands.island.setsize.nan", messageASetSizeNAN);
messageASetSizeNotConfirmedArgs = getString("admin.commands.island.setsize.no-confirm", messageASetSizeNotConfirmedArgs);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public CompletableFuture<Boolean> insertIslands(Island futurIsland) {
completableFuture.complete(null);
}
} catch (Exception e) {
logger.log(Level.FATAL, e);
logger.log(Level.FATAL, e.getMessage(), e);
completableFuture.complete(null);
}
}, null);
Expand All @@ -148,4 +148,22 @@ public CompletableFuture<Boolean> insertIslands(Island futurIsland) {
return null;
}
}

public CompletableFuture<Integer> getMaxMemberInIsland(Island island) {
CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
MariaDBExecute.executeQuery(this.api.getDatabaseLoader(), SELECT_ISLAND_BY_ISLAND_ID.formatted(this.databaseName), List.of(island.getId()), resultSet -> {
try {
if (resultSet.next()) {
int maxMembers = resultSet.getInt("max_members");
completableFuture.complete(maxMembers);
} else {
completableFuture.complete(-1);
}
} catch (SQLException e) {
logger.log(Level.FATAL, e.getMessage(), e);
completableFuture.complete(-1);
}
}, null);
return completableFuture;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class IslandUpdateQuery {
WHERE `island_id` = ?;
""";

private static final String UPDATE_SIZE_ISLAND = """
UPDATE `%s`.islands
SET `max_members` = ?
WHERE `island_id` = ?;
""";

private static final String SELECT_PRIVATE_ISLAND = """
SELECT `private`
FROM `%s`.islands
Expand Down Expand Up @@ -95,4 +101,16 @@ public CompletableFuture<Boolean> isPrivateIsland(Island island) {
return completableFuture;
}

public CompletableFuture<Boolean> setMaxMemberInIsland(Island island, int newValue) {
CompletableFuture<Boolean> completableFuture = new CompletableFuture<>();
try {
MariaDBExecute.executeQueryDML(this.api.getDatabaseLoader(), UPDATE_SIZE_ISLAND.formatted(this.databaseName), List.of(
island.getId(), newValue
), i -> completableFuture.complete(i != 0), null);
} catch (Exception e) {
completableFuture.complete(false);
}
return completableFuture;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import fr.euphyllia.skyllia.api.InterneAPI;
import fr.euphyllia.skyllia.api.skyblock.model.permissions.PermissionsIsland;
import fr.euphyllia.skyllia.listeners.ListenersUtils;
import fr.euphyllia.skyllia.listeners.bukkitevents.player.JoinEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,16 @@ public Position getPosition() {

@Override
public int getMaxMembers() {
return this.maxMemberInIsland;
int value = this.plugin.getInterneAPI().getSkyblockManager().getMaxMemberInIsland(this).join();
if (value == -1) {
return this.maxMemberInIsland;
} else {
return value;
}
}

@Override
public void setMaxMembers(int newMax) {

public boolean setMaxMembers(int newMax) {
return this.plugin.getInterneAPI().getSkyblockManager().setMaxMemberInIsland(this, newMax).join();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ public CompletableFuture<Boolean> deleteMember(Island island, Players oldMember)
public CompletableFuture<@Nullable Players> getOwnerByIslandID(Island island) {
return this.plugin.getInterneAPI().getIslandQuery().getIslandMemberQuery().getOwnerInIslandId(island);
}

public CompletableFuture<Integer> getMaxMemberInIsland(Island island) {
return this.plugin.getInterneAPI().getIslandQuery().getIslandDataQuery().getMaxMemberInIsland(island);
}

public CompletableFuture<Boolean> setMaxMemberInIsland(Island island, int newValue) {
return this.plugin.getInterneAPI().getIslandQuery().getIslandUpdateQuery().setMaxMemberInIsland(island, newValue);
}
}

0 comments on commit 4c92c02

Please sign in to comment.