How to execute commands in an event

Discussion in 'Plugin Development' started by coobro123, Apr 11, 2013.

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

    coobro123

    Hello!

    I am creating a plugin thats in development stage that lets a player right click with an item, when they right ckick it pops up witha 9 slot inventory. I want to see how i get it soo, I can left click on an item in the inventory and it will execute an essentials command, like a /sudo but when they click the item in the inventory I already have the inventory define i just need help on the cklick of the item in inv and running essentials commands. If I could have some code that would be great. Cheers.

    Main(Only class):
    Code:
    package com.gmail.cooperclark132.plugin.test;
     
    import java.util.Arrays;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main extends JavaPlugin implements Listener {
        public static Inventory testInventory;
        public static Inventory welcomeInventory;
        public void onEnable() {
            testInventory = Bukkit.createInventory(null, 9, ChatColor.DARK_AQUA + "Potion");
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
            welcomeInventory = Bukkit.createInventory(null, 9, ChatColor.RED + "Welcome");
     
        }
        public void onDisable() {
           
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("hello")) {
                sender.sendMessage("Hello there " + ChatColor.GREEN+sender.getName()+".");
                return true;
            }
            return false;
        }   
        @EventHandler
        public void OnPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            int blockId = p.getItemInHand().getType().getId();
            ItemStack diamond = (new ItemStack(Material.DIAMOND, 1));
            if(blockId == 369) {
                p.openInventory(testInventory);
                String name = "" + ChatColor.GREEN+ChatColor.BOLD+"GoldenMC";
                testInventory.addItem(setName(diamond, name, null));
                p.sendMessage("It works!");
                }
            String name3 = "Info";
            if(blockId == 264) {
                    p.openInventory(welcomeInventory);
                    ItemStack blaze = new ItemStack((Material.BLAZE_ROD));
                    welcomeInventory.addItem(setName(blaze, name3, null));
                }   
       
            }
       
     
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            String name2 = "" + "Info";
            ItemStack diamond = (new ItemStack(Material.DIAMOND, 1));
            p.getInventory().addItem(setName(diamond, name2, null));
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 1));
            e.setJoinMessage(ChatColor.GREEN + p.getName() +" Joined the game!");
           
     
     
        }
          static ItemStack setName(ItemStack is, String name, String size)
          {
            ItemMeta im = is.getItemMeta();
            if (name != null)
            {
              im.setDisplayName(name);
            }
     
            im.setLore(Arrays.asList(new String[] { "GoldenMC" }));
            is.setItemMeta(im);
            return is;
          }
     
     
    }
    
    the code is a bit dodgey atm, its like my playing plugin thing :p
     
  2. Offline

    xmarinusx

    coobro123 Listen to InventoryClickEvent and check what item is clicked (event.getCurrentItem()) then you can use:
    Code:
    player.performCommand("The command you want to be performed");
    Just tell me if you need more help.
     
    Bananinha and JoeTheDev like this.
  3. Offline

    adam753

    Well, left-clicking on your inventory picks up the item, so I would suggest using a PlayerPickupItemEvent:
    Code:
    @EventHandler
    public void OnPlayerPickupItem(PlayerPickupItemEvent event)
    {
    //stuff here
    }
    
     
  4. Offline

    xmarinusx

    You could use event.setCancelled(true) to stop picking the item...
     
  5. Offline

    Asgernohns


    xmarinusx i need some help with that. :) I wan't a command inside my eventhandler.
     
  6. Offline

    xmarinusx

    Be a bit more specefic please, if you just want a command to be executed from the player use:
    Code:java
    1.  
    2. player.performCommand("gamemode 0") //This would set the player's gamemode on survival
    3.  

    Note that you can better use player.setGamemode(GameMode.SURVIVAL) for this, I just gave an example.
     
  7. Offline

    Asgernohns

    Can i do this in an if statement xmarinusx?
     
  8. Offline

    xmarinusx

    Asgernohns Of course you can...
    What do you need it for, actually?
     
  9. Offline

    Asgernohns

    xmarinusx tp cmd in an eventhandler. Sry i'm not answering, I am enjoying my summer holodays, with no internet at the hotel rooms. I need to go to the lobby every time I need internet.
     
  10. Offline

    xmarinusx

    Asgernohns
    Why would you need to execute a command for that from inside your code? You'd better just use something like "player.teleport();"
     
  11. Offline

    Asgernohns

    xmarinusx i need to teleport to a pressed sign location.
     
Thread Status:
Not open for further replies.

Share This Page