Directly run logic on nms ItemStack

This commit is contained in:
Strokkur24 2024-12-25 01:04:18 +01:00
parent 3fdd032529
commit 1cd52a97e7
No known key found for this signature in database
2 changed files with 7 additions and 12 deletions

View file

@ -716,7 +716,7 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
* @param slot The slot to drop
* @param amount The number of items to drop from this slot. Values below 1 don't drop an Item
* @return The dropped item entity, or null if the action was unsuccessful
* @throws IndexOutOfBoundsException If the slot is negative or bigger than the player's inventory
* @throws IllegalArgumentException If the slot is negative or bigger than the player's inventory
*/
public @Nullable Item dropItem(int slot, int amount);

View file

@ -7,7 +7,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
@ -808,9 +807,8 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
public @Nullable Item dropItem(final int slot, final int amount) {
// Make sure the slot is in bounds
if (slot < 0 || slot >= this.inventory.getSize()) {
throw new IndexOutOfBoundsException("Slot " + slot + " out of range for inventory of size " + this.inventory.getSize());
throw new IllegalArgumentException("Slot " + slot + " is not a valid inventory slot.");
}
return dropItemRaw(this.inventory.getItem(slot), amount, false);
@ -823,9 +821,8 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
public @Nullable Item dropItemRandomly(final int slot, final int amount) {
// Make sure the slot is in bounds
if (slot < 0 || slot >= this.inventory.getSize()) {
throw new IndexOutOfBoundsException("Slot " + slot + " out of range for inventory of size " + this.inventory.getSize());
throw new IllegalArgumentException("Slot " + slot + " is not a valid inventory slot.");
}
return dropItemRaw(this.inventory.getItem(slot), amount, true);
@ -841,17 +838,15 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
return null;
}
final ItemStack clonedItemStack = originalItemStack.clone();
final int droppedAmount = Math.min(clonedItemStack.getAmount(), amount);
clonedItemStack.setAmount(droppedAmount);
final net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(originalItemStack);
final net.minecraft.world.item.ItemStack dropContent = nmsItemStack.split(Math.min(originalItemStack.getAmount(), amount));
final ItemEntity droppedEntity = this.getHandle().drop(CraftItemStack.asNMSCopy(clonedItemStack), throwRandomly, true);
final ItemEntity droppedEntity = this.getHandle().drop(dropContent, throwRandomly, true);
if (droppedEntity == null) {
return null;
}
originalItemStack.setAmount(originalItemStack.getAmount() - droppedAmount);
return new CraftItem(this.server, droppedEntity);
return (Item) droppedEntity.getBukkitEntity();
}
@Override