5 Commits

Author SHA1 Message Date
8808df1cf1 Added zoom functionality
All checks were successful
build / build (push) Successful in 1m39s
2025-04-23 22:32:18 +02:00
963002570d Added zoom functionality 2025-04-23 22:32:13 +02:00
2c95b5e374 Oops 2025-04-23 21:26:56 +02:00
1d3dba1b02 Changed Mixin from interface(?) to abstract class 2025-04-23 21:21:10 +02:00
c7ea933494 Added documentation to Mixin classes 2025-04-23 20:53:15 +02:00
12 changed files with 146 additions and 5 deletions

View File

@ -65,6 +65,8 @@ As a challenge I'm trying to make it as user-friendly as possible.
- Integer
- [x] Chat Calculator
- [x] Check if operator is present before calculating
- [x] Zoom
- [ ] Configurable
# To-do
@ -73,8 +75,6 @@ As a challenge I'm trying to make it as user-friendly as possible.
- Store server settings in world folder for better singleplayer use
## Client side
- Zoom
- Configurable
## Server Side
- Telekinesis

View File

@ -5,6 +5,7 @@ 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.Zoom;
public class SurvivalFabricClient implements ClientModInitializer {
@ -18,6 +19,6 @@ public class SurvivalFabricClient implements ClientModInitializer {
AngleViewer.register();
RemoveDarknessEffect.register();
SFKeyBindings.register();
Zoom.register();
}
}

View File

@ -0,0 +1,52 @@
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 wtf.hak.survivalfabric.utils.MathUtils;
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 final float INITIAL_ZOOM = 20f;
private static boolean SHOULD_ZOOM = false;
private static float ZOOM_STEP_VALUE = 2.5f;
private static int ZOOM_STEP = 0;
public static void register() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if(ZOOM_BIND.isPressed())
SHOULD_ZOOM = true;
else {
SHOULD_ZOOM = false;
ZOOM_STEP = 0;
}
});
}
public static boolean isZooming() {
return SHOULD_ZOOM;
}
public static float getZoomFov() {
return INITIAL_ZOOM - -ZOOM_STEP * ZOOM_STEP_VALUE;
}
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 - INITIAL_ZOOM) / ZOOM_STEP_VALUE)+1;
} else if (zoomFov > 110) {
ZOOM_STEP = Math.round((110 - INITIAL_ZOOM) / ZOOM_STEP_VALUE);
}
MinecraftClient.getInstance().player.sendMessage(Text.literal(ZOOM_STEP + ""), true);
}
}

View File

@ -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 abstract 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;
}
}

View File

@ -0,0 +1,31 @@
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;
@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()) {
if(MinecraftClient.getInstance().player != null) {
if(vertical>0)
Zoom.modifyStep(-1);
else
Zoom.modifyStep(1);
}
ci.cancel();
}
}
}

View File

@ -5,7 +5,9 @@
"client": [
"BackgroundRendererMixin",
"BlockEntityRendererMixin",
"EntityMixin"
"EntityMixin",
"GameRendererMixin",
"MouseMixin"
],
"injectors": {
"defaultRequire": 1

View File

@ -22,6 +22,9 @@ 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()) {
@ -30,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) {
@ -39,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)) {
@ -48,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) {

View File

@ -10,6 +10,9 @@ import wtf.hak.survivalfabric.config.ConfigManager;
@Mixin(ServerPlayNetworkHandler.class)
public abstract 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) {

View File

@ -12,6 +12,9 @@ import wtf.hak.survivalfabric.config.ConfigManager;
@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin {
/**
* Change player list name if enabled
*/
@Inject(method = "getPlayerListName", at = @At("HEAD"), cancellable = true)
private void changePlayerListName(CallbackInfoReturnable<Text> cir) {
if (ConfigManager.getConfig().dimensionIndicatorEnabled) {

View File

@ -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) {

View File

@ -3,6 +3,18 @@ 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("/");
}

View File

@ -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"
}