AMR+ Reworked ItemDamage event handling a little,

(EntityItem / throwables) - not supported yet
This commit is contained in:
melchior 2021-07-09 16:49:04 -04:00
parent e08d051014
commit c93427d089
3 changed files with 44 additions and 43 deletions

View file

@ -48,17 +48,17 @@ namespace AnvilMetalRecovery.Patches
#endif
if (DamageFilterTool.Ignore(world, __instance)) return;
#if DEBUG
world.Api.Logger.VerboseDebug("InventoryID: {0}, Class: {1}", itemslot.Inventory.InventoryID, itemslot.Inventory.ClassName);//Class: hotbar
world.Api.Logger.VerboseDebug("InventoryID: {0}, Class: {1}", itemslot?.Inventory?.InventoryID, itemslot?.Inventory?.ClassName);//Class: hotbar
world.Api.Logger.VerboseDebug("Thing has HP: {0}", itemslot.Itemstack.Hitpoints( ));
#endif
if (itemslot.Itemstack.Hitpoints( ) <= amount)
if (itemslot.Itemstack.Hitpoints( ) <= amount && itemslot.Inventory != null)
{
#if DEBUG
world.Api.Logger.VerboseDebug("Sending Item Expiry Event");
#endif
var playerEntity = byEntity as EntityPlayer;
var hotbarEvent = new HotbarObserverData(itemslot.Inventory.InventoryID, itemslot.Inventory.GetSlotId(itemslot), __instance.Code, (playerEntity == null ? String.Empty : playerEntity.PlayerUID));
world.Api.Event.PushEvent(MetalRecoverySystem.HotbarChannelName, hotbarEvent);
world.Api.Event.PushEvent(MetalRecoverySystem.ItemDamageChannelName, hotbarEvent);
}
}

View file

@ -18,7 +18,7 @@ namespace AnvilMetalRecovery
internal const string metalShavingsCode = @"metal_shaving";
internal const string itemFilterListCacheKey = @"AMR_ItemFilters";
public const float IngotVoxelEquivalent = 2.38f;
public const string HotbarChannelName = @"HotbarEvents";
public const string ItemDamageChannelName = @"ItemDamageEvents";
private Dictionary<AssetLocation, RecoveryEntry> itemToVoxelLookup = new Dictionary<AssetLocation, RecoveryEntry>();//Ammount & Material?
@ -41,7 +41,7 @@ namespace AnvilMetalRecovery
/// <summary>
/// ALL Items that have were derivable from smithing recipies (and are tool / durable)
/// </summary>
/// </summary>/
/// <value>The item filter list.</value>
public Dictionary<AssetLocation, RecoveryEntry> ItemRecoveryTable {
get
@ -90,13 +90,11 @@ namespace AnvilMetalRecovery
else {
Mod.Logger.Error("Cannot access 'ServerCoreAPI' class: API (implimentation) has changed, Contact Developer!");
return;
}
//ServerCore.ClassRegistryNative.ReplaceBlockEntityType(anvilKey, typeof(MetalRecovery_BlockEntityAnvil));
}
ServerCore.Event.ServerRunPhase(EnumServerRunPhase.GameReady, MaterialDataGathering);
SetupHotbarObserver( );
SetupGeneralObservers( );
Mod.Logger.VerboseDebug("Anvil Metal Recovery - should be installed...");
@ -119,7 +117,7 @@ namespace AnvilMetalRecovery
return;
}
//ClientCore.ClassRegistryNative.ReplaceBlockEntityType(anvilKey, typeof(MetalRecovery_BlockEntityAnvil));
Mod.Logger.VerboseDebug("Anvil Metal Recovery - should be installed...");
}
@ -133,10 +131,8 @@ namespace AnvilMetalRecovery
private void SetupHotbarObserver( ){
//ServerCore.RegisterEntityBehaviorClass(@"HotbarObserver", typeof(HotbarObserverBehavior));
ServerCore.Event.RegisterEventBusListener(HotbarEventReciever, 1.0f, HotbarChannelName);
//ServerCore.Event.PlayerNowPlaying += HotbarObserverBehavior.DirectConnect;
private void SetupGeneralObservers( ){
ServerCore.Event.RegisterEventBusListener(Item_DamageEventReciever, 1.0f, ItemDamageChannelName);
}

View file

@ -5,6 +5,7 @@ using System.Linq.Expressions;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.Datastructures;
using Vintagestory.API.Server;
using Vintagestory.Client.NoObf;
@ -57,7 +58,7 @@ namespace AnvilMetalRecovery
}
else {
itemToVoxelLookup.Add(outputItem.Code.Clone( ), new RecoveryEntry(metalObject.Code,
( uint )(setVoxels / recipie.Output.Quantity),
( uint )(setVoxels / recipie.Output.Quantity),
metalObject.CombustibleProps.MeltingDuration,
metalObject.CombustibleProps.MeltingPoint)
);
@ -115,56 +116,60 @@ namespace AnvilMetalRecovery
return false;
}
private void HotbarEventReciever(string eventName, ref EnumHandling handling, IAttribute data)
private void Item_DamageEventReciever(string eventName, ref EnumHandling handling, IAttribute data)
{
handling = EnumHandling.PassThrough;
HotbarObserverData hotbarData = data as HotbarObserverData;
#if DEBUG
Mod.Logger.VerboseDebug("HotbarEvent Rx: Item:{0} InventoryID '{1}' Slot#{2} PlayerUID:{3}", hotbarData.ItemCode.ToString( ),hotbarData.InventoryID ,hotbarData.Inventory_SlotID, hotbarData.PlayerUID);
Mod.Logger.VerboseDebug("Item_Damage Rx: Item:{0} InventoryID '{1}' Slot#{2} PlayerUID:{3}", hotbarData.ItemCode.ToString( ), hotbarData.InventoryID, hotbarData.Inventory_SlotID, hotbarData.PlayerUID);
#endif
if (ItemFilterList.Contains(hotbarData.ItemCode)) {
if (ItemFilterList.Contains(hotbarData.ItemCode)) {
RecoveryEntry rec = itemToVoxelLookup[hotbarData.ItemCode];
#if DEBUG
Mod.Logger.VerboseDebug("broken-tool/weap. {0} WORTH: {1:F1}*{2} units", hotbarData.ItemCode.ToString( ), (rec.Quantity * IngotVoxelEquivalent), rec.IngotCode.ToShortString( ));
Mod.Logger.VerboseDebug("broken-item {0} WORTH: {1:F1}*{2} units", hotbarData.ItemCode.ToString( ), (rec.Quantity * IngotVoxelEquivalent), rec.IngotCode.ToShortString( ));
#endif
if (String.IsNullOrEmpty(hotbarData.PlayerUID) || String.IsNullOrEmpty(hotbarData.InventoryID)) return;
bool probablyHotbar = hotbarData.InventoryID.StartsWith(GlobalConstants.hotBarInvClassName, StringComparison.Ordinal);
var playerTarget = ServerAPI.World.PlayerByUid(hotbarData.PlayerUID);
var spim = playerTarget.InventoryManager as ServerPlayerInventoryManager;
var hotbarInv = playerTarget.InventoryManager.GetHotbarInventory( );
var hotSlot = hotbarInv[hotbarData.Inventory_SlotID];
var spim = playerTarget.InventoryManager as ServerPlayerInventoryManager;
bool probablyHotbar = hotbarData.InventoryID.StartsWith(@"hotbar", StringComparison.Ordinal);
if (probablyHotbar && hotSlot.Empty) {
#if DEBUG
Mod.Logger.VerboseDebug("Directly inserting fragments into hotbar slot# {0}", hotbarData.Inventory_SlotID);
#endif
if (probablyHotbar && hotSlot.Empty)
{
#if DEBUG
Mod.Logger.VerboseDebug("Directly inserting fragments into hotbar slot# {0}", hotbarData.Inventory_SlotID);
#endif
VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem;
ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1);
variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack);
hotSlot.Itemstack = metalFragmentsStack;
hotSlot.Itemstack.ResolveBlockOrItem(ServerAPI.World);
hotSlot.MarkDirty( );
spim.NotifySlot(playerTarget, hotSlot);
}
else {
#if DEBUG
Mod.Logger.VerboseDebug("Hotbar (or crafting?) slot#{0} occupied; shoving {1} in general direction of player...", hotbarData.Inventory_SlotID,hotbarData.ItemCode.ToShortString());
#endif
VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem;
ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1);
variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack);
hotSlot.Itemstack = metalFragmentsStack;
hotSlot.Itemstack.ResolveBlockOrItem(ServerAPI.World);
hotSlot.MarkDirty( );
spim.NotifySlot(playerTarget, hotSlot);
}
else
{
#if DEBUG
Mod.Logger.VerboseDebug("Hotbar-occupied (or crafting) slot#{0} so; shoving {1} in general direction of player...", hotbarData.Inventory_SlotID, hotbarData.ItemCode.ToShortString( ));
#endif
VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem;
ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1);
variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack);
spim.TryGiveItemstack(metalFragmentsStack, true);
}
VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem;
ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1);
variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack);
spim.TryGiveItemstack(metalFragmentsStack, true);
}
}
}
private void EditDurability(IServerPlayer player, int groupId, CmdArgs args)
{
if (!player.Entity.RightHandItemSlot.Empty &&