diff --git a/README.md b/README.md index 03d8767..ba7de1a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ As a challenge I'm trying to make it as user-friendly as possible. ## Server Side -### Features - Custom join message ![Join Message](https://i.imgur.com/7uv5lUb.png) - Custom quit message @@ -29,6 +28,7 @@ As a challenge I'm trying to make it as user-friendly as possible. - Open/close EC block while opening/closing SEC - Play open & close sounds - Vein miner +Code inspired by Inferis! ![VeinMiner](https://i.imgur.com/zOXWMNa.gif) - Chat Calculator @@ -47,14 +47,46 @@ As a challenge I'm trying to make it as user-friendly as possible. - Automatic config adaption (currently booleans only) - Remove darkness effect - Toggleable -- Keybinding for /camera +- Keybinding for /spectator + +# Currently working on + +## Server Side +- [x] Chat Calculator + - [x] Check if operator is present before calculating +- Vein Miner + - [x] Configurable animation (tick delay) + - [x] Leaves veinmineable using shears +- [x] Replenish + + +## Client Side + +- [x] Render block entities from a longer range + - [x] Toggleable via GUI + - [x] Configurable value + - [x] In GUI +- [x] Rework Mod Menu integration to be more flexible +DISCLAIMER: this is NOT perfect and still needs to be reworked, I'm just too lazy right now... + - The following types are accepted: + - String + - Boolean + - Float + - Integer +- [x] Zoom + - [x] Configurable + - Smooth zoom + - Initial zoom value + - Zoom step value + - Scroll to zoom further # To-do ## General - Rework config system - - Store server settings in world folder for better singleplayer use - - Rework Mod Menu integration to be more flexible +- Store server settings in world folder for better singleplayer use + +## Client side ## Server Side - Telekinesis diff --git a/gradle.properties b/gradle.properties index 1ad2457..f5ca1dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.21.4+build.8 loader_version=0.16.10 # Mod Properties -mod_version=1.4.0 +mod_version=1.4.1 maven_group=wtf.hak.survivalfabric archives_base_name=survivalfabric diff --git a/src/client/java/wtf/hak/survivalfabric/SurvivalFabricClient.java b/src/client/java/wtf/hak/survivalfabric/SurvivalFabricClient.java index 099d664..6af5e0a 100644 --- a/src/client/java/wtf/hak/survivalfabric/SurvivalFabricClient.java +++ b/src/client/java/wtf/hak/survivalfabric/SurvivalFabricClient.java @@ -4,7 +4,8 @@ import net.fabricmc.api.ClientModInitializer; import wtf.hak.survivalfabric.config.client.ClientConfigManager; import wtf.hak.survivalfabric.features.AngleViewer; import wtf.hak.survivalfabric.features.RemoveDarknessEffect; -import wtf.hak.survivalfabric.features.SFKeyBindings; +import wtf.hak.survivalfabric.features.CameraShortcut; +import wtf.hak.survivalfabric.features.Zoom; public class SurvivalFabricClient implements ClientModInitializer { @@ -17,7 +18,7 @@ public class SurvivalFabricClient implements ClientModInitializer { // Features AngleViewer.register(); RemoveDarknessEffect.register(); - SFKeyBindings.register(); - + CameraShortcut.register(); + Zoom.register(); } } \ No newline at end of file diff --git a/src/client/java/wtf/hak/survivalfabric/config/client/ClientConfig.java b/src/client/java/wtf/hak/survivalfabric/config/client/ClientConfig.java index d9f5cb5..c4ae18f 100644 --- a/src/client/java/wtf/hak/survivalfabric/config/client/ClientConfig.java +++ b/src/client/java/wtf/hak/survivalfabric/config/client/ClientConfig.java @@ -1,7 +1,7 @@ package wtf.hak.survivalfabric.config.client; public class ClientConfig { - public String configVersion = "1.0"; + public String configVersion = "1.1"; public boolean renderNetherFog = false; public boolean renderOverworldFog = false; @@ -11,4 +11,10 @@ public class ClientConfig { public boolean renderSnowFog = false; public boolean removeDarknessEffect = true; public boolean lockTeleportHeadMovement = true; + public boolean manipulateBlockEntityDistance = true; + public int blockEntityRange = 512; + public boolean smoothCamera = true; + public float initialZoom = 20f; + public boolean scrollToZoom = true; + public float zoomStep = 2.5f; } diff --git a/src/client/java/wtf/hak/survivalfabric/features/SFKeyBindings.java b/src/client/java/wtf/hak/survivalfabric/features/CameraShortcut.java similarity index 91% rename from src/client/java/wtf/hak/survivalfabric/features/SFKeyBindings.java rename to src/client/java/wtf/hak/survivalfabric/features/CameraShortcut.java index 73df70b..e6c072a 100644 --- a/src/client/java/wtf/hak/survivalfabric/features/SFKeyBindings.java +++ b/src/client/java/wtf/hak/survivalfabric/features/CameraShortcut.java @@ -6,13 +6,13 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; import org.lwjgl.glfw.GLFW; -public class SFKeyBindings { +public class CameraShortcut { private static final KeyBinding CAMERA_BIND = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.survivalfabric.camera", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_PERIOD, "category.survivalfabric.survivalfabric")); public static void register() { ClientTickEvents.END_CLIENT_TICK.register(client -> { - if(client.player != null) { + if (client.player != null) { if (CAMERA_BIND.wasPressed()) { client.player.networkHandler.sendChatCommand("camera"); } diff --git a/src/client/java/wtf/hak/survivalfabric/features/Zoom.java b/src/client/java/wtf/hak/survivalfabric/features/Zoom.java new file mode 100644 index 0000000..d5d45f1 --- /dev/null +++ b/src/client/java/wtf/hak/survivalfabric/features/Zoom.java @@ -0,0 +1,63 @@ +package wtf.hak.survivalfabric.features; + +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; +import org.lwjgl.glfw.GLFW; +import org.spongepowered.asm.mixin.Unique; + +import static wtf.hak.survivalfabric.config.client.ClientConfigManager.getConfig; + +public class Zoom { + + private static final KeyBinding ZOOM_BIND = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.survivalfabric.zoom", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_C, "category.survivalfabric.survivalfabric")); + + private static boolean SHOULD_ZOOM = false; + private static int ZOOM_STEP = 0; + + @Unique + private static boolean initialSmoothZoom; + + public static void register() { + ClientTickEvents.END_CLIENT_TICK.register(client -> { + + if (ZOOM_BIND.isPressed() && !SHOULD_ZOOM) { + if(getConfig().smoothCamera) { + initialSmoothZoom = MinecraftClient.getInstance().options.smoothCameraEnabled; + MinecraftClient.getInstance().options.smoothCameraEnabled = true; + } + SHOULD_ZOOM = true; + } else if (!ZOOM_BIND.isPressed() && SHOULD_ZOOM) { + SHOULD_ZOOM = false; + ZOOM_STEP = 0; + if(getConfig().smoothCamera) { + MinecraftClient.getInstance().options.smoothCameraEnabled = initialSmoothZoom; + } + } + + }); + } + + public static boolean isZooming() { + return SHOULD_ZOOM; + } + + public static float getZoomFov() { + return getConfig().initialZoom - -ZOOM_STEP * getConfig().zoomStep; + } + + public static void modifyStep(int step) { + ZOOM_STEP += step; + + // Clamp the zoom level so the FOV stays within [1, 110] + float zoomFov = getZoomFov(); + if (zoomFov < 1) { + ZOOM_STEP = Math.round((1 - getConfig().initialZoom) / getConfig().zoomStep) + 1; + } else if (zoomFov > 110) { + ZOOM_STEP = Math.round((110 - getConfig().initialZoom) / getConfig().zoomStep); + } + } +} diff --git a/src/client/java/wtf/hak/survivalfabric/mixin/client/BackgroundRendererMixin.java b/src/client/java/wtf/hak/survivalfabric/mixin/client/BackgroundRendererMixin.java index 0c42d31..437feac 100644 --- a/src/client/java/wtf/hak/survivalfabric/mixin/client/BackgroundRendererMixin.java +++ b/src/client/java/wtf/hak/survivalfabric/mixin/client/BackgroundRendererMixin.java @@ -16,7 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import static wtf.hak.survivalfabric.config.client.ClientConfigManager.getConfig; @Mixin(value = BackgroundRenderer.class, priority = 910) -public abstract class BackgroundRendererMixin { +public class BackgroundRendererMixin { @Unique private static final Fog EMPTY_FOG = new Fog(-8.0f, 1_000_000.0F, FogShape.CYLINDER, 0, 0, 0, 0); diff --git a/src/client/java/wtf/hak/survivalfabric/mixin/client/BlockEntityRendererMixin.java b/src/client/java/wtf/hak/survivalfabric/mixin/client/BlockEntityRendererMixin.java new file mode 100644 index 0000000..21369b8 --- /dev/null +++ b/src/client/java/wtf/hak/survivalfabric/mixin/client/BlockEntityRendererMixin.java @@ -0,0 +1,19 @@ +package wtf.hak.survivalfabric.mixin.client; + +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.client.render.block.entity.BlockEntityRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import wtf.hak.survivalfabric.config.client.ClientConfigManager; + +@Mixin(BlockEntityRenderer.class) +public interface BlockEntityRendererMixin { + + @Inject(method = "getRenderDistance", at = @At("HEAD"), cancellable = true) + private void getRenderDistance(CallbackInfoReturnable cir) { + if (ClientConfigManager.getConfig().manipulateBlockEntityDistance) + cir.setReturnValue(ClientConfigManager.getConfig().blockEntityRange); + } +} diff --git a/src/client/java/wtf/hak/survivalfabric/mixin/client/GameRendererMixin.java b/src/client/java/wtf/hak/survivalfabric/mixin/client/GameRendererMixin.java new file mode 100644 index 0000000..357cb74 --- /dev/null +++ b/src/client/java/wtf/hak/survivalfabric/mixin/client/GameRendererMixin.java @@ -0,0 +1,21 @@ +package wtf.hak.survivalfabric.mixin.client; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.client.render.GameRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import wtf.hak.survivalfabric.features.Zoom; + +@Mixin(GameRenderer.class) +public class GameRendererMixin { + + /** + * Modify Zoom FOV + */ + @ModifyReturnValue(method = "getFov", at = @At("RETURN")) + private float modifyFovWithZoom(float fov, @Local(argsOnly = true) float tickDelta) { + return Zoom.isZooming() ? Zoom.getZoomFov() : fov; + } + +} diff --git a/src/client/java/wtf/hak/survivalfabric/mixin/client/MouseMixin.java b/src/client/java/wtf/hak/survivalfabric/mixin/client/MouseMixin.java new file mode 100644 index 0000000..2f6074a --- /dev/null +++ b/src/client/java/wtf/hak/survivalfabric/mixin/client/MouseMixin.java @@ -0,0 +1,33 @@ +package wtf.hak.survivalfabric.mixin.client; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.Mouse; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import wtf.hak.survivalfabric.features.Zoom; + +import static wtf.hak.survivalfabric.config.client.ClientConfigManager.getConfig; + +@Mixin(Mouse.class) +public class MouseMixin { + + /** + * Scrolling listener for zooming + */ + @Inject(method = "onMouseScroll", at = @At("HEAD"), cancellable = true) + private void onMouseScroll(long window, double horizontal, double vertical, CallbackInfo ci) { + if (Zoom.isZooming() && getConfig().scrollToZoom) { + if (MinecraftClient.getInstance().player != null) { + if (vertical > 0) + Zoom.modifyStep(-1); + else + Zoom.modifyStep(1); + } + ci.cancel(); + } + } + + +} diff --git a/src/client/java/wtf/hak/survivalfabric/modmenu/ConfigScreen.java b/src/client/java/wtf/hak/survivalfabric/modmenu/ConfigScreen.java index 6b4843f..209bd99 100644 --- a/src/client/java/wtf/hak/survivalfabric/modmenu/ConfigScreen.java +++ b/src/client/java/wtf/hak/survivalfabric/modmenu/ConfigScreen.java @@ -3,15 +3,29 @@ package wtf.hak.survivalfabric.modmenu; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.text.Text; +import net.minecraft.util.math.MathHelper; import wtf.hak.survivalfabric.config.client.ClientConfig; import wtf.hak.survivalfabric.config.client.ClientConfigManager; import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; public class ConfigScreen extends Screen { + private static final int OPTION_HEIGHT = 25; + private static final int SCROLL_BAR_WIDTH = 6; + private static final int TOP_PADDING = 40; + private static final int BOTTOM_PADDING = 35; + private static final int SIDE_PADDING = 20; private final Screen parent; + private final List> options = new ArrayList<>(); + private TextFieldWidget activeTextField = null; + private float scrollPosition = 0.0F; + private boolean scrolling = false; + private int contentHeight = 0; public ConfigScreen(Screen parent) { super(Text.literal("Survival Fabric - Client Config")); @@ -20,55 +34,272 @@ public class ConfigScreen extends Screen { @Override protected void init() { - int buttonWidth = 200; - int buttonHeight = 20; - int spacing = 5; - int startY = 40; - int i = 0; + options.clear(); + int listWidth = this.width - (SIDE_PADDING * 2); + int listHeight = this.height - TOP_PADDING - BOTTOM_PADDING; + int listLeft = SIDE_PADDING; + int listTop = TOP_PADDING; + + int index = 0; for (Field field : ClientConfig.class.getFields()) { - if (field.getType() == boolean.class) { - int y = startY + i * (buttonHeight + spacing); - try { + try { + Class type = field.getType(); + String name = formatFieldName(field.getName()); + + ConfigOption option = null; + if (type == boolean.class) { boolean value = field.getBoolean(ClientConfigManager.getConfig()); - String label = formatFieldName(field.getName()) + ": " + (value ? "ON" : "OFF"); - - ButtonWidget button = ButtonWidget.builder( - Text.literal(label), - b -> { - try { - boolean current = field.getBoolean(ClientConfigManager.getConfig()); - field.setBoolean(ClientConfigManager.getConfig(), !current); - b.setMessage(Text.literal(formatFieldName(field.getName()) + ": " + (!current ? "ON" : "OFF"))); - ClientConfigManager.save(); // Save if needed - } catch (Exception e) { - e.printStackTrace(); - } - } - ).dimensions(this.width / 2 - buttonWidth / 2, y, buttonWidth, buttonHeight).build(); - - this.addDrawableChild(button); - i++; - } catch (Exception e) { - e.printStackTrace(); + option = new BooleanConfigOption( + name, + field, + value, + listLeft, + listTop + (index * OPTION_HEIGHT) - (int) scrollPosition, + listWidth + ); + } else if (type == int.class) { + int value = field.getInt(ClientConfigManager.getConfig()); + option = new IntegerConfigOption( + name, + field, + value, + listLeft, + listTop + (index * OPTION_HEIGHT) - (int) scrollPosition, + listWidth + ); + } else if (type == float.class) { + float value = field.getFloat(ClientConfigManager.getConfig()); + option = new FloatConfigOption( + name, + field, + value, + listLeft, + listTop + (index * OPTION_HEIGHT) - (int) scrollPosition, + listWidth + ); + } else if (type == String.class) { + String value = (String) field.get(ClientConfigManager.getConfig()); + option = new StringConfigOption( + name, + field, + value, + listLeft, + listTop + (index * OPTION_HEIGHT) - (int) scrollPosition, + listWidth + ); } + + if (option != null) { + options.add(option); + index++; + } + } catch (Exception e) { + e.printStackTrace(); } } + for (ConfigOption option : options) { + if (option instanceof NumericConfigOption) { + ((NumericConfigOption) option).createTextField(this.client); + } + } + + contentHeight = options.size() * OPTION_HEIGHT; + this.addDrawableChild(ButtonWidget.builder( Text.translatable("gui.done"), button -> this.client.setScreen(parent) - ).dimensions(this.width / 2 - 75, startY + i * (buttonHeight + spacing) + 10, 150, 20).build()); + ).dimensions(this.width / 2 - 100, this.height - 27, 200, 20).build()); } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { - context.fill(0, 0, this.width, this.height, 0xC0101010); + this.renderBackground(context, mouseX, mouseY, delta); int titleX = (this.width / 2) - (this.textRenderer.getWidth(this.title) / 2); - context.drawTextWithShadow(this.textRenderer, this.title, titleX, 20, 0xFFFFFF); + context.drawText(this.textRenderer, this.title, titleX, 15, 0xFFFFFF, true); - super.render(context, mouseX, mouseY, delta); + int listWidth = this.width - (SIDE_PADDING * 2); + int listHeight = this.height - TOP_PADDING - BOTTOM_PADDING; + int listLeft = SIDE_PADDING; + int listTop = TOP_PADDING; + + context.drawBorder(listLeft, listTop, listLeft + listWidth, listTop + listHeight, 0x00000000); + + context.enableScissor( + listLeft, + listTop, + listLeft + listWidth, + listTop + listHeight + ); + + for (int i = 0; i < options.size(); i++) { + ConfigOption option = options.get(i); + option.y = listTop + (i * OPTION_HEIGHT) - (int) scrollPosition; + + if (option instanceof NumericConfigOption) { + ((NumericConfigOption) option).updateTextFieldPosition(); + } + + if (option.y < listTop + listHeight && option.y + OPTION_HEIGHT > listTop) { + option.render(context, mouseX, mouseY, delta); + } + } + + context.disableScissor(); + + if (contentHeight > listHeight) { + int scrollBarHeight = Math.max(20, (int) ((float) listHeight / (float) contentHeight * listHeight)); + int scrollBarY = listTop + (int) ((scrollPosition / (contentHeight - listHeight)) * (listHeight - scrollBarHeight)); + + context.fill( + listLeft + listWidth + 2, + listTop, + listLeft + listWidth + 2 + SCROLL_BAR_WIDTH, + listTop + listHeight, + 0xFF404040 + ); + + context.fill( + listLeft + listWidth + 2, + scrollBarY, + listLeft + listWidth + 2 + SCROLL_BAR_WIDTH, + scrollBarY + scrollBarHeight, + scrolling ? 0xFFAAAAAA : 0xFF808080 + ); + } + + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + int listWidth = this.width - (SIDE_PADDING * 2); + int listHeight = this.height - TOP_PADDING - BOTTOM_PADDING; + int listLeft = SIDE_PADDING; + int listTop = TOP_PADDING; + + boolean handledByTextField = false; + + activeTextField = null; + + if (mouseX >= listLeft && + mouseX <= listLeft + listWidth && + mouseY >= listTop && + mouseY <= listTop + listHeight) { + + for (ConfigOption option : options) { + if (option instanceof NumericConfigOption numOption && + option.y >= listTop && + option.y + OPTION_HEIGHT <= listTop + listHeight) { + + TextFieldWidget textField = numOption.getTextField(); + + if (textField.isMouseOver(mouseX, mouseY)) { + textField.setFocused(true); + activeTextField = textField; + handledByTextField = true; + + for (ConfigOption otherOption : options) { + if (otherOption instanceof NumericConfigOption && otherOption != option) { + ((NumericConfigOption) otherOption).getTextField().setFocused(false); + } + } + + return true; + } else { + textField.setFocused(false); + } + } + } + } + + if (contentHeight > listHeight && + mouseX >= listLeft + listWidth + 2 && + mouseX <= listLeft + listWidth + 2 + SCROLL_BAR_WIDTH && + mouseY >= listTop && + mouseY <= listTop + listHeight) { + + scrolling = true; + return true; + } + + // Check if clicked on an option + if (mouseX >= listLeft && + mouseX <= listLeft + listWidth && + mouseY >= listTop && + mouseY <= listTop + listHeight) { + + for (ConfigOption option : options) { + if (option.isMouseOver(mouseX, mouseY) && option.y >= listTop && option.y + OPTION_HEIGHT <= listTop + listHeight) { + option.onClick(mouseX, mouseY); + return true; + } + } + } + + return super.mouseClicked(mouseX, mouseY, button); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (activeTextField != null && activeTextField.isFocused()) { + return activeTextField.keyPressed(keyCode, scanCode, modifiers); + } + + return super.keyPressed(keyCode, scanCode, modifiers); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + if (activeTextField != null && activeTextField.isFocused()) { + return activeTextField.charTyped(chr, modifiers); + } + + return super.charTyped(chr, modifiers); + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + scrolling = false; + return super.mouseReleased(mouseX, mouseY, button); + } + + @Override + public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { + int listHeight = this.height - TOP_PADDING - BOTTOM_PADDING; + int listTop = TOP_PADDING; + + if (scrolling && contentHeight > listHeight) { + float scrollAmount = (float) deltaY / (listHeight - Math.max(20, (int) ((float) listHeight / (float) contentHeight * listHeight))); + float maxScroll = contentHeight - listHeight; + + scrollPosition = MathHelper.clamp(scrollPosition + scrollAmount * maxScroll, 0.0F, maxScroll); + return true; + } + + return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + int listWidth = this.width - (SIDE_PADDING * 2); + int listHeight = this.height - TOP_PADDING - BOTTOM_PADDING; + int listLeft = SIDE_PADDING; + int listTop = TOP_PADDING; + + if (mouseX >= listLeft && + mouseX <= listLeft + listWidth && + mouseY >= listTop && + mouseY <= listTop + listHeight && + contentHeight > listHeight) { + + float maxScroll = contentHeight - listHeight; + scrollPosition = MathHelper.clamp(scrollPosition - (float) verticalAmount * 10, 0.0F, maxScroll); + return true; + } + + return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); } private String formatFieldName(String rawName) { @@ -84,4 +315,285 @@ public class ConfigScreen extends Screen { return result.toString(); } -} + /** + * Base class for all config options + */ + private abstract class ConfigOption { + protected final String name; + protected final Field field; + protected final int width; + protected T value; + protected int x; + protected int y; + + public ConfigOption(String name, Field field, T initialValue, int x, int y, int width) { + this.name = name; + this.field = field; + this.value = initialValue; + this.x = x; + this.y = y; + this.width = width; + } + + public abstract void render(DrawContext context, int mouseX, int mouseY, float delta); + + public abstract void onClick(double mouseX, double mouseY); + + public boolean isMouseOver(double mouseX, double mouseY) { + return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + OPTION_HEIGHT; + } + + protected abstract void saveValue(); + } + + /** + * Implementation for boolean config options + */ + private class BooleanConfigOption extends ConfigOption { + private static final int BUTTON_WIDTH = 40; + private static final int BUTTON_HEIGHT = 20; + + public BooleanConfigOption(String name, Field field, Boolean initialValue, int x, int y, int width) { + super(name, field, initialValue, x, y, width); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + context.drawText(textRenderer, name, x + 5, y + (OPTION_HEIGHT - 8) / 2, 0xFFFFFF, true); + + int buttonX = x + width - BUTTON_WIDTH - 5; + int buttonY = y + (OPTION_HEIGHT - BUTTON_HEIGHT) / 2; + + boolean hovered = isButtonHovered(mouseX, mouseY); + int buttonColor = hovered ? 0xFF404040 : 0xFF303030; + int buttonBorder = hovered ? 0xFFCCCCCC : 0xFF808080; + + context.fill(buttonX, buttonY, buttonX + BUTTON_WIDTH, buttonY + BUTTON_HEIGHT, buttonBorder); + context.fill(buttonX + 1, buttonY + 1, buttonX + BUTTON_WIDTH - 1, buttonY + BUTTON_HEIGHT - 1, buttonColor); + + String buttonText = value ? "true" : "false"; + int textColor = value ? 0x00be00 : 0xbe0000; + int textWidth = textRenderer.getWidth(buttonText); + context.drawText( + textRenderer, + buttonText, + buttonX + (BUTTON_WIDTH - textWidth) / 2, + buttonY + (BUTTON_HEIGHT - 8) / 2, + textColor, + true + ); + } + + public boolean isButtonHovered(double mouseX, double mouseY) { + int buttonX = x + width - BUTTON_WIDTH - 5; + int buttonY = y + (OPTION_HEIGHT - BUTTON_HEIGHT) / 2; + return mouseX >= buttonX && mouseX <= buttonX + BUTTON_WIDTH && + mouseY >= buttonY && mouseY <= buttonY + BUTTON_HEIGHT; + } + + @Override + public void onClick(double mouseX, double mouseY) { + if (isButtonHovered(mouseX, mouseY)) { + value = !value; + saveValue(); + } + } + + @Override + protected void saveValue() { + try { + // Update config and save + field.setBoolean(ClientConfigManager.getConfig(), value); + ClientConfigManager.save(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Implementation for String config options + */ + private class StringConfigOption extends ConfigOption { + private static final int FIELD_WIDTH = 60; + private static final int FIELD_HEIGHT = 16; + protected TextFieldWidget textField; + + public StringConfigOption(String name, Field field, String initialValue, int x, int y, int width) { + super(name, field, initialValue, x, y, width); + } + + public void createTextField(net.minecraft.client.MinecraftClient client) { + int fieldX = x + width - FIELD_WIDTH - 5; + int fieldY = y + (OPTION_HEIGHT - FIELD_HEIGHT) / 2; + + textField = new TextFieldWidget( + textRenderer, + fieldX, + fieldY, + FIELD_WIDTH, + FIELD_HEIGHT, + Text.literal("") + ); + + textField.setText(value); + textField.setMaxLength(10); + textField.setChangedListener(this::onTextChanged); + } + + public TextFieldWidget getTextField() { + return textField; + } + + public void updateTextFieldPosition() { + if (textField != null) { + int fieldX = x + width - FIELD_WIDTH - 5; + int fieldY = y + (OPTION_HEIGHT - FIELD_HEIGHT) / 2; + textField.setX(fieldX); + textField.setY(fieldY); + } + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + context.drawText(textRenderer, name, x + 5, y + (OPTION_HEIGHT - 8) / 2, 0xFFFFFF, true); + + if (textField != null) { + textField.render(context, mouseX, mouseY, delta); + } + } + + @Override + public void onClick(double mouseX, double mouseY) { + } + + public void onTextChanged(String text) { + try { + value = text; + saveValue(); + } catch (NumberFormatException e) {} + } + + @Override + protected void saveValue() { + try { + field.set(ClientConfigManager.getConfig(), value); + ClientConfigManager.save(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Base class for numeric config options (int and float) + */ + private abstract class NumericConfigOption extends ConfigOption { + private static final int FIELD_WIDTH = 60; + private static final int FIELD_HEIGHT = 16; + protected TextFieldWidget textField; + + public NumericConfigOption(String name, Field field, T initialValue, int x, int y, int width) { + super(name, field, initialValue, x, y, width); + } + + public void createTextField(net.minecraft.client.MinecraftClient client) { + int fieldX = x + width - FIELD_WIDTH - 5; + int fieldY = y + (OPTION_HEIGHT - FIELD_HEIGHT) / 2; + + textField = new TextFieldWidget( + textRenderer, + fieldX, + fieldY, + FIELD_WIDTH, + FIELD_HEIGHT, + Text.literal("") + ); + + textField.setText(value.toString()); + textField.setMaxLength(10); + textField.setChangedListener(this::onTextChanged); + } + + public TextFieldWidget getTextField() { + return textField; + } + + public void updateTextFieldPosition() { + if (textField != null) { + int fieldX = x + width - FIELD_WIDTH - 5; + int fieldY = y + (OPTION_HEIGHT - FIELD_HEIGHT) / 2; + textField.setX(fieldX); + textField.setY(fieldY); + } + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + context.drawText(textRenderer, name, x + 5, y + (OPTION_HEIGHT - 8) / 2, 0xFFFFFF, true); + + if (textField != null) { + textField.render(context, mouseX, mouseY, delta); + } + } + + @Override + public void onClick(double mouseX, double mouseY) {} + + protected abstract void onTextChanged(String text); + } + + /** + * Implementation for integer config options + */ + private class IntegerConfigOption extends NumericConfigOption { + public IntegerConfigOption(String name, Field field, Integer initialValue, int x, int y, int width) { + super(name, field, initialValue, x, y, width); + } + + @Override + protected void onTextChanged(String text) { + try { + value = Integer.parseInt(text); + saveValue(); + } catch (NumberFormatException e) {} + } + + @Override + protected void saveValue() { + try { + field.setInt(ClientConfigManager.getConfig(), value); + ClientConfigManager.save(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Implementation for float config options + */ + private class FloatConfigOption extends NumericConfigOption { + public FloatConfigOption(String name, Field field, Float initialValue, int x, int y, int width) { + super(name, field, initialValue, x, y, width); + } + + @Override + protected void onTextChanged(String text) { + try { + value = Float.parseFloat(text); + saveValue(); + } catch (NumberFormatException e) {} + } + + @Override + protected void saveValue() { + try { + field.setFloat(ClientConfigManager.getConfig(), value); + ClientConfigManager.save(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/src/client/resources/survivalfabric.client.mixins.json b/src/client/resources/survivalfabric.client.mixins.json index 6e215a6..109f1b8 100644 --- a/src/client/resources/survivalfabric.client.mixins.json +++ b/src/client/resources/survivalfabric.client.mixins.json @@ -4,7 +4,10 @@ "compatibilityLevel": "JAVA_21", "client": [ "BackgroundRendererMixin", - "EntityMixin" + "BlockEntityRendererMixin", + "EntityMixin", + "GameRendererMixin", + "MouseMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/java/wtf/hak/survivalfabric/SurvivalFabric.java b/src/main/java/wtf/hak/survivalfabric/SurvivalFabric.java index 0af8e4b..3d98cad 100644 --- a/src/main/java/wtf/hak/survivalfabric/SurvivalFabric.java +++ b/src/main/java/wtf/hak/survivalfabric/SurvivalFabric.java @@ -12,6 +12,7 @@ import wtf.hak.survivalfabric.commands.SlimeChunkCommand; import wtf.hak.survivalfabric.commands.SpectatorCommand; import wtf.hak.survivalfabric.features.sharedenderchest.SharedEnderChest; import wtf.hak.survivalfabric.features.veinminer.VeinMinerEvents; +import wtf.hak.survivalfabric.utils.Scheduler; import static wtf.hak.survivalfabric.config.ConfigManager.getConfig; @@ -23,6 +24,7 @@ public class SurvivalFabric implements ModInitializer { @Override public void onInitialize() { + Scheduler.initialize(); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SpectatorCommand.register(dispatcher, "spectator", "s", "S", "camera", "c", "C")); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> ReloadConfigCommand.register(dispatcher, "reloadsurvivalconfig")); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SlimeChunkCommand.register(dispatcher, "slimechunk", "sc")); @@ -32,7 +34,7 @@ public class SurvivalFabric implements ModInitializer { if (getConfig().veinMinerEnabled) { PlayerBlockBreakEvents.BEFORE.register((world, player, pos, state, blockEntity) -> { - if (player instanceof ServerPlayerEntity serverPlayer) { + if (player instanceof ServerPlayerEntity serverPlayer && getConfig().veinMinerEnabled) { return VeinMinerEvents.beforeBlockBreak(world, serverPlayer, pos, state); } else { return true; diff --git a/src/main/java/wtf/hak/survivalfabric/config/Config.java b/src/main/java/wtf/hak/survivalfabric/config/Config.java index 3ab7c72..672cfa8 100644 --- a/src/main/java/wtf/hak/survivalfabric/config/Config.java +++ b/src/main/java/wtf/hak/survivalfabric/config/Config.java @@ -8,7 +8,7 @@ import java.util.List; public class Config { - public String configVersion = "1.0"; + public String configVersion = "1.1"; public boolean joinMessageEnabled = true; public String joinMessage = "§8[§a+§8] §7%s"; @@ -29,17 +29,20 @@ public class Config { public boolean sharedEnderChestEnabled = true; public String sharedEnderChestName = "Ender Chest"; public int sharedEnderChestRows = 6; - public boolean sharedEnderChestLimitedAccess = true; - public List sharedEnderChestNames = Lists.newArrayList("AlwaysHAK", "LunaticFox"); + public boolean sharedEnderChestLimitedAccess = false; + public List sharedEnderChestNames = Lists.newArrayList("AlwaysHAK"); public String inSlimeChunkMessage = "§aYou're currently in a slime chunk"; public String notInSlimeChunkMessage = "§cYou're currently not in a slime chunk"; public boolean veinMinerEnabled = true; public int maxVeinSize = 99999; + public long veinAnimationTicks = 0; public boolean chatCalcEnabled = true; + public boolean replenishEnabled = false; + public ScreenHandlerType screenHandlerType() { return switch (sharedEnderChestRows) { case 1 -> ScreenHandlerType.GENERIC_9X1; diff --git a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/DrillBase.java b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/DrillBase.java index edf69da..72c6350 100644 --- a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/DrillBase.java +++ b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/DrillBase.java @@ -1,6 +1,8 @@ package wtf.hak.survivalfabric.features.veinminer.drills; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.registry.tag.TagKey; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.MutableText; import net.minecraft.text.PlainTextContent; @@ -8,27 +10,77 @@ import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import wtf.hak.survivalfabric.features.veinminer.Drill; import wtf.hak.survivalfabric.features.veinminer.VeinMinerSession; +import wtf.hak.survivalfabric.utils.Scheduler; import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import static wtf.hak.survivalfabric.SurvivalFabric.LOGGER; +import static wtf.hak.survivalfabric.config.ConfigManager.getConfig; public class DrillBase implements Drill { protected VeinMinerSession session; + protected TagKey tag; - public DrillBase(VeinMinerSession session) { + public DrillBase(VeinMinerSession session, TagKey tag) { this.session = session; + this.tag = tag; } @Override - public boolean canHandle(BlockState blockId) { - return false; + public boolean canHandle(BlockState blockState) { + return blockState.isIn(tag); } @Override - public boolean drill(BlockPos blockPos) { - return false; + public boolean drill(BlockPos startPos) { + handleBlock(session.world.getBlockState(startPos).getBlock(), new HashSet<>(), startPos, 0); + return true; + } + + private void handleBlock(Block initialBlock, Set history, BlockPos pos, int brokenBlocks) { + if (brokenBlocks < getConfig().maxVeinSize) { + history.add(pos); + if (tryBreakBlock(pos)) { + brokenBlocks++; + int finalBrokenBlocks = brokenBlocks; + + // Put everything in a list to avoid scheduling a lot of tasks. + Set toBreak = new HashSet<>(); + collectAdjacentBlocks(pos, history, initialBlock, toBreak); + + long delay = getConfig().veinAnimationTicks; + + // Use final or effectively final variables for lambda + final int[] finalBrokenBlocksArr = {finalBrokenBlocks}; + + if (delay <= 0) { + handleBlocksInBatch(toBreak, initialBlock, history, finalBrokenBlocksArr[0]); + } else { + Scheduler.get().scheduleTask(() -> handleBlocksInBatch(toBreak, initialBlock, history, finalBrokenBlocksArr[0]), delay); + } + } + } + } + + private void collectAdjacentBlocks(BlockPos pos, Set history, Block initialBlock, Set toBreak) { + forXYZ(pos, 1, new ForXYZHandler() { + @Override + public void handle(BlockPos newPos) { + if (!history.contains(newPos) && session.world.getBlockState(newPos).getBlock() == initialBlock) { + toBreak.add(newPos); + } + } + }); + } + + private void handleBlocksInBatch(Set toBreak, Block initialBlock, Set history, int finalBrokenBlocks) { + for (BlockPos newPos : toBreak) { + handleBlock(initialBlock, history, newPos, finalBrokenBlocks); + } } @Override @@ -38,9 +90,12 @@ public class DrillBase implements Drill { } protected void forXYZ(BlockPos pos, int max, ForXYZHandler handler) { - forXYZ(pos, max, handlerPos -> { - handler.handle(handlerPos); - return 0; + forXYZ(pos, max, new ForXYZCounter() { + @Override + public int handle(BlockPos pos) { + handler.handle(pos); + return 1; // Accumulate one count for each position processed + } }, false); } @@ -49,14 +104,17 @@ public class DrillBase implements Drill { } protected void forXYZ(BlockPos pos, int max, ForXYZHandler handler, boolean forceVertical) { - forXYZ(pos, max, handlerPos -> { - handler.handle(handlerPos); - return 0; + forXYZ(pos, max, new ForXYZCounter() { + @Override + public int handle(BlockPos pos) { + handler.handle(pos); + return 1; // Accumulate one count for each position processed + } }, forceVertical); } protected int forXYZ(BlockPos pos, int max, ForXYZCounter handler, boolean forceVertical) { - ArrayList offsets = new ArrayList(); + Set offsets = new HashSet<>(); for (int d = 0; d <= max; ++d) { offsets.add(d); if (d != -d) { @@ -64,39 +122,21 @@ public class DrillBase implements Drill { } } - String[] order = new String[]{"x", "y", "z"}; - if (forceVertical) { - order = new String[]{"y", "x", "z"}; - } else { - ServerPlayerEntity player = session.player; - boolean majorPitchChange = player.getPitch() < -45.0 || player.getPitch() > 45.0; - boolean majorYawChange = (player.getYaw() > 45.0 && player.getYaw() < 135.0) || (player.getYaw() < -45.0 && player.getYaw() > -135.0); - if (majorPitchChange) { - if (majorYawChange) { - order = new String[]{"y", "z", "x"}; - } else { - order = new String[]{"y", "x", "z"}; - } - } else { - if (majorYawChange) { - order = new String[]{"z", "y", "x"}; - } - } - } - + String[] order = determineOrder(forceVertical); int counter = 0; + for (int i1 : offsets) { for (int i2 : offsets) { for (int i3 : offsets) { - int ix = order[0] == "x" ? i1 : order[1] == "x" ? i2 : i3; - int iy = order[0] == "y" ? i1 : order[1] == "y" ? i2 : i3; - int iz = order[0] == "z" ? i1 : order[1] == "z" ? i2 : i3; + int ix = order[0].equals("x") ? i1 : order[1].equals("x") ? i2 : i3; + int iy = order[0].equals("y") ? i1 : order[1].equals("y") ? i2 : i3; + int iz = order[0].equals("z") ? i1 : order[1].equals("z") ? i2 : i3; int px = pos.getX() + ix; int py = pos.getY() + iy; int pz = pos.getZ() + iz; - counter += handler.handle(new BlockPos(px, py, pz)); + counter += handler.handle(new BlockPos(px, py, pz)); // Works with ForXYZCounter, returning an int } } } @@ -104,6 +144,29 @@ public class DrillBase implements Drill { return counter; } + protected String[] determineOrder(boolean forceVertical) { + if (forceVertical) { + return new String[]{"y", "x", "z"}; + } else { + ServerPlayerEntity player = session.player; + boolean majorPitchChange = player.getPitch() < -45.0 || player.getPitch() > 45.0; + boolean majorYawChange = (player.getYaw() > 45.0 && player.getYaw() < 135.0) || (player.getYaw() < -45.0 && player.getYaw() > -135.0); + + if (majorPitchChange) { + if (majorYawChange) { + return new String[]{"y", "z", "x"}; + } else { + return new String[]{"y", "x", "z"}; + } + } else { + if (majorYawChange) { + return new String[]{"z", "y", "x"}; + } + } + } + return new String[]{"x", "y", "z"}; + } + protected boolean tryBreakBlock(BlockPos blockPos) { session.addPosition(blockPos); boolean success = isRightTool(blockPos) && session.player.interactionManager.tryBreakBlock(blockPos); @@ -127,4 +190,4 @@ public class DrillBase implements Drill { protected interface ForXYZCounter { int handle(BlockPos pos); } -} \ No newline at end of file +} diff --git a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/LeavesDrill.java b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/LeavesDrill.java index 3b56798..7de9a6c 100644 --- a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/LeavesDrill.java +++ b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/LeavesDrill.java @@ -1,62 +1,22 @@ package wtf.hak.survivalfabric.features.veinminer.drills; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; +import net.minecraft.item.Items; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.tag.TagKey; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import wtf.hak.survivalfabric.features.veinminer.VeinMinerSession; -import java.util.ArrayDeque; - -import static wtf.hak.survivalfabric.config.ConfigManager.getConfig; - public class LeavesDrill extends DrillBase { - public static final TagKey leavesTag = TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "leaves")); - public LeavesDrill(VeinMinerSession session) { - super(session); + super(session, TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "leaves"))); } @Override - public boolean canHandle(BlockState blockState) { - return blockState.isIn(leavesTag); - } - - @Override - public boolean drill(BlockPos startPos) { - ServerWorld world = session.world; - Block initialBlock = world.getBlockState(startPos).getBlock(); - int brokenLeaves = 0; - ArrayDeque pending = new ArrayDeque(); - pending.add(startPos); - - while (!pending.isEmpty() && brokenLeaves < getConfig().maxVeinSize) { - BlockPos leavesPos = pending.remove(); - Block leavesBlock = world.getBlockState(leavesPos).getBlock(); - if (tryBreakBlock(leavesPos)) { - if (leavesBlock == initialBlock) { - brokenLeaves += 1; - } - - if (leavesBlock == initialBlock) { - // look around current block - forXYZ(leavesPos, 1, newPos -> { - BlockState newBlockState = world.getBlockState(newPos); - Block newBlock = newBlockState.getBlock(); - boolean isSameOreBlock = newBlock == leavesBlock; - if (!pending.contains(newPos) && isSameOreBlock) { - pending.add(newPos); - } - }); - } - } - } - - return true; + public boolean isRightTool(BlockPos pos) { + var blockState = session.world.getBlockState(pos); + return session.player.getMainHandStack().isSuitableFor(blockState) || session.player.getMainHandStack().getItem() == Items.SHEARS; } } \ No newline at end of file diff --git a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/OreDrill.java b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/OreDrill.java index 55675cc..e607bf4 100644 --- a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/OreDrill.java +++ b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/OreDrill.java @@ -1,62 +1,14 @@ package wtf.hak.survivalfabric.features.veinminer.drills; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.tag.TagKey; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; import wtf.hak.survivalfabric.features.veinminer.VeinMinerSession; -import java.util.ArrayDeque; - -import static wtf.hak.survivalfabric.config.ConfigManager.getConfig; - public class OreDrill extends DrillBase { - public static final TagKey oreTag = TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "ore")); - public OreDrill(VeinMinerSession session) { - super(session); - } - - @Override - public boolean canHandle(BlockState blockState) { - return blockState.isIn(oreTag); - } - - @Override - public boolean drill(BlockPos startPos) { - ServerWorld world = session.world; - Block initialBlock = world.getBlockState(startPos).getBlock(); - int brokenOre = 0; - ArrayDeque pending = new ArrayDeque(); - pending.add(startPos); - - while (!pending.isEmpty() && brokenOre < getConfig().maxVeinSize) { - BlockPos orePos = pending.remove(); - Block oreBlock = world.getBlockState(orePos).getBlock(); - if (tryBreakBlock(orePos)) { - if (oreBlock == initialBlock) { - brokenOre += 1; - } - - if (oreBlock == initialBlock) { - // look around current block - forXYZ(orePos, 1, newPos -> { - BlockState newBlockState = world.getBlockState(newPos); - Block newBlock = newBlockState.getBlock(); - boolean isSameOreBlock = newBlock == oreBlock; - if (!pending.contains(newPos) && isSameOreBlock) { - pending.add(newPos); - } - }); - } - } - } - - return true; + super(session, TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "ore"))); } } \ No newline at end of file diff --git a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/WoodDrill.java b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/WoodDrill.java index 1c32d9b..d7d5bb1 100644 --- a/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/WoodDrill.java +++ b/src/main/java/wtf/hak/survivalfabric/features/veinminer/drills/WoodDrill.java @@ -1,80 +1,14 @@ package wtf.hak.survivalfabric.features.veinminer.drills; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.tag.TagKey; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; import wtf.hak.survivalfabric.features.veinminer.VeinMinerSession; -import java.util.ArrayDeque; - -import static wtf.hak.survivalfabric.config.ConfigManager.getConfig; - public class WoodDrill extends DrillBase { - public static final TagKey woodTag = TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "wood")); - public WoodDrill(VeinMinerSession session) { - super(session); - } - - @Override - public boolean canHandle(BlockState blockState) { - return blockState.isIn(woodTag); - } - - @Override - public boolean drill(BlockPos startPos) { - ServerWorld world = session.world; - int broken = 0; - ArrayDeque pendingLogs = new ArrayDeque<>(); - ArrayDeque logBlocks = new ArrayDeque<>(); - pendingLogs.add(startPos); - - String leavesBlockId = Registries.BLOCK.getId(world.getBlockState(startPos).getBlock()).toString().replace("_log", "_leaves"); - - while (!pendingLogs.isEmpty() && broken < getConfig().maxVeinSize) { - BlockPos woodPos = pendingLogs.remove(); - Block woodBlock = world.getBlockState(woodPos).getBlock(); - - if (tryBreakBlock(woodPos)) { - logBlocks.add(woodPos); - broken += 1; - - // look around current block - forXYZ(woodPos, 1, newPos -> { - Block newBlock = world.getBlockState(newPos).getBlock(); - if (newBlock == woodBlock && !pendingLogs.contains(newPos)) { - pendingLogs.add(newPos); - } - }, true); - } - } - - ArrayDeque pendingLeaves = logBlocks; - while (!pendingLeaves.isEmpty() && broken < getConfig().maxVeinSize) { - broken += forXYZ(pendingLeaves.remove(), 1, newPos -> { - int brokenLeaves = 0; - Block newBlock = world.getBlockState(newPos).getBlock(); - String newBlockId = Registries.BLOCK.getId(newBlock).toString(); - if (newBlockId.equals(leavesBlockId)) { - if (tryBreakBlock(newPos)) { - brokenLeaves += 1; - } - } - return brokenLeaves; - }, true); - } - - return true; - } - - private boolean isLeaf(Block block) { - return Registries.BLOCK.getId(block).toString().endsWith("_leaves"); + super(session, TagKey.of(RegistryKeys.BLOCK, Identifier.of("survivalfabric", "wood"))); } } \ No newline at end of file diff --git a/src/main/java/wtf/hak/survivalfabric/mixin/BlockMixin.java b/src/main/java/wtf/hak/survivalfabric/mixin/BlockMixin.java new file mode 100644 index 0000000..a4422a0 --- /dev/null +++ b/src/main/java/wtf/hak/survivalfabric/mixin/BlockMixin.java @@ -0,0 +1,64 @@ +package wtf.hak.survivalfabric.mixin; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.CropBlock; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.HoeItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.stat.Stats; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.GameMode; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import wtf.hak.survivalfabric.config.ConfigManager; + +import java.util.List; + +@Mixin(Block.class) +public abstract class BlockMixin { + + @Inject(method = "onBreak", at = @At("HEAD"), cancellable = true) + public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player, CallbackInfoReturnable cir) { + if (world.isClient()) return; + + if (state.getBlock() instanceof CropBlock && ConfigManager.getConfig().replenishEnabled) { + ItemStack mainHand = player.getStackInHand(Hand.MAIN_HAND); + if (mainHand.getItem() instanceof HoeItem) { + Item seedItem = state.getBlock().asItem(); + Block seedBlock = state.getBlock(); + List drops = Block.getDroppedStacks(state, (ServerWorld) world, pos, null, player, mainHand); + if (removeIfAvailable(drops, seedItem)) { + if (player.getGameMode() != GameMode.CREATIVE) { + for (ItemStack drop : drops) { + Block.dropStack(world, pos, drop); + } + player.incrementStat(Stats.USED.getOrCreateStat(seedItem)); + mainHand.damage(1, player, EquipmentSlot.MAINHAND); + } + world.getServer().executeSync(() -> world.setBlockState(pos, seedBlock.getDefaultState())); + cir.setReturnValue(Blocks.AIR.getDefaultState()); + cir.cancel(); + } + } + } + } + + private boolean removeIfAvailable(List drops, Item item) { + for (ItemStack drop : drops) { + if (drop.getItem() == item) { + drop.decrement(1); + return true; + } + } + return false; + } +} diff --git a/src/main/java/wtf/hak/survivalfabric/mixin/PlayerManagerMixin.java b/src/main/java/wtf/hak/survivalfabric/mixin/PlayerManagerMixin.java index b683469..d5b6ad6 100644 --- a/src/main/java/wtf/hak/survivalfabric/mixin/PlayerManagerMixin.java +++ b/src/main/java/wtf/hak/survivalfabric/mixin/PlayerManagerMixin.java @@ -15,12 +15,16 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import wtf.hak.survivalfabric.commands.SpectatorCommand; import wtf.hak.survivalfabric.config.ConfigManager; +import wtf.hak.survivalfabric.utils.MathUtils; import java.util.Set; @Mixin(PlayerManager.class) public abstract class PlayerManagerMixin { + /** + * Send join message to joined player + */ @Inject(method = {"onPlayerConnect"}, at = {@At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V")}) public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) { if (ConfigManager.getConfig().joinMessageEnabled && !player.getServer().isSingleplayer()) { @@ -29,6 +33,9 @@ public abstract class PlayerManagerMixin { } } + /** + * Modify join message broadcasted to server + */ @ModifyArg(method = {"onPlayerConnect"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V")) private Text onPlayerConnect(Text text) { if (ConfigManager.getConfig().joinMessageEnabled) { @@ -38,6 +45,9 @@ public abstract class PlayerManagerMixin { return text; } + /** + * Get player out of specator mode if necessary + */ @Inject(method = {"remove"}, at = {@At("HEAD")}) public void onPlayerLeave(ServerPlayerEntity player, CallbackInfo ci) { if (SpectatorCommand.spectating.containsKey(player)) { @@ -47,6 +57,9 @@ public abstract class PlayerManagerMixin { } } + /** + * Modify chat messages + */ @Inject(method = {"broadcast(Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/network/message/MessageType$Parameters;)V"}, at = {@At("HEAD")}, cancellable = true) private void onBroadcast(SignedMessage message, ServerPlayerEntity sender, MessageType.Parameters parameters, CallbackInfo ci) { if (sender != null) { @@ -56,10 +69,10 @@ public abstract class PlayerManagerMixin { String processedMessage = rawMessage; - if (isCalcEnabled) { + if (isCalcEnabled && MathUtils.hasSupportedOperator(rawMessage)) { String expression = rawMessage.endsWith("=") ? rawMessage.substring(0, rawMessage.length() - 1).trim() : rawMessage; try { - String result = String.valueOf(evaluateExpression(expression)); + String result = String.valueOf(MathUtils.evaluateExpression(expression)); StringBuilder sb = new StringBuilder(rawMessage).append("§6"); if (rawMessage.contains(" ")) sb.append(" "); @@ -83,47 +96,4 @@ public abstract class PlayerManagerMixin { } } - - private double evaluateExpression(String expression) { - return evaluate(expression.replaceAll("\\s", ""), new int[]{0}); - } - - private double evaluate(String expr, int[] index) { - double value = parseTerm(expr, index); - while (index[0] < expr.length()) { - char op = expr.charAt(index[0]); - if (op != '+' && op != '-') break; - index[0]++; - double nextTerm = parseTerm(expr, index); - value = (op == '+') ? value + nextTerm : value - nextTerm; - } - return value; - } - - private double parseTerm(String expr, int[] index) { - double value = parseFactor(expr, index); - while (index[0] < expr.length()) { - char op = expr.charAt(index[0]); - if (op != '*' && op != '/') break; - index[0]++; - double nextFactor = parseFactor(expr, index); - value = (op == '*') ? value * nextFactor : value / nextFactor; - } - return value; - } - - private double parseFactor(String expr, int[] index) { - if (expr.charAt(index[0]) == '(') { - index[0]++; - double value = evaluate(expr, index); - index[0]++; // Skip closing ')' - return value; - } - - int start = index[0]; - while (index[0] < expr.length() && (Character.isDigit(expr.charAt(index[0])) || expr.charAt(index[0]) == '.')) { - index[0]++; - } - return Double.parseDouble(expr.substring(start, index[0])); - } } diff --git a/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayNetworkHandlerMixin.java b/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayNetworkHandlerMixin.java index 8d2caf9..b17fe9e 100644 --- a/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayNetworkHandlerMixin.java +++ b/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayNetworkHandlerMixin.java @@ -8,8 +8,11 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import wtf.hak.survivalfabric.config.ConfigManager; @Mixin(ServerPlayNetworkHandler.class) -public abstract class ServerPlayNetworkHandlerMixin { +public class ServerPlayNetworkHandlerMixin { + /** + * Modify quit message + */ @ModifyArg(method = {"cleanUp"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V")) private Text quitMessage(Text text) { if (ConfigManager.getConfig().quitMessageEnabled) { diff --git a/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayerEntityMixin.java b/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayerEntityMixin.java index d0110f9..d3bec63 100644 --- a/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/wtf/hak/survivalfabric/mixin/ServerPlayerEntityMixin.java @@ -10,8 +10,11 @@ import wtf.hak.survivalfabric.commands.SpectatorCommand; import wtf.hak.survivalfabric.config.ConfigManager; @Mixin(ServerPlayerEntity.class) -public abstract class ServerPlayerEntityMixin { +public class ServerPlayerEntityMixin { + /** + * Change player list name if enabled + */ @Inject(method = "getPlayerListName", at = @At("HEAD"), cancellable = true) private void changePlayerListName(CallbackInfoReturnable cir) { if (ConfigManager.getConfig().dimensionIndicatorEnabled) { diff --git a/src/main/java/wtf/hak/survivalfabric/mixin/ServerWorldMixin.java b/src/main/java/wtf/hak/survivalfabric/mixin/ServerWorldMixin.java index d2ea4ae..60bfb28 100644 --- a/src/main/java/wtf/hak/survivalfabric/mixin/ServerWorldMixin.java +++ b/src/main/java/wtf/hak/survivalfabric/mixin/ServerWorldMixin.java @@ -12,6 +12,9 @@ import wtf.hak.survivalfabric.utils.PacketUtils; @Mixin(ServerWorld.class) public class ServerWorldMixin { + /** + * Update List Names if needed + */ @Inject(method = "onDimensionChanged", at = {@At("HEAD")}) public void onDimensionChange(Entity entity, CallbackInfo ci) { if (entity instanceof ServerPlayerEntity) { diff --git a/src/main/java/wtf/hak/survivalfabric/utils/MathUtils.java b/src/main/java/wtf/hak/survivalfabric/utils/MathUtils.java new file mode 100644 index 0000000..d5abf0e --- /dev/null +++ b/src/main/java/wtf/hak/survivalfabric/utils/MathUtils.java @@ -0,0 +1,64 @@ +package wtf.hak.survivalfabric.utils; + +public class MathUtils { + + + public static double clamp(double value, double min, double max) { + return Math.max(min, Math.min(max, value)); + } + + public static float clamp(float value, float min, float max) { + return Math.max(min, Math.min(max, value)); + } + + public static int clamp(int value, int min, int max) { + return Math.max(min, Math.min(max, value)); + } + + public static boolean hasSupportedOperator(String msg) { + return msg.contains("+") || msg.contains("-") || msg.contains("*") || msg.contains("/"); + } + + public static double evaluateExpression(String expression) { + return evaluate(expression.replaceAll("\\s", ""), new int[]{0}); + } + + public static double evaluate(String expr, int[] index) { + double value = parseTerm(expr, index); + while (index[0] < expr.length()) { + char op = expr.charAt(index[0]); + if (op != '+' && op != '-') break; + index[0]++; + double nextTerm = parseTerm(expr, index); + value = (op == '+') ? value + nextTerm : value - nextTerm; + } + return value; + } + + public static double parseTerm(String expr, int[] index) { + double value = parseFactor(expr, index); + while (index[0] < expr.length()) { + char op = expr.charAt(index[0]); + if (op != '*' && op != '/') break; + index[0]++; + double nextFactor = parseFactor(expr, index); + value = (op == '*') ? value * nextFactor : value / nextFactor; + } + return value; + } + + public static double parseFactor(String expr, int[] index) { + if (expr.charAt(index[0]) == '(') { + index[0]++; + double value = evaluate(expr, index); + index[0]++; // Skip closing ')' + return value; + } + + int start = index[0]; + while (index[0] < expr.length() && (Character.isDigit(expr.charAt(index[0])) || expr.charAt(index[0]) == '.')) { + index[0]++; + } + return Double.parseDouble(expr.substring(start, index[0])); + } +} diff --git a/src/main/java/wtf/hak/survivalfabric/utils/Scheduler.java b/src/main/java/wtf/hak/survivalfabric/utils/Scheduler.java new file mode 100644 index 0000000..37c5e99 --- /dev/null +++ b/src/main/java/wtf/hak/survivalfabric/utils/Scheduler.java @@ -0,0 +1,45 @@ +package wtf.hak.survivalfabric.utils; + +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class Scheduler { + + private static Scheduler INSTANCE; + + private final Map tasks = new ConcurrentHashMap<>(); + + public Scheduler() { + ServerTickEvents.END_SERVER_TICK.register((server) -> { + for(Runnable task : tasks.keySet()) { + long delay = tasks.get(task); + if(delay <= 0) { + task.run(); + tasks.remove(task); + } else { + tasks.put(task, delay-1); + } + } + }); + } + + public void scheduleTask(Runnable task) { + scheduleTask(task, 0L); + } + + public void scheduleTask(Runnable task, long delay) { + tasks.put(task, delay); + } + + public static Scheduler get() { + return INSTANCE; + } + + public static Scheduler initialize() { + INSTANCE = new Scheduler(); + return INSTANCE; + } +} diff --git a/src/main/resources/assets/survivalfabric/lang/en_us.json b/src/main/resources/assets/survivalfabric/lang/en_us.json index 92e23d7..03f83b6 100644 --- a/src/main/resources/assets/survivalfabric/lang/en_us.json +++ b/src/main/resources/assets/survivalfabric/lang/en_us.json @@ -17,5 +17,6 @@ "key.survivalfabric.angle14": "204.98 / -41.68", "key.survivalfabric.angle15": "244.97 / -41.71", "category.survivalfabric.survivalfabric": "Survival Fabric", - "key.survivalfabric.camera": "/camera" + "key.survivalfabric.camera": "/camera", + "key.survivalfabric.zoom": "Zoom" } \ No newline at end of file diff --git a/src/main/resources/survivalfabric.mixins.json b/src/main/resources/survivalfabric.mixins.json index 4795188..cadd775 100644 --- a/src/main/resources/survivalfabric.mixins.json +++ b/src/main/resources/survivalfabric.mixins.json @@ -3,6 +3,7 @@ "package": "wtf.hak.survivalfabric.mixin", "compatibilityLevel": "JAVA_21", "mixins": [ + "BlockMixin", "PlayerManagerMixin", "ServerPlayerEntityMixin", "ServerPlayNetworkHandlerMixin",