Search in sources :

Example 1 with VoxelGameLibException

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;
    });
}
Also used : Locale(com.voxelgameslib.voxelgameslib.lang.Locale) GameMode(com.voxelgameslib.voxelgameslib.game.GameMode) Player(org.bukkit.entity.Player) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) CommandSender(org.bukkit.command.CommandSender) Trackable(com.voxelgameslib.voxelgameslib.stats.Trackable) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext)

Example 2 with VoxelGameLibException

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);
    }
}
Also used : ItemBuilder(com.voxelgameslib.voxelgameslib.utils.ItemBuilder) InventoryMenuBuilder(org.inventivetalent.menubuilder.inventory.InventoryMenuBuilder) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) Skin(org.mineskin.data.Skin) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) MarkerDefinition(com.voxelgameslib.voxelgameslib.map.MarkerDefinition) Subcommand(co.aikar.commands.annotation.Subcommand) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 3 with VoxelGameLibException

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);
    }
}
Also used : ItemBuilder(com.voxelgameslib.voxelgameslib.utils.ItemBuilder) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) Skin(org.mineskin.data.Skin) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) Subcommand(co.aikar.commands.annotation.Subcommand) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 4 with VoxelGameLibException

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);
    }
}
Also used : VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.exception.GameStartException) DefaultGameData(com.voxelgameslib.voxelgameslib.game.DefaultGameData) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.exception.GameStartException)

Example 5 with VoxelGameLibException

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);
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) BufferedReader(java.io.BufferedReader) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) URL(java.net.URL) Nonnull(javax.annotation.Nonnull)

Aggregations

VoxelGameLibException (com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException)8 Nonnull (javax.annotation.Nonnull)3 CommandPermission (co.aikar.commands.annotation.CommandPermission)2 Subcommand (co.aikar.commands.annotation.Subcommand)2 ItemBuilder (com.voxelgameslib.voxelgameslib.utils.ItemBuilder)2 ItemStack (org.bukkit.inventory.ItemStack)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 Skin (org.mineskin.data.Skin)2 BukkitCommandExecutionContext (co.aikar.commands.BukkitCommandExecutionContext)1 InvalidCommandArgument (co.aikar.commands.InvalidCommandArgument)1 Syntax (co.aikar.commands.annotation.Syntax)1 Team (com.voxelgameslib.voxelgameslib.components.team.Team)1 VictoryCondition (com.voxelgameslib.voxelgameslib.condition.VictoryCondition)1 EmptyVictoryCondition (com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition)1 GameStartException (com.voxelgameslib.voxelgameslib.exception.GameStartException)1 DefaultGameData (com.voxelgameslib.voxelgameslib.game.DefaultGameData)1 GameMode (com.voxelgameslib.voxelgameslib.game.GameMode)1 Locale (com.voxelgameslib.voxelgameslib.lang.Locale)1 MarkerDefinition (com.voxelgameslib.voxelgameslib.map.MarkerDefinition)1 Trackable (com.voxelgameslib.voxelgameslib.stats.Trackable)1