Search in sources :

Example 1 with HolderGUI

use of me.devtec.theapi.bukkit.gui.HolderGUI in project TheAPI by TheDevTec.

the class BukkitLoader method onLoad.

public void onLoad() {
    initTheAPI(this);
    new Metrics(this, 10581);
    boolean mohist = false;
    try {
        if (Class.forName("com.mohistmc.MohistMC", true, getClassLoader()) != null)
            mohist = true;
    } catch (Exception | NoClassDefFoundError err) {
    }
    try {
        nmsProvider = (NmsProvider) Class.forName("me.devtec.theapi.bukkit.nms." + Ref.serverVersion() + (mohist ? "_Mohist" : ""), true, getClassLoader()).newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (nmsProvider != null)
        nmsProvider.loadParticles();
    if (new File("spigot.yml").exists() && new Config("spigot.yml").getBoolean("settings.late-bind")) {
        new Thread(() -> {
            // ASYNC
            if (Ref.isNewerThan(7))
                handler = new PacketHandler_New(true);
            else
                handler = (PacketHandler<?>) Ref.newInstanceByClass("me.devtec.theapi.bukkit.packetlistener.PacketHandler_Legacy", true);
        }).start();
    } else {
        if (Ref.isNewerThan(7))
            handler = new PacketHandler_New(false);
        else
            handler = (PacketHandler<?>) Ref.newInstanceByClass("me.devtec.theapi.bukkit.packetlistener.PacketHandler_Legacy", false);
    }
    resource = Ref.nmsOrOld("network.protocol.game.PacketPlayInResourcePackStatus", "PacketPlayInResourcePackStatus");
    close = Ref.nmsOrOld("network.protocol.game.PacketPlayInCloseWindow", "PacketPlayInCloseWindow");
    click = Ref.nmsOrOld("network.protocol.game.PacketPlayInWindowClick", "PacketPlayInWindowClick");
    itemname = Ref.nmsOrOld("network.protocol.game.PacketPlayInItemName", "PacketPlayInItemName");
    airBlock = Ref.invoke(Ref.getNulled(Ref.field(Ref.nmsOrOld("world.level.block.Block", "Block"), "AIR")), "getBlockData");
    if (airBlock == null)
        airBlock = Ref.getNulled(Ref.field(Ref.nmsOrOld("world.level.block.Blocks", "Blocks"), "AIR"));
    if (airBlock == null && Ref.isNewerThan(12))
        airBlock = Ref.invoke(Ref.get(Ref.cast(Ref.craft("block.data.CraftBlockData"), Bukkit.createBlockData(Material.AIR)), "state"), "getBlock");
    // BOSSBAR API: 1.7.10 - 1.8.8
    if (Ref.isOlderThan(9)) {
        new Tasker() {

            public void run() {
                for (BossBar s : bossbars) s.move();
            }
        }.runRepeating(0, 20);
    } else
        bossbars = null;
    new PacketListener() {

        @Override
        public boolean PacketPlayOut(String player, Object packet, Object channel) {
            return false;
        }

        public boolean isAllowedChatCharacter(char var0) {
            return var0 != 167 && var0 >= ' ' && var0 != 127;
        }

        public String buildText(String var0) {
            StringBuilder var1 = new StringBuilder();
            char[] var2 = var0.toCharArray();
            int var3 = var2.length;
            for (int var4 = 0; var4 < var3; ++var4) {
                char var5 = var2[var4];
                if (isAllowedChatCharacter(var5)) {
                    var1.append(var5);
                }
            }
            return var1.toString();
        }

        public boolean PacketPlayIn(String nick, Object packet, Object channel) {
            // NPC
            if (nick == null)
                return false;
            // ResourcePackAPI
            if (resource != null && packet.getClass() == resource) {
                Player player = Bukkit.getPlayer(nick);
                if (player == null)
                    return false;
                if (ResourcePackAPI.getResourcePack(player) == null || ResourcePackAPI.getHandlingPlayer(player) == null)
                    return false;
                ResourcePackAPI.getHandlingPlayer(player).onHandle(player, ResourcePackAPI.getResourcePack(player), ResourcePackResult.valueOf(Ref.isNewerThan(16) ? getLegacyNameOf(Ref.get(packet, Ref.isNewerThan(16) ? "a" : "status").toString()) : Ref.get(packet, Ref.isNewerThan(16) ? "a" : "status").toString()));
                return false;
            }
            // GUIS
            if (packet.getClass() == itemname) {
                Player player = Bukkit.getPlayer(nick);
                if (player == null)
                    return false;
                HolderGUI gui = BukkitLoader.gui.get(player.getUniqueId());
                if (gui != null && gui instanceof AnvilGUI) {
                    nmsProvider.postToMainThread(() -> {
                        ((AnvilGUI) gui).setRepairText(buildText(Ref.get(packet, "a") + ""));
                    });
                    return true;
                }
            }
            if (packet.getClass() == close) {
                Player player = Bukkit.getPlayer(nick);
                if (player == null)
                    return false;
                HolderGUI gui = BukkitLoader.gui.remove(player.getUniqueId());
                if (gui == null)
                    return false;
                gui.closeWithoutPacket(player);
                return true;
            }
            if (packet.getClass() == click) {
                Player player = Bukkit.getPlayer(nick);
                if (player == null)
                    return false;
                HolderGUI gui = BukkitLoader.gui.get(player.getUniqueId());
                return gui == null ? false : nmsProvider.processInvClickPacket(player, gui, packet);
            }
            return false;
        }

        private String getLegacyNameOf(String string) {
            switch(string.charAt(0)) {
                case 'a':
                    return "SUCCESSFULLY_LOADED";
                case 'b':
                    return "DECLINED";
                case 'c':
                    return "FAILED_DOWNLOAD";
                case 'd':
                    return "ACCEPTED";
            }
            return null;
        }
    }.register();
}
Also used : AnvilGUI(me.devtec.theapi.bukkit.gui.AnvilGUI) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) HolderGUI(me.devtec.theapi.bukkit.gui.HolderGUI) Config(me.devtec.shared.dataholder.Config) BossBar(me.devtec.theapi.bukkit.bossbar.BossBar) PacketHandler_New(me.devtec.theapi.bukkit.packetlistener.PacketHandler_New) PacketListener(me.devtec.theapi.bukkit.packetlistener.PacketListener) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Tasker(me.devtec.shared.scheduler.Tasker) PacketHandler(me.devtec.theapi.bukkit.packetlistener.PacketHandler) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 JarFile (java.util.jar.JarFile)1 Config (me.devtec.shared.dataholder.Config)1 Tasker (me.devtec.shared.scheduler.Tasker)1 BossBar (me.devtec.theapi.bukkit.bossbar.BossBar)1 AnvilGUI (me.devtec.theapi.bukkit.gui.AnvilGUI)1 HolderGUI (me.devtec.theapi.bukkit.gui.HolderGUI)1 PacketHandler (me.devtec.theapi.bukkit.packetlistener.PacketHandler)1 PacketHandler_New (me.devtec.theapi.bukkit.packetlistener.PacketHandler_New)1 PacketListener (me.devtec.theapi.bukkit.packetlistener.PacketListener)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 Player (org.bukkit.entity.Player)1