Skip to content

Commit

Permalink
refactor: split client & server
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Aug 18, 2023
1 parent d4ef4c9 commit 5305359
Show file tree
Hide file tree
Showing 51 changed files with 300 additions and 303 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ dependencies {
include(implementation("org.bouncycastle:bcprov-jdk18on:1.76"))
include(implementation("org.bouncycastle:bcpg-jdk18on:1.76"))
modRuntimeOnly "curse.maven:litematica-308892:4626718"
modRuntimeOnly "maven.modrinth:modmenu:7.2.1"
}

test {
Expand All @@ -54,6 +53,15 @@ base {
}

loom {
splitEnvironmentSourceSets()

mods {
reden {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
}

accessWidenerPath = file("src/main/resources/reden.accesswidener")
}

Expand Down
42 changes: 42 additions & 0 deletions src/client/java/com/github/zly2006/reden/Client.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.zly2006.reden

import com.github.zly2006.reden.debugger.breakpoint.breakpoints
import com.github.zly2006.reden.network.Rollback
import com.github.zly2006.reden.network.TagBlockPos
import com.github.zly2006.reden.network.TntSyncPacket
import com.github.zly2006.reden.pearl.pearlTask
import com.github.zly2006.reden.render.BlockBorder
import com.github.zly2006.reden.utils.debugLogger
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.minecraft.text.Text

fun init() {
ClientPlayNetworking.registerGlobalReceiver(Rollback.pType) { packet, player, res ->
player.sendMessage(
when (packet.status) {
0 -> Text.literal("[Reden/Undo] Rollback success")
1 -> Text.literal("[Reden/Undo] Restore success")
2 -> Text.literal("[Reden/Undo] No blocks info")
16 -> Text.literal("[Reden/Undo] No permission")
32 -> Text.literal("[Reden/Undo] Not recording")
65536 -> Text.literal("[Reden/Undo] Unknown error")
else -> Text.literal("[Reden/Undo] Unknown status")
}
)
}

ClientPlayConnectionEvents.DISCONNECT.register { _, _ ->
breakpoints.clear()
}

ClientPlayNetworking.registerGlobalReceiver(TntSyncPacket.pType) { packet, client, _ ->
pearlTask?.onTntSyncPacket(packet)
debugLogger("TntSyncPacket: TNT${packet.tntPower} @ ${packet.tntPos}")
}

ClientPlayConnectionEvents.DISCONNECT.register { _, _ -> BlockBorder.tags.clear()}
ClientPlayNetworking.registerGlobalReceiver(TagBlockPos.pType) { packet, _, _ ->
BlockBorder.tags[packet.pos.asLong()] = packet.status
}
}
81 changes: 81 additions & 0 deletions src/client/java/com/github/zly2006/reden/RedenClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.github.zly2006.reden;

import com.github.zly2006.reden.malilib.KeyCallbacksKt;
import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import com.github.zly2006.reden.pearl.PearlTask;
import com.github.zly2006.reden.report.ClientReportKt;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import fi.dy.masa.malilib.config.ConfigManager;
import fi.dy.masa.malilib.config.ConfigUtils;
import fi.dy.masa.malilib.config.IConfigHandler;
import fi.dy.masa.malilib.event.InitializationHandler;
import fi.dy.masa.malilib.event.InputEventHandler;
import fi.dy.masa.malilib.hotkeys.IHotkey;
import fi.dy.masa.malilib.hotkeys.IKeybindManager;
import fi.dy.masa.malilib.hotkeys.IKeybindProvider;
import fi.dy.masa.malilib.util.FileUtils;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static com.github.zly2006.reden.Reden.MOD_NAME;

public class RedenClient implements ClientModInitializer {
public static final String CONFIG_FILE = "reden.json";
public static final Logger LOGGER = LoggerFactory.getLogger("reden");
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
@Override
public void onInitializeClient() {
ClientReportKt.initReport();
PearlTask.Companion.register();
InitializationHandler.getInstance().registerInitializationHandler(() -> {
ConfigManager.getInstance().registerConfigHandler("reden", new IConfigHandler() {
@Override
public void load() {
try {
File file = new File(FileUtils.getConfigDirectory(), CONFIG_FILE);
if (!file.exists()) {
return;
}
JsonObject jo = GSON.fromJson(Files.readString(file.toPath()), JsonObject.class);
ConfigUtils.readConfigBase(jo, MOD_NAME, MalilibSettingsKt.getAllOptions());
} catch (IOException e) {
save();
}
}

@Override
public void save() {
JsonObject jo = new JsonObject();
ConfigUtils.writeConfigBase(jo, MOD_NAME, MalilibSettingsKt.getAllOptions());
try {
Files.writeString(new File(FileUtils.getConfigDirectory(), CONFIG_FILE).toPath(), GSON.toJson(jo));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
InputEventHandler.getKeybindManager().registerKeybindProvider(new IKeybindProvider() {
@Override
public void addKeysToMap(IKeybindManager iKeybindManager) {
MalilibSettingsKt.HOTKEYS.stream()
.map(IHotkey::getKeybind)
.forEach(iKeybindManager::addKeybindToMap);
}

@Override
public void addHotkeys(IKeybindManager iKeybindManager) {
iKeybindManager.addHotkeysForCategory("Reden", "reden.hotkeys.category.generic_hotkeys", MalilibSettingsKt.HOTKEYS);
}
});
KeyCallbacksKt.configureKeyCallbacks(MinecraftClient.getInstance());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.chat;
package com.github.zly2006.reden.mixin.client.chat;

import com.github.zly2006.reden.access.VisibleChatHudLineAccess;
import net.minecraft.client.gui.hud.ChatHud;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.chat;
package com.github.zly2006.reden.mixin.client.chat;

import com.github.zly2006.reden.access.VisibleChatHudLineAccess;
import com.github.zly2006.reden.gui.QuickMenuWidget;
Expand Down Expand Up @@ -33,7 +33,7 @@
@Mixin(ChatScreen.class)
public abstract class ChatScreenMixin extends Screen {
@Unique QuickMenuWidget quickMenuWidget;
private static final Pattern urlPattern = Pattern.compile("(https?://)?[a-zA-Z0-9\\-.]+\\.[a-zA-Z]{2,8}(/\\S*)?");
private static final @Unique Pattern urlPattern = Pattern.compile("(https?://)?[a-zA-Z0-9\\-.]+\\.[a-zA-Z]{2,8}(/\\S*)?");

protected ChatScreenMixin(Text title) {
super(title);
Expand Down Expand Up @@ -74,6 +74,7 @@ protected ChatScreenMixin(Text title) {
}
}

@Unique
private void rightClickMenu(int mouseX, int mouseY, MinecraftClient client, Text text) {
if (quickMenuWidget != null) {
quickMenuWidget.remove();
Expand Down Expand Up @@ -108,6 +109,13 @@ private void rightClickMenu(int mouseX, int mouseY, MinecraftClient client, Text
if (style.getHoverEvent() != null) {
HoverEvent.Action<?> action = style.getHoverEvent().getAction();
if (action == HoverEvent.Action.SHOW_TEXT) {
quickMenuWidget.addEntry(Text.literal("Copy Hover Text"), (entry, button) -> {
Text hoverText = style.getHoverEvent().getValue(HoverEvent.Action.SHOW_TEXT);
if (hoverText != null) {
client.keyboard.setClipboard((hoverText).getString());
entry.setName(Text.literal("Copied"));
}
});
quickMenuWidget.addEntry(Text.literal("Copy Hover Raw"), (entry, button) -> {
Text hoverText = style.getHoverEvent().getValue(HoverEvent.Action.SHOW_TEXT);
client.keyboard.setClipboard(Text.Serializer.toJson(hoverText));
Expand Down Expand Up @@ -157,6 +165,7 @@ private void rightClickMenu(int mouseX, int mouseY, MinecraftClient client, Text
addDrawable(quickMenuWidget);
}

@Unique
private ChatHudLine.Visible ct$geMessageAt(double x, double y) {
ChatHud chatHud = MinecraftClient.getInstance().inGameHud.getChatHud();
double d = chatHud.toChatLineX(x);
Expand All @@ -169,7 +178,7 @@ private void rightClickMenu(int mouseX, int mouseY, MinecraftClient client, Text

@Inject(method = "keyPressed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;keyPressed(III)Z"), cancellable = true)
private void ct$keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if (ChatScreen.hasControlDown()) {
if (hasControlDown()) {
if (keyCode == GLFW.GLFW_KEY_UP) {
MinecraftClient.getInstance().inGameHud.getChatHud().scroll(1);
cir.setReturnValue(true);
Expand All @@ -181,6 +190,7 @@ private void rightClickMenu(int mouseX, int mouseY, MinecraftClient client, Text
}
}

@Unique
private ChatScreen ct$getThis() {
return (ChatScreen) (Object) this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.chat;
package com.github.zly2006.reden.mixin.client.chat;

import com.github.zly2006.reden.access.VisibleChatHudLineAccess;
import net.minecraft.client.gui.hud.ChatHudLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.fix.forceSyncEntityPos;
package com.github.zly2006.reden.mixin.client.forceSyncEntityPos;

import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import net.minecraft.client.network.ClientPlayNetworkHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.fix.forceSyncEntityPos;
package com.github.zly2006.reden.mixin.client.forceSyncEntityPos;

import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import net.minecraft.entity.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.fix.forceSyncEntityPos;
package com.github.zly2006.reden.mixin.client.forceSyncEntityPos;

import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import net.minecraft.client.render.VertexConsumerProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.render;
package com.github.zly2006.reden.mixin.client.render;

import net.minecraft.block.BlockState;
import net.minecraft.client.render.VertexConsumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.render;
package com.github.zly2006.reden.mixin.client.render;

import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import com.github.zly2006.reden.render.StructureOutline;
Expand Down Expand Up @@ -46,7 +46,7 @@ private void capturePos(float cameraX, float cameraY, float cameraZ, BlockBuffer



boolean hasOutline = pos != null && StructureOutline.INSTANCE.getSet$reden_is_what_we_made().contains(pos);
boolean hasOutline = pos != null && StructureOutline.INSTANCE.getSet().contains(pos);

if (true) return;
if (hasOutline || true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.zly2006.reden.mixin.render;
package com.github.zly2006.reden.mixin.client.render;

import net.minecraft.client.render.Camera;
import net.minecraft.client.render.GameRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.minecraft.util.math.BlockPos

@Environment(EnvType.CLIENT)
object StructureOutline {
internal val set = mutableSetOf<BlockPos>()
val set = mutableSetOf<BlockPos>()

init {
}
Expand Down
8 changes: 8 additions & 0 deletions src/client/java/com/github/zly2006/reden/utils/ClientUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.zly2006.reden.utils

import net.minecraft.client.gui.widget.ButtonWidget
import net.minecraft.text.Text


fun buttonWidget(x: Int, y: Int, width: Int, height: Int, message: Text, onPress: ButtonWidget.PressAction) =
ButtonWidget(x, y, width, height, message, onPress) { it.get() }
27 changes: 27 additions & 0 deletions src/client/resources/reden.client.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.github.zly2006.reden.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"client.chat.ChatHudMixin",
"client.chat.ChatScreenMixin",
"client.chat.VisibleChatHudLineMixin",
"client.forceSyncEntityPos.MixinClientNetwork",
"client.forceSyncEntityPos.MixinWorldRenderer",
"client.render.MixinBlockModelRenderer",
"client.render.MixinChunkRebuildTask",
"client.render.MixinWorldRenderer",
"fix.NoTimeOut",
"fix.forceSyncEntityPos.MixinWorldRenderer",
"render.MixinBlockModelRenderer",
"render.MixinChunkRebuildTask",
"render.MixinWorldRenderer"
],
"injectors": {
"defaultRequire": 1
},
"mixins": [
"client.forceSyncEntityPos.MixinTracker"
]
}
Loading

0 comments on commit 5305359

Please sign in to comment.