Skip to content
Permalink
Browse files

Add DefaultSet.cs for default properties of blocks

  • Loading branch information...
UnknownShadow200 committed Aug 11, 2017
1 parent ebc4458 commit 5095b024f91c988c5297004d6e24379e12624be5
@@ -1493,9 +1493,7 @@ static class CpeCommands {
break;
case 4:
if (byte.TryParse(args, out value)) {
step++; def.SideTex = value;
def.LeftTex = def.SideTex; def.RightTex = def.SideTex;
def.FrontTex = def.SideTex; def.BackTex = def.SideTex;
step++; def.SetSidesTex(value);
p.Message(" &bSet sides texture index to: " + value);
}
break;
@@ -1817,9 +1815,7 @@ static class CpeCommands {
case "sidetexture":
if (byte.TryParse(args, out value)) {
p.Message("&bChanged sides texture index of &a{0}&b from &a{1}&b to &a{2}", def.Name, def.SideTex, value);
def.SideTex = value;
def.LeftTex = def.SideTex; def.RightTex = def.SideTex;
def.FrontTex = def.SideTex; def.BackTex = def.SideTex;
def.SetSidesTex(value);
hasChanged = true;
}
break;
@@ -3836,7 +3836,7 @@ static void WorldClearHandler(Player player, CommandReader cmd)
return;
}

if (PortalHandler.GetInstance().GetPortal(player.World, new Vector3I(x, y, z)) != null) {
if (PortalHandler.GetPortal(player.World, new Vector3I(x, y, z)) != null) {
player.Message("You cannot build a portal inside a portal.");
return;
}
@@ -370,12 +370,14 @@ public sealed partial class Player {
if (!Supports(CpeExt.HeldBlock)) {
HeldBlock = Block.None; return;
}
Block held;
if (!Map.GetBlockByName(World, id.ToString(), false, out held)) {
HeldBlock = Block.Stone; return;
}

// Holding an invalid block
Block held = (Block)id;
if (held > Block.StoneBrick && World.BlockDefs[id] == null) {
HeldBlock = Block.Stone; return;
}
if (HeldBlock == held) return;

HeldBlock = held;
LastUsedBlockType = held;
if (Supports(CpeExt.MessageType) && !IsPlayingCTF) {
@@ -99,7 +99,7 @@ class PortalHandler {
if (e.Player.CanUsePortal) {
if ((e.OldPosition.X != e.NewPosition.X) || (e.OldPosition.Y != e.NewPosition.Y) || (e.OldPosition.Z != (e.NewPosition.Z))) {
if (e.Player.Can(Permission.Chat)) {
if (PortalHandler.GetInstance().GetPortal(e.Player) != null && !e.Player.StandingInPortal) {
if (GetPortal(e.Player) != null && !e.Player.StandingInPortal) {
if ((DateTime.UtcNow - e.Player.LastUsedPortal).TotalSeconds < 5) {
// To prevent portal loops
if ((DateTime.UtcNow - e.Player.LastWarnedPortal).TotalSeconds > 2) {
@@ -111,7 +111,7 @@ class PortalHandler {
}

e.Player.StandingInPortal = true;
Portal portal = PortalHandler.GetInstance().GetPortal(e.Player);
Portal portal = GetPortal(e.Player);

World world = WorldManager.FindWorldExact(portal.World);
// Teleport player, portal protection
@@ -173,44 +173,38 @@ class PortalHandler {
}
}

public Portal GetPortal(Player player) {
Portal portal = null;

public static Portal GetPortal(Player player) {
try {
if (player.World.Portals != null && player.World.Portals.Count > 0) {
lock (player.World.Portals.SyncRoot) {
foreach (Portal possiblePortal in player.World.Portals) {
if (possiblePortal.IsInRange(player)) {
return possiblePortal;
foreach (Portal candidate in player.World.Portals) {
if (candidate.IsInRange(player)) {
return candidate;
}
}
}
}
} catch (Exception ex) {
Logger.Log(LogType.Error, "PortalHandler.GetPortal: " + ex);
}

return portal;
return null;
}

public Portal GetPortal(World world, Vector3I block) {
Portal portal = null;

public static Portal GetPortal(World world, Vector3I block) {
try {
if (world.Portals != null && world.Portals.Count > 0) {
lock (world.Portals.SyncRoot) {
foreach (Portal possiblePortal in world.Portals) {
if (possiblePortal.IsInRange(block)) {
return possiblePortal;
foreach (Portal candidate in world.Portals) {
if (candidate.IsInRange(block)) {
return candidate;
}
}
}
}
} catch (Exception ex) {
Logger.Log(LogType.Error, "PortalHandler.GetPortal: " + ex);
}

return portal;
return null;
}

public static void CreatePortal(Portal portal, World source) {
@@ -458,7 +458,12 @@ public static string IrcToMinecraftColors([NotNull] string input)
public struct CustomColor {
public char Code, Fallback;
public byte R, G, B, A;
public string Name;
public string Name;

public CustomColor(byte r, byte g, byte b) {
Code = '\0'; Fallback = '\0'; Name = null;
R = r; G = g; B = b; A = 255;
}

public bool Undefined { get { return Fallback == '\0'; } }
}
@@ -43,6 +43,12 @@ public sealed class BlockDefinition {
/// <summary> Name used in commands. (e.g. "Mossy Slabs" becomes "mossyslabs")"</summary>
public string BlockName;

public void SetSidesTex(byte tex) {
SideTex = tex;
LeftTex = tex; RightTex = tex;
FrontTex = tex; BackTex = tex;
}

public BlockDefinition Copy() {
BlockDefinition def = new BlockDefinition();
def.BlockID = BlockID; def.Name = Name; def.BlockName = BlockName;
@@ -0,0 +1,221 @@
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
using System;
using System.Text;

namespace fCraft {

/// <summary> Stores default properties for blocks in Minecraft Classic. (and CPE blocks). </summary>
public static class DefaultSet {

/// <summary> Constructs a custom block, with the default properties of the given classic/CPE block. </summary>
public static BlockDefinition MakeCustomBlock(Block b) {
BlockDefinition def = new BlockDefinition();
byte raw = (byte)b;
def.BlockID = raw;
def.Name = Name(raw);
def.CollideType = Collide(b);
def.Speed = 1;
def.BlocksLight = BlocksLight(b);

def.TopTex = topTex[raw];
def.BottomTex = bottomTex[raw];
def.SetSidesTex(sideTex[raw]);
def.WalkSound = (byte)StepSound(b);

def.FullBright = FullBright(b);
def.Shape = Draw(b) == DrawType.Sprite ? (byte)0 : (byte)1;
def.BlockDraw = Draw(b);
if (def.BlockDraw == DrawType.Sprite)
def.BlockDraw = DrawType.Transparent;

def.FogDensity = FogDensity(b);
CustomColor fog = FogColor(b);
def.FogR = fog.R; def.FogG = fog.G; def.FogB = fog.B;
def.FallBack = raw;

def.MaxX = 16; def.MaxZ = Height(b); def.MaxY = 16;
def.Version2 = true;
return def;
}

/// <summary> Gets the default height of a block. A value of 16 is full height. </summary>
public static byte Height(Block b) {
if (b == Block.Slab) return 8;
if (b == Block.CobbleSlab) return 8;
if (b == Block.Snow) return 2;
return 16;
}

/// <summary> Gets whether a block is full bright / light emitting by default. </summary>
public static bool FullBright(Block b) {
return b == Block.Lava || b == Block.StillLava
|| b == Block.Magma || b == Block.Fire;
}

/// <summary> Gets the default fog density of a block, in packed form. </summary>
public static byte FogDensity(Block b) {
if (b == Block.Water || b == Block.StillWater)
return 11; // (128 * 0.1f - 1);
if (b == Block.Lava || b == Block.StillLava)
return 255; // (128 * 2 - 1);
return 0;
}

/// <summary> Gets the default fog color of a block. </summary>
public static CustomColor FogColor(Block b) {
if (b == Block.Water || b == Block.StillWater)
return new CustomColor(5, 5, 51);
if (b == Block.Lava || b == Block.StillLava)
return new CustomColor(153, 25, 0);
return default(CustomColor);
}

/// <summary> Gets the default collide type of a block, see CollideType class. </summary>
public static byte Collide(Block b) {
if (b >= Block.Water && b <= Block.StillLava)
return CollideType.SwimThrough;
if (b == Block.Snow || b == Block.Air || Draw(b) == DrawType.Sprite)
return CollideType.WalkThrough;
return CollideType.Solid;
}

/// <summary> Gets whether a block blocks light (prevents light passing through) by default. </summary>
public static bool BlocksLight(Block b) {
return !(b == Block.Glass || b == Block.Leaves
|| b == Block.Air || Draw(b) == DrawType.Sprite);
}


/// <summary> Gets the default step sound of a block. </summary>
public static SoundType StepSound(Block b) {
if (b == Block.Glass) return SoundType.Stone;
if (b == Block.Rope) return SoundType.Cloth;
if (Draw(b) == DrawType.Sprite) return SoundType.None;

if (b >= Block.Red && b <= Block.White)
return SoundType.Cloth;
if (b >= Block.LightPink && b <= Block.Turquoise)
return SoundType.Cloth;
if (b == Block.Iron || b == Block.Gold)
return SoundType.Metal;

if (b == Block.Books || b == Block.Wood
|| b == Block.Log || b == Block.Crate || b == Block.Fire)
return SoundType.Wood;

if (b == Block.Rope) return SoundType.Cloth;
if (b == Block.Sand) return SoundType.Sand;
if (b == Block.Snow) return SoundType.Snow;
if (b == Block.Glass) return SoundType.Glass;
if (b == Block.Dirt || b == Block.Gravel)
return SoundType.Gravel;

if (b == Block.Grass || b == Block.Sapling || b == Block.TNT
|| b == Block.Leaves || b == Block.Sponge)
return SoundType.Grass;

if (b >= Block.YellowFlower && b <= Block.RedMushroom)
return SoundType.Grass;
if (b >= Block.Water && b <= Block.StillLava)
return SoundType.None;
if (b >= Block.Stone && b <= Block.StoneBrick)
return SoundType.Stone;
return SoundType.None;
}


/// <summary> Gets the default draw type of a block, see Draw class. </summary>
public static byte Draw(Block b) {
if (b == Block.Air || b == Block.None) return DrawType.Gas;
if (b == Block.Leaves) return DrawType.TransparentThick;

if (b == Block.Ice || b == Block.Water || b == Block.StillWater)
return DrawType.Translucent;
if (b == Block.Glass || b == Block.Leaves)
return DrawType.Transparent;

if (b >= Block.YellowFlower && b <= Block.RedMushroom)
return DrawType.Sprite;
if (b == Block.Sapling || b == Block.Rope || b == Block.Fire)
return DrawType.Sprite;
return DrawType.Opaque;
}


const string Names = "Air Stone Grass Dirt Cobblestone Wood Sapling Bedrock Water StillWater Lava" +
" StillLava Sand Gravel GoldOre IronOre CoalOre Log Leaves Sponge Glass Red Orange Yellow Lime Green" +
" Teal Aqua Cyan Blue Indigo Violet Magenta Pink Black Gray White Dandelion Rose BrownMushroom RedMushroom" +
" Gold Iron DoubleSlab Slab Brick TNT Bookshelf MossyRocks Obsidian CobblestoneSlab Rope Sandstone" +
" Snow Fire LightPink ForestGreen Brown DeepBlue Turquoise Ice CeramicTile Magma Pillar Crate StoneBrick";

static string Name(int block) {
// Find start and end of this particular block name
int start = 0;
for (int i = 0; i < block; i++)
start = Names.IndexOf(' ', start) + 1;
int end = Names.IndexOf(' ', start);
if (end == -1) end = Names.Length;

StringBuilder buffer = new StringBuilder();
SplitUppercase(buffer, start, end);
return buffer.ToString();
}

static void SplitUppercase(StringBuilder buffer, int start, int end) {
for (int i = start; i < end; i++) {
char c = Names[i];
bool upper = Char.IsUpper(c) && i > start;
bool nextLower = i < end - 1 && !Char.IsUpper(Names[i + 1]);

if (upper && nextLower) {
buffer.Append(' ');
buffer.Append(Char.ToLower(c));
} else {
buffer.Append(c);
}
}
}


static byte[] topTex = new byte[] { 0, 1, 0, 2, 16, 4, 15, 17, 14, 14,
30, 30, 18, 19, 32, 33, 34, 21, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 24, 23, 6, 6, 7, 9, 4,
36, 37, 16, 11, 25, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 26, 53, 52, };
static byte[] sideTex = new byte[] { 0, 1, 3, 2, 16, 4, 15, 17, 14, 14,
30, 30, 18, 19, 32, 33, 34, 20, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 40, 39, 5, 5, 7, 8, 35,
36, 37, 16, 11, 41, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 42, 53, 52, };
static byte[] bottomTex = new byte[] { 0, 1, 2, 2, 16, 4, 15, 17, 14, 14,
30, 30, 18, 19, 32, 33, 34, 21, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 56, 55, 6, 6, 7, 10, 4,
36, 37, 16, 11, 57, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 58, 53, 52 };
}

public static class DrawType {
public const byte Opaque = 0;
public const byte Transparent = 1;
public const byte TransparentThick = 2; // e.g. leaves render all neighbours
public const byte Translucent = 3;
public const byte Gas = 4;
public const byte Sprite = 5;
}

public static class CollideType {
public const byte WalkThrough = 0; // Gas (usually also used by sprite)
public const byte SwimThrough = 1; // Liquid
public const byte Solid = 2; // Solid
public const byte Ice = 3; // Solid and partially slidable on.
public const byte SlipperyIce = 4; // Solid and fully slidable on.
public const byte LiquidWater = 5; // Water style 'swimming'/'bobbing'
public const byte LiquidLava = 6; // Lava style 'swimming'/'bobbing'

public static bool IsSolid(byte collide) {
return collide >= Solid && collide <= SlipperyIce;
}
}

public enum SoundType : byte {
None, Wood, Gravel, Grass, Stone,
Metal, Glass, Cloth, Sand, Snow,
}
}
@@ -217,6 +217,7 @@
<Compile Include="Player\PlayerDB.cs" />
<Compile Include="Player\PlayerInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="World\DefaultSet.cs" />
<Compile Include="World\Entities.cs" />
<Compile Include="Zone\SpecialZone.Affect.cs" />
<Compile Include="Zone\SpecialZone.cs" />

0 comments on commit 5095b02

Please sign in to comment.
You can’t perform that action at this time.