aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/vision/UpdateESP.java
blob: 6db66f1a92a75971a9732d44c8a0c544aac71080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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())) );
      }
   }
}