First Machine Age's Mods (Combined repo.)
Révision | c93427d089215a1f6d3bf264aa9667edd2abd3a4 (tree) |
---|---|
l'heure | 2021-07-10 05:49:04 |
Auteur | melchior <melchior@user...> |
Commiter | melchior |
AMR+ Reworked ItemDamage event handling a little,
(EntityItem / throwables) - not supported yet
@@ -48,17 +48,17 @@ namespace AnvilMetalRecovery.Patches | ||
48 | 48 | #endif |
49 | 49 | if (DamageFilterTool.Ignore(world, __instance)) return; |
50 | 50 | #if DEBUG |
51 | - world.Api.Logger.VerboseDebug("InventoryID: {0}, Class: {1}", itemslot.Inventory.InventoryID, itemslot.Inventory.ClassName);//Class: hotbar | |
51 | + world.Api.Logger.VerboseDebug("InventoryID: {0}, Class: {1}", itemslot?.Inventory?.InventoryID, itemslot?.Inventory?.ClassName);//Class: hotbar | |
52 | 52 | world.Api.Logger.VerboseDebug("Thing has HP: {0}", itemslot.Itemstack.Hitpoints( )); |
53 | 53 | #endif |
54 | - if (itemslot.Itemstack.Hitpoints( ) <= amount) | |
54 | + if (itemslot.Itemstack.Hitpoints( ) <= amount && itemslot.Inventory != null) | |
55 | 55 | { |
56 | 56 | #if DEBUG |
57 | 57 | world.Api.Logger.VerboseDebug("Sending Item Expiry Event"); |
58 | 58 | #endif |
59 | 59 | var playerEntity = byEntity as EntityPlayer; |
60 | 60 | var hotbarEvent = new HotbarObserverData(itemslot.Inventory.InventoryID, itemslot.Inventory.GetSlotId(itemslot), __instance.Code, (playerEntity == null ? String.Empty : playerEntity.PlayerUID)); |
61 | - world.Api.Event.PushEvent(MetalRecoverySystem.HotbarChannelName, hotbarEvent); | |
61 | + world.Api.Event.PushEvent(MetalRecoverySystem.ItemDamageChannelName, hotbarEvent); | |
62 | 62 | } |
63 | 63 | } |
64 | 64 |
@@ -18,7 +18,7 @@ namespace AnvilMetalRecovery | ||
18 | 18 | internal const string metalShavingsCode = @"metal_shaving"; |
19 | 19 | internal const string itemFilterListCacheKey = @"AMR_ItemFilters"; |
20 | 20 | public const float IngotVoxelEquivalent = 2.38f; |
21 | - public const string HotbarChannelName = @"HotbarEvents"; | |
21 | + public const string ItemDamageChannelName = @"ItemDamageEvents"; | |
22 | 22 | |
23 | 23 | private Dictionary<AssetLocation, RecoveryEntry> itemToVoxelLookup = new Dictionary<AssetLocation, RecoveryEntry>();//Ammount & Material? |
24 | 24 |
@@ -41,7 +41,7 @@ namespace AnvilMetalRecovery | ||
41 | 41 | |
42 | 42 | /// <summary> |
43 | 43 | /// ALL Items that have were derivable from smithing recipies (and are tool / durable) |
44 | - /// </summary> | |
44 | + /// </summary>/ | |
45 | 45 | /// <value>The item filter list.</value> |
46 | 46 | public Dictionary<AssetLocation, RecoveryEntry> ItemRecoveryTable { |
47 | 47 | get |
@@ -90,13 +90,11 @@ namespace AnvilMetalRecovery | ||
90 | 90 | else { |
91 | 91 | Mod.Logger.Error("Cannot access 'ServerCoreAPI' class: API (implimentation) has changed, Contact Developer!"); |
92 | 92 | return; |
93 | - } | |
94 | - | |
95 | - //ServerCore.ClassRegistryNative.ReplaceBlockEntityType(anvilKey, typeof(MetalRecovery_BlockEntityAnvil)); | |
93 | + } | |
96 | 94 | |
97 | 95 | ServerCore.Event.ServerRunPhase(EnumServerRunPhase.GameReady, MaterialDataGathering); |
98 | 96 | |
99 | - SetupHotbarObserver( ); | |
97 | + SetupGeneralObservers( ); | |
100 | 98 | |
101 | 99 | Mod.Logger.VerboseDebug("Anvil Metal Recovery - should be installed..."); |
102 | 100 |
@@ -119,7 +117,7 @@ namespace AnvilMetalRecovery | ||
119 | 117 | return; |
120 | 118 | } |
121 | 119 | |
122 | - //ClientCore.ClassRegistryNative.ReplaceBlockEntityType(anvilKey, typeof(MetalRecovery_BlockEntityAnvil)); | |
120 | + | |
123 | 121 | |
124 | 122 | Mod.Logger.VerboseDebug("Anvil Metal Recovery - should be installed..."); |
125 | 123 | } |
@@ -133,10 +131,8 @@ namespace AnvilMetalRecovery | ||
133 | 131 | |
134 | 132 | |
135 | 133 | |
136 | - private void SetupHotbarObserver( ){ | |
137 | - //ServerCore.RegisterEntityBehaviorClass(@"HotbarObserver", typeof(HotbarObserverBehavior)); | |
138 | - ServerCore.Event.RegisterEventBusListener(HotbarEventReciever, 1.0f, HotbarChannelName); | |
139 | - //ServerCore.Event.PlayerNowPlaying += HotbarObserverBehavior.DirectConnect; | |
134 | + private void SetupGeneralObservers( ){ | |
135 | + ServerCore.Event.RegisterEventBusListener(Item_DamageEventReciever, 1.0f, ItemDamageChannelName); | |
140 | 136 | } |
141 | 137 | |
142 | 138 |
@@ -5,6 +5,7 @@ using System.Linq.Expressions; | ||
5 | 5 | |
6 | 6 | using Vintagestory.API.Client; |
7 | 7 | using Vintagestory.API.Common; |
8 | +using Vintagestory.API.Config; | |
8 | 9 | using Vintagestory.API.Datastructures; |
9 | 10 | using Vintagestory.API.Server; |
10 | 11 | using Vintagestory.Client.NoObf; |
@@ -57,7 +58,7 @@ namespace AnvilMetalRecovery | ||
57 | 58 | } |
58 | 59 | else { |
59 | 60 | itemToVoxelLookup.Add(outputItem.Code.Clone( ), new RecoveryEntry(metalObject.Code, |
60 | - ( uint )(setVoxels / recipie.Output.Quantity), | |
61 | + ( uint )(setVoxels / recipie.Output.Quantity), | |
61 | 62 | metalObject.CombustibleProps.MeltingDuration, |
62 | 63 | metalObject.CombustibleProps.MeltingPoint) |
63 | 64 | ); |
@@ -115,56 +116,60 @@ namespace AnvilMetalRecovery | ||
115 | 116 | return false; |
116 | 117 | } |
117 | 118 | |
118 | - private void HotbarEventReciever(string eventName, ref EnumHandling handling, IAttribute data) | |
119 | + private void Item_DamageEventReciever(string eventName, ref EnumHandling handling, IAttribute data) | |
119 | 120 | { |
120 | 121 | handling = EnumHandling.PassThrough; |
121 | 122 | HotbarObserverData hotbarData = data as HotbarObserverData; |
122 | 123 | |
123 | 124 | #if DEBUG |
124 | - Mod.Logger.VerboseDebug("HotbarEvent Rx: Item:{0} InventoryID '{1}' Slot#{2} PlayerUID:{3}", hotbarData.ItemCode.ToString( ),hotbarData.InventoryID ,hotbarData.Inventory_SlotID, hotbarData.PlayerUID); | |
125 | + Mod.Logger.VerboseDebug("Item_Damage Rx: Item:{0} InventoryID '{1}' Slot#{2} PlayerUID:{3}", hotbarData.ItemCode.ToString( ), hotbarData.InventoryID, hotbarData.Inventory_SlotID, hotbarData.PlayerUID); | |
125 | 126 | #endif |
126 | 127 | |
127 | - if (ItemFilterList.Contains(hotbarData.ItemCode)) { | |
128 | + if (ItemFilterList.Contains(hotbarData.ItemCode)) { | |
129 | + | |
128 | 130 | RecoveryEntry rec = itemToVoxelLookup[hotbarData.ItemCode]; |
129 | 131 | #if DEBUG |
130 | - Mod.Logger.VerboseDebug("broken-tool/weap. {0} WORTH: {1:F1}*{2} units", hotbarData.ItemCode.ToString( ), (rec.Quantity * IngotVoxelEquivalent), rec.IngotCode.ToShortString( )); | |
132 | + Mod.Logger.VerboseDebug("broken-item {0} WORTH: {1:F1}*{2} units", hotbarData.ItemCode.ToString( ), (rec.Quantity * IngotVoxelEquivalent), rec.IngotCode.ToShortString( )); | |
131 | 133 | #endif |
132 | 134 | |
135 | + if (String.IsNullOrEmpty(hotbarData.PlayerUID) || String.IsNullOrEmpty(hotbarData.InventoryID)) return; | |
136 | + | |
137 | + bool probablyHotbar = hotbarData.InventoryID.StartsWith(GlobalConstants.hotBarInvClassName, StringComparison.Ordinal); | |
133 | 138 | var playerTarget = ServerAPI.World.PlayerByUid(hotbarData.PlayerUID); |
139 | + var spim = playerTarget.InventoryManager as ServerPlayerInventoryManager; | |
134 | 140 | var hotbarInv = playerTarget.InventoryManager.GetHotbarInventory( ); |
135 | 141 | var hotSlot = hotbarInv[hotbarData.Inventory_SlotID]; |
136 | - var spim = playerTarget.InventoryManager as ServerPlayerInventoryManager; | |
137 | - bool probablyHotbar = hotbarData.InventoryID.StartsWith(@"hotbar", StringComparison.Ordinal); | |
138 | 142 | |
139 | - if (probablyHotbar && hotSlot.Empty) { | |
140 | - #if DEBUG | |
141 | - Mod.Logger.VerboseDebug("Directly inserting fragments into hotbar slot# {0}", hotbarData.Inventory_SlotID); | |
142 | - #endif | |
143 | + if (probablyHotbar && hotSlot.Empty) | |
144 | + { | |
145 | + #if DEBUG | |
146 | + Mod.Logger.VerboseDebug("Directly inserting fragments into hotbar slot# {0}", hotbarData.Inventory_SlotID); | |
147 | + #endif | |
143 | 148 | |
144 | - VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem; | |
145 | - ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1); | |
146 | - variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack); | |
147 | - hotSlot.Itemstack = metalFragmentsStack; | |
148 | - hotSlot.Itemstack.ResolveBlockOrItem(ServerAPI.World); | |
149 | - hotSlot.MarkDirty( ); | |
150 | - spim.NotifySlot(playerTarget, hotSlot); | |
151 | - } | |
152 | - else { | |
153 | - #if DEBUG | |
154 | - Mod.Logger.VerboseDebug("Hotbar (or crafting?) slot#{0} occupied; shoving {1} in general direction of player...", hotbarData.Inventory_SlotID,hotbarData.ItemCode.ToShortString()); | |
155 | - #endif | |
149 | + VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem; | |
150 | + ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1); | |
151 | + variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack); | |
152 | + hotSlot.Itemstack = metalFragmentsStack; | |
153 | + hotSlot.Itemstack.ResolveBlockOrItem(ServerAPI.World); | |
154 | + hotSlot.MarkDirty( ); | |
155 | + spim.NotifySlot(playerTarget, hotSlot); | |
156 | + } | |
157 | + else | |
158 | + { | |
159 | + #if DEBUG | |
160 | + Mod.Logger.VerboseDebug("Hotbar-occupied (or crafting) slot#{0} so; shoving {1} in general direction of player...", hotbarData.Inventory_SlotID, hotbarData.ItemCode.ToShortString( )); | |
161 | + #endif | |
156 | 162 | |
157 | - VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem; | |
158 | - ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1); | |
159 | - variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack); | |
160 | - spim.TryGiveItemstack(metalFragmentsStack, true); | |
161 | - } | |
163 | + VariableMetalItem variableMetal = ServerAPI.World.GetItem(new AssetLocation(metalFragmentsCode)) as VariableMetalItem; | |
164 | + ItemStack metalFragmentsStack = new ItemStack(variableMetal, 1); | |
165 | + variableMetal.ApplyMetalProperties(rec, ref metalFragmentsStack); | |
166 | + spim.TryGiveItemstack(metalFragmentsStack, true); | |
167 | + } | |
162 | 168 | } |
163 | 169 | |
164 | 170 | } |
165 | 171 | |
166 | 172 | |
167 | - | |
168 | 173 | private void EditDurability(IServerPlayer player, int groupId, CmdArgs args) |
169 | 174 | { |
170 | 175 | if (!player.Entity.RightHandItemSlot.Empty && |