Fixed player item dropping, it does not consume the item anymore when the event is cancelled

This commit is contained in:
Felix Cravic 2020-05-17 04:41:32 +02:00
parent 7512cb8f03
commit 47ca22c3d1
5 changed files with 16 additions and 11 deletions

View File

@ -190,7 +190,7 @@ public class PlayerInit {
});
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.CREATIVE);
player.setGameMode(GameMode.SURVIVAL);
player.teleport(new Position(0, 75, 0));
ItemStack item = new ItemStack(Material.STONE, (byte) 43);

View File

@ -2,11 +2,8 @@ package fr.themode.demo.commands;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandProcessor;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.Dimension;
import java.util.Optional;

View File

@ -29,7 +29,6 @@ public class SimpleCommand implements CommandProcessor {
creature.setPathTo(player.getPosition());
}
player.sendMessage("Direction: " + MathUtils.getHorizontalDirection(player.getPosition().getYaw()));
return true;

View File

@ -613,6 +613,10 @@ public class Player extends LivingEntity {
this.defaultEatingTime = defaultEatingTime;
}
/**
* @param item the item to drop
* @return true if player can drop the item (event not cancelled), false otherwise
*/
public boolean dropItem(ItemStack item) {
ItemDropEvent itemDropEvent = new ItemDropEvent(item);
callEvent(ItemDropEvent.class, itemDropEvent);

View File

@ -10,6 +10,7 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.StackingRule;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.server.play.AcknowledgePlayerDiggingPacket;
import net.minestom.server.network.packet.server.play.EntityEffectPacket;
@ -89,12 +90,13 @@ public class PlayerDiggingListener {
dropItem(player, droppedItemStack, ItemStack.getAirItem());
break;
case DROP_ITEM:
ItemStack droppedItemStack2 = player.getInventory().getItemInMainHand().clone();
droppedItemStack2.setAmount((byte) 1);
ItemStack handItem = player.getInventory().getItemInMainHand().clone();
ItemStack droppedItemStack2 = handItem.clone();
StackingRule handStackingRule = handItem.getStackingRule();
ItemStack handItem = player.getInventory().getItemInMainHand();
handItem.setAmount((byte) (handItem.getAmount() - 1));
handItem = handItem.getAmount() <= 0 ? ItemStack.getAirItem() : handItem;
droppedItemStack2 = handStackingRule.apply(droppedItemStack2, 1);
handItem = handStackingRule.apply(handItem, handStackingRule.getAmount(handItem) - 1);
dropItem(player, droppedItemStack2, handItem);
break;
@ -121,8 +123,11 @@ public class PlayerDiggingListener {
}
private static void dropItem(Player player, ItemStack droppedItem, ItemStack handItem) {
PlayerInventory playerInventory = player.getInventory();
if (player.dropItem(droppedItem)) {
player.getInventory().setItemInMainHand(handItem);
playerInventory.setItemInMainHand(handItem);
} else {
playerInventory.update();
}
}