From 8d7a7965d0e238d9afd6f4e19e3b0d132bf771c9 Mon Sep 17 00:00:00 2001 From: muriplz <46825658+muriplz@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:29:04 +0100 Subject: [PATCH] Added support to execute a command when a player votes --- .../com/kryeit/votifier/config/ConfigReader.java | 2 ++ .../model/listeners/BasicVoteListener.java | 8 ++++++++ .../java/com/kryeit/votifier/utils/Utils.java | 15 +++++++++++++++ src/main/resources/votifier.json | 3 ++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/kryeit/votifier/utils/Utils.java diff --git a/src/main/java/com/kryeit/votifier/config/ConfigReader.java b/src/main/java/com/kryeit/votifier/config/ConfigReader.java index 34a2695..c6acb5e 100644 --- a/src/main/java/com/kryeit/votifier/config/ConfigReader.java +++ b/src/main/java/com/kryeit/votifier/config/ConfigReader.java @@ -14,6 +14,7 @@ public class ConfigReader { public static String HOST; public static int PORT; public static boolean DEBUG; + public static String COMMAND; private ConfigReader() { @@ -25,6 +26,7 @@ public static void readFile(Path path) throws IOException { HOST = configObject.getString("host"); PORT = Integer.parseInt(configObject.getString("port")); DEBUG = configObject.getBoolean("debug"); + COMMAND = configObject.getString("command"); } public static String readOrCopyFile(Path path, String exampleFile) throws IOException { diff --git a/src/main/java/com/kryeit/votifier/model/listeners/BasicVoteListener.java b/src/main/java/com/kryeit/votifier/model/listeners/BasicVoteListener.java index 485211e..f2593be 100644 --- a/src/main/java/com/kryeit/votifier/model/listeners/BasicVoteListener.java +++ b/src/main/java/com/kryeit/votifier/model/listeners/BasicVoteListener.java @@ -21,9 +21,12 @@ import com.kryeit.votifier.model.Vote; import com.kryeit.votifier.model.VoteListener; import com.kryeit.votifier.model.VotifierEvent; +import com.kryeit.votifier.utils.Utils; import java.util.logging.Logger; +import static com.kryeit.votifier.config.ConfigReader.COMMAND; + /** * A basic vote listener for demonstration purposes. * @@ -37,5 +40,10 @@ public class BasicVoteListener implements VotifierEvent { @Override public void onVoteReceived(Vote vote) { log.info("Received: " + vote); + + String command = COMMAND.replace("%player%", vote.getUsername()); + + if (!command.equals("")) + Utils.executeCommandAsServer(command); } } diff --git a/src/main/java/com/kryeit/votifier/utils/Utils.java b/src/main/java/com/kryeit/votifier/utils/Utils.java new file mode 100644 index 0000000..994f460 --- /dev/null +++ b/src/main/java/com/kryeit/votifier/utils/Utils.java @@ -0,0 +1,15 @@ +package com.kryeit.votifier.utils; + +import com.kryeit.votifier.MinecraftServerSupplier; +import net.minecraft.server.command.ServerCommandSource; + +public class Utils { + + public static void executeCommandAsServer(String command) { + // Create a command source that represents the server + ServerCommandSource source = MinecraftServerSupplier.getServer().getCommandSource(); + + // Execute the command + MinecraftServerSupplier.getServer().getCommandManager().executeWithPrefix(source, command); + } +} diff --git a/src/main/resources/votifier.json b/src/main/resources/votifier.json index 72d33f0..86a13f7 100644 --- a/src/main/resources/votifier.json +++ b/src/main/resources/votifier.json @@ -1,5 +1,6 @@ { "host": "0.0.0.0", "port": "8192", - "debug": false + "debug": false, + "command-after-voting": "give %player% diamond 1", } \ No newline at end of file