Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extended facing to single-block machines #2823

Draft
wants to merge 13 commits into
base: 1.20.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.MultiblockMachineDefinition;
import com.gregtechceu.gtceu.api.machine.feature.*;
import com.gregtechceu.gtceu.common.data.GTItems;
import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner;
Expand Down Expand Up @@ -77,7 +76,7 @@ public MetaMachineBlock(Properties properties, MachineDefinition definition) {
if (rotationState != RotationState.NONE) {
BlockState defaultState = this.defaultBlockState().setValue(rotationState.property,
rotationState.defaultDirection);
if (definition instanceof MultiblockMachineDefinition multi && multi.isAllowExtendedFacing()) {
if (definition.isAllowExtendedFacing()) {
defaultState = defaultState.setValue(IMachineBlock.UPWARDS_FACING_PROPERTY, Direction.NORTH);
}
registerDefaultState(defaultState);
Expand All @@ -90,8 +89,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
RotationState rotationState = RotationState.get();
if (rotationState != RotationState.NONE) {
pBuilder.add(rotationState.property);
if (MachineDefinition.getBuilt() instanceof MultiblockMachineDefinition multi &&
multi.isAllowExtendedFacing()) {
if (MachineDefinition.getBuilt().isAllowExtendedFacing()) {
pBuilder.add(IMachineBlock.UPWARDS_FACING_PROPERTY);
}
}
Expand Down Expand Up @@ -177,7 +175,7 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
state = state.setValue(rotationState.property, Direction.DOWN);
}
}
if (getDefinition() instanceof MultiblockMachineDefinition multi && multi.isAllowExtendedFacing()) {
if (getDefinition().isAllowExtendedFacing()) {
Direction frontFacing = state.getValue(rotationState.property);
if (frontFacing == Direction.UP) {
state = state.setValue(IMachineBlock.UPWARDS_FACING_PROPERTY, player.getDirection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public class MachineDefinition implements Supplier<IMachineBlock> {
@Getter
@Setter
private boolean regressWhenWaiting = true;
/** Whether this machine can be rotated or face upwards. */
@Getter
@Setter
private boolean allowExtendedFacing;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.block.BlockProperties;
import com.gregtechceu.gtceu.api.block.IAppearance;
import com.gregtechceu.gtceu.api.block.IMachineBlock;
import com.gregtechceu.gtceu.api.block.MetaMachineBlock;
import com.gregtechceu.gtceu.api.blockentity.IPaintable;
import com.gregtechceu.gtceu.api.blockentity.ITickSubscription;
Expand All @@ -22,6 +23,7 @@
import com.gregtechceu.gtceu.api.machine.trait.MachineTrait;
import com.gregtechceu.gtceu.api.misc.IOFilteredInvWrapper;
import com.gregtechceu.gtceu.api.misc.IOFluidHandlerList;
import com.gregtechceu.gtceu.api.pattern.util.RelativeDirection;
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
import com.gregtechceu.gtceu.common.cover.FluidFilterCover;
import com.gregtechceu.gtceu.common.cover.ItemFilterCover;
Expand Down Expand Up @@ -373,6 +375,11 @@ protected InteractionResult onCrowbarClick(Player playerIn, InteractionHand hand

protected InteractionResult onWrenchClick(Player playerIn, InteractionHand hand, Direction gridSide,
BlockHitResult hitResult) {
if (gridSide == getFrontFacing() && allowExtendedFacing()) {
setUpwardsFacing(playerIn.isShiftKeyDown() ? getUpwardsFacing().getCounterClockWise() :
getUpwardsFacing().getClockWise());
return InteractionResult.CONSUME;
}
if (playerIn.isShiftKeyDown()) {
if (gridSide == getFrontFacing() || !isFacingValid(gridSide)) {
return InteractionResult.FAIL;
Expand Down Expand Up @@ -587,6 +594,9 @@ public final boolean hasFrontFacing() {
}

public boolean isFacingValid(Direction facing) {
if (allowExtendedFacing()) {
return true;
}
if (hasFrontFacing() && facing == getFrontFacing()) return false;
var blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock metaMachineBlock) {
Expand All @@ -596,15 +606,56 @@ public boolean isFacingValid(Direction facing) {
}

public void setFrontFacing(Direction facing) {
var oldFacing = getFrontFacing();

if (allowExtendedFacing()) {
var newUpwardsFacing = RelativeDirection.simulateAxisRotation(facing, oldFacing, getUpwardsFacing());
setUpwardsFacing(newUpwardsFacing);
}

var blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock metaMachineBlock && isFacingValid(facing)) {
getLevel().setBlockAndUpdate(getPos(),
blockState.setValue(metaMachineBlock.rotationState.property, facing));
}

if (getLevel() != null && !getLevel().isClientSide) {
notifyBlockUpdate();
markDirty();
}
}

public Direction getUpwardsFacing() {
return this.allowExtendedFacing() ? this.getBlockState().getValue(IMachineBlock.UPWARDS_FACING_PROPERTY) :
Direction.NORTH;
}

public void setUpwardsFacing(@NotNull Direction upwardsFacing) {
if (!getDefinition().isAllowExtendedFacing()) {
return;
}
if (upwardsFacing.getAxis() == Direction.Axis.Y) {
GTCEu.LOGGER.error("Tried to set upwards facing to invalid facing {}! Skipping", upwardsFacing);
return;
}
var blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock &&
blockState.getValue(IMachineBlock.UPWARDS_FACING_PROPERTY) != upwardsFacing) {
getLevel().setBlockAndUpdate(getPos(),
blockState.setValue(IMachineBlock.UPWARDS_FACING_PROPERTY, upwardsFacing));
if (getLevel() != null && !getLevel().isClientSide) {
notifyBlockUpdate();
markDirty();
}
}
}

public void onRotated(Direction oldFacing, Direction newFacing) {}

public boolean allowExtendedFacing() {
return getDefinition().isAllowExtendedFacing();
}

public int tintColor(int index) {
// index < -100 => emission if shimmer is installed.
if (index == 1 || index == -111) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public class MultiblockMachineDefinition extends MachineDefinition {
@Setter
@Getter
private Supplier<List<MultiblockShapeInfo>> shapes;
/** Whether this multi can be rotated or face upwards. */
@Getter
@Setter
private boolean allowExtendedFacing;
/** Set this to false only if your multiblock is set up such that it could have a wall-shared controller. */
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiPart;
import com.gregtechceu.gtceu.api.pattern.MultiblockState;
import com.gregtechceu.gtceu.api.pattern.MultiblockWorldSavedData;
import com.gregtechceu.gtceu.api.pattern.util.RelativeDirection;

import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
Expand Down Expand Up @@ -233,32 +232,21 @@ public void onRotated(Direction oldFacing, Direction newFacing) {
}
}

public boolean allowExtendedFacing() {
return getDefinition().isAllowExtendedFacing();
}

public boolean allowFlip() {
return getDefinition().isAllowFlip();
}

@Override
public boolean isFacingValid(Direction facing) {
return allowExtendedFacing() || super.isFacingValid(facing);
}

public Direction getUpwardsFacing() {
return this.allowExtendedFacing() ? this.getBlockState().getValue(IMachineBlock.UPWARDS_FACING_PROPERTY) :
Direction.NORTH;
}

public void setUpwardsFacing(@NotNull Direction upwardsFacing) {
if (!getDefinition().isAllowExtendedFacing()) return;
if (upwardsFacing == null || upwardsFacing == Direction.UP || upwardsFacing == Direction.DOWN) {
if (!getDefinition().isAllowExtendedFacing()) {
return;
}
if (upwardsFacing.getAxis() == Direction.Axis.Y) {
GTCEu.LOGGER.error("Tried to set upwards facing to invalid facing {}! Skipping", upwardsFacing);
return;
}
BlockState blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock metaMachineBlock &&
var blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock &&
blockState.getValue(IMachineBlock.UPWARDS_FACING_PROPERTY) != upwardsFacing) {
getLevel().setBlockAndUpdate(getPos(),
blockState.setValue(IMachineBlock.UPWARDS_FACING_PROPERTY, upwardsFacing));
Expand Down Expand Up @@ -292,17 +280,9 @@ protected InteractionResult onWrenchClick(Player playerIn, InteractionHand hand,

@Override
public void setFrontFacing(Direction facing) {
Direction oldFacing = getFrontFacing();

if (allowExtendedFacing()) {
Direction newUpwardsFacing = RelativeDirection.simulateAxisRotation(facing, oldFacing, getUpwardsFacing());
setUpwardsFacing(newUpwardsFacing);
}
super.setFrontFacing(facing);

if (getLevel() != null && !getLevel().isClientSide) {
notifyBlockUpdate();
markDirty();
checkPattern();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.utils.GTMath;

import net.minecraft.util.Mth;

import org.jetbrains.annotations.NotNull;

Expand All @@ -19,7 +20,7 @@ public interface ChanceBoostFunction {
int tierDiff = chanceTier - recipeTier;
if (tierDiff <= 0) return entry.chance; // equal or invalid tiers do not boost at all
if (recipeTier == GTValues.ULV) tierDiff--; // LV does not boost over ULV
return GTMath.clamp(entry.chance + (entry.tierChanceBoost * tierDiff), 0, entry.maxChance);
return Mth.clamp(entry.chance + (entry.tierChanceBoost * tierDiff), 0, entry.maxChance);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public class MachineBuilder<DEFINITION extends MachineDefinition> extends Builde
private VoxelShape shape = Shapes.block();
@Setter
private RotationState rotationState = RotationState.NON_Y_AXIS;
/**
* Whether this machine can be rotated or face upwards.
* todo: set to true by default if we manage to rotate the model accordingly
*/
@Setter
private boolean allowExtendedFacing = false;
@Setter
private boolean hasTESR;
@Setter
Expand Down Expand Up @@ -387,6 +393,7 @@ public DEFINITION register() {
definition.setEditableUI(editableUI);
}
definition.setAppearance(appearance);
definition.setAllowExtendedFacing(allowExtendedFacing);
definition.setRenderer(GTCEu.isClientSide() ? renderer.get() : IRenderer.EMPTY);
definition.setShape(shape);
definition.setDefaultPaintingColor(paintingColor);
Expand All @@ -398,6 +405,7 @@ public DEFINITION register() {

static class BlockBuilderWrapper {

@SuppressWarnings("removal")
public static <
DEFINITION extends MachineDefinition> BlockBuilder<Block, Registrate> makeBlockBuilder(MachineBuilder<DEFINITION> builder,
DEFINITION definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ public class MultiblockMachineBuilder extends MachineBuilder<MultiblockMachineDe
@Setter
private Function<MultiblockMachineDefinition, BlockPattern> pattern;
private final List<Function<MultiblockMachineDefinition, List<MultiblockShapeInfo>>> shapeInfos = new ArrayList<>();
/**
* Whether this multi can be rotated or face upwards.
*/
@Setter
private boolean allowExtendedFacing = true;
/**
* Set this to false only if your multiblock is set up such that it could have a wall-shared controller.
*/
Expand All @@ -98,6 +93,7 @@ protected MultiblockMachineBuilder(Registrate registrate, String name,
TriFunction<BlockEntityType<?>, BlockPos, BlockState, IMachineBlockEntity> blockEntityFactory) {
super(registrate, name, MultiblockMachineDefinition::createDefinition, metaMachine::apply, blockFactory,
itemFactory, blockEntityFactory);
allowExtendedFacing(true);
}

public static MultiblockMachineBuilder createMulti(Registrate registrate, String name,
Expand Down Expand Up @@ -383,6 +379,11 @@ public MultiblockMachineBuilder onBlockEntityRegister(NonNullConsumer<BlockEntit
return (MultiblockMachineBuilder) super.onBlockEntityRegister(onBlockEntityRegister);
}

@Override
public MultiblockMachineBuilder allowExtendedFacing(boolean allowExtendedFacing) {
return (MultiblockMachineBuilder) super.allowExtendedFacing(allowExtendedFacing);
}

@Override
public void generateLang(LangEventJS lang) {
super.generateLang(lang);
Expand All @@ -402,7 +403,6 @@ public MultiblockMachineDefinition register() {
definition.setPatternFactory(SupplierMemoizer.memoize(() -> pattern.apply(definition)));
definition.setShapes(() -> shapeInfos.stream().map(factory -> factory.apply(definition))
.flatMap(Collection::stream).toList());
definition.setAllowExtendedFacing(allowExtendedFacing);
definition.setAllowFlip(allowFlip);
if (!recoveryItems.isEmpty()) {
definition.setRecoveryItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -30,13 +29,15 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;

import java.util.*;
import java.util.function.Consumer;

import javax.annotation.ParametersAreNonnullByDefault;

import static com.gregtechceu.gtceu.utils.GTMatrixUtils.*;
import static com.gregtechceu.gtceu.utils.GTMatrixUtils.rotateMatrix;

/**
* @author KilaBash
* @date 2023/2/20
Expand Down Expand Up @@ -136,36 +137,12 @@ public List<BakedQuad> bakeQuads(@Nullable Direction side, Direction frontFacing
boolean isActive, boolean isWorkingEnabled) {
var quads = new ArrayList<BakedQuad>();

float degree = Mth.HALF_PI * (upwardsFacing == Direction.EAST ? 1 :
upwardsFacing == Direction.SOUTH ? 2 : upwardsFacing == Direction.WEST ? -1 : 0);

Matrix4f matrix = new Matrix4f();

if (frontFacing.getAxis() != Direction.Axis.Y) {
double rotationRad = Math.toRadians(frontFacing.toYRot());
Quaternionf worldUp = new Quaternionf().rotationAxis(Mth.PI - (float) rotationRad, 0, 1, 0);
matrix.rotate(worldUp);
} else {
matrix.rotate(Mth.HALF_PI, frontFacing.getStepY(), 0, 0);
if (upwardsFacing.getAxis() == Direction.Axis.Z) {
matrix.rotate(Mth.PI, 0, 0, upwardsFacing.getStepZ());
}
if (frontFacing.getAxisDirection() == Direction.AxisDirection.NEGATIVE) {
matrix.rotate(Mth.PI, 0, 0, 1);
}
}

Quaternionf rot = new Quaternionf().rotationAxis(degree, 0, 0,
frontFacing.getAxisDirection() == Direction.AxisDirection.NEGATIVE ? 1 : -1);

if (frontFacing.getAxisDirection() == Direction.AxisDirection.POSITIVE &&
frontFacing.getAxis() != Direction.Axis.Y) {
if (upwardsFacing.getAxis() != Direction.Axis.Z) {
matrix.rotate(Mth.PI, 0, 0, 1);
}
}

matrix.rotate(rot);
// rotate frontFacing to correct cardinal direction
var front = frontFacing.step();
rotateMatrix(matrix, Direction.NORTH.step(), frontFacing.step(), front);
// rotate upwards face to the correct orientation
rotateMatrix(matrix, upwardFacingAngle(upwardsFacing), front.x, front.y, front.z);

var rotation = new SimpleModelState(new Transformation(matrix));

Expand Down Expand Up @@ -207,7 +184,7 @@ public List<BakedQuad> bakeQuads(@Nullable Direction side, Direction frontFacing
@NotNull
@OnlyIn(Dist.CLIENT)
public TextureAtlasSprite getParticleTexture() {
for (WorkableOverlayModel.ActivePredicate predicate : sprites.values()) {
for (ActivePredicate predicate : sprites.values()) {
TextureAtlasSprite sprite = predicate.getSprite(false, false);
if (sprite != null) return sprite;
}
Expand Down
Loading