Flummi fc720a6541
All checks were successful
build / build (push) Successful in 1m29s
clothconfig & api
2023-06-05 04:39:19 +02:00

307 lines
11 KiB
Java

package lel.flummi.skilloverlay.overlays;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Formatting;
import net.minecraft.client.gui.DrawableHelper;
import com.mojang.blaze3d.systems.RenderSystem;
//import lel.flummi.skilloverlay.skilloverlay;
import lel.flummi.skilloverlay.utils.LerpUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
@Environment(EnvType.CLIENT)
public class FarmingOverlay extends DrawableHelper {
private MinecraftClient client;
private MatrixStack matrixStack;
private long lastUpdate = -1;
private final LinkedList<Long> counterQueue = new LinkedList<>();
private int cultivatingTier = -1;
private String cultivatingTierAmount = "1";
private long counter = -1;
private int cultivating = -1;
private int cultivatingLast = -1;
private float cropsPerSecondLast = 0;
private float cropsPerSecond = 0;
private ArrayList<Float> cropsOverLastXSeconds = new ArrayList<>();
private String cropName;
public FarmingOverlay() {
/*HudRenderCallback.EVENT.register((matrixStack, tickDelta) -> {
if(client.player == null)
return;
this.render(matrixStack, client);
});*/
}
public void render(MatrixStack matrixStack, MinecraftClient client) {
if (client.player == null)
return;
this.matrixStack = matrixStack;
this.client = MinecraftClient.getInstance();
RenderSystem.enableBlend();
this.renderOverlay();
this.client.getProfiler().pop();
}
public void renderOverlay() {
this.lastUpdate = System.currentTimeMillis();
ItemStack heldItem = client.player.getMainHandStack();
String internalName = "";
this.counter = 0;
boolean holdingFarmItem = false;
this.cultivatingLast = cultivating;
this.cultivating = 0;
//System.out.println(skilloverlay.PROFILE.data().skills().get("farming").level());
if (heldItem != null) {
NbtCompound tag = heldItem.getNbt();
if (tag != null && tag.contains("ExtraAttributes", 10)) {
NbtCompound ea = tag.getCompound("ExtraAttributes");
internalName = ea.getString("id");
for (CropType crop : CropType.values()) {
if (internalName.startsWith(crop.toolName)) {
this.cropName = crop.item;
holdingFarmItem = true;
}
}
if(holdingFarmItem) {
if (ea.contains("mined_crops", 99)) {
this.counter = ea.getLong("mined_crops");
cultivating = ea.getInt("farmed_cultivating");
this.counterQueue.add(0, this.counter);
} else if (ea.contains("farmed_cultivating", 99)) {
this.counter = ea.getInt("farmed_cultivating");
cultivating = ea.getInt("farmed_cultivating");
this.counterQueue.add(0, this.counter);
}
}
else
holdingFarmItem = false;
}
if (this.cultivating < 1000) {
this.cultivatingTier = 1;
this.cultivatingTierAmount = "1,000";
} else if (this.cultivating < 5000) {
this.cultivatingTier = 2;
this.cultivatingTierAmount = "5,000";
} else if (this.cultivating < 25000) {
this.cultivatingTier = 3;
this.cultivatingTierAmount = "25,000";
} else if (this.cultivating < 100000) {
this.cultivatingTier = 4;
this.cultivatingTierAmount = "100,000";
} else if (this.cultivating < 300000) {
this.cultivatingTier = 5;
this.cultivatingTierAmount = "300,000";
} else if (this.cultivating < 1500000) {
this.cultivatingTier = 6;
this.cultivatingTierAmount = "1,500,000";
} else if (this.cultivating < 5000000) {
this.cultivatingTier = 7;
this.cultivatingTierAmount = "5,000,000";
} else if (this.cultivating < 20000000) {
this.cultivatingTier = 8;
this.cultivatingTierAmount = "20,000,000";
} else if (this.cultivating < 100000000) {
this.cultivatingTier = 9;
this.cultivatingTierAmount = "100,000,000";
} else if (this.cultivating > 100000000) {
this.cultivatingTier = 10;
this.cultivatingTierAmount = "Maxed";
}
}
while (this.counterQueue.size() >= 4) {
this.counterQueue.removeLast();
}
if (this.counterQueue.isEmpty()) {
this.cropsPerSecond = -1;
this.cropsPerSecondLast = 0;
} else {
this.cropsPerSecondLast = this.cropsPerSecond;
long last = this.counterQueue.getLast();
long first = this.counterQueue.getFirst();
while (this.cropsOverLastXSeconds.size() > 60) {
this.cropsOverLastXSeconds.remove(0);
}
if ((first - last) / 2f != 0) {
this.cropsOverLastXSeconds.add((first - last) / 2f);
} else {
if (this.cropsPerSecondLast == 0) {
int i = 12;
while (i > 0) {
i--;
if (this.cropsOverLastXSeconds.size() > 0) {
this.cropsOverLastXSeconds.remove(0);
} else {
break;
}
}
}
}
if (!holdingFarmItem) {
this.cropsOverLastXSeconds.clear();
this.cropsPerSecond = -1;
this.cropsPerSecondLast = 0;
}
ArrayList<Float> temp = new ArrayList<>(cropsOverLastXSeconds);
if (this.cropsOverLastXSeconds.size() >= 3) {
temp.remove(Collections.min(temp));
}
if (this.cropsOverLastXSeconds.size() >= 6) {
temp.remove(Collections.min(temp));
temp.remove(Collections.max(temp));
}
if (this.cropsOverLastXSeconds.size() >= 10) {
temp.remove(Collections.max(temp));
}
float cropsOverLastXSecondsTotal = 0;
for (Float crops : temp) {
cropsOverLastXSecondsTotal += crops;
}
this.cropsPerSecond = temp.size() != 0 && cropsOverLastXSecondsTotal != 0
? cropsOverLastXSecondsTotal / temp.size()
: 0;
}
ArrayList<OverlayString> overlayStringList = new ArrayList<>();
NumberFormat format = NumberFormat.getIntegerInstance();
// Add all the lines to the array
if (this.counter >= 0 && this.cultivating != this.counter) {
overlayStringList.add(new OverlayString("Counter", format.format(this.counter)));
}
if (this.counter >= 0) {
if (this.cropsPerSecondLast == this.cropsPerSecond && this.cropsPerSecond <= 0) {
overlayStringList.add(new OverlayString(this.cropName + "/m", "N/A"));
} else {
float cpsInterp = interp(this.cropsPerSecond, this.cropsPerSecondLast);
overlayStringList.add(new OverlayString(this.cropName + "/m", String.format("%,.2f", cpsInterp * 60 * 30)));
}
}
if (this.cultivatingTier <= 9 && this.cultivating > 0) {
int counterInterp = (int) interp(this.cultivating, this.cultivatingLast);
overlayStringList.add(new OverlayString("Cultivating", format.format(counterInterp) + "/" + this.cultivatingTierAmount));
}
if (this.cultivatingTier == 10) {
int counterInterp = (int) interp(this.cultivating, this.cultivatingLast);
overlayStringList.add(new OverlayString("Cultivating", format.format(counterInterp)));
}
float yaw = client.player.getYaw();
float pitch = client.player.getPitch();
yaw %= 360;
if (yaw < 0)
yaw += 360;
if (yaw > 180)
yaw -= 360;
pitch %= 360;
if (pitch < 0)
pitch += 360;
if (pitch > 180)
pitch -= 360;
overlayStringList.add(new OverlayString("Yaw", String.format("%.2f", yaw) + Formatting.BOLD + "\u1D52"));
overlayStringList.add(new OverlayString("Pitch", String.format("%.2f", pitch) + Formatting.BOLD + "\u1D52"));
if (!holdingFarmItem) {
overlayStringList.clear();
}
// Draw HUD
int xAxis = 10;
int yAxis = 200;
// Get the longest string in the array
int longestString = 0;
int BoxWidth = 0;
for (OverlayString s : overlayStringList) {
String combined = s.toString();
if (combined.length() > longestString) {
longestString = combined.length();
BoxWidth = this.client.textRenderer.getWidth(combined);
}
}
int lineHeight = this.client.textRenderer.fontHeight + 2;
DrawableHelper.fill(matrixStack, xAxis, yAxis, xAxis + BoxWidth + 10,
yAxis + ((lineHeight + 1) * overlayStringList.size()), 0x64000000);
for (OverlayString line : overlayStringList) {
int offset = 5;
this.client.textRenderer.drawWithShadow(this.matrixStack, line.toString(), xAxis + offset, yAxis + offset, 0xFFFFFF);
yAxis += lineHeight;
}
}
private float interp(float now, float last) {
float interp = now;
if (last >= 0 && last != now) {
float factor = (System.currentTimeMillis() - this.lastUpdate) / 1000f;
factor = LerpUtils.clampZeroOne(factor);
interp = last + (now - last) * factor;
}
return interp;
}
private enum CropType {
WHEAT("THEORETICAL_HOE_WHEAT", "Wheat"),
NETHER_WART("THEORETICAL_HOE_WARTS", "Warts"),
SUGAR_CANE("THEORETICAL_HOE_CANE", "Sugar"),
CARROT("THEORETICAL_HOE_CARROT", "Carrots"),
POTATO("THEORETICAL_HOE_POTATO", "Potatoes"),
COCOA_BEANS("COCO_CHOPPER", "Cocoas"),
PUMPKIN("PUMPKIN_DICER", "Pumpkins"),
MELON("MELON_DICER", "Melons"),
CACTUS("CACTUS_KNIFE", "Cactus"),
;
private final String toolName;
private final String item;
CropType(String toolName, String item) {
this.toolName = toolName;
this.item = item;
}
}
private class OverlayString {
final String type;
final String text;
private OverlayString(String type, String text) {
this.type = type;
this.text = text;
}
public String toString() {
return Formatting.AQUA + this.type + ": " + Formatting.YELLOW + this.text;
}
}
}