From 1aa225f7001c77cc7d0d5cc2209c87440318cc84 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Sat, 9 Nov 2024 12:58:32 +0100 Subject: [PATCH] Replace redundant code with shorter version --- .../server/commands/admin/FlyCommand.java | 6 +----- .../server/commands/admin/GodCommand.java | 6 +----- .../server/commands/home/DeleteHomeCommand.java | 9 +++------ .../server/commands/home/HomeCommand.java | 9 +++------ .../server/commands/home/SetHomeCommand.java | 9 +++------ .../server/commands/misc/AfkCommand.java | 8 +------- .../server/commands/misc/NearCommand.java | 16 ++-------------- .../server/commands/spawn/SetSpawnCommand.java | 7 +------ .../server/commands/spawn/SpawnCommand.java | 7 +------ .../server/commands/teleport/BackCommand.java | 7 +------ .../commands/teleport/TeleportAcceptCommand.java | 7 +------ .../commands/teleport/TeleportAskCommand.java | 9 +++------ .../teleport/TeleportAskHereCommand.java | 10 +++------- .../commands/teleport/TeleportDenyCommand.java | 7 +------ .../server/commands/warp/DeleteWarpCommand.java | 5 ----- .../server/commands/warp/SetWarpCommand.java | 9 +++------ .../server/commands/warp/WarpCommand.java | 9 +++------ .../server/mixin/ServerPlayerEntityMixin.java | 1 - 18 files changed, 31 insertions(+), 110 deletions(-) diff --git a/src/main/java/cc/reconnected/server/commands/admin/FlyCommand.java b/src/main/java/cc/reconnected/server/commands/admin/FlyCommand.java index 77f1a7c..bb75406 100644 --- a/src/main/java/cc/reconnected/server/commands/admin/FlyCommand.java +++ b/src/main/java/cc/reconnected/server/commands/admin/FlyCommand.java @@ -18,11 +18,7 @@ public class FlyCommand { var rootCommand = literal("fly") .requires(Permissions.require("rcc.command.fly", 3)) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); context.getSource().sendFeedback(() -> toggleFlight(player), true); return 1; diff --git a/src/main/java/cc/reconnected/server/commands/admin/GodCommand.java b/src/main/java/cc/reconnected/server/commands/admin/GodCommand.java index 14eac18..cd77dcc 100644 --- a/src/main/java/cc/reconnected/server/commands/admin/GodCommand.java +++ b/src/main/java/cc/reconnected/server/commands/admin/GodCommand.java @@ -18,11 +18,7 @@ public class GodCommand { var rootCommand = literal("god") .requires(Permissions.require("rcc.command.god", 3)) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); context.getSource().sendFeedback(() -> toggleGod(player), true); return 1; diff --git a/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java index 7f85b7d..dbf23c6 100644 --- a/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java @@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.command.CommandSource; @@ -34,12 +35,8 @@ public class DeleteHomeCommand { dispatcher.register(rootCommand); } - private static int execute(CommandContext 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(); + private static int execute(CommandContext context, String name) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); var playerState = RccServer.state.getPlayerState(player.getUuid()); var homes = playerState.homes; var playerContext = PlaceholderContext.of(context.getSource().getPlayer()); diff --git a/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java index 1b6831f..65095c1 100644 --- a/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java @@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.command.CommandSource; @@ -34,12 +35,8 @@ public class HomeCommand { dispatcher.register(rootCommand); } - private static int execute(CommandContext 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(); + private static int execute(CommandContext context, String name) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); var playerState = RccServer.state.getPlayerState(player.getUuid()); var homes = playerState.homes; var playerContext = PlaceholderContext.of(context.getSource().getPlayer()); diff --git a/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java index 27cb1c3..4ec83e5 100644 --- a/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java @@ -7,6 +7,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.server.command.ServerCommandSource; @@ -36,12 +37,8 @@ public class SetHomeCommand { dispatcher.register(rootCommand); } - private static int execute(CommandContext context, String name, boolean forced) { - 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(); + private static int execute(CommandContext context, String name, boolean forced) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); var playerState = RccServer.state.getPlayerState(player.getUuid()); var homes = playerState.homes; var playerContext = PlaceholderContext.of(player); diff --git a/src/main/java/cc/reconnected/server/commands/misc/AfkCommand.java b/src/main/java/cc/reconnected/server/commands/misc/AfkCommand.java index 904e900..9989e2a 100644 --- a/src/main/java/cc/reconnected/server/commands/misc/AfkCommand.java +++ b/src/main/java/cc/reconnected/server/commands/misc/AfkCommand.java @@ -13,13 +13,7 @@ public class AfkCommand { var rootCommand = literal("afk") .requires(Permissions.require("rcc.command.afk", true)) .executes(context -> { - - 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 player = context.getSource().getPlayerOrThrow(); AfkTracker.getInstance().setPlayerAfk(player, true); return 1; diff --git a/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java b/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java index b4f6946..2504f91 100644 --- a/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java +++ b/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java @@ -22,21 +22,9 @@ public class NearCommand { public static void register(CommandDispatcher dispatcher) { var rootCommand = literal("near") .requires(Permissions.require("rcc.command.near", 2)) - .executes(context -> { - 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()); - }) + .executes(context -> execute(context, RccServer.CONFIG.nearCommand.nearCommandDefaultRange, context.getSource().getPlayerOrThrow())) .then(argument("radius", IntegerArgumentType.integer(0, RccServer.CONFIG.nearCommand.nearCommandMaxRange)) - .executes(context -> { - 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()); - })); + .executes(context -> execute(context, IntegerArgumentType.getInteger(context, "radius"), context.getSource().getPlayerOrThrow()))); dispatcher.register(rootCommand); } diff --git a/src/main/java/cc/reconnected/server/commands/spawn/SetSpawnCommand.java b/src/main/java/cc/reconnected/server/commands/spawn/SetSpawnCommand.java index ed9af8a..8f5c527 100644 --- a/src/main/java/cc/reconnected/server/commands/spawn/SetSpawnCommand.java +++ b/src/main/java/cc/reconnected/server/commands/spawn/SetSpawnCommand.java @@ -15,12 +15,7 @@ public class SetSpawnCommand { var rootCommand = literal("setspawn") .requires(Permissions.require("rcc.command.setspawn", 3)) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); var spawnPosition = new ServerPosition(player); var serverState = RccServer.state.getServerState(); diff --git a/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java b/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java index 61e0c05..9fe3df2 100644 --- a/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java +++ b/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java @@ -16,12 +16,7 @@ public class SpawnCommand { var rootCommand = literal("spawn") .requires(Permissions.require("rcc.command.spawn", true)) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); var serverState = RccServer.state.getServerState(); var playerContext = PlaceholderContext.of(player); var spawnPosition = serverState.spawn; diff --git a/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java index 538d84d..13912e7 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java @@ -16,12 +16,7 @@ public class BackCommand { var rootCommand = literal("back") .requires(Permissions.require("rcc.command.back", true)) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); var playerContext = PlaceholderContext.of(player); var lastPosition = BackTracker.lastPlayerPositions.get(player.getUuid()); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java index 139ff77..2aa30b4 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java @@ -20,12 +20,7 @@ public class TeleportAcceptCommand { public static void register(CommandDispatcher dispatcher) { var node = dispatcher.register(literal("tpaccept") .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); var playerUuid = player.getUuid(); var playerRequests = TeleportTracker.teleportRequests.get(playerUuid); var playerContext = PlaceholderContext.of(player); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java index d0b2447..3ef0902 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java @@ -6,6 +6,7 @@ import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import net.minecraft.command.CommandSource; import net.minecraft.server.command.ServerCommandSource; @@ -34,15 +35,11 @@ public class TeleportAskCommand { dispatcher.register(literal("tpask").redirect(node)); } - private static void execute(CommandContext context) { + private static void execute(CommandContext context) throws CommandSyntaxException { var source = context.getSource(); - if (!source.isExecutedByPlayer()) { - source.sendFeedback(() -> Text.of("This command can only be executed by players!"), false); - return; - } + var player = context.getSource().getPlayerOrThrow(); var server = source.getServer(); - var player = source.getPlayer(); var targetName = StringArgumentType.getString(context, "player"); var playerManager = server.getPlayerManager(); var target = playerManager.getPlayer(targetName); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java index 5b72006..039e992 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java @@ -6,6 +6,7 @@ import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import net.minecraft.command.CommandSource; import net.minecraft.server.command.ServerCommandSource; @@ -34,15 +35,10 @@ public class TeleportAskHereCommand { dispatcher.register(literal("tpaskhere").redirect(node)); } - private static void execute(CommandContext context) { + private static void execute(CommandContext context) throws CommandSyntaxException { var source = context.getSource(); - if (!source.isExecutedByPlayer()) { - source.sendFeedback(() -> Text.of("This command can only be executed by players!"), false); - return; - } - + var player = context.getSource().getPlayerOrThrow(); var server = source.getServer(); - var player = source.getPlayer(); var targetName = StringArgumentType.getString(context, "player"); var playerManager = server.getPlayerManager(); var target = playerManager.getPlayer(targetName); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java index 5bc8b5e..9bbf760 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java @@ -46,12 +46,7 @@ public class TeleportDenyCommand { }) .then(argument("uuid", UuidArgumentType.uuid()) .executes(context -> { - 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 player = context.getSource().getPlayerOrThrow(); var uuid = UuidArgumentType.getUuid(context, "uuid"); var playerUuid = player.getUuid(); var playerRequests = TeleportTracker.teleportRequests.get(playerUuid); diff --git a/src/main/java/cc/reconnected/server/commands/warp/DeleteWarpCommand.java b/src/main/java/cc/reconnected/server/commands/warp/DeleteWarpCommand.java index 1896b8b..e847711 100644 --- a/src/main/java/cc/reconnected/server/commands/warp/DeleteWarpCommand.java +++ b/src/main/java/cc/reconnected/server/commands/warp/DeleteWarpCommand.java @@ -31,11 +31,6 @@ public class DeleteWarpCommand { } private static int execute(CommandContext 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 warps = serverState.warps; diff --git a/src/main/java/cc/reconnected/server/commands/warp/SetWarpCommand.java b/src/main/java/cc/reconnected/server/commands/warp/SetWarpCommand.java index ec06516..59bb25a 100644 --- a/src/main/java/cc/reconnected/server/commands/warp/SetWarpCommand.java +++ b/src/main/java/cc/reconnected/server/commands/warp/SetWarpCommand.java @@ -5,6 +5,7 @@ import cc.reconnected.server.struct.ServerPosition; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; @@ -24,12 +25,8 @@ public class SetWarpCommand { dispatcher.register(rootCommand); } - private static int execute(CommandContext 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(); + private static int execute(CommandContext context, String name) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); var serverState = RccServer.state.getServerState(); var warps = serverState.warps; diff --git a/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java b/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java index 4757c45..8403b46 100644 --- a/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java +++ b/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java @@ -5,6 +5,7 @@ import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import eu.pb4.placeholders.api.PlaceholderContext; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.command.CommandSource; @@ -33,12 +34,8 @@ public class WarpCommand { dispatcher.register(rootCommand); } - private static int execute(CommandContext 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(); + private static int execute(CommandContext context, String name) throws CommandSyntaxException { + var player = context.getSource().getPlayerOrThrow(); var serverState = RccServer.state.getServerState(); var warps = serverState.warps; var playerContext = PlaceholderContext.of(player); diff --git a/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java b/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java index ca7a43d..3384ea7 100644 --- a/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java @@ -9,7 +9,6 @@ import eu.pb4.placeholders.api.Placeholders; import eu.pb4.placeholders.api.parsers.NodeParser; import eu.pb4.placeholders.api.parsers.TextParserV1; import net.minecraft.entity.damage.DamageTracker; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.packet.s2c.play.PositionFlag; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld;