Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rehdot committed Jun 2, 2024
1 parent 18c18e8 commit cece403
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 101 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
**AtHere** is a small command helper intended for Minecraft server operators.
AtHere 1.0.3
-
AtHere is a small command helper intended for Minecraft server operators.
Essentially, it's a more configurable "@a" argument that works on any command.

**Usage Examples:**
Expand All @@ -7,6 +9,7 @@ Essentially, it's a more configurable "@a" argument that works on any command.
* /msg @here hello! <- Message every player "hello!"
* /gamemode creative @here <- Set everyone's gamemode to creative
* /say Hello @here! <- Say hello to everyone online
* /athere exclude @here <- Exclude every online player from command execution

**Commands:**
-
Expand All @@ -16,12 +19,4 @@ Essentially, it's a more configurable "@a" argument that works on any command.
* /athere stop <- Stops every running AtHere task
* /athere status <- Displays AtHere's current statuses

**Keep In Mind:**
-

AtHere's functionality is limited to online players, so it only works on commands that take in player arguments or say player names in chat.

Don't use the '@here' argument with the built-in /athere command, it will not work.
:

:)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.10

mod_version=1.0.2
mod_version=1.0.3
maven_group=redot.athere
archives_base_name=athere

Expand Down
118 changes: 109 additions & 9 deletions src/main/java/redot/athere/AtHere.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
Expand All @@ -11,14 +12,23 @@
import net.minecraft.command.CommandRegistryAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redot.athere.interfaces.LongExecutor;
import redot.athere.interfaces.PhraseExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;

public class AtHere implements ModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("athere");
private static MinecraftClient client = MinecraftClient.getInstance();
static List<ScheduledExecutorService> schedulers = new ArrayList<>();
public static List<String> exclusions = new ArrayList<>();
public static long delay = 100;
private static SuggestionProvider<FabricClientCommandSource> playerSuggestions = (context, builder) -> {
if (client.player != null) {
for (String entry : CMDProcess.getOnlinePlayers()) {
Expand All @@ -43,15 +53,105 @@ public void onInitialize() {

private void registerAtHere(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess) {
dispatcher.register(literal("athere")
.then(literal("delay").then(argument("milliseconds", LongArgumentType.longArg())
.suggests(delaySuggestions)))
.then(literal("exclude").then(argument("player", StringArgumentType.word())
.suggests(playerSuggestions)))
.then(literal("include").then(argument("player", StringArgumentType.word())
.suggests(playerSuggestions)))
.then(literal("help"))
.then(literal("status"))
.then(literal("stop")));
.then(registerLongIntakeExecutor("delay", num -> {
delay = (int) num;
MSGManager.sendPlayerMSG(MSG.setDelay());
}))
.then(registerPlayerIntakeExecutor("exclude", player -> {
if (!exclusions.contains(player.toLowerCase())) {
exclusions.add(player.toLowerCase());
}
MSGManager.sendPlayerMSG(MSG.addExclusion(player));
}))
.then(registerPlayerIntakeExecutor("include", player -> {
exclusions.remove(player.toLowerCase());
MSGManager.sendPlayerMSG(MSG.addInclusion(player));
}))
.then(registerExecutor("stop", () -> {
schedulers.forEach(ExecutorService::shutdownNow);
schedulers.clear();
MSGManager.sendPlayerMSG(MSG.shutDownTasks);
}))
.then(registerExecutor("clearexclusions", () -> {
exclusions.clear();
MSGManager.sendPlayerMSG(MSG.clearExclusions);
}))
.then(registerExecutor("help", MSGManager::sendHelpMSG))
.then(registerExecutor("status", MSGManager::sendStatusMSG))
);
}

private static LiteralArgumentBuilder<FabricClientCommandSource> registerExecutor(String name, Runnable executor) {
return literal(name).executes(context -> {
executor.run();
return 1;
});
}

private static LiteralArgumentBuilder<FabricClientCommandSource> registerPlayerIntakeExecutor(String name, PhraseExecutor executor) {
return literal(name).then(argument("player", StringArgumentType.greedyString()).executes(context -> {
String player = StringArgumentType.getString(context, "player");
executor.execute(player);
return 1;
}).suggests(playerSuggestions));
}

private static LiteralArgumentBuilder<FabricClientCommandSource> registerLongIntakeExecutor(String name, LongExecutor executor) {
return literal(name).then(argument("milliseconds", LongArgumentType.longArg(0)).executes(context -> {
long num = LongArgumentType.getLong(context, "milliseconds");
executor.execute(num);
return 1;
}).suggests(delaySuggestions));
}

/*switch (args.get(1).toLowerCase()) {
case "delay":
if (args.get(2) == null) {
MSGManager.sendPlayerMSG(MSG.nullArgs);
return;
}
try {
delay = Math.max(0, Integer.parseInt(args.get(2)));
MSGManager.sendPlayerMSG(MSG.setDelay());
} catch (Exception ignored) {
MSGManager.sendPlayerMSG(MSG.invalidArgs);
}
break;
case "stop":
schedulers.forEach(ExecutorService::shutdownNow);
schedulers.clear();
MSGManager.sendPlayerMSG(MSG.shutDownTasks);
break;
case "exclude":
try { playerName = args.get(2); }
catch (ArrayIndexOutOfBoundsException ignored) {
playerName = client.player.getName().getString();
}
if (!exclusions.contains(playerName.toLowerCase())) {
exclusions.add(playerName.toLowerCase());
}
MSGManager.sendPlayerMSG(MSG.addExclusion(playerName));
break;
case "include":
try { playerName = args.get(2); }
catch (ArrayIndexOutOfBoundsException ignored) {
playerName = client.player.getName().getString();
}
exclusions.remove(playerName.toLowerCase());
MSGManager.sendPlayerMSG(MSG.addInclusion(playerName));
break;
case "help":
MSGManager.sendHelpMSG();
break;
case "status":
MSGManager.sendStatusMSG();
break;
default:
MSGManager.sendPlayerMSG(MSG.nullSubCMD);
break;
}*/

}
67 changes: 4 additions & 63 deletions src/main/java/redot/athere/CMDProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.authlib.GameProfile;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.PlayerListEntry;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -11,22 +12,19 @@

public class CMDProcess {
private static MinecraftClient client = MinecraftClient.getInstance();
public static List<String> exclusions = new ArrayList<>();
public static int delay = 100;
static List<ScheduledExecutorService> schedulers = new ArrayList<>();

public static void processAtHere(String cmd) {
if (getOnlinePlayers().isEmpty()) return;

MSGManager.sendPlayerMSG("Starting task.");
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
schedulers.add(scheduler);
AtHere.schedulers.add(scheduler);

int delayMultiplier = 0;
for (int i = 0; i < getOnlinePlayers().size(); i++) {
String playerName = getOnlinePlayers().get(i);

if (exclusions.contains(playerName.toLowerCase())) {
if (AtHere.exclusions.contains(playerName.toLowerCase())) {
continue;
}

Expand All @@ -37,64 +35,7 @@ public static void processAtHere(String cmd) {
client.player.networkHandler.sendChatCommand(cmd.replace("@here", playerName));
}
});
}, (long) delay * delayMultiplier, TimeUnit.MILLISECONDS);
}
}

public static void processCommand(List<String> args) {
String playerName;

if (args.get(1) == null) {
MSGManager.sendPlayerMSG(MSG.nullSubCMD);
}

switch (args.get(1).toLowerCase()) {
case "delay":
if (args.get(2) == null) {
MSGManager.sendPlayerMSG(MSG.nullArgs);
return;
}
try {
delay = Math.max(0, Integer.parseInt(args.get(2)));
MSGManager.sendPlayerMSG(MSG.setDelay());
} catch (Exception ignored) {
MSGManager.sendPlayerMSG(MSG.invalidArgs);
}
break;
case "stop":
schedulers.forEach(ExecutorService::shutdownNow);
MSGManager.sendPlayerMSG(MSG.shutDownTasks);
break;
case "exclude":
try { playerName = args.get(2); }
catch (ArrayIndexOutOfBoundsException ignored) {
playerName = client.player.getName().getString();
}

if (!exclusions.contains(playerName.toLowerCase())) {
exclusions.add(playerName.toLowerCase());
}

MSGManager.sendPlayerMSG(MSG.addExclusion(playerName));
break;
case "include":
try { playerName = args.get(2); }
catch (ArrayIndexOutOfBoundsException ignored) {
playerName = client.player.getName().getString();
}

exclusions.remove(playerName.toLowerCase());
MSGManager.sendPlayerMSG(MSG.addInclusion(playerName));
break;
case "help":
MSGManager.sendHelpMSG();
break;
case "status":
MSGManager.sendStatusMSG();
break;
default:
MSGManager.sendPlayerMSG(MSG.nullSubCMD);
break;
}, AtHere.delay * delayMultiplier, TimeUnit.MILLISECONDS);
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/redot/athere/MSG.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

public record MSG() {
public static String
init = "AtHere Initialized.",
nullSubCMD = "Subcommand not recognized. Try '/athere help'.",
nullArgs = "Please provide an argument.",
invalidArgs = "The provided arguments are invalid. Try '/athere help'.",
shutDownTasks = "Stopped all running AtHere tasks.";
init = "AtHere initialized.",
shutDownTasks = "Stopped all running AtHere tasks.",
clearExclusions = "Cleared AtHere's exclusions.";

public static String setDelay() {
return "Set delay to " + CMDProcess.delay + " milliseconds.";
return "Set delay to "+AtHere.delay+" millisecond"+(AtHere.delay==1?"":"s")+".";
}

public static String addExclusion(String playerName) {
Expand All @@ -23,11 +21,11 @@ public static String addInclusion(String playerName) {
}

public static String delayStatus() {
return "\nCommand delay: " + CMDProcess.delay + " milliseconds.";
return "Statuses:\nCommand delay: "+AtHere.delay+" millisecond"+(AtHere.delay==1?"":"s");
}

public static String exclusionStatus() {
String ei = CMDProcess.exclusions.stream().map(Object::toString).collect(Collectors.joining(", "));
String ei = AtHere.exclusions.stream().map(Object::toString).collect(Collectors.joining(", "));
return "\nExcluded individuals: " + (ei.isEmpty() ? "None." : ei);
}
}
3 changes: 2 additions & 1 deletion src/main/java/redot/athere/MSGManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static void sendHelpMSG() {
"\nWhen typing commands, using '@here' in place of a player will execute that command once for each online player!" +
"\nMy commands include:" +
"\n/athere delay (milliseconds) <- Sets a custom delay between each command" +
"\n/athere exclude <- Toggles on/off whether to include yourself in the command execution" +
"\n/athere exclude (player) <- Excludes a player from command execution" +
"\n/athere include (player) <- Includes a previously-excluded player from command execution" +
"\n/athere status <- Displays AtHere's statuses" +
"\n/athere stop <- Stops all running AtHere tasks");
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/redot/athere/interfaces/LongExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package redot.athere.interfaces;

@FunctionalInterface
public interface LongExecutor {
void execute(long num);
}
6 changes: 6 additions & 0 deletions src/main/java/redot/athere/interfaces/PhraseExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package redot.athere.interfaces;

@FunctionalInterface
public interface PhraseExecutor {
void execute(String phrase);
}
11 changes: 1 addition & 10 deletions src/main/java/redot/athere/mixin/CommandProcessMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,14 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import redot.athere.CMDProcess;

import java.util.Arrays;
import java.util.List;

@Mixin(ClientPlayNetworkHandler.class)
public class CommandProcessMixin {
@Inject(method = "sendChatCommand", at = @At("HEAD"), cancellable = true)
private void onCommand(String content, CallbackInfo ci) {
String cmd = content.toLowerCase();

if (cmd.startsWith("athere")) {
ci.cancel();
List<String> args = Arrays.stream(content.split(" ")).toList();
CMDProcess.processCommand(args);
return;
}

if (!cmd.contains("@here")) return;

ci.cancel();
CMDProcess.processAtHere(cmd);
}
Expand Down

0 comments on commit cece403

Please sign in to comment.