Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-11-16 13:00:06 +01:00
SPIGOT-1526 / SPIGOT-1809: Expand Inventory API
Dieser Commit ist enthalten in:
Ursprung
5d90f9712b
Commit
daf45148d1
@ -49,6 +49,16 @@ public class CraftInventory implements Inventory {
|
|||||||
return item == null ? null : CraftItemStack.asCraftMirror(item);
|
return item == null ? null : CraftItemStack.asCraftMirror(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack[] getStorageContents() {
|
||||||
|
return getContents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException {
|
||||||
|
setContents(items);
|
||||||
|
}
|
||||||
|
|
||||||
public ItemStack[] getContents() {
|
public ItemStack[] getContents() {
|
||||||
ItemStack[] items = new ItemStack[getSize()];
|
ItemStack[] items = new ItemStack[getSize()];
|
||||||
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
|
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
|
||||||
@ -79,7 +89,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(int materialId) {
|
public boolean contains(int materialId) {
|
||||||
for (ItemStack item : getContents()) {
|
for (ItemStack item : getStorageContents()) {
|
||||||
if (item != null && item.getTypeId() == materialId) {
|
if (item != null && item.getTypeId() == materialId) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -96,7 +106,7 @@ public class CraftInventory implements Inventory {
|
|||||||
if (item == null) {
|
if (item == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (ItemStack i : getContents()) {
|
for (ItemStack i : getStorageContents()) {
|
||||||
if (item.equals(i)) {
|
if (item.equals(i)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -108,7 +118,7 @@ public class CraftInventory implements Inventory {
|
|||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (ItemStack item : getContents()) {
|
for (ItemStack item : getStorageContents()) {
|
||||||
if (item != null && item.getTypeId() == materialId) {
|
if (item != null && item.getTypeId() == materialId) {
|
||||||
if ((amount -= item.getAmount()) <= 0) {
|
if ((amount -= item.getAmount()) <= 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -130,7 +140,7 @@ public class CraftInventory implements Inventory {
|
|||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (ItemStack i : getContents()) {
|
for (ItemStack i : getStorageContents()) {
|
||||||
if (item.equals(i) && --amount <= 0) {
|
if (item.equals(i) && --amount <= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -145,7 +155,7 @@ public class CraftInventory implements Inventory {
|
|||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (ItemStack i : getContents()) {
|
for (ItemStack i : getStorageContents()) {
|
||||||
if (item.isSimilar(i) && (amount -= i.getAmount()) <= 0) {
|
if (item.isSimilar(i) && (amount -= i.getAmount()) <= 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -156,7 +166,7 @@ public class CraftInventory implements Inventory {
|
|||||||
public HashMap<Integer, ItemStack> all(int materialId) {
|
public HashMap<Integer, ItemStack> all(int materialId) {
|
||||||
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
ItemStack item = inventory[i];
|
ItemStack item = inventory[i];
|
||||||
if (item != null && item.getTypeId() == materialId) {
|
if (item != null && item.getTypeId() == materialId) {
|
||||||
@ -174,7 +184,7 @@ public class CraftInventory implements Inventory {
|
|||||||
public HashMap<Integer, ItemStack> all(ItemStack item) {
|
public HashMap<Integer, ItemStack> all(ItemStack item) {
|
||||||
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
if (item.equals(inventory[i])) {
|
if (item.equals(inventory[i])) {
|
||||||
slots.put(i, inventory[i]);
|
slots.put(i, inventory[i]);
|
||||||
@ -185,7 +195,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int first(int materialId) {
|
public int first(int materialId) {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
ItemStack item = inventory[i];
|
ItemStack item = inventory[i];
|
||||||
if (item != null && item.getTypeId() == materialId) {
|
if (item != null && item.getTypeId() == materialId) {
|
||||||
@ -208,7 +218,7 @@ public class CraftInventory implements Inventory {
|
|||||||
if (item == null) {
|
if (item == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
if (inventory[i] == null) continue;
|
if (inventory[i] == null) continue;
|
||||||
|
|
||||||
@ -220,7 +230,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int firstEmpty() {
|
public int firstEmpty() {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
if (inventory[i] == null) {
|
if (inventory[i] == null) {
|
||||||
return i;
|
return i;
|
||||||
@ -230,7 +240,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int firstPartial(int materialId) {
|
public int firstPartial(int materialId) {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
ItemStack item = inventory[i];
|
ItemStack item = inventory[i];
|
||||||
if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
|
if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
|
||||||
@ -246,7 +256,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int firstPartial(ItemStack item) {
|
private int firstPartial(ItemStack item) {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -373,7 +383,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove(int materialId) {
|
public void remove(int materialId) {
|
||||||
ItemStack[] items = getContents();
|
ItemStack[] items = getStorageContents();
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
if (items[i] != null && items[i].getTypeId() == materialId) {
|
if (items[i] != null && items[i].getTypeId() == materialId) {
|
||||||
clear(i);
|
clear(i);
|
||||||
@ -387,7 +397,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove(ItemStack item) {
|
public void remove(ItemStack item) {
|
||||||
ItemStack[] items = getContents();
|
ItemStack[] items = getStorageContents();
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
if (items[i] != null && items[i].equals(item)) {
|
if (items[i] != null && items[i].equals(item)) {
|
||||||
clear(i);
|
clear(i);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package org.bukkit.craftbukkit.inventory;
|
package org.bukkit.craftbukkit.inventory;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import java.util.Arrays;
|
||||||
import net.minecraft.server.EntityPlayer;
|
import net.minecraft.server.EntityPlayer;
|
||||||
import net.minecraft.server.PacketPlayOutHeldItemSlot;
|
import net.minecraft.server.PacketPlayOutHeldItemSlot;
|
||||||
import net.minecraft.server.PacketPlayOutSetSlot;
|
import net.minecraft.server.PacketPlayOutSetSlot;
|
||||||
@ -21,6 +23,12 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i
|
|||||||
return (PlayerInventory) inventory;
|
return (PlayerInventory) inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack[] getStorageContents() {
|
||||||
|
return Arrays.copyOfRange(getContents(), 0, getInventory().items.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getItemInMainHand() {
|
public ItemStack getItemInMainHand() {
|
||||||
return CraftItemStack.asCraftMirror(getInventory().getItemInHand());
|
return CraftItemStack.asCraftMirror(getInventory().getItemInHand());
|
||||||
@ -38,7 +46,9 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setItemInOffHand(ItemStack item) {
|
public void setItemInOffHand(ItemStack item) {
|
||||||
getInventory().extraSlots[0] = CraftItemStack.asNMSCopy(item);
|
ItemStack[] extra = getExtraContents();
|
||||||
|
extra[0] = item;
|
||||||
|
setExtraContents(extra);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -135,35 +145,49 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack[] getArmorContents() {
|
public ItemStack[] getArmorContents() {
|
||||||
net.minecraft.server.ItemStack[] mcItems = getInventory().getArmorContents();
|
int start = getInventory().items.length;
|
||||||
ItemStack[] ret = new ItemStack[mcItems.length];
|
return Arrays.copyOfRange(getContents(), start, start + getInventory().armor.length);
|
||||||
|
|
||||||
for (int i = 0; i < mcItems.length; i++) {
|
|
||||||
ret[i] = CraftItemStack.asCraftMirror(mcItems[i]);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setArmorContents(ItemStack[] items) {
|
private void setSlots(ItemStack[] items, int baseSlot, int length) {
|
||||||
int cnt = getInventory().items.length;
|
|
||||||
|
|
||||||
if (items == null) {
|
if (items == null) {
|
||||||
items = new ItemStack[4];
|
items = new ItemStack[length];
|
||||||
}
|
}
|
||||||
for (ItemStack item : items) {
|
Preconditions.checkArgument(items.length <= length, "items.length must be < %s", length);
|
||||||
if (item == null || item.getTypeId() == 0) {
|
|
||||||
clear(cnt++);
|
for (int i = 0; i < length; i++) {
|
||||||
|
if (i >= items.length) {
|
||||||
|
setItem(baseSlot + i, null);
|
||||||
} else {
|
} else {
|
||||||
setItem(cnt++, item);
|
setItem(baseSlot + i, items[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException {
|
||||||
|
setSlots(items, 0, getInventory().items.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setArmorContents(ItemStack[] items) {
|
||||||
|
setSlots(items, getInventory().items.length, getInventory().armor.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack[] getExtraContents() {
|
||||||
|
int start = getInventory().items.length + getInventory().armor.length;
|
||||||
|
return Arrays.copyOfRange(getContents(), start, start + getInventory().extraSlots.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setExtraContents(ItemStack[] items) {
|
||||||
|
setSlots(items, getInventory().items.length + getInventory().armor.length, getInventory().extraSlots.length);
|
||||||
|
}
|
||||||
|
|
||||||
public int clear(int id, int data) {
|
public int clear(int id, int data) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
ItemStack[] items = getContents();
|
ItemStack[] items = getContents();
|
||||||
ItemStack[] armor = getArmorContents();
|
|
||||||
int armorSlot = getSize();
|
|
||||||
|
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
ItemStack item = items[i];
|
ItemStack item = items[i];
|
||||||
@ -175,14 +199,6 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i
|
|||||||
setItem(i, null);
|
setItem(i, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ItemStack item : armor) {
|
|
||||||
if (item == null) continue;
|
|
||||||
if (id > -1 && item.getTypeId() != id) continue;
|
|
||||||
if (data > -1 && item.getData().getData() != data) continue;
|
|
||||||
|
|
||||||
count += item.getAmount();
|
|
||||||
setItem(armorSlot++, null);
|
|
||||||
}
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren