How to edit a player's inventory

Discussion in 'Spigot Plugin Development' started by ZevoGaems, May 5, 2017.

  1. I am trying to make a staff mode plugin like some hcf servers out there. I don't know how to give a player a item or clear their inventory so how would I do that?
     
  2. A small example of how to clean, and add items to a player's inventory


    Code (Text):
    package me.zeuss.admin;

    import java.util.ArrayList;
    import java.util.HashMap;

    import org.bukkit.Material;
    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;

    public class Admin implements CommandExecutor {

        private static ArrayList<Player> admins = new ArrayList<Player>();
        private static HashMap<Player, Inventory> adminsInventorys = new HashMap<Player, Inventory>();
     
        public static ArrayList<Player> getAdmins(){
            return admins;
        }
     
        public static HashMap<Player, Inventory> getInventorys(){
            return adminsInventorys;
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("admin")){
                if(sender instanceof Player){
                    Player p = (Player)sender;
                    if(getAdmins().contains(p)){
                        p.getInventory().clear(); //Clears player inventory
                        Inventory inv = getInventorys().get(p); //Grab the saved inventory from the player
                        p.getInventory().setContents(inv.getContents()); //Puts the inventory saved on the player
                        getInventorys().remove(p); //Clears saved player inventory
                        p.sendMessage("§cYou are no longer in admin mode");
                        return true;
                    } else {
                        getInventorys().put(p, p.getInventory()); //Adds the player's current inventory to a list in order to recover when leaving admin mode
                        p.getInventory().addItem(new ItemStack(Material.BLAZE_ROD)); //Add an item to player inventory
                        p.getInventory().addItem(new ItemStack(Material.BOOK));
                        p.getInventory().addItem(new ItemStack(Material.SLIME_BALL));
                        p.getInventory().addItem(new ItemStack(Material.IRON_FENCE));
                        p.getInventory().addItem(new ItemStack(Material.FEATHER));
                        p.sendMessage("§aYou are now in admin mode.");
                        return true;
                    }
                } else {
                    sender.sendMessage("§cUse this command within the game.");
                    return true;
                }
            }
            return false;
        }

    }
     
    Sorry my english I'm not fluent
     
    • Like Like x 1
  3. Spoon feeding :p
     
  4. :D;)
     
    • Funny Funny x 2
  5. you means /invsee ?
    Code (Text):
    if (cmdlable.equals("invsee")) {
                    if(player.hasPermission("rank.owner")){
                        if (args.length != 0) {
                            if (player.getServer().getPlayer(args[0]) != null) {
                                Player targetPlayer = getServer().getPlayer(args[0]);
                                Inventory inv = targetPlayer.getInventory();
                                player.openInventory(inv);
                            } else {
                                player.sendMessage(ChatColor.GRAY + "Player Not Online!");
                            }
                        }
                    }
                }
    clear the Inventory use :
    Code (Text):
    targetPlayer.getInventory().clear();
     
  6. When you spoonfeed, make sure atleast your code is good and proper...