mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-05-01 11:30:57 +00:00
Theres documentation at https://sylvessa.zip/fourkit/ now. And a bunch of other changes. Check the discord server for a more comprehensive list
203 lines
6.4 KiB
C#
203 lines
6.4 KiB
C#
namespace Minecraft.Server.FourKit.Inventory;
|
|
|
|
using System.Runtime.InteropServices;
|
|
using Minecraft.Server.FourKit.Entity;
|
|
|
|
/// <summary>
|
|
/// Represents a player's inventory, including armor slots and the held item.
|
|
/// </summary>
|
|
public class PlayerInventory : Inventory
|
|
{
|
|
private const int INVENTORY_SIZE = 40;
|
|
private const int ARMOR_START = 36;
|
|
private const int QUICKBAR_SIZE = 9;
|
|
|
|
private int _heldItemSlot;
|
|
internal HumanEntity? _holder;
|
|
|
|
internal PlayerInventory()
|
|
: base("Player", InventoryType.PLAYER, INVENTORY_SIZE)
|
|
{
|
|
}
|
|
|
|
protected internal override void EnsureSynced()
|
|
{
|
|
if (_holder == null || NativeBridge.GetPlayerInventory == null)
|
|
return;
|
|
|
|
int entityId = _holder.getEntityId();
|
|
int[] buf = new int[121];
|
|
var gh = GCHandle.Alloc(buf, GCHandleType.Pinned);
|
|
try
|
|
{
|
|
NativeBridge.GetPlayerInventory(entityId, gh.AddrOfPinnedObject());
|
|
}
|
|
finally
|
|
{
|
|
gh.Free();
|
|
}
|
|
|
|
for (int i = 0; i < INVENTORY_SIZE; i++)
|
|
{
|
|
int id = buf[i * 3];
|
|
int count = buf[i * 3 + 1];
|
|
int aux = buf[i * 3 + 2];
|
|
if (id > 0 && count > 0)
|
|
_items[i] = new ItemStack(id, count, (short)aux);
|
|
else
|
|
_items[i] = null;
|
|
}
|
|
_heldItemSlot = buf[120];
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void setItem(int index, ItemStack? item)
|
|
{
|
|
base.setItem(index, item);
|
|
_slotModifiedByPlugin = true;
|
|
if (_holder != null && NativeBridge.SetPlayerInventorySlot != null &&
|
|
index >= 0 && index < INVENTORY_SIZE)
|
|
{
|
|
int id = item?.getTypeId() ?? 0;
|
|
int count = item?.getAmount() ?? 0;
|
|
int aux = item?.getDurability() ?? 0;
|
|
NativeBridge.SetPlayerInventorySlot(_holder.getEntityId(), index, id, count, aux);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all ItemStacks from the armor slots.
|
|
/// </summary>
|
|
/// <returns>An array of ItemStacks for the armor slots.</returns>
|
|
public ItemStack?[] getArmorContents()
|
|
{
|
|
EnsureSynced();
|
|
var armor = new ItemStack?[4];
|
|
for (int i = 0; i < 4; i++)
|
|
armor[i] = _items[ARMOR_START + i];
|
|
return armor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the ItemStack in the helmet slot.
|
|
/// </summary>
|
|
/// <returns>The helmet ItemStack.</returns>
|
|
public ItemStack? getHelmet() => getItem(ARMOR_START + 3);
|
|
|
|
/// <summary>
|
|
/// Gets the ItemStack in the chestplate slot.
|
|
/// </summary>
|
|
/// <returns>The chestplate ItemStack.</returns>
|
|
public ItemStack? getChestplate() => getItem(ARMOR_START + 2);
|
|
|
|
/// <summary>
|
|
/// Gets the ItemStack in the leggings slot.
|
|
/// </summary>
|
|
/// <returns>The leggings ItemStack.</returns>
|
|
public ItemStack? getLeggings() => getItem(ARMOR_START + 1);
|
|
|
|
/// <summary>
|
|
/// Gets the ItemStack in the boots slot.
|
|
/// </summary>
|
|
/// <returns>The boots ItemStack.</returns>
|
|
public ItemStack? getBoots() => getItem(ARMOR_START);
|
|
|
|
/// <summary>
|
|
/// Sets all four armor slots at once.
|
|
/// </summary>
|
|
/// <param name="items">An array of ItemStacks for the armor slots.</param>
|
|
public void setArmorContents(ItemStack?[] items)
|
|
{
|
|
int len = Math.Min(items.Length, 4);
|
|
for (int i = 0; i < len; i++)
|
|
setItem(ARMOR_START + i, items[i]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the helmet slot.
|
|
/// </summary>
|
|
/// <param name="helmet">The ItemStack to set.</param>
|
|
public void setHelmet(ItemStack? helmet) => setItem(ARMOR_START + 3, helmet);
|
|
|
|
/// <summary>
|
|
/// Sets the chestplate slot.
|
|
/// </summary>
|
|
/// <param name="chestplate">The ItemStack to set.</param>
|
|
public void setChestplate(ItemStack? chestplate) => setItem(ARMOR_START + 2, chestplate);
|
|
|
|
/// <summary>
|
|
/// Sets the leggings slot.
|
|
/// </summary>
|
|
/// <param name="leggings">The ItemStack to set.</param>
|
|
public void setLeggings(ItemStack? leggings) => setItem(ARMOR_START + 1, leggings);
|
|
|
|
/// <summary>
|
|
/// Sets the boots slot.
|
|
/// </summary>
|
|
/// <param name="boots">The ItemStack to set.</param>
|
|
public void setBoots(ItemStack? boots) => setItem(ARMOR_START, boots);
|
|
|
|
/// <summary>
|
|
/// Gets the item the player is currently holding.
|
|
/// </summary>
|
|
/// <returns>The held ItemStack.</returns>
|
|
public ItemStack? getItemInHand() => getItem(_heldItemSlot);
|
|
|
|
/// <summary>
|
|
/// Sets the item in the player's hand.
|
|
/// </summary>
|
|
/// <param name="stack">The ItemStack to set.</param>
|
|
public void setItemInHand(ItemStack? stack) => setItem(_heldItemSlot, stack);
|
|
|
|
/// <summary>
|
|
/// Gets the slot number of the currently held item.
|
|
/// </summary>
|
|
/// <returns>The held item slot index (0-8).</returns>
|
|
public int getHeldItemSlot()
|
|
{
|
|
EnsureSynced();
|
|
return _heldItemSlot;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the slot number of the currently held item.
|
|
/// </summary>
|
|
/// <param name="slot">The slot index (0-8).</param>
|
|
public void setHeldItemSlot(int slot)
|
|
{
|
|
if (slot < 0 || slot >= QUICKBAR_SIZE)
|
|
throw new ArgumentException($"Slot must be between 0 and {QUICKBAR_SIZE - 1} inclusive.");
|
|
_heldItemSlot = slot;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all matching items from the inventory. Setting either value
|
|
/// to -1 will skip its check, while setting both to -1 will clear all
|
|
/// items in your inventory unconditionally.
|
|
/// </summary>
|
|
/// <param name="id">The material id to match, or -1 for any.</param>
|
|
/// <param name="data">The data value to match, or -1 for any.</param>
|
|
/// <returns>The number of stacks cleared.</returns>
|
|
public int clear(int id, int data)
|
|
{
|
|
EnsureSynced();
|
|
int count = 0;
|
|
for (int i = 0; i < getSize(); i++)
|
|
{
|
|
var item = _items[i];
|
|
if (item == null) continue;
|
|
if (id != -1 && item.getTypeId() != id) continue;
|
|
if (data != -1 && item.getDurability() != data) continue;
|
|
setItem(i, null);
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the holder of this inventory.
|
|
/// </summary>
|
|
/// <returns>The HumanEntity that owns this inventory.</returns>
|
|
public HumanEntity? getHolder() => _holder;
|
|
}
|