Chest Minecart contraptions

This commit is contained in:
grimmauld 2020-09-12 14:59:31 +02:00
parent 2e938c11bd
commit f58245158d
5 changed files with 158 additions and 1 deletions

View file

@ -828,4 +828,5 @@ public abstract class Contraption {
return seats;
}
public void addExtraInventories(Entity entity) {};
}

View file

@ -77,6 +77,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD
protected boolean initialized;
final List<Entity> collidingEntities = new ArrayList<>();
private boolean isSerializingFurnaceCart;
private boolean attachedExtraInventories;
private static final Ingredient FUEL_ITEMS = Ingredient.fromItems(Items.COAL, Items.CHARCOAL);
private static final DataParameter<Boolean> STALLED =
@ -104,6 +105,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD
motionBeforeStall = Vec3d.ZERO;
stationary = entityTypeIn == AllEntityTypes.STATIONARY_CONTRAPTION.get();
isSerializingFurnaceCart = false;
attachedExtraInventories = false;
forcedAngle = -1;
}
@ -323,6 +325,10 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD
Entity riding = e;
while (riding.getRidingEntity() != null)
riding = riding.getRidingEntity();
if (!attachedExtraInventories) {
contraption.addExtraInventories(riding);
attachedExtraInventories = true;
}
boolean isOnCoupling = false;
if (contraption instanceof MountedContraption) {

View file

@ -25,6 +25,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.material.PushReaction;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.minecart.AbstractMinecartEntity;
import net.minecraft.entity.item.minecart.ChestMinecartEntity;
import net.minecraft.entity.item.minecart.FurnaceMinecartEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
@ -166,7 +167,7 @@ public class CartAssemblerBlock extends AbstractRailBlock
}
public static boolean canAssembleTo(AbstractMinecartEntity cart) {
return cart.canBeRidden() || cart instanceof FurnaceMinecartEntity;
return cart.canBeRidden() || cart instanceof FurnaceMinecartEntity || cart instanceof ChestMinecartEntity;
}
@Override

View file

@ -0,0 +1,140 @@
package com.simibubi.create.content.contraptions.components.structureMovement.mounted;
import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class ItemHandlerModifiableFromIInventory implements IItemHandlerModifiable {
private final IInventory inventory;
public ItemHandlerModifiableFromIInventory(IInventory inventory) {
this.inventory = inventory;
}
@Override
public void setStackInSlot(int slot, ItemStack stack) {
inventory.setInventorySlotContents(slot, stack);
}
@Override
public int getSlots() {
return inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory.getStackInSlot(slot);
}
@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
{
if (stack.isEmpty())
return ItemStack.EMPTY;
if (!isItemValid(slot, stack))
return stack;
validateSlotIndex(slot);
ItemStack existing = getStackInSlot(slot);
int limit = getStackLimit(slot, stack);
if (!existing.isEmpty())
{
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
return stack;
limit -= existing.getCount();
}
if (limit <= 0)
return stack;
boolean reachedLimit = stack.getCount() > limit;
if (!simulate)
{
if (existing.isEmpty())
{
setStackInSlot(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
}
else
{
existing.grow(reachedLimit ? limit : stack.getCount());
}
}
return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount()- limit) : ItemStack.EMPTY;
}
@Override
@Nonnull
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (amount == 0)
return ItemStack.EMPTY;
validateSlotIndex(slot);
ItemStack existing = getStackInSlot(slot);
if (existing.isEmpty())
return ItemStack.EMPTY;
int toExtract = Math.min(amount, existing.getMaxStackSize());
if (existing.getCount() <= toExtract)
{
if (!simulate)
{
setStackInSlot(slot, ItemStack.EMPTY);
return existing;
}
else
{
return existing.copy();
}
}
else
{
if (!simulate)
{
setStackInSlot(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
}
return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
}
}
@Override
public int getSlotLimit(int slot) {
return inventory.getInventoryStackLimit();
}
@Override
public boolean isItemValid(int slot, ItemStack stack) {
return inventory.isItemValidForSlot(slot, stack);
}
private void validateSlotIndex(int slot)
{
if (slot < 0 || slot >= getSlots())
throw new RuntimeException("Slot " + slot + " not in valid range - [0," + getSlots() + ")");
}
private int getStackLimit(int slot, ItemStack stack)
{
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
}
}

View file

@ -27,6 +27,9 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.template.Template.BlockInfo;
import net.minecraft.entity.Entity;
import net.minecraft.inventory.IInventory;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
public class MountedContraption extends Contraption {
@ -86,6 +89,7 @@ public class MountedContraption extends Contraption {
if (!CartAssemblerBlock.canAssembleTo(abstractMinecartEntity))
break;
connectedCart = abstractMinecartEntity;
addExtraInventories(abstractMinecartEntity);
}
}
}
@ -126,4 +130,9 @@ public class MountedContraption extends Contraption {
return AllBlocks.MINECART_ANCHOR.has(state);
}
@Override
public void addExtraInventories(Entity cart) {
if (cart instanceof IInventory)
inventory = new CombinedInvWrapper(new ItemHandlerModifiableFromIInventory((IInventory) cart), inventory);
}
}