Trying to make an "inventory" trading system

Discussion in 'Spigot Plugin Development' started by Squallz, Feb 14, 2016.

  1. I am trying to make a trading system where you type a command such as "/trade" followed by the name of the player you want to trade with. This will then send a sound queue to the player you are requesting to trade, and they can accept or decline it. If they accept it, an inventory will be opened on both screens of the participating players. The inventory will be cut in half, with the items one player wants to trade on one side, and the items the other player wants to trade on the other. I then want an accept/decline button and all that stuff. I can do all of that, but how would I make it so when the trade is complete, the players will get the items the other player has put in? And how would I make it so the inventory is "shared" so they can see the other person putting/taking stuff out?
     
  2. make them open same inventory. foreach loop through slots
     
  3. How could I make it so they cant modify/move the normal items, but they can still add to it?
     
  4. use the InventoryIteractEvent.
     
  5. Yes..I know how to cancel them from moving stuff but this will also cancel them from placing stuff wont it..?
     
  6. Code (Text):
    package me.Jordan.Trading;

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

    import org.bukkit.Bukkit;
    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.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class MainTrading extends JavaPlugin{
       
        Inventory TradeInv;
        Inventory TradeInv2;
       
        public HashMap<Player, Player> TradingWith;
       
        public void onEnable(){
            TradingWith = new HashMap<Player, Player>();
        }
       
        @EventHandler
        public void InventoryUpdate(InventoryClickEvent e){
            Player p = (Player) e.getWhoClicked();
            if(e.getInventory().getName().equalsIgnoreCase("Trade Inventory")){
                    ItemStack FirstItem = new ItemStack(TradeInv.getItem(0));
                    TradeInv2.addItem(FirstItem);
                    TradingWith.get(p).updateInventory();
                    TradingWith.get(p).sendMessage("§5See this?");
            }
        }
       
        public boolean onCommand(final CommandSender sender, final Command cmd, final String command, final String[] args) {
            if(command.equalsIgnoreCase("trade")){
                ItemStack Accept1 = new ItemStack(Material.WOOL,1, (byte)5);
                ItemMeta A1M = Accept1.getItemMeta();
                A1M.setDisplayName("§7Click to §aAccept");
                ArrayList<String> AL = new ArrayList<String>();
                AL.add("§7This will accept the trade");
                AL.add("§7on your end. The other player");
                AL.add("§7will also need to accept.");
                A1M.setLore(AL);
                Accept1.setItemMeta(A1M);
                ItemStack Accept2 = new ItemStack(Material.WOOL,1, (byte)5);
                ItemMeta A2M = Accept2.getItemMeta();
                A2M.setDisplayName("§7Click to §aAccept");
                ArrayList<String> ALL = new ArrayList<String>();
                ALL.add("§7This will accept the trade");
                ALL.add("§7on your end. The other player");
                ALL.add("§7will also need to accept.");
                A2M.setLore(ALL);
                Accept2.setItemMeta(A1M);
                ItemStack Divider = new ItemStack(Material.IRON_FENCE);
                ItemMeta DividerMeta = Divider.getItemMeta();
                DividerMeta.setDisplayName("§cTrade in Session");
                Divider.setItemMeta(DividerMeta);
                Player p = (Player) sender;
                if(args.length == 0){
                    p.sendMessage("§7Syntax: §b/trade <player>");
                    return true;
                }
                else if(args.length == 1){
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if(target == null){
                        p.sendMessage(ChatColor.GREEN + "§7Player §b" + args[0] + " §7not found.");
                        return true;
                    }
                        TradeInv = Bukkit.getServer().createInventory(p, 36, "Trade Inventory");
                        TradeInv.setItem(4, Divider);
                        TradeInv.setItem(13, Divider);
                        TradeInv.setItem(22, Divider);
                        TradeInv.setItem(31, Divider);
                        TradeInv.setItem(28, Accept1);
                        TradeInv2 = Bukkit.getServer().createInventory(target, 36, "Trade Inventory");
                        TradeInv2.setItem(4, Divider);
                        TradeInv2.setItem(13, Divider);
                        TradeInv2.setItem(22, Divider);
                        TradeInv2.setItem(31, Divider);
                        TradeInv2.setItem(33, Accept2);
                        p.openInventory(TradeInv);
                        target.openInventory(TradeInv2);
                        TradingWith.put(p, target);
                    }
                }
                else if (args.length > 1){
                    Player p = (Player) sender;
                    p.sendMessage("§7Syntax: §b/trade <player>");
                    return true;
                }
            return false;
           
        }

    }
     
    This code isnt working? Trying to make it so when an item is added to the inventory, it will add the item into the other inventory, and update it. Hashmap is putting in the player that sent the command, then the person they sent it to. Then trying to get the "target" when the inventory click event is called and send them a msg and update their inventory.
     
  7. display the same inventory to them....
     
  8. to stop moving the item just do

    e.setCancelled(true)
     
    • Like Like x 1