private FurnaceState GetState(IWorld world, Coordinates3D coords) { var tileEntity = world.GetTileEntity(coords); if (tileEntity == null) { tileEntity = CreateTileEntity(); } var burnTime = tileEntity.Get <NbtShort>("BurnTime"); var burnTotal = tileEntity.Get <NbtShort>("BurnTotal"); var cookTime = tileEntity.Get <NbtShort>("CookTime"); var state = new FurnaceState { BurnTimeTotal = burnTotal == null ? (short)0 : burnTotal.Value, BurnTimeRemaining = burnTime == null ? (short)0 : burnTime.Value, CookTime = cookTime == null ? (short)200 : cookTime.Value }; var items = tileEntity.Get <NbtList>("Items"); if (items != null) { var i = 0; foreach (var item in items) { state.Items[i++] = ItemStack.FromNbt((NbtCompound)item); } } return(state); }
public override void BlockMined(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) { var entity = world.GetTileEntity(descriptor.Coordinates); if (entity != null) { foreach (var item in (NbtList)entity["Items"]) { var manager = user.Server.GetEntityManagerForWorld(world); var slot = ItemStack.FromNbt((NbtCompound)item); manager.SpawnEntity(new ItemEntity(descriptor.Coordinates + new Vector3(0.5), slot)); } world.SetTileEntity(descriptor.Coordinates, null); } base.BlockMined(descriptor, face, world, user); }
public override bool BlockRightClicked(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) { var adjacent = -Coordinates3D.One; // -1, no adjacent chest var self = descriptor.Coordinates; for (int i = 0; i < AdjacentBlocks.Length; i++) { var test = self + AdjacentBlocks[i]; if (world.GetBlockID(test) == ChestBlock.BlockID) { adjacent = test; var up = world.BlockRepository.GetBlockProvider(world.GetBlockID(test + Coordinates3D.Up)); if (up.Opaque) { return(false); // Obstructed } break; } } var upSelf = world.BlockRepository.GetBlockProvider(world.GetBlockID(self + Coordinates3D.Up)); if (upSelf.Opaque) { return(false); // Obstructed } if (adjacent != -Coordinates3D.One) { // Ensure that chests are always opened in the same arrangement if (adjacent.X < self.X || adjacent.Z < self.Z) { var _ = adjacent; adjacent = self; self = _; // Swap } } var window = new ChestWindow((InventoryWindow)user.Inventory, adjacent != -Coordinates3D.One); // Add items var entity = world.GetTileEntity(self); if (entity != null) { foreach (var item in (NbtList)entity["Items"]) { var slot = ItemStack.FromNbt((NbtCompound)item); window.ChestInventory[slot.Index] = slot; } } // Add adjacent items if (adjacent != -Coordinates3D.One) { entity = world.GetTileEntity(adjacent); if (entity != null) { foreach (var item in (NbtList)entity["Items"]) { var slot = ItemStack.FromNbt((NbtCompound)item); window.ChestInventory[slot.Index + ChestWindow.DoubleChestSecondaryIndex] = slot; } } } window.WindowChange += (sender, e) => { var entitySelf = new NbtList("Items", NbtTagType.Compound); var entityAdjacent = new NbtList("Items", NbtTagType.Compound); for (int i = 0; i < window.ChestInventory.Items.Length; i++) { var item = window.ChestInventory.Items[i]; if (!item.Empty) { if (i < ChestWindow.DoubleChestSecondaryIndex) { item.Index = i; entitySelf.Add(item.ToNbt()); } else { item.Index = i - ChestWindow.DoubleChestSecondaryIndex; entityAdjacent.Add(item.ToNbt()); } } } var newEntity = world.GetTileEntity(self); if (newEntity == null) { newEntity = new NbtCompound(new[] { entitySelf }); } else { newEntity["Items"] = entitySelf; } world.SetTileEntity(self, newEntity); if (adjacent != -Coordinates3D.One) { newEntity = world.GetTileEntity(adjacent); if (newEntity == null) { newEntity = new NbtCompound(new[] { entityAdjacent }); } else { newEntity["Items"] = entityAdjacent; } world.SetTileEntity(adjacent, newEntity); } }; user.OpenWindow(window); return(false); }
public PlayerEntity LoadPlayer(string name) { PlayerEntity entity = new PlayerEntity(Difficulty); if (LevelDirectory == null || !File.Exists(Path.Combine(LevelDirectory, "players", name + ".dat"))) { // Return default player entity entity.Position = SpawnPoint; entity.SpawnPoint = SpawnPoint + new Vector3(0, PlayerEntity.Height, 0); entity.Position += new Vector3(0, PlayerEntity.Height, 0); entity.GameMode = GameMode; return(entity); } var file = new NbtFile(); using (Stream stream = File.Open(Path.Combine(LevelDirectory, "players", name + ".dat"), FileMode.Open)) file.LoadFromStream(stream, NbtCompression.GZip, null); var data = file.RootTag; entity.OnGround = data.Get <NbtByte>("OnGround").Value == 1; entity.Air = data.Get <NbtShort>("Air").Value; entity.Health = data.Get <NbtShort>("Health").Value; var dimension = (Dimension)data.Get <NbtInt>("Dimension").Value; // TODO entity.Food = (short)data.Get <NbtInt>("foodLevel").Value; entity.XpLevel = data.Get <NbtInt>("XpLevel").Value; entity.XpTotal = data.Get <NbtInt>("XpTotal").Value; // TODO: Set velocity based on fall distance entity.FoodExhaustion = data.Get <NbtFloat>("foodExhaustionLevel").Value; entity.FoodSaturation = data.Get <NbtFloat>("foodSaturationLevel").Value; entity.XpProgress = data.Get <NbtFloat>("XpP").Value; var equipment = data.Get <NbtList>("Equipment"); var inventory = data.Get <NbtList>("Inventory"); var motion = data.Get <NbtList>("Motion"); var pos = data.Get <NbtList>("Pos"); var rotation = data.Get <NbtList>("Rotation"); var abilities = data.Get <NbtCompound>("abilities"); // Appears to be unused, is overriden by the inventory contents // foreach (var item in equipment.Tags) foreach (var item in inventory) { var slot = ItemStack.FromNbt((NbtCompound)item); slot.Index = DataSlotToNetworkSlot(slot.Index); entity.Inventory[slot.Index] = slot; } entity.Velocity = new Vector3( ((NbtDouble)motion[0]).Value, ((NbtDouble)motion[1]).Value, ((NbtDouble)motion[2]).Value); entity.Position = new Vector3( ((NbtDouble)pos[0]).Value, ((NbtDouble)pos[1]).Value + PlayerEntity.Height, ((NbtDouble)pos[2]).Value); if (data.Get <NbtInt>("SpawnX") != null) { entity.SpawnPoint = new Vector3( data.Get <NbtInt>("SpawnX").Value, data.Get <NbtInt>("SpawnY").Value, data.Get <NbtInt>("SpawnZ").Value); } else { entity.SpawnPoint = SpawnPoint + new Vector3(0, PlayerEntity.Height, 0); } entity.Yaw = ((NbtFloat)rotation[0]).Value; entity.Pitch = ((NbtFloat)rotation[1]).Value; // TODO: Abilities return(entity); }