Search in sources :

Example 1 with User

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

the class SpawnFeature method enable.

@Override
public void enable() {
    map = getPhase().getFeature(MapFeature.class).getMap();
    for (Marker marker : map.getMarkers(spawnMarker)) {
        spawns.add(marker.getLoc());
    }
    if (spawns.size() == 0) {
        log.warning("Could not find any spawns on map " + map.getInfo().getDisplayName() + "(" + map.getInfo().getWorldName() + ")");
        getPhase().getGame().abortGame();
        return;
    }
    if (isInitialSpawn) {
        for (User user : getPhase().getGame().getPlayers()) {
            user.getPlayer().teleportAsync(getSpawn(user.getPlayer().getUniqueId()));
        }
    }
}
Also used : User(com.voxelgameslib.voxelgameslib.components.user.User) Marker(com.voxelgameslib.voxelgameslib.components.map.Marker)

Example 2 with User

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

the class SignPlaceholders method init.

public void init() {
    registerPlaceholders();
    // search for already loaded signs
    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;
            }
            if (!(rawLines[0].equals("") && rawLines[1].equals("") && rawLines[2].equals("") && rawLines[3].equals(""))) {
                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) User(com.voxelgameslib.voxelgameslib.components.user.User) ProtocolLibrary(com.comphenix.protocol.ProtocolLibrary) ChunkUnloadEvent(org.bukkit.event.world.ChunkUnloadEvent) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Singleton(javax.inject.Singleton) Pair(com.voxelgameslib.voxelgameslib.util.Pair) Inject(javax.inject.Inject) EventHandler(org.bukkit.event.EventHandler) Trackable(com.voxelgameslib.voxelgameslib.api.stats.Trackable) UserHandler(com.voxelgameslib.voxelgameslib.components.user.UserHandler) 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) StatsHandler(com.voxelgameslib.voxelgameslib.api.stats.StatsHandler) 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) Lang(com.voxelgameslib.voxelgameslib.internal.lang.Lang) ChatUtil(com.voxelgameslib.voxelgameslib.util.utils.ChatUtil) List(java.util.List) Component(net.kyori.text.Component) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) 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 3 with User

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

Example 4 with User

use of com.voxelgameslib.voxelgameslib.components.user.User 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.PLAYER_HEAD).name(markerDefinition.getPrefix()).meta(itemMeta -> {
                char prefix = markerDefinition.getPrefix().toUpperCase().charAt(0);
                Skin skin = textureHandler.getSkin(prefix + "").orElseThrow(() -> new VoxelGameLibException("Unknown skull " + prefix));
                SkullMeta meta = ((SkullMeta) itemMeta);
                meta.setPlayerProfile(textureHandler.getPlayerProfile(skin));
                Objects.requireNonNull(meta.getPlayerProfile()).setName(markerDefinition.getPrefix());
                meta.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 : ClickType(org.bukkit.event.inventory.ClickType) User(com.voxelgameslib.voxelgameslib.components.user.User) PlayerProfile(com.destroystokyo.paper.profile.PlayerProfile) CommandAlias(co.aikar.commands.annotation.CommandAlias) MapHandler(com.voxelgameslib.voxelgameslib.components.map.MapHandler) Singleton(javax.inject.Singleton) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) TextureHandler(com.voxelgameslib.voxelgameslib.internal.texture.TextureHandler) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) InventoryMenuBuilder(org.inventivetalent.menubuilder.inventory.InventoryMenuBuilder) MarkerDefinition(com.voxelgameslib.voxelgameslib.components.map.MarkerDefinition) CommandPermission(co.aikar.commands.annotation.CommandPermission) Nonnull(javax.annotation.Nonnull) Material(org.bukkit.Material) Subcommand(co.aikar.commands.annotation.Subcommand) BaseCommand(co.aikar.commands.BaseCommand) Skin(org.mineskin.data.Skin) UUID(java.util.UUID) Logger(java.util.logging.Logger) Lang(com.voxelgameslib.voxelgameslib.internal.lang.Lang) ItemStack(org.bukkit.inventory.ItemStack) Objects(java.util.Objects) List(java.util.List) ItemBuilder(com.voxelgameslib.voxelgameslib.util.utils.ItemBuilder) Syntax(co.aikar.commands.annotation.Syntax) Optional(java.util.Optional) LangKey(com.voxelgameslib.voxelgameslib.internal.lang.LangKey) Default(co.aikar.commands.annotation.Default) ItemBuilder(com.voxelgameslib.voxelgameslib.util.utils.ItemBuilder) InventoryMenuBuilder(org.inventivetalent.menubuilder.inventory.InventoryMenuBuilder) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) Skin(org.mineskin.data.Skin) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) MarkerDefinition(com.voxelgameslib.voxelgameslib.components.map.MarkerDefinition) Subcommand(co.aikar.commands.annotation.Subcommand) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 5 with User

use of com.voxelgameslib.voxelgameslib.components.user.User 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)

Aggregations

User (com.voxelgameslib.voxelgameslib.components.user.User)14 Nonnull (javax.annotation.Nonnull)5 Logger (java.util.logging.Logger)4 EventHandler (org.bukkit.event.EventHandler)4 UserException (com.voxelgameslib.voxelgameslib.api.exception.UserException)3 Team (com.voxelgameslib.voxelgameslib.components.team.Team)3 Lang (com.voxelgameslib.voxelgameslib.internal.lang.Lang)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Optional (java.util.Optional)3 Inject (javax.inject.Inject)3 Singleton (javax.inject.Singleton)3 Sign (org.bukkit.block.Sign)3 CommandAlias (co.aikar.commands.annotation.CommandAlias)2 CommandPermission (co.aikar.commands.annotation.CommandPermission)2 VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)2 VoxelGameLibException (com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException)2 DefaultGameData (com.voxelgameslib.voxelgameslib.api.game.DefaultGameData)2 UserHandler (com.voxelgameslib.voxelgameslib.components.user.UserHandler)2 Bukkit (org.bukkit.Bukkit)2