Fix torch placement and prevent overwriting blocks

Fixes #50
This commit is contained in:
Drew DeVault 2015-05-03 15:12:10 -06:00
parent 0decf1903e
commit 6575444768
3 changed files with 76 additions and 19 deletions

View File

@ -6,6 +6,8 @@ using TrueCraft.API.Networking;
using TrueCraft.Core.Entities; using TrueCraft.Core.Entities;
using TrueCraft.API.Entities; using TrueCraft.API.Entities;
using TrueCraft.API.Server; using TrueCraft.API.Server;
using TrueCraft.Core.Logic.Blocks;
using System.Linq;
namespace TrueCraft.Core.Logic namespace TrueCraft.Core.Logic
{ {
@ -75,7 +77,10 @@ namespace TrueCraft.Core.Logic
protected virtual ItemStack[] GetDrop(BlockDescriptor descriptor) // TODO: Include tools protected virtual ItemStack[] GetDrop(BlockDescriptor descriptor) // TODO: Include tools
{ {
return new[] { new ItemStack(descriptor.ID, 1, descriptor.Metadata) }; short meta = 0;
if (this is ICraftingRecipe)
meta = (short)((this as ICraftingRecipe).SignificantMetadata ? descriptor.Metadata : 0);
return new[] { new ItemStack(descriptor.ID, 1, meta) };
} }
public virtual void ItemUsedOnEntity(ItemStack item, IEntity usedOn, IWorld world, IRemoteClient user) public virtual void ItemUsedOnEntity(ItemStack item, IEntity usedOn, IWorld world, IRemoteClient user)
@ -91,11 +96,30 @@ namespace TrueCraft.Core.Logic
public virtual void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user) public virtual void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
{ {
coordinates += MathHelper.BlockFaceToCoordinates(face); coordinates += MathHelper.BlockFaceToCoordinates(face);
world.SetBlockID(coordinates, (byte)item.ID); var old = world.GetBlockData(coordinates);
world.SetMetadata(coordinates, (byte)item.Metadata); byte[] overwritable =
item.Count--; {
user.Inventory[user.SelectedSlot] = item; AirBlock.BlockID,
BlockPlaced(world.GetBlockData(coordinates), face, world, user); WaterBlock.BlockID,
StationaryWaterBlock.BlockID,
LavaBlock.BlockID,
StationaryLavaBlock.BlockID
};
if (overwritable.Any(b => b == old.ID))
{
world.SetBlockID(coordinates, (byte)item.ID);
world.SetMetadata(coordinates, (byte)item.Metadata);
BlockPlaced(world.GetBlockData(coordinates), face, world, user);
if (!IsSupported(world.GetBlockData(coordinates), user.Server, world))
world.SetBlockData(coordinates, old);
else
{
item.Count--;
user.Inventory[user.SelectedSlot] = item;
}
}
} }
short IItemProvider.ID short IItemProvider.ID

View File

@ -11,10 +11,10 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
public enum TorchDirection public enum TorchDirection
{ {
South = 0x01, // Positive Z West = 0x01, // West
North = 0x02, East = 0x02, // East
West = 0x03, South = 0x03, // South
East = 0x04, North = 0x04, // North
Ground = 0x05 Ground = 0x05
} }
@ -34,28 +34,59 @@ namespace TrueCraft.Core.Logic.Blocks
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{ {
TorchDirection[] preferredDirections =
{
TorchDirection.West, TorchDirection.East,
TorchDirection.North, TorchDirection.South,
TorchDirection.Ground
};
TorchDirection direction; TorchDirection direction;
switch (face) switch (face)
{ {
case BlockFace.PositiveZ: case BlockFace.PositiveZ:
direction = TorchDirection.West;
break;
case BlockFace.NegativeZ:
direction = TorchDirection.East;
break;
case BlockFace.PositiveX:
direction = TorchDirection.South; direction = TorchDirection.South;
break; break;
case BlockFace.NegativeX: case BlockFace.NegativeZ:
direction = TorchDirection.North; direction = TorchDirection.North;
break; break;
case BlockFace.PositiveX:
direction = TorchDirection.East;
break;
case BlockFace.NegativeX:
direction = TorchDirection.West;
break;
default: default:
direction = TorchDirection.Ground; direction = TorchDirection.Ground;
break; break;
} }
int i = 0;
descriptor.Metadata = (byte)direction;
while (!IsSupported(descriptor, user.Server, world) && i < preferredDirections.Length)
{
direction = preferredDirections[i++];
descriptor.Metadata = (byte)direction;
}
world.SetMetadata(descriptor.Coordinates, (byte)direction); world.SetMetadata(descriptor.Coordinates, (byte)direction);
} }
public override Coordinates3D GetSupportDirection(BlockDescriptor descriptor)
{
switch ((TorchDirection)descriptor.Metadata)
{
case TorchDirection.Ground:
return Coordinates3D.Down;
case TorchDirection.East:
return Coordinates3D.West;
case TorchDirection.West:
return Coordinates3D.East;
case TorchDirection.North:
return Coordinates3D.South;
case TorchDirection.South:
return Coordinates3D.North;
}
return Coordinates3D.Zero;
}
public override Tuple<int, int> GetTextureMap(byte metadata) public override Tuple<int, int> GetTextureMap(byte metadata)
{ {
return new Tuple<int, int>(0, 5); return new Tuple<int, int>(0, 5);

View File

@ -229,9 +229,11 @@ namespace TrueCraft.Core.World
public NbtTag Serialize(string tagName) public NbtTag Serialize(string tagName)
{ {
var chunk = (NbtCompound)Serializer.Serialize(this, tagName, true); var chunk = new NbtCompound(tagName);
var entities = new NbtList("Entities", NbtTagType.Compound); var entities = new NbtList("Entities", NbtTagType.Compound);
chunk.Add(entities); chunk.Add(entities);
chunk.Add(new NbtInt("X", X));
chunk.Add(new NbtInt("Z", Z));
chunk.Add(new NbtByteArray("Blocks", Blocks)); chunk.Add(new NbtByteArray("Blocks", Blocks));
chunk.Add(new NbtByteArray("Data", Metadata.Data)); chunk.Add(new NbtByteArray("Data", Metadata.Data));
chunk.Add(new NbtByteArray("SkyLight", SkyLight.Data)); chunk.Add(new NbtByteArray("SkyLight", SkyLight.Data));
@ -257,7 +259,7 @@ namespace TrueCraft.Core.World
public void Deserialize(NbtTag value) public void Deserialize(NbtTag value)
{ {
var chunk = (Chunk)Serializer.Deserialize(value, true); var chunk = new Chunk();
var tag = (NbtCompound)value; var tag = (NbtCompound)value;
Biomes = chunk.Biomes; Biomes = chunk.Biomes;