diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index edbd69c80..cbbca28be 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -828,4 +828,5 @@ public abstract class Contraption { return seats; } + public void addExtraInventories(Entity entity) {}; } \ No newline at end of file diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java index 05ab6b777..29766dc28 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntity.java @@ -77,6 +77,7 @@ public class ContraptionEntity extends Entity implements IEntityAdditionalSpawnD protected boolean initialized; final List 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 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) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java index d222e0d5d..392ab18b9 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java @@ -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 diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/ItemHandlerModifiableFromIInventory.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/ItemHandlerModifiableFromIInventory.java new file mode 100644 index 000000000..f0eae3e70 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/ItemHandlerModifiableFromIInventory.java @@ -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()); + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java index 03383f747..1ac7813f6 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java @@ -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); + } }