pl.openinventory does not work?!

Discussion in 'Plugin Development' started by johnny_boy, Jun 26, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    johnny_boy

    title, here is my code.
    Code:
            if (args.length == 2 && args[0].equals("accept")) {
                Player p = (Player) sender;
              
                Inventory testGUI = Bukkit.getServer().createInventory(p, 9, "ffs");
              
                ItemStack hi = new ItemStack(Material.CACTUS, 1);
              
                testGUI.addItem(hi);
              
                p.sendMessage("okay u accepted the headflip 1 sec!");
                Player opponent = Bukkit.getPlayer(args[1]);
              
                p.sendMessage(sender.getName() + "has accepted your wager!");
              
                //p.sendMessage(hi.toString());    debug
                //p.sendMessage(testGUI.toString()); debug
              
                p.openInventory(testGUI);
              
              
            }
    EDIT: this is for 1.8.8 spigot
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    johnny_boy

    absolutely nothing.. however the 2 debug lines, which I also just now changed to
    Code:
    System.out.println(hi);
    System.out.println(testGUI);
    still return nothing. So possibly that is the issue.
     
  4. Offline

    CommonSenze

    @MemeplexOwner
    Are you receiving the "okay u accepted the headflip 1 sec!" message?
     
  5. Offline

    johnny_boy

    I am.
     
  6. Offline

    CommonSenze

    @MemeplexOwner
    Remove the "getServer()" method when you're creating your inventory, change the InventoryHolder from the player, to null. Try that out. Also be for the player opens the inventory add a debug having the inventories display name.
     
  7. Offline

    johnny_boy

    Nothing, and still nothing for the debugs.
     
  8. Offline

    CommonSenze

  9. Offline

    johnny_boy

    You may, code it bad I just got back into this a month ago.
    Code:
    package placeholder.placeholder.coinflip;
    
    import java.util.HashMap;
    import java.util.Map;
    import placeholder.placeholder.coinflip.ActiveCoinflips;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    
    import com.earth2me.essentials.api.Economy;
    
    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.minecraft.server.v1_8_R3.Block;
    import placeholder.placeholder.coinflip.ActiveCoinflips;
    
    public class CoinflipSend implements CommandExecutor {
      
        private Main plugin;
      
        public CoinflipSend(Main instance) {
            plugin = instance;
        }
      
      
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Error. You must be a player to use this command.");
                return false;
            }      
          
            if (!(sender.hasPermission(new Permission("coinflip.use")))) {
                sender.sendMessage("Error u have no perms");
                return false;
            } else {
                sender.sendMessage("how");
            }
          
          
            if (args.length == 2 && args[0].equals("accept")) {
                Player p = (Player) sender;
              
                Inventory testGUI = Bukkit.createInventory(p, 9, "ffs");
              
                ItemStack hi = new ItemStack(Material.CACTUS, 1);
              
                testGUI.addItem(hi);
              
                p.sendMessage("okay u accepted the headflip 1 sec!");
                Player opponent = Bukkit.getPlayer(args[1]);
              
                p.sendMessage(sender.getName() + "has accepted your wager!");
              
                System.out.println(hi);
                System.out.println(testGUI);
              
                p.openInventory(testGUI);
              
              
            }
          
          
            if (args.length == 0) {
                String cmdUsage = plugin.getConfig().getString("coinflip.commandUsage");
                  
                String cmdUsageColour = ChatColor.translateAlternateColorCodes('&', cmdUsage);
              
                Player pl = (Player) sender;
                pl.sendMessage(cmdUsageColour);
              
          
            } else if (args[0].equals("wager")) {
              
                if (args.length != 2 && args.length < 3) {
                    sender.sendMessage(ChatColor.RED + "Error. No player supplied!");
                    return false;
                }
              
                if (Bukkit.getPlayer(args[1]) == null) {
                    sender.sendMessage(ChatColor.RED + "Error. Player is not online!");
                    return false;
                }
              
                Player opponent = Bukkit.getPlayer(args[1]);
              
                TextComponent opponentName = new TextComponent(sender.getName());
                opponentName.setBold(true);
                opponentName.setColor(ChatColor.GREEN);
              
                TextComponent challengeMessage1 = new TextComponent(" has challenged you to a wager!\n");
                challengeMessage1.setColor(ChatColor.WHITE);
              
                TextComponent challengeMessage2 = new TextComponent("Click ");
                challengeMessage2.setColor(ChatColor.GRAY);
              
                TextComponent acceptButton = new TextComponent("[here] ");
                acceptButton.setColor(ChatColor.GREEN);
                acceptButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/coinflip accept " + sender.getName()));
                acceptButton.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("/coinflip accept " + sender.getName()).create()));
              
                TextComponent messageEnd = new TextComponent("to accept it.");
                messageEnd.setColor(ChatColor.GRAY);
              
                TextComponent finalMessage = new TextComponent("");
                finalMessage.addExtra(opponentName);
                finalMessage.addExtra(challengeMessage1);
                finalMessage.addExtra(challengeMessage2);
                finalMessage.addExtra(acceptButton);
                finalMessage.addExtra(messageEnd);
              
      
                opponent.spigot().sendMessage(finalMessage);
              
                if (!(ActiveCoinflips.activeDuels.containsValue(sender.getName()))) {
              
                    ActiveCoinflips.activeDuels.put(sender.getName(), opponent.toString());
              
                } else {
                    sender.sendMessage("ERROR PLAYER ALREADY HAS AN INVITE SENT BY U");
                }
                sender.sendMessage(ActiveCoinflips.returnActive().toString());
          
              
            }
          
            return false;  
        } 
    }
    
    edit: put this in hastebin or something :p
     
  10. Offline

    CommonSenze

    @MemeplexOwner
    Do you receive the "sender.getName() + 'has accepted your wager!'"? I apologize for all these questions, I just don't see a mistake.
     
  11. Offline

    johnny_boy

    i did.

    edit: if you remember what I said, my debug statements are returning nothing.. no idea why though
     
  12. Offline

    CommonSenze

    @MemeplexOwner
    System.out.print sends to the console only maybe use Bukkit#broadcastMessage(String) instead and see what happens.
     
  13. Offline

    johnny_boy

    before i tried p.sendmessage but just for whatever reason i also tried Bukkit#broadcastMessage(String) and no result.
     
  14. Offline

    CommonSenze

    @MemeplexOwner
    I don't see the mistake. How is the command suppose to go? Like what is the command to execute those lines of code.
     
  15. Online

    timtower Moderator Moderator

  16. Offline

    johnny_boy

    how would I try this, im guessing thread.sleep is a bad idea, do i make a bukkit runnable?
     
  17. Online

    timtower Moderator Moderator

    BukkitRunnable, sleep is never a good idea, certainly not when you have the timer structure like Bukkit does.
     
  18. Offline

    johnny_boy

    ok i will try, thanks, in fact i was also thinking of this.

    @timtower
    for some reason this bukkit runnable isn't working, i've no idea why.
    Code:
    new BukkitRunnable() {
              
                    @Override
                    public void run() {
                        p.openInventory(testGUI);
                        Bukkit.broadcastMessage("this is running yes");
                    }
                }.runTaskLater(plugin, 20);
    
    wait i think i need to extend BukkitRunnable

    eh, still nothing, and it's asking me to declare run() outside onCommand, i've forgotten how to do Runnables so google i guess

    basically i've no idea what's going on. nothing i have tried works, including what you guys have said.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.


    EDIT 2:
    I have made a new project and just run this code:
    Code:
    package placeholder.placeholder.firsttimejoin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class JoinListener implements Listener {
      
        private Main plugin;
    
        public JoinListener(Main instance) {
            plugin = instance;
        }
      
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            e.setJoinMessage(null);
          
            Inventory testGUI = Bukkit.getServer().createInventory(null, 9, "ffs");
          
            new BukkitRunnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    e.getPlayer().openInventory(testGUI);
                }
              
            }.runTaskLater(plugin, 20L);
          
    
            e.getPlayer().sendMessage("Welcome to the server");
        }
    
    }
    
    
    the Runnable works and the inventory is displayed.

    What is going on?!

    EDIT 4:



    i rewrite the code


    and it now works?



    what the hell?!!
     
    Last edited: Jun 26, 2018
  19. Offline

    CommonSenze

    @MemeplexOwner
    I don't know what to tell you that was the weirdest thing to happen in Java yet, for me at least.
     
  20. Offline

    johnny_boy

    Well on the topic of Inventories, what is the best way to compare inventories?
    silly question but :/
     
  21. Online

    timtower Moderator Moderator

    Title mostly
     
  22. Offline

    johnny_boy

    ah good idea, thank you.
     
Thread Status:
Not open for further replies.

Share This Page