feat: new module UpdateESP

This commit is contained in:
əlemi 2023-03-12 02:14:33 +01:00
parent 28a7cb9660
commit 9eab4544b0
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -0,0 +1,150 @@
package ftbsc.bscv.modules.vision;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.lwjgl.opengl.GL11;
import com.google.auto.service.AutoService;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.QuickModule;
import ftbsc.bscv.patches.PacketPatch.PacketEvent;
import ftbsc.bscv.tools.Setting;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.WorldVertexBufferUploader;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.network.play.server.SChangeBlockPacket;
import net.minecraft.network.play.server.SMultiBlockChangePacket;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3i;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@AutoService(ILoadable.class)
public class UpdateESP extends QuickModule {
public final ForgeConfigSpec.ConfigValue<Integer> duration;
public final ForgeConfigSpec.ConfigValue<Double> alpha;
public final ForgeConfigSpec.ConfigValue<Double> width;
public final ForgeConfigSpec.ConfigValue<Boolean> fade;
public UpdateESP() {
super();
this.updates = new ConcurrentLinkedQueue<>();
this.duration = Setting.Number.builder()
.min(0)
.fallback(250)
.name("duration")
.comment("how long to show block updates")
.build(this);
this.alpha = Setting.Decimal.builder()
.min(0.)
.max(1.)
.fallback(.25)
.name("alpha")
.comment("alpha channel value for highlights")
.build(this);
this.width = Setting.Decimal.builder()
.min(0.)
.fallback(1.)
.name("width")
.comment("line width for highlights")
.build(this);
this.fade = Setting.Bool.builder()
.fallback(true)
.name("fade")
.comment("gradually decrease alpha to disappear smoothly")
.build(this);
}
private final ConcurrentLinkedQueue<Tuple<Vector3i, Long>> updates;
private float getAlpha(float multiplier, long insert, long now, long duration) {
long age = now - insert;
if (age > duration) return 0.f;
return multiplier * (1.f - ((float) age / (float) duration));
}
@SubscribeEvent
public void onRenderWorld(RenderWorldLastEvent event) {
RenderSystem.pushMatrix(); // ??
RenderSystem.enableBlend();
RenderSystem.disableTexture();
RenderSystem.disableDepthTest();
RenderSystem.lineWidth(this.width.get().floatValue());
Vector3d projectedView = MC.gameRenderer.getMainCamera().getPosition().scale(-1);
MatrixStack stack = event.getMatrixStack();
stack.pushPose(); // ??
stack.translate(projectedView.x(), projectedView.y(), projectedView.z());
BufferBuilder builder = Tessellator.getInstance().getBuilder();
builder.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
long time = System.currentTimeMillis();
long duration = this.duration.get();
float multiplier = this.alpha.get().floatValue();
boolean fade = this.fade.get();
for (Tuple<Vector3i, Long> entry : this.updates) {
Vector3i pos = entry.getA();
WorldRenderer.renderLineBox(
stack, builder,
pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1,
1.f, 1.f, 1.f, fade ? this.getAlpha(multiplier, entry.getB(), time, duration) : multiplier
);
}
builder.end();
WorldVertexBufferUploader.end(builder);
stack.popPose(); // ??
RenderSystem.lineWidth(1.f);
RenderSystem.enableTexture();
RenderSystem.enableDepthTest();
RenderSystem.disableBlend();
RenderSystem.enableCull();
RenderSystem.popMatrix(); // ??
}
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if (event.phase == Phase.END) return;
long time = System.currentTimeMillis();
long duration = this.duration.get();
while (this.updates.peek() != null && time - this.updates.peek().getB() > duration) {
this.updates.poll();
}
}
@SubscribeEvent
public void onPacket(PacketEvent.Incoming event) {
if (event.packet instanceof SChangeBlockPacket) {
SChangeBlockPacket packet = (SChangeBlockPacket) event.packet;
this.updates.add(new Tuple<>(packet.getPos(), System.currentTimeMillis()));
}
if (event.packet instanceof SMultiBlockChangePacket) {
SMultiBlockChangePacket packet = (SMultiBlockChangePacket) event.packet;
packet.runUpdates( (pos, state) -> this.updates.add(new Tuple<>(pos, System.currentTimeMillis())) );
}
}
}