コード例 #1
0
 public override void BlockUpdate(World world, Vector3 updatedBlock, Vector3 modifiedBlock)
 {
     if (!(world.GetBlock(updatedBlock + Vector3.Down) is FarmlandBlock))
     {
         var entity = new ItemEntity(updatedBlock + new Vector3(0.5), new ItemStack(new PumpkinSeedsItem()));
         entity.ApplyRandomVelocity();
         world.SetBlock(updatedBlock, new AirBlock());
         world.OnSpawnEntity(entity);
         return;
     }
     if (!world.UpdatePending(updatedBlock))
     {
         var possibleLocations = new List<Vector3>(new[]
         {
             updatedBlock + Vector3.Left, updatedBlock + Vector3.Right,
             updatedBlock + Vector3.Forwards, updatedBlock + Vector3.Backwards
         });
         bool found = false;
         for (int i = 0; i < possibleLocations.Count; i++)
         {
             if (world.GetBlock(possibleLocations[i]) is PumpkinBlock)
                 found = true;
         }
         if (!found)
             ScheduleGrowth(world, updatedBlock);
     }
     base.BlockUpdate(world, updatedBlock, modifiedBlock);
 }
コード例 #2
0
ファイル: PotatoBlock.cs プロジェクト: cpancake/Craft.Net
 public override void BlockUpdate(World world, Vector3 updatedBlock, Vector3 modifiedBlock)
 {
     if (!(world.GetBlock(updatedBlock + Vector3.Down) is FarmlandBlock))
     {
         var entity = new ItemEntity(updatedBlock + new Vector3(0.5), new ItemStack(new PotatoItem()));
         entity.ApplyRandomVelocity();
         world.SetBlock(updatedBlock, new AirBlock());
         world.OnSpawnEntity(entity);
     }
     base.BlockUpdate(world, updatedBlock, modifiedBlock);
 }
コード例 #3
0
ファイル: CactusBlock.cs プロジェクト: cpancake/Craft.Net
 public override void BlockUpdate(World world, Vector3 updatedBlock, Vector3 modifiedBlock)
 {
     var below = world.GetBlock(updatedBlock + Vector3.Down);
     if (!(below is SandBlock || below is CactusBlock))
     {
         world.SetBlock(updatedBlock, new AirBlock());
         var entity = new ItemEntity(updatedBlock + new Vector3(0.5), new ItemStack(this));
         entity.ApplyRandomVelocity();
         world.OnSpawnEntity(entity);
     }
     base.BlockUpdate(world, updatedBlock, modifiedBlock);
 }
コード例 #4
0
ファイル: SugarCaneBlock.cs プロジェクト: cpancake/Craft.Net
 public override void BlockUpdate(World world, Vector3 updatedBlock, Vector3 modifiedBlock)
 {
     var block = world.GetBlock(updatedBlock + Vector3.Down);
     if (!(block is SugarCaneBlock || block is DirtBlock || block is GrassBlock || block is SandBlock))
     {
         world.SetBlock(updatedBlock, new AirBlock());
         var entity = new ItemEntity(updatedBlock + new Vector3(0.5), new ItemStack(new SugarCanesItem()));
         entity.ApplyRandomVelocity();
         world.OnSpawnEntity(entity);
     }
     base.BlockUpdate(world, updatedBlock, modifiedBlock);
 }
コード例 #5
0
ファイル: EntityTest.cs プロジェクト: ammaraskar/Craft.Net
        public void TestVerticalBlockCollision()
        {
            Level level = new Level();
            level.World.EntityUpdateTimer.Change(Timeout.Infinite, Timeout.Infinite); // Manual updates only

            for (int y = 10; y < 100; y += 10)
            {
                var entity = new ItemEntity(new Vector3(3, y, 3), new Slot(1, 1));
                level.World.Entities.Add(entity);

                for (int i = 0; i < 1000; i++)
                    entity.PhysicsUpdate(level.World);

                Assert.AreEqual(4, (int)(entity.Position.Y));

                level.World.Entities.Remove(entity);
            }
        }
コード例 #6
0
ファイル: EntityTest.cs プロジェクト: ammaraskar/Craft.Net
        public void TestZBlockCollision()
        {
            Level level = new Level();
            level.World.EntityUpdateTimer.Change(Timeout.Infinite, Timeout.Infinite); // Manual updates only

            // Build a wall to throw the item at
            for (int x = -10; x < 10; x++)
                for (int y = 0; y < 23; y++)
                    level.World.SetBlock(new Vector3(x, y, 3), new StoneBlock());

            var entity = new ItemEntity(new Vector3(0, 10, 0), new Slot(1, 1));
            entity.Velocity = new Vector3(0, 0, 3); // Throw item
            level.World.Entities.Add(entity);

            for (int i = 0; i < 1000; i++)
                entity.PhysicsUpdate(level.World);

            Assert.AreEqual(2, (int)(entity.Position.Z));

            level.World.Entities.Remove(entity);
        }
コード例 #7
0
 public static void CreativeInventoryAction(MinecraftClient client, MinecraftServer server, IPacket _packet)
 {
     var packet = (CreativeInventoryActionPacket)_packet;
     if (packet.SlotIndex == -1)
     {
         var entity = new ItemEntity(client.Entity.GivenPosition +
                         new Vector3(0, client.Entity.Size.Height, 0), packet.Item);
         entity.Velocity = MathHelper.FowardVector(client.Entity) * new Vector3(0.25);
         server.EntityManager.SpawnEntity(client.World, entity);
     }
     else if (packet.SlotIndex < client.Entity.Inventory.Length && packet.SlotIndex > 0)
     {
         client.Entity.Inventory[packet.SlotIndex] = packet.Item;
         if (packet.SlotIndex == client.Entity.SelectedSlot)
         {
             var clients = server.EntityManager.GetKnownClients(client.Entity);
             foreach (var _client in clients)
                 _client.SendPacket(new EntityEquipmentPacket(client.Entity.Id, EntityEquipmentPacket.EntityEquipmentSlot.HeldItem, 
                     client.Entity.Inventory[packet.SlotIndex]));
         }
     }
 }
コード例 #8
0
        public override void UsedByEntity(World world, bool leftClick, LivingEntity usedBy)
        {
            if (leftClick)
            {
                world.OnDestroyEntity(this);
            }
            var  player = usedBy as PlayerEntity;
            bool drop   = true;

            if (player != null)
            {
                if (player.GameMode == GameMode.Creative)
                {
                    drop = false;
                }
            }
            if (drop)
            {
                var item = new ItemEntity(Center, new ItemStack(new PaintingItem()));
                item.ApplyRandomVelocity();
                world.OnSpawnEntity(item);
            }
        }
コード例 #9
0
        public static double MaxDigDistance = 6; // TODO: Move somewhere else

        public static void PlayerDigging(MinecraftClient client, MinecraftServer server, IPacket _packet)
        {
            var packet = (PlayerDiggingPacket)_packet;
            var position = new Vector3(packet.X, packet.Y, packet.Z);
            // TODO: Enforce line-of-sight
            var block = client.World.GetBlock(position);
            short damage;
            switch (packet.Action)
            {
                case PlayerDiggingPacket.PlayerAction.StartedDigging:
                    if (client.Entity.Position.DistanceTo(position) <= MaxDigDistance)
                    {
                        if (client.Entity.Abilities.InstantMine || block.Hardness == 0)
                            block.OnBlockMined(client.World, position, client.Entity);
                        else
                        {
                            client.ExpectedMiningEnd = DateTime.Now.AddMilliseconds(
                                block.GetHarvestTime(client.Entity.SelectedItem.AsItem(),
                                client.World, client.Entity, out damage) - (client.Ping + 100));
                            client.ExpectedBlockToMine = position;
                        }
                    }
                    break;
                case PlayerDiggingPacket.PlayerAction.FinishedDigging:
                    if (client.Entity.Position.DistanceTo(position) <= MaxDigDistance)
                    {
                        if (client.ExpectedMiningEnd > DateTime.Now || client.ExpectedBlockToMine != position)
                            return;
                        block.GetHarvestTime(client.Entity.SelectedItem.AsItem(),
                                client.World, client.Entity, out damage);
                        if (damage != 0)
                        {
                            var slot = client.Entity.Inventory[client.Entity.SelectedSlot];
                            if (!slot.Empty)
                            {
                                if (slot.AsItem() is ToolItem)
                                {
                                    var tool = slot.AsItem() as ToolItem;
                                    bool destroy = tool.Damage(damage);
                                    slot.Metadata = tool.Data;
                                    if (destroy)
                                        client.Entity.SetSlot(client.Entity.SelectedSlot, ItemStack.EmptyStack);
                                    else
                                        client.Entity.SetSlot(client.Entity.SelectedSlot, slot);
                                }
                            }
                        }
                        block.OnBlockMined(client.World, position, client.Entity);
                        client.Entity.FoodExhaustion += 0.025f;
                    }
                    break;
                case PlayerDiggingPacket.PlayerAction.DropItem:
                case PlayerDiggingPacket.PlayerAction.DropStack:
                    var SlotItem = client.Entity.Inventory[client.Entity.SelectedSlot];
                    if (!SlotItem.Empty)
                    {
                        var ItemCopy = (ItemStack)SlotItem.Clone();
                        if (packet.Action == PlayerDiggingPacket.PlayerAction.DropStack)
                            client.Entity.SetSlot(client.Entity.SelectedSlot, ItemStack.EmptyStack);
                        else
                        {
                            ItemCopy.Count = 1;
                            SlotItem.Count--; // Decrease the player's item by 1
                            if (SlotItem.Count == 0)
                                client.Entity.SetSlot(client.Entity.SelectedSlot, ItemStack.EmptyStack);
                            else
                                client.Entity.SetSlot(client.Entity.SelectedSlot, SlotItem);
                        }
                        var entity = new ItemEntity(client.Entity.GivenPosition + 
                            new Vector3(0, client.Entity.Size.Height, 0), ItemCopy);
                        entity.Velocity = MathHelper.FowardVector(client.Entity) * new Vector3(0.25);
                        server.EntityManager.SpawnEntity(client.World, entity);
                    }
                    break;
            }
        }
コード例 #10
0
ファイル: EntityManager.cs プロジェクト: seaboy1234/Craft.Net
 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);
         }
     }
 }
コード例 #11
0
        public static void ClickWindow(MinecraftClient client, MinecraftServer server, IPacket _packet)
        {
            var packet = (ClickWindowPacket)_packet;
            if (packet.MouseButton == 3 && packet.Shift)
                return; // No effect in vanilla minecraft
            Window window = null;
            if (packet.WindowId == 0)
                window = client.Entity.Inventory;
            // TODO: Fetch appropriate furnace/crafting bench/etc window
            if (window == null)
                return;
            if (packet.Shift)
            {
                window.MoveToAlternateArea(packet.SlotIndex);
                return;
            }

            var heldItem = client.Entity.ItemInMouse;

            if (packet.SlotIndex == -999)
            {
                if (heldItem.Empty)
                    return;
                var entity = new ItemEntity(client.Entity.GivenPosition +
                                new Vector3(0, client.Entity.Size.Height, 0), heldItem);
                entity.Velocity = MathHelper.FowardVector(client.Entity) * new Vector3(0.25);
                server.EntityManager.SpawnEntity(client.World, entity);
                return;
            }

            var clickedItem = client.Entity.Inventory[packet.SlotIndex];

            if (heldItem.Empty)
            {
                if (clickedItem.Empty)
                    return;
                if (packet.MouseButton == 1)
                {
                    var heldCount = (sbyte)(clickedItem.Count / 2 + (clickedItem.Count % 2));
                    var leftCount = (sbyte)(clickedItem.Count / 2);
                    client.Entity.ItemInMouse = new Slot(clickedItem.Id, heldCount, clickedItem.Metadata);
                    var old = client.Entity.Inventory[packet.SlotIndex];
                    client.Entity.Inventory[packet.SlotIndex] = new Slot(old.Id, leftCount, old.Metadata, old.Nbt);
                }
                else
                {
                    client.Entity.ItemInMouse = clickedItem;
                    client.Entity.Inventory[packet.SlotIndex] = Slot.EmptySlot;
                }
            }
            else
            {
                if (packet.MouseButton == 1 && ((clickedItem.Id == heldItem.Id &&
                    clickedItem.Metadata == heldItem.Metadata) || clickedItem.Empty))
                {
                    if (!clickedItem.Empty && clickedItem.Count < clickedItem.AsItem().MaximumStack)
                    {
                        client.Entity.Inventory[packet.SlotIndex] = new Slot(heldItem.Id,
                            (sbyte)(clickedItem.Count + (clickedItem.Empty ? 0 : 1)), heldItem.Metadata);
                        client.Entity.ItemInMouse = new Slot(client.Entity.ItemInMouse.Id, (sbyte)(client.Entity.ItemInMouse.Count - 1),
                            client.Entity.ItemInMouse.Metadata, client.Entity.ItemInMouse.Nbt);
                    }
                    else
                        client.Entity.Inventory[packet.SlotIndex] = new Slot(heldItem.Id, 1, heldItem.Metadata);
                }
                else
                {
                    if (clickedItem.Empty)
                    {
                        client.Entity.Inventory[packet.SlotIndex] = heldItem;
                        client.Entity.ItemInMouse = Slot.EmptySlot;
                    }
                    else
                    {
                        client.Entity.ItemInMouse = clickedItem;
                        client.Entity.Inventory[packet.SlotIndex] = heldItem;
                    }
                }
            }
        }
コード例 #12
0
ファイル: Block.cs プロジェクト: cpancake/Craft.Net
 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);
                 }
             }
         }
     }
 }
コード例 #13
0
 public override void UsedByEntity(World world, bool leftClick, LivingEntity usedBy)
 {
     var player = usedBy as PlayerEntity;
     if (!leftClick)
     {
         if (!Item.Empty)
         {
             Orientation++;
             OnPropertyChanged("Metadata");
         }
         else
         {
             if (!player.SelectedItem.Empty)
             {
                 var slot = player.SelectedItem;
                 Item = new ItemStack(slot.Id, 1, slot.Metadata, slot.Nbt);
                 if (player.GameMode != GameMode.Creative)
                 {
                     slot.Count--;
                     player.Inventory[player.SelectedSlot] = slot;
                 }
             }
         }
     }
     else
     {
         world.OnDestroyEntity(this);
         if (player.GameMode != GameMode.Creative)
         {
             var spawnPosition = Position + new Vector3(0.5);
             switch (Direction)
             {
                 case ItemFrameDirection.North: spawnPosition += Vector3.North; break;
                 case ItemFrameDirection.South: spawnPosition += Vector3.South; break;
                 case ItemFrameDirection.East:  spawnPosition += Vector3.East;  break;
                 case ItemFrameDirection.West:  spawnPosition += Vector3.West;  break;
             }
             var frame = new ItemEntity(spawnPosition, new ItemStack(new ItemFrameItem()));
             var item = new ItemEntity(spawnPosition, Item);
             frame.ApplyRandomVelocity();
             item.ApplyRandomVelocity();
             world.OnSpawnEntity(frame);
             world.OnSpawnEntity(item);
         }
     }
     base.UsedByEntity(world, leftClick, usedBy);
 }
コード例 #14
0
ファイル: PaintingEntity.cs プロジェクト: cpancake/Craft.Net
 public override void UsedByEntity(World world, bool leftClick, LivingEntity usedBy)
 {
     if (leftClick)
         world.OnDestroyEntity(this);
     var player = usedBy as PlayerEntity;
     bool drop = true;
     if (player != null)
     {
         if (player.GameMode == GameMode.Creative)
             drop = false;
     }
     if (drop)
     {
         var item = new ItemEntity(Center, new ItemStack(new PaintingItem()));
         item.ApplyRandomVelocity();
         world.OnSpawnEntity(item);
     }
 }
コード例 #15
0
 public SpawnDroppedItemPacket(ItemEntity item)
 {
     Item = item;
 }