Code refactoring (related to #1024)

This commit is contained in:
ORelio 2020-05-25 21:39:24 +02:00
parent d120001d70
commit 23870711a0
11 changed files with 69 additions and 100 deletions

View file

@ -887,17 +887,16 @@ namespace MinecraftClient
}
/// <summary>
/// Interact with an entity
/// Give Creative Mode items into regular/survival Player Inventory
/// </summary>
/// <param name="slot"></param>
/// <param name="ItemType"></param>
/// <param name="count"></param>
/// <returns></returns>
protected bool CreativeInventoryAction(int slot, ItemType ItemType, int count)
/// <remarks>(obviously) requires to be in creative mode</remarks>
/// <param name="slot">Destination inventory slot</param>
/// <param name="itemType">Item type</param>
/// <param name="count">Item count</param>
/// <returns>TRUE if item given successfully</returns>
protected bool CreativeGive(int slot, ItemType itemType, int count)
{
Dictionary<string, object> NBT = null;
Item item = new Item((int)ItemType, count, NBT);
return Handler.DoCreativeInventoryAction(slot, item);
return Handler.DoCreativeGive(slot, itemType, count);
}
/// <summary>

View file

@ -9,7 +9,7 @@ namespace MinecraftClient.Commands
class Inventory : Command
{
public override string CMDName { get { return "inventory"; } }
public override string CMDDesc { get { return "inventory <<id>|player|container> <list|close|drop <slot> <1|all>|click <slot> <left|right|middle>|creativegive <slot> <itemtype> <count>>: Interact with inventories"; } }
public override string CMDDesc { get { return "inventory <<id>|player|container> <list|close|drop <slot> <1|all>|click <slot> <left|right|middle>> | inventory creativegive <slot> <itemtype> <count>: Interact with inventories"; } }
public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars)
{
@ -34,6 +34,26 @@ namespace MinecraftClient.Commands
inventoryId = availableIds[0]; // one container, use it
else return "Cannot find container, please retry with explicit ID";
}
else if (args[0].ToLower() == "creativegive")
{
if (args.Length >= 4)
{
int slot = int.Parse(args[1]);
ItemType itemType = ItemType.Stone;
if (Enum.TryParse(args[2], out itemType))
{
int count = int.Parse(args[3]);
if (handler.DoCreativeGive(slot, itemType, count))
return "Requested " + itemType + " x" + count + " in slot #" + slot;
else return "Failed to request Creative Give";
}
else
{
return CMDDesc;
}
}
else return CMDDesc;
}
else inventoryId = int.Parse(args[0]);
string action = args.Length > 1
? args[1].ToLower()
@ -110,32 +130,6 @@ namespace MinecraftClient.Commands
}
}
else return CMDDesc;
case "creativegive":
if (args.Length >= 3)
{
int slot = int.Parse(args[2]);
ItemType ItemType = ItemType.Stone;
if (Enum.TryParse(args[3], out ItemType))
{
int count = int.Parse(args[4]);
Dictionary<string, object> NBT = null;
Item item = new Item((int)ItemType, count, NBT);
if (handler.DoCreativeInventoryAction(slot, item))
{
return "You have received " + ItemType + " x" + count + " in the slot #" + slot;
}
else
{
return "Failed";
}
}
else;
{
return CMDDesc;
}
}
else return CMDDesc;
default:
return CMDDesc;
}

View file

@ -46,12 +46,13 @@ namespace MinecraftClient.Mapping
this.Location = location;
}
/// <summary>
/// Create a new entity based on Entity ID, Entity Type, location and UUID
/// Create a new entity based on Entity ID, Entity Type, location, name and UUID
/// </summary>
/// <param name="ID">Entity ID</param>
/// <param name="type">Entity Type Enum</param>
/// <param name="location">Entity location</param>
/// <param name="uuid">Player uuid</param>
/// <param name="name">Player name</param>
public Entity(int ID, EntityType type, Location location, Guid uuid, string name)
{
this.ID = ID;

View file

@ -14,9 +14,9 @@ namespace MinecraftClient.Mapping.EntityPalettes
protected abstract Dictionary<int, EntityType> GetDict();
/// <summary>
/// Get mapping dictionary for pre-1.13. May be overriden with proper implementation.
/// Get mapping dictionary for pre-1.14 non-living entities.
/// </summary>
/// <returns>Palette dictionary for non-living entities (pre-1.13)</returns>
/// <returns>Palette dictionary for non-living entities (pre-1.14)</returns>
protected virtual Dictionary<int, EntityType> GetDictNonLiving()
{
return null;
@ -34,13 +34,13 @@ namespace MinecraftClient.Mapping.EntityPalettes
if (entityTypesNonLiving != null && !living)
{
//Pre-1.13 non-living entities have a different set of IDs (entityTypesNonLiving != null)
//Pre-1.14 non-living entities have a different set of IDs (entityTypesNonLiving != null)
if (entityTypesNonLiving.ContainsKey(id))
return entityTypesNonLiving[id];
}
else
{
//Post-1.13 entities have the same set of IDs regardless of living status
//1.14+ entities have the same set of IDs regardless of living status
if (entityTypes.ContainsKey(id))
return entityTypes[id];
}

View file

@ -6,7 +6,7 @@ namespace MinecraftClient.Mapping.EntityPalettes
/// <summary>
/// Defines mappings for pre-1.14 entitiy IDs
/// Pre-1.14 Minecraft has 2 set of ids: One for non-living objects and one for living mobs
/// Post-1.14 Minecraft has only one set of ids for all types of entities
/// 1.14+ Minecraft has only one set of ids for all types of entities
/// </summary>
public class EntityPalette113 : EntityPalette
{

View file

@ -9,8 +9,8 @@ namespace MinecraftClient.Mapping
/// 1. Generate registries.json using data reporting on Vanilla Minecraft (https://wiki.vg/Data_Generators)
/// 2. Generate temporary EntityTypeXXX.cs and EntityPaletteXXX.cs using EntityPaletteGenerator.cs
/// 3. Perform a diff with existing versions, add missing entries in EntityType.cs and EntityTypeExtensions.cs
/// 4. If existing entity IDs were not randomized by Mojang, simply add missing state entries to the latest existing PaletteXXX.cs
/// 5. If existing entity IDs were randomized, add a new palette as PaletteXXX.cs into the codebase (worst case)
/// 4. If existing entity IDs were not randomized by Mojang, simply add missing entries to the latest existing EntityPaletteXXX.cs
/// 5. If existing entity IDs were randomized, add a new palette as EntityPaletteXXX.cs into the codebase (worst case)
/// </remarks>
public enum EntityType
{

View file

@ -1250,48 +1250,20 @@ namespace MinecraftClient
}
/// <summary>
/// Called when a player was spawned/in the render distance
/// Called when a player spawns or enters the client's render distance
/// </summary>
/// <param name="EntityID"></param>
/// <param name="UUID"></param>
/// <param name="location"></param>
/// <param name="Yaw"></param>
/// <param name="Pitch"></param>
public void OnSpawnPlayer(int EntityID, Guid UUID, Location location, byte Yaw, byte Pitch)
public void OnSpawnPlayer(int entityID, Guid uuid, Location location, byte Yaw, byte Pitch)
{
if (entities.ContainsKey(EntityID)) return;
string name = "";
Dictionary<string, string> uuids = GetOnlinePlayersWithUUID();
foreach (KeyValuePair<string, string> keyValue in uuids)
{
if (keyValue.Key == UUID.ToString())
{
name = keyValue.Value;
}
}
Entity entity = new Entity(EntityID, EntityType.Player, location, UUID, name);
entities.Add(EntityID, entity);
foreach (ChatBot bot in bots.ToArray())
{
try
{
bot.OnEntitySpawn(entity);
}
catch (Exception e)
{
if (!(e is ThreadAbortException))
{
ConsoleIO.WriteLogLine("OnEntitySpawn: Got error from " + bot.ToString() + ": " + e.ToString());
}
else throw; //ThreadAbortException should not be caught
}
}
string playerName = null;
if (onlinePlayers.ContainsKey(uuid))
playerName = onlinePlayers[uuid];
Entity playerEntity = new Entity(entityID, EntityType.Player, location, uuid, playerName);
OnSpawnEntity(playerEntity);
}
/// <summary>
/// Called when entities dead/despawn.
/// </summary>
/// <param name="Entities"></param>
public void OnDestroyEntities(int[] Entities)
{
foreach (int a in Entities)
@ -1499,14 +1471,16 @@ namespace MinecraftClient
}
/// <summary>
/// Close the specified inventory window
/// Give Creative Mode items into regular/survival Player Inventory
/// </summary>
/// <param name="slot">Inventory slot</param>
/// <param name="item">Item</param>
/// <returns>TRUE if the window was successfully closed</returns>
public bool DoCreativeInventoryAction(int slot, Item item)
/// <remarks>(obviously) requires to be in creative mode</remarks>
/// <param name="slot">Destination inventory slot</param>
/// <param name="itemType">Item type</param>
/// <param name="count">Item count</param>
/// <returns>TRUE if item given successfully</returns>
public bool DoCreativeGive(int slot, ItemType itemType, int count)
{
return handler.SendCreativeInventoryAction(slot, item);
return handler.SendCreativeInventoryAction(slot, itemType, count);
}
/// <summary>

View file

@ -693,7 +693,7 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Currently not implemented
}
public bool SendCreativeInventoryAction(int slot, Item item)
public bool SendCreativeInventoryAction(int slot, ItemType item, int count)
{
return false; //Currently not implemented
}

View file

@ -215,8 +215,6 @@ namespace MinecraftClient.Protocol.Handlers
break;
case PacketIncomingType.JoinGame:
handler.OnGameJoined();
// by reinforce
// get client player EntityID
int playerEntityID = dataTypes.ReadNextInt(packetData);
handler.SetPlayerEntityID(playerEntityID);
dataTypes.ReadNextByte(packetData);
@ -1377,13 +1375,13 @@ namespace MinecraftClient.Protocol.Handlers
catch (ObjectDisposedException) { return false; }
}
public bool SendCreativeInventoryAction(int slot, Item item)
public bool SendCreativeInventoryAction(int slot, ItemType itemType, int count)
{
try
{
List<byte> packet = new List<byte>();
packet.AddRange(dataTypes.GetShort((short)slot));
packet.AddRange(dataTypes.GetItemSlot(item));
packet.AddRange(dataTypes.GetItemSlot(new Item((int)itemType, count, null)));
SendPacket(PacketOutgoingType.CreativeInventoryAction, packet);
return true;

View file

@ -150,11 +150,14 @@ namespace MinecraftClient.Protocol
bool SendWindowAction(int windowId, int slotId, WindowActionType action, Item item);
/// <summary>
/// Send a click window slot packet to the server
/// Request Creative Mode item creation into regular/survival Player Inventory
/// </summary>
/// <param name="slot">Id of inventory slot</param>
/// <param name="item">Id of item </param>
bool SendCreativeInventoryAction(int slot, Item item);
/// <remarks>(obviously) requires to be in creative mode</remarks>
/// <param name="slot">Destination inventory slot</param>
/// <param name="itemType">Item type</param>
/// <param name="count">Item count</param>
/// <returns>TRUE if item given successfully</returns>
bool SendCreativeInventoryAction(int slot, ItemType itemType, int count);
/// <summary>
/// Send a close window packet to the server

View file

@ -137,13 +137,13 @@ namespace MinecraftClient.Protocol
void OnSpawnEntity(Entity entity);
/// <summary>
/// Called when a player has spawned
/// Called when a player spawns or enters the client's render distance
/// </summary>
/// <param name="EntityID">Entity ID</param>
/// <param name="UUID">Entity UUID</param>
/// <param name="entityID">Entity ID</param>
/// <param name="uuid">Entity UUID</param>
/// <param name="location">Entity location</param>
/// <param name="Yaw">Player head yaw</param>
/// <param name="Pitch">Player head pitch</param>
/// <param name="yaw">Player head yaw</param>
/// <param name="pitch">Player head pitch</param>
void OnSpawnPlayer(int entityID, Guid uuid, Location location, byte yaw, byte pitch);
/// <summary>
@ -160,7 +160,7 @@ namespace MinecraftClient.Protocol
/// <param name="Dy">Y offset</param>
/// <param name="Dz">Z offset</param>
/// <param name="onGround">TRUE if on ground</param>
void OnEntityPosition(int entityID, Double dx, Double dy, Double dz,bool onGround);
void OnEntityPosition(int entityID, Double dx, Double dy, Double dz, bool onGround);
/// <summary>
/// Called when an entity moved to fixed coordinates