From 735e04768343ce28829213c1c6c039339ea2732d Mon Sep 17 00:00:00 2001 From: Pedro270707 Date: Mon, 25 Sep 2023 12:45:33 -0300 Subject: [PATCH] Added message command --- .../commander/CommanderHelper.java | 13 +++ .../content/CommanderClientCommandSource.java | 7 ++ .../content/CommanderCommandManager.java | 1 + .../content/CommanderCommandSource.java | 2 + .../CommanderConsoleCommandSource.java | 5 ++ .../content/CommanderServerCommandSource.java | 5 ++ .../BlockCoordinatesArgumentType.java | 16 ++-- .../content/commands/AchievementCommand.java | 88 +++++++++---------- .../content/commands/KillCommand.java | 22 ++--- .../content/commands/MessageCommand.java | 49 +++++++++++ .../content/commands/SetBlockCommand.java | 16 ++-- .../content/commands/SummonCommand.java | 31 +++++-- .../content/commands/TeleportCommand.java | 15 +++- .../content/helpers/DoubleCoordinates.java | 6 +- .../helpers/EntitySelectorOptions.java | 4 +- ...Coordinate.java => IntegerCoordinate.java} | 12 +-- ...ordinates.java => IntegerCoordinates.java} | 10 +-- src/main/resources/lang/commander/en_US.lang | 13 +++ 18 files changed, 223 insertions(+), 92 deletions(-) create mode 100644 src/main/java/net/pedroricardo/commander/content/commands/MessageCommand.java rename src/main/java/net/pedroricardo/commander/content/helpers/{BlockCoordinate.java => IntegerCoordinate.java} (78%) rename src/main/java/net/pedroricardo/commander/content/helpers/{BlockCoordinates.java => IntegerCoordinates.java} (91%) diff --git a/src/main/java/net/pedroricardo/commander/CommanderHelper.java b/src/main/java/net/pedroricardo/commander/CommanderHelper.java index df433ce..efc1578 100644 --- a/src/main/java/net/pedroricardo/commander/CommanderHelper.java +++ b/src/main/java/net/pedroricardo/commander/CommanderHelper.java @@ -5,8 +5,12 @@ import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.core.entity.Entity; +import net.minecraft.core.entity.EntityDispatcher; +import net.minecraft.core.entity.EntityLiving; import net.minecraft.core.net.command.Command; import net.minecraft.core.net.command.Commands; +import net.minecraft.core.util.helper.LogPrintStream; import net.pedroricardo.commander.content.CommanderCommandManager; import java.awt.event.KeyEvent; @@ -114,4 +118,13 @@ public static boolean matchesKeyString(String checkedString, String input) { } return checkedString.substring(checkedString.indexOf('.') + 1).equals(input); } + + public static String getEntityName(Entity entity) { + if (entity instanceof EntityLiving) { + if (!LogPrintStream.removeColorCodes(((EntityLiving) entity).getDisplayName()).isEmpty()) { + return ((EntityLiving) entity).getDisplayName(); + } + } + return EntityDispatcher.getEntityString(entity); + } } diff --git a/src/main/java/net/pedroricardo/commander/content/CommanderClientCommandSource.java b/src/main/java/net/pedroricardo/commander/content/CommanderClientCommandSource.java index c3f5ede..b8e9c01 100644 --- a/src/main/java/net/pedroricardo/commander/content/CommanderClientCommandSource.java +++ b/src/main/java/net/pedroricardo/commander/content/CommanderClientCommandSource.java @@ -77,6 +77,13 @@ public void sendMessage(String message) { this.mc.ingameGUI.addChatMessage(message); } + @Override + public void sendMessageToPlayer(EntityPlayer player, String message) { + if (player == this.mc.thePlayer) { + this.sendMessage(message); + } + } + @Override public World getWorld() { return this.mc.theWorld; diff --git a/src/main/java/net/pedroricardo/commander/content/CommanderCommandManager.java b/src/main/java/net/pedroricardo/commander/content/CommanderCommandManager.java index 08958d8..f34eaa9 100644 --- a/src/main/java/net/pedroricardo/commander/content/CommanderCommandManager.java +++ b/src/main/java/net/pedroricardo/commander/content/CommanderCommandManager.java @@ -27,6 +27,7 @@ public class CommanderCommandManager { SetBlockCommand.register(DISPATCHER); SummonCommand.register(DISPATCHER); TeleportCommand.register(DISPATCHER); + MessageCommand.register(DISPATCHER); registerLegacyCommands(); diff --git a/src/main/java/net/pedroricardo/commander/content/CommanderCommandSource.java b/src/main/java/net/pedroricardo/commander/content/CommanderCommandSource.java index 12ed3b6..c322c19 100644 --- a/src/main/java/net/pedroricardo/commander/content/CommanderCommandSource.java +++ b/src/main/java/net/pedroricardo/commander/content/CommanderCommandSource.java @@ -33,6 +33,8 @@ default Collection getEntitySuggestions() { void sendMessage(String message); + void sendMessageToPlayer(EntityPlayer player, String message); + World getWorld(); World getWorld(int dimension); diff --git a/src/main/java/net/pedroricardo/commander/content/CommanderConsoleCommandSource.java b/src/main/java/net/pedroricardo/commander/content/CommanderConsoleCommandSource.java index 1dabde3..c9018bd 100644 --- a/src/main/java/net/pedroricardo/commander/content/CommanderConsoleCommandSource.java +++ b/src/main/java/net/pedroricardo/commander/content/CommanderConsoleCommandSource.java @@ -75,6 +75,11 @@ public void sendMessage(String message) { LOGGER.info(LogPrintStream.removeColorCodes(message)); } + @Override + public void sendMessageToPlayer(EntityPlayer player, String message) { + this.server.configManager.sendPacketToPlayer(player.username, new Packet3Chat(message, AES.keyChain.get(player.username))); + } + @Override public World getWorld() { return this.server.getWorldManager(0); diff --git a/src/main/java/net/pedroricardo/commander/content/CommanderServerCommandSource.java b/src/main/java/net/pedroricardo/commander/content/CommanderServerCommandSource.java index 4d95f9a..13ca9b3 100644 --- a/src/main/java/net/pedroricardo/commander/content/CommanderServerCommandSource.java +++ b/src/main/java/net/pedroricardo/commander/content/CommanderServerCommandSource.java @@ -73,6 +73,11 @@ public void sendMessage(String message) { this.player.playerNetServerHandler.sendPacket(new Packet3Chat(message, AES.keyChain.get(this.player.username))); } + @Override + public void sendMessageToPlayer(EntityPlayer player, String message) { + this.server.configManager.sendPacketToPlayer(player.username, new Packet3Chat(message, AES.keyChain.get(player.username))); + } + @Override public World getWorld() { return this.getSender().world; diff --git a/src/main/java/net/pedroricardo/commander/content/arguments/BlockCoordinatesArgumentType.java b/src/main/java/net/pedroricardo/commander/content/arguments/BlockCoordinatesArgumentType.java index f8b407a..52fa01f 100644 --- a/src/main/java/net/pedroricardo/commander/content/arguments/BlockCoordinatesArgumentType.java +++ b/src/main/java/net/pedroricardo/commander/content/arguments/BlockCoordinatesArgumentType.java @@ -9,15 +9,15 @@ import net.minecraft.core.util.phys.Vec3d; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.exceptions.CommanderExceptions; -import net.pedroricardo.commander.content.helpers.BlockCoordinate; -import net.pedroricardo.commander.content.helpers.BlockCoordinates; +import net.pedroricardo.commander.content.helpers.IntegerCoordinate; +import net.pedroricardo.commander.content.helpers.IntegerCoordinates; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.CompletableFuture; -public class BlockCoordinatesArgumentType implements ArgumentType { +public class BlockCoordinatesArgumentType implements ArgumentType { private static final List EXAMPLES = Arrays.asList("~ ~ ~", "0 0 0", "~ ~60 ~", "~-20 ~10 -25"); public static BlockCoordinatesArgumentType blockCoordinates() { @@ -25,9 +25,9 @@ public static BlockCoordinatesArgumentType blockCoordinates() { } @Override - public BlockCoordinates parse(StringReader reader) throws CommandSyntaxException { + public IntegerCoordinates parse(StringReader reader) throws CommandSyntaxException { int i = reader.getCursor(); - BlockCoordinate x = BlockCoordinate.parse(reader); + IntegerCoordinate x = IntegerCoordinate.parse(reader); if (!reader.canRead() || reader.peek() != ' ') { if (reader.peek() == 'f' || reader.peek() == 'd') { reader.skip(); @@ -41,7 +41,7 @@ public BlockCoordinates parse(StringReader reader) throws CommandSyntaxException } } reader.skip(); - BlockCoordinate y = BlockCoordinate.parse(reader); + IntegerCoordinate y = IntegerCoordinate.parse(reader); if (!reader.canRead() || reader.peek() != ' ') { if (reader.peek() == 'f' || reader.peek() == 'd') { reader.skip(); @@ -55,8 +55,8 @@ public BlockCoordinates parse(StringReader reader) throws CommandSyntaxException } } reader.skip(); - BlockCoordinate z = BlockCoordinate.parse(reader); - return new BlockCoordinates(x, y, z); + IntegerCoordinate z = IntegerCoordinate.parse(reader); + return new IntegerCoordinates(x, y, z); } @Override diff --git a/src/main/java/net/pedroricardo/commander/content/commands/AchievementCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/AchievementCommand.java index ed7b5df..9b26261 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/AchievementCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/AchievementCommand.java @@ -28,60 +28,60 @@ public class AchievementCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register((LiteralArgumentBuilder)(((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("achievement")) .requires(source -> ((CommanderCommandSource)source).hasAdmin()) - .then(((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("grant") - ).then((RequiredArgumentBuilder)RequiredArgumentBuilder.argument("entities", EntityArgumentType.players()) - .then(RequiredArgumentBuilder.argument("achievement", AchievementArgumentType.achievement()) - .executes(c -> { - List entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource()); - Achievement achievement = c.getArgument("achievement", Achievement.class); + .then(((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("grant")) + .then((RequiredArgumentBuilder)RequiredArgumentBuilder.argument("entities", EntityArgumentType.players()) + .then(RequiredArgumentBuilder.argument("achievement", AchievementArgumentType.achievement()) + .executes(c -> { + List entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource()); + Achievement achievement = c.getArgument("achievement", Achievement.class); - if (entities.size() == 1 && ((EntityPlayer)entities.get(0)).getStat(achievement) != 0) { - throw PLAYER_ALREADY_HAS_ACHIEVEMENT.create(); - } - if (entities.size() == 0) { - throw CommanderExceptions.emptySelector().create(); - } + if (entities.size() == 1 && ((EntityPlayer)entities.get(0)).getStat(achievement) != 0) { + throw PLAYER_ALREADY_HAS_ACHIEVEMENT.create(); + } + if (entities.size() == 0) { + throw CommanderExceptions.emptySelector().create(); + } - List achievements = new ArrayList<>(); - achievements.add(achievement); + List achievements = new ArrayList<>(); + achievements.add(achievement); - while (achievements.get(achievements.size() - 1).parent != null) { - achievements.add(achievements.get(achievements.size() - 1).parent); - } - for (int i = 0; i < achievements.size(); i++) { - for (Entity entity : entities) { - ((EntityPlayer)entity).triggerAchievement(achievements.get(achievements.size() - 1 - i)); - } - } + while (achievements.get(achievements.size() - 1).parent != null) { + achievements.add(achievements.get(achievements.size() - 1).parent); + } + for (int i = 0; i < achievements.size(); i++) { + for (Entity entity : entities) { + ((EntityPlayer)entity).triggerAchievement(achievements.get(achievements.size() - 1 - i)); + } + } - sendContextualMessage((CommanderCommandSource) c.getSource(), entities, achievement); + sendContextualMessage((CommanderCommandSource) c.getSource(), entities, achievement); - return CommanderCommandManager.SINGLE_SUCCESS; - })) - .then((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("*") - .executes(c -> { - List entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource()); + return CommanderCommandManager.SINGLE_SUCCESS; + })) + .then((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("*") + .executes(c -> { + List entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource()); - if (entities.size() == 0) { - throw CommanderExceptions.emptySelector().create(); - } + if (entities.size() == 0) { + throw CommanderExceptions.emptySelector().create(); + } - for (Achievement achievement : AchievementList.achievementList) { - List achievements = new ArrayList<>(); - achievements.add(achievement); - while (achievements.get(achievements.size() - 1).parent != null) { - achievements.add(achievements.get(achievements.size() - 1).parent); - } - for (int i = 0; i < achievements.size(); i++) { - for (Entity entity : entities) { - ((EntityPlayer)entity).triggerAchievement(achievements.get(achievements.size() - 1 - i)); + for (Achievement achievement : AchievementList.achievementList) { + List achievements = new ArrayList<>(); + achievements.add(achievement); + while (achievements.get(achievements.size() - 1).parent != null) { + achievements.add(achievements.get(achievements.size() - 1).parent); + } + for (int i = 0; i < achievements.size(); i++) { + for (Entity entity : entities) { + ((EntityPlayer)entity).triggerAchievement(achievements.get(achievements.size() - 1 - i)); + } + } } - } - } - sendWildcardContextualMessage(((CommanderCommandSource)c.getSource()), entities); + sendWildcardContextualMessage(((CommanderCommandSource)c.getSource()), entities); - return CommanderCommandManager.SINGLE_SUCCESS; + return CommanderCommandManager.SINGLE_SUCCESS; })))))); } diff --git a/src/main/java/net/pedroricardo/commander/content/commands/KillCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/KillCommand.java index 86ee489..4af0309 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/KillCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/KillCommand.java @@ -1,27 +1,19 @@ package net.pedroricardo.commander.content.commands; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; -import net.minecraft.core.block.Block; import net.minecraft.core.entity.Entity; import net.minecraft.core.entity.EntityLiving; import net.minecraft.core.entity.player.EntityPlayer; -import net.minecraft.core.util.phys.Vec3d; -import net.pedroricardo.commander.Commander; +import net.minecraft.core.lang.I18n; +import net.pedroricardo.commander.CommanderHelper; import net.pedroricardo.commander.content.CommanderCommandManager; import net.pedroricardo.commander.content.CommanderCommandSource; -import net.pedroricardo.commander.content.arguments.BlockArgumentType; -import net.pedroricardo.commander.content.arguments.BlockCoordinatesArgumentType; import net.pedroricardo.commander.content.arguments.EntityArgumentType; import net.pedroricardo.commander.content.exceptions.CommanderExceptions; -import net.pedroricardo.commander.content.helpers.BlockCoordinates; import net.pedroricardo.commander.content.helpers.EntitySelector; -import java.util.Collection; -import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @SuppressWarnings("unchecked") @@ -36,12 +28,21 @@ public static void register(CommandDispatcher dispatcher sender.killPlayer(); + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", sender.getDisplayName())); + return CommanderCommandManager.SINGLE_SUCCESS; }) .then(RequiredArgumentBuilder.argument("entities", EntityArgumentType.entities()) .executes(c -> { EntitySelector entitySelector = c.getArgument("entities", EntitySelector.class); CopyOnWriteArrayList entities = new CopyOnWriteArrayList<>(entitySelector.get((CommanderCommandSource)c.getSource())); + + int entityCount = entities.size(); + + if (entityCount == 0) ((CommanderCommandSource)c.getSource()).sendMessage("§e" + I18n.getInstance().translateKey("commands.commander.kill.failure")); + else if (entityCount == 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", CommanderHelper.getEntityName(entities.get(0)))); + else ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.multiple_entities", entityCount)); + for (Entity entity : entities) { if (entity instanceof EntityPlayer) { ((EntityPlayer) entity).killPlayer(); @@ -51,6 +52,7 @@ public static void register(CommandDispatcher dispatcher entity.remove(); } } + return CommanderCommandManager.SINGLE_SUCCESS; }))); } diff --git a/src/main/java/net/pedroricardo/commander/content/commands/MessageCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/MessageCommand.java new file mode 100644 index 0000000..e703962 --- /dev/null +++ b/src/main/java/net/pedroricardo/commander/content/commands/MessageCommand.java @@ -0,0 +1,49 @@ +package net.pedroricardo.commander.content.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.tree.CommandNode; +import net.minecraft.core.entity.Entity; +import net.minecraft.core.entity.player.EntityPlayer; +import net.minecraft.core.lang.I18n; +import net.minecraft.core.util.helper.LogPrintStream; +import net.pedroricardo.commander.content.CommanderCommandManager; +import net.pedroricardo.commander.content.CommanderCommandSource; +import net.pedroricardo.commander.content.arguments.EntityArgumentType; +import net.pedroricardo.commander.content.helpers.EntitySelector; + +import java.util.List; + +@SuppressWarnings("unchecked") +public class MessageCommand { + public static void register(CommandDispatcher dispatcher) { + CommandNode command = dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("message") + .then((RequiredArgumentBuilder) RequiredArgumentBuilder.argument("targets", EntityArgumentType.players()) + .then((RequiredArgumentBuilder) RequiredArgumentBuilder.argument("message", StringArgumentType.greedyString()) + .executes(c -> { + EntitySelector entitySelector = c.getArgument("targets", EntitySelector.class); + String message = c.getArgument("message", String.class); + + String senderName = ((CommanderCommandSource)c.getSource()).getSender() == null ? "Server" : LogPrintStream.removeColorCodes(((CommanderCommandSource)c.getSource()).getSender().getDisplayName()); + + List players = entitySelector.get((CommanderCommandSource) c.getSource()); + + for (Entity player : players) { + ((CommanderCommandSource)c.getSource()).sendMessage("§8§o" + I18n.getInstance().translateKeyAndFormat("commands.commander.message.outgoing", LogPrintStream.removeColorCodes(((EntityPlayer)player).getDisplayName()), message)); + ((CommanderCommandSource)c.getSource()).sendMessageToPlayer((EntityPlayer)player, "§8§o" + I18n.getInstance().translateKeyAndFormat("commands.commander.message.incoming", senderName, message)); + } + + return CommanderCommandManager.SINGLE_SUCCESS; + })))); + dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("msg") + .redirect(command)); + dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("whisper") + .redirect(command)); + dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("w") + .redirect(command)); + dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("tell") + .redirect(command)); + } +} diff --git a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java index 52bcc97..85cbe1a 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java @@ -1,29 +1,33 @@ package net.pedroricardo.commander.content.commands; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import net.minecraft.core.block.Block; +import net.minecraft.core.lang.I18n; import net.minecraft.core.util.collection.Pair; -import net.minecraft.core.util.phys.Vec3d; import net.pedroricardo.commander.content.CommanderCommandManager; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.arguments.*; -import net.pedroricardo.commander.content.helpers.BlockCoordinates; +import net.pedroricardo.commander.content.helpers.IntegerCoordinates; @SuppressWarnings("unchecked") public class SetBlockCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("setblock") - .requires(sourceStack -> ((CommanderCommandSource)sourceStack).hasAdmin()) + .requires(source -> ((CommanderCommandSource)source).hasAdmin()) .then(RequiredArgumentBuilder.argument("position", BlockCoordinatesArgumentType.blockCoordinates()) .then(RequiredArgumentBuilder.argument("block", BlockArgumentType.block()) .executes(c -> { - BlockCoordinates coordinates = c.getArgument("position", BlockCoordinates.class); + IntegerCoordinates coordinates = c.getArgument("position", IntegerCoordinates.class); Pair pair = c.getArgument("block", Pair.class); - ((CommanderCommandSource)c.getSource()).getWorld().setBlockAndMetadataWithNotify(coordinates.getX((CommanderCommandSource)c.getSource()), coordinates.getY((CommanderCommandSource)c.getSource()), coordinates.getZ((CommanderCommandSource)c.getSource()), pair.getLeft().id, pair.getRight()); + if (!((CommanderCommandSource)c.getSource()).getWorld().isBlockLoaded(coordinates.getX((CommanderCommandSource)c.getSource()), coordinates.getY((CommanderCommandSource)c.getSource()), coordinates.getZ((CommanderCommandSource)c.getSource()))) { + ((CommanderCommandSource)c.getSource()).sendMessage("§e" + I18n.getInstance().translateKey("commands.commander.setblock.failure")); + } else { + ((CommanderCommandSource)c.getSource()).getWorld().setBlockAndMetadataWithNotify(coordinates.getX((CommanderCommandSource)c.getSource()), coordinates.getY((CommanderCommandSource)c.getSource()), coordinates.getZ((CommanderCommandSource)c.getSource()), pair.getLeft().id, pair.getRight()); + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.setblock.success", coordinates.getX((CommanderCommandSource)c.getSource()), coordinates.getY((CommanderCommandSource)c.getSource()), coordinates.getZ((CommanderCommandSource)c.getSource()))); + } return CommanderCommandManager.SINGLE_SUCCESS; })))); diff --git a/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java index 2ad23b1..06ece93 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java @@ -1,12 +1,15 @@ package net.pedroricardo.commander.content.commands; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import net.minecraft.core.entity.Entity; +import net.minecraft.core.lang.I18n; import net.minecraft.core.util.phys.Vec3d; import net.minecraft.core.world.World; +import net.pedroricardo.commander.CommanderHelper; import net.pedroricardo.commander.content.CommanderCommandManager; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.arguments.EntitySummonArgumentType; @@ -18,13 +21,15 @@ public class SummonCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("summon") - .requires(c -> ((CommanderCommandSource)c).hasAdmin()) + .requires(source -> ((CommanderCommandSource)source).hasAdmin()) .then(RequiredArgumentBuilder.argument("entity", EntitySummonArgumentType.entity()) .executes(c -> { Vec3d coordinates = ((CommanderCommandSource)c.getSource()).getCoordinates(false); if (coordinates == null) throw CommanderExceptions.notInWorld().create(); - summonEntityAt(c, coordinates.xCoord, coordinates.yCoord - 1.6, coordinates.zCoord, 0.0f, 0.0f); + Entity entity = summonEntityAt(c, coordinates.xCoord, coordinates.yCoord - ((CommanderCommandSource)c.getSource()).getSender().heightOffset, coordinates.zCoord, 0.0f, 0.0f); + + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.summon.success_single_entity", CommanderHelper.getEntityName(entity))); return CommanderCommandManager.SINGLE_SUCCESS; }) @@ -32,13 +37,28 @@ public static void register(CommandDispatcher dispatcher .executes(c -> { DoubleCoordinates coordinates = c.getArgument("position", DoubleCoordinates.class); - summonEntityAt(c, coordinates.getX(((CommanderCommandSource)c.getSource())), coordinates.getY(((CommanderCommandSource)c.getSource())), coordinates.getZ(((CommanderCommandSource)c.getSource())), 0.0f, 0.0f); + Entity entity = summonEntityAt(c, coordinates.getX(((CommanderCommandSource)c.getSource())), coordinates.getY(((CommanderCommandSource)c.getSource()), true), coordinates.getZ(((CommanderCommandSource)c.getSource())), 0.0f, 0.0f); + + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.summon.success_single_entity", CommanderHelper.getEntityName(entity))); return CommanderCommandManager.SINGLE_SUCCESS; - })))); + }) + .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1, 255)) + .executes(c -> { + DoubleCoordinates coordinates = c.getArgument("position", DoubleCoordinates.class); + int amount = c.getArgument("amount", Integer.class); + + for (int i = 0; i < amount; i++) { + summonEntityAt(c, coordinates.getX(((CommanderCommandSource) c.getSource())), coordinates.getY(((CommanderCommandSource) c.getSource()), true), coordinates.getZ(((CommanderCommandSource) c.getSource())), 0.0f, 0.0f); + } + + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.summon.success_multiple_entities", amount)); + + return CommanderCommandManager.SINGLE_SUCCESS; + }))))); } - private static void summonEntityAt(CommandContext c, double x, double y, double z, float yaw, float pitch) { + private static Entity summonEntityAt(CommandContext c, double x, double y, double z, float yaw, float pitch) { Class entityClass = c.getArgument("entity", Class.class); Entity entity; try { @@ -49,5 +69,6 @@ private static void summonEntityAt(CommandContext c, double x, double y, entity.spawnInit(); entity.moveTo(x, y, z, yaw, pitch); ((CommanderCommandSource)c.getSource()).getWorld().entityJoinedWorld(entity); + return entity; } } diff --git a/src/main/java/net/pedroricardo/commander/content/commands/TeleportCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/TeleportCommand.java index 7b9798d..dbb3400 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/TeleportCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/TeleportCommand.java @@ -4,8 +4,9 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; -import com.mojang.brigadier.tree.LiteralCommandNode; import net.minecraft.core.entity.Entity; +import net.minecraft.core.lang.I18n; +import net.pedroricardo.commander.CommanderHelper; import net.pedroricardo.commander.content.CommanderCommandManager; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.arguments.EntityArgumentType; @@ -20,12 +21,14 @@ public class TeleportCommand { public static void register(CommandDispatcher dispatcher) { CommandNode command = dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("teleport") + .requires(source -> ((CommanderCommandSource)source).hasAdmin()) .then((RequiredArgumentBuilder)RequiredArgumentBuilder.argument("position", Vec3dArgumentType.vec3d()) .executes(c -> { DoubleCoordinates targetCoordinates = c.getArgument("position", DoubleCoordinates.class); if (((CommanderCommandSource)c.getSource()).getSender() != null) { - ((CommanderCommandSource)c.getSource()).getSender().moveTo(targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()).getCoordinates(true).yCoord), targetCoordinates.getZ((CommanderCommandSource)c.getSource()), ((CommanderCommandSource) c.getSource()).getSender().yRot, ((CommanderCommandSource) c.getSource()).getSender().xRot); + ((CommanderCommandSource)c.getSource()).getSender().moveTo(targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()), true), targetCoordinates.getZ((CommanderCommandSource)c.getSource()), ((CommanderCommandSource) c.getSource()).getSender().yRot, ((CommanderCommandSource) c.getSource()).getSender().xRot); + ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.teleport.location.success_single_entity", ((CommanderCommandSource)c.getSource()).getSender().getDisplayName(), targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()), true), targetCoordinates.getZ((CommanderCommandSource)c.getSource()))); } else { throw CommanderExceptions.notInWorld().create(); } @@ -40,9 +43,12 @@ public static void register(CommandDispatcher dispatcher List entities = entitySelector.get((CommanderCommandSource) c.getSource()); for (Entity entity : entities) { - entity.moveTo(targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()).getCoordinates(true).yCoord), targetCoordinates.getZ((CommanderCommandSource)c.getSource()), entity.yRot, entity.xRot); + entity.moveTo(targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()), true), targetCoordinates.getZ((CommanderCommandSource)c.getSource()), entity.yRot, entity.xRot); } + if (entities.size() == 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.teleport.location.success_single_entity", entities.get(0), targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()), true), targetCoordinates.getZ((CommanderCommandSource)c.getSource()))); + else if (entities.size() > 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.teleport.location.success_multiple_entities", entities.size(), targetCoordinates.getX((CommanderCommandSource)c.getSource()), targetCoordinates.getY(((CommanderCommandSource) c.getSource()), true), targetCoordinates.getZ((CommanderCommandSource)c.getSource()))); + return CommanderCommandManager.SINGLE_SUCCESS; })) .then((RequiredArgumentBuilder)RequiredArgumentBuilder.argument("target", EntityArgumentType.entity()) @@ -56,6 +62,9 @@ public static void register(CommandDispatcher dispatcher entity.moveTo(targetEntity.x, targetEntity.y - targetEntity.heightOffset, targetEntity.z, entity.yRot, entity.xRot); } + if (entities.size() == 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.teleport.entity.success_single_entity", entities.get(0), CommanderHelper.getEntityName(targetEntity))); + else if (entities.size() > 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.teleport.entity.success_multiple_entities", entities.size(), CommanderHelper.getEntityName(targetEntity))); + return CommanderCommandManager.SINGLE_SUCCESS; })))); dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("tp") diff --git a/src/main/java/net/pedroricardo/commander/content/helpers/DoubleCoordinates.java b/src/main/java/net/pedroricardo/commander/content/helpers/DoubleCoordinates.java index 0629550..714b720 100644 --- a/src/main/java/net/pedroricardo/commander/content/helpers/DoubleCoordinates.java +++ b/src/main/java/net/pedroricardo/commander/content/helpers/DoubleCoordinates.java @@ -38,15 +38,15 @@ public double getX(CommanderCommandSource source) throws CommandSyntaxException return this.x.get(source.getCoordinates(false).xCoord); } - public double getY(CommanderCommandSource source) throws CommandSyntaxException { - if (source.getCoordinates(false) == null) { + public double getY(CommanderCommandSource source, boolean offsetHeight) throws CommandSyntaxException { + if (source.getCoordinates(offsetHeight) == null) { if (!this.y.isRelative()) { return this.y.get(0.0); } else { throw CommanderExceptions.notInWorld().create(); } } - return this.y.get(source.getCoordinates(false).yCoord); + return this.y.get(source.getCoordinates(offsetHeight).yCoord); } public double getZ(CommanderCommandSource source) throws CommandSyntaxException { diff --git a/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelectorOptions.java b/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelectorOptions.java index 9debf0c..c86dae7 100644 --- a/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelectorOptions.java +++ b/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelectorOptions.java @@ -13,6 +13,7 @@ import net.minecraft.core.lang.text.Text; import net.minecraft.core.lang.text.TextTranslatable; import net.minecraft.core.player.gamemode.Gamemode; +import net.minecraft.core.util.helper.LogPrintStream; import net.pedroricardo.commander.Commander; import net.pedroricardo.commander.CommanderHelper; @@ -84,8 +85,7 @@ public static void register(String key, Modifier modifier, Predicate { if (!(entity instanceof EntityLiving)) return bl; else if (entity instanceof EntityPlayer) return ((EntityPlayer)entity).username.equals(string) != bl; - else if (!((EntityLiving)entity).getDisplayName().startsWith("§") || ((EntityLiving)entity).getDisplayName().length() < 2) return ((EntityLiving)entity).getDisplayName().equals(string) != bl; - return ((EntityLiving)entity).getDisplayName().substring(2).equals(string) != bl; + else return LogPrintStream.removeColorCodes(((EntityLiving)entity).getDisplayName()).equals(string) != bl; }); }, entitySelectorParser -> !entitySelectorParser.hasNameEquals(), new TextTranslatable("argument_types.commander.entity.selector.options.name.description")); register("distance", (parser) -> { diff --git a/src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinate.java b/src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinate.java similarity index 78% rename from src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinate.java rename to src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinate.java index 48c18fc..c05ab01 100644 --- a/src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinate.java +++ b/src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinate.java @@ -5,11 +5,11 @@ import net.pedroricardo.commander.content.exceptions.CommanderExceptions; import org.jetbrains.annotations.Nullable; -public class BlockCoordinate { +public class IntegerCoordinate { private final boolean isRelative; private final int coordinate; - public BlockCoordinate(boolean isRelative, int coordinate) { + public IntegerCoordinate(boolean isRelative, int coordinate) { this.isRelative = isRelative; this.coordinate = coordinate; } @@ -26,20 +26,20 @@ public int get(@Nullable Integer sourceCoordinate) throws CommandSyntaxException return this.coordinate; } - public static BlockCoordinate parse(StringReader reader) throws CommandSyntaxException { + public static IntegerCoordinate parse(StringReader reader) throws CommandSyntaxException { if (!reader.canRead()) throw CommanderExceptions.incomplete().createWithContext(reader); if (reader.peek() == '~') { reader.skip(); if (reader.canRead() && reader.peek() != ' ') { int coordinate = reader.readInt(); - return new BlockCoordinate(true, coordinate); + return new IntegerCoordinate(true, coordinate); } else { - return new BlockCoordinate(true, 0); + return new IntegerCoordinate(true, 0); } } else if (reader.peek() != ' ') { int coordinate = reader.readInt(); - return new BlockCoordinate(false, coordinate); + return new IntegerCoordinate(false, coordinate); } throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt().createWithContext(reader); diff --git a/src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinates.java b/src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinates.java similarity index 91% rename from src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinates.java rename to src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinates.java index 6f86ae5..f3f5ad8 100644 --- a/src/main/java/net/pedroricardo/commander/content/helpers/BlockCoordinates.java +++ b/src/main/java/net/pedroricardo/commander/content/helpers/IntegerCoordinates.java @@ -5,12 +5,12 @@ import net.pedroricardo.commander.content.exceptions.CommanderExceptions; import org.jetbrains.annotations.Nullable; -public class BlockCoordinates { - private final BlockCoordinate x; - private final BlockCoordinate y; - private final BlockCoordinate z; +public class IntegerCoordinates { + private final IntegerCoordinate x; + private final IntegerCoordinate y; + private final IntegerCoordinate z; - public BlockCoordinates(BlockCoordinate x, BlockCoordinate y, BlockCoordinate z) { + public IntegerCoordinates(IntegerCoordinate x, IntegerCoordinate y, IntegerCoordinate z) { this.x = x; this.y = y; this.z = z; diff --git a/src/main/resources/lang/commander/en_US.lang b/src/main/resources/lang/commander/en_US.lang index 6cd43d4..dd88a46 100644 --- a/src/main/resources/lang/commander/en_US.lang +++ b/src/main/resources/lang/commander/en_US.lang @@ -46,5 +46,18 @@ commands.commander.clear.success_multiple_items_single_entity=%s item slots clea commands.commander.clear.success_single_item_multiple_entities=%s item slot cleared from %s entities commands.commander.clear.success_multiple_items_multiple_entities=%s item slots cleared from %s entities commands.commander.clear.failure=No items were cleared +commands.commander.kill.single_entity=Killed %s +commands.commander.kill.multiple_entities=Killed %s entities +commands.commander.kill.failure=No entity was killed +commands.commander.summon.success_single_entity=Summoned %s +commands.commander.summon.success_multiple_entities=Summoned %s entities +commands.commander.teleport.entity.success_single_entity=Teleported %s to %s +commands.commander.teleport.entity.success_multiple_entities=Teleported %s entities to %s +commands.commander.teleport.location.success_single_entity=Teleported %s to %s, %s, %s +commands.commander.teleport.location.success_multiple_entities=Teleported %s entities to %s, %s, %s +commands.commander.setblock.failure=Could not set the block +commands.commander.setblock.success=Changed the block at %s, %s, %s +commands.commander.message.incoming=%s whispers to you: %s +commands.commander.message.outgoing=You whisper to %s: %s argument_types.commander.incomplete=Incomplete argument argument_types.commander.not_in_world=Argument requires sender to be in world, but they are not present \ No newline at end of file