aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java
blob: 96a8687e22b1d9bc25641b1b0304fb43b0573c08 (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
package ftbsc.bscv.modules.hud;

import com.google.auto.service.AutoService;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.HudModule;
import ftbsc.bscv.tools.Setting;
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 implements ICommons {

   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 Queue<Double> history_speed = 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> biome;
   // public final ForgeConfigSpec.ConfigValue<Boolean> latency;
   // public final ForgeConfigSpec.ConfigValue<Boolean> tps;
   // 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 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.hide_effects = Setting.Bool.builder()
         .name("hide-effects")
         .comment("hide effect icons on top right corner")
         .fallback(false)
         .build(this);
   }

   @SubscribeEvent
   public void onTick(TickEvent.ClientTickEvent event) {
      if (!this.speed.get()) return;
      if (event.phase == Phase.END) return;
      if (MC.player != null) {
         this.instant_speed = this.last_position.distanceTo(MC.player.position());
         this.last_position = MC.player.position();
      } else {
         this.instant_speed = 0.0;
      }

      this.history_speed.add(this.instant_speed);
      while (this.history_speed.size() >= 100) { // TODO customize this parameter
         this.history_speed.remove();
      }

      double buf = 0.0;
      for (double v : this.history_speed) { buf += v; }
      this.average_speed = buf / this.history_speed.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 onRenderOverlay(RenderGameOverlayEvent event) {
      if (event.getType() == ElementType.POTION_ICONS) {
         if (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.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";

   // 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 > 13000) return 23000;
      if (time > 12000) return 13000;
      return 12000;
   }

   private final int TPS = 20;
}