Armour disappears when dropped

Here is what I am doing:

        for(Inventory slot : user.getInventory().slots()){
	    	if (slot.peek().isPresent()) {
	    		build.offer(slot.peek().get());
	    	}
        }

	    player.openInventory(build.parent(), Text.of(TextColors.DARK_RED, targetName + "'s " + "inventory"));

You can get the items this way… but if you equip the armour, and then try to drop it on the ground, it disappears. If you just drop the items normally, all is fine. They appear on the ground as normal.

What should I be doing here to fix this?

Sorry there are a few things you have assumed that the reader will know.

First of all what API are you using? Api 8 (MC 1.16) or API 7 (1.12) - based on your usage of Text im going to assume API 7 but clarification would be useful

Next is what are the types of your variables?

Player user;
Player player;
Inventory build;

for(Inventory slot : user.getInventory().slots()){
	    	if (slot.peek().isPresent()) {
	    		build.offer(slot.peek().get());
	    	}
        }

	    player.openInventory(build.parent(), Text.of(TextColors.DARK_RED, targetName + "'s " + "inventory"));

Like that? Or is user a type of User?

Next. Is this code ran as a command or as event? If event then what event?

And finally… When you say Drop normally could you please clarify. Is it?

When the armor is equiped in the selected slot and then the player presses Q to drop the item this is “Normal”

Or

When the inventory is open and the player drags the armor outside the inventory window to drop the item this is “Normal”

Also the same goes for what is causing you bug. Is it when the inventory is open and the item is equipped to the player (say helmet on the helmet slot) and then attempts to drop the item, is this what causes the bug? And if you have it in that slot, but move it to another slot and then drop it, does it not cause the bug?

Sadly, if you want help, you need to specify as much useful data as possible

But in all seriousness. In Sponge, there is no special rules for specific items when dropping. That is unless a event gets in the way.

I would first check your testing environment. I would recommend Sponge Vanilla with only your plugin to ensure another plugin, mod or Forge is not interfering

If it still happens, i would then take a look at your own events, in particular any events that covers either dropping items or changing slots and ensure it doesnt delete the item (please note in sponge, cancelling events may not reset the inventory to its state before the trigger, therefore cancelling the drop item event will cancel the item being dropped, but the action still triggered and therefore the item is gone from the inventory)

Apologies for the missing details. Made the same mistake in another thread heh.

API 7. user is of type User, player is of type Player, and build is of type Inventory. It is ran as a command.

…and wow, I can’t bug test I guess. Thanks - the armour factor here doesn’t matter. When I drop the items by pressing Q, all is fine. When I drag them out of my inventory window with my mouse to drop them, they disappear. That is the whole story (for example if I put the armour on, put it in my hotbar and then press Q, it drops normally).

The only other plugin I have is worldedit - I will remove it tomorrow when testing. I am also using Sponge Vanilla.

I am not entirely sure what event means. I haven’t really created any events I don’t think but I’ll read documentation tomorrow.

So “events” are things that happen in game that you as a developer can listen to and manipulate (such as cancel or even change more specific things)

Common events are

MoveEntityEvent (typically filtered to just player)
InventoryInteractEvent (typically on custom inventories)
BlockInteractEvent (filtered to a player)

These can come in really useful. Take a look at the javadocs for all events :slight_smile:

1 Like

Thanks, great stuff.

Sorry to bump an old post, but I returned to this plugin I made a few months ago and just noticed when testing that if I’m in creative and drag and drop the items, they don’t disappear. They only disappear when I’m in survival mode and drag and drop items. (And interestingly enough, if I’m in creative and drop the items on the ground - and then go into survival and pick up the items - and then drag and drop them on the ground - they disappear.)

Hopefully* this extra bit of information is somewhat useful? An annoying bug and I’m so curious as to what is causing it.

Thanks.

So at this point im going to need to see some code. In particular your code for preventing the item spawning into the world.

I can give some pointers that may help. When it comes to creative inventory, it gets treated differently in Minecraft itself. So when a player clicks a item in the creative grid, it doesn’t tell the server until something happens to the item (such as put into the players inventory, or dragged and dropped) and even then its classed as a whole different event

https://jd.spongepowered.org/spongeapi/7.4.0/org/spongepowered/api/event/item/inventory/ClickInventoryEvent.Drop.Outside.Creative.html

https://jd.spongepowered.org/spongeapi/7.4.0/org/spongepowered/api/event/item/inventory/ClickInventoryEvent.Creative.html

1 Like

Cheers, I’ll look into that.

So I think the important method is probably “seeInventory”, though all the code can be viewed here: lightonia/PlayerFunctions.java at main · Jelly-Pudding/lightonia · GitHub

I think invsee is a thing - might also look into how that is made. But yeah here you just open up a player’s inventory from a backup and it is these items you retrieve which cause the problems with disappearing when dragged and dropped in survival mode.

So looking at your code, i cannot see anything to do with events.

For me, you build a custom inventory on line 68

In the inventory builder, you can add events to it. Those events will only fire with that custom inventory. I would add a event that is ran when a user clicks a slot, if they do then cancel the event. This will prevent the user from picking up the item in the first place

1 Like

Thanks. I didn’t realise events could be so specific.

After work today I’ll do some messing around and see if I can fix this.

1 Like

Changed it to this:

	public static void seeInventory(Player player, User user, String targetName) {		

		Inventory build = Inventory.builder()
				.of(InventoryArchetypes.DOUBLE_CHEST)
				.listener(ClickInventoryEvent.class, clickInventoryEvent -> {
					clickInventoryEvent.setCancelled(true);
					ItemStack loll = clickInventoryEvent.getCursorTransaction().getFinal().createStack();
					player.sendMessage(Text.of(TextColors.DARK_RED, loll.getType()));
					player.sendMessage(Text.of(TextColors.DARK_RED, loll.getQuantity()));
					player.getInventory().offer(loll);
				})
				.build(Lightonia.getPlugin());
        
		for(Inventory slot : user.getInventory().slots()){
			if (slot.peek().isPresent()) {
				build.offer(slot.peek().get());
			}
		}

		player.openInventory(build.parent(), Text.of(TextColors.DARK_RED, targetName + "'s " + "inventory"));      
	}

And then I discovered this applies to all blocks that get dragged and dropped when in survival mode. Even deleted all the plugins in my mods folder and the issue still persisted. I’m such an idiot when it comes to bug testing.

This does not appear to be a plugin issue. Sorry for wasting your time - but thanks for showing me about events (this wasn’t a complete waste of time I promise!)

Hey. Just glad i could help