Skip to content

Commit

Permalink
✨ BasePositionalCommand, SummonCommand
Browse files Browse the repository at this point in the history
'ExecuteAtCommandBuilder' requires command of type BasePositionalCommand
  • Loading branch information
MrGraversen committed Jan 19, 2020
1 parent 444a5e3 commit 558a432
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/graversen/minecraft/rcon/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public final class Defaults {
public static final Duration RCON_TIMEOUT = Duration.ofSeconds(1);
public static final Duration CONNECTION_WATCHER_INTERVAL = Duration.ofSeconds(1);
public static final int RCON_PORT = 25575;
public static final int WORLD_BOUND_MAX = 30_000_000;
public static final int WORLD_BOUND_MIN = -30_000_000;

private Defaults() {

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/io/graversen/minecraft/rcon/MinecraftRcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public MinecraftRcon(IMinecraftClient rconClient) {
this.rconClient = Objects.requireNonNull(rconClient);
}

@Override
public Future<RconResponse> sendAsync(ICommand command) {
return rconClient.sendRaw(commandToString(command));
}

@Override
public void sendAsync(ICommand... commands) {
Arrays.stream(commands).forEach(this::sendAsync);
}

@Override
public RconResponse sendSync(ICommand command) {
final var responseFuture = sendAsync(command);
Expand All @@ -26,19 +36,13 @@ public RconResponse sendSync(ICommand command) {
}
}

@Override
public Future<RconResponse> sendAsync(ICommand command) {
return rconClient.sendRaw(command.command());
}

@Override
public void sendAsync(ICommand... commands) {
Arrays.stream(commands).forEach(this::sendAsync);
}

@Override
public <T> T query(ICommand command, IRconResponseMapper<T> rconResponseMapper) {
final var rconResponse = sendSync(command);
return rconResponseMapper.safeApply(rconResponse);
}

private String commandToString(ICommand command) {
return command.command().trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.graversen.minecraft.rcon.commands;

import io.graversen.minecraft.rcon.commands.base.BasePositionalCommand;
import io.graversen.minecraft.rcon.util.Position;
import org.apache.commons.text.StringSubstitutor;

import java.util.Map;
import java.util.Objects;

public class SummonCommand extends BasePositionalCommand {
private final String entityName;
private final String nbt;

public SummonCommand(Position position, String entityName, String nbt) {
super(position);
this.entityName = Objects.requireNonNull(entityName);
this.nbt = Objects.requireNonNullElse(nbt, "");;
}

public String getEntityName() {
return entityName;
}

public String getNbt() {
return nbt;
}

@Override
public String command() {
return StringSubstitutor.replace(
"summon ${entityName} ${position} ${nbt}",
Map.of(
"entityName", getEntityName(),
"position", getPosition().toString(),
"nbt", getNbt()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.graversen.minecraft.rcon.commands.base;

import io.graversen.minecraft.rcon.util.Position;

import java.util.Objects;

public abstract class BasePositionalCommand implements ICommand {
private final Position position;

public BasePositionalCommand(Position position) {
this.position = Objects.requireNonNull(position);
}

public Position getPosition() {
return position;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.graversen.minecraft.rcon.commands.execute;

import io.graversen.minecraft.rcon.commands.base.ICommand;
import io.graversen.minecraft.rcon.commands.base.INonTargetedCommand;
import io.graversen.minecraft.rcon.commands.base.BasePositionalCommand;
import io.graversen.minecraft.rcon.util.Selectors;
import org.apache.commons.text.StringSubstitutor;

Expand All @@ -21,7 +20,6 @@ public static ExecuteAtCommandBuilder executeAt(Selectors selector) {
}

public static class ExecuteAtCommandBuilder {
private static final String EXECUTE_AT_COMMAND_TEMPLATE = "execute at ${target} run ${command}";
private final String target;

ExecuteAtCommandBuilder(String target) {
Expand All @@ -32,9 +30,9 @@ public String getTarget() {
return target;
}

public ExecuteCommand run(INonTargetedCommand command) {
public ExecuteCommand run(BasePositionalCommand command) {
final String compiledCommand = StringSubstitutor.replace(
EXECUTE_AT_COMMAND_TEMPLATE,
"execute at ${target} run ${command}",
Map.of(
"target", getTarget(),
"command", command.command()
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/graversen/minecraft/rcon/util/Coordinate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.graversen.minecraft.rcon.util;

import io.graversen.minecraft.rcon.Defaults;

import java.util.Objects;

public class Coordinate {
private final String coordinate;

private Coordinate(String coordinate) {
this.coordinate = Objects.requireNonNull(coordinate);
}

public String coordinate() {
return coordinate;
}

public static Coordinate simple(long coordinate) {
validate(coordinate);
return new Coordinate(String.valueOf(coordinate));
}

public static Coordinate simple(double coordinate) {
validate(coordinate);
return new Coordinate(String.valueOf(coordinate));
}

public static Coordinate relative(long coordinate) {
validate(coordinate);
return new Coordinate("~" + coordinate);
}

public static Coordinate relative(double coordinate) {
validate(coordinate);
return new Coordinate("~" + coordinate);
}

private static void validate(double coordinate) {
if (coordinate >= Defaults.WORLD_BOUND_MAX || coordinate <= Defaults.WORLD_BOUND_MIN) {
throw new IllegalArgumentException("Coordinate is outside of world bounds");
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/io/graversen/minecraft/rcon/util/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
import java.util.Map;

public class Position {
private final long x;
private final long y;
private final long z;
private final Coordinate x;
private final Coordinate y;
private final Coordinate z;

public Position(long x, long y, long z) {
public Position(Coordinate x, Coordinate y, Coordinate z) {
this.x = x;
this.y = y;
this.z = z;
}

public long getX() {
public Coordinate getX() {
return x;
}

public long getY() {
public Coordinate getY() {
return y;
}

public long getZ() {
public Coordinate getZ() {
return z;
}

Expand All @@ -32,9 +32,9 @@ public String toString() {
return StringSubstitutor.replace(
"${x} ${y} ${z}",
Map.of(
"x", x,
"y", y,
"z", z
"x", getX().coordinate(),
"y", getY().coordinate(),
"z", getZ().coordinate()
)
);
}
Expand Down

0 comments on commit 558a432

Please sign in to comment.