Skip to content

Commit

Permalink
Fixed clientplayerentity not being in sync on some servers
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed May 28, 2022
1 parent e51e272 commit 94c62a7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 86 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.18.2
yarn_mappings=1.18.2+build.2
loader_version=0.13.3
yarn_mappings=1.18.2+build.3
loader_version=0.14.6

# Mod Properties
mod_version = 2.3
Expand All @@ -17,4 +17,4 @@ org.gradle.jvmargs=-Xmx1G
litematica_projectid=308892

# Dependencies
fabric_version=0.48.0+1.18.2
fabric_version=0.53.4+1.18.2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.aleksilassila.litematica.printer.mixin;

import com.mojang.authlib.GameProfile;
import fi.dy.masa.litematica.world.SchematicWorldHandler;
import me.aleksilassila.litematica.printer.LitematicaMixinMod;
import me.aleksilassila.litematica.printer.printer.Printer;
import me.aleksilassila.litematica.printer.printer.UpdateChecker;
Expand Down Expand Up @@ -29,16 +30,18 @@ public MixinClientPlayerEntity(ClientWorld world, GameProfile profile) {

@Inject(at = @At("TAIL"), method = "tick")
public void tick(CallbackInfo ci) {
// if (!(this.world.isPosLoaded(this.getBlockX(), this.getBlockZ())))
// return;

if (!didCheckForUpdates) {
didCheckForUpdates = true;

checkForUpdates();
}

if (Printer.getPrinter() == null) {
Printer.init(client);
return;
}

if (Printer.getPrinter() == null || !(LitematicaMixinMod.PRINT_MODE.getBooleanValue() || LitematicaMixinMod.PRINT.getKeybind().isPressed()))
if (!(LitematicaMixinMod.PRINT_MODE.getBooleanValue() || LitematicaMixinMod.PRINT.getKeybind().isPressed()))
return;

Printer.getPrinter().tick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public abstract ActionResult interactBlock(
public abstract ActionResult interactItem(PlayerEntity playerEntity_1,
World world_1, Hand hand_1);

@Inject(at = @At("HEAD"), method = "interactBlock")
public void interactBlock(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
System.out.println("Player interactBlock: pos: (" + hitResult.getBlockPos().toShortString() + "), side: " + hitResult.getSide().getName() + ", vector: " + hitResult.getPos().toString());
PlacementGuide.Action a = Printer.getPrinter().guide.getAction(hitResult.getBlockPos());
if (a == null) return;
for (Direction side : a.getSides().keySet()) {
System.out.println("Side: " + side + ", " + a.getSides().get(side).toString());
}
System.out.println("Valid: " + a.getValidSide(world, hitResult.getBlockPos()));
System.out.println("Look: " + a.getLookDirection());
}
// @Inject(at = @At("HEAD"), method = "interactBlock")
// public void interactBlock(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
// System.out.println("Player interactBlock: pos: (" + hitResult.getBlockPos().toShortString() + "), side: " + hitResult.getSide().getName() + ", vector: " + hitResult.getPos().toString());
// PlacementGuide.Action a = Printer.getPrinter().guide.getAction(hitResult.getBlockPos());
// if (a == null) return;
// for (Direction side : a.getSides().keySet()) {
// System.out.println("Side: " + side + ", " + a.getSides().get(side).toString());
// }
// System.out.println("Valid: " + a.getValidSide(world, hitResult.getBlockPos()));
// System.out.println("Look: " + a.getLookDirection());
// }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -25,27 +26,21 @@
public class PlacementGuide extends PrinterUtils {
@NotNull
protected final MinecraftClient client;
@NotNull
protected final ClientWorld world;
@NotNull
protected final WorldSchematic worldSchematic;

public PlacementGuide(@NotNull MinecraftClient client, @NotNull ClientWorld world, @NotNull WorldSchematic worldSchematic) {
public PlacementGuide(@NotNull MinecraftClient client) {
this.client = client;
this.world = world;
this.worldSchematic = worldSchematic;
}

public @Nullable Action getAction(BlockPos pos) {
public @Nullable Action getAction(World world, WorldSchematic worldSchematic, BlockPos pos) {
for (ClassHook hook : ClassHook.values()) {
for (Class<?> clazz : hook.classes) {
if (clazz != null && clazz.isInstance(worldSchematic.getBlockState(pos).getBlock())) {
return buildAction(pos, hook);
return buildAction(world, worldSchematic, pos, hook);
}
}
}

return buildAction(pos, ClassHook.DEFAULT);
return buildAction(world, worldSchematic, pos, ClassHook.DEFAULT);
}

// public static Placement getPlacement(BlockState requiredState, MinecraftClient client) {
Expand All @@ -54,7 +49,7 @@ public PlacementGuide(@NotNull MinecraftClient client, @NotNull ClientWorld worl
// }

@SuppressWarnings("EnhancedSwitchMigration")
private @Nullable Action buildAction(BlockPos pos, ClassHook requiredType) {
private @Nullable Action buildAction(World world, WorldSchematic worldSchematic, BlockPos pos, ClassHook requiredType) {
BlockState requiredState = worldSchematic.getBlockState(pos);
BlockState currentState = world.getBlockState(pos);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@ public class Printer extends PrinterUtils {
private static Printer INSTANCE;
@NotNull
private final MinecraftClient client;
@NotNull
private final ClientPlayerEntity pEntity;
@NotNull
private final ClientWorld world;
private WorldSchematic worldSchematic;
public final PlacementGuide guide;
public final Queue queue;

int tick = 0;

public static @Nullable Printer init(MinecraftClient client, ClientWorld world, WorldSchematic schematicWorld) {
public static void init(MinecraftClient client) {
if (client == null || client.player == null || client.world == null) {
return null;
return;
}

INSTANCE = new Printer(client);

return INSTANCE;
}

public static @Nullable Printer getPrinter() {
Expand All @@ -58,11 +52,8 @@ public class Printer extends PrinterUtils {

private Printer(MinecraftClient client) {
this.client = client;
this.pEntity = client.player;
this.world = client.world;
this.worldSchematic = SchematicWorldHandler.getSchematicWorld();

this.guide = new PlacementGuide(client, client.world, this.worldSchematic);
this.guide = new PlacementGuide(client);
this.queue = new Queue(this);

INSTANCE = this;
Expand All @@ -77,12 +68,21 @@ private Printer(MinecraftClient client) {
*/

public void tick() {
WorldSchematic worldSchematic = SchematicWorldHandler.getSchematicWorld();
ClientPlayerEntity player = client.player;
ClientWorld world = client.world;

if (worldSchematic == null || player == null || world == null) {
return;
}

int tickRate = LitematicaMixinMod.PRINT_INTERVAL.getIntegerValue();
// System.out.println(worldSchematic.getBlockState(pEntity.getBlockPos().north(1)).getBlock().getName() + ", " + world.getBlockState(pEntity.getBlockPos().north(1)).getBlock().getName());

tick = tick == 0x7fffffff ? 0 : tick + 1;
if (tick % tickRate != 0) {
// if (!queue.didSendLook) {
queue.sendQueue();
queue.sendQueue(player);
// }
return;
}
Expand All @@ -96,9 +96,9 @@ public void tick() {
for (int y = -range; y < range + 1; y++) {
for (int x = -range; x < range + 1; x++) {
for (int z = -range; z < range + 1; z++) {
BlockPos center = pEntity.getBlockPos().north(x).west(z).up(y);
BlockPos center = player.getBlockPos().north(x).west(z).up(y);
BlockState requiredState = worldSchematic.getBlockState(center);
PlacementGuide.Action action = guide.getAction(center);
PlacementGuide.Action action = guide.getAction(world, worldSchematic, center);

if (!DataManager.getRenderLayerRange().isPositionWithinRange(center)) continue;
if (action == null) continue;
Expand All @@ -107,7 +107,7 @@ public void tick() {
if (side == null) continue;

Item[] requiredItems = action.getRequiredItems(requiredState.getBlock());
if (playerHasAccessToItems(pEntity, requiredItems)) {
if (playerHasAccessToItems(player, requiredItems)) {

// Handle shift and chest placement
// Won't be required if clickAction
Expand Down Expand Up @@ -142,7 +142,7 @@ public void tick() {
}

Direction lookDir = action.getLookDirection();
sendPlacementPreparation(requiredItems, lookDir);
sendPlacementPreparation(player, requiredItems, lookDir);
action.queueAction(queue, center, side, useShift, lookDir != null);
return;
}
Expand All @@ -151,19 +151,19 @@ public void tick() {
}
}

private void sendPlacementPreparation(Item[] requiredItems, Direction lookDir) {
switchToItems(requiredItems);
sendLook(lookDir);
private void sendPlacementPreparation(ClientPlayerEntity player, Item[] requiredItems, Direction lookDir) {
switchToItems(player, requiredItems);
sendLook(player, lookDir);
}

private void switchToItems(Item[] items) {
private void switchToItems(ClientPlayerEntity player, Item[] items) {
if (items == null) return;

PlayerInventory inv = Implementation.getInventory(pEntity);
PlayerInventory inv = Implementation.getInventory(player);

for (Item item : items) {
if (inv.getMainHandStack().getItem() == item) return;
if (Implementation.getAbilities(pEntity).creativeMode) {
if (Implementation.getAbilities(player).creativeMode) {
InventoryUtils.setPickedItemToHand(new ItemStack(item), client);
client.interactionManager.clickCreativeStack(client.player.getStackInHand(Hand.MAIN_HAND), 36 + inv.selectedSlot);
return;
Expand All @@ -175,21 +175,21 @@ private void switchToItems(Item[] items) {
}

if (slot != -1) {
swapHandWithSlot(slot);
swapHandWithSlot(player, slot);
return;
}
}
}
}

private void swapHandWithSlot(int slot) {
ItemStack stack = Implementation.getInventory(pEntity).getStack(slot);
private void swapHandWithSlot(ClientPlayerEntity player, int slot) {
ItemStack stack = Implementation.getInventory(player).getStack(slot);
InventoryUtils.setPickedItemToHand(stack, client);
}

public void sendLook(Direction direction) {
public void sendLook(ClientPlayerEntity player, Direction direction) {
if (direction != null) {
Implementation.sendLookPacket(client.player, direction);
Implementation.sendLookPacket(player, direction);
}

queue.lookDir = direction;
Expand All @@ -205,11 +205,9 @@ public static class Queue {
public Direction lookDir = null;

final Printer printerInstance;
final ClientPlayerEntity pEntity;

public Queue(Printer printerInstance) {
this.printerInstance = printerInstance;
this.pEntity = printerInstance.pEntity;
}

public void queueClick(@NotNull BlockPos target, @NotNull Direction side, @NotNull Vec3d hitModifier) {
Expand All @@ -229,10 +227,10 @@ public void queueClick(@NotNull BlockPos target, @NotNull Direction side, @NotNu
this.shift = shift;
}

public void sendQueue() {
public void sendQueue(ClientPlayerEntity player) {
if (target == null || side == null || hitModifier == null) return;

boolean wasSneaking = pEntity.isSneaking();
boolean wasSneaking = player.isSneaking();

Direction direction = side.getAxis() == Direction.Axis.Y ?
((lookDir == null || !lookDir.getAxis().isHorizontal())
Expand All @@ -246,19 +244,19 @@ public void sendQueue() {
.add(hitModifier.multiply(0.5));

if (shift && !wasSneaking)
pEntity.networkHandler.sendPacket(new ClientCommandC2SPacket(pEntity, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
player.networkHandler.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
else if (!shift && wasSneaking)
pEntity.networkHandler.sendPacket(new ClientCommandC2SPacket(pEntity, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
player.networkHandler.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));

((IClientPlayerInteractionManager) printerInstance.client.interactionManager)
.rightClickBlock(target, side, hitVec);

System.out.println("Printed at " + (target.toString()) + ", " + side + ", modifier: " + hitVec);

if (shift && !wasSneaking)
pEntity.networkHandler.sendPacket(new ClientCommandC2SPacket(pEntity, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
player.networkHandler.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
else if (!shift && wasSneaking)
pEntity.networkHandler.sendPacket(new ClientCommandC2SPacket(pEntity, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
player.networkHandler.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));

clearQueue();
}
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/litematica-printer.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"MixinClientPlayerInteractionManager",
"ClientPlayNetworkHandlerMixin",
"PlayerMoveC2SPacketAccessor",
"WorldLoadListenerMixin",
"FlowerPotBlockAccessor"
],
"injectors": {
Expand Down

0 comments on commit 94c62a7

Please sign in to comment.