UserInterfaceLib 0.0.3

an easy to use API to create your own GUI

  1. wysohn
    Contributors:
    wysohn
    This library is completely re-coded.

    https://www.spigotmc.org/resources/userinterfacelib2.25351/


    this plugin must be used with ProtocolLib


    i'm not sure if the same thing happens in spigot server, cancelling the inventoryclickevent in craftbukkit server has a bug: if a player shift+click the GUI icon fast enough and close the GUI 'before' the item actually going back to its original place, the item remains at player's inventory. I'm using ProtocolLib to control this glitch

    usage:

    there are two classes you have to aware of: GUIInterface and GUIObject

    GUIInterface represents the GUI itself and GUIObject is what you can add in your GUI. GUIObject is flextible since you can set your own handler.

    examples)

    1. check if the UserInterfaceLib is enabled

    if(pm.getPlugin("UserInterfaceLib").isEnabled()){
    this.userInterfaceLibEnabled = true;
    this.userInterfaceLib = (UserInterfaceLib) pm.getPlugin("UserInterfaceLib");
    }

    something like this

    2. create your GUIInterface

    GUIInterface gui = UserInterfaceLibAPI.createNewGUI("RPGStatManagement", 9);

    first argument is the name of interface, and second argument is the size of the inventory. size must be multiple of 9


    3. as you already noticed, you can add InterfaceOpenCloseHandler if you want.

    gui.setOpencloseHandler(new InterfaceOpenCloseHandler(){
    @Override
    public void onOpen(Player player) {
    player.sendMessage("you opened a GUI :D");
    }
    @Override
    public void onClose(Player player) {
    player.sendMessage("you closed the GUI :/");
    }
    });

    or just don't set anything if you don't want to handle open close events

    4. and now you can make GUIObject
    GUIObject btn = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.ENCHANTED_BOOK), gui);

    the first argument is the itemstack that will be used as the GUI's object, and the second argument is the GUIInterface where you gonna add your object.

    and don't forget to add object into interface!

    gui.addGUIObject(row, col, btn);

    5. you added your object in you interface, now you have to add handlers for your object.

    there are three types of handlers: ClickEventHandler, DropEventHandler, and KeyPressEventHandler.

    btn.setClickEventHandler();
    btn.setDropEventHandler();
    btn.setKeyPressEventHandler();

    are the method you have to use to set handlers

    the coding part is exactly same as openclose handler of GUIInterface so i will skip the example part

    6. now you are all set! but how to open this GUIInterface to users?

    very easy

    use the method of GUIInterface called toBukkitInv() removed
    use the method of GUIInterface called showGUIto(Player)

    for example) gui.showGUIto(player);

    ----------------------------------------------------------------------------------
    example

    Code (Text):
    public class UserInterfaceManager {
        private RPGSurvival plugin;

        public UserInterfaceManager(Plugin plugin){
            this.plugin = (RPGSurvival) plugin;
        }
     
        public GUIInterface createGUIStatManager(final RPGUser user){
            final GUIInterface gui;
         
            gui = UserInterfaceLibAPI.createNewGUI("RPGStatManagement", 9);
            gui.setOpencloseHandler(new InterfaceOpenCloseHandler(){
                @Override
                public void onOpen(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    user.setStatManagement(new StatManagement(user));
                }

                @Override
                public void onClose(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    user.setStatManagement(null);
                }
             
            });
         
            if(user.getStatManagement() == null)
                user.setStatManagement(new StatManagement(user));
         
            final List<String> instruction = new ArrayList<String>(){{
                add(getFormedMsg("좌클릭","1 증가"));
                add(getFormedMsg("우클릭","1 감소"));
                add(getFormedMsg("shift좌","5 증가"));
                add(getFormedMsg("shift우","5 감소"));
            }};
            gui.addGUIObject(0, 0, createBtnstrategy(gui, new ArrayList<String>(){{
                add(getFormedMsg("값",String.valueOf(user.getStatManagement().getStrategyOriginal())));
                addAll(instruction);
            }}));
            gui.addGUIObject(0, 1, createBtnpolitics(gui, new ArrayList<String>(){{
                add(getFormedMsg("값",String.valueOf(user.getStatManagement().getPoliticsOriginal())));
                addAll(instruction);
            }}));
            gui.addGUIObject(0, 2, createBtnknowledge(gui, new ArrayList<String>(){{
                add(getFormedMsg("값",String.valueOf(user.getStatManagement().getKnowledgeOriginal())));
                addAll(instruction);
            }}));
            gui.addGUIObject(0, 3, createBtntechnique(gui, new ArrayList<String>(){{
                add(getFormedMsg("값",String.valueOf(user.getStatManagement().getTechniqueOriginal())));
                addAll(instruction);
            }}));
            gui.addGUIObject(0, 7, createBtnStatCounter(gui, new ArrayList<String>(){{
                add(getFormedMsg("남은 스텟",String.valueOf(user.getStatManagement().getStatLeft())));
            }}));
            gui.addGUIObject(0, 8, createBtnconfirm(gui, new ArrayList<String>(){{
                add(getFormedMsg("좌클릭","스텟 적용 후 저장"));
            }}));
         
            plugin.logDebug("gui: "+gui);
         
            return gui;
        }
     
        private String getFormedMsg(String key, String value){
            return ChatColor.GOLD+key
                    +ChatColor.GRAY+" - "
                    +ChatColor.WHITE+value;
        }
     
        private void setDisplayName(ItemStack IS, String str){
            ItemMeta IM = IS.getItemMeta();
            IM.setDisplayName(ChatColor.AQUA+str);
            IS.setItemMeta(IM);
        }
     
        private void setLore(ItemStack IS, List<String> lore){
            ItemMeta IM = IS.getItemMeta();
            IM.setLore(lore);
            IS.setItemMeta(IM);
        }
     
        //used by clickeventhandlers
        private void setBtnAppearance(Player player, GUIObject btn, final int value){
            Inventory inv = player.getOpenInventory().getTopInventory();
            if(inv == null) return;
         
            ItemStack IS = btn.getIS();
            setLore(IS, new ArrayList<String>(){{
                add(getFormedMsg("값", String.valueOf(value)));
                add(getFormedMsg("좌클릭","1 증가"));
                add(getFormedMsg("우클릭","1 감소"));
                add(getFormedMsg("shift좌","5 증가"));
                add(getFormedMsg("shift우","5 감소"));
            }});
         
            inv.setItem(btn.getRow()*9 + btn.getCol(), IS);
         

        }
     
        //used by clickeventhandlers
        private void setCounterAppearance(Player player, GUIObject btn, final int value){
            Inventory inv = player.getOpenInventory().getTopInventory();
            if(inv == null) return;
         
            ItemStack counterIS = btn.getIS();
            setLore(counterIS, new ArrayList<String>(){{
                add(getFormedMsg("남은 스텟",String.valueOf(value)));
            }});
            inv.setItem(btn.getRow()*9 + 7, counterIS);
        }
     
        public GUIObject createBtnstrategy(final GUIInterface parent, List<String> lore) {
            final GUIObject btnStrategy = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.BOOK), parent);
            setLore(btnStrategy.getIS(), lore);
            setDisplayName(btnStrategy.getIS(), "전략");
         
            btnStrategy.setClickEventHandler(new ClickEventHandler(){

                @Override
                public void onClick_Left(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseStrategy(1);
                 
                    setBtnAppearance(player, btnStrategy,
                            management.getStrategyOriginal() + management.getStrategy());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Right(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseStrategy(1);
                 
                    setBtnAppearance(player, btnStrategy,
                            management.getStrategyOriginal() + management.getStrategy());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Middle(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftDouble(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseStrategy(5);
                 
                    setBtnAppearance(player, btnStrategy,
                            management.getStrategyOriginal() + management.getStrategy());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_RightShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseStrategy(5);
                 
                    setBtnAppearance(player, btnStrategy,
                            management.getStrategyOriginal() + management.getStrategy());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_BorderLeft(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderRight(Player player) {
                 
                 
                }
             
            });
         
            return btnStrategy;
        }

        public GUIObject createBtnpolitics(final GUIInterface parent, List<String> lore) {
            final GUIObject btnPolitics = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.COMPASS), parent);
            setLore(btnPolitics.getIS(), lore);
            setDisplayName(btnPolitics.getIS(), "정치");
         
            btnPolitics.setClickEventHandler(new ClickEventHandler(){

                @Override
                public void onClick_Left(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increasePolitics(1);
                 
                    setBtnAppearance(player, btnPolitics,
                            management.getPoliticsOriginal() + management.getPolitics());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Right(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreasePolitics(1);
                 
                    setBtnAppearance(player, btnPolitics,
                            management.getPoliticsOriginal() + management.getPolitics());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Middle(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftDouble(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increasePolitics(5);
                 
                    setBtnAppearance(player, btnPolitics,
                            management.getPoliticsOriginal() + management.getPolitics());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_RightShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreasePolitics(5);
                 
                    setBtnAppearance(player, btnPolitics,
                            management.getPoliticsOriginal() + management.getPolitics());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());

                }
             
                @Override
                public void onClick_BorderLeft(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderRight(Player player) {
                 
                 
                }
             
            });
         
            return btnPolitics;
        }

        public GUIObject createBtnknowledge(final GUIInterface parent, List<String> lore) {
            final GUIObject btnKnowledge = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.WOOD_SWORD), parent);
            setLore(btnKnowledge.getIS(), lore);
            setDisplayName(btnKnowledge.getIS(), "지식");
         
            btnKnowledge.setClickEventHandler(new ClickEventHandler(){

                @Override
                public void onClick_Left(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseKnowledge(1);
                 
                    setBtnAppearance(player, btnKnowledge,
                            management.getKnowledgeOriginal() + management.getKnowledge());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Right(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseKnowledge(1);
                 
                    setBtnAppearance(player, btnKnowledge,
                            management.getKnowledgeOriginal() + management.getKnowledge());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Middle(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftDouble(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseKnowledge(5);
                 
                    setBtnAppearance(player, btnKnowledge,
                            management.getKnowledgeOriginal() + management.getKnowledge());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_RightShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseKnowledge(5);
                 
                    setBtnAppearance(player, btnKnowledge,
                            management.getKnowledgeOriginal() + management.getKnowledge());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_BorderLeft(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderRight(Player player) {
                 
                 
                }
             
            });
         
            return btnKnowledge;
        }

        public GUIObject createBtntechnique(final GUIInterface parent, List<String> lore) {
            final GUIObject btnTechnique = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.LEATHER_CHESTPLATE), parent);
            setLore(btnTechnique.getIS(), lore);
            setDisplayName(btnTechnique.getIS(), "기술");
         
            btnTechnique.setClickEventHandler(new ClickEventHandler(){

                @Override
                public void onClick_Left(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseTechnique(1);
                 
                    setBtnAppearance(player, btnTechnique,
                            management.getTechniqueOriginal() + management.getTechnique());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Right(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseTechnique(1);
                 
                    setBtnAppearance(player, btnTechnique,
                            management.getTechniqueOriginal() + management.getTechnique());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_Middle(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftDouble(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.increaseTechnique(5);
                 
                    setBtnAppearance(player, btnTechnique,
                            management.getTechniqueOriginal() + management.getTechnique());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_RightShift(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    StatManagement management = user.getStatManagement();
                 
                    management.decreaseTechnique(5);
                 
                    setBtnAppearance(player, btnTechnique,
                            management.getTechniqueOriginal() + management.getTechnique());
                 
                    setCounterAppearance(player, parent.getGUIObject(0, 7), management.getStatLeft());
                }

                @Override
                public void onClick_BorderLeft(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderRight(Player player) {
                 
                 
                }
             
            });
         
            return btnTechnique;
        }

        public GUIObject createBtnconfirm(GUIInterface parent, List<String> lore) {
            GUIObject btnConfirm = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.BOOK_AND_QUILL), parent);
            setLore(btnConfirm.getIS(), lore);
            setDisplayName(btnConfirm.getIS(), "적용");
         
            btnConfirm.setClickEventHandler(new ClickEventHandler(){

                @Override
                public void onClick_Left(Player player) {
                    RPGUser user = RPGSurvival.getUserManager().getUser(player);
                    if(user == null){
                        player.sendMessage(ChatColor.RED+"문제가 발생 했습니다.");
                        player.sendMessage(ChatColor.RED+"어드민에게 알려 주세요.");
                        return;
                    }
                 
                    user.applyManagement();
                    player.closeInventory();
                    user.setStatManagement(null);
                }

                @Override
                public void onClick_Right(Player player) {
                 
                 
                }

                @Override
                public void onClick_Middle(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftDouble(Player player) {
                 
                 
                }

                @Override
                public void onClick_LeftShift(Player player) {
                 
                 
                }

                @Override
                public void onClick_RightShift(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderLeft(Player player) {
                 
                 
                }

                @Override
                public void onClick_BorderRight(Player player) {
                 
                 
                }
             
            });
         
            return btnConfirm;
        }
     
        public GUIObject createBtnStatCounter(GUIInterface parent, List<String> lore){
            GUIObject btnCounter = UserInterfaceLibAPI.createNewGUIObject(new ItemStack(Material.APPLE), parent);
            setLore(btnCounter.getIS(), lore);
            setDisplayName(btnCounter.getIS(), "남은 스텟");
         
            return btnCounter;
        }
    and this is the part of onCommand where I show GUI to users
    Code (Text):
                        if(plugin.userInterfaceLibEnabled){
                            RPGUser user = RPGSurvival.getUserManager().getUser(player);
                         
                            RPGSurvival.getUserInterfaceManager().createGUIStatManager(user).showGUIto(player);
                        }else{
                            statExplaination(player);
                        }
    ----------------------------------------------------------------------------------

    [​IMG]

    please e-mail me if you have any question or found a bug
    to : [email protected]

    thank you

Recent Updates

  1. works without protocolLib
  2. some packet changes
  3. fixed the bug