Skip to content

Commit

Permalink
Fixed placement of sings, skulls and banners
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed Dec 6, 2022
1 parent 33e40b0 commit dc8a26b
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.aleksilassila.litematica.printer.v1_19;

import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import me.aleksilassila.litematica.printer.v1_19.actions.PrepareAction;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.math.Direction;
Expand All @@ -13,8 +14,8 @@ public class ActionHandler {
private final MinecraftClient client;
private final ClientPlayerEntity player;

private final Queue<AbstractAction> actionQueue = new LinkedList<>();
public Direction lockedLookDirection = null;
private final Queue<Action> actionQueue = new LinkedList<>();
public PrepareAction lookAction = null;

public ActionHandler(MinecraftClient client, ClientPlayerEntity player) {
this.client = client;
Expand All @@ -31,26 +32,27 @@ public void onGameTick() {
return;
}

AbstractAction nextAction = actionQueue.poll();
Action nextAction = actionQueue.poll();
if (nextAction != null) {
if (LitematicaMixinMod.DEBUG) System.out.println("Sending action " + nextAction);
nextAction.send(client, player);
} else {
lockedLookDirection = null;
lookAction = null;
}
}

public boolean acceptsActions() {
return actionQueue.isEmpty();
}

public void addActions(AbstractAction... actions) {
public void addActions(Action... actions) {
if (!acceptsActions()) return;

for (AbstractAction action : actions) {
if (action.lockedLookDirection() != null)
lockedLookDirection = action.lockedLookDirection();
for (Action action : actions) {
if (action instanceof PrepareAction)
lookAction = (PrepareAction) action;
}

actionQueue.addAll(List.of(actions));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class LitematicaMixinMod implements ModInitializer {

public static Printer printer;
public static boolean DEBUG = false;
public static boolean DEBUG = true;
// Config settings
public static final ConfigInteger PRINT_INTERVAL = new ConfigInteger("printInterval", 12, 1, 40, "Printing interval. Lower values mean faster printing speed.\nIf the printer creates \"ghost blocks\" or blocks are facing the wrong way, raise this value.");
public static final ConfigDouble PRINTING_RANGE = new ConfigDouble("printingRange", 2, 1, 5, "Printing block place range\nLower values are recommended for servers.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fi.dy.masa.litematica.data.DataManager;
import fi.dy.masa.litematica.world.SchematicWorldHandler;
import fi.dy.masa.litematica.world.WorldSchematic;
import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import me.aleksilassila.litematica.printer.v1_19.guides.Guide;
import me.aleksilassila.litematica.printer.v1_19.guides.Guides;
import net.minecraft.client.MinecraftClient;
Expand All @@ -22,20 +22,20 @@ public class Printer {
@NotNull
public final ClientPlayerEntity player;

public final ActionHandler packetHandler;
public final ActionHandler actionHandler;

private final Guides interactionGuides = new Guides();

public Printer(@NotNull MinecraftClient client, @NotNull ClientPlayerEntity player) {
this.player = player;

this.packetHandler = new ActionHandler(client, player);
this.actionHandler = new ActionHandler(client, player);
}

public boolean onGameTick() {
WorldSchematic worldSchematic = SchematicWorldHandler.getSchematicWorld();

if (!packetHandler.acceptsActions()) return false;
if (!actionHandler.acceptsActions()) return false;

if (worldSchematic == null) return false;

Expand All @@ -59,8 +59,8 @@ public boolean onGameTick() {
for (Guide guide : guides) {
if (guide.canExecute(player)) {
System.out.println("Executing " + guide + " for " + state);
List<AbstractAction> actions = guide.execute(player);
packetHandler.addActions(actions.toArray(AbstractAction[]::new));
List<Action> actions = guide.execute(player);
actionHandler.addActions(actions.toArray(Action[]::new));
return true;
}
if (guide.skipOtherGuides()) continue findBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.math.Direction;

public abstract class AbstractAction {
public Direction lockedLookDirection() {
return null;
}

public abstract class Action {
abstract public void send(MinecraftClient client, ClientPlayerEntity player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;

abstract public class InteractAction extends AbstractAction {
abstract public class InteractAction extends Action {
public final PrinterPlacementContext context;

public InteractAction(PrinterPlacementContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.minecraft.util.Hand;
import net.minecraft.util.math.Direction;

public class PrepareAction extends AbstractAction {
public class PrepareAction extends Action {
// public final Direction lookDirection;
// public final boolean requireSneaking;
// public final Item item;
Expand All @@ -27,13 +27,38 @@ public class PrepareAction extends AbstractAction {

public final PrinterPlacementContext context;

public boolean modifyYaw = true;
public boolean modifyPitch = true;
public float yaw = 0;
public float pitch = 0;

public PrepareAction(PrinterPlacementContext context) {
this.context = context;

Direction lookDirection = context.lookDirection;

if (lookDirection != null && lookDirection.getAxis().isHorizontal()) {
this.yaw = lookDirection.asRotation();
} else {
this.modifyYaw = false;
}

if (lookDirection == Direction.UP) {
this.pitch = -90;
} else if (lookDirection == Direction.DOWN) {
this.pitch = 90;
} else if (lookDirection != null) {
this.pitch = 0;
} else {
this.modifyPitch = false;
}
}

@Override
public Direction lockedLookDirection() {
return context.lookDirection;
public PrepareAction(PrinterPlacementContext context, float yaw, float pitch) {
this.context = context;

this.yaw = yaw;
this.pitch = pitch;
}

@Override
Expand All @@ -57,16 +82,9 @@ public void send(MinecraftClient client, ClientPlayerEntity player) {
}
}

if (context.lookDirection != null) {
// Implementation.sendLookPacket(player, context.lookDirection);
float yaw = player.getYaw();
float pitch = player.getPitch();

if (context.lookDirection.getAxis().isHorizontal()) {
yaw = context.lookDirection.asRotation();
} else {
pitch = context.lookDirection == Direction.UP ? -90 : 90;
}
if (modifyPitch || modifyYaw) {
float yaw = modifyYaw ? this.yaw : player.getYaw();
float pitch = modifyPitch ? this.pitch : player.getPitch();

PlayerMoveC2SPacket packet = new PlayerMoveC2SPacket.Full(player.getX(), player.getY(), player.getZ(), yaw, pitch, player.isOnGround());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;

public class ReleaseShiftAction extends AbstractAction {
public class ReleaseShiftAction extends Action {
@Override
public void send(MinecraftClient client, ClientPlayerEntity player) {
player.input.sneaking = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.aleksilassila.litematica.printer.v1_19.guides;

import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import net.minecraft.block.BlockState;
import net.minecraft.block.CoralBlock;
import net.minecraft.client.network.ClientPlayerEntity;
Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean canExecute(ClientPlayerEntity player) {
return !statesEqual(targetState, currentState);
}

abstract public List<AbstractAction> execute(ClientPlayerEntity player);
abstract public List<Action> execute(ClientPlayerEntity player);

abstract protected @NotNull List<ItemStack> getRequiredItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ protected static void registerGuide(Class<? extends Guide> guideClass, Class<? e
}

static {
registerGuide(SkipGuide.class, AbstractSignBlock.class, SkullBlock.class, BannerBlock.class);
// registerGuide(SkipGuide.class, AbstractSignBlock.class, SkullBlock.class, BannerBlock.class);

registerGuide(RotatingBlockGuide.class, AbstractSkullBlock.class, AbstractSignBlock.class, AbstractBannerBlock.class);
registerGuide(SlabGuide.class, SlabBlock.class);
registerGuide(WallTorchGuide.class, WallTorchBlock.class, WallRedstoneTorchBlock.class);
registerGuide(FarmlandGuide.class, FarmlandBlock.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.aleksilassila.litematica.printer.v1_19.guides;

import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,7 +26,7 @@ public boolean canExecute(ClientPlayerEntity player) {
}

@Override
public List<AbstractAction> execute(ClientPlayerEntity player) {
public List<Action> execute(ClientPlayerEntity player) {
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import me.aleksilassila.litematica.printer.v1_19.PrinterPlacementContext;
import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import me.aleksilassila.litematica.printer.v1_19.actions.PrepareAction;
import me.aleksilassila.litematica.printer.v1_19.actions.ReleaseShiftAction;
import me.aleksilassila.litematica.printer.v1_19.guides.Guide;
Expand All @@ -27,12 +27,12 @@ public InteractionGuide(SchematicBlockState state) {
}

@Override
public List<AbstractAction> execute(ClientPlayerEntity player) {
public List<Action> execute(ClientPlayerEntity player) {
BlockHitResult hitResult = new BlockHitResult(Vec3d.ofCenter(state.blockPos), Direction.UP, state.blockPos, false);
ItemStack itemStack = getRequiredItem(player).orElse(ItemStack.EMPTY);
PrinterPlacementContext ctx = new PrinterPlacementContext(player, hitResult, itemStack, getSlotWithItem(player, itemStack));

List<AbstractAction> actions = new ArrayList<>();
List<Action> actions = new ArrayList<>();
actions.add(new ReleaseShiftAction());
actions.add(new PrepareAction(ctx));
actions.add(new InteractActionImpl(ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import me.aleksilassila.litematica.printer.v1_19.LitematicaMixinMod;
import me.aleksilassila.litematica.printer.v1_19.PrinterPlacementContext;
import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import me.aleksilassila.litematica.printer.v1_19.actions.AbstractAction;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import me.aleksilassila.litematica.printer.v1_19.actions.PrepareAction;
import me.aleksilassila.litematica.printer.v1_19.actions.ReleaseShiftAction;
import me.aleksilassila.litematica.printer.v1_19.guides.Guide;
Expand Down Expand Up @@ -91,12 +91,12 @@ && getProperty(state.currentState, FluidBlock.LEVEL).orElse(1) == 0)
}

@Override
public List<AbstractAction> execute(ClientPlayerEntity player) {
public List<Action> execute(ClientPlayerEntity player) {
PrinterPlacementContext ctx = getPlacementContext(player);

if (ctx == null) return null;

List<AbstractAction> actions = new ArrayList<>();
List<Action> actions = new ArrayList<>();
actions.add(new PrepareAction(ctx));
actions.add(new InteractActionImpl(ctx));
if (ctx.shouldSneak) actions.add(new ReleaseShiftAction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.aleksilassila.litematica.printer.v1_19.guides.placement;

import me.aleksilassila.litematica.printer.v1_19.PrinterPlacementContext;
import me.aleksilassila.litematica.printer.v1_19.SchematicBlockState;
import me.aleksilassila.litematica.printer.v1_19.actions.Action;
import me.aleksilassila.litematica.printer.v1_19.actions.PrepareAction;
import net.minecraft.block.*;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.Direction;

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

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

@Override
protected List<Direction> getPossibleSides() {
Block block = state.targetState.getBlock();
if (block instanceof WallSkullBlock || block instanceof WallSignBlock || block instanceof WallBannerBlock) {
Optional<Direction> side = getProperty(state.targetState, Properties.HORIZONTAL_FACING).map(Direction::getOpposite);
return side.map(Collections::singletonList).orElseGet(Collections::emptyList);
}

return Collections.singletonList(Direction.DOWN);
}

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

@Override
public List<Action> execute(ClientPlayerEntity player) {
PrinterPlacementContext ctx = getPlacementContext(player);

if (ctx == null) return null;

int rotation = getProperty(state.targetState, Properties.ROTATION).orElse(0);
if (targetState.getBlock() instanceof BannerBlock || targetState.getBlock() instanceof SignBlock) {
rotation = (rotation + 8) % 16;
}

int distTo0 = rotation > 8 ? 16 - rotation : rotation;
float yaw = Math.round(distTo0 / 8f * 180f * (rotation > 8 ? -1 : 1));

List<Action> actions = super.execute(player);
actions.set(0, new PrepareAction(ctx, yaw, 0));

return actions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void tick(CallbackInfo ci) {
if (didFindPlacement) {
didFindPlacement = LitematicaMixinMod.printer.onGameTick();
}
LitematicaMixinMod.printer.packetHandler.onGameTick();
LitematicaMixinMod.printer.actionHandler.onGameTick();
}
}

Expand Down
Loading

0 comments on commit dc8a26b

Please sign in to comment.