Skip to content
Permalink
Browse files

Initial port of the teleport component to Piston

  • Loading branch information
DarkArc committed Jan 2, 2020
1 parent 3a855e1 commit 2f1bcf356f99744f7963112a73560b4a6d313937
@@ -21,4 +21,8 @@ public MultiPlayerTarget(Collection<Player> player) {
public Iterator<Player> iterator() {
return players.iterator();
}

public int size() {
return players.size();
}
}
@@ -1,12 +1,14 @@
package com.sk89q.commandbook.command.argument;

import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.commandbook.util.suggestion.SuggestionHelper;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.FailedConversion;
import org.enginehub.piston.converter.SuccessfulConversion;
@@ -17,7 +19,7 @@
import java.util.List;
import java.util.Optional;

public class MultiPlayerTargetConverter extends PlayerTargetConverter<MultiPlayerTarget> {
public class MultiPlayerTargetConverter implements ArgumentConverter<MultiPlayerTarget> {
public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(MultiPlayerTarget.class), new MultiPlayerTargetConverter());
}
@@ -47,14 +49,13 @@ public Component describeAcceptableArguments() {
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

suggestions.add(input);
trialAddSuggestion(suggestions, "*");
trialAddSuggestion(suggestions, "#world");
trialAddSuggestion(suggestions, "#near");
trialAddSuggestion(suggestions, input + "*");
trialAddSuggestion(suggestions, "*" + input);
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "*");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#world");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "#near");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, input + "*");
SuggestionHelper.trialAddPlayerSuggestion(suggestions, "*" + input);

addPlayerNameSuggestions(suggestions);
SuggestionHelper.addPlayerNameSuggestions(suggestions);

return suggestions;
}
@@ -1,12 +1,14 @@
package com.sk89q.commandbook.command.argument;

import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.commandbook.util.suggestion.SuggestionHelper;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.FailedConversion;
import org.enginehub.piston.converter.SuccessfulConversion;
@@ -17,7 +19,7 @@
import java.util.List;
import java.util.Optional;

public class SinglePlayerTargetConverter extends PlayerTargetConverter<SinglePlayerTarget> {
public class SinglePlayerTargetConverter implements ArgumentConverter<SinglePlayerTarget> {
public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(SinglePlayerTarget.class), new SinglePlayerTargetConverter());
}
@@ -47,7 +49,7 @@ public Component describeAcceptableArguments() {
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

addPlayerNameSuggestions(suggestions);
SuggestionHelper.addPlayerNameSuggestions(suggestions);

return suggestions;
}
@@ -0,0 +1,12 @@
package com.sk89q.commandbook.component.locations;

import java.util.HashSet;
import java.util.Set;

public class LocationDirectiveManager {
private Set<String> directives = new HashSet<>();

public Set<String> getSupportedDirectives() {
return directives;
}
}
@@ -0,0 +1,35 @@
package com.sk89q.commandbook.component.locations;

import org.bukkit.Location;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class LocationTarget {
private Location location;
private boolean[] relative;

public LocationTarget(Location location, boolean[] relative) {
checkNotNull(location);
checkArgument(relative.length == 3);

this.location = location;
this.relative = relative;
}

public Location get() {
return location;
}

public boolean isRelativeX() {
return relative[0];
}

public boolean isRelativeY() {
return relative[1];
}

public boolean isRelativeZ() {
return relative[2];
}
}
@@ -0,0 +1,117 @@
package com.sk89q.commandbook.component.locations;

import com.sk89q.commandbook.ComponentCommandRegistrar;
import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.commandbook.util.LocationUtil;
import com.sk89q.commandbook.util.suggestion.SuggestionHelper;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.FailedConversion;
import org.enginehub.piston.converter.SuccessfulConversion;
import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class LocationTargetConverter implements ArgumentConverter<LocationTarget> {
public static void register(ComponentCommandRegistrar registrar) {
registrar.registerConverter(Key.of(LocationTarget.class), new LocationTargetConverter());
}

public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(LocationTarget.class), new LocationTargetConverter());
}

@Override
public Component describeAcceptableArguments() {
return TextComponent.of("any player, named destination, or coordinate");
}

private ConversionResult<LocationTarget> getInvalidFormatResult() {
return FailedConversion.from(new IllegalArgumentException("Invalid location format."));
}

private String[] normalizedSplit(String input) {
input = input.replaceAll("(\\s|,)+", " ");
return input.split(" ");
}

@Override
public ConversionResult<LocationTarget> convert(String argument, InjectedValueAccess context) {
Optional<CommandSender> optSender = context.injectedValue(Key.of(CommandSender.class));
if (!optSender.isPresent()){
return FailedConversion.from(new IllegalStateException("No command sender present"));
}

CommandSender sender = optSender.get();


Location loc;
boolean[] relative = new boolean[] { false, false, false };
boolean precise = false;

String[] parts = normalizedSplit(argument);
if (parts.length == 1) {
try {
loc = InputUtil.LocationParser.matchLocation(sender, parts[0]); // matches both #7 and #8
} catch (CommandException e) {
return FailedConversion.from(new IllegalArgumentException(e.getMessage()));
}
} else if (parts.length == 3 && sender.hasPermission("commandbook.locations.coords")) {
String xArg = parts[0];
String yArg = parts[1];
String zArg = parts[2];

if (xArg.startsWith("~")) relative[0] = true;
if (yArg.startsWith("~")) relative[1] = true;
if (zArg.startsWith("~")) relative[2] = true;

if (relative[0] || relative[1] || relative[2]) {
if (!sender.hasPermission("commandbook.locations.coords.relative")) {
return getInvalidFormatResult();
}
}

double x = InputUtil.LocationParser.parseCoordinateValue(xArg);
double y = InputUtil.LocationParser.parseCoordinateValue(yArg);
double z = InputUtil.LocationParser.parseCoordinateValue(zArg);

if (x != (int) x || y != (int) y || z != (int) z) {
precise = true;
}

World world = LocationUtil.extractWorld(sender);

loc = new Location(world, x, y, z);
} else {
return getInvalidFormatResult();
}

if (!precise) {
if (loc.getX() == loc.getBlockX()) loc.add(0.5, 0, 0);
if (loc.getZ() == loc.getBlockZ()) loc.add(0, 0, 0.5);
}

return SuccessfulConversion.fromSingle(new LocationTarget(loc, relative));
}

@Override
public List<String> getSuggestions(String input) {
List<String> suggestions = new ArrayList<>();

if (normalizedSplit(input).length == 1) {
SuggestionHelper.addPlayerNameSuggestions(suggestions);
}

return suggestions;
}
}

0 comments on commit 2f1bcf3

Please sign in to comment.
You can’t perform that action at this time.