use of com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.
the class VoxelGamesLib method registerCommandContexts.
private void registerCommandContexts() {
CommandContexts<BukkitCommandExecutionContext> con = commandManager.getCommandContexts();
con.registerIssuerAwareContext(User.class, c -> {
boolean isOptional = c.hasAnnotation(Optional.class);
CommandSender sender = c.getSender();
boolean isPlayerSender = sender instanceof Player;
if (!c.hasFlag("other")) {
Player player = isPlayerSender ? (Player) sender : null;
if (player == null) {
if (!isOptional) {
throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
} else {
return null;
}
}
return userHandler.getUser(player.getUniqueId()).orElseThrow(() -> new VoxelGameLibException("Unknown user " + player.getDisplayName()));
} else {
String arg = c.popFirstArg();
if (arg == null) {
if (isOptional) {
if (c.hasFlag("defaultself")) {
if (isPlayerSender) {
return userHandler.getUser(((Player) sender).getUniqueId()).orElseThrow(() -> new VoxelGameLibException("Unknown user " + ((Player) sender).getDisplayName()));
} else {
throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
}
}
}
return null;
} else {
return userHandler.getUser(arg).orElseThrow(() -> new VoxelGameLibException("Unknown user " + arg));
}
}
});
con.registerContext(int.class, c -> Integer.parseInt(c.popFirstArg()));
con.registerContext(GameMode.class, c -> {
GameMode mode = gameHandler.getGameModes().stream().filter(gameMode -> gameMode.getName().equalsIgnoreCase(c.getFirstArg())).findAny().orElseThrow(() -> new InvalidCommandArgument("Unknown gamemode " + c.getFirstArg()));
// pop later so that we can get a nice error message
c.popLastArg();
return mode;
});
con.registerContext(Locale.class, c -> {
Locale locale = Locale.fromName(c.getFirstArg()).orElse(Locale.fromTag(c.getFirstArg()).orElseThrow(() -> new InvalidCommandArgument("Unknown locale " + c.getFirstArg())));
// pop later so that we can get a nice error message
c.popFirstArg();
return locale;
});
con.registerContext(Role.class, c -> Role.fromName(c.popFirstArg()));
con.registerContext(UUID.class, c -> UUID.fromString(c.popFirstArg()));
con.registerContext(Trackable.class, c -> {
Trackable s = StatsHandler.fromName(c.getFirstArg()).orElseThrow(() -> new InvalidCommandArgument("Unknown stats type " + c.getFirstArg()));
// pop later so that we can get a nice error message
c.popFirstArg();
return s;
});
}
use of com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.
the class EditMode method gui.
@Subcommand("gui")
@CommandPermission("%admin")
public void gui(@Nonnull User sender) {
if (editMode.contains(sender.getUuid())) {
// TODO implement paginated invs
InventoryMenuBuilder builder = new InventoryMenuBuilder().withSize(9).withTitle(Lang.legacy(LangKey.INV_MARKER));
for (int i = 0; i < mapHandler.getMarkerDefinitions().size(); i++) {
MarkerDefinition markerDefinition = mapHandler.getMarkerDefinitions().get(i);
ItemStack is = new ItemBuilder(Material.SKULL_ITEM).durability(3).name(markerDefinition.getPrefix()).meta((itemMeta -> {
char prefix = markerDefinition.getPrefix().toUpperCase().charAt(0);
Skin skin = textureHandler.getSkin(prefix + "").orElseThrow(() -> new VoxelGameLibException("Unknown skull " + prefix));
((SkullMeta) itemMeta).setPlayerProfile(textureHandler.getPlayerProfile(skin));
((SkullMeta) itemMeta).setOwner(markerDefinition.getPrefix());
})).build();
builder.withItem(i, is, (player, clickType, itemStack) -> sender.getPlayer().performCommand("editmode skull " + itemStack.getItemMeta().getDisplayName()), ClickType.LEFT);
}
builder.show(sender.getPlayer());
} else {
Lang.msg(sender, LangKey.EDITMODE_NOT_ENABLED);
}
}
use of com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.
the class EditMode method skull.
@Subcommand("skull")
@CommandPermission("%admin")
@Syntax("<name> - the name of the skull")
public void skull(@Nonnull User sender, @Nonnull String name) {
if (editMode.contains(sender.getUuid())) {
ItemStack skull = new ItemBuilder(Material.SKULL_ITEM).durability(3).name(name).meta((itemMeta -> {
char prefix = name.toUpperCase().charAt(0);
Skin skin = textureHandler.getSkin(prefix + "").orElseThrow(() -> new VoxelGameLibException("Unknown skull " + prefix));
((SkullMeta) itemMeta).setPlayerProfile(textureHandler.getPlayerProfile(skin));
((SkullMeta) itemMeta).setOwner(name);
})).build();
sender.getPlayer().getInventory().setItemInMainHand(skull);
} else {
Lang.msg(sender, LangKey.EDITMODE_NOT_ENABLED);
}
}
use of com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.
the class MapFeature method enable.
@Override
public void enable() {
// we already set the map externally, no need to do anything of the following, just set the world
if (map != null) {
world = Bukkit.getWorld(map.getLoadedName(getPhase().getGame().getUuid()));
return;
}
DefaultGameData gameData = getPhase().getGame().getGameData(DefaultGameData.class).orElse(new DefaultGameData());
if ((type == Type.LOBBY && gameData.lobbyMap == null) || (type == Type.VOTEWINNER && gameData.voteWinner == null)) {
throw new GameStartException(getPhase().getGame().getGameMode(), "No map data was stored!");
}
String mapName;
if (type == Type.LOBBY) {
mapName = gameData.lobbyMap.getName();
} else if (type == Type.VOTEWINNER) {
mapName = gameData.voteWinner.getName();
} else {
throw new VoxelGameLibException("Unknown maptype");
}
try {
map = worldHandler.loadMap(mapName);
if (!map.isLoaded(getPhase().getGame().getUuid())) {
world = worldHandler.loadWorld(map, getPhase().getGame().getUuid(), true);
} else {
world = Bukkit.getWorld(map.getLoadedName(getPhase().getGame().getUuid()));
}
} catch (Exception ex) {
throw new GameStartException(getPhase().getGame().getGameMode(), ex);
}
}
use of com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException in project VoxelGamesLibv2 by VoxelGamesLib.
the class MojangUtil method getDisplayName.
// TODO we need to do some caching to not break mojang api rate limits
/**
* Tries to fetch the current display name for the user
*
* @param id the id of the user to check
* @return the current display name of that user
* @throws IOException if something goes wrong
* @throws VoxelGameLibException if the user has no display name
*/
@Nonnull
public static String getDisplayName(@Nonnull UUID id) throws IOException, VoxelGameLibException {
URL url = new URL(NAME_HISTORY_URL.replace("%1", id.toString().replace("-", "")));
System.out.println(url.toString());
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(url.openStream())));
if (scanner.hasNext()) {
String json = scanner.nextLine();
try {
JSONArray jsonArray = (JSONArray) new JSONParser().parse(json);
if (json.length() > 0) {
return (String) ((JSONObject) jsonArray.get(0)).get("name");
}
} catch (ParseException ignore) {
}
}
throw new VoxelGameLibException("User has no name! " + id);
}
Aggregations