From 018f21ea88d2244478c5f92e85838ad602e540c3 Mon Sep 17 00:00:00 2001 From: Dresmyr Date: Sun, 18 Jun 2023 01:17:21 -0700 Subject: [PATCH] Moved several commands from RPGMods I remember you saying you didn't like having all that in the helper class, so the find and kick utilities for the kick command are in here. the moved commands are: Kick, Shutdown, and Ping in the new Server Commands file, and List All Items in the Search Commands --- Commands/SearchCommands.cs | 19 ++++++++- Commands/ServerCommands.cs | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 Commands/ServerCommands.cs diff --git a/Commands/SearchCommands.cs b/Commands/SearchCommands.cs index aebbfc6..56387ce 100644 --- a/Commands/SearchCommands.cs +++ b/Commands/SearchCommands.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -56,4 +56,21 @@ public static void GiveItem(ChatCommandContext ctx, string search, int page = 1) Core.LogException(e); } } + + + [Command("List All Items", "LAI", adminOnly: true)] + public static void listAllItems(ChatCommandContext ctx) { + var gameDataSystem = Core.Server.GetExistingSystem(); + var managed = gameDataSystem.ManagedDataRegistry; + + foreach (var entry in gameDataSystem.ItemHashLookupMap) { + try { + var item = managed.GetOrDefault(entry.Key); + ctx.Reply(item.PrefabName); + Core.Log.LogInfo("Prefab Name: " + item.PrefabName); + + } catch { } + } + + } } diff --git a/Commands/ServerCommands.cs b/Commands/ServerCommands.cs new file mode 100644 index 0000000..97c3eca --- /dev/null +++ b/Commands/ServerCommands.cs @@ -0,0 +1,82 @@ +using ProjectM.Network; +using Unity.Collections; +using Unity.Entities; + +using VampireCommandFramework; + +namespace CommunityCommands.Commands; +public class ServerCommands { + public static class Shutdown { + [Command("shutdown", shortHand: "quit", description: "Trigger the exit signal & shutdown the server.", adminOnly: true)] + public static void Initialize() { + UnityEngine.Application.Quit(); + } + } + + public static class Kick { + [Command("kick", "kick ", "Kick the specified player out of the server.", adminOnly: true)] + public static void Initialize(ICommandContext ctx, string name) { + + + if (FindPlayer(name, true, out _, out var targetUserEntity)) { + KickPlayer(targetUserEntity); + ctx.Reply($"Player \"{name}\" has been kicked from server."); + } else { + ctx.Reply("Specified player not found."); + } + } + public static bool FindPlayer(string name, bool mustOnline, out Entity playerEntity, out Entity userEntity) { + foreach (var UsersEntity in Core.EntityManager.CreateEntityQuery(ComponentType.ReadOnly()).ToEntityArray(Allocator.Temp)) { + var target_component = Core.EntityManager.GetComponentData(UsersEntity); + if (mustOnline) { + if (!target_component.IsConnected) continue; + } + + string CharName = target_component.CharacterName.ToString(); + if (CharName.Equals(name)) { + userEntity = UsersEntity; + playerEntity = target_component.LocalCharacter._Entity; + return true; + } + } + playerEntity = new Entity(); + userEntity = new Entity(); + return false; + } + public static void KickPlayer(Entity userEntity) { + EntityManager em = Core.Server.EntityManager; + var userData = em.GetComponentData(userEntity); + int index = userData.Index; + + var entity = em.CreateEntity( + ComponentType.ReadOnly(), + ComponentType.ReadOnly(), + ComponentType.ReadOnly() + ); + + var KickEvent = new KickEvent() { + PlatformId = userData.PlatformId + }; + + em.SetComponentData(entity, new() { + UserIndex = index + }); + em.SetComponentData(entity, new() { + EventId = NetworkEvents.EventId_KickEvent, + IsAdminEvent = false, + IsDebugEvent = false + }); + + em.SetComponentData(entity, KickEvent); + } + } + + public static class Ping { + [Command("ping", "p", "ping", "Shows your latency.")] + public static void Initialize(ChatCommandContext ctx) { + var ping = Core.EntityManager.GetComponentData(ctx.Event.SenderCharacterEntity).Value; + ctx.Reply($"Your latency is {ping}s"); + } + } + +}