Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 301
src/main/java/com/gearkordy/GearkordyClient.java
package com.gearkordy;
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.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
public class GearkordyClient implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("gearkordy");
private static final KeyBinding OPEN_KEY = new KeyBinding(
"key.gearkordy.open",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_O,
"category.gearkordy"
);
private static final List<String> CONTACTS = new ArrayList<>();
private static boolean contactsLoaded;
@Override
public void onInitializeClient() {
KeyBindingHelper.registerKeyBinding(OPEN_KEY);
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.player != null && client.world != null && !contactsLoaded) {
loadContacts(client.runDirectory.toPath());
}
while (OPEN_KEY.wasPressed()) {
if (client.player != null && client.world != null) {
client.setScreen(new GearkordyScreen());
}
}
});
}
private static Path contactsFile(Path runDirectory) {
return runDirectory.resolve("config").resolve("gearkordy.txt");
}
private static void loadContacts(Path runDirectory) {
contactsLoaded = true;
Path file = contactsFile(runDirectory);
if (!Files.exists(file)) {
return;
}
try {
LinkedHashSet<String> loaded = new LinkedHashSet<>();
for (String line : Files.readAllLines(file)) {
String name = line.trim();
if (!name.isEmpty() && name.length() <= 16 && loaded.size() < 8) {
loaded.add(name);
}
}
CONTACTS.addAll(loaded);
} catch (IOException exception) {
LOGGER.error("Nie udało się wczytać listy graczy Gearkordy.", exception);
}
}
private static void saveContacts(Path runDirectory) {
Path file = contactsFile(runDirectory);
try {
Files.createDirectories(file.getParent());
Files.write(file, CONTACTS);
} catch (IOException exception) {
LOGGER.error("Nie udało się zapisać listy graczy Gearkordy.", exception);
}
}
private static final class GearkordyScreen extends Screen {
private static final int MAX_CONTACTS = 8;
private static final int PANEL_WIDTH = 360;
private static final int PANEL_HEIGHT = 260;
private String input = "";
private String status = "Wpisz nick i wybierz Dodaj.";
private boolean inputFocused;
private GearkordyScreen() {
super(Text.literal("Gearkordy"));
}
private int left() {
return width / 2 - PANEL_WIDTH / 2;
}
private int top() {
return height / 2 - PANEL_HEIGHT / 2;
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context, mouseX, mouseY, delta);
int x = left();
int y = top();
context.fill(x, y, x + PANEL_WIDTH, y + PANEL_HEIGHT, 0xE920252D);
context.fill(x, y, x + PANEL_WIDTH, y + 2, 0xFF61C7FF);
context.drawCenteredTextWithShadow(textRenderer, "Gearkordy", width / 2, y + 12, 0xFFFFFFFF);
context.drawCenteredTextWithShadow(textRenderer, "Koordynaty zapisanych graczy", width / 2, y + 29, 0xFFB9C4D0);
int inputY = y + 51;
context.fill(x + 14, inputY, x + 252, inputY + 20, inputFocused ? 0xFF354351 : 0xFF29313B);
context.drawTextWithShadow(textRenderer, input + (inputFocused ? "_" : ""), x + 20, inputY + 6, 0xFFFFFFFF);
context.fill(x + 262, inputY, x + 346, inputY + 20, 0xFF245B78);
context.drawCenteredTextWithShadow(textRenderer, "Dodaj", x + 304, inputY + 6, 0xFFFFFFFF);
context.drawTextWithShadow(textRenderer, "Gracz", x + 16, y + 80, 0xFF9EABB8);
context.drawTextWithShadow(textRenderer, "X Y Z", x + 148, y + 80, 0xFF9EABB8);
for (int index = 0; index < CONTACTS.size(); index++) {
int rowY = y + 98 + index * 18;
String name = CONTACTS.get(index);
context.drawTextWithShadow(textRenderer, name, x + 16, rowY, 0xFFFFFFFF);
context.drawTextWithShadow(textRenderer, coordinatesFor(name), x + 148, rowY, 0xFF8FE0A0);
context.drawTextWithShadow(textRenderer, "X", x + 328, rowY, 0xFFFF8585);
}
context.drawCenteredTextWithShadow(textRenderer, status, width / 2, y + 238, 0xFFB9C4D0);
context.drawCenteredTextWithShadow(textRenderer, "Esc: zamknij | Nicki zapisują się w config/gearkordy.txt", width / 2, y + 250, 0xFF8995A2);
super.render(context, mouseX, mouseY, delta);
}
private String coordinatesFor(String name) {
if (client == null || client.world == null) {
return "brak świata";
}
for (var player : client.world.getPlayers()) {
if (player.getName().getString().equalsIgnoreCase(name)) {
return player.getBlockX() + " " + player.getBlockY() + " " + player.getBlockZ();
}
}
return "nieśledzony";
}
private void addContact() {
String name = input.trim();
if (name.isEmpty()) {
status = "Wpisz nick gracza.";
} else if (CONTACTS.stream().anyMatch(existing -> existing.equalsIgnoreCase(name))) {
status = "Ten nick jest już na liście.";
} else if (CONTACTS.size() >= MAX_CONTACTS) {
status = "Lista jest pełna (maksymalnie 8 graczy).";
} else {
CONTACTS.add(name);
saveContacts(client.runDirectory.toPath());
input = "";
status = "Dodano gracza: " + name;
}
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
int x = left();
int y = top();
int inputY = y + 51;
if (mouseX >= x + 14 && mouseX <= x + 252 && mouseY >= inputY && mouseY <= inputY + 20) {
inputFocused = true;
return true;
}
if (mouseX >= x + 262 && mouseX <= x + 346 && mouseY >= inputY && mouseY <= inputY + 20) {
inputFocused = false;
addContact();
return true;
}
inputFocused = false;
for (int index = 0; index < CONTACTS.size(); index++) {
int rowY = y + 98 + index * 18;
if (mouseX >= x + 320 && mouseX <= x + 346 && mouseY >= rowY - 2 && mouseY <= rowY + 12) {
CONTACTS.remove(index);
saveContacts(client.runDirectory.toPath());
status = "Usunięto gracza z listy.";
return true;
}
}
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public boolean charTyped(char chr, int modifiers) {
if (inputFocused && !Character.isISOControl(chr) && input.length() < 16) {
input += chr;
return true;
}
return super.charTyped(chr, modifiers);
}