Search in sources :

Example 6 with User

use of com.voxelgameslib.voxelgameslib.user.User in project VoxelGamesLibv2 by VoxelGamesLib.

the class EloHandler method handleGameEnd.

/**
 * Handles the elo calculation for generic games
 *
 * @param game  the game that ended
 * @param users the users which played this game, in order, first = winner
 */
public void handleGameEnd(@Nonnull Game game, @Nonnull User... users) {
    List<ITeam> teams = new ArrayList<>();
    for (User user : users) {
        teams.add(new jskills.Team(user, user.getRating(game.getGameMode())));
    }
    Map<IPlayer, Rating> newRatings = calculator.calculateNewRatings(game.getGameMode(), teams, IntStream.of(users.length).toArray());
    update(game, newRatings);
}
Also used : IPlayer(jskills.IPlayer) User(com.voxelgameslib.voxelgameslib.user.User) Rating(jskills.Rating) ArrayList(java.util.ArrayList) ITeam(jskills.ITeam)

Example 7 with User

use of com.voxelgameslib.voxelgameslib.user.User 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 8 with User

use of com.voxelgameslib.voxelgameslib.user.User 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 9 with User

use of com.voxelgameslib.voxelgameslib.user.User in project VoxelGamesLibv2 by VoxelGamesLib.

the class SignPlaceholders method init.

public void init() {
    registerPlaceholders();
    Bukkit.getWorlds().stream().flatMap(w -> Arrays.stream(w.getLoadedChunks())).flatMap(s -> Arrays.stream(s.getTileEntities())).filter(s -> s instanceof Sign).map(s -> (Sign) s).forEach(s -> lastSeenSigns.put(s.getLocation(), s));
    // modify update packets
    protocolManager.addPacketListener(new PacketAdapter(voxelGamesLib, PacketType.Play.Server.TILE_ENTITY_DATA) {

        @Override
        public void onPacketSending(@Nonnull PacketEvent event) {
            int action = event.getPacket().getIntegers().read(0);
            // 9 = set sign text action
            if (action != 9) {
                return;
            }
            NbtCompound data = (NbtCompound) event.getPacket().getNbtModifier().read(0);
            // read data
            Component[] lines = new Component[4];
            String[] rawLines = new String[4];
            for (int i = 0; i < lines.length; i++) {
                lines[i] = ComponentSerializers.JSON.deserialize(data.getString("Text" + (i + 1)));
                rawLines[i] = ChatUtil.toPlainText(lines[i]);
            }
            // sometimes its a double, sometimes its an int...
            double x;
            double y;
            double z;
            try {
                x = data.getDouble("x");
                y = data.getDouble("y");
                z = data.getDouble("z");
            } catch (ClassCastException ex) {
                x = data.getInteger("x");
                y = data.getInteger("y");
                z = data.getInteger("z");
            }
            Location loc = new Location(event.getPlayer().getWorld(), x, y, z);
            if (event.getPlayer().getLocation().distanceSquared(loc) > 200 * 200) {
                return;
            }
            Block b = loc.getBlock();
            if (!(b.getState() instanceof Sign)) {
                return;
            }
            Sign sign = (Sign) b.getState();
            lastSeenSigns.put(loc, sign);
            Optional<User> user = userHandler.getUser(event.getPlayer().getUniqueId());
            if (!user.isPresent()) {
                return;
            }
            // call sign placeholders
            modifySign(user.get(), loc, rawLines, lines);
            // modify packet
            for (int i = 0; i < lines.length; i++) {
                data.put("Text" + (i + 1), ComponentSerializers.JSON.serialize(lines[i]));
            }
        }
    });
    // update task
    new BukkitRunnable() {

        @Override
        public void run() {
            lastSeenSigns.forEach((loc, sign) -> sign.update());
        }
    }.runTaskTimer(voxelGamesLib, 20, 20);
}
Also used : Arrays(java.util.Arrays) ProtocolLibrary(com.comphenix.protocol.ProtocolLibrary) ChunkUnloadEvent(org.bukkit.event.world.ChunkUnloadEvent) UserHandler(com.voxelgameslib.voxelgameslib.user.UserHandler) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) EventHandler(org.bukkit.event.EventHandler) Block(org.bukkit.block.Block) Location(org.bukkit.Location) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) Map(java.util.Map) ChunkLoadEvent(org.bukkit.event.world.ChunkLoadEvent) LocalTime(java.time.LocalTime) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Nonnull(javax.annotation.Nonnull) ChatUtil(com.voxelgameslib.voxelgameslib.utils.ChatUtil) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) ComponentSerializers(net.kyori.text.serializer.ComponentSerializers) Sign(org.bukkit.block.Sign) PacketEvent(com.comphenix.protocol.events.PacketEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PacketType(com.comphenix.protocol.PacketType) TextComponent(net.kyori.text.TextComponent) Logger(java.util.logging.Logger) Component(net.kyori.text.Component) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) User(com.voxelgameslib.voxelgameslib.user.User) ProtocolManager(com.comphenix.protocol.ProtocolManager) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) Optional(java.util.Optional) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) PacketEvent(com.comphenix.protocol.events.PacketEvent) Block(org.bukkit.block.Block) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Sign(org.bukkit.block.Sign) Location(org.bukkit.Location)

Example 10 with User

use of com.voxelgameslib.voxelgameslib.user.User in project VoxelGamesLibv2 by VoxelGamesLib.

the class TeamSelectFeature method disable.

@Override
public void disable() {
    Map<String, Integer> sizes = calcSizes();
    // add everyone who isn't in a team yet to the smallest team
    for (User user : getPhase().getGame().getPlayers()) {
        if (!getTeam(user).isPresent()) {
            Team t = getTeam(findSmallest(sizes)).orElseThrow(() -> new RuntimeException("Null team encountered"));
            t.join(user, user.getRating(getPhase().getGame().getGameMode()));
            Lang.msg(user, LangKey.TEAM_AUTO_ASSIGNED, t.getName());
            sizes.put(t.getName(), sizes.get(t.getName()) + 1);
        }
    }
    // make teams balanced
    balance();
    // save teams for next phase
    DefaultGameData defaultGameData = getPhase().getGame().getGameData(DefaultGameData.class).orElse(new DefaultGameData());
    defaultGameData.teams = teams;
    getPhase().getGame().putGameData(defaultGameData);
}
Also used : User(com.voxelgameslib.voxelgameslib.user.User) Team(com.voxelgameslib.voxelgameslib.components.team.Team) DefaultGameData(com.voxelgameslib.voxelgameslib.game.DefaultGameData)

Aggregations

User (com.voxelgameslib.voxelgameslib.user.User)12 Nonnull (javax.annotation.Nonnull)4 EventHandler (org.bukkit.event.EventHandler)4 Team (com.voxelgameslib.voxelgameslib.components.team.Team)3 UserException (com.voxelgameslib.voxelgameslib.exception.UserException)3 Logger (java.util.logging.Logger)3 Sign (org.bukkit.block.Sign)3 VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)2 GameLeaveEvent (com.voxelgameslib.voxelgameslib.event.events.game.GameLeaveEvent)2 DefaultGameData (com.voxelgameslib.voxelgameslib.game.DefaultGameData)2 UserHandler (com.voxelgameslib.voxelgameslib.user.UserHandler)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 IPlayer (jskills.IPlayer)2 Bukkit (org.bukkit.Bukkit)2 Listener (org.bukkit.event.Listener)2