-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCodeClient.java
More file actions
466 lines (409 loc) · 19.7 KB
/
Copy pathCodeClient.java
File metadata and controls
466 lines (409 loc) · 19.7 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package dev.dfonline.codeclient;
import com.google.gson.Gson;
import dev.dfonline.codeclient.action.Action;
import dev.dfonline.codeclient.action.None;
import dev.dfonline.codeclient.action.impl.DevForBuild;
import dev.dfonline.codeclient.command.CommandManager;
import dev.dfonline.codeclient.config.Config;
import dev.dfonline.codeclient.config.KeyBinds;
import dev.dfonline.codeclient.dev.*;
import dev.dfonline.codeclient.dev.debug.Debug;
import dev.dfonline.codeclient.dev.menu.InsertOverlayFeature;
import dev.dfonline.codeclient.dev.menu.RecentValues;
import dev.dfonline.codeclient.dev.menu.SlotGhostManager;
import dev.dfonline.codeclient.dev.overlay.ActionViewer;
import dev.dfonline.codeclient.dev.overlay.CPUDisplay;
import dev.dfonline.codeclient.dev.overlay.ChestPeeker;
import dev.dfonline.codeclient.hypercube.actiondump.ActionDump;
import dev.dfonline.codeclient.location.*;
import dev.dfonline.codeclient.switcher.StateSwitcher;
import dev.dfonline.codeclient.websocket.SocketHandler;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.AbstractNbtNumber;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtString;
import net.minecraft.network.listener.PacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.CloseScreenS2CPacket;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
public class CodeClient implements ClientModInitializer {
public static final String MOD_NAME = "CodeClient";
public static final String MOD_ID = "codeclient";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);
public static final Gson gson = new Gson();
public static MinecraftClient MC = MinecraftClient.getInstance();
public static AutoJoin autoJoin = AutoJoin.NONE;
@NotNull
public static Action currentAction = new None();
public static Action confirmingAction = null;
public static Location lastLocation = null;
public static Location location = null;
public static boolean shouldReload = false;
public static boolean isPreviewingItemTags = false;
private static final HashMap<Class<? extends Feature>, Feature> features = new HashMap<>();
private static boolean isCodeChest = false;
public static SocketHandler API = new SocketHandler();
@Override
public void onInitializeClient() {
MC = MinecraftClient.getInstance();
loadFeatures();
ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (MC.player == null || MC.world == null) clean();
});
BlockRenderLayerMap.INSTANCE.putBlock(Blocks.BARRIER, RenderLayer.getTranslucent());
BlockRenderLayerMap.INSTANCE.putBlock(Blocks.STRUCTURE_VOID, RenderLayer.getTranslucent());
BlockRenderLayerMap.INSTANCE.putBlock(Blocks.LIGHT, RenderLayer.getTranslucent());
ClientLifecycleEvents.CLIENT_STOPPING.register(new Identifier(MOD_ID, "close"), client -> API.stop());
if (Config.getConfig().CodeClientAPI) {
try {
API.start();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
if (Config.getConfig().AutoJoin) {
autoJoin = AutoJoin.GAME;
}
KeyBinds.init();
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> CommandManager.init(dispatcher, registryAccess));
ItemTooltipCallback.EVENT.register((stack, context, lines) -> {
if (isPreviewingItemTags) {
if (!stack.hasNbt()) return;
NbtCompound nbt = stack.getNbt();
if (nbt == null) return;
if (!nbt.contains("PublicBukkitValues")) return;
NbtCompound publicBukkit = nbt.getCompound("PublicBukkitValues");
for (var key : publicBukkit.getKeys()) {
if (key.startsWith("hypercube:")) {
NbtElement element = publicBukkit.get(key);
// Any type = yellow, number = red, string = aqua.
MutableText value = Text.literal(publicBukkit.get(key).toString()).formatted(Formatting.GREEN);
if (element instanceof NbtString) value.formatted(Formatting.AQUA);
if (element instanceof AbstractNbtNumber) value.formatted(Formatting.RED);
lines.add(
Text.literal(key.replace("hypercube:", ""))
.withColor(0xAAFF55)
.append(Text.literal(" = ").formatted(Formatting.DARK_GRAY))
.append(value)
);
}
}
}
});
try {
registerResourcePack("dark_mode", Text.literal("Dark Mode").formatted(Formatting.WHITE));
} catch (NullPointerException exception) {
LOGGER.warn("Could not load dark mode resource pack!");
}
LOGGER.info("CodeClient, making it easier to wipe your plot and get banned for hacks since 2022");
}
private static void feat(Feature feature) {
features.put(feature.getClass(), feature);
}
private static void loadFeatures() {
features.clear();
feat(new BuildPhaser());
feat(new ChestPeeker());
feat(new Debug());
feat(new RecentChestInsert());
feat(new BlockBreakDeltaCalculator());
feat(new Navigation());
feat(new NoClip());
feat(new ReportBrokenBlocks());
feat(new InsertOverlayFeature());
feat(new SlotGhostManager());
feat(new ActionViewer());
feat(new RecentValues());
feat(new ValueDetails());
feat(new ChatAutoEdit());
feat(new CPUDisplay());
}
/**
* Get all active features.
*/
private static Stream<Feature> features() {
return features.values().stream().filter(Feature::enabled);
}
/**
* Get all active chest features.
*/
private static Stream<ChestFeature> chestFeatures() {
return features().map(Feature::getChest).filter(Optional::isPresent).map(Optional::get);
}
public static void isCodeChest() {
isCodeChest = true;
}
public static <T extends Feature> Optional<T> getFeature(Class<T> clazz) {
var feat = features.get(clazz);
if (feat != null && feat.enabled()) return Optional.of(clazz.cast(feat));
return Optional.empty();
}
public static <T extends PacketListener> boolean handlePacket(Packet<T> packet) {
if (currentAction.onReceivePacket(packet)) return true;
for (var feature : features().toList()) {
if (feature.onReceivePacket(packet)) return true;
}
Event.handlePacket(packet);
LastPos.handlePacket(packet);
//noinspection unused
String name = packet.getClass().getName().replace("net.minecraft.network.packet.s2c.play.", "");
// if(!java.util.List.of("PlayerListS2CPacket","WorldTimeUpdateS2CPacket","GameMessageS2CPacket","KeepAliveS2CPacket", "ChunkDataS2CPacket", "UnloadChunkS2CPacket","TeamS2CPacket", "ChunkRenderDistanceCenterS2CPacket", "MessageHeaderS2CPacket", "LightUpdateS2CPacket", "OverlayMessageS2CPacket").contains(name)) LOGGER.info(name);
if (CodeClient.location instanceof Dev dev) {
try {
if (packet instanceof BlockEntityUpdateS2CPacket beu && dev.isInDev(beu.getPos()) && beu.getBlockEntityType() == BlockEntityType.SIGN) {
dev.clearLineStarterCache();
}
} catch (ConcurrentModificationException exception) {
// Not sure how this comes to happen. My guess it's the getBlockEntity call.
// Unfortunately, I don't know what state the game has to be in to make it fail, maybe an unloaded chunk?
// It's hard to check for that, apparently.
dev.clearLineStarterCache();
}
}
return (MC.currentScreen instanceof GameMenuScreen || MC.currentScreen instanceof ChatScreen || MC.currentScreen instanceof StateSwitcher) && packet instanceof CloseScreenS2CPacket;
}
/**
* All outgoing packet events and debugging.
*
* @param <T> ClientToServer
* @return If the packet shouldn't be sent. True to not send.
*/
public static <T extends PacketListener> boolean onSendPacket(Packet<T> packet) {
if (CodeClient.currentAction.onSendPacket(packet)) return true;
for (var feature : features().toList()) if (feature.onSendPacket(packet)) return true;
Event.onSendPacket(packet);
//noinspection unused
String name = packet.getClass().getName().replace("net.minecraft.network.packet.c2s.play.", "");
// LOGGER.info(name);
return false;
}
public static void onTick() {
currentAction.tick();
features().forEach(Feature::tick);
KeyBinds.tick();
if (!(location instanceof Dev) || !(MC.currentScreen instanceof HandledScreen<?>)) {
isCodeChest = false;
features().forEach(Feature::closeChest);
}
if (location instanceof Dev dev) {
if (MC.player == null) return;
var pos = new BlockPos(dev.getX() - 1, 49, dev.getZ());
if (dev.getSize() == null) {
// TODO wait for plugin messages, or make a fix now.
var world = CodeClient.MC.world;
if (world == null) return;
var FIFTY = world.getBlockState(pos.south(50));
var FIFTY_ONE = world.getBlockState(pos.south(51));
var HUNDRED = world.getBlockState(pos.south(100));
var HUNDRED_ONE = world.getBlockState(pos.south(101));
var THREE_HUNDRED = world.getBlockState(pos.south(300));
var THREE_HUNDRED_ONE = world.getBlockState(pos.south(301));
var MEGA = world.getBlockState(pos.add(-19, 0, 10));
var MEGA_ONE = world.getBlockState(pos.add(-20, 0, 10));
if (MEGA_ONE.isOf(Blocks.GRASS_BLOCK) && MEGA.isOf(Blocks.GRASS_BLOCK)) {
dev.setSize(Plot.Size.MEGA);
} else if (!MEGA.isOf(Blocks.VOID_AIR) && !MEGA_ONE.isOf(Blocks.VOID_AIR) && !MEGA.isOf(Blocks.GRASS_BLOCK) && !MEGA.isOf(Blocks.STONE) && !MEGA_ONE.isOf(Blocks.GRASS_BLOCK)) {
dev.setSize(Plot.Size.MEGA);
} else if (!(FIFTY.isOf(Blocks.VOID_AIR) || FIFTY_ONE.isOf(Blocks.VOID_AIR)) && (!FIFTY.isOf(FIFTY_ONE.getBlock()))) {
dev.setSize(Plot.Size.BASIC);
} else if (!(HUNDRED.isOf(Blocks.VOID_AIR) || HUNDRED_ONE.isOf(Blocks.VOID_AIR)) && !HUNDRED.isOf(HUNDRED_ONE.getBlock())) {
dev.setSize(Plot.Size.LARGE);
} else if (!(THREE_HUNDRED.isOf(Blocks.VOID_AIR) || THREE_HUNDRED_ONE.isOf(Blocks.VOID_AIR)) && !THREE_HUNDRED.isOf(THREE_HUNDRED_ONE.getBlock())) {
dev.setSize(Plot.Size.MASSIVE);
}
}
var size = dev.assumeSize();
assert CodeClient.MC.world != null;
var groundCheck = MC.world.getBlockState(new BlockPos(
Math.max(Math.min((int) MC.player.getX(), dev.getX() - 1), dev.getX() - (size.codeWidth)),
49,
Math.max(Math.min((int) MC.player.getZ(), dev.getZ() + size.codeLength), dev.getZ())
));
if (!groundCheck.isOf(Blocks.VOID_AIR))
dev.setHasUnderground(!groundCheck.isOf(Blocks.GRASS_BLOCK) && !groundCheck.isOf(Blocks.STONE));
}
if (CodeClient.location instanceof Spawn spawn && MC.getNetworkHandler() != null && spawn.consumeHasJustJoined()) {
if (autoJoin == AutoJoin.PLOT) {
MC.getNetworkHandler().sendCommand("join " + Config.getConfig().AutoJoinPlotId);
autoJoin = AutoJoin.NONE;
} else if (Config.getConfig().AutoFly) {
MC.getNetworkHandler().sendCommand("fly");
}
}
}
public static void onRender(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers, double cameraX, double cameraY, double cameraZ) {
features().forEach(feature -> feature.render(matrices, vertexConsumers, cameraX, cameraY, cameraZ));
if (shouldReload) {
MC.worldRenderer.reload();
shouldReload = false;
}
}
public static void onClickChest(BlockHitResult hitResult) {
features().forEach(feature -> feature.onClickChest(hitResult));
}
public static void onBreakBlock(Dev dev, BlockPos pos, BlockPos breakPos) {
features().forEach(feature -> feature.onBreakBlock(dev, pos, breakPos));
}
public static void onScreenInit(HandledScreen<?> screen) {
features().forEach(Feature::closeChest);
if (!isCodeChest) return;
features().forEach(feat -> feat.openChest(screen));
}
public static void onScreenClosed() {
isCodeChest = false;
features().forEach(Feature::closeChest);
}
public static void onRender(DrawContext context, int mouseX, int mouseY, int x, int y, float delta) {
chestFeatures().forEach(feat -> feat.render(context, mouseX, mouseY, x, y, delta));
}
public static void onDrawSlot(DrawContext context, Slot slot) {
chestFeatures().forEach(feat -> feat.drawSlot(context, slot));
}
public static ItemStack onGetHoverStack(Slot instance) {
return chestFeatures().map(feat -> feat.getHoverStack(instance)).filter(Objects::nonNull).findFirst().orElse(null);
}
public static boolean onMouseClicked(double mouseX, double mouseY, int button) {
return chestFeatures().anyMatch(feature -> feature.mouseClicked(mouseX, mouseY, button));
}
public static boolean onKeyPressed(int keyCode, int scanCode, int modifiers) {
return chestFeatures().anyMatch(feature -> feature.keyPressed(keyCode, scanCode, modifiers));
}
public static boolean onKeyReleased(int keyCode, int scanCode, int modifiers) {
return chestFeatures().anyMatch(feature -> feature.keyReleased(keyCode, scanCode, modifiers));
}
public static boolean onCharTyped(char chr, int modifiers) {
return chestFeatures().anyMatch(feature -> feature.charTyped(chr, modifiers));
}
public static void onClickSlot(Slot slot, int button, SlotActionType actionType, int syncId, int revision) {
chestFeatures().forEach(feature -> feature.clickSlot(slot, button, actionType, syncId, revision));
}
public static boolean onMouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
return chestFeatures().anyMatch(feature -> feature.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount));
}
public static boolean noClipOn() {
if (MC.player == null) return false;
if (!Config.getConfig().NoClipEnabled) return false;
if (!(location instanceof Dev)) return false;
if (!(currentAction instanceof None)) return false;
return MC.player.getAbilities().creativeMode;
}
/**
* Remove all state from being on DF.
*/
public static void clean() {
for (var feature : features.values()) {
feature.reset();
}
CodeClient.currentAction = new None();
CodeClient.confirmingAction = null;
CodeClient.location = null;
}
/**
* As much as possible, set CodeClient to its startup state.
*/
public static void reset() {
clean();
loadFeatures();
API.setConnection(null);
ActionDump.clear();
Config.clear();
}
public static void onModeChange(Location location) {
if (Config.getConfig().DevForBuild && (currentAction instanceof None || currentAction instanceof DevForBuild) && location instanceof Build) {
currentAction = new DevForBuild(() -> currentAction = new None());
currentAction.init();
}
currentAction.onModeChange(location);
}
/**
* Gets the mod container needed for registering resources like data packs and resource packs to CodeClient.
*
* @return the mod container.
* @throws NullPointerException the mod's container was not found.
*/
public static ModContainer getModContainer() throws NullPointerException {
var container = FabricLoader.getInstance().getModContainer(CodeClient.MOD_ID);
if (container.isEmpty()) throw new NullPointerException("Could not get mod container.");
return container.get();
}
/**
* Registers a resource pack with a normal activation type.
*
* @param id the resource id
* @param name the resource pack's display name
* @return if the resource pack was created
* @throws NullPointerException if the mod container is not found
*/
private boolean registerResourcePack(String id, Text name) throws NullPointerException {
return registerResourcePack(id, name, ResourcePackActivationType.NORMAL);
}
/**
* Registers a resource pack with the given activation type.
*
* @param id the resource id
* @param name the resource pack's display name
* @param type the resource pack's activation type
* @return if the resource pack was created
* @throws NullPointerException if the mod container is not found
*/
private boolean registerResourcePack(String id, Text name, ResourcePackActivationType type) throws NullPointerException {
var prefix = String.format("[%s] ", MOD_NAME);
return ResourceManagerHelper.registerBuiltinResourcePack(
new Identifier(CodeClient.MOD_ID, id),
getModContainer(),
Text.literal(prefix).formatted(Formatting.GRAY).append(name),
type
);
}
public enum AutoJoin {
/**
* Done or nothing to act on.
*/
NONE,
/**
* If the main menu should take us to the server.
*/
GAME,
/**
* If we need to automatically join the plot.
*/
PLOT
}
}