stable
This commit is contained in:
parent
215031ffff
commit
1ebf86f575
|
|
@ -28,7 +28,7 @@ public final class Config {
|
||||||
|
|
||||||
static final ModConfigSpec SPEC = BUILDER.build();
|
static final ModConfigSpec SPEC = BUILDER.build();
|
||||||
|
|
||||||
|
|
||||||
public static String apiBaseUrl;
|
public static String apiBaseUrl;
|
||||||
public static String apiKey;
|
public static String apiKey;
|
||||||
public static int requestTimeout;
|
public static int requestTimeout;
|
||||||
|
|
@ -46,4 +46,13 @@ public final class Config {
|
||||||
enableLogging = ENABLE_LOGGING.get();
|
enableLogging = ENABLE_LOGGING.get();
|
||||||
enableWhitelist = ENABLE_WHITELIST.get();
|
enableWhitelist = ENABLE_WHITELIST.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void save() {
|
||||||
|
ENABLE_WHITELIST.set(enableWhitelist);
|
||||||
|
ENABLE_LOGGING.set(enableLogging);
|
||||||
|
API_BASE_URL.set(apiBaseUrl);
|
||||||
|
API_KEY.set(apiKey);
|
||||||
|
REQUEST_TIMEOUT.set(requestTimeout);
|
||||||
|
SPEC.save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,10 @@ import net.minecraft.commands.CommandSourceStack;
|
||||||
import net.minecraft.commands.Commands;
|
import net.minecraft.commands.Commands;
|
||||||
import net.minecraft.commands.SharedSuggestionProvider;
|
import net.minecraft.commands.SharedSuggestionProvider;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
@ -55,25 +52,36 @@ public class WhitelistCommands {
|
||||||
private static int enableWhitelist(CommandContext<CommandSourceStack> ctx) {
|
private static int enableWhitelist(CommandContext<CommandSourceStack> ctx) {
|
||||||
boolean changed = setEnableWhitelistPersisted(ctx, true);
|
boolean changed = setEnableWhitelistPersisted(ctx, true);
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
ctx.getSource().sendFailure(Component.literal("Whitelist already enabled"));
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendFailure(Component.literal("Whitelist already enabled"))
|
||||||
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal("Whitelist enabled"), true);
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendSuccess(() -> Component.literal("Whitelist enabled"), true)
|
||||||
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int disableWhitelist(CommandContext<CommandSourceStack> ctx) {
|
private static int disableWhitelist(CommandContext<CommandSourceStack> ctx) {
|
||||||
boolean changed = setEnableWhitelistPersisted(ctx, false);
|
boolean changed = setEnableWhitelistPersisted(ctx, false);
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
ctx.getSource().sendFailure(Component.literal("Whitelist already disabled"));
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendFailure(Component.literal("Whitelist already disabled"))
|
||||||
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal("Whitelist disabled"), true);
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendSuccess(() -> Component.literal("Whitelist disabled"), true)
|
||||||
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int whitelistStatus(CommandContext<CommandSourceStack> ctx) {
|
private static int whitelistStatus(CommandContext<CommandSourceStack> ctx) {
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal("Whitelist is " + (Config.enableWhitelist ? "ON" : "OFF")), false);
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
Component.literal("Whitelist is " + (Config.enableWhitelist ? "ON" : "OFF")), false)
|
||||||
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,7 +96,10 @@ public class WhitelistCommands {
|
||||||
String addedBy = getSourceName(ctx);
|
String addedBy = getSourceName(ctx);
|
||||||
String addedAt = Instant.now().toString();
|
String addedAt = Instant.now().toString();
|
||||||
|
|
||||||
sendOnServer(ctx, Component.literal("Adding player to whitelist: " + player), false);
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
Component.literal("Adding player to whitelist: " + player), false)
|
||||||
|
);
|
||||||
|
|
||||||
CompletableFuture<WhitelistApiClient.WhitelistEntry> fut =
|
CompletableFuture<WhitelistApiClient.WhitelistEntry> fut =
|
||||||
API.addPlayer(player, addedBy, Instant.parse(addedAt), null, true, reason);
|
API.addPlayer(player, addedBy, Instant.parse(addedAt), null, true, reason);
|
||||||
|
|
@ -96,9 +107,11 @@ public class WhitelistCommands {
|
||||||
fut.thenAccept(entry ->
|
fut.thenAccept(entry ->
|
||||||
dispatchBack(ctx, () -> {
|
dispatchBack(ctx, () -> {
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal("Player " + player + " added"), false);
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
Component.literal("Player " + player + " added"), false);
|
||||||
} else {
|
} else {
|
||||||
ctx.getSource().sendFailure(Component.literal("Failed to add " + player));
|
ctx.getSource().sendFailure(
|
||||||
|
Component.literal("Failed to add " + player));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
@ -107,17 +120,27 @@ public class WhitelistCommands {
|
||||||
|
|
||||||
private static int removePlayer(CommandContext<CommandSourceStack> ctx) {
|
private static int removePlayer(CommandContext<CommandSourceStack> ctx) {
|
||||||
String player = StringArgumentType.getString(ctx, "player");
|
String player = StringArgumentType.getString(ctx, "player");
|
||||||
sendOnServer(ctx, Component.literal("Removing player from whitelist: " + player), false);
|
|
||||||
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
Component.literal("Removing player from whitelist: " + player), false)
|
||||||
|
);
|
||||||
|
|
||||||
API.removePlayer(player).thenAccept(success ->
|
API.removePlayer(player).thenAccept(success ->
|
||||||
dispatchBack(ctx, () -> {
|
dispatchBack(ctx, () -> {
|
||||||
if (success) {
|
if (success) {
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal("Player " + player + " removed"), false);
|
ctx.getSource().sendSuccess(() ->
|
||||||
ServerPlayer sp = ctx.getSource().getServer().getPlayerList().getPlayerByName(player);
|
Component.literal("Player " + player + " removed"), false);
|
||||||
|
|
||||||
|
ServerPlayer sp = ctx.getSource().getServer()
|
||||||
|
.getPlayerList().getPlayerByName(player);
|
||||||
if (sp != null) {
|
if (sp != null) {
|
||||||
sp.connection.disconnect(Component.literal("Removed from whitelist"));
|
sp.connection.disconnect(
|
||||||
|
Component.literal("Removed from whitelist"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.getSource().sendFailure(Component.literal("Failed to remove " + player));
|
ctx.getSource().sendFailure(
|
||||||
|
Component.literal("Failed to remove " + player));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
@ -126,27 +149,23 @@ public class WhitelistCommands {
|
||||||
|
|
||||||
private static int checkPlayer(CommandContext<CommandSourceStack> ctx) {
|
private static int checkPlayer(CommandContext<CommandSourceStack> ctx) {
|
||||||
String player = StringArgumentType.getString(ctx, "player");
|
String player = StringArgumentType.getString(ctx, "player");
|
||||||
|
|
||||||
API.checkPlayer(player).thenAccept(resp ->
|
API.checkPlayer(player).thenAccept(resp ->
|
||||||
dispatchBack(ctx, () -> {
|
dispatchBack(ctx, () -> {
|
||||||
if (resp != null && resp.isSuccess()) {
|
if (resp != null && resp.isSuccess()) {
|
||||||
String msg = resp.isWhitelisted() ? "is in whitelist" : "is NOT in whitelist";
|
String msg = resp.isWhitelisted() ?
|
||||||
ctx.getSource().sendSuccess(() -> Component.literal(player + " " + msg), false);
|
"is in whitelist" : "is NOT in whitelist";
|
||||||
|
ctx.getSource().sendSuccess(() ->
|
||||||
|
Component.literal(player + " " + msg), false);
|
||||||
} else {
|
} else {
|
||||||
ctx.getSource().sendFailure(Component.literal("Check failed for " + player));
|
ctx.getSource().sendFailure(
|
||||||
|
Component.literal("Check failed for " + player));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendOnServer(CommandContext<CommandSourceStack> ctx, Component msg, boolean broadcast) {
|
|
||||||
if (broadcast) {
|
|
||||||
ctx.getSource().getServer().getPlayerList().broadcastSystemMessage(msg, false);
|
|
||||||
} else {
|
|
||||||
ctx.getSource().sendSuccess(() -> msg, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void dispatchBack(CommandContext<CommandSourceStack> ctx, Runnable task) {
|
private static void dispatchBack(CommandContext<CommandSourceStack> ctx, Runnable task) {
|
||||||
ctx.getSource().getServer().execute(task);
|
ctx.getSource().getServer().execute(task);
|
||||||
}
|
}
|
||||||
|
|
@ -161,18 +180,21 @@ public class WhitelistCommands {
|
||||||
|
|
||||||
private static boolean setEnableWhitelistPersisted(CommandContext<CommandSourceStack> ctx, boolean enabled) {
|
private static boolean setEnableWhitelistPersisted(CommandContext<CommandSourceStack> ctx, boolean enabled) {
|
||||||
boolean changed = Config.enableWhitelist != enabled;
|
boolean changed = Config.enableWhitelist != enabled;
|
||||||
Config.enableWhitelist = enabled;
|
if (changed) {
|
||||||
WhitelistApiClient.refreshConfigFromSpec();
|
Config.enableWhitelist = enabled;
|
||||||
ctx.getSource().getServer().submit(() -> {}).join();
|
WhitelistApiClient.refreshConfigFromSpec();
|
||||||
|
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
Config.save();
|
||||||
|
} catch (Exception e) {
|
||||||
|
dispatchBack(ctx, () ->
|
||||||
|
ctx.getSource().sendFailure(
|
||||||
|
Component.literal("Failed to save config: " + e.getMessage()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String resolveUuid(MinecraftServer srv, String player) {
|
|
||||||
try {
|
|
||||||
ServerPlayer sp = srv.getPlayerList().getPlayerByName(player);
|
|
||||||
if (sp != null) return sp.getUUID().toString();
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
UUID offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + player).getBytes(StandardCharsets.UTF_8));
|
|
||||||
return offline.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue