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

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

import java.util.ArrayDeque;

import com.mojang.brigadier.CommandDispatcher;

import ftbsc.bscv.ICommons;
import ftbsc.bscv.modules.HudModule;
import ftbsc.bscv.tools.Setting;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource;
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.eventbus.api.SubscribeEvent;

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 ArrayDeque<Double> history_speed = new ArrayDeque<>();

   public final ForgeConfigSpec.ConfigValue<Boolean> logo;
   public final ForgeConfigSpec.ConfigValue<Boolean> speed;
   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(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
      super("InfoDisplay", builder, dispatcher);

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

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

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

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

   @SubscribeEvent
   public void onTick(TickEvent.ClientTickEvent event) {
      if (!this.speed.get()) 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.push(this.instant_speed);
      while (this.history_speed.size() >= 100) { // TODO customize this parameter
         this.history_speed.pop();
      }

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

   @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;
      }

      if (this.time.get()) {
         long daytime = 0;
         if (MC.level != null) {
            daytime = MC.level.dayTime();
         }
         TextBuilder()
            .txt(String.format("> time: %d/1200 (%d day)", (daytime / 20) % 1200, daytime / (20 * 1200) ))
            .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(String.format("> 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;
      }
   }
}