Architectury build system & huge code cleanup

This commit is contained in:
Martin Prokoph
2024-07-17 14:26:17 +02:00
parent 9e3b2ae060
commit 27221b62cd
146 changed files with 1529 additions and 855 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright © 2021 LambdAurora <aurora42lambda@gmail.com>
*
* This file is part of midnightcontrols.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package eu.midnightdust.midnightcontrols.client.util;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import org.jetbrains.annotations.Nullable;
/**
* Represents an accessor to AbstractContainerScreen.
*/
public interface HandledScreenAccessor {
/**
* Gets the left coordinate of the GUI.
*
* @return the left coordinate of the GUI
*/
int getX();
/**
* Gets the top coordinate of the GUI.
*
* @return the top coordinate of the GUI
*/
int getY();
/**
* Gets the slot at position.
*
* @param posX the X position to check
* @param posY the Y position to check
* @return the slot at the specified position
*/
Slot midnightcontrols$getSlotAt(double posX, double posY);
boolean midnightcontrols$isClickOutsideBounds(double mouseX, double mouseY, int x, int y, int button);
/**
* Handles a mouse click on the specified slot.
*
* @param slot the slot instance
* @param slotId the slot id
* @param clickData the click data
* @param actionType the action type
*/
void midnightcontrols$onMouseClick(@Nullable Slot slot, int slotId, int clickData, SlotActionType actionType);
}

View File

@@ -0,0 +1,63 @@
package eu.midnightdust.midnightcontrols.client.util;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.screen.slot.Slot;
import org.aperlambda.lambdacommon.utils.Pair;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;
import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.client;
public class InventoryUtil {
// Finds the closest slot in the GUI within 14 pixels.
public static Optional<Slot> findClosestSlot(HandledScreen<?> inventory, int direction) {
var accessor = (HandledScreenAccessor) inventory;
int guiLeft = accessor.getX();
int guiTop = accessor.getY();
double mouseX = client.mouse.getX() * (double) client.getWindow().getScaledWidth() / (double) client.getWindow().getWidth();
double mouseY = client.mouse.getY() * (double) client.getWindow().getScaledHeight() / (double) client.getWindow().getHeight();
// Finds the hovered slot.
var mouseSlot = accessor.midnightcontrols$getSlotAt(mouseX, mouseY);
return inventory.getScreenHandler().slots.parallelStream()
.filter(Predicate.isEqual(mouseSlot).negate())
.map(slot -> {
int posX = guiLeft + slot.x + 8;
int posY = guiTop + slot.y + 8;
int otherPosX = (int) mouseX;
int otherPosY = (int) mouseY;
if (mouseSlot != null) {
otherPosX = guiLeft + mouseSlot.x + 8;
otherPosY = guiTop + mouseSlot.y + 8;
}
// Distance between the slot and the cursor.
double distance = Math.sqrt(Math.pow(posX - otherPosX, 2) + Math.pow(posY - otherPosY, 2));
return Pair.of(slot, distance);
}).filter(entry -> {
var slot = entry.key;
int posX = guiLeft + slot.x + 8;
int posY = guiTop + slot.y + 8;
int otherPosX = (int) mouseX;
int otherPosY = (int) mouseY;
if (mouseSlot != null) {
otherPosX = guiLeft + mouseSlot.x + 8;
otherPosY = guiTop + mouseSlot.y + 8;
}
if (direction == 0)
return posY < otherPosY;
else if (direction == 1)
return posY > otherPosY;
else if (direction == 2)
return posX > otherPosX;
else if (direction == 3)
return posX < otherPosX;
else
return false;
})
.min(Comparator.comparingDouble(p -> p.value))
.map(p -> p.key);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright © 2021 LambdAurora <aurora42lambda@gmail.com>
*
* This file is part of midnightcontrols.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package eu.midnightdust.midnightcontrols.client.util;
/**
* Represents a Minecraft keybinding with extra access.
*/
public interface KeyBindingAccessor {
boolean midnightcontrols$press();
boolean midnightcontrols$unpress();
default boolean midnightcontrols$handlePressState(boolean pressed) {
if (pressed)
return this.midnightcontrols$press();
else
return this.midnightcontrols$unpress();
}
}

View File

@@ -0,0 +1,23 @@
package eu.midnightdust.midnightcontrols.client.util;
import net.minecraft.util.math.MathHelper;
public class MathUtil {
public static class PolarUtil {
public float polarX;
public float polarY;
public PolarUtil() {}
public void calculate(float x, float y, float speedFactor) {
calculate(x, y, speedFactor, 0);
}
public void calculate(float x, float y, float speedFactor, double deadZone) {
double inputR = Math.pow(x, 2) + Math.pow(y, 2);
inputR = (Math.abs(speedFactor * MathHelper.clamp(inputR,0.f,1.f)));
inputR = inputR < deadZone ? 0f : (inputR-deadZone) / (1f-deadZone);
double inputTheta = Math.atan2(y, x);
polarX = (float) (inputR *Math.cos(inputTheta));
polarY = (float) (inputR *Math.sin(inputTheta));
}
}
}

View File

@@ -0,0 +1,15 @@
package eu.midnightdust.midnightcontrols.client.util;
import java.awt.*;
public class RainbowColor {
public static float hue;
public static void tick() {
if (hue > 1) hue = 0f;
hue = hue + 0.01f;
}
public static Color radialRainbow(float saturation, float brightness) {
return Color.getHSBColor(hue, saturation, brightness);
}
}

View File

@@ -0,0 +1,47 @@
package eu.midnightdust.midnightcontrols.client.util;
import eu.midnightdust.midnightcontrols.client.MidnightControlsConfig;
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding;
import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.client;
public class ToggleSneakSprintUtil {
public static boolean toggleSneak(ButtonBinding button) {
if (client.player == null) return false;
boolean isFlying = client.player.getAbilities().flying;
var option = client.options.getSneakToggled();
button.asKeyBinding().ifPresent(binding -> {
boolean sneakToggled = option.getValue();
if (isFlying && sneakToggled)
option.setValue(false);
else if (MidnightControlsConfig.controllerToggleSneak != sneakToggled)
option.setValue(!sneakToggled);
binding.setPressed(button.isPressed());
if (isFlying && sneakToggled)
option.setValue(true);
else if (MidnightControlsConfig.controllerToggleSneak != sneakToggled)
option.setValue(sneakToggled);
});
return true;
}
public static boolean toggleSprint(ButtonBinding button) {
if (client.player == null) return false;
boolean isFlying = client.player.getAbilities().flying;
var option = client.options.getSprintToggled();
button.asKeyBinding().ifPresent(binding -> {
boolean sprintToggled = option.getValue();
if (isFlying && sprintToggled)
option.setValue(false);
else if (MidnightControlsConfig.controllerToggleSprint != sprintToggled)
option.setValue(!sprintToggled);
binding.setPressed(button.isPressed());
if (client.player.getAbilities().flying && sprintToggled)
option.setValue(true);
else if (MidnightControlsConfig.controllerToggleSprint != sprintToggled)
option.setValue(sprintToggled);
});
return true;
}
}

View File

@@ -0,0 +1,50 @@
package eu.midnightdust.midnightcontrols.client.util.platform;
import dev.architectury.injectables.annotations.ExpectPlatform;
import eu.midnightdust.midnightcontrols.client.mixin.CreativeInventoryScreenAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemGroups;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class ItemGroupUtil {
@ExpectPlatform
public static List<ItemGroup> getVisibleGroups(CreativeInventoryScreen screen) {
throw new AssertionError();
}
@ExpectPlatform
public static boolean cyclePage(boolean next, CreativeInventoryScreen screen) {
throw new AssertionError();
}
public static @NotNull ItemGroup cycleTab(boolean next, MinecraftClient client) {
ItemGroup currentTab = CreativeInventoryScreenAccessor.getSelectedTab();
int currentColumn = currentTab.getColumn();
ItemGroup.Row currentRow = currentTab.getRow();
ItemGroup newTab = null;
List<ItemGroup> visibleTabs = ItemGroupUtil.getVisibleGroups((CreativeInventoryScreen) client.currentScreen);
for (ItemGroup tab : visibleTabs) {
if (tab.getRow().equals(currentRow) && ((newTab == null && ((next && tab.getColumn() > currentColumn) ||
(!next && tab.getColumn() < currentColumn))) || (newTab != null && ((next && tab.getColumn() > currentColumn && tab.getColumn() < newTab.getColumn()) ||
(!next && tab.getColumn() < currentColumn && tab.getColumn() > newTab.getColumn())))))
newTab = tab;
}
if (newTab == null)
for (ItemGroup tab : visibleTabs) {
if ((tab.getRow().compareTo(currentRow)) != 0 && ((next && newTab == null || next && newTab.getColumn() > tab.getColumn()) || (!next && newTab == null) || (!next && newTab.getColumn() < tab.getColumn())))
newTab = tab;
}
if (newTab == null) {
for (ItemGroup tab : visibleTabs) {
if ((next && tab.getRow() == ItemGroup.Row.TOP && tab.getColumn() == 0) ||
!next && tab.getRow() == ItemGroup.Row.BOTTOM && (newTab == null || tab.getColumn() > newTab.getColumn()))
newTab = tab;
}
}
if (newTab == null || newTab.equals(currentTab)) newTab = ItemGroups.getDefaultTab();
return newTab;
}
}

View File

@@ -0,0 +1,16 @@
package eu.midnightdust.midnightcontrols.client.util.platform;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.Packet;
public class NetworkUtil {
@ExpectPlatform
public static void sendPacketC2S(Packet<?> packet) {
throw new AssertionError();
}
@ExpectPlatform
public static void sendPayloadC2S(CustomPayload payload) {
throw new AssertionError();
}
}