Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying the code structure and adding some commands #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Commands/AddMaterials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CommunityCommands.Utils;
using Unity.Entities;
using VampireCommandFramework;

namespace CommunityCommands.Commands
{
public class AddMaterials
{
[Command("ct5", description: "Add some materials to a player's inventory to directly builde a level 5 castle.", adminOnly: false)]
public static void AddMaterialsCommand(ChatCommandContext ctx, string playerName = "")
{
if (string.IsNullOrEmpty(playerName))
{
playerName = ctx.Event.User.CharacterName.ToString();
}

var playerEntity = CharacterUtil.GetPlayerEntityByName(playerName);

if (playerEntity != Entity.Null)
{
foreach (var keyValuePair in DatabaseUtils.GetMaterialDictionary)
{
Helper.AddItemToInventory(playerEntity, keyValuePair.Value.Item1, keyValuePair.Value.Item2);
}

ctx.Reply($"Materials added to {playerName}'s inventory.");
}
else
{
ctx.Reply($"Player '{playerName}' not found.");
}
}
}
}
33 changes: 33 additions & 0 deletions Commands/Blood.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ProjectM;
using Unity.Entities;
using UnityEngine;
using VampireCommandFramework;
using CommunityCommands.Utils;

namespace CommunityCommands.Commands;

internal static class Blood
{
[Command("bloodpotion", "bp", description: "Creates a Potion with specified Blood Type, Quality and Value",
adminOnly: false)]
public static void GiveBloodPotionCommand(ChatCommandContext ctx, string bloodName = "Frailed",
float quality = 100f)
{
quality = Mathf.Clamp(quality, 0, 100);
var em = Helper.Server.EntityManager;

Entity entity =
Helper.AddItemToInventory(ctx.Event.SenderCharacterEntity, BloodUtils.GetBloodPotionRecipe(), 1);

var blood = new StoredBlood()
{
BloodQuality = quality,
BloodType = BloodUtils.GetBloodPrefabGuid(bloodName)
};

em.SetComponentData(entity, blood);

ctx.Reply(
$"Got Blood Potion Type <color=#ff0>{BloodUtils.GetBloodName(bloodName)}</color> with <color=#ff0>{(int)quality}</color>% quality");
}
}
3 changes: 2 additions & 1 deletion Commands/BloodCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public enum BloodType
[Command("bloodpotion", "bp", description: "Creates a Potion with specified Blood Type, Quality and Value", adminOnly: true)]
public static void GiveBloodPotionCommand(ChatCommandContext ctx, BloodType type = BloodType.Frailed, float quality = 100f)
{
var bloodPotionRecipeId = 1223264867;
quality = Mathf.Clamp(quality, 0, 100);
var em = Helper.Server.EntityManager;

Entity entity = Helper.AddItemToInventory(ctx.Event.SenderCharacterEntity, new PrefabGUID(828432508), 1);
Entity entity = Helper.AddItemToInventory(ctx.Event.SenderCharacterEntity, new PrefabGUID(bloodPotionRecipeId), 1);

var blood = new StoredBlood()
{
Expand Down
13 changes: 13 additions & 0 deletions Commands/ChangeSpeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommunityCommands.Utils;
using VampireCommandFramework;

namespace CommunityCommands.Commands;

public class ChangeSpeed
{
[Command("changespeed", "cs", description: "Change the speed of your character.", adminOnly: false)]
public static void ChangeSpeedCommand(ChatCommandContext ctx, float speed = 4.4f)
{
CharacterUtil.ChangeSpeed(ctx, speed);
}
}
13 changes: 13 additions & 0 deletions Commands/ClearInventory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommunityCommands.Utils;
using VampireCommandFramework;

namespace CommunityCommands.Commands;

public class ClearInventory
{
[Command("clearinventory", "ci", description: "Clears your inventory", adminOnly: false)]
public static void ClearInventoryCommand(ChatCommandContext ctx)
{
CharacterUtil.ClearInventory(ctx);
}
}
108 changes: 108 additions & 0 deletions Commands/DoorsWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections.Generic;
using CommunityCommands.Utils;
using ProjectM;
using ProjectM.CastleBuilding;
using ProjectM.Scripting;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
using VampireCommandFramework;

namespace CommunityCommands.Commands
{
public class DoorsWindow
{
[Command("close-all", "ca", description: "Close all doors.", adminOnly: false)]
public static void CloseAll(ChatCommandContext ctx, DoorWindow? target = null) => ChangeDoorWindows(ctx, false, target);

[Command("open-all", "oa", description: "Open all doors.", adminOnly: false)]
public static void OpenAll(ChatCommandContext ctx, DoorWindow? target = null) => ChangeDoorWindows(ctx, true, target);

public enum DoorWindow { Doors, Windows }
private static readonly HashSet<int> WINDOW_PREFABS = new HashSet<int> { -1771014048 };
private const float MaxCastleDistance = 60f;

public struct DoorState
{
public Entity Entity;
public bool OpenState;
}

public static void ChangeDoorWindows(ChatCommandContext ctx, bool open, DoorWindow? target)
{
var character = ctx.Event.SenderCharacterEntity;
var castleHearts = Helper.Server.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<CastleHeart>(), ComponentType.ReadOnly<LocalToWorld>())
.ToEntityArray(Allocator.Temp);
var gameManager = Helper.Server.GetExistingSystem<ServerScriptMapper>()?._ServerGameManager;
var playerPos = Helper.Server.EntityManager.GetComponentData<LocalToWorld>(character).Position;

var closestCastle = Entity.Null;
float closest = float.MaxValue;

foreach (var castle in castleHearts)
{
var isPlayerOwned = gameManager.Value.IsAllies(character, castle);
if (!isPlayerOwned) continue;

var castlePos = Helper.Server.EntityManager.GetComponentData<LocalToWorld>(castle).Position;
var distance = Vector3.Distance(castlePos, playerPos);
if (distance < closest && distance < MaxCastleDistance)
{
closest = distance;
closestCastle = castle;
}
}

if (closestCastle == Entity.Null)
{
ctx.Reply("Could not find nearby castle you own");
return;
}

var doors = Helper.Server.EntityManager.CreateEntityQuery(new EntityQueryDesc()
{
All = new[] { ComponentType.ReadWrite<Door>(), ComponentType.ReadOnly<CastleHeartConnection>(), ComponentType.ReadOnly<PrefabGUID>(), ComponentType.ReadOnly<Team>() },
Options = EntityQueryOptions.IncludeDisabled
}).ToEntityArray(Allocator.Temp);

var doorStates = new List<DoorState>();

int count = 0;
for (int i = 0; i < doors.Length; i++)
{
var connection = Helper.Server.EntityManager.GetComponentData<CastleHeartConnection>(doors[i]);
if (connection.CastleHeartEntity._Entity != closestCastle) continue;

if (target != null)
{
var prefab = Helper.Server.EntityManager.GetComponentData<PrefabGUID>(doors[i]);

if (target == DoorWindow.Doors && WINDOW_PREFABS.Contains(prefab.GuidHash)) continue;
if (target == DoorWindow.Windows && !WINDOW_PREFABS.Contains(prefab.GuidHash)) continue;
}

var doorState = new DoorState
{
Entity = doors[i],
OpenState = open,

};
doorStates.Add(doorState);
count++;
}

var doorComponents = Helper.Server.EntityManager.GetComponentDataFromEntity<Door>(true);

foreach (var doorState in doorStates)
{
var door = doorComponents[doorState.Entity];
door.OpenState = doorState.OpenState;
doorComponents[doorState.Entity] = door;
}
doorStates.Clear();

ctx.Reply($"Changed {count} {(target?.ToString() ?? "Windows and Doors")}");
}
}
}
25 changes: 25 additions & 0 deletions Commands/GetPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ProjectM;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using VampireCommandFramework;
using System;
using Il2CppSystem.Collections.Generic;
using Unity.Transforms;

namespace CommunityCommands.Commands
{
internal static class GetPosition
{
[Command("getposition", "gp", description: "Get the current position of the player", adminOnly: false)]
public static void GetPositionCommand(ChatCommandContext ctx)
{
var em = Helper.Server.EntityManager;
var playerEntity = ctx.Event.SenderCharacterEntity;
var translation = em.GetComponentData<Translation>(playerEntity);
var currentPosition = translation.Value;

ctx.Reply($"Current position: {currentPosition}");
}
}
}
25 changes: 25 additions & 0 deletions Commands/GiveItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CommunityCommands.Commons;
using CommunityCommands.Utils;
using VampireCommandFramework;

namespace CommunityCommands.Commands;

public class GiveItem
{
[Command("give", "g", description: "Give a specific item or set of items to yourself", adminOnly: false)]
public static void GiveItemCommand(ChatCommandContext ctx, GiveType type, string name)
{
switch (type)
{
case GiveType.Item:
DatabaseUtils.GiveSingleItem(ctx, name);
return;
case GiveType.Set:
DatabaseUtils.GiveEquipmentSet(ctx, name);
break;
default:
throw ctx.Error(
"<color=#ff0000>Invalid give type.</color> Please specify either '<color=#ff0000>item</color>' or '<color=#ff0000>set</color>'.");
}
}
}
85 changes: 85 additions & 0 deletions Commands/Horse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using ProjectM;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using VampireCommandFramework;
using System;
using CommunityCommands.Utils;
using Il2CppSystem.Collections.Generic;
using Unity.DebugDisplay;
using Unity.Transforms;

namespace CommunityCommands.Commands
{
internal static class Horse
{
[Command("horse", "h", description: "Manage horses (spawn or set max attributes)", adminOnly: false)]
public static void HorseCommand(ChatCommandContext ctx, string subCommand, int quantity = 1, float speed = 11f,
float acceleration = 7.0f, float rotation = 14.0f)
{
var em = Helper.Server.EntityManager;
var playerEntity = ctx.Event.SenderCharacterEntity;
if (!em.Exists(playerEntity))
{
ctx.Reply("Player entity not found.");
return;
}

subCommand = subCommand.ToLower();

if (subCommand == "spawn")
{
SpawnHorse(ctx, playerEntity, quantity, speed, acceleration, rotation);
}
else if (subCommand == "max")
{
SetMaxAttributes(ctx, playerEntity, speed, acceleration, rotation);
}
else
{
ctx.Reply("Invalid subcommand. Available subcommands: spawn, max.");
}
}

private static void SpawnHorse(ChatCommandContext ctx, Entity playerEntity, int quantity, float speed,
float acceleration, float rotation)
{
try
{
var em = Helper.Server.EntityManager;
var playerPosition = em.GetComponentData<Translation>(playerEntity).Value;

for (int i = 0; i < quantity; i++)
{
HorseUtil.SpawnHorse(1, playerPosition);
}

ctx.Reply($"Spawned {quantity} horse(s) next to the player.");
}
catch (Exception e)
{
throw ctx.Error($"An error occurred: {e.Message}");
}
}

private static void SetMaxAttributes(ChatCommandContext ctx, Entity playerEntity, float speed,
float acceleration, float rotation)
{

var closestHorse = HorseUtil.GetClosetHorse(playerEntity);

if (closestHorse != null)
{
HorseUtil.ModifyHorseAttributes(ctx, closestHorse.Value, speed, acceleration, rotation);

ctx.Reply(
$"Changed attributes for the closest horse: Speed: {speed}, Acceleration: {acceleration}, Rotation: {rotation}");
}
else
{
ctx.Reply("No horse found nearby.");
}

}
}
}
27 changes: 27 additions & 0 deletions Commands/Jewels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CommunityCommands.Commons;
using CommunityCommands.Utils;
using VampireCommandFramework;

namespace CommunityCommands.Commands
{
public class Jewels
{
[Command("jewel", "jw", description: "Add a jewel in your inventory.", adminOnly: false)]
public static void FindJewelPrefabCommand(ChatCommandContext ctx, GiveType type, string name,
string jewelTier = "t02")
{
if (type == GiveType.Item)
{
DatabaseUtils.GiveSingleJewel(ctx, name, jewelTier);
}
else if (type == GiveType.Set)
{
DatabaseUtils.GiveJewelSet(ctx, name);
}
else
{
ctx.Reply($"No jewel or jewel set matching the name '{name}' and tier '{jewelTier}' was found.");
}
}
}
}
Loading