Search in sources :

Example 1 with GroupedItems

use of com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method read.

@Override
protected void read(CompoundTag compound, boolean clientPacket) {
    Phase phaseBefore = phase;
    GroupedItems before = this.groupedItems;
    inventory.deserializeNBT(compound.getCompound("Inventory"));
    input.read(compound.getCompound("ConnectedInput"));
    groupedItems = GroupedItems.read(compound.getCompound("GroupedItems"));
    phase = Phase.IDLE;
    String name = compound.getString("Phase");
    for (Phase phase : Phase.values()) if (phase.name().equals(name))
        this.phase = phase;
    countDown = compound.getInt("CountDown");
    covered = compound.getBoolean("Cover");
    super.read(compound, clientPacket);
    if (!clientPacket)
        return;
    if (compound.contains("Redraw"))
        level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 16);
    if (phaseBefore != phase && phase == Phase.CRAFTING)
        groupedItemsBeforeCraft = before;
    if (phaseBefore == Phase.EXPORTING && phase == Phase.WAITING) {
        Direction facing = getBlockState().getValue(MechanicalCrafterBlock.HORIZONTAL_FACING);
        Vec3 vec = Vec3.atLowerCornerOf(facing.getNormal()).scale(.75).add(VecHelper.getCenterOf(worldPosition));
        Direction targetDirection = MechanicalCrafterBlock.getTargetDirection(getBlockState());
        vec = vec.add(Vec3.atLowerCornerOf(targetDirection.getNormal()).scale(1));
        level.addParticle(ParticleTypes.CRIT, vec.x, vec.y, vec.z, 0, 0, 0);
    }
}
Also used : GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) Vec3(net.minecraft.world.phys.Vec3) Direction(net.minecraft.core.Direction)

Example 2 with GroupedItems

use of com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems in project Create by Creators-of-Create.

the class MechanicalCrafterRenderer method renderItems.

public void renderItems(MechanicalCrafterTileEntity te, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light, int overlay) {
    if (te.phase == Phase.IDLE) {
        ItemStack stack = te.getInventory().getItem(0);
        if (!stack.isEmpty()) {
            ms.pushPose();
            ms.translate(0, 0, -1 / 256f);
            ms.mulPose(Vector3f.YP.rotationDegrees(180));
            Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
            ms.popPose();
        }
    } else {
        // render grouped items
        GroupedItems items = te.groupedItems;
        float distance = .5f;
        ms.pushPose();
        if (te.phase == Phase.CRAFTING) {
            items = te.groupedItemsBeforeCraft;
            items.calcStats();
            float progress = Mth.clamp((2000 - te.countDown + te.getCountDownSpeed() * partialTicks) / 1000f, 0, 1);
            float earlyProgress = Mth.clamp(progress * 2, 0, 1);
            float lateProgress = Mth.clamp(progress * 2 - 1, 0, 1);
            ms.scale(1 - lateProgress, 1 - lateProgress, 1 - lateProgress);
            Vec3 centering = new Vec3(-items.minX + (-items.width + 1) / 2f, -items.minY + (-items.height + 1) / 2f, 0).scale(earlyProgress);
            ms.translate(centering.x * .5f, centering.y * .5f, 0);
            distance += (-4 * (progress - .5f) * (progress - .5f) + 1) * .25f;
        }
        boolean onlyRenderFirst = te.phase == Phase.INSERTING || te.phase == Phase.CRAFTING && te.countDown < 1000;
        final float spacing = distance;
        items.grid.forEach((pair, stack) -> {
            if (onlyRenderFirst && (pair.getLeft().intValue() != 0 || pair.getRight().intValue() != 0))
                return;
            ms.pushPose();
            Integer x = pair.getKey();
            Integer y = pair.getValue();
            ms.translate(x * spacing, y * spacing, 0);
            int offset = 0;
            if (te.phase == Phase.EXPORTING && te.getBlockState().hasProperty(MechanicalCrafterBlock.POINTING)) {
                Pointing value = te.getBlockState().getValue(MechanicalCrafterBlock.POINTING);
                offset = value == Pointing.UP ? -1 : value == Pointing.LEFT ? 2 : value == Pointing.RIGHT ? -2 : 1;
            }
            TransformStack.cast(ms).rotateY(180).translate(0, 0, (x + y * 3 + offset * 9) / 1024f);
            Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
            ms.popPose();
        });
        ms.popPose();
        if (te.phase == Phase.CRAFTING) {
            items = te.groupedItems;
            float progress = Mth.clamp((1000 - te.countDown + te.getCountDownSpeed() * partialTicks) / 1000f, 0, 1);
            float earlyProgress = Mth.clamp(progress * 2, 0, 1);
            float lateProgress = Mth.clamp(progress * 2 - 1, 0, 1);
            ms.mulPose(Vector3f.ZP.rotationDegrees(earlyProgress * 2 * 360));
            float upScaling = earlyProgress * 1.125f;
            float downScaling = 1 + (1 - lateProgress) * .125f;
            ms.scale(upScaling, upScaling, upScaling);
            ms.scale(downScaling, downScaling, downScaling);
            items.grid.forEach((pair, stack) -> {
                if (pair.getLeft().intValue() != 0 || pair.getRight().intValue() != 0)
                    return;
                ms.pushPose();
                ms.mulPose(Vector3f.YP.rotationDegrees(180));
                Minecraft.getInstance().getItemRenderer().renderStatic(stack, TransformType.FIXED, light, overlay, ms, buffer, 0);
                ms.popPose();
            });
        }
    }
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) Vec3(net.minecraft.world.phys.Vec3) ItemStack(net.minecraft.world.item.ItemStack)

Example 3 with GroupedItems

use of com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method begin.

protected void begin() {
    phase = Phase.ACCEPTING;
    groupedItems = new GroupedItems(inventory.getItem(0));
    inventory.setStackInSlot(0, ItemStack.EMPTY);
    if (RecipeGridHandler.getPrecedingCrafters(this).isEmpty()) {
        phase = Phase.ASSEMBLING;
        countDown = 500;
    }
    sendData();
}
Also used : GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems)

Example 4 with GroupedItems

use of com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method tick.

@Override
public void tick() {
    super.tick();
    if (phase == Phase.ACCEPTING)
        return;
    boolean onClient = level.isClientSide;
    boolean runLogic = !onClient || isVirtual();
    if (wasPoweredBefore != level.hasNeighborSignal(worldPosition)) {
        wasPoweredBefore = level.hasNeighborSignal(worldPosition);
        if (wasPoweredBefore) {
            if (!runLogic)
                return;
            checkCompletedRecipe(true);
        }
    }
    if (phase == Phase.ASSEMBLING) {
        countDown -= getCountDownSpeed();
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            if (RecipeGridHandler.getTargetingCrafter(this) != null) {
                phase = Phase.EXPORTING;
                countDown = 1000;
                sendData();
                return;
            }
            ItemStack result = isVirtual() ? scriptedResult : RecipeGridHandler.tryToApplyRecipe(level, groupedItems);
            if (result != null) {
                List<ItemStack> containers = new ArrayList<>();
                groupedItems.grid.values().forEach(stack -> {
                    if (stack.hasContainerItem())
                        containers.add(stack.getContainerItem().copy());
                });
                if (isVirtual())
                    groupedItemsBeforeCraft = groupedItems;
                groupedItems = new GroupedItems(result);
                for (int i = 0; i < containers.size(); i++) {
                    ItemStack stack = containers.get(i);
                    GroupedItems container = new GroupedItems();
                    container.grid.put(Pair.of(i, 0), stack);
                    container.mergeOnto(groupedItems, Pointing.LEFT);
                }
                phase = Phase.CRAFTING;
                countDown = 2000;
                sendData();
                return;
            }
            ejectWholeGrid();
            return;
        }
    }
    if (phase == Phase.EXPORTING) {
        countDown -= getCountDownSpeed();
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            MechanicalCrafterTileEntity targetingCrafter = RecipeGridHandler.getTargetingCrafter(this);
            if (targetingCrafter == null) {
                ejectWholeGrid();
                return;
            }
            Pointing pointing = getBlockState().getValue(MechanicalCrafterBlock.POINTING);
            groupedItems.mergeOnto(targetingCrafter.groupedItems, pointing);
            groupedItems = new GroupedItems();
            float pitch = targetingCrafter.groupedItems.grid.size() * 1 / 16f + .5f;
            AllSoundEvents.CRAFTER_CLICK.playOnServer(level, worldPosition, 1, pitch);
            phase = Phase.WAITING;
            countDown = 0;
            sendData();
            targetingCrafter.continueIfAllPrecedingFinished();
            targetingCrafter.sendData();
            return;
        }
    }
    if (phase == Phase.CRAFTING) {
        if (onClient) {
            Direction facing = getBlockState().getValue(MechanicalCrafterBlock.HORIZONTAL_FACING);
            float progress = countDown / 2000f;
            Vec3 facingVec = Vec3.atLowerCornerOf(facing.getNormal());
            Vec3 vec = facingVec.scale(.65).add(VecHelper.getCenterOf(worldPosition));
            Vec3 offset = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .125f).multiply(VecHelper.axisAlingedPlaneOf(facingVec)).normalize().scale(progress * .5f).add(vec);
            if (progress > .5f)
                level.addParticle(ParticleTypes.CRIT, offset.x, offset.y, offset.z, 0, 0, 0);
            if (!groupedItemsBeforeCraft.grid.isEmpty() && progress < .5f) {
                if (groupedItems.grid.containsKey(Pair.of(0, 0))) {
                    ItemStack stack = groupedItems.grid.get(Pair.of(0, 0));
                    groupedItemsBeforeCraft = new GroupedItems();
                    for (int i = 0; i < 10; i++) {
                        Vec3 randVec = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .125f).multiply(VecHelper.axisAlingedPlaneOf(facingVec)).normalize().scale(.25f);
                        Vec3 offset2 = randVec.add(vec);
                        randVec = randVec.scale(.35f);
                        level.addParticle(new ItemParticleOption(ParticleTypes.ITEM, stack), offset2.x, offset2.y, offset2.z, randVec.x, randVec.y, randVec.z);
                    }
                }
            }
        }
        int prev = countDown;
        countDown -= getCountDownSpeed();
        if (countDown < 1000 && prev >= 1000) {
            AllSoundEvents.CRAFTER_CLICK.playOnServer(level, worldPosition, 1, 2);
            AllSoundEvents.CRAFTER_CRAFT.playOnServer(level, worldPosition);
        }
        if (countDown < 0) {
            countDown = 0;
            if (!runLogic)
                return;
            tryInsert();
            return;
        }
    }
    if (phase == Phase.INSERTING) {
        if (runLogic && isTargetingBelt())
            tryInsert();
        return;
    }
}
Also used : Pointing(com.simibubi.create.foundation.utility.Pointing) GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) Vec3(net.minecraft.world.phys.Vec3) ArrayList(java.util.ArrayList) ItemParticleOption(net.minecraft.core.particles.ItemParticleOption) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Example 5 with GroupedItems

use of com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method eject.

public void eject() {
    BlockState blockState = getBlockState();
    boolean present = AllBlocks.MECHANICAL_CRAFTER.has(blockState);
    Vec3 vec = present ? Vec3.atLowerCornerOf(blockState.getValue(HORIZONTAL_FACING).getNormal()).scale(.75f) : Vec3.ZERO;
    Vec3 ejectPos = VecHelper.getCenterOf(worldPosition).add(vec);
    groupedItems.grid.forEach((pair, stack) -> dropItem(ejectPos, stack));
    if (!inventory.getItem(0).isEmpty())
        dropItem(ejectPos, inventory.getItem(0));
    phase = Phase.IDLE;
    groupedItems = new GroupedItems();
    inventory.setStackInSlot(0, ItemStack.EMPTY);
    sendData();
}
Also used : GroupedItems(com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems) BlockState(net.minecraft.world.level.block.state.BlockState) Vec3(net.minecraft.world.phys.Vec3)

Aggregations

GroupedItems (com.simibubi.create.content.contraptions.components.crafter.RecipeGridHandler.GroupedItems)5 Vec3 (net.minecraft.world.phys.Vec3)4 Pointing (com.simibubi.create.foundation.utility.Pointing)2 Direction (net.minecraft.core.Direction)2 ItemStack (net.minecraft.world.item.ItemStack)2 ArrayList (java.util.ArrayList)1 ItemParticleOption (net.minecraft.core.particles.ItemParticleOption)1 BlockState (net.minecraft.world.level.block.state.BlockState)1