Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/main/java/fr/liveinground/admin_craft/AdminCraft.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package fr.liveinground.admin_craft;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.logging.LogUtils;
import fr.liveinground.admin_craft.commands.tools.AltCommand;
import fr.liveinground.admin_craft.commands.tools.*;
import fr.liveinground.admin_craft.commands.moderation.*;
import fr.liveinground.admin_craft.commands.tools.EchestCommand;
import fr.liveinground.admin_craft.commands.tools.InvseeCommand;
import fr.liveinground.admin_craft.moderation.SanctionConfig;
import fr.liveinground.admin_craft.mutes.MuteEventsHandler;
import fr.liveinground.admin_craft.storage.PlayerDataManager;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
Expand All @@ -35,9 +35,12 @@
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Mod(AdminCraft.MODID)
Expand Down Expand Up @@ -87,6 +90,8 @@ public void onCommandRegister(RegisterCommandsEvent event) {
TempBanCommand.register(dispatcher);
InvseeCommand.register(dispatcher);
EchestCommand.register(dispatcher);
OfflineTeleportCommand.register(dispatcher);
OfflineTagCommand.register(dispatcher);
// StaffModeCommand.register(dispatcher);
}

Expand Down Expand Up @@ -262,4 +267,22 @@ public void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
player.sendSystemMessage(Component.literal("You can disable this message by changing the 'configVersion' key to " + AdminCraft._VERSION + " in the configuration."));
}
}

@Nullable
public static GameProfile getOneProfile(Collection<GameProfile> profiles) {
if (profiles.isEmpty()) return null;
for (GameProfile p: profiles) {
if (p != null) return p;
}
return null;
}

public static boolean isOnline(MinecraftServer server, GameProfile profile) {
return getOnlinePlayer(server, profile) != null;
}

@Nullable
public static ServerPlayer getOnlinePlayer(MinecraftServer server, GameProfile profile) {
return server.getPlayerList().getPlayer(profile.getId());
}
}
14 changes: 12 additions & 2 deletions src/main/java/fr/liveinground/admin_craft/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class Config {
private static final ForgeConfigSpec.IntValue INVSEE_LEVEL;
public static int invsee_level;

private static final ForgeConfigSpec.IntValue OTP_LEVEL;
public static int otp_level;

private static final ForgeConfigSpec.IntValue OTAG_LEVEL;
public static int otag_level;

// ---------------
// -- Sanctions --
// ---------------
Expand Down Expand Up @@ -223,6 +229,8 @@ public class Config {
REPORTS_LEVEL = BUILDER.comment("The OP level required to run the /reports command").worldRestart().defineInRange("reports", 3, 0, 4);
TEMPBAN_LEVEL = BUILDER.comment("The OP level required to run the /tempban command").worldRestart().defineInRange("tempban", 3, 0,4);
INVSEE_LEVEL = BUILDER.comment("The OP level required to run the /invsee and /echest commands").worldRestart().defineInRange("invsee", 2, 0,4);
OTP_LEVEL = BUILDER.comment("The OP level required to run the /otp command").worldRestart().defineInRange("otp", 2, 0, 4);
OTAG_LEVEL = BUILDER.comment("The OP level required to run the /otag command").worldRestart().defineInRange("otp", 2, 0, 4);

BUILDER.pop();
}
Expand Down Expand Up @@ -302,7 +310,7 @@ public class Config {
.define("enter", "You are now in the spawn protection");

SPAWN_PROTECTION_LEAVE = BUILDER.comment("Message when leaving spawn protection")
.define("leave", "You are no more in the spawn protection");
.define("leave", "You are no longer in the spawn protection");

TIME_REMAINING = BUILDER.comment("Message for displaying a sanction duration. Available placeholders: %days%, %hours%, and %minutes%")
.define("timeRemainingMessage", "Time remaining: %days% days, %hours%, and %minutes% minutes");
Expand All @@ -328,7 +336,7 @@ public class Config {
REPORT_FAILED_SELF = BUILDER.comment("The message sent to a player trying to report himself").define("selfReport", "You can't report yourself!");

FREEZE_START = BUILDER.comment("The message sent to the player when he is frozen").define("freezeStartMessage", "You have been frozen by an operator. Please wait for instructions and don't log out.");
FREEZE_STOP = BUILDER.comment("The message sent to the player when he is unfrozen").define("freezeStopMessage", "You are no more frozen. You can continue playing normally.");
FREEZE_STOP = BUILDER.comment("The message sent to the player when he is unfrozen").define("freezeStopMessage", "You are no longer frozen. You can continue playing normally.");

BUILDER.pop();
}
Expand Down Expand Up @@ -424,6 +432,8 @@ static void onLoad(final ModConfigEvent.Loading event) {
reports_level = REPORTS_LEVEL.get();
tempban_level = TEMPBAN_LEVEL.get();
invsee_level = INVSEE_LEVEL.get();
otag_level = OTAG_LEVEL.get();
otp_level = OTP_LEVEL.get();

// ---------------
// -- Sanctions --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fr.liveinground.admin_craft.PlaceHolderSystem;
import fr.liveinground.admin_craft.moderation.CustomSanctionSystem;
import fr.liveinground.admin_craft.moderation.SanctionConfig;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
Expand All @@ -31,7 +32,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {

dispatcher.register(Commands.literal("mute")
.requires(commandSource -> commandSource.hasPermission(Config.mute_level))
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("player", GameProfileArgument.gameProfile())
.executes(ctx -> {
mute(ctx, null, null);
return 1;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {

dispatcher.register(Commands.literal("tempmute")
.requires(commandSource -> commandSource.hasPermission(Config.mute_level))
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("player", GameProfileArgument.gameProfile())
.then(Commands.argument("duration", StringArgumentType.word())
.executes(ctx -> {
Date duration = SanctionConfig.getDurationAsDate(StringArgumentType.getString(ctx, "duration"));
Expand Down Expand Up @@ -108,18 +109,22 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
}

private static void mute(@NotNull CommandContext<CommandSourceStack> ctx, @Nullable String reason, @Nullable Date duration) throws CommandSyntaxException {
ServerPlayer player = EntityArgument.getPlayer(ctx, "player");
GameProfile profile = AdminCraft.getOneProfile(GameProfileArgument.getGameProfiles(ctx, "player"));
if (profile == null) {
ctx.getSource().sendFailure(Component.literal("No player was found").withStyle(ChatFormatting.RED));
return;
}

if (reason == null) {
reason = "Muted by an operator.";
}
if (AdminCraft.mutedPlayersUUID.contains(player.getStringUUID())) {
ctx.getSource().sendFailure(Component.literal(PlaceHolderSystem.replacePlaceholders(Config.mute_failed_already_muted, Map.of("player", player.getName().getString()))));
if (AdminCraft.mutedPlayersUUID.contains(String.valueOf(profile.getId()))) {
ctx.getSource().sendFailure(Component.literal(PlaceHolderSystem.replacePlaceholders(Config.mute_failed_already_muted, Map.of("player", profile.getName()))));
return;
}
CustomSanctionSystem.mutePlayer(player, reason, duration);
CustomSanctionSystem.mutePlayer(ctx.getSource().getServer(), profile, reason, duration);

String msgToOperator = PlaceHolderSystem.replacePlaceholders(Config.mute_success, Map.of("player", player.getName().getString(), "reason", reason));
String msgToOperator = PlaceHolderSystem.replacePlaceholders(Config.mute_success, Map.of("player", profile.getName(), "reason", reason));
ctx.getSource().sendSuccess(() -> Component.literal(msgToOperator), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
reason = template.sanctionMessage();
switch (template.type()) {
case BAN:
CustomSanctionSystem.banPlayer(ctx.getSource().getServer(), ctx.getSource().toString(), sanctionedPlayer, reason, null);
CustomSanctionSystem.banPlayer(ctx.getSource().getServer(), ctx.getSource().toString(), sanctionedPlayer.getGameProfile(), reason, null);
break;
case TEMPBAN:
Date banExpiresOn = SanctionConfig.getDurationAsDate(template.duration());
CustomSanctionSystem.banPlayer(ctx.getSource().getServer(), ctx.getSource().toString(), sanctionedPlayer, reason, banExpiresOn);
CustomSanctionSystem.banPlayer(ctx.getSource().getServer(), ctx.getSource().toString(), sanctionedPlayer.getGameProfile(), reason, banExpiresOn);
break;
case KICK:
CustomSanctionSystem.kickPlayer(sanctionedPlayer, reason);
Expand All @@ -95,15 +95,15 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
ctx.getSource().sendFailure(Component.literal(DUPLICATE_SANCTION));
return 1;
}
CustomSanctionSystem.mutePlayer(sanctionedPlayer, reason, null);
CustomSanctionSystem.mutePlayer(ctx.getSource().getServer(), sanctionedPlayer.getGameProfile(), reason, null);
break;
case TEMPMUTE:
if (AdminCraft.mutedPlayersUUID.contains(sanctionedPlayer.getStringUUID())) {
ctx.getSource().sendFailure(Component.literal(DUPLICATE_SANCTION));
return 1;
}
Date muteExpiresOn = SanctionConfig.getDurationAsDate(template.duration());
CustomSanctionSystem.mutePlayer(sanctionedPlayer, reason, muteExpiresOn);
CustomSanctionSystem.mutePlayer(ctx.getSource().getServer(), sanctionedPlayer.getGameProfile(), reason, muteExpiresOn);
break;
case WARN:
CustomSanctionSystem.warnPlayer(sanctionedPlayer, reason, ctx.getSource().getDisplayName().getString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package fr.liveinground.admin_craft.commands.moderation;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import fr.liveinground.admin_craft.AdminCraft;
import fr.liveinground.admin_craft.Config;
import fr.liveinground.admin_craft.moderation.CustomSanctionSystem;
import fr.liveinground.admin_craft.moderation.SanctionConfig;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.GameProfileArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

Expand All @@ -19,37 +23,49 @@ public class TempBanCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("tempban")
.requires(commandSource -> commandSource.hasPermission(Config.tempban_level))
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("player", GameProfileArgument.gameProfile())
.then(Commands.argument("duration", StringArgumentType.word())
.executes(ctx -> {
ServerPlayer sanctionedPlayer = EntityArgument.getPlayer(ctx, "player");
GameProfile profile = AdminCraft.getOneProfile(GameProfileArgument.getGameProfiles(ctx, "player"));
if (profile == null) {
ctx.getSource().sendFailure(Component.literal("No player was found").withStyle(ChatFormatting.RED));
return 1;
}
String reason = "Banned by an operator";
Date duration = SanctionConfig.getDurationAsDate(StringArgumentType.getString(ctx, "duration"));

if (duration == null) {
ctx.getSource().sendFailure(Component.literal("Invalid duration, expecting format 1d1h1m1s"));
return 1;
}
tempban(ctx, sanctionedPlayer, duration, reason);
tempban(ctx, profile, duration, reason);
return 1;

})
.then(Commands.argument("reason", StringArgumentType.greedyString())
.executes(ctx -> {
ServerPlayer sanctionedPlayer = EntityArgument.getPlayer(ctx, "player");
GameProfile profile = AdminCraft.getOneProfile(GameProfileArgument.getGameProfiles(ctx, "player"));
if (profile == null) {
ctx.getSource().sendFailure(Component.literal("No player was found").withStyle(ChatFormatting.RED));
return 1;
}

String reason = StringArgumentType.getString(ctx, "reason");
Date duration = SanctionConfig.getDurationAsDate(StringArgumentType.getString(ctx, "duration"));

if (duration == null) {
ctx.getSource().sendFailure(Component.literal("Invalid duration, expecting format 1d1h1m1s"));
return 1;
}
tempban(ctx, sanctionedPlayer, duration, reason);
tempban(ctx, profile, duration, reason);
return 1;
})))
)
);
}

private static void tempban(CommandContext<CommandSourceStack> ctx, ServerPlayer player, Date duration, String reason) {
private static void tempban(CommandContext<CommandSourceStack> ctx, GameProfile player, Date duration, String reason) {
CustomSanctionSystem.banPlayer(ctx.getSource().getServer(), ctx.getSource().getTextName(), player, reason, duration);
ctx.getSource().sendSuccess(() -> Component.literal("Banned " + player.getDisplayName().getString() + " " + duration + ": " + reason), true);
ctx.getSource().sendSuccess(() -> Component.literal("Banned " + player.getName() + " " + duration + ": " + reason), true);
}
}
Loading