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

Use standard wildcard system to parse unsuitable codes #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 8 additions & 16 deletions Block/BlockTrough.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public class BlockTroughBase : Block

public BlockPos RootOffset = new BlockPos();

protected string[] unsuitableEntityCodesBeginsWith = new string[0];
protected string[] unsuitableEntityCodesExact;
protected string unsuitableEntityFirstLetters = "";
protected AssetLocation[] unsuitableEntityCodes;

public void init()
{
Expand Down Expand Up @@ -84,8 +82,8 @@ public void init()
}
};

string[] codes = Attributes?["unsuitableFor"].AsArray<string>(new string[0]);
if (codes.Length > 0) AiTaskBaseTargetable.InitializeTargetCodes(codes, ref unsuitableEntityCodesExact, ref unsuitableEntityCodesBeginsWith, ref unsuitableEntityFirstLetters); ;
// Defaulting to * instead of entity.Code.Domain for compatibility with mods for 1.20.0-rc and earlier
unsuitableEntityCodes = Attributes?["unsuitableFor"].AsArray<string>(new string[0]).Select(str => AssetLocation.Create(str, "*")).ToArray();
}

public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor world, BlockSelection selection, IPlayer forPlayer)
Expand All @@ -94,18 +92,12 @@ public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor
}


public virtual bool UnsuitableForEntity(string testPath) // Similar code to AiTaskBaseTargetable.IsTargetEntity(testPath)
public virtual bool UnsuitableForEntity(AssetLocation code)
{
if (unsuitableEntityFirstLetters.IndexOf(testPath[0]) < 0) return false; // early exit if we don't have the first letter

for (int i = 0; i < unsuitableEntityCodesExact.Length; i++)
{
if (testPath == unsuitableEntityCodesExact[i]) return true;
}

for (int i = 0; i < unsuitableEntityCodesBeginsWith.Length; i++)
{
if (testPath.StartsWithFast(unsuitableEntityCodesBeginsWith[i])) return true;
for (int i = 0; i < unsuitableEntityCodes.Length; ++i) {
if (WildcardUtil.Match(unsuitableEntityCodes[i], code)) {
return true;
}
}

return false;
Expand Down