diff --git a/README.md b/README.md index 52066a9..b8b6901 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,9 @@ Allows you to perform various actions with objects at the beginning of the round ### Notes -To identify objects, their **index** is used, which can change when the number of slots on the server changes. In a future version of the plugin, it is planned to use a more secure method for identifying objects. +To identify objects, their **index** is used, which can change, when: + +* The number of slots on the server changes +* Other plugins are creating/removing entities + +In a future version of the plugin, it is planned to use a more secure method for identifying objects. diff --git a/scripting/entity-manager.sp b/scripting/entity-manager.sp index 2fd6a54..2d96563 100644 --- a/scripting/entity-manager.sp +++ b/scripting/entity-manager.sp @@ -9,7 +9,6 @@ #include "em/menu" #include "em/message" #include "em/storage" -#include "em/use-case" #include "modules/console-command.sp" #include "modules/console-variable.sp" @@ -24,7 +23,7 @@ public Plugin myinfo = { name = "Entity manager", author = "Dron-elektron", description = "Allows you to perform various actions with objects at the beginning of the round", - version = "0.2.0", + version = "1.0.0", url = "" }; diff --git a/scripting/include/em/message.inc b/scripting/include/em/message.inc index 79471ef..b6c34b5 100644 --- a/scripting/include/em/message.inc +++ b/scripting/include/em/message.inc @@ -5,8 +5,3 @@ #define PREFIX "[Entity manager] " #define CONSOLE 0 - -enum MessageType { - MessageType_Reply, - MessageType_Log -}; diff --git a/scripting/include/em/use-case.inc b/scripting/include/em/use-case.inc deleted file mode 100644 index 216c327..0000000 --- a/scripting/include/em/use-case.inc +++ /dev/null @@ -1,12 +0,0 @@ -#if defined _em_use_case_included - #endinput -#endif -#define _em_use_case_included - -enum UseCaseResult { - UseCaseResult_Success, - UseCaseResult_EntityNotFound, - UseCaseResult_AlreadyHasAction, - UseCaseResult_NotFrozen, - UseCaseResult_NotDeleted -}; diff --git a/scripting/modules/console-command.sp b/scripting/modules/console-command.sp index f170a19..be8299a 100644 --- a/scripting/modules/console-command.sp +++ b/scripting/modules/console-command.sp @@ -15,61 +15,25 @@ public Action Command_Menu(int client, int args) { } public Action Command_Freeze(int client, int args) { - int entity; - UseCaseResult result = UseCase_FreezeEntity(client, entity); - - if (result == UseCaseResult_Success) { - Message_EntityFrozen(client, entity); - } else if (result == UseCaseResult_EntityNotFound) { - Message_EntityNotFound(client); - } else if (result == UseCaseResult_AlreadyHasAction) { - Message_EntityAlreadyHasAction(client, entity); - } + UseCase_FreezeEntity(client); return Plugin_Handled; } public Action Command_Unfreeze(int client, int args) { - int entity; - UseCaseResult result = UseCase_UnfreezeEntity(client, entity); - - if (result == UseCaseResult_Success) { - Message_EntityUnfrozen(client, entity); - } else if (result == UseCaseResult_EntityNotFound) { - Message_EntityNotFound(client); - } else if (result == UseCaseResult_NotFrozen) { - Message_EntityNotFrozen(client, entity); - } + UseCase_UnfreezeEntity(client); return Plugin_Handled; } public Action Command_Delete(int client, int args) { - int entity; - UseCaseResult result = UseCase_DeleteEntity(client, entity); - - if (result == UseCaseResult_Success) { - Message_EntityDeleted(client, entity); - } else if (result == UseCaseResult_EntityNotFound) { - Message_EntityNotFound(client); - } else if (result == UseCaseResult_AlreadyHasAction) { - Message_EntityAlreadyHasAction(client, entity); - } + UseCase_DeleteEntity(client); return Plugin_Handled; } public Action Command_Restore(int client, int args) { - int entity; - UseCaseResult result = UseCase_RestoreEntity(client, entity); - - if (result == UseCaseResult_Success) { - Message_EntityRestored(client, entity); - } else if (result == UseCaseResult_EntityNotFound) { - Message_EntityNotFound(client); - } else if (result == UseCaseResult_NotDeleted) { - Message_EntityNotDeleted(client, entity); - } + UseCase_RestoreEntity(client); return Plugin_Handled; } @@ -77,27 +41,11 @@ public Action Command_Restore(int client, int args) { public Action Command_Save(int client, int args) { UseCase_SaveEntities(client); - int entitiesAmount = EntityList_Size(); - - if (entitiesAmount == 0) { - Message_ListOfEntitiesCleared(client); - } else { - Message_EntitiesSaved(client, entitiesAmount); - } - return Plugin_Handled; } public Action Command_Load(int client, int args) { UseCase_LoadEntities(client); - int entitiesAmount = EntityList_Size(); - - if (entitiesAmount == 0) { - Message_NoEntitiesForLoading(client); - } else { - Message_EntitiesLoaded(client, entitiesAmount); - } - return Plugin_Handled; } diff --git a/scripting/modules/menu.sp b/scripting/modules/menu.sp index e15fa90..117bf74 100644 --- a/scripting/modules/menu.sp +++ b/scripting/modules/menu.sp @@ -16,18 +16,17 @@ void Menu_EntityManager(int client) { public int MenuHandler_EntityManager(Menu menu, MenuAction action, int param1, int param2) { if (action == MenuAction_Select) { char info[MENU_TEXT_MAX_SIZE]; - int entity; menu.GetItem(param2, info, sizeof(info)); if (strcmp(info, ITEM_ENTITY_FREEZE) == 0) { - UseCase_FreezeEntity(param1, entity); + UseCase_FreezeEntity(param1); } else if (strcmp(info, ITEM_ENTITY_UNFREEZE) == 0) { - UseCase_UnfreezeEntity(param1, entity); + UseCase_UnfreezeEntity(param1); } else if (strcmp(info, ITEM_ENTITY_DELETE) == 0) { - UseCase_DeleteEntity(param1, entity); + UseCase_DeleteEntity(param1); } else if (strcmp(info, ITEM_ENTITY_RESTORE) == 0) { - UseCase_RestoreEntity(param1, entity); + UseCase_RestoreEntity(param1); } else if (strcmp(info, ITEM_ENTITIES_SAVE) == 0) { UseCase_SaveEntities(param1); } else if (strcmp(info, ITEM_ENTITIES_LOAD) == 0) { diff --git a/scripting/modules/message.sp b/scripting/modules/message.sp index 29d8df7..5a7370c 100644 --- a/scripting/modules/message.sp +++ b/scripting/modules/message.sp @@ -1,83 +1,89 @@ -void Message_EntityNotFound(int client) { +void MessageReply_EntityNotFound(int client) { ReplyToCommand(client, "%s%t", PREFIX, "Entity not found"); } -void Message_EntityAlreadyHasAction(int client, int entity) { +void MessageReply_EntityAlreadyHasAction(int client, int entity) { ReplyToCommand(client, "%s%t", PREFIX, "Entity already has an action", entity); } -void Message_EntityNotFrozen(int client, int entity) { +void MessageReply_EntityNotFrozen(int client, int entity) { ReplyToCommand(client, "%s%t", PREFIX, "Entity not frozen", entity); } -void Message_EntityNotDeleted(int client, int entity) { +void MessageReply_EntityNotDeleted(int client, int entity) { ReplyToCommand(client, "%s%t", PREFIX, "Entity not deleted", entity); } -void Message_EntityFrozen(int client, int entity, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entity has been frozen", entity); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" froze entity %d", client, entity); - } +void MessageActivity_EntityFrozen(int client, int entity) { + ShowActivity2(client, PREFIX, "%t", "Entity has been frozen", entity); } -void Message_EntityUnfrozen(int client, int entity, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entity has been unfrozen", entity); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" unfroze entity %d", client, entity); - } +void MessageLog_EntityFrozen(int client, int entity) { + LogMessage("\"%L\" froze entity %d", client, entity); } -void Message_EntityDeleted(int client, int entity, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entity deleted", entity); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" deleted entity %d", client, entity); - } +void MessageActivity_EntityUnfrozen(int client, int entity) { + ShowActivity2(client, PREFIX, "%t", "Entity has been unfrozen", entity); } -void Message_EntityRestored(int client, int entity, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entity restored", entity); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" restored entity %d", client, entity); - } +void MessageLog_EntityUnfrozen(int client, int entity) { + LogMessage("\"%L\" unfroze entity %d", client, entity); } -void Message_ListOfEntitiesCleared(int client, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "List of entities cleared"); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" cleared the list of entities", client); - } +void MessageActivity_EntityDeleted(int client, int entity) { + ShowActivity2(client, PREFIX, "%t", "Entity has been deleted", entity); } -void Message_EntitiesSaved(int client, int entitiesAmount, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entities saved", entitiesAmount); - } else if (type == MessageType_Log) { - LogMessage("\"%L\" saved %d entities", client, entitiesAmount); - } +void MessageLog_EntityDeleted(int client, int entity) { + LogMessage("\"%L\" deleted entity %d", client, entity); +} + +void MessageActivity_EntityRestored(int client, int entity) { + ShowActivity2(client, PREFIX, "%t", "Entity has been restored", entity); +} + +void MessageLog_EntityRestored(int client, int entity) { + LogMessage("\"%L\" restored entity %d", client, entity); +} + +void MessageActivity_ListOfEntitiesCleared(int client) { + ShowActivity2(client, PREFIX, "%t", "List of entities cleared"); } -void Message_NoEntitiesForLoading(int client, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { +void MessageLog_ListOfEntitiesCleared(int client) { + LogMessage("\"%L\" cleared the list of entities", client); +} + +void MessageActivity_EntitiesSaved(int client, int entitiesAmount) { + ShowActivity2(client, PREFIX, "%t", "Entities saved", entitiesAmount); +} + +void MessageLog_EntitiesSaved(int client, int entitiesAmount) { + LogMessage("\"%L\" saved %d entities", client, entitiesAmount); +} + +void MessageReply_NoEntitiesForLoading(int client) { + if (client != CONSOLE) { ReplyToCommand(client, "%s%t", PREFIX, "No entities for loading"); - } else if (type == MessageType_Log && client == CONSOLE) { + } +} + +void MessageLog_NoEntitiesForLoading(int client) { + if (client == CONSOLE) { LogMessage("No entities for this map"); } } -void Message_EntitiesLoaded(int client, int entitiesAmount, MessageType type = MessageType_Reply) { - if (type == MessageType_Reply) { - ReplyToCommand(client, "%s%t", PREFIX, "Entities loaded", entitiesAmount); - } else if (type == MessageType_Log) { - if (client == CONSOLE) { - LogMessage("Loaded %d entities", entitiesAmount); - } else { - LogMessage("\"%L\" loaded %d entities", client, entitiesAmount); - } +void MessageActivity_EntitiesLoaded(int client, int entitiesAmount) { + if (client != CONSOLE) { + ShowActivity2(client, PREFIX, "%t", "Entities loaded", entitiesAmount); + } +} + +void MessageLog_EntitiesLoaded(int client, int entitiesAmount) { + if (client == CONSOLE) { + LogMessage("Loaded %d entities", entitiesAmount); + } else { + LogMessage("\"%L\" loaded %d entities", client, entitiesAmount); } } diff --git a/scripting/modules/use-case.sp b/scripting/modules/use-case.sp index 5cbdcbd..4714943 100644 --- a/scripting/modules/use-case.sp +++ b/scripting/modules/use-case.sp @@ -1,73 +1,85 @@ -UseCaseResult UseCase_FreezeEntity(int client, int& entity) { - entity = Entity_Trace(client); +void UseCase_FreezeEntity(int client) { + int entity = Entity_Trace(client); if (entity <= ENTITY_WORLD) { - return UseCaseResult_EntityNotFound; + MessageReply_EntityNotFound(client); + + return; } if (EntityList_Contains(entity)) { - return UseCaseResult_AlreadyHasAction; + MessageReply_EntityAlreadyHasAction(client, entity); + + return; } Entity_Freeze(entity); EntityList_Add(entity, ENTITY_ACTION_FREEZE); - Message_EntityFrozen(client, entity, MessageType_Log); - - return UseCaseResult_Success; + MessageActivity_EntityFrozen(client, entity); + MessageLog_EntityFrozen(client, entity); } -UseCaseResult UseCase_UnfreezeEntity(int client, int& entity) { - entity = Entity_Trace(client); +void UseCase_UnfreezeEntity(int client) { + int entity = Entity_Trace(client); if (entity <= ENTITY_WORLD) { - return UseCaseResult_EntityNotFound; + MessageReply_EntityNotFound(client); + + return; } if (!EntityList_IsFrozen(entity)) { - return UseCaseResult_NotFrozen; + MessageReply_EntityNotFrozen(client, entity); + + return; } Entity_Unfreeze(entity); EntityList_Remove(entity); - Message_EntityUnfrozen(client, entity, MessageType_Log); - - return UseCaseResult_Success; + MessageActivity_EntityUnfrozen(client, entity); + MessageLog_EntityUnfrozen(client, entity); } -UseCaseResult UseCase_DeleteEntity(int client, int& entity) { - entity = Entity_Trace(client); +void UseCase_DeleteEntity(int client) { + int entity = Entity_Trace(client); if (entity <= ENTITY_WORLD) { - return UseCaseResult_EntityNotFound; + MessageReply_EntityNotFound(client); + + return; } if (EntityList_Contains(entity)) { - return UseCaseResult_AlreadyHasAction; + MessageReply_EntityAlreadyHasAction(client, entity); + + return; } Entity_Delete(entity); EntityList_Add(entity, ENTITY_ACTION_DELETE); - Message_EntityDeleted(client, entity, MessageType_Log); - - return UseCaseResult_Success; + MessageActivity_EntityDeleted(client, entity); + MessageLog_EntityDeleted(client, entity); } -UseCaseResult UseCase_RestoreEntity(int client, int& entity) { - entity = Entity_Trace(client); +void UseCase_RestoreEntity(int client) { + int entity = Entity_Trace(client); if (entity <= ENTITY_WORLD) { - return UseCaseResult_EntityNotFound; + MessageReply_EntityNotFound(client); + + return; } if (!EntityList_IsDeleted(entity)) { - return UseCaseResult_NotDeleted; + MessageReply_EntityNotDeleted(client, entity); + + return; } Entity_Restore(entity); EntityList_Remove(entity); - Message_EntityRestored(client, entity, MessageType_Log); - - return UseCaseResult_Success; + MessageActivity_EntityRestored(client, entity); + MessageLog_EntityRestored(client, entity); } void UseCase_SaveEntities(int client) { @@ -76,9 +88,11 @@ void UseCase_SaveEntities(int client) { int entitiesAmount = EntityList_Size(); if (entitiesAmount == 0) { - Message_ListOfEntitiesCleared(client, MessageType_Log); + MessageActivity_ListOfEntitiesCleared(client); + MessageLog_ListOfEntitiesCleared(client); } else { - Message_EntitiesSaved(client, entitiesAmount, MessageType_Log); + MessageActivity_EntitiesSaved(client, entitiesAmount); + MessageLog_EntitiesSaved(client, entitiesAmount); } } @@ -89,9 +103,11 @@ void UseCase_LoadEntities(int client) { int entitiesAmount = EntityList_Size(); if (entitiesAmount == 0) { - Message_NoEntitiesForLoading(client, MessageType_Log); + MessageReply_NoEntitiesForLoading(client); + MessageLog_NoEntitiesForLoading(client); } else { - Message_EntitiesLoaded(client, entitiesAmount, MessageType_Log); + MessageActivity_EntitiesLoaded(client, entitiesAmount); + MessageLog_EntitiesLoaded(client, entitiesAmount); } } diff --git a/translations/entity-manager.phrases.txt b/translations/entity-manager.phrases.txt index 03d008e..9ee5303 100644 --- a/translations/entity-manager.phrases.txt +++ b/translations/entity-manager.phrases.txt @@ -35,13 +35,13 @@ "en" "Entity {1} has been unfrozen" } - "Entity deleted" + "Entity has been deleted" { "#format" "{1:d}" "en" "Entity {1} deleted" } - "Entity restored" + "Entity has been restored" { "#format" "{1:d}" "en" "Entity {1} restored" diff --git a/translations/ru/entity-manager.phrases.txt b/translations/ru/entity-manager.phrases.txt index 1c6f250..49baac2 100644 --- a/translations/ru/entity-manager.phrases.txt +++ b/translations/ru/entity-manager.phrases.txt @@ -30,12 +30,12 @@ "ru" "Объект {1} разморожен" } - "Entity deleted" + "Entity has been deleted" { "ru" "Объект {1} удален" } - "Entity restored" + "Entity has been restored" { "ru" "Объект {1} восстановлен" }