Fix all warnings & Trim (#2226)

* Fix AutoFishing crash
* Fix all warnings
* Remove DotNetZip.
* Fix the usage of HttpClient.
This commit is contained in:
BruceChen 2022-10-02 18:31:08 +08:00 committed by GitHub
parent 4aa6c1c99f
commit 1d52d1eadd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 2201 additions and 43564 deletions

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using MinecraftClient.Mapping.BlockPalettes;
namespace MinecraftClient.Mapping
@ -18,7 +15,7 @@ namespace MinecraftClient.Mapping
/// Get or set global block ID to Material mapping
/// The global Palette is a concept introduced with Minecraft 1.13
/// </summary>
public static BlockPalette Palette { get; set; }
public static BlockPalette Palette { get; set; } = new Palette112();
/// <summary>
/// Storage for block ID and metadata, as ushort for compatibility, performance and lower memory footprint
@ -45,13 +42,13 @@ namespace MinecraftClient.Mapping
if (Palette.IdHasMetadata)
{
if (value > (ushort.MaxValue >> 4) || value < 0)
throw new ArgumentOutOfRangeException("value", "Invalid block ID. Accepted range: 0-4095");
throw new ArgumentOutOfRangeException(nameof(value), "Invalid block ID. Accepted range: 0-4095");
blockIdAndMeta = (ushort)(value << 4 | BlockMeta);
}
else
{
if (value > ushort.MaxValue || value < 0)
throw new ArgumentOutOfRangeException("value", "Invalid block ID. Accepted range: 0-65535");
throw new ArgumentOutOfRangeException(nameof(value), "Invalid block ID. Accepted range: 0-65535");
blockIdAndMeta = (ushort)value;
}
}
@ -100,9 +97,9 @@ namespace MinecraftClient.Mapping
{
if (!Palette.IdHasMetadata)
throw new InvalidOperationException("Current global Palette does not support block Metadata");
this.blockIdAndMeta = 0;
this.BlockId = type;
this.BlockMeta = metadata;
blockIdAndMeta = 0;
BlockId = type;
BlockMeta = metadata;
}
/// <summary>
@ -112,7 +109,7 @@ namespace MinecraftClient.Mapping
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public Block(ushort typeAndMeta)
{
this.blockIdAndMeta = typeAndMeta;
blockIdAndMeta = typeAndMeta;
}
/// <summary>

View file

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
{

View file

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping.BlockPalettes
@ -42,13 +42,13 @@ namespace MinecraftClient.Mapping.BlockPalettes
/// <param name="outputEnum">output path for material.cs</param>
/// <remarks>java -cp minecraft_server.jar net.minecraft.data.Main --reports</remarks>
/// <returns>state => block name mappings</returns>
public static void JsonToClass(string blocksJsonFile, string outputClass, string outputEnum = null)
public static void JsonToClass(string blocksJsonFile, string outputClass, string? outputEnum = null)
{
string outputPalettePath = Path.Combine(Path.GetDirectoryName(blocksJsonFile), outputClass + "XXX.cs");
string outputEnumPath = Path.Combine(Path.GetDirectoryName(blocksJsonFile), outputEnum + "XXX.cs");
string outputPalettePath = Path.Combine(Path.GetDirectoryName(blocksJsonFile)!, outputClass + "XXX.cs");
string outputEnumPath = Path.Combine(Path.GetDirectoryName(blocksJsonFile)!, outputEnum + "XXX.cs");
HashSet<int> knownStates = new HashSet<int>();
Dictionary<string, HashSet<int>> blocks = new Dictionary<string, HashSet<int>>();
HashSet<int> knownStates = new();
Dictionary<string, HashSet<int>> blocks = new();
Json.JSONData palette = Json.ParseJson(File.ReadAllText(blocksJsonFile, Encoding.UTF8));
foreach (KeyValuePair<string, Json.JSONData> item in palette.Properties)
@ -76,17 +76,16 @@ namespace MinecraftClient.Mapping.BlockPalettes
}
}
HashSet<string> materials = new HashSet<string>();
List<string> outFile = new List<string>();
HashSet<string> materials = new();
List<string> outFile = new();
outFile.AddRange(new[] {
"using System;",
"using System.Collections.Generic;",
"",
"namespace MinecraftClient.Mapping.BlockPalettes",
"{",
" public class PaletteXXX : BlockPalette",
" {",
" private static Dictionary<int, Material> materials = new Dictionary<int, Material>();",
" private static readonly Dictionary<int, Material> materials = new();",
"",
" static PaletteXXX()",
" {",
@ -103,7 +102,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
if (idList.Count > 1)
{
idList.Sort();
Queue<int> idQueue = new Queue<int>(idList);
Queue<int> idQueue = new(idList);
while (idQueue.Count > 0)
{

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
@ -11,7 +10,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
/// </summary>
public class Palette112 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>()
private static readonly Dictionary<int, Material> materials = new()
{
{ 0, Material.Air },
{ 1, Material.Stone },

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
@ -9,7 +8,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
/// </summary>
public class Palette113 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette113()
{

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
@ -9,7 +8,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
/// </summary>
public class Palette114 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette114()
{

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
@ -9,7 +8,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
/// </summary>
public class Palette115 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette115()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
{
public class Palette116 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette116()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
{
public class Palette117 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette117()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.BlockPalettes
{
public class Palette119 : BlockPalette
{
private static Dictionary<int, Material> materials = new Dictionary<int, Material>();
private static readonly Dictionary<int, Material> materials = new();
static Palette119()
{

View file

@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace MinecraftClient.Mapping
{

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MinecraftClient.Mapping
{

View file

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
{
/// <summary>

View file

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
{
/// <summary>

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
{
@ -120,7 +117,7 @@ namespace MinecraftClient.Mapping
/// </summary>
public Dimension()
{
this.Name = "minecraft:overworld";
Name = "minecraft:overworld";
}
/// <summary>
@ -130,73 +127,66 @@ namespace MinecraftClient.Mapping
/// <param name="nbt">The dimension type (NBT Tag Compound)</param>
public Dimension(string name, Dictionary<string, object> nbt)
{
if (name == null)
throw new ArgumentNullException("name");
if (nbt == null)
throw new ArgumentNullException("nbt Data");
Name = name ?? throw new ArgumentNullException(nameof(name));
this.Name = name;
if (nbt == null)
throw new ArgumentNullException(nameof(nbt));
if (nbt.ContainsKey("piglin_safe"))
this.piglinSafe = 1 == (byte)nbt["piglin_safe"];
piglinSafe = Convert.ToBoolean(nbt["piglin_safe"]);
if (nbt.ContainsKey("monster_spawn_light_level"))
{
try
{
var monsterSpawnLightLevelObj = nbt["monster_spawn_light_level"];
if (monsterSpawnLightLevelObj.GetType() == typeof(int))
this.monsterSpawnMinLightLevel = this.monsterSpawnMaxLightLevel = (int)monsterSpawnLightLevelObj;
else
try
{
monsterSpawnMinLightLevel = monsterSpawnMaxLightLevel = Convert.ToInt32(monsterSpawnLightLevelObj);
}
catch (Exception)
{
var inclusive = (Dictionary<string, object>)(((Dictionary<string, object>)monsterSpawnLightLevelObj)["value"]);
this.monsterSpawnMinLightLevel = (int)inclusive["min_inclusive"];
this.monsterSpawnMaxLightLevel = (int)inclusive["max_inclusive"];
monsterSpawnMinLightLevel = Convert.ToInt32(inclusive["min_inclusive"]);
monsterSpawnMaxLightLevel = Convert.ToInt32(inclusive["max_inclusive"]);
}
}
catch (KeyNotFoundException) { }
}
if (nbt.ContainsKey("monster_spawn_block_light_limit"))
this.monsterSpawnBlockLightLimit = (int)nbt["monster_spawn_block_light_limit"];
monsterSpawnBlockLightLimit = Convert.ToInt32(nbt["monster_spawn_block_light_limit"]);
if (nbt.ContainsKey("natural"))
this.natural = 1 == (byte)nbt["natural"];
natural = Convert.ToBoolean(nbt["natural"]);
if (nbt.ContainsKey("ambient_light"))
this.ambientLight = (float)nbt["ambient_light"];
ambientLight = (float)Convert.ToDouble(nbt["ambient_light"]);
if (nbt.ContainsKey("fixed_time"))
this.fixedTime = (long)nbt["fixed_time"];
fixedTime = Convert.ToInt64(nbt["fixed_time"]);
if (nbt.ContainsKey("infiniburn"))
this.infiniburn = (string)nbt["infiniburn"];
infiniburn = Convert.ToString(nbt["infiniburn"]) ?? string.Empty;
if (nbt.ContainsKey("respawn_anchor_works"))
this.respawnAnchorWorks = 1 == (byte)nbt["respawn_anchor_works"];
respawnAnchorWorks = Convert.ToBoolean(nbt["respawn_anchor_works"]);
if (nbt.ContainsKey("has_skylight"))
this.hasSkylight = 1 == (byte)nbt["has_skylight"];
hasSkylight = Convert.ToBoolean(nbt["has_skylight"]);
if (nbt.ContainsKey("bed_works"))
this.bedWorks = 1 == (byte)nbt["bed_works"];
bedWorks = Convert.ToBoolean(nbt["bed_works"]);
if (nbt.ContainsKey("effects"))
this.effects = (string)nbt["effects"];
effects = Convert.ToString(nbt["effects"]) ?? string.Empty;
if (nbt.ContainsKey("has_raids"))
this.hasRaids = 1 == (byte)nbt["has_raids"];
hasRaids = Convert.ToBoolean(nbt["has_raids"]);
if (nbt.ContainsKey("min_y"))
this.minY = (int)nbt["min_y"];
minY = Convert.ToInt32(nbt["min_y"]);
if (nbt.ContainsKey("height"))
this.height = (int)nbt["height"];
height = Convert.ToInt32(nbt["height"]);
if (nbt.ContainsKey("min_y") && nbt.ContainsKey("height"))
this.maxY = this.minY + this.height;
maxY = minY + height;
if (nbt.ContainsKey("logical_height") && nbt["logical_height"].GetType() != typeof(byte))
this.logicalHeight = (int)nbt["logical_height"];
logicalHeight = Convert.ToInt32(nbt["logical_height"]);
if (nbt.ContainsKey("coordinate_scale"))
{
var coordinateScaleObj = nbt["coordinate_scale"];
if (coordinateScaleObj.GetType() == typeof(float))
this.coordinateScale = (float)coordinateScaleObj;
else
this.coordinateScale = (double)coordinateScaleObj;
}
coordinateScale = Convert.ToDouble(nbt["coordinate_scale"]);
if (nbt.ContainsKey("ultrawarm"))
this.ultrawarm = 1 == (byte)nbt["ultrawarm"];
ultrawarm = Convert.ToBoolean(nbt["ultrawarm"]);
if (nbt.ContainsKey("has_ceiling"))
this.hasCeiling = 1 == (byte)nbt["has_ceiling"];
hasCeiling = Convert.ToBoolean(nbt["has_ceiling"]);
}
}
}

View file

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
/// <summary>
/// Represents a unit movement in the world

View file

@ -23,12 +23,12 @@ namespace MinecraftClient.Mapping
/// Nickname of the entity if it is a player.
/// </summary>
public string? Name;
/// <summary>
/// CustomName of the entity.
/// </summary>
public string CustomNameJson;
public string? CustomNameJson;
/// <summary>
/// IsCustomNameVisible of the entity.
/// </summary>
@ -37,8 +37,8 @@ namespace MinecraftClient.Mapping
/// <summary>
/// CustomName of the entity.
/// </summary>
public string CustomName;
public string? CustomName;
/// <summary>
/// Latency of the entity if it is a player.
/// </summary>
@ -77,21 +77,21 @@ namespace MinecraftClient.Mapping
/// Health of the entity
/// </summary>
public float Health;
/// <summary>
/// Item of the entity if ItemFrame or Item
/// </summary>
public Item Item;
/// <summary>
/// Entity pose in the Minecraft world
/// </summary>
public EntityPose Pose;
/// <summary>
/// Entity metadata
/// </summary>
public Dictionary<int, object?> Metadata;
public Dictionary<int, object?>? Metadata;
/// <summary>
/// Entity equipment
@ -107,11 +107,11 @@ namespace MinecraftClient.Mapping
public Entity(int ID, EntityType type, Location location)
{
this.ID = ID;
this.Type = type;
this.Location = location;
this.Health = 1.0f;
this.Equipment = new Dictionary<int, Item>();
this.Item = new Item(ItemType.Air, 0, null);
Type = type;
Location = location;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
}
/// <summary>
@ -123,14 +123,14 @@ namespace MinecraftClient.Mapping
public Entity(int ID, EntityType type, Location location, byte yaw, byte pitch, int objectData)
{
this.ID = ID;
this.Type = type;
this.Location = location;
this.Health = 1.0f;
this.Equipment = new Dictionary<int, Item>();
this.Item = new Item(ItemType.Air, 0, null);
this.Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
this.Pitch = pitch * (1 / 256) * 360;
this.ObjectData = objectData;
Type = type;
Location = location;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
Pitch = pitch * (1 / 256) * 360;
ObjectData = objectData;
}
/// <summary>
@ -144,15 +144,15 @@ namespace MinecraftClient.Mapping
public Entity(int ID, EntityType type, Location location, Guid uuid, string? name, byte yaw, byte pitch)
{
this.ID = ID;
this.Type = type;
this.Location = location;
this.UUID = uuid;
this.Name = name;
this.Health = 1.0f;
this.Equipment = new Dictionary<int, Item>();
this.Item = new Item(ItemType.Air, 0, null);
this.Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
this.Pitch = pitch * (1 / 256) * 360;
Type = type;
Location = location;
UUID = uuid;
Name = name;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
Pitch = pitch * (1 / 256) * 360;
}
}
}

View file

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{
@ -17,7 +14,7 @@ namespace MinecraftClient.Mapping.EntityPalettes
/// Get mapping dictionary for pre-1.14 non-living entities.
/// </summary>
/// <returns>Palette dictionary for non-living entities (pre-1.14)</returns>
protected virtual Dictionary<int, EntityType> GetDictNonLiving()
protected virtual Dictionary<int, EntityType>? GetDictNonLiving()
{
return null;
}
@ -30,7 +27,7 @@ namespace MinecraftClient.Mapping.EntityPalettes
public EntityType FromId(int id, bool living)
{
Dictionary<int, EntityType> entityTypes = GetDict();
Dictionary<int, EntityType> entityTypesNonLiving = GetDictNonLiving();
Dictionary<int, EntityType>? entityTypesNonLiving = GetDictNonLiving();
if (entityTypesNonLiving != null && !living)
{

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
@ -9,7 +8,7 @@ namespace MinecraftClient.Mapping.EntityPalettes
/// </summary>
public class EntityPalette114 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette114()
{

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
@ -9,7 +8,7 @@ namespace MinecraftClient.Mapping.EntityPalettes
/// </summary>
public class EntityPalette115 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette115()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{
public class EntityPalette1161 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette1161()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{
public class EntityPalette1162 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette1162()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{
public class EntityPalette117 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette117()
{

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Mapping.EntityPalettes
{
public class EntityPalette119 : EntityPalette
{
private static Dictionary<int, EntityType> mappings = new Dictionary<int, EntityType>();
private static readonly Dictionary<int, EntityType> mappings = new();
static EntityPalette119()
{

View file

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
public static class EntityTypeExtensions
{

View file

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
public enum InteractType
{

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace MinecraftClient.Mapping
{
@ -159,7 +156,7 @@ namespace MinecraftClient.Mapping
/// <returns>New location</returns>
public Location ToFloor()
{
return new Location(Math.Floor(this.X), Math.Floor(this.Y), Math.Floor(this.Z));
return new Location(Math.Floor(X), Math.Floor(Y), Math.Floor(Z));
}
/// <summary>
@ -168,7 +165,7 @@ namespace MinecraftClient.Mapping
/// <returns>New location</returns>
public Location ToCenter()
{
return new Location(Math.Floor(this.X) + 0.5, this.Y, Math.Floor(this.Z) + 0.5);
return new Location(Math.Floor(X) + 0.5, Y, Math.Floor(Z) + 0.5);
}
/// <summary>
@ -273,15 +270,15 @@ namespace MinecraftClient.Mapping
/// </summary>
/// <param name="obj">Object to compare to</param>
/// <returns>TRUE if the locations are equals</returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj == null)
return false;
if (obj is Location)
if (obj is Location location)
{
return ((int)this.X) == ((int)((Location)obj).X)
&& ((int)this.Y) == ((int)((Location)obj).Y)
&& ((int)this.Z) == ((int)((Location)obj).Z);
return ((int)X) == ((int)location.X)
&& ((int)Y) == ((int)location.Y)
&& ((int)Z) == ((int)location.Z);
}
return false;
}
@ -293,6 +290,11 @@ namespace MinecraftClient.Mapping
/// <param name="loc2">Second location to compare</param>
/// <returns>TRUE if the locations are equals</returns>
public static bool operator ==(Location loc1, Location loc2)
{
return loc1.Equals(loc2);
}
public static bool operator ==(Location? loc1, Location? loc2)
{
if (loc1 == null && loc2 == null)
return true;
@ -308,6 +310,11 @@ namespace MinecraftClient.Mapping
/// <param name="loc2">Second location to compare</param>
/// <returns>TRUE if the locations are equals</returns>
public static bool operator !=(Location loc1, Location loc2)
{
return !loc1.Equals(loc2);
}
public static bool operator !=(Location? loc1, Location? loc2)
{
if (loc1 == null && loc2 == null)
return false;

View file

@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinecraftClient.Protocol.Handlers;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
public class MapIcon
{

View file

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
public enum MapIconType
{

View file

@ -1,5 +1,5 @@
using MinecraftClient.Inventory;
using System.Collections.Generic;
using System.Collections.Generic;
using MinecraftClient.Inventory;
namespace MinecraftClient.Mapping
{
@ -8,7 +8,7 @@ namespace MinecraftClient.Mapping
// Made with the following ressources: https://minecraft.fandom.com/wiki/Breaking
// Sorted in alphabetical order.
// Minable by Any Pickaxe.
private static readonly List<Material> pickaxeTier0 = new List<Material>()
private static readonly List<Material> pickaxeTier0 = new()
{
Material.ActivatorRail,
Material.Andesite,
@ -281,7 +281,7 @@ namespace MinecraftClient.Mapping
Material.YellowTerracotta,
};
// Minable by Stone, iron, diamond, netherite.
private static readonly List<Material> pickaxeTier1 = new List<Material>()
private static readonly List<Material> pickaxeTier1 = new()
{
Material.CopperOre,
Material.CopperBlock,
@ -329,7 +329,7 @@ namespace MinecraftClient.Mapping
Material.WeatheredCutCopper,
};
// Minable by Iron, diamond, netherite.
private static readonly List<Material> pickaxeTier2 = new List<Material>()
private static readonly List<Material> pickaxeTier2 = new()
{
Material.DeepslateDiamondOre,
Material.DeepslateEmeraldOre,
@ -345,7 +345,7 @@ namespace MinecraftClient.Mapping
Material.RedstoneOre,
};
// Minable by Diamond, Netherite.
private static readonly List<Material> pickaxeTier3 = new List<Material>()
private static readonly List<Material> pickaxeTier3 = new()
{
Material.AncientDebris,
Material.CryingObsidian,
@ -355,7 +355,7 @@ namespace MinecraftClient.Mapping
};
// Every shovel can mine every block (speed difference).
private static readonly List<Material> shovel = new List<Material>()
private static readonly List<Material> shovel = new()
{
Material.BlackConcretePowder,
Material.BlueConcretePowder,
@ -393,7 +393,7 @@ namespace MinecraftClient.Mapping
Material.YellowConcretePowder,
};
// Every axe can mine every block (speed difference).
private static readonly List<Material> axe = new List<Material>()
private static readonly List<Material> axe = new()
{
Material.AcaciaButton,
Material.AcaciaDoor,
@ -578,7 +578,7 @@ namespace MinecraftClient.Mapping
Material.YellowWallBanner,
};
// Every block a shear can mine.
private static readonly List<Material> shears = new List<Material>()
private static readonly List<Material> shears = new()
{
Material.AcaciaLeaves,
Material.AzaleaLeaves,
@ -607,7 +607,7 @@ namespace MinecraftClient.Mapping
Material.YellowWool,
};
// Every block that is mined with a sword.
private static readonly List<Material> sword = new List<Material>()
private static readonly List<Material> sword = new()
{
Material.Bamboo,
Material.Cobweb,
@ -620,7 +620,7 @@ namespace MinecraftClient.Mapping
Material.InfestedStoneBricks,
};
// Every block that can be mined with a hoe.
private static readonly List<Material> hoe = new List<Material>()
private static readonly List<Material> hoe = new()
{
Material.AcaciaLeaves,
Material.BirchLeaves,
@ -639,14 +639,14 @@ namespace MinecraftClient.Mapping
Material.WetSponge,
};
// Liquids
private static readonly List<Material> bucket = new List<Material>()
private static readonly List<Material> bucket = new()
{
Material.Lava,
Material.Water
};
// Unbreakable Blocks
private static readonly List<Material> unbreakable = new List<Material>()
private static readonly List<Material> unbreakable = new()
{
Material.Air,
Material.Barrier,
@ -773,7 +773,7 @@ namespace MinecraftClient.Mapping
ItemType.Bucket,
};
}
else { return new ItemType[0]; }
else { return System.Array.Empty<ItemType>(); }
}
public static bool IsUnbreakable(Material block) { return unbreakable.Contains(block); }

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
namespace MinecraftClient.Mapping
{
/// <summary>
/// Defines extension methods for the Material enumeration

View file

@ -23,7 +23,7 @@ namespace MinecraftClient.Mapping
{
if (Settings.GravityEnabled)
{
Location onFoots = new Location(location.X, Math.Floor(location.Y), location.Z);
Location onFoots = new(location.X, Math.Floor(location.Y), location.Z);
Location belowFoots = Move(location, Direction.Down);
if (location.Y > Math.Truncate(location.Y) + 0.0001)
{
@ -94,7 +94,7 @@ namespace MinecraftClient.Mapping
{
//Use MC-Like falling algorithm
double Y = start.Y;
Queue<Location> fallSteps = new Queue<Location>();
Queue<Location> fallSteps = new();
fallSteps.Enqueue(start);
double motionPrev = motionY;
motionY -= 0.08D;
@ -102,7 +102,7 @@ namespace MinecraftClient.Mapping
Y += motionY;
if (Y < goal.Y)
return new Queue<Location>(new[] { goal });
else
else
return new Queue<Location>(new[] { new Location(start.X, Y, start.Z) });
}
else
@ -120,7 +120,7 @@ namespace MinecraftClient.Mapping
movementSteps.Enqueue(start + step * i);
return movementSteps;
}
else
else
return new Queue<Location>(new[] { goal });
}
}
@ -142,7 +142,7 @@ namespace MinecraftClient.Mapping
/// <returns>A list of locations, or null if calculation failed</returns>
public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout)
{
CancellationTokenSource cts = new CancellationTokenSource();
CancellationTokenSource cts = new();
Task<Queue<Location>?> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token));
pathfindingTask.Wait(timeout);
if (!pathfindingTask.IsCompleted)
@ -171,7 +171,7 @@ namespace MinecraftClient.Mapping
{
// This is a bad configuration
if (minOffset > maxOffset)
throw new ArgumentException("minOffset must be lower or equal to maxOffset", "minOffset");
throw new ArgumentException("minOffset must be lower or equal to maxOffset", nameof(minOffset));
// Round start coordinates for easier calculation
Location startLower = start.ToFloor();
@ -184,18 +184,18 @@ namespace MinecraftClient.Mapping
///---///
// Prepare variables and datastructures for A*
///---///
// Dictionary that contains the relation between all coordinates and resolves the final path
Dictionary<Location, Location> CameFrom = new Dictionary<Location, Location>();
Dictionary<Location, Location> CameFrom = new();
// Create a Binary Heap for all open positions => Allows fast access to Nodes with lowest scores
BinaryHeap openSet = new BinaryHeap();
BinaryHeap openSet = new();
// Dictionary to keep track of the G-Score of every location
Dictionary<Location, int> gScoreDict = new Dictionary<Location, int>();
Dictionary<Location, int> gScoreDict = new();
// Set start values for variables
openSet.Insert(0, (int)startLower.DistanceSquared(goalLower), startLower);
gScoreDict[startLower] = 0;
BinaryHeap.Node current = null;
BinaryHeap.Node? current = null;
///---///
// Start of A*
@ -239,7 +239,7 @@ namespace MinecraftClient.Mapping
}
//// Goal could not be reached. Set the path to the closest location if close enough
if (current != null && (maxOffset == int.MaxValue || openSet.MinH_ScoreNode.H_score <= maxOffset))
if (current != null && openSet.MinH_ScoreNode != null && (maxOffset == int.MaxValue || openSet.MinH_ScoreNode.H_score <= maxOffset))
return ReconstructPath(CameFrom, openSet.MinH_ScoreNode.Location, start, goal);
else
return null;
@ -306,17 +306,17 @@ namespace MinecraftClient.Mapping
public Node(int g_score, int h_score, Location loc)
{
this.G_score = g_score;
this.H_score = h_score;
G_score = g_score;
H_score = h_score;
Location = loc;
}
}
// List which contains all nodes in form of a Binary Heap
private List<Node> heapList;
private readonly List<Node> heapList;
// Hashset for quick checks of locations included in the heap
private HashSet<Location> locationList;
public Node MinH_ScoreNode;
private readonly HashSet<Location> locationList;
public Node? MinH_ScoreNode;
public BinaryHeap()
{
@ -337,7 +337,7 @@ namespace MinecraftClient.Mapping
int i = heapList.Count;
// Temporarily save the node created with the parameters to allow comparisons
Node newNode = new Node(newG_Score, newH_Score, loc);
Node newNode = new(newG_Score, newH_Score, loc);
// Add new note to the end of the list
heapList.Add(newNode);
@ -384,7 +384,7 @@ namespace MinecraftClient.Mapping
locationList.Remove(rootNode.Location);
// Temporarirly store the last item's value.
Node lastNode = heapList[heapList.Count - 1];
Node lastNode = heapList[^1];
// Remove the last value.
heapList.RemoveAt(heapList.Count - 1);
@ -524,7 +524,7 @@ namespace MinecraftClient.Mapping
public static bool IsSafe(World world, Location location)
{
return
//No block that can harm the player
//No block that can harm the player
!world.GetBlock(location).Type.CanHarmPlayers()
&& !world.GetBlock(Move(location, Direction.Up)).Type.CanHarmPlayers()
&& !world.GetBlock(Move(location, Direction.Down)).Type.CanHarmPlayers()
@ -640,7 +640,7 @@ namespace MinecraftClient.Mapping
return Move(Direction.North) + Move(Direction.West);
default:
throw new ArgumentException("Unknown direction", "direction");
throw new ArgumentException("Unknown direction", nameof(direction));
}
}

View file

@ -2,8 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MinecraftClient.Mapping
{
@ -23,7 +21,7 @@ namespace MinecraftClient.Mapping
/// </summary>
private static Dimension curDimension = new();
private static Dictionary<string, Dimension> dimensionList = new();
private static readonly Dictionary<string, Dimension> dimensionList = new();
/// <summary>
/// Chunk data parsing progress
@ -62,10 +60,11 @@ namespace MinecraftClient.Mapping
public static void StoreDimensionList(Dictionary<string, object> registryCodec)
{
var dimensionListNbt = (object[])(((Dictionary<string, object>)registryCodec["minecraft:dimension_type"])["value"]);
foreach (Dictionary<string, object> dimensionNbt in dimensionListNbt)
foreach (var (dimensionName, dimensionType) in from Dictionary<string, object> dimensionNbt in dimensionListNbt
let dimensionName = (string)dimensionNbt["name"]
let dimensionType = (Dictionary<string, object>)dimensionNbt["element"]
select (dimensionName, dimensionType))
{
string dimensionName = (string)dimensionNbt["name"];
Dictionary<string, object> dimensionType = (Dictionary<string, object>)dimensionNbt["element"];
StoreOneDimension(dimensionName, dimensionType);
}
}
@ -78,7 +77,7 @@ namespace MinecraftClient.Mapping
public static void StoreOneDimension(string dimensionName, Dictionary<string, object> dimensionType)
{
if (dimensionList.ContainsKey(dimensionName))
dimensionList.Remove(dimensionName);
dimensionList.Remove(dimensionName);
dimensionList.Add(dimensionName, new Dimension(dimensionName, dimensionType));
}
@ -170,16 +169,16 @@ namespace MinecraftClient.Mapping
/// <returns>Block matching the specified block type</returns>
public List<Location> FindBlock(Location from, Material block, int radiusx, int radiusy, int radiusz)
{
Location minPoint = new Location(from.X - radiusx, from.Y - radiusy, from.Z - radiusz);
Location maxPoint = new Location(from.X + radiusx, from.Y + radiusy, from.Z + radiusz);
List<Location> list = new List<Location> { };
Location minPoint = new(from.X - radiusx, from.Y - radiusy, from.Z - radiusz);
Location maxPoint = new(from.X + radiusx, from.Y + radiusy, from.Z + radiusz);
List<Location> list = new() { };
for (double x = minPoint.X; x <= maxPoint.X; x++)
{
for (double y = minPoint.Y; y <= maxPoint.Y; y++)
{
for (double z = minPoint.Z; z <= maxPoint.Z; z++)
{
Location doneloc = new Location(x, y, z);
Location doneloc = new(x, y, z);
Block doneblock = GetBlock(doneloc);
Material blockType = doneblock.Type;
if (blockType == block)
@ -218,5 +217,19 @@ namespace MinecraftClient.Mapping
chunkCnt = 0;
chunkLoadNotCompleted = 0;
}
public static string GetChunkLoadingStatus(World world)
{
double chunkLoadedRatio;
if (world.chunkCnt == 0)
chunkLoadedRatio = 0;
else
chunkLoadedRatio = (world.chunkCnt - world.chunkLoadNotCompleted) / (double)world.chunkCnt;
string status = Translations.Get("cmd.move.chunk_loading_status",
chunkLoadedRatio, world.chunkCnt - world.chunkLoadNotCompleted, world.chunkCnt);
return status;
}
}
}