Skip to content

Commit

Permalink
Added support to execute a command when a player votes
Browse files Browse the repository at this point in the history
  • Loading branch information
muriplz committed Jan 17, 2024
1 parent 67360fc commit 8d7a796
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/kryeit/votifier/config/ConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/kryeit/votifier/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/votifier.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"host": "0.0.0.0",
"port": "8192",
"debug": false
"debug": false,
"command-after-voting": "give %player% diamond 1",
}

0 comments on commit 8d7a796

Please sign in to comment.