Search in sources :

Example 1 with UserException

use of com.voxelgameslib.voxelgameslib.api.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.components.user.User) Sign(org.bukkit.block.Sign) UserException(com.voxelgameslib.voxelgameslib.api.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Example 2 with UserException

use of com.voxelgameslib.voxelgameslib.api.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() + ")"));
    user.setLeaving(true);
    gameHandler.getGames(event.getPlayer().getUniqueId(), true).forEach((game -> game.leave(user, false)));
}
Also used : PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) GamePostLeaveEvent(com.voxelgameslib.voxelgameslib.api.event.events.game.GamePostLeaveEvent) User(com.voxelgameslib.voxelgameslib.components.user.User) SpawnFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature) UserException(com.voxelgameslib.voxelgameslib.api.exception.UserException) Logger(java.util.logging.Logger) Inject(javax.inject.Inject) EventHandler(org.bukkit.event.EventHandler) UserHandler(com.voxelgameslib.voxelgameslib.components.user.UserHandler) GameJoinEvent(com.voxelgameslib.voxelgameslib.api.event.events.game.GameJoinEvent) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) Nonnull(javax.annotation.Nonnull) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) User(com.voxelgameslib.voxelgameslib.components.user.User) UserException(com.voxelgameslib.voxelgameslib.api.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Example 3 with UserException

use of com.voxelgameslib.voxelgameslib.api.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.components.user.User) Sign(org.bukkit.block.Sign) UserException(com.voxelgameslib.voxelgameslib.api.exception.UserException) EventHandler(org.bukkit.event.EventHandler)

Aggregations

UserException (com.voxelgameslib.voxelgameslib.api.exception.UserException)3 User (com.voxelgameslib.voxelgameslib.components.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.api.event.events.game.GameJoinEvent)1 GamePostLeaveEvent (com.voxelgameslib.voxelgameslib.api.event.events.game.GamePostLeaveEvent)1 SpawnFeature (com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature)1 UserHandler (com.voxelgameslib.voxelgameslib.components.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