From c1316d1c8d0df6acd43dd20114bcf977ce8ae9b4 Mon Sep 17 00:00:00 2001 From: grimmauld Date: Thu, 1 Oct 2020 12:37:55 +0200 Subject: [PATCH] Dump the moving bucket --- .../java/com/simibubi/create/AllBlocks.java | 2 + .../processing/BasinMovementBehaviour.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/processing/BasinMovementBehaviour.java diff --git a/src/main/java/com/simibubi/create/AllBlocks.java b/src/main/java/com/simibubi/create/AllBlocks.java index 3514f9fb9..7a253cb0d 100644 --- a/src/main/java/com/simibubi/create/AllBlocks.java +++ b/src/main/java/com/simibubi/create/AllBlocks.java @@ -73,6 +73,7 @@ import com.simibubi.create.content.contraptions.fluids.tank.FluidTankGenerator; import com.simibubi.create.content.contraptions.fluids.tank.FluidTankItem; import com.simibubi.create.content.contraptions.fluids.tank.FluidTankModel; import com.simibubi.create.content.contraptions.processing.BasinBlock; +import com.simibubi.create.content.contraptions.processing.BasinMovementBehaviour; import com.simibubi.create.content.contraptions.processing.burner.BlazeBurnerBlock; import com.simibubi.create.content.contraptions.processing.burner.BlazeBurnerBlockItem; import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerBlock; @@ -408,6 +409,7 @@ public class AllBlocks { public static final BlockEntry BASIN = REGISTRATE.block("basin", BasinBlock::new) .initialProperties(SharedProperties::stone) .blockstate((ctx, prov) -> prov.simpleBlock(ctx.getEntry(), AssetLookup.standardModel(ctx, prov))) + .onRegister(addMovementBehaviour(new BasinMovementBehaviour())) .simpleItem() .register(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/processing/BasinMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/processing/BasinMovementBehaviour.java new file mode 100644 index 000000000..890f26db5 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/processing/BasinMovementBehaviour.java @@ -0,0 +1,64 @@ +package com.simibubi.create.content.contraptions.processing; + +import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; +import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.foundation.utility.VecHelper; +import net.minecraft.entity.item.ItemEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Direction; +import net.minecraft.util.math.Vec3d; +import net.minecraftforge.items.ItemStackHandler; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class BasinMovementBehaviour extends MovementBehaviour { + @SuppressWarnings("unchecked") + public Map getOrReadInventory(MovementContext context) { + if (!(context.temporaryData instanceof List)) { + Map map = new HashMap<>(); + map.put("InputItems", new ItemStackHandler(9)); + map.put("OutputItems", new ItemStackHandler(8)); + map.forEach((s, h) -> h.deserializeNBT(context.tileData.getCompound(s))); + context.temporaryData = map; + } + return (Map) context.temporaryData; + } + + @Override + public void writeExtraData(MovementContext context) { + super.writeExtraData(context); + getOrReadInventory(context).forEach((s, h) -> context.tileData.put(s, h.serializeNBT())); + } + + @Override + public boolean hasSpecialMovementRenderer() { + return false; + } + + @Override + public void tick(MovementContext context) { + super.tick(context); + Vec3d facingVec = VecHelper.rotate(new Vec3d(Direction.UP.getDirectionVec()), context.rotation.x, context.rotation.y, context.rotation.z); + facingVec.normalize(); + if (Direction.getFacingFromVector(facingVec.x, facingVec.y, facingVec.z) == Direction.DOWN) + dump(context, facingVec); + } + + private void dump(MovementContext context, Vec3d facingVec) { + getOrReadInventory(context).forEach((key, itemStackHandler) -> { + for (int i = 0; i < itemStackHandler.getSlots(); i++) { + if (itemStackHandler.getStackInSlot(i).isEmpty()) + continue; + ItemEntity itemEntity = new ItemEntity(context.world, context.position.x, context.position.y, context.position.z, itemStackHandler.getStackInSlot(i)); + itemEntity.setMotion(facingVec.scale(.05)); + context.world.addEntity(itemEntity); + itemStackHandler.setStackInSlot(i, ItemStack.EMPTY); + } + context.tileData.put(key, itemStackHandler.serializeNBT()); + }); + context.temporaryData = null; + context.contraption.customRenderTEs.stream().filter(te -> te.getPos().equals(context.localPos)).forEach(te -> te.read(context.tileData)); + } +}