use of org.bukkit.GameMode in project Bukkit by Bukkit.
the class DefaultGameModeCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!testPermission(sender))
return true;
if (args.length == 0) {
sender.sendMessage("Usage: " + usageMessage);
return false;
}
String modeArg = args[0];
int value = -1;
try {
value = Integer.parseInt(modeArg);
} catch (NumberFormatException ex) {
}
GameMode mode = GameMode.getByValue(value);
if (mode == null) {
if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) {
mode = GameMode.CREATIVE;
} else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) {
mode = GameMode.ADVENTURE;
} else {
mode = GameMode.SURVIVAL;
}
}
Bukkit.getServer().setDefaultGameMode(mode);
Command.broadcastCommandMessage(sender, "Default game mode set to " + mode.toString().toLowerCase());
return true;
}
use of org.bukkit.GameMode in project Bukkit by Bukkit.
the class GameModeCommand method execute.
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender))
return true;
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
String modeArg = args[0];
String playerArg = sender.getName();
if (args.length == 2) {
playerArg = args[1];
}
Player player = Bukkit.getPlayerExact(playerArg);
if (player != null) {
int value = -1;
try {
value = Integer.parseInt(modeArg);
} catch (NumberFormatException ex) {
}
GameMode mode = GameMode.getByValue(value);
if (mode == null) {
if (modeArg.equalsIgnoreCase("creative") || modeArg.equalsIgnoreCase("c")) {
mode = GameMode.CREATIVE;
} else if (modeArg.equalsIgnoreCase("adventure") || modeArg.equalsIgnoreCase("a")) {
mode = GameMode.ADVENTURE;
} else {
mode = GameMode.SURVIVAL;
}
}
if (mode != player.getGameMode()) {
player.setGameMode(mode);
if (mode != player.getGameMode()) {
sender.sendMessage("Game mode change for " + player.getName() + " failed!");
} else {
if (player == sender) {
Command.broadcastCommandMessage(sender, "Set own game mode to " + mode.toString() + " mode");
} else {
Command.broadcastCommandMessage(sender, "Set " + player.getName() + "'s game mode to " + mode.toString() + " mode");
}
}
} else {
sender.sendMessage(player.getName() + " already has game mode " + mode.getValue());
}
} else {
sender.sendMessage("Can't find player " + playerArg);
}
return true;
}
use of org.bukkit.GameMode in project Glowstone by GlowstoneMC.
the class CommandTarget method getMatched.
/**
* Gets all the matched entities from the target.
*
* @param source the location from which the targets should be found
* @return the entities matching the query
*/
public Entity[] getMatched(Location source) {
if (selector == SelectorType.SENDER) {
if (sender instanceof Entity) {
return new Entity[] { (Entity) sender };
} else {
return new Entity[0];
}
}
List<EntityType> types = getTypes();
List<GameMode> gameModes = getGameModes();
Integer count = getCount();
int maxRadius = getMaxRange() * getMaxRange();
int minRadius = getMinRange() * getMinRange();
Integer minLevel = getMinLevel();
Integer maxLevel = getMaxLevel();
List<Entity> entities = new ArrayList<>();
if (count == null) {
if (selector == SelectorType.NEAREST_PLAYER || selector == SelectorType.RANDOM) {
count = 1;
} else {
count = source.getWorld().getEntities().size();
}
}
for (Entity entity : source.getWorld().getEntities()) {
if (entity.getLocation().distanceSquared(source) < minRadius) {
continue;
}
if (!(maxRadius == 0 || entity.getLocation().distanceSquared(source) < maxRadius)) {
continue;
}
if (!types.contains(entity.getType())) {
continue;
}
if (getX() != null && getX() != entity.getLocation().getBlockX()) {
continue;
}
if (getY() != null && getY() != entity.getLocation().getBlockY()) {
continue;
}
if (getZ() != null && getZ() != entity.getLocation().getBlockZ()) {
continue;
}
if (gameModes != null && entity.getType() != EntityType.PLAYER) {
continue;
}
if (gameModes != null && gameModes.contains(((Player) entity).getGameMode())) {
continue;
}
if (maxLevel != null && entity.getType() != EntityType.PLAYER) {
continue;
}
if (maxLevel != null && ((Player) entity).getLevel() > maxLevel) {
continue;
}
if (minLevel != null && entity.getType() != EntityType.PLAYER) {
continue;
}
if (minLevel != null && ((Player) entity).getLevel() < minLevel) {
continue;
}
// TODO: Add more checks
entities.add(entity);
}
Collections.sort(entities, new EntityDistanceComparator(count < 0, source));
if (count > entities.size()) {
count = entities.size();
}
List<Entity> matched = new ArrayList<>();
List<Integer> used = new ArrayList<>();
for (int i = 0; i < count; i++) {
if (selector == SelectorType.RANDOM) {
while (true) {
int random = ThreadLocalRandom.current().nextInt(entities.size());
if (!used.contains(random)) {
matched.add(entities.get(random));
used.add(random);
break;
}
}
} else {
matched.add(entities.get(i));
}
}
return matched.toArray(new Entity[matched.size()]);
}
use of org.bukkit.GameMode in project Glowstone by GlowstoneMC.
the class DefaultGameModeCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
if (!testPermission(sender, messages.getPermissionMessage())) {
return true;
}
if (args.length == 0) {
sendUsageMessage(sender, messages);
return false;
}
final String inputMode = args[0];
final ResourceBundle bundle = messages.getResourceBundle();
final GameMode gamemode = GameModeUtils.build(inputMode, messages.getLocale());
if (gamemode == null) {
new LocalizedStringImpl("defaultgamemode.unknown", bundle).sendInColor(ChatColor.RED, sender, inputMode);
return false;
}
ServerProvider.getServer().setDefaultGameMode(gamemode);
new LocalizedStringImpl("defaultgamemode.done", bundle).send(sender, ChatColor.GRAY + "" + ChatColor.ITALIC + GameModeUtils.prettyPrint(gamemode, messages.getLocale()));
return true;
}
use of org.bukkit.GameMode in project Glowstone by GlowstoneMC.
the class GlowAnimal method entityInteract.
@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
if (!super.entityInteract(player, message) && message.getAction() == InteractEntityMessage.Action.INTERACT.ordinal()) {
GameMode gameMode = player.getGameMode();
if (gameMode == GameMode.SPECTATOR) {
return false;
}
ItemStack item = player.getInventory().getItem(message.getHandSlot());
if (InventoryUtil.isEmpty(item)) {
return false;
}
boolean successfullyUsed = tryFeed(item.getType(), player);
if (successfullyUsed && GameMode.CREATIVE != gameMode) {
player.getInventory().consumeItem(message.getHand());
}
return successfullyUsed;
}
return false;
}
Aggregations