forked from MeteorDevelopment/meteor-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMeteorClient.java
More file actions
210 lines (171 loc) · 7.69 KB
/
MeteorClient.java
File metadata and controls
210 lines (171 loc) · 7.69 KB
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
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/
package meteordevelopment.meteorclient;
import meteordevelopment.meteorclient.addons.AddonManager;
import meteordevelopment.meteorclient.addons.MeteorAddon;
import meteordevelopment.meteorclient.events.game.OpenScreenEvent;
import meteordevelopment.meteorclient.events.meteor.KeyEvent;
import meteordevelopment.meteorclient.events.meteor.MouseClickEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.gui.GuiThemes;
import meteordevelopment.meteorclient.gui.WidgetScreen;
import meteordevelopment.meteorclient.gui.tabs.Tabs;
import meteordevelopment.meteorclient.systems.Systems;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.systems.hud.screens.HudEditorScreen;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.misc.DiscordPresence;
import meteordevelopment.meteorclient.utils.PostInit;
import meteordevelopment.meteorclient.utils.PreInit;
import meteordevelopment.meteorclient.utils.ReflectInit;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.misc.Version;
import meteordevelopment.meteorclient.utils.misc.input.KeyAction;
import meteordevelopment.meteorclient.utils.misc.input.KeyBinds;
import meteordevelopment.meteorclient.utils.misc.text.MeteorTranslatableTextContent;
import meteordevelopment.meteorclient.utils.network.OnlinePlayers;
import meteordevelopment.orbit.EventBus;
import meteordevelopment.orbit.EventHandler;
import meteordevelopment.orbit.EventPriority;
import meteordevelopment.orbit.IEventBus;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.text.MutableText;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.MixinEnvironment;
import java.io.File;
import java.lang.invoke.MethodHandles;
public class MeteorClient implements ClientModInitializer {
public static final String MOD_ID = "meteor-client";
public static final ModMetadata MOD_META;
public static final String NAME;
public static final Version VERSION;
public static final String BUILD_NUMBER;
public static MeteorClient INSTANCE;
public static MeteorAddon ADDON;
public static MinecraftClient mc;
public static final IEventBus EVENT_BUS = new EventBus();
public static final File FOLDER = FabricLoader.getInstance().getGameDir().resolve(MOD_ID).toFile();
public static final Logger LOG;
static {
MOD_META = FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata();
NAME = MOD_META.getName();
LOG = LoggerFactory.getLogger(NAME);
String versionString = MOD_META.getVersion().getFriendlyString();
if (versionString.contains("-")) versionString = versionString.split("-")[0];
// When building and running through IntelliJ and not Gradle it doesn't replace the version so just use a dummy
if (versionString.equals("${version}")) versionString = "0.0.0";
VERSION = new Version(versionString);
BUILD_NUMBER = MOD_META.getCustomValue(MeteorClient.MOD_ID + ":build_number").getAsString();
}
@Override
public void onInitializeClient() {
if (INSTANCE == null) {
INSTANCE = this;
return;
}
// Global minecraft client accessor
mc = MinecraftClient.getInstance();
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
LOG.info("Force loading mixins");
MixinEnvironment.getCurrentEnvironment().audit();
}
LOG.info("Initializing {}", NAME);
// Pre-load
if (!FOLDER.exists()) {
FOLDER.getParentFile().mkdirs();
FOLDER.mkdir();
Systems.addPreLoadTask(() -> Modules.get().get(DiscordPresence.class).enable());
}
// Register addons
AddonManager.init();
// Register event handlers
AddonManager.ADDONS.forEach(addon -> {
try {
EVENT_BUS.registerLambdaFactory(addon.getPackage(), (lookupInMethod, klass) -> (MethodHandles.Lookup) lookupInMethod.invoke(null, klass, MethodHandles.lookup()));
} catch (AbstractMethodError e) {
throw new RuntimeException("Addon \"%s\" is too old and cannot be ran.".formatted(addon.name), e);
}
});
// Register init classes
ReflectInit.registerPackages();
// Pre init
ReflectInit.init(PreInit.class);
// Register module categories
Categories.init();
// Load systems
Systems.init();
// Subscribe after systems are loaded
EVENT_BUS.subscribe(this);
// Initialise addons
AddonManager.ADDONS.forEach(MeteorAddon::onInitialize);
// Sort modules after addons have added their own
Modules.get().sortModules();
// Load configs
Systems.load();
// Post init
ReflectInit.init(PostInit.class);
// Save on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
OnlinePlayers.leave();
Systems.save();
GuiThemes.save();
}));
}
@EventHandler
private void onTick(TickEvent.Post event) {
if (mc.currentScreen == null && mc.getOverlay() == null && KeyBinds.OPEN_COMMANDS.wasPressed()) {
mc.setScreen(new ChatScreen(Config.get().prefix.get(), true));
}
}
@EventHandler
private void onKey(KeyEvent event) {
if (event.action == KeyAction.Press && KeyBinds.OPEN_GUI.matchesKey(event.input)) {
toggleGui();
}
}
@EventHandler
private void onMouseClick(MouseClickEvent event) {
if (event.action == KeyAction.Press && KeyBinds.OPEN_GUI.matchesMouse(event.click)) {
toggleGui();
}
}
private void toggleGui() {
if (Utils.canCloseGui()) mc.currentScreen.close();
else if (Utils.canOpenGui()) Tabs.get().getFirst().openScreen(GuiThemes.get());
}
// Hide HUD
private boolean wasWidgetScreen, wasHudHiddenRoot;
@EventHandler(priority = EventPriority.LOWEST)
private void onOpenScreen(OpenScreenEvent event) {
if (event.screen instanceof WidgetScreen) {
if (!wasWidgetScreen) wasHudHiddenRoot = mc.options.hudHidden;
if (GuiThemes.get().hideHUD() || wasHudHiddenRoot) {
// Always show the MC HUD in the HUD editor screen since people like
// to align some items with the hotbar or chat
mc.options.hudHidden = !(event.screen instanceof HudEditorScreen);
}
} else {
if (wasWidgetScreen) mc.options.hudHidden = wasHudHiddenRoot;
wasHudHiddenRoot = mc.options.hudHidden;
}
wasWidgetScreen = event.screen instanceof WidgetScreen;
}
public static Identifier identifier(String path) {
return Identifier.of(MeteorClient.MOD_ID, path);
}
public static MutableText translatable(String key, Object... args) {
return MutableText.of(new MeteorTranslatableTextContent(key, args));
}
public static MutableText translatable(String key, String fallback, Object... args) {
return MutableText.of(new MeteorTranslatableTextContent(key, fallback, args));
}
}