]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/ClientProxy.java
6b80f8c5a22fbf6bcf4314ac967da0fdb9d6a4fe
[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", 0x30, Name)
19                 .onKeyPressHandler(ClientProxy::toggleActive)
20                 .onLongKeyPressHandler(60, SettingsScreen::show);
21         KeyListener.register("Toggle Outer Box Only", 0x18, 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     }
53
54     private void render(Render event) {
55         if (!active || !ready) return;
56
57         renderer.render(event.getDimensionId(), ConfigManager.outerBoxesOnly.get());
58     }
59
60     private void disconnectedFromServer() {
61         active = false;
62         if (ConfigManager.keepCacheBetweenSessions.get()) return;
63         ready = false;
64         VillageColorCache.clear();
65         clearCaches();
66         renderer.clear();
67     }
68
69     private void addBoundingBox(AddBoundingBoxReceived event) {
70         BoundingBoxCache cache = getOrCreateCache(event.getDimensionId());
71         if (cache == null) return;
72
73         cache.addBoundingBoxes(event.getKey(), event.getBoundingBoxes());
74     }
75
76     private void onRemoveBoundingBoxReceived(RemoveBoundingBoxReceived event) {
77         super.removeBoundingBox(event.getDimensionId(), event.getKey());
78     }
79
80     private void onInitializeClientReceived(InitializeClientReceived event) {
81         setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
82         setSeed(event.getSeed());
83     }
84
85     private void onUpdateWorldSpawnReceived(UpdateWorldSpawnReceived event) {
86         setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
87     }
88
89     private void onSeedCommandTyped(SeedCommandTyped event) {
90         setSeed(event.getSeed());
91     }
92
93     @Override
94     public void setSeed(long seed) {
95         super.setSeed(seed);
96         renderer.setSeed(seed);
97         ready = true;
98     }
99
100     @Override
101     protected void setWorldSpawn(int spawnX, int spawnZ) {
102         super.setWorldSpawn(spawnX, spawnZ);
103         renderer.setWorldSpawn(spawnX, spawnZ);
104     }
105 }