Updating the inventory (using Craftbukkit with Bukkit)

Discussion in 'Resources' started by bergerkiller, Jul 21, 2011.

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

    bergerkiller

    You need both libraries referenced for this to work. :)

    Imports:
    Code:
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.*;
    import net.minecraft.server.*;
    Main function:
    Code:
        public static void updateInventory(Player p) {
            CraftPlayer c = (CraftPlayer) p;
            for (int i = 0;i < 36;i++) {
                int nativeindex = i;
                if (i < 9) nativeindex = i + 36;
                ItemStack olditem =  c.getInventory().getItem(i);
                net.minecraft.server.ItemStack item = null;
                if (olditem != null && olditem.getType() != Material.AIR) {
                    item = new net.minecraft.server.ItemStack(0, 0, 0);
                    item.id = olditem.getTypeId();
                    item.count = olditem.getAmount();
                    item.damage = olditem.getDurability();
                }
                Packet103SetSlot pack = new Packet103SetSlot(0, nativeindex, item);
                c.getHandle().netServerHandler.sendPacket(pack);
            }
        }
    Basically what this code does, is sending packets of all inventory items to the client. You can use only a certain index to update a single ItemStack. Use this simple (monitored) event to update the inventory after an /item or /give command was sent:
    Code:
        @Override
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
            String msg = event.getMessage().toLowerCase();
            if (msg.startsWith("/item ")) {
                updateInventory(event.getPlayer());
            } else if (msg.startsWith("/give ")) {
                //parse the command
                msg = msg.substring(6);
                if (msg.contains(" ")) {
                    msg = msg.substring(0, msg.indexOf(" "));
                    List<Player> m = event.getPlayer().getServer().matchPlayer(msg);
                    if (m.size() != 0) {
                        updateInventory(m.get(0));
                    }
                }
            }
        }
    Not sure if the standard Bukkit updateInventory function was patched already, so this can be used as a (possible) workaround.

    Note: it matches the player twice now. (once in the processing of the command and during the validation of the preprocess)

    EDIT:

    All works EXCELLENTLY! So glad I can now host free-build servers without users complaining of missing items. :)
    Do need to work more on the /item and /give listener; it fails at some point.
     
  2. Offline

    Tim Visee

    Hi, thanks for this, but I've one question, how do I import the net.minecraft.server thing, do I need to import the minecraft_server.jar file as a library into my project. I always wanted to know how to do this, but I never found a solution.. :(
     
  3. Offline

    AbeJ

    You need to import craftbukkit as well as bukkit into your project. craftbukkit includes the net.minecraft.server classes, and bukkit doesn't.
     
  4. Offline

    AVirusC

    Code:
    item.damage = olditem.getDurability();
    I'm getting The Field ItemStack.damage is not visible.

    Looking at ItemStack.class I see:
    Code:
    /*     */   public int count;
    /*     */   public int b;
    /*     */   public int id;
    /*     */   private int damage;
    
    Is this due to the fact that it is private now and not public? Is there a way around this? Sorry, I'm new to java.
     
  5. Offline

    bergerkiller

    @AVirusC It seems as if you now need to use the b(int i) function to set the damage. Something with data protection of CraftBukkit...
     
  6. Offline

    Iso

    ank how can I make this?
     
  7. Offline

    bergerkiller

    I have this in my coding, no idea when I changed it. Neither do I know if it works though.
    Code:
        public static void updateInventory(Player p, int slotindex) {
            CraftPlayer c = (CraftPlayer) p;
            int nativeindex = slotindex;
            if (slotindex < 9) nativeindex = slotindex + 36;
            ItemStack olditem =  c.getInventory().getItem(slotindex);
            net.minecraft.server.ItemStack item = null;
            if (olditem != null && olditem.getType() != Material.AIR) {
                item = new net.minecraft.server.ItemStack(0, 0, 0);
                item.id = olditem.getTypeId();
                item.count = olditem.getAmount();
                item.b = olditem.getDurability();
            }
            Packet103SetSlot pack = new Packet103SetSlot(0, nativeindex, item);
            c.getHandle().netServerHandler.sendPacket(pack);
        }
     
  8. Offline

    Sleaker

    player.updateInventory() still works btw. this is a lot of unecessary codebits. updateInventory is tagged as deprecated though it has no direct replacement, I'm pretty sure it's the bukkit team just saying the method needs to get redone, or reworked as the inventory should sync automatically rather than needing to have an update call.
     
    Acrobot likes this.
  9. Offline

    Ziden

    takes enchantments go away
    gotta dicsover how to convert Hash<Enchancement, int> to NBTags

    until then this can work i guess:

    Code:
    public static void updateInventory(Player p) {
            CraftPlayer c = (CraftPlayer) p;
            for (int i = 0;i < 36;i++) {
                int nativeindex = i;
                if (i < 9) nativeindex = i + 36;
                ItemStack olditem =  c.getInventory().getItem(i);
                net.minecraft.server.ItemStack item = null;
                if (olditem != null && olditem.getType() != Material.AIR) {
                    item = new net.minecraft.server.ItemStack(0, 0, 0);
                    item.id = olditem.getTypeId();
                    item.count = olditem.getAmount();
                    item.b = olditem.getDurability();
                }
                if(olditem.getEnchantments().size()==0) {
                    Packet103SetSlot pack = new Packet103SetSlot(0, nativeindex, item);
                    c.getHandle().netServerHandler.sendPacket(pack);
                }
            }
        }
    no updates for enchanted items.
     
  10. Offline

    sionzee

    in 1.4.7
    need replace:
    net.minecraft.server
    to:
    net.minecraft.server.v1_4_R1
     
Thread Status:
Not open for further replies.

Share This Page