public void SetTileEntity (int x, int y, int z, TileEntity te) { BlockInfoEx info = BlockInfo.BlockTable[_blocks[x, y, z]] as BlockInfoEx; if (info == null) { throw new InvalidOperationException("The given block is of a type that does not support TileEntities."); } if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) { throw new ArgumentException("The TileEntity type is not valid for this block.", "te"); } BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) : new BlockKey(x, y, z); TagNodeCompound oldte; if (_tileEntityTable.TryGetValue(key, out oldte)) { _tileEntities.Remove(oldte); } te.X = key.x; te.Y = key.y; te.Z = key.z; TagNodeCompound tree = te.BuildTree() as TagNodeCompound; _tileEntities.Add(tree); _tileEntityTable[key] = tree; }
public void SetTileEntity(int x, int y, int z, TileEntity te) { BlockInfoEx info = BlockInfo.BlockTable[_blocks[x, y, z]] as BlockInfoEx; if (info == null) { throw new InvalidOperationException("The given block is of a type that does not support TileEntities."); } if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) { throw new ArgumentException("The TileEntity type is not valid for this block.", "te"); } BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) : new BlockKey(x, y, z); TagNodeCompound oldte; if (_tileEntityTable.TryGetValue(key, out oldte)) { _tileEntities.Remove(oldte); } te.X = key.x; te.Y = key.y; te.Z = key.z; TagNodeCompound tree = te.BuildTree() as TagNodeCompound; _tileEntities.Add(tree); _tileEntityTable[key] = tree; }
private static int FixPotions(ChunkRef chunk, int x, int y, int z) { int fixedPotions = 0; TileEntity te = chunk.Blocks.GetTileEntity(x, y, z); switch (te) { case TileEntityChest chest: fixedPotions = FixPotions(ref chest); chunk.Blocks.SetTileEntity(x, y, z, chest); return(fixedPotions); case TileEntityTrap dispenser: fixedPotions = FixPotions(ref dispenser); chunk.Blocks.SetTileEntity(x, y, z, dispenser); return(fixedPotions); case TileEntityBrewingStand brewingStand: fixedPotions = FixPotions(ref brewingStand); chunk.Blocks.SetTileEntity(x, y, z, brewingStand); return(fixedPotions); default: Console.WriteLine("UNIDENTIFIED TILE ENTITY: {0} at {1},{2},{3}", te.GetType(), te.X, te.Y, te.Z); return(0); } }
public static string ToStringBetter([CanBeNull] this TileEntity te) { if (te == null) { return("TileEntity (null)"); } return($"{te.GetType()} ({te.GetTileEntityType()}) [{te.ToWorldPos()}]"); }
private bool ContainsRoof(Tile t, int floor) { if (t) { TileEntity entity = t.GetTileContent(floor); return(entity && entity.GetType() == typeof(Roof)); } return(false); }
/// <summary> /// Deletes the given tile entity from it's chunk and creates a new one based on the tile entity type /// </summary> private void RecreateTileEntity([NotNull] TileEntity tileEntity) { var chunk = tileEntity.GetChunk(); // Prevent further errors on client updates; crucial when removing power item! tileEntity.SetDisableModifiedCheck(true); // Remove corrupt tile entity chunk.RemoveTileEntity(World, tileEntity); // Remove power item var tePowered = tileEntity as TileEntityPowered; var powerItem = tePowered?.GetPowerItem(); if (powerItem != null) { PowerManager.Instance.RemovePowerNode(powerItem); } // Create new tile entity var newTileEntity = TileEntity.Instantiate(tileEntity.GetTileEntityType(), chunk); newTileEntity.localChunkPos = tileEntity.localChunkPos; chunk.AddTileEntity(newTileEntity); // Recreate power item if necessary var newPowered = newTileEntity as TileEntityPowered; if (newPowered != null) { // Restore old PowerItemType and TriggerType values if (tePowered != null) { newPowered.PowerItemType = tePowered.PowerItemType; } // fancy new C#7 syntax, isn't it? :) if (tileEntity is TileEntityPoweredTrigger teTrigger && newPowered is TileEntityPoweredTrigger newTrigger) { newTrigger.TriggerType = teTrigger.TriggerType; } // Create power item according to PowerItemType and TriggerType newPowered.InitializePowerData(); // Wires to the corrupt block are cut and not restored. We could try to reattach everything, but meh... } var newPowerItem = newPowered?.GetPowerItem(); Log.Debug($"[{tileEntity.ToWorldPos()}] Replaced old {tileEntity.GetType()} with new {newTileEntity.GetType()}" + $"{(newPowerItem != null ? " and new power item " + newPowerItem.GetType() : "")}."); }
public static bool InjectItem(Point16 origin, Item item) { TileEntity tEStorageCenter = FindHeart(origin); if (tEStorageCenter == null) { return(false); } if (regexMagicStorage.IsMatch(tEStorageCenter.GetType().Assembly.GetName().Name)) { Common.MagicStorageAdapter.DepositItem(tEStorageCenter, item); return(true); } if (regexMagicStorageExtra.IsMatch(tEStorageCenter.GetType().Assembly.GetName().Name)) { Common.MagicStorageExtraAdapter.DepositItem(tEStorageCenter, item); return(true); } return(false); }
public virtual void emptyBagOnMagicStorage(Player p, int x, int y) { Mod magicStorage = ModLoader.GetMod("MagicStorage"); if (magicStorage != null) { try { if (Main.tile[x, y].frameX % 36 == 18) { x--; } if (Main.tile[x, y].frameY % 36 == 18) { y--; } ModTile t = TileLoader.GetTile(Main.tile[x, y].type); MethodInfo getHeart = (t.GetType()).GetMethod("GetHeart", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(int) }, null); if (getHeart == null) { BagsOfHoldingMod.debugChat("GetHeart() is null. Report to author."); ErrorLogger.Log("GetHeart() is null on " + (Main.netMode == NetmodeID.MultiplayerClient ? "client" : "server")); return; } object[] param = new object[2]; param[0] = x; param[1] = y; TileEntity heart = (TileEntity)getHeart.Invoke(t, param); if (heart == null) { BagsOfHoldingMod.sendChat("This Access does not have an associated Storage Heart."); return; } Type[] depCal = new Type[1]; depCal[0] = typeof(Item); MethodInfo deposit = heart.GetType().GetMethod("DepositItem", BindingFlags.Public | BindingFlags.Instance, null, depCal, null); if (deposit == null) { BagsOfHoldingMod.debugChat("DepositItem(Item) is null. Report to author."); ErrorLogger.Log("DepositItem(Item) is null on " + (Main.netMode == NetmodeID.MultiplayerClient ? "client" : "server")); return; } for (int i = 0; i < order.Count; i++) { if (items.ContainsKey(order[i])) { long remain = items.GetAsLong(order[i]); bool prematureBreak = false; while (remain > 0 && !prematureBreak) { Item itm = getItemFromTag(order[i]); int stackSize = (int)Math.Min(remain, itm.maxStack); itm.stack = stackSize; deposit.Invoke(heart, new object[] { itm }); if (itm.IsAir) { remain -= stackSize; } else { remain -= (stackSize - itm.stack); prematureBreak = true; } } items.Remove(order[i]); if (remain > 0) { items[order[i]] = remain; } } } }catch (Exception e) { BagsOfHoldingMod.debugChat(e.ToString()); } if (Main.netMode == NetmodeID.MultiplayerClient) { ModPacket pack = mod.GetPacket(); pack.Write((byte)2); pack.Write((byte)p.whoAmI); pack.Write((byte)p.selectedItem); TagIO.Write(items, pack); pack.Send(); } } }
private void Update() { bool snapToGrid = snapToGridToggle.isOn; bool rotationSnapping = rotationSnappingToggle.isOn; float rotationEditSensitivity = 1; float.TryParse(rotationSensitivityInput.text, NumberStyles.Any, CultureInfo.InvariantCulture, out rotationEditSensitivity); bool propertiesNeedSaving = false; if (Math.Abs(rotationEditSensitivity - Properties.Instance.DecorationRotationSensitivity) > float.Epsilon) { Properties.Instance.DecorationRotationSensitivity = rotationEditSensitivity; propertiesNeedSaving = true; } if (snapToGrid != Properties.Instance.DecorationSnapToGrid) { Properties.Instance.DecorationSnapToGrid = snapToGrid; propertiesNeedSaving = true; } if (rotationSnapping != Properties.Instance.DecorationRotationSnapping) { Properties.Instance.DecorationRotationSnapping = rotationSnapping; propertiesNeedSaving = true; } if (propertiesNeedSaving) { Properties.Instance.SaveProperties(); } RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { if (ghostObject) { ghostObject.SetActive(false); } return; } DecorationData data = (DecorationData)GuiManager.Instance.ObjectsTree.SelectedValue; bool dataChanged = data != lastFrameData; lastFrameData = data; if (!data) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); GroundMesh groundMesh = raycast.transform.GetComponent <GroundMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); Material ghostMaterial = GraphicsManager.Instance.GhostMaterial; if (dataChanged) { CoroutineManager.Instance.QueueCoroutine(data.Model.CreateOrGetModel(ghostMaterial, OnGhostCreated)); return; } if (!ghostObject) { return; } int targetFloor = LayoutManager.Instance.CurrentCamera.Floor; if (tileEntity && tileEntity.Valid && tileEntity.GetType() == typeof(Floor)) { targetFloor = tileEntity.Floor; } if (data.CenterOnly || data.Tree || data.Bush) { targetFloor = 0; } Map map = GameManager.Instance.Map; if (!placingDecoration) { position = CalculateCorrectedPosition(raycast.point, data, snapToGrid); targetedTile = null; if (overlayMesh) { int tileX = Mathf.FloorToInt(position.x / 4f); int tileY = Mathf.FloorToInt(position.z / 4f); targetedTile = map[tileX, tileY]; position.y = map.GetInterpolatedHeight(position.x, position.z); if (data.Floating) { position.y = Mathf.Max(position.y, 0); } else { float floorHeight = 3f; position.y += targetFloor * floorHeight; } } else if (tileEntity && tileEntity.Valid) { targetedTile = tileEntity.Tile; } } bool canPlaceNewObject = overlayMesh || groundMesh || (tileEntity && tileEntity.Valid && tileEntity.GetType() == typeof(Floor)); if (canPlaceNewObject || placingDecoration) { ghostObject.gameObject.SetActive(true); ghostObject.transform.position = position; } else { ghostObject.gameObject.SetActive(false); } bool placementAllowed = true; Vector2 position2d = new Vector2(position.x, position.z); IEnumerable <Decoration> nearbyDecorations = GetAllNearbyDecorations(targetedTile); foreach (Decoration decoration in nearbyDecorations) { Vector3 decorationPosition3d = decoration.transform.position; Vector2 decorationPosition2d = new Vector2(decorationPosition3d.x, decorationPosition3d.z); float distance = Vector2.Distance(position2d, decorationPosition2d); if (distance < minimumPlacementGap) { placementAllowed = false; break; } } ToggleGhostPropertyBlock(placementAllowed ? allowedGhostPropertyBlock : disabledGhostPropertyBlock); if (Input.GetMouseButtonDown(0) && placementAllowed) { placingDecoration = true; dragStartPos = LayoutManager.Instance.CurrentCamera.MousePosition; } if (Input.GetMouseButton(0) && placingDecoration) { Vector2 dragEndPos = LayoutManager.Instance.CurrentCamera.MousePosition; Vector2 difference = dragEndPos - dragStartPos; rotation = -difference.x * rotationEditSensitivity; if (rotationSnapping) { rotation = Mathf.Round(rotation / 45f) * 45f; } ghostObject.transform.localRotation = Quaternion.Euler(0, rotation, 0); } if (Input.GetMouseButtonUp(0) && placingDecoration) { float decorationPositionX = position.x - targetedTile.X * 4f; float decorationPositionY = position.z - targetedTile.Y * 4f; Vector2 decorationPosition = new Vector2(decorationPositionX, decorationPositionY); targetedTile.SetDecoration(data, decorationPosition, rotation * Mathf.Deg2Rad, targetFloor, data.Floating); map.CommandManager.FinishAction(); placingDecoration = false; ghostObject.transform.localRotation = Quaternion.identity; } if (Input.GetMouseButtonDown(1)) { placingDecoration = false; ghostObject.transform.localRotation = Quaternion.identity; } if (Input.GetMouseButtonDown(1) && !placingDecoration) { IEnumerable <Decoration> decorationsOnTile = targetedTile.GetDecorations(); foreach (Decoration decoration in decorationsOnTile) { targetedTile.SetDecoration(null, decoration.Position, decoration.Rotation, targetFloor); } map.CommandManager.FinishAction(); } }