Search in sources :

Example 6 with Direction

use of net.minecraft.core.Direction in project MinecraftForge by MinecraftForge.

the class DispenseFluidContainer method dumpContainer.

/**
 * Drains a filled container and places the fluid in front of the Dispenser.
 */
@Nonnull
private ItemStack dumpContainer(BlockSource source, @Nonnull ItemStack stack) {
    ItemStack singleStack = stack.copy();
    singleStack.setCount(1);
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(singleStack).orElse(null);
    if (fluidHandler == null) {
        return super.execute(source, stack);
    }
    FluidStack fluidStack = fluidHandler.drain(FluidAttributes.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
    Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
    BlockPos blockpos = source.getPos().relative(dispenserFacing);
    FluidActionResult result = FluidUtil.tryPlaceFluid(null, source.getLevel(), InteractionHand.MAIN_HAND, blockpos, stack, fluidStack);
    if (result.isSuccess()) {
        ItemStack drainedStack = result.getResult();
        if (drainedStack.getCount() == 1) {
            return drainedStack;
        } else if (!drainedStack.isEmpty() && ((DispenserBlockEntity) source.getEntity()).addItem(drainedStack) < 0) {
            this.dispenseBehavior.dispense(source, drainedStack);
        }
        ItemStack stackCopy = drainedStack.copy();
        stackCopy.shrink(1);
        return stackCopy;
    } else {
        return this.dispenseBehavior.dispense(source, stack);
    }
}
Also used : IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) Nonnull(javax.annotation.Nonnull)

Example 7 with Direction

use of net.minecraft.core.Direction in project MinecraftForge by MinecraftForge.

the class VanillaInventoryCodeHooks method dropperInsertHook.

/**
 * Copied from BlockDropper#dispense and added capability support
 */
public static boolean dropperInsertHook(Level world, BlockPos pos, DispenserBlockEntity dropper, int slot, @Nonnull ItemStack stack) {
    Direction enumfacing = world.getBlockState(pos).getValue(DropperBlock.FACING);
    BlockPos blockpos = pos.relative(enumfacing);
    return getItemHandler(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), enumfacing.getOpposite()).map(destinationResult -> {
        IItemHandler itemHandler = destinationResult.getKey();
        Object destination = destinationResult.getValue();
        ItemStack dispensedStack = stack.copy().split(1);
        ItemStack remainder = putStackInInventoryAllSlots(dropper, destination, itemHandler, dispensedStack);
        if (remainder.isEmpty()) {
            remainder = stack.copy();
            remainder.shrink(1);
        } else {
            remainder = stack.copy();
        }
        dropper.setItem(slot, remainder);
        return false;
    }).orElse(true);
}
Also used : HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) Direction(net.minecraft.core.Direction) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) DropperBlock(net.minecraft.world.level.block.DropperBlock) Pair(org.apache.commons.lang3.tuple.Pair) BlockPos(net.minecraft.core.BlockPos) HopperBlock(net.minecraft.world.level.block.HopperBlock) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Hopper(net.minecraft.world.level.block.entity.Hopper) Level(net.minecraft.world.level.Level) Mth(net.minecraft.util.Mth) Nonnull(javax.annotation.Nonnull) DispenserBlockEntity(net.minecraft.world.level.block.entity.DispenserBlockEntity) Nullable(javax.annotation.Nullable) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Example 8 with Direction

use of net.minecraft.core.Direction in project MinecraftForge by MinecraftForge.

the class BlockStateProvider method paneBlock.

public void paneBlock(IronBarsBlock block, ModelFile post, ModelFile side, ModelFile sideAlt, ModelFile noSide, ModelFile noSideAlt) {
    MultiPartBlockStateBuilder builder = getMultipartBuilder(block).part().modelFile(post).addModel().end();
    PipeBlock.PROPERTY_BY_DIRECTION.entrySet().forEach(e -> {
        Direction dir = e.getKey();
        if (dir.getAxis().isHorizontal()) {
            boolean alt = dir == Direction.SOUTH;
            builder.part().modelFile(alt || dir == Direction.WEST ? sideAlt : side).rotationY(dir.getAxis() == Axis.X ? 90 : 0).addModel().condition(e.getValue(), true).end().part().modelFile(alt || dir == Direction.EAST ? noSideAlt : noSide).rotationY(dir == Direction.WEST ? 270 : dir == Direction.SOUTH ? 90 : 0).addModel().condition(e.getValue(), false);
        }
    });
}
Also used : Direction(net.minecraft.core.Direction)

Example 9 with Direction

use of net.minecraft.core.Direction in project MinecraftForge by MinecraftForge.

the class ModelBuilder method toJson.

@VisibleForTesting
public JsonObject toJson() {
    JsonObject root = new JsonObject();
    if (this.parent != null) {
        root.addProperty("parent", this.parent.getLocation().toString());
    }
    if (!this.ambientOcclusion) {
        root.addProperty("ambientocclusion", this.ambientOcclusion);
    }
    if (this.guiLight != null) {
        root.addProperty("gui_light", this.guiLight.getSerializedName());
    }
    Map<Perspective, ItemTransform> transforms = this.transforms.build();
    if (!transforms.isEmpty()) {
        JsonObject display = new JsonObject();
        for (Entry<Perspective, ItemTransform> e : transforms.entrySet()) {
            JsonObject transform = new JsonObject();
            ItemTransform vec = e.getValue();
            if (vec.equals(ItemTransform.NO_TRANSFORM))
                continue;
            if (!vec.rotation.equals(ItemTransform.Deserializer.DEFAULT_ROTATION)) {
                transform.add("rotation", serializeVector3f(vec.rotation));
            }
            if (!vec.translation.equals(ItemTransform.Deserializer.DEFAULT_TRANSLATION)) {
                transform.add("translation", serializeVector3f(e.getValue().translation));
            }
            if (!vec.scale.equals(ItemTransform.Deserializer.DEFAULT_SCALE)) {
                transform.add("scale", serializeVector3f(e.getValue().scale));
            }
            display.add(e.getKey().name, transform);
        }
        root.add("display", display);
    }
    if (!this.textures.isEmpty()) {
        JsonObject textures = new JsonObject();
        for (Entry<String, String> e : this.textures.entrySet()) {
            textures.addProperty(e.getKey(), serializeLocOrKey(e.getValue()));
        }
        root.add("textures", textures);
    }
    if (!this.elements.isEmpty()) {
        JsonArray elements = new JsonArray();
        this.elements.stream().map(ElementBuilder::build).forEach(part -> {
            JsonObject partObj = new JsonObject();
            partObj.add("from", serializeVector3f(part.from));
            partObj.add("to", serializeVector3f(part.to));
            if (part.rotation != null) {
                JsonObject rotation = new JsonObject();
                rotation.add("origin", serializeVector3f(part.rotation.origin));
                rotation.addProperty("axis", part.rotation.axis.getSerializedName());
                rotation.addProperty("angle", part.rotation.angle);
                if (part.rotation.rescale) {
                    rotation.addProperty("rescale", part.rotation.rescale);
                }
                partObj.add("rotation", rotation);
            }
            if (!part.shade) {
                partObj.addProperty("shade", part.shade);
            }
            JsonObject faces = new JsonObject();
            for (Direction dir : Direction.values()) {
                BlockElementFace face = part.faces.get(dir);
                if (face == null)
                    continue;
                JsonObject faceObj = new JsonObject();
                faceObj.addProperty("texture", serializeLocOrKey(face.texture));
                if (!Arrays.equals(face.uv.uvs, part.uvsByFace(dir))) {
                    faceObj.add("uv", new Gson().toJsonTree(face.uv.uvs));
                }
                if (face.cullForDirection != null) {
                    faceObj.addProperty("cullface", face.cullForDirection.getSerializedName());
                }
                if (face.uv.rotation != 0) {
                    faceObj.addProperty("rotation", face.uv.rotation);
                }
                if (face.tintIndex != -1) {
                    faceObj.addProperty("tintindex", face.tintIndex);
                }
                faces.add(dir.getSerializedName(), faceObj);
            }
            if (!part.faces.isEmpty()) {
                partObj.add("faces", faces);
            }
            elements.add(partObj);
        });
        root.add("elements", elements);
    }
    if (customLoader != null)
        return customLoader.toJson(root);
    return root;
}
Also used : JsonArray(com.google.gson.JsonArray) ItemTransform(net.minecraft.client.renderer.block.model.ItemTransform) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) BlockElementFace(net.minecraft.client.renderer.block.model.BlockElementFace) Direction(net.minecraft.core.Direction) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with Direction

use of net.minecraft.core.Direction in project MinecraftForge by MinecraftForge.

the class ItemLayerModel method getQuadsForSprite.

public static ImmutableList<BakedQuad> getQuadsForSprite(int tint, TextureAtlasSprite sprite, Transformation transform, boolean fullbright) {
    ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
    int uMax = sprite.getWidth();
    int vMax = sprite.getHeight();
    FaceData faceData = new FaceData(uMax, vMax);
    boolean translucent = false;
    for (int f = 0; f < sprite.getFrameCount(); f++) {
        boolean ptu;
        boolean[] ptv = new boolean[uMax];
        Arrays.fill(ptv, true);
        for (int v = 0; v < vMax; v++) {
            ptu = true;
            for (int u = 0; u < uMax; u++) {
                int alpha = sprite.getPixelRGBA(f, u, vMax - v - 1) >> 24 & 0xFF;
                boolean t = alpha / 255f <= 0.1f;
                if (!t && alpha < 255) {
                    translucent = true;
                }
                if (// left - transparent, right - opaque
                ptu && !t) {
                    faceData.set(Direction.WEST, u, v);
                }
                if (// left - opaque, right - transparent
                !ptu && t) {
                    faceData.set(Direction.EAST, u - 1, v);
                }
                if (// up - transparent, down - opaque
                ptv[u] && !t) {
                    faceData.set(Direction.UP, u, v);
                }
                if (// up - opaque, down - transparent
                !ptv[u] && t) {
                    faceData.set(Direction.DOWN, u, v - 1);
                }
                ptu = t;
                ptv[u] = t;
            }
            if (// last - opaque
            !ptu) {
                faceData.set(Direction.EAST, uMax - 1, v);
            }
        }
        // last line
        for (int u = 0; u < uMax; u++) {
            if (!ptv[u]) {
                faceData.set(Direction.DOWN, u, vMax - 1);
            }
        }
    }
    // horizontal quads
    for (Direction facing : HORIZONTALS) {
        for (int v = 0; v < vMax; v++) {
            int uStart = 0, uEnd = uMax;
            boolean building = false;
            for (int u = 0; u < uMax; u++) {
                boolean face = faceData.get(facing, u, v);
                if (!translucent) {
                    if (face) {
                        if (!building) {
                            building = true;
                            uStart = u;
                        }
                        uEnd = u + 1;
                    }
                } else {
                    if (// finish current quad
                    building && !face) {
                        // make quad [uStart, u]
                        int off = facing == Direction.DOWN ? 1 : 0;
                        builder.add(buildSideQuad(transform, facing, tint, sprite, uStart, v + off, u - uStart, fullbright));
                        building = false;
                    } else if (// start new quad
                    !building && face) {
                        building = true;
                        uStart = u;
                    }
                }
            }
            if (// build remaining quad
            building) {
                // make quad [uStart, uEnd]
                int off = facing == Direction.DOWN ? 1 : 0;
                builder.add(buildSideQuad(transform, facing, tint, sprite, uStart, v + off, uEnd - uStart, fullbright));
            }
        }
    }
    // vertical quads
    for (Direction facing : VERTICALS) {
        for (int u = 0; u < uMax; u++) {
            int vStart = 0, vEnd = vMax;
            boolean building = false;
            for (int v = 0; v < vMax; v++) {
                boolean face = faceData.get(facing, u, v);
                if (!translucent) {
                    if (face) {
                        if (!building) {
                            building = true;
                            vStart = v;
                        }
                        vEnd = v + 1;
                    }
                } else {
                    if (// finish current quad
                    building && !face) {
                        // make quad [vStart, v]
                        int off = facing == Direction.EAST ? 1 : 0;
                        builder.add(buildSideQuad(transform, facing, tint, sprite, u + off, vStart, v - vStart, fullbright));
                        building = false;
                    } else if (// start new quad
                    !building && face) {
                        building = true;
                        vStart = v;
                    }
                }
            }
            if (// build remaining quad
            building) {
                // make quad [vStart, vEnd]
                int off = facing == Direction.EAST ? 1 : 0;
                builder.add(buildSideQuad(transform, facing, tint, sprite, u + off, vStart, vEnd - vStart, fullbright));
            }
        }
    }
    // front
    builder.add(buildQuad(transform, Direction.NORTH, sprite, tint, fullbright, 0, 0, 7.5f / 16f, sprite.getU0(), sprite.getV1(), 0, 1, 7.5f / 16f, sprite.getU0(), sprite.getV0(), 1, 1, 7.5f / 16f, sprite.getU1(), sprite.getV0(), 1, 0, 7.5f / 16f, sprite.getU1(), sprite.getV1()));
    // back
    builder.add(buildQuad(transform, Direction.SOUTH, sprite, tint, fullbright, 0, 0, 8.5f / 16f, sprite.getU0(), sprite.getV1(), 1, 0, 8.5f / 16f, sprite.getU1(), sprite.getV1(), 1, 1, 8.5f / 16f, sprite.getU1(), sprite.getV0(), 0, 1, 8.5f / 16f, sprite.getU0(), sprite.getV0()));
    return builder.build();
}
Also used : BakedQuad(net.minecraft.client.renderer.block.model.BakedQuad) Direction(net.minecraft.core.Direction)

Aggregations

Direction (net.minecraft.core.Direction)17 BlockPos (net.minecraft.core.BlockPos)4 Nonnull (javax.annotation.Nonnull)3 BakedQuad (net.minecraft.client.renderer.block.model.BakedQuad)3 ItemStack (net.minecraft.world.item.ItemStack)3 Level (net.minecraft.world.level.Level)2 BellBlock (net.minecraft.world.level.block.BellBlock)2 DispenserBlockEntity (net.minecraft.world.level.block.entity.DispenserBlockEntity)2 org.bukkit.block (org.bukkit.block)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 VertexConsumer (com.mojang.blaze3d.vertex.VertexConsumer)1 Transformation (com.mojang.math.Transformation)1 Vector3f (com.mojang.math.Vector3f)1 Vector4f (com.mojang.math.Vector4f)1 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 BlockElementFace (net.minecraft.client.renderer.block.model.BlockElementFace)1