Search in sources :

Example 1 with VoxelGamesLib

use of com.voxelgameslib.voxelgameslib.VoxelGamesLib 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 2 with VoxelGamesLib

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

the class MetricHandler method enable.

@Override
public void enable() {
    try {
        metrics = new Metrics(voxelGamesLib);
        // TODO add custom charts, like user/gamemode, installed gamesmodes, user/lang, installed langs etc
        // gamemodes multiline TODO enable this on the bstats page once its implemented....
        metrics.addCustomChart(new Metrics.MultiLineChart("gamemodes", () -> {
            Map<String, Integer> valueMap = new HashMap<>();
            gameHandler.getGameModes().forEach((gm) -> valueMap.put(gm.getName(), 1));
            return valueMap;
        }));
    } catch (Throwable ex) {
        log.warning("Metrics failed to enabled. This is not a critical problem. You can ignore it.");
    }
}
Also used : Inject(javax.inject.Inject) GameHandler(com.voxelgameslib.voxelgameslib.game.GameHandler) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) Handler(com.voxelgameslib.voxelgameslib.handler.Handler) Map(java.util.Map) Metrics(org.bstats.bukkit.Metrics) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) Singleton(javax.inject.Singleton) Metrics(org.bstats.bukkit.Metrics) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Logger (java.util.logging.Logger)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 PacketType (com.comphenix.protocol.PacketType)1 ProtocolLibrary (com.comphenix.protocol.ProtocolLibrary)1 ProtocolManager (com.comphenix.protocol.ProtocolManager)1 PacketAdapter (com.comphenix.protocol.events.PacketAdapter)1 PacketEvent (com.comphenix.protocol.events.PacketEvent)1 NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)1 GameHandler (com.voxelgameslib.voxelgameslib.game.GameHandler)1 Handler (com.voxelgameslib.voxelgameslib.handler.Handler)1 User (com.voxelgameslib.voxelgameslib.user.User)1 UserHandler (com.voxelgameslib.voxelgameslib.user.UserHandler)1 ChatUtil (com.voxelgameslib.voxelgameslib.utils.ChatUtil)1 LocalTime (java.time.LocalTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Arrays (java.util.Arrays)1