Skip to content

Commit

Permalink
Fix pick-block not working on feedthroughs and creating item entities…
Browse files Browse the repository at this point in the history
… (when it is working) if the block would drop multiple items, closes #2984
  • Loading branch information
malte0811 committed May 5, 2018
1 parent 6336557 commit b247b8f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraftforge.client.model.obj.OBJLoader;
import net.minecraftforge.client.model.obj.OBJModel;

import javax.annotation.Nonnull;
import java.util.*;

public class IEOBJLoader implements ICustomModelLoader
Expand All @@ -34,32 +35,32 @@ public void addDomain(String domain)
}

@Override
public boolean accepts(ResourceLocation modelLocation)
public boolean accepts(@Nonnull ResourceLocation modelLocation)
{
return enabledDomains.contains(modelLocation.getResourceDomain()) && modelLocation.getResourcePath().endsWith(".obj.ie");
}

@Nonnull
@Override
public IModel loadModel(ResourceLocation modelLocation) throws Exception
public IModel loadModel(@Nonnull ResourceLocation modelLocation) throws Exception
{
ResourceLocation file = new ResourceLocation(modelLocation.getResourceDomain(), modelLocation.getResourcePath());
if(!cache.containsKey(file))
if(!cache.containsKey(modelLocation))
{
IModel model = OBJLoader.INSTANCE.loadModel(modelLocation);
if(model instanceof OBJModel)
{
IEOBJModel ieobj = new IEOBJModel(((OBJModel)model).getMatLib(), file);
IEOBJModel ieobj = new IEOBJModel(((OBJModel)model).getMatLib(), modelLocation);
cache.put(modelLocation, ieobj);
}
}
IEOBJModel model = cache.get(file);
IEOBJModel model = cache.get(modelLocation);
if(model == null)
return ModelLoaderRegistry.getMissingModel();
return model;
}

@Override
public void onResourceManagerReload(IResourceManager resourceManager)
public void onResourceManagerReload(@Nonnull IResourceManager resourceManager)
{
this.manager = resourceManager;
cache.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ else if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
}
if(tile instanceof ITileDrop)
{
ItemStack s = ((ITileDrop)tile).getTileDrop(harvesters.get(), state);
if(!s.isEmpty())
drops.add(s);
NonNullList<ItemStack> s = ((ITileDrop)tile).getTileDrops(harvesters.get(), state);
drops.addAll(s);
}
else
super.getDrops(drops, world, pos, state, fortune);
Expand Down Expand Up @@ -231,7 +230,7 @@ public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World wo
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof ITileDrop)
{
ItemStack s = ((ITileDrop)tile).getTileDrop(player, world.getBlockState(pos));
ItemStack s = ((ITileDrop)tile).getPickBlock(player, world.getBlockState(pos), target);
if(!s.isEmpty())
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
Expand Down Expand Up @@ -177,7 +178,24 @@ public interface IConfigurableSides

public interface ITileDrop
{
ItemStack getTileDrop(@Nullable EntityPlayer player, IBlockState state);
/**
* Don't call this on generic TE'S, use getTileDrops or getPickBlock
*/
default ItemStack getTileDrop(@Nullable EntityPlayer player, IBlockState state)
{
NonNullList<ItemStack> drops = getTileDrops(player, state);
return drops.size()>0?drops.get(0):ItemStack.EMPTY;
}

default NonNullList<ItemStack> getTileDrops(@Nullable EntityPlayer player, IBlockState state)
{
return NonNullList.from(ItemStack.EMPTY, getTileDrop(player, state));
}

default ItemStack getPickBlock(@Nullable EntityPlayer player, IBlockState state, RayTraceResult rayRes)
{
return getTileDrop(player, state);
}

void readOnPlacement(@Nullable EntityLivingBase placer, ItemStack stack);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World wo
}
}
}
Item item = Item.getItemFromBlock(this);
return item==Items.AIR?ItemStack.EMPTY: new ItemStack(item, 1, this.damageDropped(world.getBlockState(pos)));
return super.getPickBlock(state, target, world, pos, player);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;

Expand Down Expand Up @@ -175,24 +176,19 @@ public BlockPos getConnectionMaster(WireType cableType, TargetingInfo target)
}

@Override
public ItemStack getTileDrop(@Nullable EntityPlayer player, IBlockState state)
public NonNullList<ItemStack> getTileDrops(@Nullable EntityPlayer player, IBlockState state)
{
WireApi.FeedthroughModelInfo info = INFOS.get(reference);
if (info.canReplace())
{
if (offset==0)
return Utils.getDrops(stateForMiddle);
else
{
NonNullList<ItemStack> ret = Utils.getDrops(stateForMiddle);
if (ret.size()>0)
{
for (int i = 1;i<ret.size();i++)
Utils.dropStackAtPos(world, pos, ret.get(i));
return ret.get(0);
}
assert info.conn!=null;//If it's marked as replaceable it should have a state to replace with
return NonNullList.from(ItemStack.EMPTY,
new ItemStack(info.conn.getBlock(), 1, info.conn.getBlock().getMetaFromState(info.conn)));
}
else
return new ItemStack(info.conn.getBlock(), 1, info.conn.getBlock().getMetaFromState(info.conn));
return ItemStack.EMPTY;
}
else
{
Expand All @@ -201,8 +197,26 @@ public ItemStack getTileDrop(@Nullable EntityPlayer player, IBlockState state)
NBTTagCompound stateNbt = new NBTTagCompound();
Utils.stateToNBT(stateNbt, stateForMiddle);
stack.setTagInfo(MIDDLE_STATE, stateNbt);
return stack;
return NonNullList.from(ItemStack.EMPTY, stack);
}
}

@Override
public ItemStack getPickBlock(@Nullable EntityPlayer player, IBlockState state, RayTraceResult rayRes)
{
WireApi.FeedthroughModelInfo info = INFOS.get(reference);
if (info.canReplace() && offset==0)
{
//getPickBlock needs a proper World, not an IBlockAccess, which is hard to emulate quickly.
// "world, pos" won't have anything remotely like the state this expects, I hope it won't notice.
try
{
return stateForMiddle.getBlock().getPickBlock(stateForMiddle, rayRes, world, pos, player);
}
catch (Exception x)// We can't predict what is going to happen with weird inputs. The block is mostly inert, so it shouldn't be too bad.
{} // No output as WAILA etc call this every tick (every frame?)
}
return getTileDrop(player, state);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ public static IBlockState stateFromNBT(NBTTagCompound in)

public static NonNullList<ItemStack> getDrops(IBlockState state)
{
DropBlockAcess w = new DropBlockAcess(state);
IBlockAccess w = getSingleBlockWorldAccess(state);
NonNullList<ItemStack> ret = NonNullList.create();
state.getBlock().getDrops(ret, w, BlockPos.ORIGIN, state, 0);
return ret;
Expand All @@ -1575,10 +1575,15 @@ private static <T extends Comparable<T>> IBlockState setProp(IBlockState state,
return state;
}

private static class DropBlockAcess implements IBlockAccess
public static IBlockAccess getSingleBlockWorldAccess(IBlockState state)
{
return new SingleBlockAcess(state);
}

private static class SingleBlockAcess implements IBlockAccess
{
IBlockState state;
public DropBlockAcess(IBlockState state) {
public SingleBlockAcess(IBlockState state) {
this.state = state;
}

Expand Down

0 comments on commit b247b8f

Please sign in to comment.