Skip to content

Commit

Permalink
chore: refactor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JiveOff committed Sep 13, 2024
1 parent e4ffd45 commit b3db519
Show file tree
Hide file tree
Showing 41 changed files with 649 additions and 2,164 deletions.
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'fr.efreicraft'
version = '1.2.1'
version = '1.3'

compileJava.options.encoding "UTF-8"

Expand All @@ -19,6 +19,9 @@ repositories {
name = 'sonatype'
url = 'https://oss.sonatype.org/content/groups/public/'
}
maven {
url = 'https://repo.panda-lang.org/releases'
}
maven { url "https://repo.dmulloy2.net/repository/public/" }
if (System.getenv("NEXUS_REPOSITORY")) {
println "Using Nexus repository"
Expand All @@ -37,9 +40,14 @@ dependencies {
compileOnly 'io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT'

compileOnly "fr.efreicraft:AnimusClient:latest.integration"

implementation "net.kyori:adventure-text-minimessage:4.15.0"
implementation "dev.rollczi:litecommands-bukkit:3.4.1"
}

jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE

from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
Expand All @@ -61,6 +69,7 @@ tasks.withType(JavaCompile).configureEach {
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
options.release = targetJavaVersion
}
options.compilerArgs.add("-parameters")
}

processResources {
Expand Down
202 changes: 67 additions & 135 deletions src/main/java/fr/efreicraft/ecatup/ECATUP.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
package fr.efreicraft.ecatup;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
import dev.rollczi.litecommands.schematic.SchematicFormat;
import fr.efreicraft.animus.invoker.ApiException;
import fr.efreicraft.ecatup.commands.*;
import fr.efreicraft.ecatup.commands.gamemode.*;
import fr.efreicraft.ecatup.commands.permissions.GroupsCom;
import fr.efreicraft.ecatup.commands.permissions.PlayersCom;
import fr.efreicraft.ecatup.commands.speeds.FlySpeed;
import fr.efreicraft.ecatup.commands.speeds.ResetSpeed;
import fr.efreicraft.ecatup.commands.speeds.WalkSpeed;
import fr.efreicraft.ecatup.commands.arguments.GameModeArgument;
import fr.efreicraft.ecatup.commands.exceptions.CommandException;
import fr.efreicraft.ecatup.commands.handlers.InvalidUsage;
import fr.efreicraft.ecatup.commands.handlers.PermissionHandler;
import fr.efreicraft.ecatup.commands.validator.NotTheSamePlayer;
import fr.efreicraft.ecatup.commands.validator.NotTheSamePlayerValidator;
import fr.efreicraft.ecatup.commands.validator.Speed;
import fr.efreicraft.ecatup.commands.validator.SpeedValidator;
import fr.efreicraft.ecatup.groups.GroupManager;
import fr.efreicraft.ecatup.listeners.ChatListener;
import fr.efreicraft.ecatup.listeners.JoinListener;
import fr.efreicraft.ecatup.listeners.LeaveListener;
import fr.efreicraft.ecatup.players.PlayerManager;
import fr.efreicraft.ecatup.players.menus.MenuListener;
import fr.efreicraft.ecatup.utils.DiscordWebhook;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import fr.efreicraft.ecatup.utils.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nullable;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@SuppressWarnings("UnstableApiUsage")
public final class ECATUP extends JavaPlugin {
Expand All @@ -46,6 +41,8 @@ public final class ECATUP extends JavaPlugin {

private GroupManager groupManager;

private LiteCommands<CommandSender> liteCommands;

@Override
public void onEnable() {

Expand All @@ -57,52 +54,63 @@ public void onEnable() {
config.options().copyDefaults(true); // au cas où le fichier existe, mais est incomplet.
INSTANCE.saveConfig();

// Register BungeeCord channel
getServer().getMessenger().registerOutgoingPluginChannel(INSTANCE, "BungeeCord");
getServer().getMessenger().registerIncomingPluginChannel(INSTANCE, "BungeeCord", new ChatListener());

// Register events
Bukkit.getPluginManager().registerEvents(new ChatListener(), INSTANCE);
Bukkit.getPluginManager().registerEvents(new JoinListener(), INSTANCE);
Bukkit.getPluginManager().registerEvents(new LeaveListener(), INSTANCE);

registerCommand("chat", new fr.efreicraft.ecatup.commands.Chat());
for (PreferenceCache.ChatChannel channel : PreferenceCache.ChatChannel.values()) {
Bukkit.getPluginManager().addPermission(new Permission("ecatup.channel." + channel.toString().toLowerCase()));
}

registerCommand("flyspeed", new FlySpeed());
registerCommand("gm", new Gm());
registerCommand("gms", new Gms());
registerCommand("gma", new Gma());
registerCommand("gmc", new Gmc());
registerCommand("gmsp", new Gmsp());
registerCommand("resetspeed", new ResetSpeed());
registerCommand("skull", new Skull());
registerCommand("slap", new Slap());
registerCommand("sudo", new Sudo());
registerCommand("walkspeed", new WalkSpeed());
registerCommand("whois", new WhoIs());

registerCommand("groupperms", new GroupsCom());
registerCommand("playerperms", new PlayersCom());

registerCommand("hasperm", new HasPerm());

// Send log to Discord
DiscordWebhook webhook = new DiscordWebhook(config.getString("webhook"));
webhook.addEmbed(new DiscordWebhook.EmbedObject()
.setTitle("Serveur")
.setDescription("Le serveur a démarré !")
.setColor(Color.white)
.setFooter("Efrei Craft", "https://efreicraft.fr/img/favicon.png")
);
try {
webhook.execute();
} catch (IOException ignored) {}
this.liteCommands = LiteBukkitFactory.builder()
.settings(settings -> settings
.fallbackPrefix("ECATUP")
.nativePermissions(false)
)

.argument(GameMode.class, new GameModeArgument())

.annotations(configuration -> configuration
.validator(Player.class, NotTheSamePlayer.class, new NotTheSamePlayerValidator())
.validator(Float.class, Speed.class, new SpeedValidator())
)

.commands(
new GameModeCommand(),
new SpeedCommand(),
new FlyCommand(),
new HasPermCommand(),
new SkullCommand(),
new SlapCommand(),
new RideCommand(),
new SudoCommand()
)

.exception(ApiException.class, (invocation, exception, chain) -> {
if (invocation.sender() instanceof Player) {
MessageUtils.sendMessage(
invocation.sender(),
MessageUtils.ChatPrefix.PLUGIN,
"&cErreur API: &7%s".formatted(exception.getMessage())
);
}
})

.exception(CommandException.class, (invocation, exception, chain) -> {
if (invocation.sender() instanceof Player) {
MessageUtils.sendMessage(
invocation.sender(),
MessageUtils.ChatPrefix.COMMAND,
"&cErreur: &7%s".formatted(exception.getMessage())
);
}
})

.missingPermission(new PermissionHandler())
.invalidUsage(new InvalidUsage())

.schematicGenerator(SchematicFormat.angleBrackets())

.build();

playerManager = new PlayerManager();

groupManager = new GroupManager();

Bukkit.getPluginManager().registerEvents(new MenuListener(), INSTANCE);
Expand All @@ -111,82 +119,6 @@ public void onEnable() {
@Override
public void onDisable() {
// Plugin shutdown logic

// Unregister BungeeCord channel
getServer().getMessenger().unregisterOutgoingPluginChannel(INSTANCE);
getServer().getMessenger().unregisterIncomingPluginChannel(INSTANCE);

if (!config.getString("server_name", "").equalsIgnoreCase("lobby"))
for (Player player : Bukkit.getOnlinePlayers()) {
sendPlayerToServer(player, "lobby");
}

try {
// Send log to Discord
DiscordWebhook webhook = new DiscordWebhook(config.getString("webhook"));
webhook.addEmbed(new DiscordWebhook.EmbedObject()
.setTitle("Serveur")
.setDescription("Le serveur s'est arrêté !")
.setColor(java.awt.Color.decode("#ffffff"))
.setFooter("Efrei Craft", "https://efreicraft.fr/img/favicon.png")
);
// webhook.execute();
} catch (Throwable ignored) {}
}

void registerCommand(String command, CommandExecutor executor) {
Objects.requireNonNull(Bukkit.getPluginCommand(command)).setExecutor(executor);
}

public static List<String> getPlayersForTabList(String[] args, int argPos) {
List<Player> players = Bukkit.getOnlinePlayers().stream().filter(player -> player.getName().toLowerCase().startsWith(args[argPos].toLowerCase())).collect(Collectors.toList());
List<String> results = new ArrayList<>();
players.forEach(player -> results.add(player.getName()));
return results.isEmpty() ? null : results;
}

public static void sendPlayerToServer(Player player, String server) {
if (!player.hasPermission("server." + server.toLowerCase())) {
Component nope = Component.text("Vous ne pouvez pas aller sur ce serveur !").color(NamedTextColor.RED);
player.sendMessage(nope);
INSTANCE.getLogger().info(player.getName() + " tried to join " + server + " but doesn't have permission to do so.");
return;
}

ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(server);
player.sendPluginMessage(INSTANCE, "BungeeCord", out.toByteArray());
}

public static void sendGlobalChat(String msg, @Nullable Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();

out.writeUTF("Forward");
out.writeUTF("ONLINE");
out.writeUTF("ecatup:globalchat");

ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
DataOutputStream msgout = new DataOutputStream(msgbytes);

try {
msgout.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();

INSTANCE.getLogger().severe("Couldn't send " +
(player == null ? "a player's" : (player.getName() + "'s")) +
" global message: " + msg);
if (player != null) {
Component failed = Component.text("Votre dernier message n'a pas été envoyé aux autres serveurs.").color(NamedTextColor.DARK_RED);
player.sendMessage(failed);
}
}

out.writeShort(msgbytes.toByteArray().length);
out.write(msgbytes.toByteArray());

Bukkit.getServer().sendPluginMessage(INSTANCE, "BungeeCord", out.toByteArray());
}

public static ECATUP getInstance() {
Expand Down
95 changes: 0 additions & 95 deletions src/main/java/fr/efreicraft/ecatup/commands/Chat.java

This file was deleted.

Loading

0 comments on commit b3db519

Please sign in to comment.