Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
10 289
src/client/java/com/kodari/nearesttotemfinder/mixin/AbstractContainerScreenMixin.java
package com.kodari.nearesttotemfinder.mixin;
import com.kodari.nearesttotemfinder.client.NearestTotemFinderConfig;
import com.mojang.blaze3d.platform.Window;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.Items;
import org.lwjgl.sdl.SDLMouse;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(AbstractContainerScreen.class)
public abstract class AbstractContainerScreenMixin {
@Shadow
@Final
protected AbstractContainerMenu menu;
@Shadow
protected int leftPos;
@Shadow
protected int topPos;
@Inject(method = "init", at = @At("TAIL"))
private void nearesttotemfinder$moveCursorToNearestTotem(CallbackInfo callbackInfo) {
if (!NearestTotemFinderConfig.isEnabled() || !(((Object) this) instanceof InventoryScreen)) {
return;
}
Minecraft minecraft = Minecraft.getInstance();
Window window = minecraft.getWindow();
double mouseX = MouseHandler.getScaledXPos(window, minecraft.mouseHandler.xpos());
double mouseY = MouseHandler.getScaledYPos(window, minecraft.mouseHandler.ypos());
Slot nearestSlot = null;
double nearestDistanceSquared = Double.POSITIVE_INFINITY;
double nearestCenterX = 0.0;
double nearestCenterY = 0.0;
for (int index = InventoryMenu.INV_SLOT_START; index < InventoryMenu.USE_ROW_SLOT_END; index++) {
Slot slot = this.menu.slots.get(index);
if (slot.getItem().getItem() != Items.TOTEM_OF_UNDYING) {
continue;
}
double centerX = this.leftPos + slot.x + 8.0;
double centerY = this.topPos + slot.y + 8.0;
double distanceX = centerX - mouseX;
double distanceY = centerY - mouseY;
double distanceSquared = distanceX * distanceX + distanceY * distanceY;
if (distanceSquared < nearestDistanceSquared) {
nearestSlot = slot;
nearestDistanceSquared = distanceSquared;
nearestCenterX = centerX;
nearestCenterY = centerY;
}
}
if (nearestSlot == null) {
return;
}
double windowX = nearestCenterX * window.getScreenWidth() / window.getGuiScaledWidth();
double windowY = nearestCenterY * window.getScreenHeight() / window.getGuiScaledHeight();
SDLMouse.SDL_WarpMouseInWindow(window.handle(), (float) windowX, (float) windowY);
}
}