Replace redundant code with shorter version

This commit is contained in:
Alessandro Proto 2024-11-09 12:58:32 +01:00
parent c2047ccd2c
commit 1aa225f700
18 changed files with 31 additions and 110 deletions

View file

@ -18,11 +18,7 @@ public class FlyCommand {
var rootCommand = literal("fly") var rootCommand = literal("fly")
.requires(Permissions.require("rcc.command.fly", 3)) .requires(Permissions.require("rcc.command.fly", 3))
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
context.getSource().sendFeedback(() -> toggleFlight(player), true); context.getSource().sendFeedback(() -> toggleFlight(player), true);
return 1; return 1;

View file

@ -18,11 +18,7 @@ public class GodCommand {
var rootCommand = literal("god") var rootCommand = literal("god")
.requires(Permissions.require("rcc.command.god", 3)) .requires(Permissions.require("rcc.command.god", 3))
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
context.getSource().sendFeedback(() -> toggleGod(player), true); context.getSource().sendFeedback(() -> toggleGod(player), true);
return 1; return 1;

View file

@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import me.lucko.fabric.api.permissions.v0.Permissions; import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@ -34,12 +35,8 @@ public class DeleteHomeCommand {
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }
private static int execute(CommandContext<ServerCommandSource> context, String name) { private static int execute(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var playerState = RccServer.state.getPlayerState(player.getUuid()); var playerState = RccServer.state.getPlayerState(player.getUuid());
var homes = playerState.homes; var homes = playerState.homes;
var playerContext = PlaceholderContext.of(context.getSource().getPlayer()); var playerContext = PlaceholderContext.of(context.getSource().getPlayer());

View file

@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import me.lucko.fabric.api.permissions.v0.Permissions; import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@ -34,12 +35,8 @@ public class HomeCommand {
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }
private static int execute(CommandContext<ServerCommandSource> context, String name) { private static int execute(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var playerState = RccServer.state.getPlayerState(player.getUuid()); var playerState = RccServer.state.getPlayerState(player.getUuid());
var homes = playerState.homes; var homes = playerState.homes;
var playerContext = PlaceholderContext.of(context.getSource().getPlayer()); var playerContext = PlaceholderContext.of(context.getSource().getPlayer());

View file

@ -7,6 +7,7 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import me.lucko.fabric.api.permissions.v0.Permissions; import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -36,12 +37,8 @@ public class SetHomeCommand {
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }
private static int execute(CommandContext<ServerCommandSource> context, String name, boolean forced) { private static int execute(CommandContext<ServerCommandSource> context, String name, boolean forced) throws CommandSyntaxException {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var playerState = RccServer.state.getPlayerState(player.getUuid()); var playerState = RccServer.state.getPlayerState(player.getUuid());
var homes = playerState.homes; var homes = playerState.homes;
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);

View file

@ -13,13 +13,7 @@ public class AfkCommand {
var rootCommand = literal("afk") var rootCommand = literal("afk")
.requires(Permissions.require("rcc.command.afk", true)) .requires(Permissions.require("rcc.command.afk", true))
.executes(context -> { .executes(context -> {
var player = context.getSource().getPlayerOrThrow();
if (!context.getSource().isExecutedByPlayer()) {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
AfkTracker.getInstance().setPlayerAfk(player, true); AfkTracker.getInstance().setPlayerAfk(player, true);
return 1; return 1;

View file

@ -22,21 +22,9 @@ public class NearCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
var rootCommand = literal("near") var rootCommand = literal("near")
.requires(Permissions.require("rcc.command.near", 2)) .requires(Permissions.require("rcc.command.near", 2))
.executes(context -> { .executes(context -> execute(context, RccServer.CONFIG.nearCommand.nearCommandDefaultRange, context.getSource().getPlayerOrThrow()))
if (!context.getSource().isExecutedByPlayer()) {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
return execute(context, RccServer.CONFIG.nearCommand.nearCommandDefaultRange, context.getSource().getPlayer());
})
.then(argument("radius", IntegerArgumentType.integer(0, RccServer.CONFIG.nearCommand.nearCommandMaxRange)) .then(argument("radius", IntegerArgumentType.integer(0, RccServer.CONFIG.nearCommand.nearCommandMaxRange))
.executes(context -> { .executes(context -> execute(context, IntegerArgumentType.getInteger(context, "radius"), context.getSource().getPlayerOrThrow())));
if (!context.getSource().isExecutedByPlayer()) {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
return execute(context, IntegerArgumentType.getInteger(context, "radius"), context.getSource().getPlayer());
}));
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }

View file

@ -15,12 +15,7 @@ public class SetSpawnCommand {
var rootCommand = literal("setspawn") var rootCommand = literal("setspawn")
.requires(Permissions.require("rcc.command.setspawn", 3)) .requires(Permissions.require("rcc.command.setspawn", 3))
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var spawnPosition = new ServerPosition(player); var spawnPosition = new ServerPosition(player);
var serverState = RccServer.state.getServerState(); var serverState = RccServer.state.getServerState();

View file

@ -16,12 +16,7 @@ public class SpawnCommand {
var rootCommand = literal("spawn") var rootCommand = literal("spawn")
.requires(Permissions.require("rcc.command.spawn", true)) .requires(Permissions.require("rcc.command.spawn", true))
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var serverState = RccServer.state.getServerState(); var serverState = RccServer.state.getServerState();
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);
var spawnPosition = serverState.spawn; var spawnPosition = serverState.spawn;

View file

@ -16,12 +16,7 @@ public class BackCommand {
var rootCommand = literal("back") var rootCommand = literal("back")
.requires(Permissions.require("rcc.command.back", true)) .requires(Permissions.require("rcc.command.back", true))
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);
var lastPosition = BackTracker.lastPlayerPositions.get(player.getUuid()); var lastPosition = BackTracker.lastPlayerPositions.get(player.getUuid());

View file

@ -20,12 +20,7 @@ public class TeleportAcceptCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
var node = dispatcher.register(literal("tpaccept") var node = dispatcher.register(literal("tpaccept")
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var playerUuid = player.getUuid(); var playerUuid = player.getUuid();
var playerRequests = TeleportTracker.teleportRequests.get(playerUuid); var playerRequests = TeleportTracker.teleportRequests.get(playerUuid);
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);

View file

@ -6,6 +6,7 @@ import cc.reconnected.server.util.Components;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -34,15 +35,11 @@ public class TeleportAskCommand {
dispatcher.register(literal("tpask").redirect(node)); dispatcher.register(literal("tpask").redirect(node));
} }
private static void execute(CommandContext<ServerCommandSource> context) { private static void execute(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource(); var source = context.getSource();
if (!source.isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
source.sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return;
}
var server = source.getServer(); var server = source.getServer();
var player = source.getPlayer();
var targetName = StringArgumentType.getString(context, "player"); var targetName = StringArgumentType.getString(context, "player");
var playerManager = server.getPlayerManager(); var playerManager = server.getPlayerManager();
var target = playerManager.getPlayer(targetName); var target = playerManager.getPlayer(targetName);

View file

@ -6,6 +6,7 @@ import cc.reconnected.server.util.Components;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
@ -34,15 +35,10 @@ public class TeleportAskHereCommand {
dispatcher.register(literal("tpaskhere").redirect(node)); dispatcher.register(literal("tpaskhere").redirect(node));
} }
private static void execute(CommandContext<ServerCommandSource> context) { private static void execute(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource(); var source = context.getSource();
if (!source.isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
source.sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return;
}
var server = source.getServer(); var server = source.getServer();
var player = source.getPlayer();
var targetName = StringArgumentType.getString(context, "player"); var targetName = StringArgumentType.getString(context, "player");
var playerManager = server.getPlayerManager(); var playerManager = server.getPlayerManager();
var target = playerManager.getPlayer(targetName); var target = playerManager.getPlayer(targetName);

View file

@ -46,12 +46,7 @@ public class TeleportDenyCommand {
}) })
.then(argument("uuid", UuidArgumentType.uuid()) .then(argument("uuid", UuidArgumentType.uuid())
.executes(context -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var uuid = UuidArgumentType.getUuid(context, "uuid"); var uuid = UuidArgumentType.getUuid(context, "uuid");
var playerUuid = player.getUuid(); var playerUuid = player.getUuid();
var playerRequests = TeleportTracker.teleportRequests.get(playerUuid); var playerRequests = TeleportTracker.teleportRequests.get(playerUuid);

View file

@ -31,11 +31,6 @@ public class DeleteWarpCommand {
} }
private static int execute(CommandContext<ServerCommandSource> context, String name) { private static int execute(CommandContext<ServerCommandSource> context, String name) {
if (!context.getSource().isExecutedByPlayer()) {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var serverState = RccServer.state.getServerState(); var serverState = RccServer.state.getServerState();
var warps = serverState.warps; var warps = serverState.warps;

View file

@ -5,6 +5,7 @@ import cc.reconnected.server.struct.ServerPosition;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions; import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text; import net.minecraft.text.Text;
@ -24,12 +25,8 @@ public class SetWarpCommand {
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }
private static int execute(CommandContext<ServerCommandSource> context, String name) { private static int execute(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var serverState = RccServer.state.getServerState(); var serverState = RccServer.state.getServerState();
var warps = serverState.warps; var warps = serverState.warps;

View file

@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderContext;
import me.lucko.fabric.api.permissions.v0.Permissions; import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@ -33,12 +34,8 @@ public class WarpCommand {
dispatcher.register(rootCommand); dispatcher.register(rootCommand);
} }
private static int execute(CommandContext<ServerCommandSource> context, String name) { private static int execute(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
if (!context.getSource().isExecutedByPlayer()) { var player = context.getSource().getPlayerOrThrow();
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1;
}
var player = context.getSource().getPlayer();
var serverState = RccServer.state.getServerState(); var serverState = RccServer.state.getServerState();
var warps = serverState.warps; var warps = serverState.warps;
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);

View file

@ -9,7 +9,6 @@ import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.parsers.NodeParser; import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.TextParserV1; import eu.pb4.placeholders.api.parsers.TextParserV1;
import net.minecraft.entity.damage.DamageTracker; import net.minecraft.entity.damage.DamageTracker;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.s2c.play.PositionFlag; import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;