public void TestBuckets() { World world = new World(new Level(), new FlatlandGenerator()); world.WorldGenerator.Initialize(new Level()); PlayerEntity player = new PlayerEntity(Difficulty.Normal) { GameMode = GameMode.Creative }; Vector3 targetBlock = new Vector3(0, 2, 0); Vector3 alteredBlock = targetBlock + Vector3.Up; world.SetBlock(alteredBlock, new AirBlock()); BucketItem bucket = new BucketItem(); LavaBucketItem lavaBucket = new LavaBucketItem(); WaterBucketItem waterBucket = new WaterBucketItem(); // TODO: Survival tests waterBucket.OnItemUsedOnBlock(world, targetBlock, Vector3.Up, Vector3.Zero, player); Assert.AreEqual(new WaterFlowingBlock(), world.GetBlock(alteredBlock)); bucket.OnItemUsedOnBlock(world, targetBlock, Vector3.Up, Vector3.Zero, player); Assert.AreEqual(new AirBlock(), world.GetBlock(alteredBlock)); lavaBucket.OnItemUsedOnBlock(world, targetBlock, Vector3.Up, Vector3.Zero, player); Assert.AreEqual(new LavaFlowingBlock(), world.GetBlock(alteredBlock)); bucket.OnItemUsedOnBlock(world, targetBlock, Vector3.Up, Vector3.Zero, player); Assert.AreEqual(new AirBlock(), world.GetBlock(alteredBlock)); world.SetBlock(alteredBlock, new BedrockBlock()); bucket.OnItemUsedOnBlock(world, targetBlock, Vector3.Up, Vector3.Zero, player); Assert.AreEqual(new BedrockBlock(), world.GetBlock(alteredBlock)); }
/// <summary> /// Gets a byte representing block direction based on the rotation /// of the entity that placed it. /// </summary> public static Direction DirectionByRotation(PlayerEntity p, Vector3 position, bool invert = false) { // TODO: Figure out some algorithm based on player's look yaw double d = Math.Asin((p.Position.Y - position.Y) / position.DistanceTo(p.Position)); if (d > (Math.PI / 4)) return invert ? (Direction)1 : (Direction)0; if (d < -(Math.PI / 4)) return invert ? (Direction)0 : (Direction)1; return DirectionByRotationFlat(p, invert); }
public PlayerAbilities(PlayerEntity entity) { FirePropertyChanged = false; IsFlying = false; MayFly = false; Invulnerable = false; InstantMine = false; FirePropertyChanged = true; WalkingSpeed = 12; FlyingSpeed = 24; PlayerEntity = entity; }
public MinecraftClient GetClient(PlayerEntity entity) { var clients = server.Clients.Where(c => c.Entity == entity); if (!clients.Any()) return null; return clients.First(); }
private void UpdateGivenPosition(PlayerEntity entity) { // Used to update a player's position based on the one provided by the client. entity.Position = entity.GivenPosition; // Firing an event within the same event's event handler cannot be done in .NET, so // we manually call UpdateEntityPosition UpdateEntityPosition(entity); }
private void SpawnInventoryEntities(PlayerEntity player) { var world = GetEntityWorld(player); foreach (var slot in player.Inventory.GetSlots()) { if (!slot.Empty) { var entity = new ItemEntity(player.Position + new Vector3( MathHelper.Random.NextDouble() * 0.5 - 0.25, MathHelper.Random.NextDouble() * 0.5 - 0.25, MathHelper.Random.NextDouble() * 0.5 - 0.25), slot); SpawnEntity(world, entity); } } }
public virtual int GetHarvestTime(Item item, World world, PlayerEntity entity, out short damage) { int time = GetHarvestTime(item, out damage); var tool = item as ToolItem; if (tool != null) { if (!tool.IsEfficient(this)) { if (entity.IsUnderwater(world) && !entity.IsOnGround(world)) time *= 25; else if (entity.IsUnderwater(world) || !entity.IsOnGround(world)) time *= 5; } } return time; }
public virtual void OnBlockMined(World world, Vector3 destroyedBlock, PlayerEntity player) { if (Hardness != -1) { var slot = player.Inventory[player.SelectedSlot]; world.SetBlock(destroyedBlock, new AirBlock()); if (CanHarvest(slot.AsItem() as ToolItem) && player.GameMode != GameMode.Creative) { ItemStack[] drops; bool spawnEntity = GetDrop(slot.AsItem() as ToolItem, out drops); if (spawnEntity) { foreach (var drop in drops) { var entity = new ItemEntity(destroyedBlock + new Vector3(0.5), drop); entity.ApplyRandomVelocity(); world.OnSpawnEntity(entity); } } } } }
public void SavePlayer(PlayerEntity entity) { // TODO: Generalize to all mobs NbtFile file = new NbtFile(); var data = new NbtCompound(); data.Add(new NbtByte("OnGround", (byte)(entity.OnGround ? 1 : 0))); data.Add(new NbtShort("Air", entity.Air)); data.Add(new NbtShort("Health", entity.Health)); data.Add(new NbtInt("Dimension", 0)); // TODO data.Add(new NbtInt("foodLevel", entity.Food)); data.Add(new NbtInt("XpLevel", entity.XpLevel)); data.Add(new NbtInt("XpTotal", entity.XpTotal)); data.Add(new NbtFloat("foodExhaustionLevel", entity.FoodExhaustion)); data.Add(new NbtFloat("foodSaturationLevel", entity.FoodSaturation)); data.Add(new NbtFloat("XpP", entity.XpProgress)); data.Add(new NbtList("Equipment")); var inventory = new NbtList("Inventory"); for (int index = 0; index < entity.Inventory.Length; index++) { var slot = entity.Inventory[index]; if (slot.Empty) continue; slot.Index = NetworkSlotToDataSlot(index); inventory.Add(slot.ToNbt()); } data.Add(inventory); var motion = new NbtList("Motion"); motion.Add(new NbtDouble(entity.Velocity.X)); motion.Add(new NbtDouble(entity.Velocity.Y)); motion.Add(new NbtDouble(entity.Velocity.Z)); data.Add(motion); var pos = new NbtList("Pos"); pos.Add(new NbtDouble(entity.Position.X)); pos.Add(new NbtDouble(entity.Position.Y)); pos.Add(new NbtDouble(entity.Position.Z)); data.Add(pos); var rotation = new NbtList("Rotation"); rotation.Add(new NbtFloat(entity.Yaw)); rotation.Add(new NbtFloat(entity.Pitch)); data.Add(rotation); data.Add(new NbtCompound("abilities")); file.RootTag = data; if (!Directory.Exists(Path.Combine(LevelDirectory, "players"))) Directory.CreateDirectory(Path.Combine(LevelDirectory, "players")); using (Stream stream = File.Open(Path.Combine(LevelDirectory, "players", entity.Username + ".dat"), FileMode.OpenOrCreate)) file.SaveToStream(stream, NbtCompression.GZip); }
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; entity.Position += new Vector3(0, PlayerEntity.Height, 0); entity.GameMode = GameMode; return entity; } NbtFile 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; Dimension 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 = Slot.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, ((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); } entity.Yaw = ((NbtFloat)rotation[0]).Value; entity.Pitch = ((NbtFloat)rotation[1]).Value; // TODO: Abilities return entity; }
public PlayerDeathEventArgs(DamageType deathType, PlayerEntity player, Entity killer) : this(deathType, player) { Killer = killer; }
public PlayerDeathEventArgs(DamageType deathType, PlayerEntity player) { DeathType = deathType; Handled = false; Player = player; }