mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-28 23:38:25 +01:00
#1023: Convert InventoryView to interface
By: Miles Holder <mwholder2005@gmail.com>
This commit is contained in:
parent
d7e28ba2f2
commit
dbc554a91b
1 changed files with 18 additions and 194 deletions
|
@ -1,6 +1,5 @@
|
|||
package org.bukkit.inventory;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -14,7 +13,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
* contracts of certain methods, there's no guarantee that the game will work
|
||||
* as it should.
|
||||
*/
|
||||
public abstract class InventoryView {
|
||||
public interface InventoryView {
|
||||
public static final int OUTSIDE = -999;
|
||||
/**
|
||||
* Represents various extra properties of certain inventory windows.
|
||||
|
@ -139,7 +138,7 @@ public abstract class InventoryView {
|
|||
* @return the inventory
|
||||
*/
|
||||
@NotNull
|
||||
public abstract Inventory getTopInventory();
|
||||
public Inventory getTopInventory();
|
||||
|
||||
/**
|
||||
* Get the lower inventory involved in this transaction.
|
||||
|
@ -147,7 +146,7 @@ public abstract class InventoryView {
|
|||
* @return the inventory
|
||||
*/
|
||||
@NotNull
|
||||
public abstract Inventory getBottomInventory();
|
||||
public Inventory getBottomInventory();
|
||||
|
||||
/**
|
||||
* Get the player viewing.
|
||||
|
@ -155,7 +154,7 @@ public abstract class InventoryView {
|
|||
* @return the player
|
||||
*/
|
||||
@NotNull
|
||||
public abstract HumanEntity getPlayer();
|
||||
public HumanEntity getPlayer();
|
||||
|
||||
/**
|
||||
* Determine the type of inventory involved in the transaction. This
|
||||
|
@ -165,7 +164,7 @@ public abstract class InventoryView {
|
|||
* @return the inventory type
|
||||
*/
|
||||
@NotNull
|
||||
public abstract InventoryType getType();
|
||||
public InventoryType getType();
|
||||
|
||||
/**
|
||||
* Sets one item in this inventory view by its raw slot ID.
|
||||
|
@ -176,14 +175,7 @@ public abstract class InventoryView {
|
|||
* @param slot The ID as returned by InventoryClickEvent.getRawSlot()
|
||||
* @param item The new item to put in the slot, or null to clear it.
|
||||
*/
|
||||
public void setItem(int slot, @Nullable ItemStack item) {
|
||||
Inventory inventory = getInventory(slot);
|
||||
if (inventory != null) {
|
||||
inventory.setItem(convertSlot(slot), item);
|
||||
} else if (item != null) {
|
||||
getPlayer().getWorld().dropItemNaturally(getPlayer().getLocation(), item);
|
||||
}
|
||||
}
|
||||
public void setItem(int slot, @Nullable ItemStack item);
|
||||
|
||||
/**
|
||||
* Gets one item in this inventory view by its raw slot ID.
|
||||
|
@ -192,10 +184,7 @@ public abstract class InventoryView {
|
|||
* @return The item currently in the slot.
|
||||
*/
|
||||
@Nullable
|
||||
public ItemStack getItem(int slot) {
|
||||
Inventory inventory = getInventory(slot);
|
||||
return (inventory == null) ? null : inventory.getItem(convertSlot(slot));
|
||||
}
|
||||
public ItemStack getItem(int slot);
|
||||
|
||||
/**
|
||||
* Sets the item on the cursor of one of the viewing players.
|
||||
|
@ -203,9 +192,7 @@ public abstract class InventoryView {
|
|||
* @param item The item to put on the cursor, or null to remove the item
|
||||
* on their cursor.
|
||||
*/
|
||||
public final void setCursor(@Nullable ItemStack item) {
|
||||
getPlayer().setItemOnCursor(item);
|
||||
}
|
||||
public void setCursor(@Nullable ItemStack item);
|
||||
|
||||
/**
|
||||
* Get the item on the cursor of one of the viewing players.
|
||||
|
@ -214,9 +201,7 @@ public abstract class InventoryView {
|
|||
* one.
|
||||
*/
|
||||
@Nullable
|
||||
public final ItemStack getCursor() {
|
||||
return getPlayer().getItemOnCursor();
|
||||
}
|
||||
public ItemStack getCursor();
|
||||
|
||||
/**
|
||||
* Gets the inventory corresponding to the given raw slot ID.
|
||||
|
@ -231,21 +216,7 @@ public abstract class InventoryView {
|
|||
* @return corresponding inventory, or null
|
||||
*/
|
||||
@Nullable
|
||||
public final Inventory getInventory(int rawSlot) {
|
||||
// Slot may be -1 if not properly detected due to client bug
|
||||
// e.g. dropping an item into part of the enchantment list section of an enchanting table
|
||||
if (rawSlot == OUTSIDE || rawSlot == -1) {
|
||||
return null;
|
||||
}
|
||||
Preconditions.checkArgument(rawSlot >= 0, "Negative, non outside slot %s", rawSlot);
|
||||
Preconditions.checkArgument(rawSlot < countSlots(), "Slot %s greater than inventory slot count", rawSlot);
|
||||
|
||||
if (rawSlot < getTopInventory().getSize()) {
|
||||
return getTopInventory();
|
||||
} else {
|
||||
return getBottomInventory();
|
||||
}
|
||||
}
|
||||
public Inventory getInventory(int rawSlot);
|
||||
|
||||
/**
|
||||
* Converts a raw slot ID into its local slot ID into whichever of the two
|
||||
|
@ -259,69 +230,7 @@ public abstract class InventoryView {
|
|||
* @param rawSlot The raw slot ID.
|
||||
* @return The converted slot ID.
|
||||
*/
|
||||
public final int convertSlot(int rawSlot) {
|
||||
int numInTop = getTopInventory().getSize();
|
||||
// Index from the top inventory as having slots from [0,size]
|
||||
if (rawSlot < numInTop) {
|
||||
return rawSlot;
|
||||
}
|
||||
|
||||
// Move down the slot index by the top size
|
||||
int slot = rawSlot - numInTop;
|
||||
|
||||
// Player crafting slots are indexed differently. The matrix is caught by the first return.
|
||||
// Creative mode is the same, except that you can't see the crafting slots (but the IDs are still used)
|
||||
if (getType() == InventoryType.CRAFTING || getType() == InventoryType.CREATIVE) {
|
||||
/*
|
||||
* Raw Slots:
|
||||
*
|
||||
* 5 1 2 0
|
||||
* 6 3 4
|
||||
* 7
|
||||
* 8 45
|
||||
* 9 10 11 12 13 14 15 16 17
|
||||
* 18 19 20 21 22 23 24 25 26
|
||||
* 27 28 29 30 31 32 33 34 35
|
||||
* 36 37 38 39 40 41 42 43 44
|
||||
*/
|
||||
|
||||
/*
|
||||
* Converted Slots:
|
||||
*
|
||||
* 39 1 2 0
|
||||
* 38 3 4
|
||||
* 37
|
||||
* 36 40
|
||||
* 9 10 11 12 13 14 15 16 17
|
||||
* 18 19 20 21 22 23 24 25 26
|
||||
* 27 28 29 30 31 32 33 34 35
|
||||
* 0 1 2 3 4 5 6 7 8
|
||||
*/
|
||||
|
||||
if (slot < 4) {
|
||||
// Send [5,8] to [39,36]
|
||||
return 39 - slot;
|
||||
} else if (slot > 39) {
|
||||
// Slot lives in the extra slot section
|
||||
return slot;
|
||||
} else {
|
||||
// Reset index so 9 -> 0
|
||||
slot -= 4;
|
||||
}
|
||||
}
|
||||
|
||||
// 27 = 36 - 9
|
||||
if (slot >= 27) {
|
||||
// Put into hotbar section
|
||||
slot -= 27;
|
||||
} else {
|
||||
// Take out of hotbar section
|
||||
// 9 = 36 - 27
|
||||
slot += 9;
|
||||
}
|
||||
|
||||
return slot;
|
||||
}
|
||||
public int convertSlot(int rawSlot);
|
||||
|
||||
/**
|
||||
* Determine the type of the slot by its raw slot ID.
|
||||
|
@ -333,93 +242,12 @@ public abstract class InventoryView {
|
|||
* @return the slot type
|
||||
*/
|
||||
@NotNull
|
||||
public final InventoryType.SlotType getSlotType(int slot) {
|
||||
InventoryType.SlotType type = InventoryType.SlotType.CONTAINER;
|
||||
if (slot >= 0 && slot < this.getTopInventory().getSize()) {
|
||||
switch (this.getType()) {
|
||||
case BLAST_FURNACE:
|
||||
case FURNACE:
|
||||
case SMOKER:
|
||||
if (slot == 2) {
|
||||
type = InventoryType.SlotType.RESULT;
|
||||
} else if (slot == 1) {
|
||||
type = InventoryType.SlotType.FUEL;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
case BREWING:
|
||||
if (slot == 3) {
|
||||
type = InventoryType.SlotType.FUEL;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
case ENCHANTING:
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
break;
|
||||
case WORKBENCH:
|
||||
case CRAFTING:
|
||||
if (slot == 0) {
|
||||
type = InventoryType.SlotType.RESULT;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
case BEACON:
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
break;
|
||||
case ANVIL:
|
||||
case SMITHING:
|
||||
case CARTOGRAPHY:
|
||||
case GRINDSTONE:
|
||||
case MERCHANT:
|
||||
if (slot == 2) {
|
||||
type = InventoryType.SlotType.RESULT;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
case STONECUTTER:
|
||||
if (slot == 1) {
|
||||
type = InventoryType.SlotType.RESULT;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
case LOOM:
|
||||
case SMITHING_NEW:
|
||||
if (slot == 3) {
|
||||
type = InventoryType.SlotType.RESULT;
|
||||
} else {
|
||||
type = InventoryType.SlotType.CRAFTING;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Nothing to do, it's a CONTAINER slot
|
||||
}
|
||||
} else {
|
||||
if (slot < 0) {
|
||||
type = InventoryType.SlotType.OUTSIDE;
|
||||
} else if (this.getType() == InventoryType.CRAFTING) { // Also includes creative inventory
|
||||
if (slot < 9) {
|
||||
type = InventoryType.SlotType.ARMOR;
|
||||
} else if (slot > 35) {
|
||||
type = InventoryType.SlotType.QUICKBAR;
|
||||
}
|
||||
} else if (slot >= (this.countSlots() - (9 + 4 + 1))) { // Quickbar, Armor, Offhand
|
||||
type = InventoryType.SlotType.QUICKBAR;
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
public InventoryType.SlotType getSlotType(int slot);
|
||||
|
||||
/**
|
||||
* Closes the inventory view.
|
||||
*/
|
||||
public final void close() {
|
||||
getPlayer().closeInventory();
|
||||
}
|
||||
public void close();
|
||||
|
||||
/**
|
||||
* Check the total number of slots in this view, combining the upper and
|
||||
|
@ -430,9 +258,7 @@ public abstract class InventoryView {
|
|||
*
|
||||
* @return The total size
|
||||
*/
|
||||
public final int countSlots() {
|
||||
return getTopInventory().getSize() + getBottomInventory().getSize();
|
||||
}
|
||||
public int countSlots();
|
||||
|
||||
/**
|
||||
* Sets an extra property of this inventory if supported by that
|
||||
|
@ -443,9 +269,7 @@ public abstract class InventoryView {
|
|||
* @return true if the property was updated successfully, false if the
|
||||
* property is not supported by that inventory
|
||||
*/
|
||||
public final boolean setProperty(@NotNull Property prop, int value) {
|
||||
return getPlayer().setWindowProperty(prop, value);
|
||||
}
|
||||
public boolean setProperty(@NotNull Property prop, int value);
|
||||
|
||||
/**
|
||||
* Get the title of this inventory window.
|
||||
|
@ -453,7 +277,7 @@ public abstract class InventoryView {
|
|||
* @return The title.
|
||||
*/
|
||||
@NotNull
|
||||
public abstract String getTitle();
|
||||
public String getTitle();
|
||||
|
||||
/**
|
||||
* Get the original title of this inventory window, before any changes were
|
||||
|
@ -462,7 +286,7 @@ public abstract class InventoryView {
|
|||
* @return the original title
|
||||
*/
|
||||
@NotNull
|
||||
public abstract String getOriginalTitle();
|
||||
public String getOriginalTitle();
|
||||
|
||||
/**
|
||||
* Sets the title of this inventory window to the specified title if the
|
||||
|
@ -474,5 +298,5 @@ public abstract class InventoryView {
|
|||
*
|
||||
* @param title The new title.
|
||||
*/
|
||||
public abstract void setTitle(@NotNull String title);
|
||||
public void setTitle(@NotNull String title);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue