mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
[Refactor] Singleton Pattern
This commit is contained in:
1 parent
5a90da8002
commit
b617c374f3
7 files changed
+38
-28
No files matched your search
@@ -10,23 +10,34 @@ import java.io.File;
|
||||
|
||||
public class OrangeX {
|
||||
@Getter
|
||||
private static final EventBus eventBus = EventBus.builder()
|
||||
.logNoSubscriberMessages(false)
|
||||
.logSubscriberExceptions(false)
|
||||
.sendNoSubscriberEvent(false)
|
||||
.sendSubscriberExceptionEvent(false)
|
||||
.installDefaultEventBus();
|
||||
private static final OrangeX Instance = new OrangeX();
|
||||
@Getter
|
||||
private static ModuleManager moduleManager = new ModuleManager();
|
||||
private final EventBus eventBus;
|
||||
@Getter
|
||||
private static String orangexDir = new File(System.getProperty("user.dir") + "/.orangex/").getAbsolutePath();
|
||||
private final ModuleManager moduleManager;
|
||||
private static FontManager fontManager;
|
||||
|
||||
public static void start() {
|
||||
System.out.println("OrangeX loading...");
|
||||
public OrangeX() {
|
||||
// EventBus
|
||||
this.eventBus = EventBus.builder()
|
||||
.logNoSubscriberMessages(false)
|
||||
.logSubscriberExceptions(false)
|
||||
.sendNoSubscriberEvent(false)
|
||||
.sendSubscriberExceptionEvent(false)
|
||||
.installDefaultEventBus();
|
||||
|
||||
// Module Manager
|
||||
this.moduleManager = new ModuleManager();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("OrangeX start!");
|
||||
MixinManager.start();
|
||||
moduleManager.init();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("OrangeX stopping...");
|
||||
}
|
||||
|
||||
public static FontManager getFontManager() {
|
||||
@@ -35,8 +46,4 @@ public class OrangeX {
|
||||
}
|
||||
return fontManager;
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
System.out.println("OrangeX stopping...");
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ public class MixinGuiIngame {
|
||||
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
mc.entityRenderer.setupOverlayRendering();
|
||||
|
||||
OrangeX.getEventBus().post(new EventRender2D(partialTicks));
|
||||
OrangeX.getInstance().getEventBus().post(new EventRender2D(partialTicks));
|
||||
|
||||
GlStateManager.enableDepth();
|
||||
GlStateManager.disableBlend();
|
||||
|
||||
@@ -16,17 +16,17 @@ public class MixinMinecraft {
|
||||
|
||||
@Inject(method = "runGameLoop", desc = "()V", target = @Target("HEAD"))
|
||||
private void onLoop() {
|
||||
OrangeX.getEventBus().post(new EventLoop());
|
||||
OrangeX.getInstance().getEventBus().post(new EventLoop());
|
||||
}
|
||||
|
||||
@Inject(method = "runTick", desc = "()V", target = @Target("HEAD"))
|
||||
public void onTick() {
|
||||
OrangeX.getEventBus().post(new EventTick());
|
||||
OrangeX.getInstance().getEventBus().post(new EventTick());
|
||||
}
|
||||
|
||||
@Inject(method = "shutdownMinecraftApplet", desc = "()V", target = @Target("HEAD"))
|
||||
public void onShutdown() {
|
||||
OrangeX.stop();
|
||||
OrangeX.getInstance().stop();
|
||||
}
|
||||
|
||||
@Inject(
|
||||
@@ -39,11 +39,11 @@ public class MixinMinecraft {
|
||||
)
|
||||
public void onKey(@Local(source = "key", index = 1) int key) {
|
||||
if (Minecraft.getMinecraft().currentScreen == null && Keyboard.getEventKeyState())
|
||||
OrangeX.getEventBus().post(new EventKey(key));
|
||||
OrangeX.getInstance().getEventBus().post(new EventKey(key));
|
||||
}
|
||||
|
||||
@Inject(method = "loadWorld", desc = "(Lnet/minecraft/client/multiplayer/WorldClient;Ljava/lang/String;)V", target = @Target("HEAD"))
|
||||
public void onLoadWorld() {
|
||||
OrangeX.getEventBus().post(new EventWorldLoad());
|
||||
OrangeX.getInstance().getEventBus().post(new EventWorldLoad());
|
||||
}
|
||||
}
|
||||
@@ -41,14 +41,14 @@ public abstract class Module {
|
||||
this.enabled = setEnabled;
|
||||
|
||||
if (this.enabled) {
|
||||
if (OrangeX.getEventBus().isRegistered(this))
|
||||
if (OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||
return;
|
||||
try { OrangeX.getEventBus().register(this); } catch (Exception ignored) {}
|
||||
try { OrangeX.getInstance().getEventBus().register(this); } catch (Exception ignored) {}
|
||||
onEnable();
|
||||
} else {
|
||||
if (!OrangeX.getEventBus().isRegistered(this))
|
||||
if (!OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||
return;
|
||||
try { OrangeX.getEventBus().unregister(this); } catch (Exception ignored) {}
|
||||
try { OrangeX.getInstance().getEventBus().unregister(this); } catch (Exception ignored) {}
|
||||
onDisable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,14 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ModuleManager {
|
||||
// 使用线程安全列表
|
||||
private static final List<Module> modules = new CopyOnWriteArrayList<>();
|
||||
|
||||
public ModuleManager() {
|
||||
OrangeX.getEventBus().register(this);
|
||||
|
||||
}
|
||||
|
||||
public void init() {
|
||||
OrangeX.getInstance().getEventBus().register(this);
|
||||
registerAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public class HUD extends Module {
|
||||
}
|
||||
|
||||
private void renderModuleList(ScaledResolution sr) {
|
||||
List<Module> enabledModules = OrangeX.getModuleManager()
|
||||
List<Module> enabledModules = OrangeX.getInstance().getModuleManager()
|
||||
.getAllModules()
|
||||
.stream()
|
||||
.filter(Module::isEnabled)
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ClickGuiScreen extends GuiScreen {
|
||||
public void initGui() {
|
||||
panel = new PanelUI(10, 10, 120, 180, "Combat");
|
||||
|
||||
for (Module module : OrangeX.getModuleManager().getAllModules()) {
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||
panel.addComponent(new ModuleButton(module, 120));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user