internal void SaveDeathContent(InventoryGeneric inventory, IPlayer player) { ICoreAPI api = player.Entity?.Api; if (api == null) { throw new NullReferenceException("player.Entity.api is null"); } string datapath = api.GetOrCreateDataPath($"ModData/{api.GetWorldId()}/{ConstantsCore.ModId}/{player.PlayerUID}"); string[] files = Directory.GetFiles(datapath).OrderByDescending(f => new FileInfo(f).Name).ToArray(); for (int i = files.Count() - 1; i > Config.Current.MaxDeathContentSavedPerPlayer.Val - 2; i--) { File.Delete(files[i]); } TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); string name = "inventory-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".dat"; File.WriteAllBytes($"{datapath}/{name}", tree.ToBytes()); }
public override bool OnPlayerRightClick(IPlayer byPlayer, BlockSelection blockSel) { if (blockSel.SelectionBoxIndex == 1) { return(false); } if (api.World is IServerWorldAccessor) { byte[] data; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); writer.Write("BEMortarAndPestle"); writer.Write(DialogTitle); TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); tree.ToBytes(writer); data = ms.ToArray(); } ((ICoreServerAPI)api).Network.SendBlockEntityPacket( (IServerPlayer)byPlayer, pos.X, pos.Y, pos.Z, (int)EnumBlockStovePacket.OpenGUI, data ); byPlayer.InventoryManager.OpenInventory(inventory); } return(true); }
/// <summary> /// Serializes the itemstack into a series of bytes, including its stack attributes /// </summary> /// <param name="stream"></param> public void ToBytes(BinaryWriter stream) { stream.Write((int)Class); stream.Write(Id); stream.Write(stacksize); stackAttributes.ToBytes(stream); }
public override bool OnPlayerRightClick(IPlayer byPlayer, BlockSelection blockSel) { if (api.World is IServerWorldAccessor) { byte[] data; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); writer.Write("BlockEntityInventory"); writer.Write(Lang.Get(dialogTitleLangCode)); writer.Write((byte)4); TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); tree.ToBytes(writer); data = ms.ToArray(); } ((ICoreServerAPI)api).Network.SendBlockEntityPacket( (IServerPlayer)byPlayer, pos.X, pos.Y, pos.Z, (int)EnumBlockContainerPacketId.OpenInventory, data ); byPlayer.InventoryManager.OpenInventory(inventory); } return(true); }
public override void ToBytes(BinaryWriter writer, bool forClient) { base.ToBytes(writer, forClient); writer.Write(initialPos.X); writer.Write(initialPos.Y); writer.Write(initialPos.Z); writer.Write(blockCode.ToShortString()); writer.Write(blockEntityAttributes == null); if (blockEntityAttributes != null) { blockEntityAttributes.ToBytes(writer); writer.Write(blockEntityClass); } }
public static byte[] InventoryByteBuilder(IInventory incomingInventory, string blockName, string dialogTitle) { byte[] byteInventory; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); writer.Write(blockName); writer.Write(dialogTitle); writer.Write((byte)4); TreeAttribute tree = new TreeAttribute(); (incomingInventory as InventoryGeneric).ToTreeAttributes(tree); tree.ToBytes(writer); byteInventory = ms.ToArray(); } return(byteInventory); }
public override bool OnPlayerRightClick(IPlayer byPlayer, BlockSelection blockSel) { if (byPlayer.WorldData.CurrentGameMode == EnumGameMode.Creative) { inventory.PutLocked = false; } if (inventory.PutLocked && inventory.Empty) { return(false); } if (Api.World is IServerWorldAccessor) { byte[] data; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); writer.Write("BlockEntityInventory"); writer.Write(DialogTitle); writer.Write((byte)quantityColumns); TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); tree.ToBytes(writer); data = ms.ToArray(); } ((ICoreServerAPI)Api).Network.SendBlockEntityPacket( (IServerPlayer)byPlayer, Pos.X, Pos.Y, Pos.Z, (int)EnumBlockContainerPacketId.OpenInventory, data ); byPlayer.InventoryManager.OpenInventory(inventory); } return(true); }
public void SaveDeathContent(InventoryGeneric inventory, IPlayer player) { ICoreAPI api = player.Entity.Api; string localPath = "ModData" + api.GetWorldId() + "/" + Mod.Info.ModID + "/" + player.PlayerUID; string path = api.GetOrCreateDataPath(localPath); string[] files = Directory.GetFiles(path).OrderByDescending(f => new FileInfo(f).Name).ToArray(); for (int i = files.Length - 1; i > Config.Current.MaxDeathContentSavedPerPlayer.Value - 2; i--) { File.Delete(files[i]); } TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); string name = "inventory-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".dat"; File.WriteAllBytes(path + "/" + name, tree.ToBytes()); }
public override bool OnPlayerRightClick(IPlayer byPlayer, BlockSelection blockSel) { if (Api.World is IServerWorldAccessor) { byte[] data; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); writer.Write("BlockEntityTinkerTable"); writer.Write(DialogTitle); TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); tree.ToBytes(writer); data = ms.ToArray(); ((ICoreServerAPI)Api).Network.SendBlockEntityPacket((IServerPlayer)byPlayer, Pos.X, Pos.Y, Pos.Z, (int)Packet.OpenGUI, data); byPlayer.InventoryManager.OpenInventory(inventory); } } return(true); }
private void MouthInv_SlotModified(int slotid) { ITreeAttribute tree = new TreeAttribute(); entity.WatchedAttributes["mouthInv"] = tree; entity.WatchedAttributes.MarkPathDirty("mouthInv"); mouthInv.ToTreeAttributes(tree); if (entityAgent.Api is ICoreServerAPI sapi) { sapi.Network.BroadcastEntityPacket(entity.EntityId, 1235, SerializerUtil.ToBytes((w) => tree.ToBytes(w))); } }
private void WorkInv_SlotModified(int slotid) { ITreeAttribute tree = new TreeAttribute(); maintree["workInv"] = tree; entity.WatchedAttributes.MarkPathDirty("reprog"); workInv.ToTreeAttributes(tree); if (entity.Api is ICoreServerAPI sapi) { sapi.Network.BroadcastEntityPacket(entity.EntityId, 2450, SerializerUtil.ToBytes((w) => tree.ToBytes(w))); } }
private void OnSlotModified(int slot) { if (Api.Side == EnumAppSide.Server) { bool dirty = false; if (slot == 0) { if (Inventory[0].Itemstack == null) { DropInvalidParts(); inventory.Resize(4); dirty = true; } else if (Inventory[0].Itemstack != null) { // Drop any items that are not ToolPart into the world DropInvalidParts(); IModularItem item = Inventory[0].Itemstack?.Item as IModularItem; if (item != null) { // Fill part slots SlotDefinition[] slotdefs = item.TinkerProps.AvailableSlots; if (slotdefs != null) { inventory.Resize(slotdefs.Length + 1); var slots = item.GetSlots(Inventory[0].Itemstack); for (int i = 0; i < slotdefs.Length; ++i) { var toolpart = slots[slotdefs[i].SlotName]; Inventory[i + 1].Itemstack = toolpart; } } else { DropParts(); inventory.Resize(1); } } else { // Not a valid IModularItem, so no slots DropParts(); inventory.Resize(1); } dirty = true; } } else { if (Inventory[0].Itemstack != null) { int partindex = slot - 1; IModularItem item = Inventory[0].Itemstack.Item as IModularItem; SlotDefinition[] slotdefs = item.TinkerProps.AvailableSlots; // Only apply the part if it inside the number of available slots if (partindex < slotdefs?.Length) { SlotDefinition slotdef = slotdefs[partindex]; item.RemovePart(Inventory[0].Itemstack, slotdef.SlotName); ToolPart part = Inventory[slot].Itemstack?.Item as ToolPart; if (part != null) { item.AddPart(Inventory[0].Itemstack, slotdef.SlotName, Inventory[slot].Itemstack); } } if (!item.HasNeededParts(Inventory[0].Itemstack)) { DropOptionalParts(); Inventory[0].Itemstack = null; } } else { ToolPart toolhead = Inventory[1].Itemstack?.Item as ToolPart; ToolPart handle = Inventory[3].Itemstack?.Item as ToolPart; if (toolhead != null && handle != null && toolhead.TinkerProps.ResultItem != null) { IModularTool result = Api.World.GetItem(new AssetLocation(toolhead.TinkerProps.ResultItem)) as IModularTool; if (result != null) { Inventory[0].Itemstack = new ItemStack(result as Item, 1); if (!(result.AddPart(Inventory[0].Itemstack, "toolhead", Inventory[1].Itemstack) && result.AddPart(Inventory[0].Itemstack, "handle", Inventory[3].Itemstack))) { Inventory[0].Itemstack = null; } } } } dirty = true; } if (lastStack?.Item == null || lastStack.Item is IModularItem) { lastStack = Inventory[0].Itemstack; } // Resend the inventory if needed if (dirty) { byte[] data; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); TreeAttribute tree = new TreeAttribute(); inventory.ToTreeAttributes(tree); tree.ToBytes(writer); data = ms.ToArray(); } foreach (string guid in Inventory.openedByPlayerGUIds) { IServerPlayer player = Api.World.PlayerByUid(guid) as IServerPlayer; // Make sure that only online players recieve the update if (player.ConnectionState != EnumClientState.Offline) { UpdateInventory(player, data); } } } } }
protected void SyncToNetworkInventory() { TreeAttribute tree = new TreeAttribute(); Inventory.ToTreeAttributes(tree); Api.ModLoader.GetModSystem <TradeRoutesSystem>().TradeRoutesHandler.SyncInventories(this.blockEnityId, this.networkId, tree.ToBytes()); }
public override void OnReceivedClientPacket(IServerPlayer player, int packetid, byte[] data) { if (packetid < 1000) { Inventory.InvNetworkUtil.HandleClientPacket(player, packetid, data); return; } if (packetid == 1000) { EnumTransactionResult result = Inventory.TryBuySell(player); if (result == EnumTransactionResult.Success) { (Api as ICoreServerAPI).WorldManager.GetChunk(ServerPos.AsBlockPos)?.MarkModified(); AnimManager.StopAnimation("idle"); AnimManager.StartAnimation(new AnimationMetaData() { Animation = "nod", Code = "nod", Weight = 10, EaseOutSpeed = 10000, EaseInSpeed = 10000 }); TreeAttribute tree = new TreeAttribute(); Inventory.ToTreeAttributes(tree); (Api as ICoreServerAPI).Network.BroadcastEntityPacket(EntityId, 1234, tree.ToBytes()); } } }