Files
src
build.gradle
gradle.properties
settings.gradle
Code
15 873
src/main/java/com/kodari/mirrorblocks/MirrorBlocksClient.java
package com.kodari.mirrorblocks;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.glfw.GLFW;
public class MirrorBlocksClient implements ClientModInitializer {
private static MirrorManager mirrorManager;
private KeyBinding toggleWand;
public static MirrorManager getMirrorManager() {
return mirrorManager;
}
@Override
public void onInitializeClient() {
mirrorManager = new MirrorManager();
mirrorManager.load();
toggleWand = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mirrorblocks.toggle_wand", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_M, "category.mirrorblocks"));
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
MirrorCommand.register(dispatcher));
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
MinecraftClient client = MinecraftClient.getInstance();
SelectionSession selection = mirrorManager.getSelection();
if (!selection.isWandEnabled() || !isWand(player.getStackInHand(hand))) {
return ActionResult.PASS;
}
setWandPoint(selection, pos, 1, client);
return ActionResult.FAIL;
});
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
MinecraftClient client = MinecraftClient.getInstance();
SelectionSession selection = mirrorManager.getSelection();
if (!selection.isWandEnabled() || !isWand(player.getStackInHand(hand))) {
return ActionResult.PASS;
}
setWandPoint(selection, hitResult.getBlockPos(), 2, client);
return ActionResult.FAIL;
});
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleWand.wasPressed()) {
SelectionSession selection = mirrorManager.getSelection();
selection.setWandEnabled(!selection.isWandEnabled());
message((selection.isWandEnabled() ? "Selection wand enabled; hold a blaze rod." : "Selection wand disabled."),
selection.isWandEnabled() ? Formatting.GREEN : Formatting.YELLOW);
}
});
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> mirrorManager.clearSelection());
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> mirrorManager.save());
}
private static void setWandPoint(SelectionSession selection, BlockPos pos, int point, MinecraftClient client) {
if (client.world == null) {
return;
}
selection.setPoint(selection.getActiveSide(), point,
client.world.getRegistryKey().getValue().toString(), pos);
message(selection.getActiveSide().name() + " position " + point + " set at "
+ pos.getX() + ", " + pos.getY() + ", " + pos.getZ(), Formatting.GREEN);
}
public static boolean isWand(ItemStack stack) {
return !stack.isEmpty() && stack.isOf(Items.BLAZE_ROD);
}
public static void handlePlacedBlock(net.minecraft.server.network.ServerPlayerEntity player, Hand hand,
BlockHitResult hitResult, net.minecraft.block.BlockState before,
ItemStack originalStack) {
MinecraftClient client = MinecraftClient.getInstance();
if (!client.isInSingleplayer() || mirrorManager == null || originalStack.isEmpty()
|| !(originalStack.getItem() instanceof net.minecraft.item.BlockItem blockItem)
|| player.isCreative()) {
return;
}
net.minecraft.server.world.ServerWorld world = player.getServerWorld();
BlockPos clicked = hitResult.getBlockPos();
BlockPos adjacent = clicked.offset(hitResult.getSide());
BlockPos placed = before.isReplaceable() ? clicked : adjacent;
net.minecraft.block.BlockState state = world.getBlockState(placed);
if (!state.isOf(blockItem.getBlock())) {
return;
}
mirrorManager.mirrorPlacedBlock(world, placed, state, originalStack, player);
}
public static void handleBrokenBlock(net.minecraft.server.network.ServerPlayerEntity player, BlockPos pos) {
MinecraftClient client = MinecraftClient.getInstance();
if (!client.isInSingleplayer() || mirrorManager == null) {
return;
}
mirrorManager.clearMirroredBlock(player.getServerWorld(), pos);
}
private static void message(String text, Formatting formatting) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player != null) {
client.player.sendMessage(Text.literal(text).formatted(formatting), true);
}
}
}