aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java
blob: 6553e9413bc83991807ee2122b92ca7b3e3cabc0 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package ftbsc.bscv.modules.hud;

import com.google.auto.service.AutoService;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.HudModule;
import ftbsc.bscv.patches.PacketPatch.PacketEvent;
import ftbsc.bscv.tools.Setting;
import net.minecraft.client.network.play.NetworkPlayerInfo;
import net.minecraft.network.play.server.SUpdateTimePacket;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.Color;
import net.minecraft.util.text.Style;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.util.LinkedList;
import java.util.Queue;

import static ftbsc.bscv.tools.Text.TextBuilder;

@AutoService(ILoadable.class)
public class InfoDisplay extends HudModule {

   private Vector3d last_position = new Vector3d(0.0, 0.0, 0.0);
   private double instant_speed = 0.0;
   private double average_speed = 0.0;
   private double instant_tps   = 0.0;
   private int    instant_ping  = 0;
   private Queue<Double> speed_history = new LinkedList<>();
   private Queue<Long>   tps_history   = new LinkedList<>();

   public final ForgeConfigSpec.ConfigValue<Boolean> logo;
   public final ForgeConfigSpec.ConfigValue<Boolean> speed;
   public final ForgeConfigSpec.ConfigValue<Boolean> age;
   public final ForgeConfigSpec.ConfigValue<Boolean> time;
   public final ForgeConfigSpec.ConfigValue<Boolean> fps;
   public final ForgeConfigSpec.ConfigValue<Boolean> ping;
   public final ForgeConfigSpec.ConfigValue<Boolean> tps;
   // public final ForgeConfigSpec.ConfigValue<Boolean> biome;
   // public final ForgeConfigSpec.ConfigValue<Boolean> light;
   // public final ForgeConfigSpec.ConfigValue<Boolean> saturation;
   // public final ForgeConfigSpec.ConfigValue<Boolean> system_time;
   // public final ForgeConfigSpec.ConfigValue<Boolean> damage_value;
   // public final ForgeConfigSpec.ConfigValue<Boolean> effects_list;
   // public final ForgeConfigSpec.ConfigValue<Boolean> item_quantity;
   // public final ForgeConfigSpec.ConfigValue<Boolean> client_chunk_size;

   public final ForgeConfigSpec.ConfigValue<Boolean> hide_effects;

   public final ForgeConfigSpec.ConfigValue<Integer> tps_sample_size;
   public final ForgeConfigSpec.ConfigValue<Integer> speed_sample_size;

   public InfoDisplay() {
      super();

      this.logo = Setting.Bool.builder()
         .name("logo")
         .comment("show logo at top of list")
         .fallback(true)
         .build(this);

      this.speed = Setting.Bool.builder()
         .name("speed")
         .comment("show speed meter")
         .fallback(true)
         .build(this);

      this.age = Setting.Bool.builder()
         .name("age")
         .comment("show age of the world")
         .fallback(true)
         .build(this);

      this.time = Setting.Bool.builder()
         .name("time")
         .comment("show world time")
         .fallback(true)
         .build(this);

      this.fps = Setting.Bool.builder()
         .name("fps")
         .comment("show current framerate")
         .fallback(true)
         .build(this);

      this.ping = Setting.Bool.builder()
         .name("ping")
         .comment("show server latency in ms")
         .fallback(true)
         .build(this);

      this.tps = Setting.Bool.builder()
         .name("tps")
         .comment("show client-calculated server TPS")
         .fallback(true)
         .build(this);

      this.hide_effects = Setting.Bool.builder()
         .name("hide-effects")
         .comment("hide effect icons on top right corner")
         .fallback(false)
         .build(this);

      this.tps_sample_size = Setting.Number.builder()
         .min(2)
         .name("tps-sample-size")
         .comment("TPS samples to store (1 taken each second)")
         .fallback(60)
         .build(this);

      this.speed_sample_size = Setting.Number.builder()
         .min(2)
         .name("speed-sample-size")
         .comment("instant speed samples to store (1 taken each tick)")
         .fallback(100)
         .build(this);
   }

   @SubscribeEvent
   public void onTick(TickEvent.ClientTickEvent event) {
      if (!this.speed.get()) return;
      if (event.phase == Phase.START) return;
      if (MC.player != null) {
         this.instant_speed = this.last_position.distanceTo(MC.player.position());
         this.last_position = MC.player.position();
         NetworkPlayerInfo info = MC.getConnection().getPlayerInfo(
            MC.player.getGameProfile().getId()
         );
         if (info != null) { // bungeecord switching makes this null for a second
            this.instant_ping = info.getLatency();
         }
      } else {
         this.instant_speed = 0.0;
         this.instant_ping = 0;
      }

      this.speed_history.offer(this.instant_speed);
      while (this.speed_history.size() >= this.speed_sample_size.get()) {
         this.speed_history.poll();
      }

      double buf = 0.0;
      for (double v : this.speed_history) { buf += v; }
      this.average_speed = buf / this.speed_history.size();

      if (this.last_fps_string != MC.fpsString) {
         this.last_fps_string = MC.fpsString;
         this.curr_fps = this.last_fps_string.split(" ")[0];
      }
   }

   @SubscribeEvent
   public void onPacket(PacketEvent.Incoming event) {
      if (event.packet instanceof SUpdateTimePacket) {
         this.tps_history.offer(System.currentTimeMillis());
         while (this.tps_history.size() > this.tps_sample_size.get()) {
            this.tps_history.poll();
         }
         double positive_time = 0.;
         double last_time = 0;
         for (long t : this.tps_history) {
            if (last_time != 0) {
               double delta = (double) (t - last_time) / 1000.;
               positive_time += Math.max(delta, 1.);
            }
            last_time = t;
         }
         this.instant_tps = 20 / (positive_time / (this.tps_history.size() - 1));
      }
   }

   @SubscribeEvent
   public void onRenderOverlay(RenderGameOverlayEvent event) {
      if (
         event.getType() == ElementType.POTION_ICONS
         && MC.screen == null
         && this.hide_effects.get()
         && event.isCancelable()
      ) {
         event.setCanceled(true);
      }

      if (event.getType() != ElementType.TEXT) return;
      if (this.shouldHide()) return;

      int offset = 0;
      double scale = this.scale.get();
      if (this.logo.get()) {
         TextBuilder()
            .txt("BSCV")
            .anchor(this.anchor.get())
            .x(this.x.get() / 4)
            .y(this.y.get() / 4)
            .style(Style.EMPTY.withColor(Color.fromRgb(12542314)).withBold(true))
            .scale(scale * 4.0)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale * 4.0;
      }

      long day = 0;
      long time = 0;
      if (MC.level != null) {
         day = MC.level.dayTime() / 24000L;
         time = MC.level.dayTime() % 24000L;
      }

      if (this.fps.get()) {
         TextBuilder()
            .txt(this.affixed("fps: %s", this.curr_fps))
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }

      if (this.ping.get()) {
         TextBuilder()
            .txt(this.affixed("ping: %d", this.instant_ping))
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }

      if (this.tps.get()) {
         TextBuilder()
            .txt(this.affixed("tps: %.1f", this.instant_tps))
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }

      if (this.speed.get()) {
         TextBuilder()
            .txt(this.affixed("speed: %.1f [%.1f] m/s", this.instant_speed * 20.0, this.average_speed * 20.0))
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }

      if (this.age.get()) {
         TextBuilder()
            .txt(this.affixed("age: %d (~%d days)", day, day / (3 * 24) )) // 3 mc days last 1 hour
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }

      if (this.time.get()) {
         TextBuilder()
            .txt(this.affixed("time: %d/%d (%s)", (time / TPS), this.getNextStep(time) / TPS, this.getTimePhase(time) ))
            .anchor(this.anchor.get())
            .x(this.x.get())
            .y(this.y.get() + offset)
            .scale(scale)
            .render(event.getMatrixStack(), event.getWindow());
         offset += MC.font.lineHeight * scale;
      }
   }

   private String last_fps_string;
   private String curr_fps = "0";

   // TPS utils


   // Time utils
   private String getTimePhase(long time) {
      if (time > 23000) return "Dawn";
      if (time > 18500) return "Night";
      if (time > 17500) return "Midnight";
      if (time > 13000) return "Evening";
      if (time > 12000) return "Dusk";
      if (time > 6500) return "Afternoon";
      if (time > 5500) return "Noon";
      return "Morning";
   }

   private int getNextStep(long time) {
      if (time > 23000) return 24000;
      if (time > 18500) return 23000;
      if (time > 17500) return 18500;
      if (time > 13000) return 17500;
      if (time > 12000) return 13000;
      if (time > 6500) return 12000;
      if (time > 5500) return 6500;
      return 5500;
   }

   private final int TPS = 20;
}