From 77992aa4f8da517afa83f49892287c77f88e390b Mon Sep 17 00:00:00 2001 From: Martin Graversen Date: Fri, 24 Jan 2020 23:05:07 +0100 Subject: [PATCH] :sparkles: TeleportCommand --- .../rcon/commands/TeleportCommand.java | 37 +++++++++++++++++++ .../rcon/commands/TeleportCommandTest.java | 18 +++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/io/graversen/minecraft/rcon/commands/TeleportCommand.java create mode 100644 src/test/java/io/graversen/minecraft/rcon/commands/TeleportCommandTest.java diff --git a/src/main/java/io/graversen/minecraft/rcon/commands/TeleportCommand.java b/src/main/java/io/graversen/minecraft/rcon/commands/TeleportCommand.java new file mode 100644 index 0000000..297fe75 --- /dev/null +++ b/src/main/java/io/graversen/minecraft/rcon/commands/TeleportCommand.java @@ -0,0 +1,37 @@ +package io.graversen.minecraft.rcon.commands; + +import io.graversen.minecraft.rcon.commands.base.BaseTargetedCommand; +import io.graversen.minecraft.rcon.util.Position; +import io.graversen.minecraft.rcon.util.Target; +import org.apache.commons.text.StringSubstitutor; + +import java.util.Map; + +public class TeleportCommand extends BaseTargetedCommand { + private final String destination; + + public TeleportCommand(Target target, Target destination) { + super(target); + this.destination = destination.toString(); + } + + public TeleportCommand(Target target, Position destination) { + super(target); + this.destination = destination.toString(); + } + + public String getDestination() { + return destination; + } + + @Override + public String command() { + return StringSubstitutor.replace( + "tp ${target} ${destination}", + Map.of( + "target", getTarget(), + "destination", getDestination() + ) + ); + } +} diff --git a/src/test/java/io/graversen/minecraft/rcon/commands/TeleportCommandTest.java b/src/test/java/io/graversen/minecraft/rcon/commands/TeleportCommandTest.java new file mode 100644 index 0000000..bb8fa2d --- /dev/null +++ b/src/test/java/io/graversen/minecraft/rcon/commands/TeleportCommandTest.java @@ -0,0 +1,18 @@ +package io.graversen.minecraft.rcon.commands; + +import io.graversen.minecraft.rcon.util.Position; +import io.graversen.minecraft.rcon.util.Target; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class TeleportCommandTest { + @Test + void teleportCommand_target() { + assertEquals("tp player1 player2", new TeleportCommand(Target.player("player1"), Target.player("player2")).command()); + } + + void teleportCommand_position() { + assertEquals("tp test 3 2 -1", new TeleportCommand(Target.player("test"), Position.simple(3, 2, -1)).command()); + } +} \ No newline at end of file