GUI Error (On all plugins)

Discussion in 'Spigot Help' started by Simsimpicpic, Jan 14, 2020.

  1. Hello ^^'
    I have a little problem...
    When i click on a 1st GUI (item or command), it work...
    When i click an item on the 1st GUI, it work too, and it's open the 2nd GUI. (normal)
    But... When i click an item on the 2nd GUI, it do nothing, and don't do the action he supposed to do...

    It's on my plugin, (i've reached and checked all the code),
    And on other plugin (don't create by me)...

    Someone can help me ?
    Thanks.

    (Google don't help me :()
     
  2. You have to delay your task by a little bit of time by using a Scheduler:

    Bukkit.getScheduler().scheduleSyncDelayedTask(new Runnable(){

    @Override
    public void run(){
    p.openInventory(Your second Gui)
    }
    }, 5);
     
    • Informative Informative x 1
  3. I have tested, and it not worked...
    On every plugin, the second GUI don't work...
    I think this is a problem with spigot...
     
  4. In my plugins it worked just fine, why don't you send over your code, so I can look over it
     
    • Informative Informative x 1
  5. package fr.simon.francostaff;

    import java.lang.reflect.Field;
    import java.util.UUID;

    import org.apache.commons.codec.binary.Base64;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.SkullType;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.mojang.authlib.GameProfile;
    import com.mojang.authlib.properties.Property;

    public class FrancoStaff extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
    Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    getCommand("staff").setExecutor(this);
    getServer().getPluginManager().registerEvents(this, this);
    getLogger().info("Activé !");
    }

    @EventHandler
    public void onItemClicked(InventoryClickEvent e) {
    Player player = (Player) e.getWhoClicked();
    ItemStack current = e.getCurrentItem();
    Inventory i = e.getInventory();
    if(current == null) return;
    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    @Override
    public void run() {
    if (i.getName() == "§e§lOptions") {
    if(current.getItemMeta().getDisplayName() == "§lRetour§") {
    player.openInventory(getInv("staff-options", player));
    }
    }
    }
    if(i.getName().equalsIgnoreCase("§8Menu utilisateur staff")) {
    switch(e.getCurrentItem().getItemMeta().getDisplayName()) {
    case "§b§lNouvelle infraction":
    Inventory new_infra = Bukkit.createInventory(null, 18, "§9§lNouvelle infraction");
    new_infra.setItem(9, GlassItem(1, "§4§lEn développement", 14));
    player.openInventory(new_infra);
    break;
    case "§f§lListe des joueurs":
    Inventory player_list = Bukkit.createInventory(null, 18, "§e§lListe des joueurs");
    player_list.setItem(0, getSkullByUrl("http://textures.minecraft.net/texture/5cce7359a25de6da56308e6a369c6372e2c30906c62647040da137a32addc9", "§9§lBungeeCoord"));
    player.openInventory(player_list);
    break;
    case "§e§lOptions":
    player.openInventory(getInv("staff-options", player));
    break;
    default:
    break;
    }
    }
    }
    }, 5);

    }

    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] a) {
    if(s instanceof Player) {
    Player p = ((Player) s).getPlayer();
    p.openInventory(getInv("staff", ((Player) s).getPlayer()));
    return true;
    } else {
    s.sendMessage(String.format("[FrancoStaff] " + ChatColor.RED + "Cette commande n'est pas utilisable depuis la console !"));
    return true;
    }
    }

    public ItemStack GlassItem(int number, String name, int dye) {
    ItemStack v = new ItemStack(Material.STAINED_GLASS_PANE, number, (short) dye);
    ItemMeta vm = v.getItemMeta();
    vm.setDisplayName(name);
    v.setItemMeta(vm);
    return v;
    }

    public ItemStack getHeadByName(String name, Player player) {
    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (byte) SkullType.PLAYER.ordinal());
    SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
    skull.setItemMeta(skullMeta);
    ItemMeta itemMeta = skull.getItemMeta();
    itemMeta.setDisplayName(name);
    skull.setItemMeta(itemMeta);
    return skull;
    }

    private static ItemStack getSkullByUrl(String url, String name) {
    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
    if(url != null && !url.isEmpty()) {
    SkullMeta skullMeta = (SkullMeta)skull.getItemMeta();
    skullMeta.setDisplayName(name);
    GameProfile profile = new GameProfile(UUID.randomUUID(), (String)null);
    byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", new Object[]{url}).getBytes());
    profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
    Field profileField = null;
    try {
    profileField = skullMeta.getClass().getDeclaredField("profile");
    } catch (SecurityException | NoSuchFieldException var9) {
    var9.printStackTrace();
    }
    assert profileField != null;
    profileField.setAccessible(true);
    try {
    profileField.set(skullMeta, profile);
    } catch (IllegalAccessException var8) {
    var8.printStackTrace();
    }
    skull.setItemMeta(skullMeta);
    return skull;
    } else {
    return skull;
    }
    }

    public Inventory getInv(String name, Player player) {
    switch (name) {
    case "staff":
    Inventory i = Bukkit.createInventory(null, 54, "§8Menu utilisateur Staff");
    i.setItem(0, GlassItem(1, " ", 14)); //Supérieur gauche
    i.setItem(1, GlassItem(1, " ", 14)); //Supérieur gauche 2
    i.setItem(2, getSkullByUrl("http://textures.minecraft.net/textu...8f49e96689ab4bf2bc126bbc298555b93da79328419d8", "§f§lS"));
    i.setItem(3, getSkullByUrl("http://textures.minecraft.net/texture/e5bf4db51fab4aa279628dfb2a1720e01bf7e9b94bb8c7e702fb63757405d", "§f§lT"));
    i.setItem(4, getSkullByUrl("http://textures.minecraft.net/texture/1a74d9212e6491bc370caf025fd459fe632ca7c6ba4df7ebefafd489ab32fd", "§f§lA"));
    i.setItem(5, getSkullByUrl("http://textures.minecraft.net/textu...4aeecba9fe6afb18470b99a262bd1ba1bcc6913a2baaf", "§f§lF"));
    i.setItem(6, getSkullByUrl("http://textures.minecraft.net/textu...4aeecba9fe6afb18470b99a262bd1ba1bcc6913a2baaf", "§f§lF"));
    i.setItem(7, GlassItem(1, " ", 14));
    i.setItem(8, GlassItem(1, " ", 14));
    i.setItem(9, GlassItem(1, " ", 14));
    i.setItem(17, GlassItem(1, " ", 14));
    i.setItem(21, getSkullByUrl("http://textures.minecraft.net/textu...a1b82ef82f8ae55929325766ea84dde18c11de0c7d591", "§9§lNouvelle infraction"));
    i.setItem(23, getSkullByUrl("http://textures.minecraft.net/texture/b1dd4fe4a429abd665dfdb3e21321d6efa6a6b5e7b956db9c5d59c9efab25", "§f§lListe des joueurs"));

    i.setItem(29, getSkullByUrl("http://textures.minecraft.net/textu...dc68478f476e7d202c3ba6e7cbf297241be81681074bf", "§6§lListe Staff"));
    i.setItem(30, getSkullByUrl("http://textures.minecraft.net/texture/7f6bf958abd78295eed6ffc293b1aa59526e80f54976829ea068337c2f5e8", "§c§lListe d'infraction"));
    i.setItem(31, getSkullByUrl("http://textures.minecraft.net/texture/b6ea2135838461534372f2da6c862d21cd5f3d2c7119f2bb674bbd42791", "§a§lBuild Mode"));
    i.setItem(32, getSkullByUrl("http://textures.minecraft.net/textu...73694159864b83351a4d14717e476ebda1c3bf38fcf37", "§e§lOptions"));
    i.setItem(33, getSkullByUrl("http://textures.minecraft.net/texture/f23d5ff814f095d3fbe5e53aef08515e02f99c8591648835f6edd59356b6", "§9§lReports en attente"));
    //i.setItem(33, getSkullByUrl("http://textures.minecraft.net/texture/e7f9c6fef2ad96b3a5465642ba954671be1c4543e2e25e56aef0a47d5f1f", "§f§lF"));

    i.setItem(36, GlassItem(1, " ", 14));
    i.setItem(44, GlassItem(1, " ", 14));
    i.setItem(45, GlassItem(1, " ", 14));
    i.setItem(46, GlassItem(1, " ", 14));
    i.setItem(52, GlassItem(1, " ", 14));
    i.setItem(53, GlassItem(1, " ", 14));
    return i;
    case "staff-options":
    Inventory o = Bukkit.createInventory(null, 27, "§e§lOptions");
    o.setItem(1, GlassItem(1, " ", 14));
    o.setItem(13, getSkullByUrl("http://textures.minecraft.net/textu...1175972eefd77eca5269d4867c4eda1594ff3e6634658", "§c§lMaintenance"));
    o.setItem(0, getSkullByUrl("http://textures.minecraft.net/texture/edf5c2f893bd3f89ca40703ded3e42dd0fbdba6f6768c8789afdff1fa78bf6", "§4§lRetour"));
    o.setItem(1, GlassItem(1, " ", 14));
    o.setItem(2, GlassItem(1, " ", 14));
    o.setItem(3, GlassItem(1, " ", 14));
    o.setItem(4, GlassItem(1, " ", 14));
    o.setItem(5, GlassItem(1, " ", 14));
    o.setItem(6, GlassItem(1, " ", 14));
    o.setItem(7, GlassItem(1, " ", 14));
    o.setItem(8, GlassItem(1, " ", 14));
    o.setItem(18, GlassItem(1, " ", 14));
    o.setItem(19, GlassItem(1, " ", 14));
    o.setItem(20, GlassItem(1, " ", 14));
    o.setItem(21, GlassItem(1, " ", 14));
    o.setItem(22, GlassItem(1, " ", 14));
    o.setItem(23, GlassItem(1, " ", 14));
    o.setItem(24, GlassItem(1, " ", 14));
    o.setItem(25, GlassItem(1, " ", 14));
    o.setItem(26, GlassItem(1, " ", 14));
    return o;
    default:
    return null;
    }
    }
    }
     
  6. I have exactly the same problem as @Simsimpicpic can someone answer him?
     
    • Informative Informative x 1
  7. You have to close the inventory before the Scheduler by using:

    p.closeInventory()
     
    • Informative Informative x 1
  8. This doesn't work... :(
     
  9. I wrote the following code and it works just fine. Try implementing this into your plugin:

    Code (Text):

    package main;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    public class Click implements Listener{

        @EventHandler//Make sure you call the EventHandler and the event is registered in your Main
        public void onClick(InventoryClickEvent e) {
         
            if(e.getCurrentItem() == null)return;
         
            Inventory gui2 = Bukkit.createInventory(null, 27, "Gui 2");
            ItemStack i = new ItemStack(Material.WOODEN_AXE);
            gui2.addItem(i);
         
            if(e.getView().getTitle().equalsIgnoreCase("Gui 1")) {
                e.getWhoClicked().closeInventory(); //Closing the inventory to fix the bug
                Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
                 
                    @Override
                    public void run() {
                        e.getWhoClicked().openInventory(gui2);//Open the second inventory after some time
                    }
                }, 5);
            }
            else if(e.getView().getTitle().equalsIgnoreCase("Gui 2")) {
                e.getWhoClicked().closeInventory();
                e.getWhoClicked().sendMessage("works");
            }
        }
    }
     
     
    • Informative Informative x 1