Files
src
.gitignore
build.gradle
gradle.properties
settings.gradle
Code
23 887
src/client/java/com/robotcompanion/RobotCompanionClient.java
package com.robotcompanion;
import com.robotcompanion.client.render.RobotEntityRenderer;
import com.robotcompanion.entity.ModEntities;
import com.robotcompanion.entity.RobotEntity;
import com.robotcompanion.network.RobotControlPayload;
import com.robotcompanion.network.RobotStatusPayload;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
public class RobotCompanionClient implements ClientModInitializer {
private static KeyBinding controlKey;
private static RobotEntity controlledRobot;
private static boolean wasControlKeyPressed = false;
@Override
public void onInitializeClient() {
EntityRendererRegistry.register(ModEntities.ROBOT, RobotEntityRenderer::new);
controlKey = new KeyBinding(
"key.robotcompanion.control",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"category.robotcompanion"
);
ClientPlayNetworking.registerGlobalReceiver(RobotStatusPayload.ID, (payload, context) -> {
context.client().execute(() -> {
var entity = context.client().world.getEntityById(payload.robotId());
if (entity instanceof RobotEntity robot) {
robot.setEnergy(payload.energy());
robot.setControlled(payload.controlled());
if (payload.controlled()) {
controlledRobot = robot;
} else if (controlledRobot == robot) {
controlledRobot = null;
}
}
});
});
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (controlledRobot != null && controlledRobot.isAlive()) {
handleRobotControl(client);
}
boolean isControlKeyPressed = controlKey.isPressed();
if (isControlKeyPressed && !wasControlKeyPressed) {
}
wasControlKeyPressed = isControlKeyPressed;
});
}
private void handleRobotControl(MinecraftClient client) {
if (client.player == null) return;
float forward = 0;
float strafe = 0;
if (client.options.forwardKey.isPressed()) forward += 1;
if (client.options.backKey.isPressed()) forward -= 1;
if (client.options.leftKey.isPressed()) strafe += 1;
if (client.options.rightKey.isPressed()) strafe -= 1;
if (forward != 0 || strafe != 0) {
float yaw = client.player.getYaw();
ClientPlayNetworking.send(new RobotControlPayload(
controlledRobot.getId(),
forward,
strafe,
yaw
));
}
}
public static RobotEntity getControlledRobot() {
return controlledRobot;
}
}