Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
12 752
src/client/java/com/appleuis/appleclient/AppleClientClient.java
package com.appleuis.appleclient;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.platform.Window;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.input.MouseButtonInfo;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
public final class AppleClientClient implements ClientModInitializer {
private static final Deque<Long> LEFT_CLICKS = new ArrayDeque<>();
private static final Map<Entity, Boolean> PREVIOUS_GLOWING = new IdentityHashMap<>();
private static KeyMapping openGuiKey;
private static double previousGamma;
private static boolean gammaCaptured;
@Override
public void onInitializeClient() {
AppleClientConfig.load();
KeyMapping.Category category = KeyMapping.Category.register(Identifier.fromNamespaceAndPath("appleclient", "general"));
openGuiKey = KeyMappingHelper.registerKeyMapping(
new KeyMapping("key.appleclient.open_gui", InputConstants.KEY_RSHIFT, category)
);
ClientTickEvents.END_CLIENT_TICK.register(AppleClientClient::tick);
HudElementRegistry.addLast(
Identifier.fromNamespaceAndPath("appleclient", "client_hud"),
AppleClientClient::renderHud
);
}
public static void recordMouseClick(int button, int action) {
if (button != InputConstants.MOUSE_BUTTON_LEFT || action != InputConstants.PRESS) {
return;
}
long now = System.currentTimeMillis();
LEFT_CLICKS.addLast(now);
trimClicks(now);
}
private static void tick(Minecraft client) {
while (openGuiKey.consumeClick()) {
if (client.gui.screen() instanceof AppleClientScreen screen) {
screen.closeAnimated();
} else {
client.gui.setScreen(new AppleClientScreen());
}
}
updateFullbright(client);
Player player = client.player;
ClientLevel level = client.level;
if (player == null || level == null) {
restoreEntityHighlights();
return;
}
player.setSprinting(AppleClientConfig.toggleSprint || client.options.keySprint.isDown());
player.setShiftKeyDown(AppleClientConfig.toggleSneak || client.options.keyShift.isDown());
updateEntityHighlights(player, level);
}
private static void updateFullbright(Minecraft client) {
if (AppleClientConfig.fullbright) {
if (!gammaCaptured) {
previousGamma = client.options.gamma().get();
gammaCaptured = true;
}
if (client.options.gamma().get() < 1.0) {
client.options.gamma().set(1.0);
}
} else if (gammaCaptured) {
client.options.gamma().set(previousGamma);
gammaCaptured = false;
}
}
private static void updateEntityHighlights(Player player, ClientLevel level) {
if (!AppleClientConfig.entityHighlight) {
restoreEntityHighlights();
return;
}
for (Entity entity : level.entitiesForRendering()) {
if (entity == player || player.distanceToSqr(entity) > 48.0 * 48.0) {
continue;
}
PREVIOUS_GLOWING.putIfAbsent(entity, entity.isCurrentlyGlowing());
entity.setGlowingTag(true);
}
}
private static void restoreEntityHighlights() {
for (Map.Entry<Entity, Boolean> entry : PREVIOUS_GLOWING.entrySet()) {
entry.getKey().setGlowingTag(entry.getValue());
}
PREVIOUS_GLOWING.clear();
}
private static void renderHud(GuiGraphicsExtractor graphics, net.minecraft.client.DeltaTracker deltaTracker) {
Minecraft client = Minecraft.getInstance();
Player player = client.player;
if (player == null) {
return;
}
List<String> lines = new ArrayList<>();
if (AppleClientConfig.showFps) {
lines.add("FPS " + client.getFps());
}
if (AppleClientConfig.showCps) {
lines.add("CPS " + currentCps());
}
if (AppleClientConfig.showAttackMeter) {
lines.add("ATTACK " + Math.round(player.getAttackStrengthScale(0.0F) * 100.0F) + "%");
}
if (AppleClientConfig.showCoordinates) {
Vec3 position = player.position();
lines.add("XYZ " + (int) Math.floor(position.x) + " " + (int) Math.floor(position.y) + " " + (int) Math.floor(position.z));
}
if (AppleClientConfig.showDirection) {
lines.add("FACING " + direction(player.getYRot()));
}
if (lines.isEmpty()) {
return;
}
int maxWidth = lines.stream().mapToInt(client.font::width).max().orElse(0);
Window window = client.getWindow();
boolean right = AppleClientConfig.hudCorner == 1 || AppleClientConfig.hudCorner == 3;
boolean bottom = AppleClientConfig.hudCorner >= 2;
int x = right ? window.getGuiScaledWidth() - maxWidth - 8 : 8;
int y = bottom ? window.getGuiScaledHeight() - lines.size() * 10 - 8 : 8;
int color = 0xFF000000 | AppleClientConfig.accent;
for (int index = 0; index < lines.size(); index++) {
graphics.text(client.font, lines.get(index), x, y + index * 10, color, true);
}
}
private static int currentCps() {
long now = System.currentTimeMillis();
trimClicks(now);
return LEFT_CLICKS.size();
}
private static void trimClicks(long now) {
while (!LEFT_CLICKS.isEmpty() && now - LEFT_CLICKS.peekFirst() > 1000L) {
LEFT_CLICKS.removeFirst();
}
}
private static String direction(float yaw) {
return switch (Math.floorMod(Math.round(yaw / 90.0F), 4)) {
case 0 -> "South";
case 1 -> "West";
case 2 -> "North";
default -> "East";
};
}
}