Search in sources :

Example 1 with UserException

use of com.voxelgameslib.voxelgameslib.exception.UserException in project VoxelGamesLibv2 by VoxelGamesLib.

the class SignButtons method signInteract.

@EventHandler
public void signInteract(@Nonnull PlayerInteractEvent event) {
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null) {
        return;
    }
    // is block a sign?
    if (event.getClickedBlock().getState() instanceof Sign) {
        User user = userHandler.getUser(event.getPlayer().getUniqueId()).orElseThrow(() -> new UserException("Unknown user " + event.getPlayer().getDisplayName() + "(" + event.getPlayer().getUniqueId() + ")"));
        Sign sign = (Sign) event.getClickedBlock().getState();
        for (int i = 0; i < sign.getLines().length; i++) {
            String line = sign.getLines()[i];
            for (String key : getButtons().keySet()) {
                if (line.contains("[" + key + "]")) {
                    // TODO implement perm check
                    getButtons().get(key).execute(user, event.getClickedBlock());
                }
            }
        }
    }
}
Also used : User(com.voxelgameslib.voxelgameslib.user.User) Sign(org.bukkit.block.Sign) UserException(com.voxelgameslib.voxelgameslib.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Example 2 with UserException

use of com.voxelgameslib.voxelgameslib.exception.UserException in project VoxelGamesLibv2 by VoxelGamesLib.

the class SignListener method signBreakEvent.

@EventHandler
public void signBreakEvent(@Nonnull BlockBreakEvent event) {
    // is block a sign?
    if (event.getBlock().getState() instanceof Sign) {
        User user = userHandler.getUser(event.getPlayer().getUniqueId()).orElseThrow(() -> new UserException("Unknown user " + event.getPlayer().getDisplayName() + "(" + event.getPlayer().getUniqueId() + ")"));
        Sign sign = (Sign) event.getBlock().getState();
        for (int i = 0; i < sign.getLines().length; i++) {
            String line = sign.getLines()[i];
            for (String key : signHandler.getSignPlaceholders().getPlaceHolders().keySet()) {
                if (line.contains("%" + key + "%")) {
                    // has user permission for that?
                    if (user.hasPermission(placeHolderSignBreak)) {
                        Lang.msg(user, LangKey.SIGNS_BREAK_SUCCESS, key);
                        event.getBlock().setType(Material.AIR);
                        return;
                    } else {
                        event.setCancelled(true);
                        Lang.msg(user, LangKey.SIGNS_BREAK_NO_PERM, key, placeHolderSignBreak.getRole().getName());
                        return;
                    }
                }
            }
            for (String key : signHandler.getSignButtons().getButtons().keySet()) {
                if (line.contains("%" + key + "%")) {
                    // has user permission for that?
                    if (user.hasPermission(buttonSignBreak)) {
                        Lang.msg(user, LangKey.SIGNS_BREAK_SUCCESS, key);
                        event.getBlock().setType(Material.AIR);
                        return;
                    } else {
                        event.setCancelled(true);
                        Lang.msg(user, LangKey.SIGNS_BREAK_NO_PERM, key, buttonSignBreak.getRole().getName());
                        return;
                    }
                }
            }
        }
    }
}
Also used : User(com.voxelgameslib.voxelgameslib.user.User) Sign(org.bukkit.block.Sign) UserException(com.voxelgameslib.voxelgameslib.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Example 3 with UserException

use of com.voxelgameslib.voxelgameslib.exception.UserException in project VoxelGamesLibv2 by VoxelGamesLib.

the class GameListener method onLeave.

@EventHandler
public void onLeave(@Nonnull PlayerQuitEvent event) {
    User user = userHandler.getUser(event.getPlayer().getUniqueId()).orElseThrow(() -> new UserException("Unknown user " + event.getPlayer().getDisplayName() + "(" + event.getPlayer().getUniqueId() + ")"));
    gameHandler.getGames(event.getPlayer().getUniqueId(), true).forEach((game -> game.leave(user)));
}
Also used : PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) UserHandler(com.voxelgameslib.voxelgameslib.user.UserHandler) Logger(java.util.logging.Logger) GameLeaveEvent(com.voxelgameslib.voxelgameslib.event.events.game.GameLeaveEvent) Inject(javax.inject.Inject) EventHandler(org.bukkit.event.EventHandler) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) UserException(com.voxelgameslib.voxelgameslib.exception.UserException) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) User(com.voxelgameslib.voxelgameslib.user.User) Nonnull(javax.annotation.Nonnull) GameJoinEvent(com.voxelgameslib.voxelgameslib.event.events.game.GameJoinEvent) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) User(com.voxelgameslib.voxelgameslib.user.User) UserException(com.voxelgameslib.voxelgameslib.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Aggregations

UserException (com.voxelgameslib.voxelgameslib.exception.UserException)3 User (com.voxelgameslib.voxelgameslib.user.User)3 EventHandler (org.bukkit.event.EventHandler)3 Sign (org.bukkit.block.Sign)2 VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)1 GameJoinEvent (com.voxelgameslib.voxelgameslib.event.events.game.GameJoinEvent)1 GameLeaveEvent (com.voxelgameslib.voxelgameslib.event.events.game.GameLeaveEvent)1 UserHandler (com.voxelgameslib.voxelgameslib.user.UserHandler)1 Logger (java.util.logging.Logger)1 Nonnull (javax.annotation.Nonnull)1 Inject (javax.inject.Inject)1 Bukkit (org.bukkit.Bukkit)1 Listener (org.bukkit.event.Listener)1 PlayerJoinEvent (org.bukkit.event.player.PlayerJoinEvent)1 PlayerQuitEvent (org.bukkit.event.player.PlayerQuitEvent)1