Skip to content

Commit

Permalink
Refactor command execution to use User and SierraUser objects
Browse files Browse the repository at this point in the history
Commands are now executed using User and SierraUser objects which simplifies the retrieval of player-related data. Unnecessary file: ISierraSender was deleted and code related to it adjusted. Additional player statistics and control methods were added to SierraUser.
  • Loading branch information
SquareCodeFX committed May 18, 2024
1 parent 4e20025 commit 5f3626a
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 370 deletions.
4 changes: 4 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ java.targetCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url = 'https://repo.codemc.io/repository/maven-snapshots/' } // PacketEvents
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT'
compileOnly ('com.github.retrooper.packetevents:spigot:2.3.1-SNAPSHOT') {
changing = true
}

compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'
Expand Down
16 changes: 10 additions & 6 deletions api/src/main/java/de/feelix/sierraapi/commands/ISierraCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.feelix.sierraapi.commands;

import com.github.retrooper.packetevents.protocol.player.User;
import de.feelix.sierraapi.user.impl.SierraUser;

import java.util.List;

/**
Expand All @@ -9,14 +12,15 @@
public interface ISierraCommand {

/**
* This method is used to process a Sierra command. It executes the specified command when it is called.
* This method processes the given inputs.
*
* @param sierraSender an implementation of ISierraSender, representing the sender of the command
* @param abstractCommand an implementation of IBukkitAbstractCommand, representing the command being executed
* @param sierraLabel an implementation of ISierraLabel, representing the alias of the command used
* @param sierraArguments an implementation of ISierraArguments, representing the arguments passed with the command
* @param user the user initiating the process
* @param sierraUser the Sierra user associated with the process
* @param abstractCommand the abstract command object used for processing
* @param sierraLabel the Sierra label used for the process
* @param sierraArguments the arguments provided for the process
*/
void process(ISierraSender sierraSender, IBukkitAbstractCommand abstractCommand,
void process(User user, SierraUser sierraUser, IBukkitAbstractCommand abstractCommand,
ISierraLabel sierraLabel, ISierraArguments sierraArguments);

/**
Expand Down
24 changes: 0 additions & 24 deletions api/src/main/java/de/feelix/sierraapi/commands/ISierraSender.java

This file was deleted.

29 changes: 29 additions & 0 deletions api/src/main/java/de/feelix/sierraapi/user/impl/SierraUser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.feelix.sierraapi.user.impl;

import com.github.retrooper.packetevents.protocol.player.GameMode;
import de.feelix.sierraapi.check.CheckRepository;
import de.feelix.sierraapi.timing.TimingHandler;
import de.feelix.sierraapi.user.settings.AlertSettings;
Expand All @@ -18,13 +19,34 @@ public interface SierraUser {
*/
String username();

/**
* Retrieves the brand of the user.
*
* @return The brand of the user as a String.
*/
String brand();

/**
* Gets the entity ID of the SierraUser.
*
* @return The entity ID of the SierraUser.
*/
int entityId();

/**
* The ping method sends a ping request and returns the round trip time in milliseconds.
*
* @return The round trip time in milliseconds as an integer.
*/
int ping();

/**
* Returns the number of ticks that the instance has existed for.
*
* @return The number of ticks that the instance has existed for.
*/
int ticksExisted();

/**
* Generates a universally unique identifier (UUID) for the SierraUser.
*
Expand Down Expand Up @@ -62,6 +84,13 @@ public interface SierraUser {
*/
boolean isExempt();

/**
* Retrieves the game mode of the player.
*
* @return The game mode of the player as an instance of GameMode.
*/
GameMode gameMode();

/**
* Sets whether the player is exempt from certain actions or checks.
*
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/de/feelix/sierra/command/CommandHelper.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
package de.feelix.sierra.command;

import com.github.retrooper.packetevents.protocol.player.User;
import de.feelix.sierra.Sierra;
import de.feelix.sierraapi.commands.ISierraSender;
import org.bukkit.command.CommandSender;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

/**
* The CommandHelper class provides helper methods for processing and sending commands related to the Sierra plugin.
*/
public class CommandHelper {

/**
* Sends the version output of the Sierra plugin to the specified {@link ISierraSender}.
* Includes information about the plugin version and a link to the Discord server.
* Sends the version output to the command sender.
*
* @param sierraSender the ISierraSender object representing the sender of the command.
* @param user the user containing the command sender
*/
public static void sendVersionOutput(ISierraSender sierraSender) {
final String sierraPrefix = Sierra.PREFIX;
Sierra sierraPlugin = Sierra.getPlugin();
CommandSender commandSender = sierraSender.getSender();
public static void sendVersionOutput(User user) {
final String sierraPrefix = Sierra.PREFIX;
Sierra sierraPlugin = Sierra.getPlugin();

String pluginVersionMessage = createVersionMessage(sierraPlugin, commandSender);
String pluginVersionMessage = createVersionMessage(sierraPlugin, user);

commandSender.sendMessage(sierraPrefix + " §fRunning §cSierra " + pluginVersionMessage);
commandSender.sendMessage(sierraPrefix + " §fMore info at §cdiscord.gg/squarecode");
user.sendMessage(sierraPrefix + " §fRunning §cSierra " + pluginVersionMessage);
user.sendMessage(sierraPrefix + " §fMore info at §cdiscord.gg/squarecode");
}

/**
* Creates a version message for the Sierra plugin based on the given plugin instance and command sender.
*
* @param sierraPlugin the instance of the Sierra plugin
* @param commandSender the command sender
* @param sierraPlugin the instance of the Sierra plugin
* @param commandSender the command sender
* @return the version message
*/
private static String createVersionMessage(Sierra sierraPlugin, CommandSender commandSender) {
private static String createVersionMessage(Sierra sierraPlugin, User user) {
String pluginVersion = "§7(" + sierraPlugin.getDescription().getVersion() + ")";
boolean isVersionHidden = sierraPlugin.getSierraConfigEngine().config().getBoolean(
"hideVersion",
"hide-version",
true
);
boolean isPermissionGranted = commandSender.hasPermission("sierra.command");

Player player = Bukkit.getPlayer(user.getUUID());

boolean isPermissionGranted = player != null && player.hasPermission("sierra.command");

if (isVersionHidden && !isPermissionGranted) {
pluginVersion = "§7(version hidden)";
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/de/feelix/sierra/command/SierraCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package de.feelix.sierra.command;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.User;
import de.feelix.sierra.Sierra;
import de.feelix.sierra.command.api.BukkitAbstractCommand;
import de.feelix.sierra.command.api.SierraArguments;
import de.feelix.sierra.command.api.SierraLabel;
import de.feelix.sierra.command.api.SierraSender;
import de.feelix.sierra.command.impl.*;
import de.feelix.sierra.manager.storage.PlayerData;
import de.feelix.sierraapi.commands.ISierraCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -92,10 +94,14 @@ public SierraCommand() {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
String[] args) {

SierraSender sierraSender = new SierraSender(sender);
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;
User user = PacketEvents.getAPI().getPlayerManager().getUser(player);
PlayerData playerData = Sierra.getPlugin().getSierraDataManager().getPlayerData(user).get();

if (hasNoPermission(sender)) {
CommandHelper.sendVersionOutput(sierraSender);
CommandHelper.sendVersionOutput(user);
return true;
}

Expand All @@ -106,11 +112,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
if (iSierraCommand != null) {

if (iSierraCommand.permission() != null && !sender.hasPermission(iSierraCommand.permission())) {
CommandHelper.sendVersionOutput(sierraSender);
CommandHelper.sendVersionOutput(user);
return;
}

iSierraCommand.process(sierraSender, new BukkitAbstractCommand(command),
iSierraCommand.process(user, playerData, new BukkitAbstractCommand(command),
new SierraLabel(label), new SierraArguments(args)
);
} else {
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/de/feelix/sierra/command/api/SierraSender.java

This file was deleted.

58 changes: 18 additions & 40 deletions src/main/java/de/feelix/sierra/command/impl/AlertsCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.feelix.sierra.command.impl;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.User;
import de.feelix.sierra.Sierra;
import de.feelix.sierra.manager.storage.PlayerData;
import de.feelix.sierraapi.commands.*;
import org.bukkit.entity.Player;
import de.feelix.sierraapi.user.impl.SierraUser;

import java.util.Collections;
import java.util.List;
Expand All @@ -17,55 +15,35 @@
public class AlertsCommand implements ISierraCommand {

/**
* The process method performs the necessary actions to toggle the alert messages for a player.
* It first retrieves the sender of the command as a Player using the provided sierraSender object.
* If the sender is not a Player, the method returns.
* It then retrieves the PlayerData object associated with the player using the getPlayerData method.
* If the playerData is null, the method returns.
* Finally, it calls the toggleAlertMessages method to toggle the alert messages for the player.
* This method processes the given parameters to perform a specific action.
*
* @param sierraSender the ISierraSender object representing the sender of the command
* @param abstractCommand the IBukkitAbstractCommand object representing the executed command
* @param sierraLabel the ISierraLabel object representing the label of the initial symbol
* @param sierraArguments the ISierraArguments object representing the arguments passed with the command
* @param user The User object representing the user.
* @param sierraUser The SierraUser object representing the user.
* @param abstractCommand The IBukkitAbstractCommand object representing the command.
* @param sierraLabel The ISierraLabel object representing the label of the initial symbol.
* @param sierraArguments The ISierraArguments object representing the arguments passed with the command.
*/
@Override
public void process(ISierraSender sierraSender, IBukkitAbstractCommand abstractCommand,
public void process(User user, SierraUser sierraUser, IBukkitAbstractCommand abstractCommand,
ISierraLabel sierraLabel, ISierraArguments sierraArguments) {

Player playerSender = sierraSender.getSenderAsPlayer();
if (playerSender == null) return;

PlayerData playerData = getPlayerData(playerSender);
if (playerData == null) return;

toggleAlertMessages(playerData);
toggleAlertMessages(user, sierraUser);
}

/**
* Retrieves the PlayerData object associated with a player.
* Toggles the alert messages for a given user.
*
* @param playerSender the Player object representing the player
* @return the PlayerData object associated with the player
* @param user The User object representing the user.
* @param sierraUser The SierraUser object representing the user.
*/
private PlayerData getPlayerData(Player playerSender) {
return Sierra.getPlugin().getSierraDataManager().getPlayerData(
PacketEvents.getAPI().getPlayerManager().getUser(playerSender)
).get();
}
private void toggleAlertMessages(User user, SierraUser sierraUser) {

/**
* Toggles the alert messages for a player.
*
* @param playerData the PlayerData object representing the player
*/
private void toggleAlertMessages(PlayerData playerData) {
if (playerData.getAlertSettings().enabled()) {
playerData.getAlertSettings().toggle(false);
sendMessage(playerData.getUser(), true);
if (sierraUser.alertSettings().enabled()) {
sendMessage(user, true);
sierraUser.alertSettings().toggle(false);
} else {
playerData.getAlertSettings().toggle(true);
sendMessage(playerData.getUser(), false);
sendMessage(user, false);
sierraUser.alertSettings().toggle(true);
}
}

Expand Down
Loading

0 comments on commit 5f3626a

Please sign in to comment.