Skip to content
Permalink
Browse files

Initial player target argument implementation

  • Loading branch information
DarkArc committed Jan 1, 2020
1 parent c91c46d commit ee712a5cbfb3a22ec1b837b46195716f7a2dbe97
@@ -4,6 +4,8 @@
import com.google.common.collect.ImmutableSet;
import com.sk89q.bukkit.util.CommandInfo;
import com.sk89q.bukkit.util.CommandRegistration;
import com.sk89q.commandbook.command.argument.MultiPlayerTargetConverter;
import com.sk89q.commandbook.command.argument.SinglePlayerTargetConverter;
import com.sk89q.commandbook.util.WorldEditAdapter;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.argument.Arguments;
@@ -80,7 +82,8 @@ private void registerAlwaysInjectedValues() {
}

private void registerArgumentConverters() {

SinglePlayerTargetConverter.register(commandManager);
MultiPlayerTargetConverter.register(commandManager);
}

private void registerCoreCommands() {
@@ -0,0 +1,24 @@
package com.sk89q.commandbook.command.argument;

import com.google.common.collect.ImmutableList;
import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.Iterator;

public class MultiPlayerTarget implements PlayerTarget {
private Collection<Player> players;

public MultiPlayerTarget(Player player) {
this.players = ImmutableList.of(player);
}

public MultiPlayerTarget(Collection<Player> player) {
this.players = player;
}

@Override
public Iterator<Player> iterator() {
return players.iterator();
}
}
@@ -0,0 +1,61 @@
package com.sk89q.commandbook.command.argument;

import com.sk89q.commandbook.util.InputUtil;
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.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 MultiPlayerTargetConverter extends PlayerTargetConverter<MultiPlayerTarget> {
public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(MultiPlayerTarget.class), new MultiPlayerTargetConverter());
}

@Override
public Component describeAcceptableArguments() {
return TextComponent.of("any number of online players, or a player matching pattern");
}

@Override
public ConversionResult<MultiPlayerTarget> 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();
try {
List<Player> results = InputUtil.PlayerParser.matchPlayers(sender, argument);
return SuccessfulConversion.fromSingle(new MultiPlayerTarget(results));
} catch (CommandException e) {
return FailedConversion.from(new IllegalArgumentException(e.getMessage()));
}
}

@Override
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);

addPlayerNameSuggestions(suggestions);

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

import org.bukkit.entity.Player;

public interface PlayerTarget extends Iterable<Player> {
}
@@ -0,0 +1,35 @@
package com.sk89q.commandbook.command.argument;

import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.minecraft.util.commands.CommandException;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.enginehub.piston.converter.ArgumentConverter;

import java.util.Collection;
import java.util.List;

public abstract class PlayerTargetConverter<T> implements ArgumentConverter<T> {
protected void trialAddSuggestion(List<String> suggestions, String trailText) {
try {
InputUtil.PlayerParser.matchPlayers(CommandBook.server().getConsoleSender(), trailText);
suggestions.add(trailText);
} catch (CommandException ignored) { }
}

protected void addPlayerNameSuggestions(List<String> suggestions) {
Collection<? extends Player> players = CommandBook.server().getOnlinePlayers();
boolean useDisplayNames = CommandBook.inst().lookupWithDisplayNames;

for (Player player : players) {
String playerName = player.getName();
String displayName = ChatColor.stripColor(player.getDisplayName());

suggestions.add(playerName);
if (useDisplayNames && !playerName.equals(displayName)) {
suggestions.add(displayName);
}
}
}
}
@@ -0,0 +1,23 @@
package com.sk89q.commandbook.command.argument;

import com.google.common.collect.ImmutableList;
import org.bukkit.entity.Player;

import java.util.Iterator;

public class SinglePlayerTarget implements PlayerTarget {
private Player player;

public SinglePlayerTarget(Player player) {
this.player = player;
}

public Player get() {
return player;
}

@Override
public Iterator<Player> iterator() {
return ImmutableList.of(player).iterator();
}
}
@@ -0,0 +1,54 @@
package com.sk89q.commandbook.command.argument;

import com.sk89q.commandbook.util.InputUtil;
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.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 SinglePlayerTargetConverter extends PlayerTargetConverter<SinglePlayerTarget> {
public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(SinglePlayerTarget.class), new SinglePlayerTargetConverter());
}

@Override
public Component describeAcceptableArguments() {
return TextComponent.of("any single online player");
}

@Override
public ConversionResult<SinglePlayerTarget> 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();
try {
Player result = InputUtil.PlayerParser.matchSinglePlayer(sender, argument);
return SuccessfulConversion.fromSingle(new SinglePlayerTarget(result));
} catch (CommandException e) {
return FailedConversion.from(new IllegalArgumentException(e.getMessage()));
}
}

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

addPlayerNameSuggestions(suggestions);

return suggestions;
}
}
@@ -223,9 +223,9 @@ public static int matchMCWorldTime(String timeStr) throws CommandException {
List<Player> list = new ArrayList<Player>();

for (Player player : players) {
if (player.getName().toLowerCase().startsWith(filter)
if (player.getName().toLowerCase().equals(filter)
|| (useDisplayNames
&& ChatColor.stripColor(player.getDisplayName().toLowerCase()).startsWith(filter))) {
&& ChatColor.stripColor(player.getDisplayName().toLowerCase()).equals(filter))) {
list.add(player);
}
}

0 comments on commit ee712a5

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