Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 319
src/client/java/com/fhztiers/FhzTiersClient.java
package com.fhztiers;
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.gui.Click;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
public final class FhzTiersClient implements ClientModInitializer {
private static final String MOD_ID = "fhztiers";
private static final List<String> TIERS = List.of("HT1", "HT2", "LT1", "LT2", "LT3", "LT4");
private static KeyBinding openPanel;
private static String selectedTier;
@Override
public void onInitializeClient() {
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));
openPanel = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.fhztiers.open", GLFW.GLFW_KEY_RIGHT_SHIFT, category)
);
selectedTier = loadSelection();
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (openPanel.wasPressed()) {
client.setScreen(new TierScreen());
}
});
}
public static String getSelectedTier() {
return selectedTier;
}
private static Path settingsPath() {
return MinecraftClient.getInstance().runDirectory.toPath()
.resolve("config")
.resolve("fhztiers")
.resolve("Fhz.properties");
}
private static String loadSelection() {
Properties properties = new Properties();
Path path = settingsPath();
if (!Files.exists(path)) {
return TIERS.get(0);
}
try (InputStream input = Files.newInputStream(path)) {
properties.load(input);
} catch (IOException ignored) {
return TIERS.get(0);
}
String tier = properties.getProperty("tier", TIERS.get(0));
return TIERS.contains(tier) ? tier : TIERS.get(0);
}
private static void saveSelection() {
Path path = settingsPath();
Properties properties = new Properties();
properties.setProperty("player", "Fhz");
properties.setProperty("tier", selectedTier);
try {
Files.createDirectories(path.getParent());
try (OutputStream output = Files.newOutputStream(path)) {
properties.store(output, "Fhz Tiers local selection");
}
} catch (IOException ignored) {
}
}
private static final class TierScreen extends Screen {
private static final int PANEL_WIDTH = 300;
private static final int PANEL_HEIGHT = 250;
private static final int BUTTON_WIDTH = 122;
private static final int BUTTON_HEIGHT = 32;
private static final int BUTTON_GAP_X = 10;
private static final int BUTTON_GAP_Y = 9;
private TierScreen() {
super(Text.literal("Fhz Tiers"));
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float deltaTicks) {
super.render(context, mouseX, mouseY, deltaTicks);
int panelX = (this.width - PANEL_WIDTH) / 2;
int panelY = (this.height - PANEL_HEIGHT) / 2;
context.fill(panelX - 2, panelY - 2, panelX + PANEL_WIDTH + 2, panelY + PANEL_HEIGHT + 2, 0xFFFF3030);
context.fill(panelX, panelY, panelX + PANEL_WIDTH, panelY + PANEL_HEIGHT, 0xFF101010);
context.fill(panelX + 1, panelY + 1, panelX + PANEL_WIDTH - 1, panelY + 3, 0xFFB51F2A);
context.drawText(this.textRenderer, Text.literal("FHZ TIERS"), panelX + 18, panelY + 17, 0xFFFF4545, true);
context.drawText(this.textRenderer, Text.literal("PERFIL LOCAL: Fhz"), panelX + 18, panelY + 39, 0xFFB8B8B8, true);
context.drawText(this.textRenderer, Text.literal("SELECIONE SEU TIER"), panelX + 18, panelY + 61, 0xFFE5E5E5, true);
int gridWidth = BUTTON_WIDTH * 2 + BUTTON_GAP_X;
int gridX = panelX + (PANEL_WIDTH - gridWidth) / 2;
int gridY = panelY + 84;
for (int index = 0; index < TIERS.size(); index++) {
int column = index % 2;
int row = index / 2;
int buttonX = gridX + column * (BUTTON_WIDTH + BUTTON_GAP_X);
int buttonY = gridY + row * (BUTTON_HEIGHT + BUTTON_GAP_Y);
boolean selected = TIERS.get(index).equals(selectedTier);
int fillColor = selected ? 0xFF711820 : 0xFF242424;
int borderColor = selected ? 0xFFFF4545 : 0xFF555555;
context.fill(buttonX - 1, buttonY - 1, buttonX + BUTTON_WIDTH + 1, buttonY + BUTTON_HEIGHT + 1, borderColor);
context.fill(buttonX, buttonY, buttonX + BUTTON_WIDTH, buttonY + BUTTON_HEIGHT, fillColor);
context.drawText(this.textRenderer, Text.literal(TIERS.get(index)), buttonX + 12, buttonY + 11,
selected ? 0xFFFF6060 : 0xFFF0F0F0, true);
if (selected) {
context.fill(buttonX + BUTTON_WIDTH - 10, buttonY + 10, buttonX + BUTTON_WIDTH - 5, buttonY + 22,
0xFFFF4545);
}
}
context.drawText(this.textRenderer, Text.literal("ESC para fechar"), panelX + 18, panelY + 225,
0xFF888888, true);
}
@Override
public boolean mouseClicked(Click click, boolean doubled) {
if (click.button() != 0) {
return super.mouseClicked(click, doubled);
}
int panelX = (this.width - PANEL_WIDTH) / 2;
int panelY = (this.height - PANEL_HEIGHT) / 2;
int gridWidth = BUTTON_WIDTH * 2 + BUTTON_GAP_X;
int gridX = panelX + (PANEL_WIDTH - gridWidth) / 2;
int gridY = panelY + 84;
for (int index = 0; index < TIERS.size(); index++) {
int column = index % 2;
int row = index / 2;
int buttonX = gridX + column * (BUTTON_WIDTH + BUTTON_GAP_X);
int buttonY = gridY + row * (BUTTON_HEIGHT + BUTTON_GAP_Y);
if (click.x() >= buttonX && click.x() < buttonX + BUTTON_WIDTH
&& click.y() >= buttonY && click.y() < buttonY + BUTTON_HEIGHT) {
selectedTier = TIERS.get(index);
saveSelection();
return true;
}
}
return super.mouseClicked(click, doubled);
}
}
}