Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 345
src/client/java/dev/kodari/radialspherecontrol/RadialSphereControlClient.java
package dev.kodari.radialspherecontrol;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.util.Identifier;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public final class RadialSphereControlClient implements ClientModInitializer {
public static final String MOD_ID = "radialspherecontrol";
private static final DustParticleEffect RED_RING = new DustParticleEffect(0xFF0000, 2.0F);
private static final DustParticleEffect CYAN_RING = new DustParticleEffect(0x00D8FF, 2.0F);
private static final DustParticleEffect GOLD_RING = new DustParticleEffect(0xFFD000, 2.0F);
private static KeyBinding openMenuKey;
private static int activeSphere;
@Override
public void onInitializeClient() {
loadActiveSphere();
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));
openMenuKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.radialspherecontrol.open_menu",
InputUtil.Type.KEYSYM,
InputUtil.GLFW_KEY_X,
category
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (openMenuKey.wasPressed()) {
client.setScreen(new RadialMenuScreen());
}
if (client.player == null || client.world == null || activeSphere == 0) {
return;
}
renderParticleRing(client.world, client.player.getX(), client.player.getY(), client.player.getZ());
});
}
public static int getActiveSphere() {
return activeSphere;
}
public static void setActiveSphere(int sphere) {
if (sphere < 0 || sphere > 4) {
throw new IllegalArgumentException("active_sphere must be between 0 and 4");
}
activeSphere = sphere;
saveActiveSphere();
}
private static void renderParticleRing(ClientWorld world, double playerX, double playerY, double playerZ) {
DustParticleEffect effect = switch (activeSphere) {
case 1, 2 -> RED_RING;
case 3 -> CYAN_RING;
case 4 -> GOLD_RING;
default -> null;
};
if (effect == null) {
return;
}
for (int i = 0; i < 40; i++) {
double angle = Math.PI * 2.0 * i / 40.0;
double x = playerX + Math.cos(angle) * 15.0;
double z = playerZ + Math.sin(angle) * 15.0;
world.addParticleClient(effect, true, false, x, playerY + 0.1, z, 0.0, 0.0, 0.0);
}
}
private static Path statePath() {
return MinecraftClient.getInstance().runDirectory.toPath()
.resolve("config")
.resolve(MOD_ID)
.resolve("active_sphere.txt");
}
private static void loadActiveSphere() {
Path path = statePath();
if (!Files.exists(path)) {
activeSphere = 0;
return;
}
try {
int savedSphere = Integer.parseInt(Files.readString(path).trim());
activeSphere = savedSphere >= 0 && savedSphere <= 4 ? savedSphere : 0;
} catch (IOException | NumberFormatException ignored) {
activeSphere = 0;
}
}
private static void saveActiveSphere() {
Path path = statePath();
try {
Files.createDirectories(path.getParent());
Files.writeString(path, Integer.toString(activeSphere));
} catch (IOException ignored) {
}
}
}