]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/ClientProxy.java
Stop rendering slime chunks if not initialized
[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 import java.net.InetSocketAddress;
14
15 public class ClientProxy extends CommonProxy {
16     public static final String Name = "Bounding Box Outline Reloaded";
17     public static boolean active;
18
19     static {
20         KeyListener.register("Toggle Active", 0x42, Name)
21                 .onKeyPressHandler(ClientProxy::toggleActive)
22                 .onLongKeyPressHandler(60, SettingsScreen::show);
23         KeyListener.register("Toggle Outer Box Only", 0x4f, Name)
24                 .onKeyPressHandler(ClientProxy::toggleOuterBoxesOnly);
25     }
26
27     private boolean ready;
28
29     public static void toggleActive() {
30         active = !active;
31         if (active)
32             PlayerCoords.setActiveY();
33     }
34
35     private static void toggleOuterBoxesOnly() {
36         Setting<Boolean> outerBoxesOnly = ConfigManager.outerBoxesOnly;
37         outerBoxesOnly.set(!outerBoxesOnly.get());
38     }
39
40     private ClientRenderer renderer;
41
42     @Override
43     public void init() {
44         super.init();
45         EventBus.subscribe(Render.class, this::render);
46         EventBus.subscribe(ConnectedToRemoteServer.class, this::connectedToServer);
47         EventBus.subscribe(DisconnectedFromRemoteServer.class, e -> disconnectedFromServer());
48         EventBus.subscribe(InitializeClientReceived.class, this::onInitializeClientReceived);
49         EventBus.subscribe(AddBoundingBoxReceived.class, this::addBoundingBox);
50         EventBus.subscribe(RemoveBoundingBoxReceived.class, this::onRemoveBoundingBoxReceived);
51
52         renderer = new ClientRenderer(this::getCache);
53         KeyListener.init();
54     }
55
56     private void render(Render event) {
57         if (!active || !ready) return;
58
59         renderer.render(event.getDimensionId(), ConfigManager.outerBoxesOnly.get());
60     }
61
62     private void connectedToServer(ConnectedToRemoteServer event) {
63         InetSocketAddress internetAddress = event.getInternetAddress();
64         NBTFileParser.loadLocalDatFiles(internetAddress.getHostName(), internetAddress.getPort(), this::setWorldData, this::getOrCreateCache);
65     }
66
67     private void disconnectedFromServer() {
68         ready = false;
69         active = false;
70         if (ConfigManager.keepCacheBetweenSessions.get()) return;
71         VillageColorCache.clear();
72         clearCaches();
73     }
74
75     private void addBoundingBox(AddBoundingBoxReceived event) {
76         BoundingBoxCache cache = getCache(event.getDimensionId());
77         if (cache == null) return;
78
79         cache.addBoundingBoxes(event.getKey(), event.getBoundingBoxes());
80     }
81
82     private void onRemoveBoundingBoxReceived(RemoveBoundingBoxReceived event) {
83         super.removeBoundingBox(event.getDimensionId(), event.getKey());
84     }
85
86     private void onInitializeClientReceived(InitializeClientReceived event) {
87         long seed = event.getSeed();
88         int spawnX = event.getSpawnX();
89         int spawnZ = event.getSpawnZ();
90         setWorldData(seed, spawnX, spawnZ);
91     }
92
93     @Override
94     protected void setWorldData(long seed, int spawnX, int spawnZ) {
95         super.setWorldData(seed, spawnX, spawnZ);
96         renderer.setWorldData(seed, spawnX, spawnZ);
97         ready = true;
98     }
99 }