How to know which block was hit?

Discussion in 'Spigot Plugin Development' started by ServerProxy, Apr 15, 2017.

  1. My code:
    Code (Text):
    package me.diyar.adminmode.commands;

    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import net.minecraft.server.v1_7_R4.Material;

    public class Admin implements CommandExecutor {
       

    public HashMap<Player, ItemStack[]> invsave = new HashMap<Player, ItemStack[]>();
    public HashMap<Player, ItemStack[]> armorsave = new HashMap<Player, ItemStack[]>();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            String target2 = args[0];
           
           
            Player target = Bukkit.getServer().getPlayer(target2);
          if (cmd.getName().equalsIgnoreCase("admin")) {
              if (sender.hasPermission("admin.toggle")) {
              }else{
                  sender.sendMessage(ChatColor.AQUA + "You need the correct permissions!");
                 
              if (args.length == 0) {
                  sender.sendMessage(ChatColor.RED + "/admin <PLAYER>");
              }
                 
              }
              if (args.length == 1) {
                  sender.sendMessage(ChatColor.DARK_GRAY + "You have entered AdminMode!");
                        invsave.put(p, p.getInventory().getContents());
                        armorsave.put(p, p.getInventory().getArmorContents());
                  p.getInventory().clear();
                  p.getInventory().setArmorContents(null);
                  p.setGameMode(GameMode.ADVENTURE);
                  p.teleport(target);
                 
                 
              }
        }
            return true;
           
        }
           
            public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("exit")) {
                    Player p = (Player) sender;
                    ItemStack Ban = new ItemStack(org.bukkit.Material.REDSTONE_BLOCK);
                    ItemStack OtherStaff = new ItemStack(org.bukkit.Material.REDSTONE_BLOCK);
                    ItemStack CloseTheCase = new ItemStack(org.bukkit.Material.REDSTONE_BLOCK);
                    if (args.length > 0);
                        sender.sendMessage(ChatColor.GOLD + "You have exited StaffMode");
                    Inventory inv =    Bukkit.createInventory(null, 27);
                    p.openInventory(inv);
                    inv.setItem(14, Ban);
                    inv.setItem(17, OtherStaff);
                    inv.setItem(20, CloseTheCase);
                   
                   
                   
        }
                return true;
            }
           
    }
     
    If they press Ban, I want it to ban the target, If they press Pursue the case/OtherStaff, it will notify all staff members, and it will close the case, if close the case is pressed.
     
  2. What I want to know, how do I know which one they pressed.
     
  3. Use a PlayerInteractEvent, get the item in their hand, and check if it has the displayname/lore you want.
     
  4. Also i'm 99% sure you can't use onCommand1.
     
  5. It looks like you're trying to use a menu GUI for part of this, you'll need to listen to the inventoryClickEvent.

    Then get the item and check the display name OR get the slot clicked and if the slot is == the one you specified above you'll know which button they clicked.

    On a side note it appears you imported net.minecraft.material. You should delete this import and use org.bukkit.material for this code.


    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
  6. Right ...
     
  7. Andre_601

    Supporter

    Give the Items a displayname and Check that.

    For example
    Code (Java):
    //Create the Redstoneblock (You don't need to use org.bukkit.)
    ItemStack() Ban = new ItemStack(Material.REDSTONE_BLOCK);
    //Get the ItemMeta of the Redstoneblock
    ItemMeta BanMeta = Ban.getItemMeta();
    //Setting the displayname
    BanMeta.setDisplayName(ChatColor.DARK_RED + "Ban Player");
    //Set the new ItemMeta to the Block
    Ban.setItemMeta(BanMeta);

    //Set the Item in the inv
    inv.setItem(14, Ban);
    After that, you just need to make a InventoryClickEvent, in wich you Check, if the clicked Item is that Redstone Block with that name and in that inventory (Give the Inventory for that a own name).

    By the way:
    You don't need to make a seperate onCommand1 for a new command.
    Just add at the 3rd last curved Bracked a "else" followed by the command.

    Like this:
    Code (Java):
    //First command
    if(cmd.getName().equalsIgnoreCase("example1"){
      //Things that should happen
    }else
    //Second command
    if(cmd.getName().equalsIgnoreCase("example2"){
      //Other stuff, that should happen
    }
     
    • Agree Agree x 1
  8. How would I ban the player? So if they press it, I got down: p.performcommand("ban" + target) but Target gets a red line.
     
  9. You would have to assign target to some variable... like a player name.


    Sent from my iPhone using Tapatalk
     
  10. How do I link an Event, and a onCommand? So If I made a ItemStack in onCommand, How would I use that in an Event.
     
  11. What is the end goal you're trying to accomplish here? You're saying a lot of things that can be done but more context is needed those are pretty general questions.


    Sent from my iPhone using Tapatalk
     
  12. Okay, I am making a Staff Plugin, with /admin and /exit. /admin watches someone supposedly "Hack" and /exit gives them a GUI with three options: Ban a Player, Pursue the Case and Close The Case. I am trying to get it so when they click Ban a Player, it bans the target. I made the target in my first command. I am trying to put that in my Event.
     
  13. You'll need to make the target stored in a variable somewhere, possibly a hashmap like.

    HashMap<Player,Player> target = new HashMap<Player,Player>()

    This needs to be somewhere your GUI can access it. When your admin command is used, and you start following the player or what not.

    Do target.add(adminPlayer,suspectPlayer)

    Then when you click the GUI you can do target.get(adminPlayer) to find their stored target player object for banning or whatever


    Sent from my iPhone using Tapatalk
     
  14. Andre_601

    Supporter

    First of all: I cannot garantee, that this will work!

    Try something like this:
    Code (Java):
    public class YourPlugin extends JavaPlugin implements Listener{

        // Inventory and HashMap at the beginning
        private Inventory inv = null;
        private HashMap<Player, Player> target = new HashMap<Player, Player>();

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
       
            Player p = (Player)sender;
       
            // The first command
            if(cmd.getName().equalsIgnoreCase("admin")){
                // Check, if player has permissions
                if(p.hasPermission("yourplugin.admin")){
                    // Check, if there are args
                    if(args.length == 0){
                        p.sendMessage("/admin <Player>");
                        return true;
                    }else
                    if(args.length >= 1){
                   
                        // Getting the TargetPlayer
                        Player p2 = Bukkit.getPlayer(args[0]);
                   
                        // Check, if TargetPlayer exist
                        if(p2 != null){
                       
                            // Putting Target into the HashMap
                            target.put(p, p2);
                       
                            p.sendMessage(ChatColor.GREEN + "You entered AdminMode!");
                            return true;
                       
                        }else{
                            p.sendMessage("This player is not online/does not exist!");
                        }
                    }
               
                }else{
                    // sending message, that he has not permissions
                    p.sendMessage(ChatColor.RED + "ERROR! No permissions!");
                    return true;
               
                }
            }else
            // Second command
            if(cmd.getName().equalsIgnoreCase("exit")){
           
                // Checking permissions again...
                if(p.hasPermission("yourplugin.admin")){
               
                    // Checking, if HashMap contains CommandSender (Admin)
                    if(target.containsKey(p)){
                   
                        // Creating inventory
                        inv = p.getPlayer().getServer().createInventory(null, 9, ChatColor.RED + "AdminPanel");
                   
                        // Creating Items
                        ItemStack Ban = new ItemStack(Material.REDSTONE_BLOCK);
                        ItemMeta BanMeta = Ban.getItemMeta();
                        BanMeta.setDisplayName(ChatColor.DARK_RED + "Ban Player");
                        Ban.setItemMeta(BanMeta);
                   
                        ItemStack PursueCase = new ItemStack(Material.REDSTONE_BLOCK);
                        ItemMeta PCMeta = PursueCase.getItemMeta();
                        BanMeta.setDisplayName(ChatColor.GRAY + "Pursue Case");
                        Ban.setItemMeta(PCMeta);
                   
                        ItemStack CloseCase = new ItemStack(Material.REDSTONE_BLOCK);
                        ItemMeta CCMeta = CloseCase.getItemMeta();
                        BanMeta.setDisplayName(ChatColor.GREEN + "Close Case");
                        Ban.setItemMeta(CCMeta);
                   
                        // Setting the Items and open inv.
                        inv.setItem(0, Ban);
                        inv.setItem(4, PursueCase);
                        inv.setItem(8, CloseCase);
                   
                        p.openInventory(inv);
                        return true;
                   
                        // For everything else, we
                        // will use the InventoryClickEvent
                        // at the bottom...
                   
                    }else{
                        // sending message, that he isnt in AdminMode
                        p.sendMessage(ChatColor.RED + "ERROR! You are not in AdminMode!");
                        return true;
                    }
                }else{
                    // sending message, that he has not permissions
                    p.sendMessage(ChatColor.RED + "ERROR! No permissions!");
                    return true;
               
                }
            }
       
       
            return false;
        }

        @EventHandler
        public void on(InventoryClickEvent e){
       
            // getting the Player. You need to cast it to player...
            Player p = (Player)e.getWhoClicked();
       
            // Check, if the clicked inv is the same like above
            if(e.getInventory().getName().equalsIgnoreCase(ChatColor.RED + "AdminPanel")){
           
                // Check, if the clicked slots are the slots of the upper inv
                // and not the own
                if(e.getSlot() == e.getRawSlot()){
               
                    // Checking, if clicked item is NOT air (empty)
                    if(e.getCurrentItem() != null){
                   
                        // Cancel ClickEvent
                        e.setCancelled(true);
                   
                        // Check if Item is Redstoneblock
                        if(e.getCurrentItem().getType() == Material.REDSTONE_BLOCK){
                            // Get TargetPlayer from HashMap
                            Player p2 = target.get(p);
                       
                            // Is TargetPlayer still online?
                            if(p2 != null){
                           
                                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_RED + "Ban Player")){
                               
                                    // Closing Inv
                                    e.getView().close();
                               
                                    // Perform Ban-command
                                    p.performCommand("Ban " + p2);
                               
                                    /* You can perform it even through console, like this:
                                    *
                                    * Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "ban " + p2);
                                    *
                                    */

                               
                               
                                    // Removing you from HashMap (Leaving AdminMode)
                                    target.remove(p);
                               
                                }else
                                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GRAY + "Pursue Case")){
                               
                                    // Closing Inv
                                    e.getView().close();
                               
                                    //Maybe teleport?
                                    p.teleport(p2);
                               
                                }else
                                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Close Case")){
                               
                                    // Closing Inv
                                    e.getView().close();
                               
                                    // Removing you from HashMap (Leaving AdminMode)
                                    target.remove(p);
                               
                                    p.sendMessage(ChatColor.GREEN + "You closed the case...");
                               
                                }
                            }else{
                                // sending message, that TragetPlayer is not online
                                p.sendMessage(ChatColor.RED + "ERROR! Player not online!");
                            }
                        }
                    }
                }
            }
        }
    }
    I use something else in my plugin HelpGUI and it works great...