Skip to content

Commit 5095b02

Browse files
Add DefaultSet.cs for default properties of blocks
1 parent ebc4458 commit 5095b02

File tree

8 files changed

+255
-30
lines changed

8 files changed

+255
-30
lines changed

fCraft/Commands/CpeCommands.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,9 +1493,7 @@ static void CustomBlockDefineHandler(Player p, string args, bool global, BlockDe
14931493
break;
14941494
case 4:
14951495
if (byte.TryParse(args, out value)) {
1496-
step++; def.SideTex = value;
1497-
def.LeftTex = def.SideTex; def.RightTex = def.SideTex;
1498-
def.FrontTex = def.SideTex; def.BackTex = def.SideTex;
1496+
step++; def.SetSidesTex(value);
14991497
p.Message(" &bSet sides texture index to: " + value);
15001498
}
15011499
break;
@@ -1817,9 +1815,7 @@ static void CustomBlockEditHandler(Player p, CommandReader cmd, bool global, Blo
18171815
case "sidetexture":
18181816
if (byte.TryParse(args, out value)) {
18191817
p.Message("&bChanged sides texture index of &a{0}&b from &a{1}&b to &a{2}", def.Name, def.SideTex, value);
1820-
def.SideTex = value;
1821-
def.LeftTex = def.SideTex; def.RightTex = def.SideTex;
1822-
def.FrontTex = def.SideTex; def.BackTex = def.SideTex;
1818+
def.SetSidesTex(value);
18231819
hasChanged = true;
18241820
}
18251821
break;

fCraft/Commands/WorldCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,7 @@ static void PortalCreateCallback(Player player, Vector3I[] marks, object tag) {
38363836
return;
38373837
}
38383838

3839-
if (PortalHandler.GetInstance().GetPortal(player.World, new Vector3I(x, y, z)) != null) {
3839+
if (PortalHandler.GetPortal(player.World, new Vector3I(x, y, z)) != null) {
38403840
player.Message("You cannot build a portal inside a portal.");
38413841
return;
38423842
}

fCraft/Network/Player.Networking.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,14 @@ void UpdateHeldBlock() {
370370
if (!Supports(CpeExt.HeldBlock)) {
371371
HeldBlock = Block.None; return;
372372
}
373-
Block held;
374-
if (!Map.GetBlockByName(World, id.ToString(), false, out held)) {
375-
HeldBlock = Block.Stone; return;
376-
}
377373

374+
// Holding an invalid block
375+
Block held = (Block)id;
376+
if (held > Block.StoneBrick && World.BlockDefs[id] == null) {
377+
HeldBlock = Block.Stone; return;
378+
}
378379
if (HeldBlock == held) return;
380+
379381
HeldBlock = held;
380382
LastUsedBlockType = held;
381383
if (Supports(CpeExt.MessageType) && !IsPlayingCTF) {

fCraft/Portals/PortalHandler.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void Player_Moved(object sender, Events.PlayerMovedEventArgs e) {
9999
if (e.Player.CanUsePortal) {
100100
if ((e.OldPosition.X != e.NewPosition.X) || (e.OldPosition.Y != e.NewPosition.Y) || (e.OldPosition.Z != (e.NewPosition.Z))) {
101101
if (e.Player.Can(Permission.Chat)) {
102-
if (PortalHandler.GetInstance().GetPortal(e.Player) != null && !e.Player.StandingInPortal) {
102+
if (GetPortal(e.Player) != null && !e.Player.StandingInPortal) {
103103
if ((DateTime.UtcNow - e.Player.LastUsedPortal).TotalSeconds < 5) {
104104
// To prevent portal loops
105105
if ((DateTime.UtcNow - e.Player.LastWarnedPortal).TotalSeconds > 2) {
@@ -111,7 +111,7 @@ static void Player_Moved(object sender, Events.PlayerMovedEventArgs e) {
111111
}
112112

113113
e.Player.StandingInPortal = true;
114-
Portal portal = PortalHandler.GetInstance().GetPortal(e.Player);
114+
Portal portal = GetPortal(e.Player);
115115

116116
World world = WorldManager.FindWorldExact(portal.World);
117117
// Teleport player, portal protection
@@ -173,44 +173,38 @@ static void Player_Moved(object sender, Events.PlayerMovedEventArgs e) {
173173
}
174174
}
175175

176-
public Portal GetPortal(Player player) {
177-
Portal portal = null;
178-
176+
public static Portal GetPortal(Player player) {
179177
try {
180178
if (player.World.Portals != null && player.World.Portals.Count > 0) {
181179
lock (player.World.Portals.SyncRoot) {
182-
foreach (Portal possiblePortal in player.World.Portals) {
183-
if (possiblePortal.IsInRange(player)) {
184-
return possiblePortal;
180+
foreach (Portal candidate in player.World.Portals) {
181+
if (candidate.IsInRange(player)) {
182+
return candidate;
185183
}
186184
}
187185
}
188186
}
189187
} catch (Exception ex) {
190188
Logger.Log(LogType.Error, "PortalHandler.GetPortal: " + ex);
191189
}
192-
193-
return portal;
190+
return null;
194191
}
195192

196-
public Portal GetPortal(World world, Vector3I block) {
197-
Portal portal = null;
198-
193+
public static Portal GetPortal(World world, Vector3I block) {
199194
try {
200195
if (world.Portals != null && world.Portals.Count > 0) {
201196
lock (world.Portals.SyncRoot) {
202-
foreach (Portal possiblePortal in world.Portals) {
203-
if (possiblePortal.IsInRange(block)) {
204-
return possiblePortal;
197+
foreach (Portal candidate in world.Portals) {
198+
if (candidate.IsInRange(block)) {
199+
return candidate;
205200
}
206201
}
207202
}
208203
}
209204
} catch (Exception ex) {
210205
Logger.Log(LogType.Error, "PortalHandler.GetPortal: " + ex);
211206
}
212-
213-
return portal;
207+
return null;
214208
}
215209

216210
public static void CreatePortal(Portal portal, World source) {

fCraft/System/Utils/Color.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,12 @@ public static int Hex(char hexChar) {
458458
public struct CustomColor {
459459
public char Code, Fallback;
460460
public byte R, G, B, A;
461-
public string Name;
461+
public string Name;
462+
463+
public CustomColor(byte r, byte g, byte b) {
464+
Code = '\0'; Fallback = '\0'; Name = null;
465+
R = r; G = g; B = b; A = 255;
466+
}
462467

463468
public bool Undefined { get { return Fallback == '\0'; } }
464469
}

fCraft/World/BlockDefinition.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public sealed class BlockDefinition {
4343
/// <summary> Name used in commands. (e.g. "Mossy Slabs" becomes "mossyslabs")"</summary>
4444
public string BlockName;
4545

46+
public void SetSidesTex(byte tex) {
47+
SideTex = tex;
48+
LeftTex = tex; RightTex = tex;
49+
FrontTex = tex; BackTex = tex;
50+
}
51+
4652
public BlockDefinition Copy() {
4753
BlockDefinition def = new BlockDefinition();
4854
def.BlockID = BlockID; def.Name = Name; def.BlockName = BlockName;

fCraft/World/DefaultSet.cs

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
2+
using System;
3+
using System.Text;
4+
5+
namespace fCraft {
6+
7+
/// <summary> Stores default properties for blocks in Minecraft Classic. (and CPE blocks). </summary>
8+
public static class DefaultSet {
9+
10+
/// <summary> Constructs a custom block, with the default properties of the given classic/CPE block. </summary>
11+
public static BlockDefinition MakeCustomBlock(Block b) {
12+
BlockDefinition def = new BlockDefinition();
13+
byte raw = (byte)b;
14+
def.BlockID = raw;
15+
def.Name = Name(raw);
16+
def.CollideType = Collide(b);
17+
def.Speed = 1;
18+
def.BlocksLight = BlocksLight(b);
19+
20+
def.TopTex = topTex[raw];
21+
def.BottomTex = bottomTex[raw];
22+
def.SetSidesTex(sideTex[raw]);
23+
def.WalkSound = (byte)StepSound(b);
24+
25+
def.FullBright = FullBright(b);
26+
def.Shape = Draw(b) == DrawType.Sprite ? (byte)0 : (byte)1;
27+
def.BlockDraw = Draw(b);
28+
if (def.BlockDraw == DrawType.Sprite)
29+
def.BlockDraw = DrawType.Transparent;
30+
31+
def.FogDensity = FogDensity(b);
32+
CustomColor fog = FogColor(b);
33+
def.FogR = fog.R; def.FogG = fog.G; def.FogB = fog.B;
34+
def.FallBack = raw;
35+
36+
def.MaxX = 16; def.MaxZ = Height(b); def.MaxY = 16;
37+
def.Version2 = true;
38+
return def;
39+
}
40+
41+
/// <summary> Gets the default height of a block. A value of 16 is full height. </summary>
42+
public static byte Height(Block b) {
43+
if (b == Block.Slab) return 8;
44+
if (b == Block.CobbleSlab) return 8;
45+
if (b == Block.Snow) return 2;
46+
return 16;
47+
}
48+
49+
/// <summary> Gets whether a block is full bright / light emitting by default. </summary>
50+
public static bool FullBright(Block b) {
51+
return b == Block.Lava || b == Block.StillLava
52+
|| b == Block.Magma || b == Block.Fire;
53+
}
54+
55+
/// <summary> Gets the default fog density of a block, in packed form. </summary>
56+
public static byte FogDensity(Block b) {
57+
if (b == Block.Water || b == Block.StillWater)
58+
return 11; // (128 * 0.1f - 1);
59+
if (b == Block.Lava || b == Block.StillLava)
60+
return 255; // (128 * 2 - 1);
61+
return 0;
62+
}
63+
64+
/// <summary> Gets the default fog color of a block. </summary>
65+
public static CustomColor FogColor(Block b) {
66+
if (b == Block.Water || b == Block.StillWater)
67+
return new CustomColor(5, 5, 51);
68+
if (b == Block.Lava || b == Block.StillLava)
69+
return new CustomColor(153, 25, 0);
70+
return default(CustomColor);
71+
}
72+
73+
/// <summary> Gets the default collide type of a block, see CollideType class. </summary>
74+
public static byte Collide(Block b) {
75+
if (b >= Block.Water && b <= Block.StillLava)
76+
return CollideType.SwimThrough;
77+
if (b == Block.Snow || b == Block.Air || Draw(b) == DrawType.Sprite)
78+
return CollideType.WalkThrough;
79+
return CollideType.Solid;
80+
}
81+
82+
/// <summary> Gets whether a block blocks light (prevents light passing through) by default. </summary>
83+
public static bool BlocksLight(Block b) {
84+
return !(b == Block.Glass || b == Block.Leaves
85+
|| b == Block.Air || Draw(b) == DrawType.Sprite);
86+
}
87+
88+
89+
/// <summary> Gets the default step sound of a block. </summary>
90+
public static SoundType StepSound(Block b) {
91+
if (b == Block.Glass) return SoundType.Stone;
92+
if (b == Block.Rope) return SoundType.Cloth;
93+
if (Draw(b) == DrawType.Sprite) return SoundType.None;
94+
95+
if (b >= Block.Red && b <= Block.White)
96+
return SoundType.Cloth;
97+
if (b >= Block.LightPink && b <= Block.Turquoise)
98+
return SoundType.Cloth;
99+
if (b == Block.Iron || b == Block.Gold)
100+
return SoundType.Metal;
101+
102+
if (b == Block.Books || b == Block.Wood
103+
|| b == Block.Log || b == Block.Crate || b == Block.Fire)
104+
return SoundType.Wood;
105+
106+
if (b == Block.Rope) return SoundType.Cloth;
107+
if (b == Block.Sand) return SoundType.Sand;
108+
if (b == Block.Snow) return SoundType.Snow;
109+
if (b == Block.Glass) return SoundType.Glass;
110+
if (b == Block.Dirt || b == Block.Gravel)
111+
return SoundType.Gravel;
112+
113+
if (b == Block.Grass || b == Block.Sapling || b == Block.TNT
114+
|| b == Block.Leaves || b == Block.Sponge)
115+
return SoundType.Grass;
116+
117+
if (b >= Block.YellowFlower && b <= Block.RedMushroom)
118+
return SoundType.Grass;
119+
if (b >= Block.Water && b <= Block.StillLava)
120+
return SoundType.None;
121+
if (b >= Block.Stone && b <= Block.StoneBrick)
122+
return SoundType.Stone;
123+
return SoundType.None;
124+
}
125+
126+
127+
/// <summary> Gets the default draw type of a block, see Draw class. </summary>
128+
public static byte Draw(Block b) {
129+
if (b == Block.Air || b == Block.None) return DrawType.Gas;
130+
if (b == Block.Leaves) return DrawType.TransparentThick;
131+
132+
if (b == Block.Ice || b == Block.Water || b == Block.StillWater)
133+
return DrawType.Translucent;
134+
if (b == Block.Glass || b == Block.Leaves)
135+
return DrawType.Transparent;
136+
137+
if (b >= Block.YellowFlower && b <= Block.RedMushroom)
138+
return DrawType.Sprite;
139+
if (b == Block.Sapling || b == Block.Rope || b == Block.Fire)
140+
return DrawType.Sprite;
141+
return DrawType.Opaque;
142+
}
143+
144+
145+
const string Names = "Air Stone Grass Dirt Cobblestone Wood Sapling Bedrock Water StillWater Lava" +
146+
" StillLava Sand Gravel GoldOre IronOre CoalOre Log Leaves Sponge Glass Red Orange Yellow Lime Green" +
147+
" Teal Aqua Cyan Blue Indigo Violet Magenta Pink Black Gray White Dandelion Rose BrownMushroom RedMushroom" +
148+
" Gold Iron DoubleSlab Slab Brick TNT Bookshelf MossyRocks Obsidian CobblestoneSlab Rope Sandstone" +
149+
" Snow Fire LightPink ForestGreen Brown DeepBlue Turquoise Ice CeramicTile Magma Pillar Crate StoneBrick";
150+
151+
static string Name(int block) {
152+
// Find start and end of this particular block name
153+
int start = 0;
154+
for (int i = 0; i < block; i++)
155+
start = Names.IndexOf(' ', start) + 1;
156+
int end = Names.IndexOf(' ', start);
157+
if (end == -1) end = Names.Length;
158+
159+
StringBuilder buffer = new StringBuilder();
160+
SplitUppercase(buffer, start, end);
161+
return buffer.ToString();
162+
}
163+
164+
static void SplitUppercase(StringBuilder buffer, int start, int end) {
165+
for (int i = start; i < end; i++) {
166+
char c = Names[i];
167+
bool upper = Char.IsUpper(c) && i > start;
168+
bool nextLower = i < end - 1 && !Char.IsUpper(Names[i + 1]);
169+
170+
if (upper && nextLower) {
171+
buffer.Append(' ');
172+
buffer.Append(Char.ToLower(c));
173+
} else {
174+
buffer.Append(c);
175+
}
176+
}
177+
}
178+
179+
180+
static byte[] topTex = new byte[] { 0, 1, 0, 2, 16, 4, 15, 17, 14, 14,
181+
30, 30, 18, 19, 32, 33, 34, 21, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
182+
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 24, 23, 6, 6, 7, 9, 4,
183+
36, 37, 16, 11, 25, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 26, 53, 52, };
184+
static byte[] sideTex = new byte[] { 0, 1, 3, 2, 16, 4, 15, 17, 14, 14,
185+
30, 30, 18, 19, 32, 33, 34, 20, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
186+
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 40, 39, 5, 5, 7, 8, 35,
187+
36, 37, 16, 11, 41, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 42, 53, 52, };
188+
static byte[] bottomTex = new byte[] { 0, 1, 2, 2, 16, 4, 15, 17, 14, 14,
189+
30, 30, 18, 19, 32, 33, 34, 21, 22, 48, 49, 64, 65, 66, 67, 68, 69, 70, 71,
190+
72, 73, 74, 75, 76, 77, 78, 79, 13, 12, 29, 28, 56, 55, 6, 6, 7, 10, 4,
191+
36, 37, 16, 11, 57, 50, 38, 80, 81, 82, 83, 84, 51, 54, 86, 58, 53, 52 };
192+
}
193+
194+
public static class DrawType {
195+
public const byte Opaque = 0;
196+
public const byte Transparent = 1;
197+
public const byte TransparentThick = 2; // e.g. leaves render all neighbours
198+
public const byte Translucent = 3;
199+
public const byte Gas = 4;
200+
public const byte Sprite = 5;
201+
}
202+
203+
public static class CollideType {
204+
public const byte WalkThrough = 0; // Gas (usually also used by sprite)
205+
public const byte SwimThrough = 1; // Liquid
206+
public const byte Solid = 2; // Solid
207+
public const byte Ice = 3; // Solid and partially slidable on.
208+
public const byte SlipperyIce = 4; // Solid and fully slidable on.
209+
public const byte LiquidWater = 5; // Water style 'swimming'/'bobbing'
210+
public const byte LiquidLava = 6; // Lava style 'swimming'/'bobbing'
211+
212+
public static bool IsSolid(byte collide) {
213+
return collide >= Solid && collide <= SlipperyIce;
214+
}
215+
}
216+
217+
public enum SoundType : byte {
218+
None, Wood, Gravel, Grass, Stone,
219+
Metal, Glass, Cloth, Sand, Snow,
220+
}
221+
}

fCraft/fCraft.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<Compile Include="Player\PlayerDB.cs" />
218218
<Compile Include="Player\PlayerInfo.cs" />
219219
<Compile Include="Properties\AssemblyInfo.cs" />
220+
<Compile Include="World\DefaultSet.cs" />
220221
<Compile Include="World\Entities.cs" />
221222
<Compile Include="Zone\SpecialZone.Affect.cs" />
222223
<Compile Include="Zone\SpecialZone.cs" />

0 commit comments

Comments
 (0)