]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/ClientProxy.java
Remove loading from dat file support
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / ClientProxy.java
1 package com.irtimaled.bbor.client;
2
3 import com.irtimaled.bbor.client.events.*;
4 import com.irtimaled.bbor.client.gui.SettingsScreen;
5 import com.irtimaled.bbor.client.keyboard.KeyListener;
6 import com.irtimaled.bbor.common.BoundingBoxCache;
7 import com.irtimaled.bbor.common.CommonProxy;
8 import com.irtimaled.bbor.common.EventBus;
9 import com.irtimaled.bbor.common.VillageColorCache;
10 import com.irtimaled.bbor.config.ConfigManager;
11 import com.irtimaled.bbor.config.Setting;
12
13 public class ClientProxy extends CommonProxy {
14     public static final String Name = "Bounding Box Outline Reloaded";
15     public static boolean active;
16
17     static {
18         KeyListener.register("Toggle Active", 0x42, Name)
19                 .onKeyPressHandler(ClientProxy::toggleActive)
20                 .onLongKeyPressHandler(60, SettingsScreen::show);
21         KeyListener.register("Toggle Outer Box Only", 0x4f, Name)
22                 .onKeyPressHandler(ClientProxy::toggleOuterBoxesOnly);
23     }
24
25     private boolean ready;
26
27     public static void toggleActive() {
28         active = !active;
29         if (active)
30             PlayerCoords.setActiveY();
31     }
32
33     private static void toggleOuterBoxesOnly() {
34         Setting<Boolean> outerBoxesOnly = ConfigManager.outerBoxesOnly;
35         outerBoxesOnly.set(!outerBoxesOnly.get());
36     }
37
38     private ClientRenderer renderer;
39
40     @Override
41     public void init() {
42         super.init();
43         EventBus.subscribe(Render.class, this::render);
44         EventBus.subscribe(DisconnectedFromRemoteServer.class, e -> disconnectedFromServer());
45         EventBus.subscribe(InitializeClientReceived.class, this::onInitializeClientReceived);
46         EventBus.subscribe(AddBoundingBoxReceived.class, this::addBoundingBox);
47         EventBus.subscribe(RemoveBoundingBoxReceived.class, this::onRemoveBoundingBoxReceived);
48         EventBus.subscribe(UpdateWorldSpawnReceived.class, this::onUpdateWorldSpawnReceived);
49         EventBus.subscribe(SeedCommandTyped.class, this::onSeedCommandTyped);
50
51         renderer = new ClientRenderer(this::getCache);
52         KeyListener.init();
53     }
54
55     private void render(Render event) {
56         if (!active || !ready) return;
57
58         renderer.render(event.getDimensionId(), ConfigManager.outerBoxesOnly.get());
59     }
60
61     private void disconnectedFromServer() {
62         active = false;
63         if (ConfigManager.keepCacheBetweenSessions.get()) return;
64         ready = false;
65         VillageColorCache.clear();
66         clearCaches();
67         renderer.clear();
68     }
69
70     private void addBoundingBox(AddBoundingBoxReceived event) {
71         BoundingBoxCache cache = getOrCreateCache(event.getDimensionId());
72         if (cache == null) return;
73
74         cache.addBoundingBoxes(event.getKey(), event.getBoundingBoxes());
75     }
76
77     private void onRemoveBoundingBoxReceived(RemoveBoundingBoxReceived event) {
78         super.removeBoundingBox(event.getDimensionId(), event.getKey());
79     }
80
81     private void onInitializeClientReceived(InitializeClientReceived event) {
82         setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
83         setSeed(event.getSeed());
84     }
85
86     private void onUpdateWorldSpawnReceived(UpdateWorldSpawnReceived event) {
87         setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
88     }
89
90     private void onSeedCommandTyped(SeedCommandTyped event) {
91         setSeed(event.getSeed());
92     }
93
94     @Override
95     public void setSeed(long seed) {
96         super.setSeed(seed);
97         renderer.setSeed(seed);
98         ready = true;
99     }
100
101     @Override
102     protected void setWorldSpawn(int spawnX, int spawnZ) {
103         super.setWorldSpawn(spawnX, spawnZ);
104         renderer.setWorldSpawn(spawnX, spawnZ);
105     }
106 }