Skip to content

Commit

Permalink
Fixed bug with torch placements & added partially working rail guide
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed Dec 6, 2022
1 parent dc8a26b commit ad67ee4
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected static void registerGuide(Class<? extends Guide> guideClass, Class<? e

registerGuide(RotatingBlockGuide.class, AbstractSkullBlock.class, AbstractSignBlock.class, AbstractBannerBlock.class);
registerGuide(SlabGuide.class, SlabBlock.class);
registerGuide(WallTorchGuide.class, WallTorchBlock.class, WallRedstoneTorchBlock.class);
registerGuide(TorchGuide.class, TorchBlock.class);
registerGuide(FarmlandGuide.class, FarmlandBlock.class);
registerGuide(TillingGuide.class, FarmlandBlock.class);
registerGuide(RailGuesserGuide.class, RailBlock.class);
registerGuide(RailGuesserGuide.class, AbstractRailBlock.class);
registerGuide(ChestGuide.class, ChestBlock.class);
registerGuide(FlowerPotGuide.class, FlowerPotBlock.class);
registerGuide(FlowerPotFillGuide.class, FlowerPotBlock.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package me.aleksilassila.litematica.printer.v1_19.guides.interaction;

import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import net.minecraft.block.BlockState;
import net.minecraft.block.ComparatorBlock;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.property.Properties;
import net.minecraft.state.property.Property;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class CycleStateGuide extends InteractionGuide {
private static final Property<?>[] propertiesToIgnore = new Property[] {
Properties.POWERED
};

public CycleStateGuide(SchematicBlockState state) {
super(state);
}
Expand All @@ -24,4 +32,9 @@ public boolean canExecute(ClientPlayerEntity player) {
protected @NotNull List<ItemStack> getRequiredItems() {
return Collections.singletonList(ItemStack.EMPTY);
}

@Override
protected boolean statesEqual(BlockState state1, BlockState state2) {
return statesEqualIgnoreProperties(state1, state2, propertiesToIgnore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public List<Action> execute(ClientPlayerEntity player) {
}

protected static boolean canBeClicked(World world, BlockPos pos) {
return getOutlineShape(world, pos) != VoxelShapes.empty();
return getOutlineShape(world, pos) != VoxelShapes.empty() && !(world.getBlockState(pos).getBlock() instanceof AbstractSignBlock); // FIXME signs
}

private static VoxelShape getOutlineShape(World world, BlockPos pos) {
Expand Down Expand Up @@ -148,6 +148,6 @@ private boolean canPlaceInWater(BlockState blockState) {
DropperBlock.class, DispenserBlock.class, ShulkerBoxBlock.class, LecternBlock.class,
FlowerPotBlock.class, BarrelBlock.class, BellBlock.class, SmithingTableBlock.class,
LoomBlock.class, CartographyTableBlock.class, GrindstoneBlock.class,
StonecutterBlock.class
StonecutterBlock.class, AbstractSignBlock.class,
};
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package me.aleksilassila.litematica.printer.v1_19.guides.placement;

import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.enums.RailShape;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.Direction;

import java.util.Arrays;
import java.util.*;

public class RailGuesserGuide extends PropertySpecificGuesserGuide {
public class RailGuesserGuide extends GuesserGuide {
static final RailShape[] STRAIGHT_RAIL_SHAPES = new RailShape[]{
RailShape.NORTH_SOUTH,
RailShape.EAST_WEST
Expand All @@ -18,37 +21,101 @@ public RailGuesserGuide(SchematicBlockState state) {
super(state);
}

@Override
public boolean skipOtherGuides() {
return true;
}

@Override
public boolean canExecute(ClientPlayerEntity player) {
// PrinterPlacementContext ctx = getPlacementContext(player);
// BlockState result = targetState.getBlock().getPlacementState(ctx);
//
// if (result != null) {
// RailShape shape = result.get(Properties.RAIL_SHAPE);
//
// if (Arrays.asList(STRAIGHT_RAIL_SHAPES).contains(shape)) {
// Direction direction1 = shape == RailShape.NORTH_SOUTH ? Direction.NORTH : Direction.EAST;
// Direction direction2 = shape == RailShape.NORTH_SOUTH ? Direction.SOUTH : Direction.WEST;
protected boolean statesEqual(BlockState resultState, BlockState targetState) {
if (!wouldConnectCorrectly()) return false;
// if (wouldBlockAnotherConnection()) return false;

if (getRailShape(resultState).isPresent()) {
if (Arrays.stream(STRAIGHT_RAIL_SHAPES).anyMatch(shape -> shape == getRailShape(resultState).orElse(null))) {
return super.statesEqualIgnoreProperties(resultState, targetState, Properties.RAIL_SHAPE, Properties.STRAIGHT_RAIL_SHAPE, Properties.POWERED);
}
}

return super.statesEqual(resultState, targetState);
}

private boolean wouldConnectCorrectly() {
RailShape targetShape = getRailShape(state.targetState).orElse(null);
if (targetShape == null) return false;

List<Direction> allowedConnections = getRailDirections(targetShape);

List<Direction> possibleConnections = new ArrayList<>();
for (Direction d : Direction.values()) {
if (d.getAxis().isVertical()) continue;
SchematicBlockState neighbor = state.offset(d);

if (hasFreeConnections(neighbor)) {
possibleConnections.add(d);
}
}

if (possibleConnections.size() > 2) return false;

return allowedConnections.containsAll(possibleConnections);
}

// private boolean wouldBlockAnotherConnection() {
// List<Direction> possibleConnections = new ArrayList<>();
//
// for (Direction d : Direction.values()) {
// if (d.getAxis().isVertical()) continue;
// SchematicBlockState neighbor = state.offset(d);
//
// if (couldConnectWrongly(neighbor)) {
// possibleConnections.add(d);
// }
//
//
// return Arrays.asList(STRAIGHT_RAIL_SHAPES).contains(shape);
// }
//
// return possibleConnections.size() > 1;
// }

return super.canExecute(player);
}
private boolean hasFreeConnections(SchematicBlockState state) {
List<Direction> possibleConnections = getRailDirections(state);
if (possibleConnections.isEmpty()) return false;

@Override
protected boolean statesEqual(BlockState resultState, BlockState targetState) {
if (resultState.contains(Properties.RAIL_SHAPE)) {
if (Arrays.stream(STRAIGHT_RAIL_SHAPES).anyMatch(shape -> shape == resultState.get(Properties.RAIL_SHAPE))) {
return super.statesEqualIgnoreProperties(resultState, targetState, Properties.RAIL_SHAPE);
for (Direction d : possibleConnections) {
SchematicBlockState neighbor = state.offset(d);
if (neighbor.currentState.getBlock() != neighbor.currentState.getBlock()) {
return false;
}
}

return super.statesEqual(resultState, targetState);
return possibleConnections.stream().anyMatch(possibleDirection -> {
SchematicBlockState neighbor = state.offset(possibleDirection);
return !getRailDirections(neighbor).contains(possibleDirection.getOpposite());
});
}

private List<Direction> getRailDirections(SchematicBlockState state) {
RailShape shape = getRailShape(state.currentState).orElse(null);
if (shape == null) return new ArrayList<>();

return getRailDirections(shape);
}

private List<Direction> getRailDirections(RailShape railShape) {
String name = railShape.getName();

if (railShape.isAscending()) {
Direction d = Direction.valueOf(name.replace("ascending_", "").toUpperCase());
return Arrays.asList(d, d.getOpposite());
} else {
Direction d1 = Direction.valueOf(name.split("_")[0].toUpperCase());
Direction d2 = Direction.valueOf(name.split("_")[1].toUpperCase());
return Arrays.asList(d1, d2);
}
}

Optional<RailShape> getRailShape(BlockState state) {
Optional<RailShape> shape = getProperty(state, Properties.RAIL_SHAPE);
if (shape.isEmpty()) return getProperty(state, Properties.STRAIGHT_RAIL_SHAPE);
return shape;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.aleksilassila.litematica.printer.v1_19.guides.placement;

import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.util.math.Direction;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class TorchGuide extends BlockPlacementGuide {
public TorchGuide(SchematicBlockState state) {
super(state);
}

@Override
protected List<Direction> getPossibleSides() {
Optional<Direction> facing = getProperty(targetState, HorizontalFacingBlock.FACING);

return facing
.map(direction -> Collections.singletonList(direction.getOpposite()))
.orElseGet(() -> Collections.singletonList(Direction.DOWN));
}
}

This file was deleted.

0 comments on commit ad67ee4

Please sign in to comment.