Skip to content

Commit

Permalink
use more pattern matching in code (#359)
Browse files Browse the repository at this point in the history
* use switch instead of if

* code review

* pattern matching everywhere!

* i forgot white
  • Loading branch information
InFTord committed May 29, 2023
1 parent 853d1a8 commit 8d64678
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 101 deletions.
33 changes: 18 additions & 15 deletions Obsidian.API/Plugins/PluginBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ public Task FriendlyInvokeAsync(string methodName, params object[] args)
try
{
var result = method?.Invoke(this, args);
if (result is Task task)
return task;
else if (result is ValueTask valueTask)
return valueTask.AsTask();
return Task.FromResult(result);
return result switch
{
Task task => task,
ValueTask valueTask => valueTask.AsTask(),
_ => Task.FromResult(result)
};
}
catch (Exception e)
{
Expand All @@ -68,11 +69,12 @@ public Task InvokeAsync(string methodName, params object[] args)
{
var method = GetMethod(methodName, args);
var result = method?.Invoke(this, args);
if (result is Task task)
return task;
else if (result is ValueTask valueTask)
return valueTask.AsTask();
return Task.FromResult(result);
return result switch
{
Task task => task,
ValueTask valueTask => valueTask.AsTask(),
_ => Task.FromResult(result)
};
}

/// <summary>
Expand All @@ -92,11 +94,12 @@ public Task InvokeAsync(string methodName, params object[] args)
{
var method = GetMethod(methodName, args);
var result = method?.Invoke(this, args);
if (result is Task<T?> task)
return task;
else if (result is ValueTask<T?> valueTask)
return valueTask.AsTask();
return Task.FromResult((T?)result);
return result switch
{
Task<T?> task => task,
ValueTask<T?> valueTask => valueTask.AsTask(),
_ => Task.FromResult((T?)result)
};
}

/// <summary>
Expand Down
25 changes: 13 additions & 12 deletions Obsidian.Nbt/NbtReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ public NbtReader() : this(new MemoryStream()) { }

public NbtReader(Stream input, NbtCompression compressionMode = NbtCompression.None)
{
if (compressionMode == NbtCompression.GZip)
this.BaseStream = new GZipStream(input, CompressionMode.Decompress);
else if (compressionMode == NbtCompression.ZLib)
this.BaseStream = new ZLibStream(input, CompressionMode.Decompress);
else
this.BaseStream = input;
this.BaseStream = compressionMode switch
{
NbtCompression.GZip => new GZipStream(input, CompressionMode.Decompress),
NbtCompression.ZLib => new ZLibStream(input, CompressionMode.Decompress),
_ => input
};
}

public NbtTagType ReadTagType()
{
var type = this.BaseStream.ReadByte();

if (type <= 0)
return NbtTagType.End;
else if (type > (byte)NbtTagType.LongArray)
throw new ArgumentOutOfRangeException($"Tag is out of range: {(NbtTagType)type}");

return (NbtTagType)type;
return type switch
{
<= 0 => NbtTagType.End,
> (byte)NbtTagType.LongArray => throw new ArgumentOutOfRangeException(
$"Tag is out of range: {(NbtTagType)type}"),
_ => (NbtTagType)type
};
}

private INbtTag GetCurrentTag(NbtTagType type, bool readName = true)
Expand Down
24 changes: 12 additions & 12 deletions Obsidian.Nbt/NbtWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public sealed partial class NbtWriter : IDisposable, IAsyncDisposable

public NbtWriter(Stream outstream, NbtCompression compressionMode = NbtCompression.None)
{
if (compressionMode == NbtCompression.GZip)
this.BaseStream = new GZipStream(outstream, CompressionMode.Compress);
else if (compressionMode == NbtCompression.ZLib)
this.BaseStream = new ZLibStream(outstream, CompressionMode.Compress);
else
this.BaseStream = outstream;
this.BaseStream = compressionMode switch
{
NbtCompression.GZip => new GZipStream(outstream, CompressionMode.Compress),
NbtCompression.ZLib => new ZLibStream(outstream, CompressionMode.Compress),
_ => outstream
};
}

public NbtWriter(Stream outstream, string name)
Expand All @@ -38,12 +38,12 @@ public NbtWriter(Stream outstream, string name)

public NbtWriter(Stream outstream, NbtCompression compressionMode, string name)
{
if (compressionMode == NbtCompression.GZip)
this.BaseStream = new GZipStream(outstream, CompressionMode.Compress);
else if (compressionMode == NbtCompression.ZLib)
this.BaseStream = new ZLibStream(outstream, CompressionMode.Compress);
else
this.BaseStream = outstream;
this.BaseStream = compressionMode switch
{
NbtCompression.GZip => new GZipStream(outstream, CompressionMode.Compress),
NbtCompression.ZLib => new ZLibStream(outstream, CompressionMode.Compress),
_ => outstream
};

this.Write(NbtTagType.Compound);
this.WriteStringInternal(name);
Expand Down
12 changes: 6 additions & 6 deletions Obsidian/ChunkData/Palette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public static class Palette
{
public static IPalette<IBlock> DetermineBlockPalette(this byte bitsPerEntry)
{
if (bitsPerEntry <= 4)
return new IndirectPalette(4);
else if (bitsPerEntry > 4 && bitsPerEntry <= 8)
return new IndirectPalette(bitsPerEntry);

return new GlobalBlockStatePalette(BlocksRegistry.GlobalBitsPerBlocks);
return bitsPerEntry switch
{
<= 4 => new IndirectPalette(4),
> 4 and <= 8 => new IndirectPalette(bitsPerEntry),
_ => new GlobalBlockStatePalette(BlocksRegistry.GlobalBitsPerBlocks)
};
}

public static IPalette<Biome> DetermineBiomePalette(this byte bitsPerEntry)
Expand Down
13 changes: 6 additions & 7 deletions Obsidian/Commands/MainCommandModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ public async Task TPSAsync(CommandContext ctx)
{
var sender = ctx.Sender;

ChatColor color;
if (ctx.Server.Tps > 15)
color = ChatColor.BrightGreen;
else if (ctx.Server.Tps > 10)
color = ChatColor.Yellow;
else
color = ChatColor.Red;
ChatColor color = ctx.Server.Tps switch
{
> 15 => ChatColor.BrightGreen,
> 10 => ChatColor.Yellow,
_ => ChatColor.Red
};

await sender.SendMessageAsync($"{ChatColor.Gold}Current server TPS: {color}{ctx.Server.Tps}");
}
Expand Down
17 changes: 6 additions & 11 deletions Obsidian/Net/MinecraftStream.Reading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@ public bool ReadBoolean()
public async Task<bool> ReadBooleanAsync()
{
var value = (int)await this.ReadByteAsync();
if (value == 0x00)
return value switch
{
return false;
}
else if (value == 0x01)
{
return true;
}
else
{
throw new ArgumentOutOfRangeException("Byte returned by stream is out of range (0x00 or 0x01)", nameof(BaseStream));
}
0x00 => false,
0x01 => true,
_ => throw new ArgumentOutOfRangeException("Byte returned by stream is out of range (0x00 or 0x01)",
nameof(BaseStream))
};
}

[ReadMethod]
Expand Down
10 changes: 6 additions & 4 deletions Obsidian/Net/Packets/Play/Serverbound/ClickContainerPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,12 @@ private void HandleDragClick(BaseContainer container, Player player, int value)
{
if (ClickedSlot == Outsideinventory)
{
if (Button == 0 || Button == 4 || Button == 8)
player.isDragging = true;
else if (Button == 2 || Button == 6 || Button == 10)
player.isDragging = false;
player.isDragging = Button switch
{
0 or 4 or 8 => true,
2 or 6 or 10 => false,
_ => player.isDragging
};
}
else if (player.isDragging)
{
Expand Down
32 changes: 14 additions & 18 deletions Obsidian/Utilities/XpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ public static class XpHelper
{
public static int TotalExperienceFromLevel(int level)
{
if (level < 0)
throw new ArgumentOutOfRangeException(nameof(level));

if (level < 17)
return level * (level + 6);
else if (level < 32)
return (int)(level * (2.5f * level - 40.5f) + 360f);

return (int)(level * (4.5f * level - 162.5f) + 2220f);
return level switch
{
< 0 => throw new ArgumentOutOfRangeException(nameof(level)),
< 17 => level * (level + 6),
< 32 => (int)(level * (2.5f * level - 40.5f) + 360f),
_ => (int)(level * (4.5f * level - 162.5f) + 2220f)
};
}
public static int ExperienceRequiredForNextLevel(int currentLevel)
{
if (currentLevel < 0)
throw new ArgumentOutOfRangeException(nameof(currentLevel));

if (currentLevel < 16)
return 2 * currentLevel + 7;
else if (currentLevel < 31)
return 5 * currentLevel - 38;

return 9 * currentLevel - 158;
return currentLevel switch
{
< 0 => throw new ArgumentOutOfRangeException(nameof(currentLevel)),
< 16 => 2 * currentLevel + 7,
< 31 => 5 * currentLevel - 38,
_ => 9 * currentLevel - 158
};
}
}
27 changes: 11 additions & 16 deletions Obsidian/WorldData/Decorators/ErodedBadlandsDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,17 @@ public override void Decorate()
{
//TODO SET BLOCK COLOR
var a = (pos.Y + y) % 15;
if (a == 15)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.BrownTerracotta);
else if (a == 14)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.WhiteTerracotta);
else if (a == 13)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.GrayTerracotta);
else if (a >= 11)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.YellowTerracotta);
else if (a == 8 || a == 9)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.RedTerracotta);
else if (a == 6)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.OrangeTerracotta);
else if (a == 3)
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.YellowTerracotta);
else
chunk.SetBlock(pos + (0, y, 0), BlocksRegistry.Terracotta);
chunk.SetBlock(pos + (0, y, 0), a switch
{
15 => BlocksRegistry.BrownTerracotta,
14 => BlocksRegistry.WhiteTerracotta,
13 => BlocksRegistry.GrayTerracotta,
>= 11 => BlocksRegistry.YellowTerracotta,
8 or 9 => BlocksRegistry.RedTerracotta,
6 => BlocksRegistry.OrangeTerracotta,
3 => BlocksRegistry.YellowTerracotta,
_ => BlocksRegistry.Terracotta
});
}

var bushNoise = noise.Decoration.GetValue(worldX * 0.1, 0, worldZ * 0.1);
Expand Down

0 comments on commit 8d64678

Please sign in to comment.