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 GetCollectibleInterface/GetInterface where possible #74

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Block/BlockAnvil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override void OnLoaded(ICoreAPI api)
Itemstacks = workableStacklist.ToArray(),
GetMatchingStacks = (wi, bs, es) => {
BlockEntityAnvil bea = api.World.BlockAccessor.GetBlockEntity(bs.Position) as BlockEntityAnvil;
return bea?.WorkItemStack == null ? null : new ItemStack[] { (bea.WorkItemStack.Collectible as IAnvilWorkable).GetBaseMaterial(bea.WorkItemStack) };
return bea?.WorkItemStack == null ? null : new ItemStack[] { bea.WorkItemStack.Collectible.GetCollectibleInterface<IAnvilWorkable>().GetBaseMaterial(bea.WorkItemStack) };
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions Block/BlockChute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ bool CanStay(IWorldAccessor world, BlockPos pos)
{
BlockFacing face = BlockFacing.FromCode(val);
Block block = world.BlockAccessor.GetBlock(npos.Set(pos).Add(face));
if (block.CanAttachBlockAt(world.BlockAccessor, this, pos, face) || (block as IBlockItemFlow)?.HasItemFlowConnectorAt(face.Opposite) == true || ba.GetBlock(pos).GetBlockEntity< BlockEntityContainer>(npos) != null) return true;
if (block.CanAttachBlockAt(world.BlockAccessor, this, pos, face) || block.GetInterface<IBlockItemFlow>(world, pos)?.HasItemFlowConnectorAt(face.Opposite) == true || ba.GetBlock(pos).GetBlockEntity< BlockEntityContainer>(npos) != null) return true;
}
}

Expand All @@ -225,7 +225,7 @@ bool CanStay(IWorldAccessor world, BlockPos pos)
{
BlockFacing face = BlockFacing.FromCode(val);
Block block = world.BlockAccessor.GetBlock(npos.Set(pos).Add(face));
if (block.CanAttachBlockAt(world.BlockAccessor, this, pos, face) || (block as IBlockItemFlow)?.HasItemFlowConnectorAt(face.Opposite) == true || ba.GetBlock(pos).GetBlockEntity<BlockEntityContainer>(npos) != null) return true;
if (block.CanAttachBlockAt(world.BlockAccessor, this, pos, face) || block.GetInterface<IBlockItemFlow>(world, pos)?.HasItemFlowConnectorAt(face.Opposite) == true || ba.GetBlock(pos).GetBlockEntity<BlockEntityContainer>(npos) != null) return true;
}
}

Expand Down
10 changes: 4 additions & 6 deletions Block/BlockCookedContainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void ServeIntoBowl(Block selectedBlock, BlockPos pos, ItemSlot potslot, I

world.BlockAccessor.SetBlock(mealblock.BlockId, pos);

IBlockEntityMealContainer bemeal = api.World.BlockAccessor.GetBlockEntity(pos) as IBlockEntityMealContainer;
IBlockEntityMealContainer bemeal = api.World.BlockAccessor.GetBlock(pos).GetInterface<IBlockEntityMealContainer>(world, pos);
if (bemeal == null) return;

if (tryMergeServingsIntoBE(bemeal, potslot)) return;
Expand Down Expand Up @@ -209,10 +209,8 @@ public bool ServeIntoStack(ItemSlot bowlSlot, ItemSlot potslot, IWorldAccessor w
float servingCapacity = bowlSlot.Itemstack.Block.Attributes["servingCapacity"].AsFloat(1);

// Merge existing servings
if (bowlSlot.Itemstack.Block is IBlockMealContainer)
if (bowlSlot.Itemstack.Collectible.GetCollectibleInterface<IBlockMealContainer>() is IBlockMealContainer mealcont)
{
var mealcont = (bowlSlot.Itemstack.Block as IBlockMealContainer);

ItemStack[] myStacks = GetNonEmptyContents(api.World, potslot.Itemstack);

string hisRecipeCode = mealcont.GetRecipeCode(world, bowlSlot.Itemstack);
Expand Down Expand Up @@ -271,7 +269,7 @@ public bool ServeIntoStack(ItemSlot bowlSlot, ItemSlot potslot, IWorldAccessor w
float servingsToTransfer = Math.Min(quantityServings, servingCapacity);

ItemStack stack = new ItemStack(mealblock);
(mealblock as IBlockMealContainer).SetContents(ownRecipeCode, stack, stacks, servingsToTransfer);
(mealblock.GetCollectibleInterface<IBlockMealContainer>()).SetContents(ownRecipeCode, stack, stacks, servingsToTransfer);

SetServingsMaybeEmpty(world, potslot, quantityServings - servingsToTransfer);

Expand Down Expand Up @@ -345,7 +343,7 @@ public bool OnContainedInteractStart(BlockEntityContainer be, ItemSlot slot, IPl
var targetSlot = byPlayer.InventoryManager.ActiveHotbarSlot;
if (targetSlot.Empty) return false;

if ((targetSlot.Itemstack.Collectible.Attributes?.IsTrue("mealContainer") == true || targetSlot.Itemstack.Block is IBlockMealContainer) && GetServings(api.World, slot.Itemstack) > 0)
if ((targetSlot.Itemstack.Collectible.Attributes?.IsTrue("mealContainer") == true || targetSlot.Itemstack.Collectible.GetCollectibleInterface<IBlockMealContainer>() is IBlockMealContainer) && GetServings(api.World, slot.Itemstack) > 0)
{
if (targetSlot.StackSize > 1)
{
Expand Down
6 changes: 3 additions & 3 deletions Block/BlockSmeltedContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, Bl
{
if (blockSel == null) return;

ILiquidMetalSink be = byEntity.World.BlockAccessor.GetBlockEntity(blockSel.Position) as ILiquidMetalSink;
ILiquidMetalSink be = blockSel.Block.GetInterface<ILiquidMetalSink>(byEntity.World, blockSel.Position);

if (be != null)
{
Expand Down Expand Up @@ -180,7 +180,7 @@ public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, Entity
{
if (blockSel == null) return false;

ILiquidMetalSink be = byEntity.World.BlockAccessor.GetBlockEntity(blockSel.Position) as ILiquidMetalSink;
ILiquidMetalSink be = blockSel.Block.GetInterface<ILiquidMetalSink>(byEntity.World, blockSel.Position);
if (be == null) return false;

if (!be.CanReceiveAny) return false;
Expand Down Expand Up @@ -381,7 +381,7 @@ public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, Entity

if (blockSel == null) return;

ILiquidMetalSink be = byEntity.World.BlockAccessor.GetBlockEntity(blockSel.Position) as ILiquidMetalSink;
ILiquidMetalSink be = blockSel.Block.GetInterface<ILiquidMetalSink>(byEntity.World, blockSel.Position);
be?.OnPourOver();
}

Expand Down
4 changes: 2 additions & 2 deletions Block/BlockTorch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override void OnGroundIdle(EntityItem entityItem)

public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling)
{
if (blockSel != null && byEntity.World.BlockAccessor.GetBlock(blockSel.Position) is IIgnitable ign)
if (blockSel != null && blockSel.Block.GetInterface<IIgnitable>(byEntity.World, blockSel.Position) is IIgnitable ign)
{
if (isExtinct)
{
Expand All @@ -129,7 +129,7 @@ public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, Bl

public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
{
if (isExtinct && blockSel != null && byEntity.World.BlockAccessor.GetBlock(blockSel.Position) is IIgnitable ign)
if (isExtinct && blockSel != null && blockSel.Block.GetInterface<IIgnitable>(byEntity.World, blockSel.Position) is IIgnitable ign)
{
var state = ign.OnTryIgniteStack(byEntity, blockSel.Position, slot, secondsUsed);
if (state == EnumIgniteState.Ignitable)
Expand Down
2 changes: 1 addition & 1 deletion BlockBehavior/BehaviorHeatSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public float GetHeatStrength(IWorldAccessor world, BlockPos heatSourcePos, Block
{
if (block.EntityClass != null)
{
var behs = world.BlockAccessor.GetBlockEntity(heatSourcePos) as IHeatSource;
var behs = world.BlockAccessor.GetBlock(heatSourcePos).GetInterface<IHeatSource>(world, heatSourcePos);
if (behs != null)
{
return behs.GetHeatStrength(world, heatSourcePos, heatReceiverPos);
Expand Down
4 changes: 2 additions & 2 deletions BlockBehavior/BehaviorReparable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer

if (byPlayer.WorldData.CurrentGameMode != EnumGameMode.Creative)
{
if (slot.Itemstack.Collectible is IBlockMealContainer mc)
if (slot.Itemstack.Collectible.GetCollectibleInterface<IBlockMealContainer>() is IBlockMealContainer mc)
{
float servingsLeft = mc.GetQuantityServings(world, slot.Itemstack) - 1f;
mc.SetQuantityServings(world, slot.Itemstack, servingsLeft);
Expand Down Expand Up @@ -207,7 +207,7 @@ private float GetItemRepairAmount(IWorldAccessor world, ItemSlot slot)
return stack.Collectible.Attributes["repairGain"].AsFloat(0.2f);
}

if (stack.Collectible is IBlockMealContainer mc)
if (stack.Collectible.GetCollectibleInterface<IBlockMealContainer>() is IBlockMealContainer mc)
{
ItemStack[] stacks = mc.GetNonEmptyContents(world, stack);
if (stacks.Length > 0 && stacks[0] != null && stacks[0].Collectible.Code.PathStartsWith("glueportion")) return stacks[0].Collectible.Attributes["repairGain"].AsFloat(0.2f);
Expand Down
12 changes: 6 additions & 6 deletions BlockEntity/BEAnvil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public SmithingRecipe SelectedRecipe

public bool CanWorkCurrent
{
get { return workItemStack != null && (workItemStack.Collectible as IAnvilWorkable).CanWork(WorkItemStack); }
get { return workItemStack != null && workItemStack.Collectible.GetCollectibleInterface<IAnvilWorkable>().CanWork(WorkItemStack); }
}

public ItemStack WorkItemStack
Expand Down Expand Up @@ -255,7 +255,7 @@ private bool TryPut(IWorldAccessor world, IPlayer byPlayer, BlockSelection block
if (slot.Itemstack == null) return false;
ItemStack stack = slot.Itemstack;

IAnvilWorkable workableobj = stack.Collectible as IAnvilWorkable;
IAnvilWorkable workableobj = stack.Collectible.GetCollectibleInterface<IAnvilWorkable>();



Expand Down Expand Up @@ -439,7 +439,7 @@ internal string PrintDebugText()
SmithingRecipe recipe = SelectedRecipe;


EnumHelveWorkableMode? mode = (workItemStack?.Collectible as IAnvilWorkable)?.GetHelveWorkableMode(workItemStack, this);
EnumHelveWorkableMode? mode = workItemStack?.Collectible.GetCollectibleInterface<IAnvilWorkable>()?.GetHelveWorkableMode(workItemStack, this);

StringBuilder sb = new StringBuilder();
sb.AppendLine("Workitem: " + workItemStack);
Expand All @@ -460,7 +460,7 @@ public virtual void OnHelveHammerHit()
return;
}

var mode = (workItemStack.Collectible as IAnvilWorkable).GetHelveWorkableMode(workItemStack, this);
var mode = workItemStack.Collectible.GetCollectibleInterface<IAnvilWorkable>().GetHelveWorkableMode(workItemStack, this);
if (mode == EnumHelveWorkableMode.NotWorkable) return;

rotation = 0;
Expand Down Expand Up @@ -611,7 +611,7 @@ public void ditchWorkItemStack(IPlayer byPlayer = null)
ItemStack ditchedStack;
if (SelectedRecipe == null)
{
ditchedStack = returnOnCancelStack ?? (workItemStack.Collectible as IAnvilWorkable).GetBaseMaterial(workItemStack);
ditchedStack = returnOnCancelStack ?? workItemStack.Collectible.GetCollectibleInterface<IAnvilWorkable>().GetBaseMaterial(workItemStack);
float temp = workItemStack.Collectible.GetTemperature(Api.World, workItemStack);
ditchedStack.Collectible.SetTemperature(Api.World, ditchedStack, temp);
}
Expand Down Expand Up @@ -1148,7 +1148,7 @@ public override void OnReceivedClientPacket(IPlayer player, int packetid, byte[]

internal void OpenDialog(ItemStack ingredient)
{
List<SmithingRecipe> recipes = (ingredient.Collectible as IAnvilWorkable).GetMatchingRecipes(ingredient);
List<SmithingRecipe> recipes = ingredient.Collectible.GetCollectibleInterface<IAnvilWorkable>().GetMatchingRecipes(ingredient);

List<ItemStack> stacks = recipes
.Select(r => r.Output.ResolvedItemstack)
Expand Down
2 changes: 1 addition & 1 deletion BlockEntity/BECondenser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void genBucketMesh()
if (inventory.Count < 2 || inventory[1].Empty || Api.Side == EnumAppSide.Server) return;

var stack = inventory[1].Itemstack;
var meshSource = stack.Collectible as IContainedMeshSource;
var meshSource = stack.Collectible.GetCollectibleInterface<IContainedMeshSource>();
if (meshSource != null)
{
bucketMeshTmp = meshSource.GenMesh(stack, (Api as ICoreClientAPI).BlockTextureAtlas, Pos);
Expand Down
4 changes: 2 additions & 2 deletions BlockEntity/BEContainerDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected virtual void updateMesh(int index)

protected virtual string getMeshCacheKey(ItemStack stack)
{
var meshSource = stack.Collectible as IContainedMeshSource;
var meshSource = stack.Collectible.GetCollectibleInterface<IContainedMeshSource>();
if (meshSource != null)
{
return meshSource.GetMeshCacheKey(stack);
Expand All @@ -212,7 +212,7 @@ protected virtual MeshData getOrCreateMesh(ItemStack stack, int index)
MeshData mesh = getMesh(stack);
if (mesh != null) return mesh;

var meshSource = stack.Collectible as IContainedMeshSource;
var meshSource = stack.Collectible.GetCollectibleInterface<IContainedMeshSource>();

if (meshSource != null)
{
Expand Down
2 changes: 1 addition & 1 deletion BlockEntity/BECrock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void ServeInto(IPlayer player, ItemSlot slot)
ItemStack mealstack = new ItemStack(block);
mealstack.StackSize = 1;

(block as IBlockMealContainer).SetContents(RecipeCode, mealstack, GetNonEmptyContentStacks(), servings);
(block.GetCollectibleInterface<IBlockMealContainer>()).SetContents(RecipeCode, mealstack, GetNonEmptyContentStacks(), servings);

if (slot.StackSize == 1)
{
Expand Down
2 changes: 1 addition & 1 deletion BlockEntity/BEFruitpress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ private void genBucketMesh()
if (BucketSlot.Empty || capi == null) return;

var stack = BucketSlot.Itemstack;
var meshSource = stack.Collectible as IContainedMeshSource;
var meshSource = stack.Collectible.GetCollectibleInterface<IContainedMeshSource>();
if (meshSource != null)
{
bucketMeshTmp = meshSource.GenMesh(stack, capi.BlockTextureAtlas, Pos);
Expand Down
12 changes: 6 additions & 6 deletions BlockEntity/BEGroundStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public virtual bool OnPlayerInteractStart(IPlayer player, BlockSelection bs)

public bool OnPlayerInteractStep(float secondsUsed, IPlayer byPlayer, BlockSelection blockSel)
{
if (isUsingSlot?.Itemstack?.Collectible is IContainedInteractable collIci)
if (isUsingSlot?.Itemstack?.Collectible.GetCollectibleInterface<IContainedInteractable>() is IContainedInteractable collIci)
{
return collIci.OnContainedInteractStep(secondsUsed, this, isUsingSlot, byPlayer, blockSel);
}
Expand All @@ -677,7 +677,7 @@ public bool OnPlayerInteractStep(float secondsUsed, IPlayer byPlayer, BlockSelec

public void OnPlayerInteractStop(float secondsUsed, IPlayer byPlayer, BlockSelection blockSel)
{
if (isUsingSlot?.Itemstack.Collectible is IContainedInteractable collIci)
if (isUsingSlot?.Itemstack.Collectible.GetCollectibleInterface<IContainedInteractable>() is IContainedInteractable collIci)
{
collIci.OnContainedInteractStop(secondsUsed, this, isUsingSlot, byPlayer, blockSel);
}
Expand Down Expand Up @@ -949,7 +949,7 @@ public bool TryTakeItem(IPlayer player)
public bool putOrGetItemSingle(ItemSlot ourSlot, IPlayer player, BlockSelection bs)
{
isUsingSlot = null;
if (!ourSlot.Empty && ourSlot.Itemstack.Collectible is IContainedInteractable collIci)
if (!ourSlot.Empty && ourSlot.Itemstack.Collectible.GetCollectibleInterface<IContainedInteractable>() is IContainedInteractable collIci)
{
if (collIci.OnContainedInteractStart(this, ourSlot, player, bs))
{
Expand Down Expand Up @@ -1106,7 +1106,7 @@ public virtual string GetBlockName()
ItemStack stack = firstSlot.Itemstack;
int sumQ = inventory.Sum(s => s.StackSize);

if (firstSlot.Itemstack.Collectible is IContainedCustomName ccn)
if (firstSlot.Itemstack.Collectible.GetCollectibleInterface<IContainedCustomName>() is IContainedCustomName ccn)
{
string name = ccn.GetContainedName(firstSlot, sumQ);
if (name != null) return name;
Expand All @@ -1128,7 +1128,7 @@ public override void GetBlockInfo(IPlayer forPlayer, StringBuilder dsc)

ItemStack stack = inventory.FirstNonEmptySlot.Itemstack;
// Only add supplemental info for non-BlockEntities (otherwise it will be wrong or will get into a recursive loop, because right now this BEGroundStorage is the BlockEntity)
if (contentSummary.Length == 1 && !(stack.Collectible is IContainedCustomName) && stack.Class == EnumItemClass.Block && ((Block)stack.Collectible).EntityClass == null)
if (contentSummary.Length == 1 && !(stack.Collectible.GetCollectibleInterface<IContainedCustomName>() is IContainedCustomName) && stack.Class == EnumItemClass.Block && ((Block)stack.Collectible).EntityClass == null)
{
string detailedInfo = stack.Block.GetPlacedBlockInfo(Api.World, Pos, forPlayer);
if (detailedInfo != null && detailedInfo.Length > 0) dsc.Append(detailedInfo);
Expand Down Expand Up @@ -1164,7 +1164,7 @@ public virtual string[] getContentSummary()

string stackName = slot.Itemstack.GetName();

if (slot.Itemstack.Collectible is IContainedCustomName ccn)
if (slot.Itemstack.Collectible.GetCollectibleInterface<IContainedCustomName>() is IContainedCustomName ccn)
{
stackName = ccn.GetContainedInfo(slot);
}
Expand Down
2 changes: 1 addition & 1 deletion BlockEntity/BEToolrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void loadToolMeshes()

tmpItem = stack.Collectible;

var meshSource = stack.Collectible as IContainedMeshSource;
var meshSource = stack.Collectible.GetCollectibleInterface<IContainedMeshSource>();
if (meshSource != null)
{
toolMeshes[i] = meshSource.GenMesh(stack, capi.BlockTextureAtlas, Pos);
Expand Down
Loading