protected internal virtual int MoveOrMergeItem(int index, Slot slot, WindowArea from) { int emptyIndex = -1; for (int i = 0; i < Length; i++) { if (this[i].Empty && emptyIndex == -1) emptyIndex = i; else if (this[i].Id == slot.Id && this[i].Metadata == slot.Metadata && this[i].Count < slot.Item.MaximumStack) { // Merging takes precedence over empty slots emptyIndex = -1; if (from != null) from[index] = new Slot(); if (this[i].Count + slot.Count > slot.Item.MaximumStack) { slot.Count -= (byte)(slot.Item.MaximumStack - this[i].Count); this[i].Count = slot.Item.MaximumStack; continue; } this[i] = new Slot(slot.Id, (byte)(this[i].Count + slot.Count)); return i; } } if (emptyIndex != -1) { if (from != null) from[index] = new Slot(); this[emptyIndex] = slot; } return emptyIndex; }
public EntityEquipmentPacket(int entityId, EntityEquipmentSlot slot, Slot item) { EntityId = entityId; SlotIndex = (short)slot; Item = item; if (slot == 0 && item.Id != 0xFFFF) item.Id = 0xFFFF; }
public Slot[] GetSlots() { int length = WindowAreas.Sum(area => area.Length); var slots = new Slot[length]; foreach (var windowArea in WindowAreas) Array.Copy(windowArea.Items, 0, slots, windowArea.StartIndex, windowArea.Length); return slots; }
public WindowArea(int startIndex, int length) { StartIndex = startIndex; Length = length; Items = new Slot[Length]; for (int i = 0; i < Items.Length; i++) Items[i] = new Slot(); }
public SetSlotPacket(byte windowId, short index, Slot slot) { WindowId = windowId; Index = index; Slot = slot; }
public SetWindowItemsPacket(byte windowId, Slot[] slots) { WindowId = windowId; Slots = slots; }
/// <summary> /// Returns true if the specified slot is valid to /// be placed in this index. /// </summary> protected virtual bool IsValid(Slot slot, int index) { return true; }
public EntityEquipmentPacket(int entityId, EntityEquipmentSlot slot, Slot item) { EntityId = entityId; SlotIndex = (short)slot; Item = item; }
public static bool TryReadSlot(byte[] buffer, ref int offset, out Slot slot) { slot = new Slot(); if (!DataUtility.TryReadUInt16(buffer, ref offset, out slot.id)) return false; if (slot.Id == 0xFFFF) return true; if (!DataUtility.TryReadByte(buffer, ref offset, out slot.count)) return false; if (!DataUtility.TryReadUInt16(buffer, ref offset, out slot.metadata)) return false; short length = 0; if (!DataUtility.TryReadInt16(buffer, ref offset, out length)) return false; if (length == -1) return true; byte[] compressed; if (!DataUtility.TryReadArray(buffer, length, ref offset, out compressed)) return false; if (length != -1) { var output = new MemoryStream(); var gzs = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress, false); gzs.CopyTo(output); gzs.Close(); slot.Nbt = new NbtFile(); slot.Nbt.LoadFile(output, false); } return true; }
/// <summary> /// Reads a slot from the given stream. /// </summary> /// <param name="stream">The stream to read from.</param> /// <returns></returns> /// <remarks></remarks> public static Slot ReadSlot(Stream stream) { var s = new Slot(); s.Id = ReadUShort(stream); if (s.Id == 0xFFFF) return s; s.Count = (byte)stream.ReadByte(); s.Metadata = ReadUShort(stream); short length = ReadShort(stream); if (length != -1) { var compressed = new byte[length]; stream.Read(compressed, 0, length); var output = new MemoryStream(); var gzs = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress, false); gzs.CopyTo(output); gzs.Close(); s.Nbt = new NbtFile(); s.Nbt.LoadFile(output, false); } return s; }
public static Slot FromNbt(NbtCompound compound) { var s = new Slot(); s.Id = (ushort)compound.Get<NbtShort>("id").Value; s.Metadata = (ushort)compound.Get<NbtShort>("Damage").Value; s.Count = compound.Get<NbtByte>("Count").Value; s.Index = compound.Get<NbtByte>("Slot").Value; if (compound.Get<NbtCompound>("tag") != null) { s.Nbt = new NbtFile(); s.Nbt.RootTag = compound.Get<NbtCompound>("tag"); } return s; }
public static Slot FromNbt(NbtCompound compound) { var s = new Slot(); s.Id = (ushort)compound.Get<NbtShort>("id").Value; s.Metadata = (ushort)compound.Get<NbtShort>("Damage").Value; s.Count = compound.Get<NbtByte>("Count").Value; s.Index = compound.Get<NbtByte>("Slot").Value; return s; }
/// <summary> /// When shift-clicking items between areas, this method is used /// to determine which area links to which. /// </summary> /// <param name="index">The index of the area the item is coming from</param> /// <param name="slot">The item being moved</param> /// <returns>The area to place the item into</returns> protected abstract WindowArea GetLinkedArea(int index, Slot slot);
public virtual bool GetDrop(ToolItem tool, out Slot[] drop) { drop = new[] { new Slot(this.Id, 1, this.Metadata) }; return CanHarvest(tool); }