use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.
the class TeleportCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 4 || args.length == 5) {
sendUsageMessage(sender, commandMessages);
return false;
}
if (!CommandUtils.isPhysical(sender)) {
commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL).sendInColor(ChatColor.RED, sender);
return false;
}
Location location = CommandUtils.getLocation(sender);
Entity[] targets;
if (args[0].startsWith("@")) {
targets = new CommandTarget(sender, args[0]).getMatched(location);
} else {
Player targetPlayer = Bukkit.getPlayerExact(args[0]);
if (targetPlayer != null) {
location = targetPlayer.getLocation();
}
targets = targetPlayer == null ? NO_ENTITY : new Entity[] { targetPlayer };
}
if (targets.length == 0) {
commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, args[0]);
} else {
for (Entity target : targets) {
String x = args[1];
String y = args[2];
String z = args[3];
Location targetLocation = CommandUtils.getLocation(location, x, y, z);
if (args.length > 4) {
String yaw = args[4];
String pitch = args[5];
targetLocation = CommandUtils.getRotation(target.getLocation(), yaw, pitch);
} else {
targetLocation.setYaw(target.getLocation().getYaw());
targetLocation.setPitch(target.getLocation().getPitch());
}
target.teleport(targetLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
new LocalizedStringImpl("teleport.done", commandMessages.getResourceBundle()).send(sender, target.getName(), targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
}
}
return true;
}
use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.
the class SpawnPointCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length != 0 && args.length != 1 && args.length < 4) {
sendUsageMessage(sender, commandMessages);
return false;
}
final String playerPattern = args.length >= 1 ? args[0] : null;
List<Player> targets;
Location spawnLocation;
// Manage player(s)
if (playerPattern == null) {
// Default player, set to current one (if a player)
if (sender instanceof Player) {
targets = ImmutableList.of((Player) sender);
} else {
new LocalizedStringImpl("spawnpoint.no-player", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender);
return false;
}
} else if (playerPattern.startsWith("@") && playerPattern.length() > 1 && CommandUtils.isPhysical(sender)) {
// Manage selectors
final Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
final Entity[] entities = new CommandTarget(sender, args[0]).getMatched(location);
targets = new ArrayList<>(entities.length);
for (final Entity entity : entities) {
if (entity instanceof Player) {
targets.add((Player) entity);
}
}
} else {
// One player given (with or without coordinates)
final Player player = Bukkit.getPlayerExact(playerPattern);
if (player == null) {
commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, playerPattern);
return false;
} else {
targets = Collections.singletonList(player);
}
}
// Manage coordinates
if (args.length == 4) {
// Coordinates are given
final Location currentLocation;
final World world = CommandUtils.getWorld(sender);
// If we are using relative coordinates, we need to get the sender location
if (args[1].startsWith("~") || args[2].startsWith("~") || args[3].startsWith("~")) {
if (!CommandUtils.isPhysical(sender)) {
commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
return false;
} else {
currentLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
}
} else {
// Otherwise, the current location can be set to 0/0/0 (since it's absolute)
currentLocation = new Location(world, 0, 0, 0);
}
spawnLocation = CommandUtils.getLocation(currentLocation, args[1], args[2], args[3]);
if (spawnLocation.getY() < 0) {
commandMessages.getGeneric(GenericMessage.TOO_LOW).sendInColor(ChatColor.RED, sender);
return false;
} else if (spawnLocation.getBlockY() > world.getMaxHeight()) {
commandMessages.getGeneric(GenericMessage.TOO_HIGH).sendInColor(ChatColor.RED, sender, world.getMaxHeight());
return false;
}
} else {
// Use the sender coordinates
if (CommandUtils.isPhysical(sender)) {
spawnLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
} else {
commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
return false;
}
}
// Update spawn location
LocalizedStringImpl doneForOne = new LocalizedStringImpl("spawnpoint.done", commandMessages.getResourceBundle());
int newX = spawnLocation.getBlockX();
int newY = spawnLocation.getBlockY();
int newZ = spawnLocation.getBlockZ();
for (final Player target : targets) {
target.setBedSpawnLocation(spawnLocation, true);
doneForOne.send(sender, target.getName(), newX, newY, newZ);
}
return true;
}
use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.
the class TestForCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 1) {
sendUsageMessage(sender, commandMessages);
return false;
}
String name = args[0];
Entity[] entities;
if (name.startsWith("@")) {
CommandTarget target = new CommandTarget(sender, name);
entities = target.getMatched(CommandUtils.getLocation(sender));
if (entities.length == 0) {
commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
return false;
}
} else {
// TODO: Select custom-named non-player entities?
GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
if (player == null) {
commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return false;
} else {
entities = new Entity[] { player };
}
}
LocalizedStringImpl foundMessage = new LocalizedStringImpl("testfor.found", commandMessages.getResourceBundle());
if (args.length >= 2) {
String data = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
CompoundTag tag;
try {
tag = Mojangson.parseCompound(data);
} catch (MojangsonParseException e) {
commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
return false;
}
LocalizedStringImpl wrongDataMessage = new LocalizedStringImpl("testfor.wrong-data", commandMessages.getResourceBundle());
for (Entity entity : entities) {
if (entity instanceof GlowEntity) {
CompoundTag entityTag = new CompoundTag();
EntityStorage.save((GlowEntity) entity, entityTag);
if (tag.matches(entityTag)) {
foundMessage.send(sender, CommandUtils.getName(entity));
} else {
wrongDataMessage.sendInColor(ChatColor.RED, sender, CommandUtils.getName(entity));
}
}
}
} else {
for (Entity entity : entities) {
foundMessage.send(sender, CommandUtils.getName(entity));
}
}
// matching entities.
return true;
}
use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.
the class ClearCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
if (!testPermission(sender, messages.getPermissionMessage())) {
return true;
}
final ResourceBundle resourceBundle = messages.getResourceBundle();
if (args.length == 0) {
if ((sender instanceof Player)) {
Player player = (Player) sender;
return clearAll(sender, player, null, -1, -1, resourceBundle);
} else {
sendUsageMessage(sender, messages);
return false;
}
}
String name = args[0];
List<Player> players = new ArrayList<>();
if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
CommandTarget target = new CommandTarget(sender, name);
Entity[] matched = target.getMatched(location);
for (Entity entity : matched) {
if (entity instanceof Player) {
players.add((Player) entity);
}
}
} else {
Player player = Bukkit.getPlayerExact(name);
if (player == null) {
messages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return false;
} else {
players.add(player);
}
}
if (players.size() == 0) {
messages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return false;
}
if (args.length >= 2) {
String itemName = CommandUtils.toNamespaced(args[1]);
Material type = ItemIds.getItem(itemName);
if (type == null) {
new LocalizedStringImpl("clear.no-such-item", resourceBundle).sendInColor(ChatColor.RED, sender, itemName);
return false;
}
if (args.length >= 3) {
String dataString = args[2];
int data;
try {
data = Integer.valueOf(dataString);
} catch (NumberFormatException ex) {
messages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, dataString);
return false;
}
if (data < -1) {
new LocalizedStringImpl("clear.negative", resourceBundle).sendInColor(ChatColor.RED, sender, data);
return false;
}
if (args.length >= 4) {
String amountString = args[3];
int amount;
try {
amount = Integer.valueOf(amountString);
} catch (NumberFormatException ex) {
messages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, amountString);
return false;
}
if (amount < -1) {
new LocalizedStringImpl("clear.negative", resourceBundle).sendInColor(ChatColor.RED, sender, amount);
return false;
}
if (args.length >= 5) {
new LocalizedStringImpl("clear.tag-unsupported", resourceBundle).sendInColor(ChatColor.RED, sender);
return false;
} else {
boolean success = true;
for (Player player : players) {
if (!clearAll(sender, player, type, data, amount, resourceBundle)) {
success = false;
}
}
return success;
}
} else {
boolean success = true;
for (Player player : players) {
if (!clearAll(sender, player, type, data, -1, resourceBundle)) {
success = false;
}
}
return success;
}
} else {
boolean success = true;
for (Player player : players) {
if (!clearAll(sender, player, type, -1, -1, resourceBundle)) {
success = false;
}
}
return success;
}
} else {
boolean success = true;
for (Player player : players) {
if (!clearAll(sender, player, null, -1, -1, resourceBundle)) {
success = false;
}
}
return success;
}
}
use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.
the class EnchantCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 3) {
sendUsageMessage(sender, commandMessages);
return false;
}
String name = args[0];
Stream<GlowPlayer> players;
if (name.startsWith("@")) {
CommandTarget target = new CommandTarget(sender, name);
players = Arrays.stream(target.getMatched(CommandUtils.getLocation(sender))).filter(GlowPlayer.class::isInstance).map(GlowPlayer.class::cast);
} else {
GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
if (player == null) {
commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return false;
} else {
players = Collections.singletonList(player).stream();
}
}
Enchantment enchantment = GlowEnchantment.parseEnchantment(args[1]);
if (enchantment == null) {
new LocalizedStringImpl("enchant.unknown", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[1]);
return false;
}
int level;
try {
level = Integer.parseInt(args[2]);
} catch (NumberFormatException exc) {
commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[2]);
return false;
}
LocalizedStringImpl successMessage = new LocalizedStringImpl("enchant.done", commandMessages.getResourceBundle());
players.filter(player -> player.getItemInHand() != null).filter(player -> player.getItemInHand().getData().getItemType() != Material.AIR).filter(player -> enchantment.canEnchantItem(player.getItemInHand())).forEach(player -> {
ItemStack itemInHand = player.getItemInHand();
itemInHand.addUnsafeEnchantment(enchantment, level);
player.setItemInHand(itemInHand);
successMessage.send(sender);
});
return true;
}
Aggregations