-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
177 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Audio; | ||
using HarmonyLib; | ||
using UnityEngine; | ||
|
||
namespace SCore.Harmony.TileEntities { | ||
public class CheckItemsForContainers { | ||
private static bool IsStackAllowedInContainer(XUiC_ItemStack itemStack) { | ||
switch (itemStack.StackLocation) | ||
{ | ||
case XUiC_ItemStack.StackLocationTypes.Backpack: | ||
case XUiC_ItemStack.StackLocationTypes.ToolBelt: | ||
return true; | ||
} | ||
|
||
var currentStack = itemStack.xui.dragAndDrop?.CurrentStack; | ||
if (currentStack == null || currentStack.IsEmpty()) return true; | ||
|
||
// Only run on loot containers and their slots. | ||
if (itemStack.xui.lootContainer == null) return true; | ||
if (itemStack.StackLocation != XUiC_ItemStack.StackLocationTypes.LootContainer) return true; | ||
|
||
var block = itemStack.xui.lootContainer.blockValue.Block; | ||
return CanPlaceItemInContainerViaTags(block, currentStack, true); | ||
} | ||
private static bool CanPlaceItemInContainerViaTags(Block block, ItemStack itemStack, bool showToolTip = false) { | ||
|
||
// If the tags don't exist, skip all the checks. | ||
if (!block.Properties.Contains("AllowTags") && !block.Properties.Contains("DisallowTags")) return true; | ||
|
||
var all = FastTags<TagGroup.Global>.Parse("all"); | ||
if (block.Properties.Contains("AllowTags")) | ||
{ | ||
var allowedTags = FastTags<TagGroup.Global>.Parse(block.Properties.GetString("AllowTags")); | ||
if (allowedTags.Test_AnySet(all)) return true; | ||
if (itemStack.itemValue.ItemClass.HasAnyTags(allowedTags)) return true; | ||
} | ||
|
||
if (block.Properties.Contains("DisallowTags")) | ||
{ | ||
var blockedTags = FastTags<TagGroup.Global>.Parse(block.Properties.GetString("DisallowTags")); | ||
if (!itemStack.itemValue.ItemClass.HasAnyTags(blockedTags)) return true; | ||
} | ||
|
||
|
||
if (!showToolTip) return false; | ||
var message = Localization.Get("ItemCannotBePlaced"); | ||
if (block.Properties.Contains("DisallowedKey")) | ||
{ | ||
message = Localization.Get(block.Properties.GetString("DisallowedKey")); | ||
} | ||
if (itemStack.itemValue.ItemClass.Properties.Contains("DisallowedKey")) | ||
{ | ||
message = Localization.Get(itemStack.itemValue.ItemClass.Properties.GetString("DisallowedKey")); | ||
} | ||
Manager.PlayInsidePlayerHead("ui_denied"); | ||
var primaryPlayer = GameManager.Instance.World.GetPrimaryPlayer(); | ||
XUiC_PopupToolTip.ClearTooltips(primaryPlayer.playerUI.xui); | ||
GameManager.ShowTooltip(primaryPlayer, message); | ||
return false; | ||
} | ||
|
||
// For clicking and sending objects to the toolbelt/backpack/loot container | ||
public class TEFeatureStoragePatch { | ||
[HarmonyPatch(typeof(TEFeatureStorage))] | ||
[HarmonyPatch("TryStackItem")] | ||
public class TEFeatureStorageTryStackItem { | ||
public static bool Prefix(TEFeatureStorage __instance, ItemStack _itemStack) { | ||
if (__instance is ITileEntityLootable tileEntityLootable) | ||
return CanPlaceItemInContainerViaTags(tileEntityLootable.blockValue.Block, _itemStack); | ||
return true; | ||
} | ||
} | ||
|
||
[HarmonyPatch(typeof(TEFeatureStorage))] | ||
[HarmonyPatch("AddItem")] | ||
public class TEFeatureStorageAddItem { | ||
public static bool Prefix(TEFeatureStorage __instance, ItemStack _itemStack) { | ||
if (__instance is ITileEntityLootable tileEntityLootable) | ||
return CanPlaceItemInContainerViaTags(tileEntityLootable.blockValue.Block, _itemStack); | ||
return true; | ||
|
||
} | ||
} | ||
} | ||
|
||
// For other methods, such as automatic stashing, dragging and dropping, etc. | ||
[HarmonyPatch] | ||
public class XUiC_ItemStack_SlotTags { | ||
[HarmonyTargetMethod] | ||
static IEnumerable<MethodBase> TargetMethods() { | ||
yield return typeof(XUiC_ItemStack).GetMethod("CanSwap"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("ForceSetItemStack"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("HandleDropOne"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("HandleMoveToPreferredLocation"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("HandlePartialStackPickup"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("HandleStackSwap"); | ||
yield return typeof(XUiC_ItemStack).GetMethod("SwapItem"); | ||
} | ||
|
||
public static bool Prefix(XUiC_ItemStack __instance) { | ||
return IsStackAllowedInContainer(__instance); | ||
} | ||
} | ||
|
||
|
||
public class XUICLootContainerCheckItemsForContainers { | ||
[HarmonyPatch(typeof(XUiC_LootWindow))] | ||
[HarmonyPatch("SetTileEntityChest")] | ||
public class XUiCLootWindowSetTileEntityChest { | ||
public static void Postfix(XUiC_LootWindow __instance, string _lootContainerName) { | ||
if (__instance.te == null) return; | ||
|
||
var block = __instance.te.blockValue.Block; | ||
if (block.Properties.Contains("AllowTags")) | ||
{ | ||
var display = block.Properties.GetString("AllowTags"); | ||
__instance.lootContainerName = $"{_lootContainerName} ( Tag Limited: {display} )"; | ||
__instance.RefreshBindings(true); | ||
return; | ||
} | ||
if (block.Properties.Contains("DisallowTags")) | ||
{ | ||
var display = block.Properties.GetString("DisallowTags"); | ||
__instance.lootContainerName = $"{_lootContainerName} ( Blocked Tags: {display} )"; | ||
__instance.RefreshBindings(true); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters