public static Dictionary <string, ItemStack> GetSlots(IItemStack stack, IWorldAccessor world) { Dictionary <string, ItemStack> slots = null; IModularItem tool = stack.Item as IModularItem; if (tool != null) { ITreeAttribute ToolSlots = stack.Attributes.GetOrAddTreeAttribute("toolparts"); slots = new Dictionary <string, ItemStack>(); foreach (SlotDefinition slot in GetSlotDefinitions(tool)) { ItemStack partstack = ToolSlots.GetItemstack(slot.SlotName); if (partstack != null) { partstack.ResolveBlockOrItem(world); } slots[slot.SlotName] = partstack; } } return(slots); }
public static void RecalculateAttributes(IItemStack parent, IWorldAccessor world) { int durability = 0; int maxdurability = 0; ITreeAttribute ToolSlots = parent.Attributes.GetTreeAttribute("toolparts"); foreach (KeyValuePair <string, IAttribute> pair in ToolSlots) { ItemStack stack = pair.Value.GetValue() as ItemStack; stack.ResolveBlockOrItem(world); IModularItem item = (IModularItem)stack.Item; durability += stack.Attributes.GetInt("durability", item != null ? item.GetDurability(stack) : 0); maxdurability += stack.Attributes.GetInt("maxdurability", item != null ? item.GetDurability(stack) : 0); } parent.Attributes.SetInt("maxdurability", maxdurability); if (durability != maxdurability) { parent.Attributes.SetInt("durability", durability); } else { parent.Attributes.RemoveAttribute("durability"); } }
public static bool AddPart(IItemStack parent, string slotName, ItemStack partstack) { IModularItem item = parent.Item as IModularItem; IToolPart part = partstack.Item as IToolPart; if (item != null && part != null) { SlotDefinition[] slotdefs = GetSlotDefinitions(item); SlotDefinition slot = slotdefs?.First(s => s.SlotName == slotName); if (slot?.ValidPartTypes?.Any(type => part.TinkerProps.PartType == type) == true) { return(ApplyPart(parent, slotName, partstack)); } } return(false); }
private static string BuildMeshId(ClientMain game, ItemStack stack) { string result = stack.Item.Code.ToString().Replace(':', '.'); IModularItem item = stack.Item as IModularItem; if (item != null) { var slots = ModularItemHelper.GetSlots(stack, game); foreach (var slot in slots) { if (slot.Value != null) { result = result + "." + BuildMeshId(game, slot.Value); } } } return(result); }
public static ItemStack RemovePart(IItemStack parent, string slot) { IModularItem item = parent.Item as IModularItem; if (item != null) { ITreeAttribute ToolSlots = parent.Attributes.GetOrAddTreeAttribute("toolparts"); ItemStack partstack = ToolSlots.GetItemstack(slot); ToolSlots.RemoveAttribute(slot); if (partstack != null) { item.RecalculateAttributes(parent); } return(partstack); } return(null); }
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); } } } } }
public void SetupDialog() { ItemSlot hoveredSlot = capi.World.Player.InventoryManager.CurrentHoveredSlot; if (hoveredSlot != null && hoveredSlot.Inventory == Inventory) { capi.Input.TriggerOnMouseLeaveSlot(hoveredSlot); } else { hoveredSlot = null; } ElementBounds quernBounds = ElementBounds.Fixed(0, 0, 400, 400); // 2. Around all that is 10 pixel padding ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding); bgBounds.BothSizing = ElementSizing.FitToChildren; bgBounds.WithChildren(quernBounds); // 3. Finally Dialog ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle) .WithFixedAlignmentOffset(-GuiStyle.DialogToScreenPadding, 0); ClearComposers(); SingleComposer = capi.Gui .CreateCompo("BlockEntityTinkerTable" + BlockEntityPosition, dialogBounds) .AddShadedDialogBG(bgBounds) .AddDialogTitleBar(DialogTitle, OnTitleBarClose) .BeginChildElements(bgBounds) .AddItemSlotGrid(Inventory, SendInvPacket, 1, new int[] { 0 }, ElementStdBounds.SlotGrid(EnumDialogArea.CenterMiddle, 0, 0, 1, 1), "targetSlot") ; IModularItem item = Inventory[0].Itemstack?.Item as IModularItem; SlotDefinition[] slotdefs = item?.TinkerProps?.AvailableSlots; if (item != null && slotdefs != null) { var slots = item.GetSlots(Inventory[0].Itemstack); for (int i = 0; i < slotdefs.Length && i < Inventory.Count - 1; ++i) { var slotdef = slotdefs[i]; ItemStack stack = slots[slotdef.SlotName]; int invslot = i + 1; if (stack == null) { stack = Inventory[invslot].Itemstack; } Inventory[invslot].Itemstack = stack; SingleComposer = SingleComposer.AddItemSlotGrid(Inventory, SendInvPacket, 1, new int[] { invslot }, ElementStdBounds.SlotGrid(EnumDialogArea.CenterMiddle, slotdef.OffsetX, slotdef.OffsetY, 1, 1), "partSlot" + invslot.ToString()); } } else if (Inventory[0].Itemstack?.Item == null && Inventory.Count >= 4) { SingleComposer = SingleComposer .AddItemSlotGrid(Inventory, SendInvPacket, 1, new int[] { 1 }, ElementStdBounds.SlotGrid(EnumDialogArea.CenterMiddle, 0, -90.0, 1, 1), "partSlot1"); SingleComposer = SingleComposer .AddItemSlotGrid(Inventory, SendInvPacket, 1, new int[] { 3 }, ElementStdBounds.SlotGrid(EnumDialogArea.CenterMiddle, 0, 90.0, 1, 1), "partSlot2"); } SingleComposer = SingleComposer.Compose(); if (hoveredSlot != null) { SingleComposer.OnMouseMove(new MouseEvent(capi.Input.MouseX, capi.Input.MouseY)); } }
public static SlotDefinition[] GetSlotDefinitions(IModularItem item) { return(item.TinkerProps.AvailableSlots); }