Well it works now

This commit is contained in:
MrMelon54 committed 2023-06-27 18:00:11 +01:00
1 parent 63415c3e4b
commit 3a874ba49f
26 files changed
+861 -92

No files matched your search

@@ -4,8 +4,9 @@ import com.mrmelon54.Draggable.config.ConfigStructure;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
import net.minecraft.server.packs.repository.Pack;
import java.util.List;
import java.util.function.Supplier;
public class Draggable {
@@ -24,4 +25,12 @@ public class Draggable {
public static Supplier<Screen> createConfigScreen(Screen screen) {
return AutoConfig.getConfigScreen(ConfigStructure.class, screen);
}
public static boolean shouldNotTouch(Pack pack) {
return pack.isFixedPosition() || pack.isRequired() || pack.getId().equals("vanilla");
}
public static boolean shouldNotTouch(PackSelectionModel.Entry pack) {
return pack.isFixedPosition() || pack.isRequired() || pack.getId().equals("vanilla");
}
}
@@ -1,27 +0,0 @@
package com.mrmelon54.Draggable;
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.platform.Platform;
import java.nio.file.Path;
public class ExampleExpectPlatform {
/**
* We can use {@link Platform#getConfigFolder()} but this is just an example of {@link ExpectPlatform}.
* <p>
* This must be a <b>public static</b> method. The platform-implemented solution must be placed under a
* platform sub-package, with its class suffixed with {@code Impl}.
* <p>
* Example:
* Expect: net.examplemod.ExampleExpectPlatform#getConfigDirectory()
* Actual Fabric: net.examplemod.fabric.ExampleExpectPlatformImpl#getConfigDirectory()
* Actual Forge: net.examplemod.forge.ExampleExpectPlatformImpl#getConfigDirectory()
* <p>
* <a href="https://plugins.jetbrains.com/plugin/16210-architectury">You should also get the IntelliJ plugin to help with @ExpectPlatform.</a>
*/
@ExpectPlatform
public static Path getConfigDirectory() {
// Just throw an error, the content should get replaced at runtime.
throw new AssertionError();
}
}
@@ -3,7 +3,7 @@ package com.mrmelon54.Draggable.config;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
@Config(name = "draggable-resource-packs")
@Config(name = "draggable")
@Config.Gui.Background("minecraft:textures/block/brain_coral_block.png")
public class ConfigStructure implements ConfigData {
public boolean disableResourcePackArrows = true;
@@ -0,0 +1,7 @@
package com.mrmelon54.Draggable.duck;
public interface AbstractPackDuckProvider {
void moveTo(int j);
int getIndexInSelectedList();
}
@@ -0,0 +1,7 @@
package com.mrmelon54.Draggable.duck;
import net.minecraft.client.multiplayer.ServerData;
public interface MultiplayerScreenDuckProvider {
int getIndexOfServerInfo(ServerData serverInfo);
}
@@ -0,0 +1,12 @@
package com.mrmelon54.Draggable.duck;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
public interface ResourcePackEntryDuckProvider {
PackSelectionModel.Entry getUnderlyingPack();
void renderPoppedOut(GuiGraphics guiGraphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta);
void setBeingDragged(boolean v);
}
@@ -0,0 +1,5 @@
package com.mrmelon54.Draggable.duck;
public interface ResourcePackOrganizerDuckProvider {
void updateSelectedList();
}
@@ -0,0 +1,12 @@
package com.mrmelon54.Draggable.duck;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.multiplayer.ServerData;
public interface ServerEntryDuckProvider {
ServerData getUnderlyingServer();
void renderPoppedOut(GuiGraphics guiGraphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta);
void setBeingDragged(boolean v);
}
@@ -0,0 +1,7 @@
package com.mrmelon54.Draggable.duck;
import net.minecraft.client.multiplayer.ServerData;
public interface ServerListDuckProvider {
void add(int index, ServerData serverInfo);
}
@@ -1,15 +0,0 @@
package com.mrmelon54.Draggable.mixin;
import net.minecraft.client.gui.screens.TitleScreen;
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;
@Mixin(TitleScreen.class)
public class MixinTitleScreen {
@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {
System.out.println("Hello from example architectury common mixin!");
}
}
@@ -0,0 +1,19 @@
package com.mrmelon54.Draggable.mixin.packs;
import com.mrmelon54.Draggable.duck.ResourcePackOrganizerDuckProvider;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(PackSelectionModel.class)
public abstract class MixinPackSelectionModel implements ResourcePackOrganizerDuckProvider {
@Shadow
@Final
Runnable onListChanged;
@Override
public void updateSelectedList() {
onListChanged.run();
}
}
@@ -0,0 +1,52 @@
package com.mrmelon54.Draggable.mixin.packs;
import com.mrmelon54.Draggable.Draggable;
import com.mrmelon54.Draggable.duck.AbstractPackDuckProvider;
import com.mrmelon54.Draggable.duck.ResourcePackOrganizerDuckProvider;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
import net.minecraft.server.packs.repository.Pack;
import org.spongepowered.asm.mixin.Dynamic;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.List;
import java.util.stream.Stream;
@Mixin(PackSelectionModel.EntryBase.class)
public abstract class MixinPackSelectionModel_EntryBase implements AbstractPackDuckProvider {
@Shadow
protected abstract List<Pack> getSelfList();
@Shadow
@Final
private Pack pack;
@Shadow
@Dynamic("field_25460 is provided by PackSelectionModel.EntryBase but has no mapping")
@Final
private PackSelectionModel field_25460;
@Override
public void moveTo(int j) {
if (Draggable.shouldNotTouch(pack)) return;
List<Pack> list = getSelfList();
list.remove(pack);
list.add(j, pack);
// get all fixed position resource packs, remove them and add them all at the end
Stream<Pack> packStream = list.stream().filter(Draggable::shouldNotTouch);
List<Pack> list1 = packStream.toList();
list.removeAll(list1);
list.addAll(list1);
if (field_25460 instanceof ResourcePackOrganizerDuckProvider duckProvider)
duckProvider.updateSelectedList();
}
@Override
public int getIndexInSelectedList() {
return getSelfList().indexOf(pack);
}
}
@@ -0,0 +1,42 @@
package com.mrmelon54.Draggable.mixin.packs;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.packs.PackSelectionScreen;
import net.minecraft.client.gui.screens.packs.TransferableSelectionList;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(PackSelectionScreen.class)
public class MixinPackSelectionScreen extends Screen {
@Shadow
private TransferableSelectionList selectedPackList;
@Shadow
private TransferableSelectionList availablePackList;
protected MixinPackSelectionScreen(Component component) {
super(component);
}
@Override
public boolean mouseClicked(double d, double e, int i) {
if (selectedPackList.isMouseOver(d, e)) return selectedPackList.mouseClicked(d, e, i);
if (availablePackList.isMouseOver(d, e)) return availablePackList.mouseClicked(d, e, i);
return super.mouseClicked(d, e, i);
}
@Override
public boolean mouseDragged(double d, double e, int i, double f, double g) {
if (selectedPackList.isMouseOver(d, e)) return selectedPackList.mouseDragged(d, e, i, f, g);
if (availablePackList.isMouseOver(d, e)) return availablePackList.mouseDragged(d, e, i, f, g);
return super.mouseDragged(d, e, i, f, g);
}
@Override
public boolean mouseReleased(double d, double e, int i) {
if (selectedPackList.isDragging()) selectedPackList.mouseReleased(d, e, i);
if (availablePackList.isDragging()) availablePackList.mouseReleased(d, e, i);
return super.mouseReleased(d, e, i);
}
}
@@ -0,0 +1,179 @@
package com.mrmelon54.Draggable.mixin.packs;
import com.mrmelon54.Draggable.Draggable;
import com.mrmelon54.Draggable.duck.AbstractPackDuckProvider;
import com.mrmelon54.Draggable.duck.ResourcePackEntryDuckProvider;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
import net.minecraft.client.gui.screens.packs.TransferableSelectionList;
import net.minecraft.util.Mth;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
@Mixin(TransferableSelectionList.class)
@Environment(EnvType.CLIENT)
public abstract class MixinTransferableSelectionList extends ObjectSelectionList<TransferableSelectionList.PackEntry> {
private TransferableSelectionList.PackEntry draggingObject = null;
private double draggingStartX = 0;
private double draggingStartY = 0;
private double draggingOffsetX = 0;
private double draggingOffsetY = 0;
private long softScrollingTimer = 0;
private double softScrollingOrigin = 0;
public MixinTransferableSelectionList(Minecraft minecraft, int i, int j, int k, int l, int m) {
super(minecraft, i, j, k, l, m);
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button != 0 || draggingObject != null || !isCapMouseY((int) mouseY)) {
return super.mouseClicked(mouseX, mouseY, button);
}
draggingObject = this.getEntryAtPosition(mouseX, mouseY);
if ((draggingObject != null) && (draggingObject instanceof ResourcePackEntryDuckProvider duckProvider) && isValidForDragging(duckProvider)) {
// Save the mouse origin position and the offset for the top left corner of the widget
draggingStartX = mouseX;
draggingStartY = mouseY;
draggingOffsetX = getRowLeft() - draggingStartX;
draggingOffsetY = getRowTop(this.children().indexOf(draggingObject)) - draggingStartY;
// Don't grab if inside the pack icon
if (draggingOffsetX > -32f) {
draggingObject = null;
return super.mouseClicked(mouseX, mouseY, button);
}
this.setDragging(true);
this.setFocused(draggingObject);
duckProvider.setBeingDragged(true);
softScrollingTimer = 0;
GLFW.glfwSetCursor(minecraft.getWindow().getWindow(), GLFW.glfwCreateStandardCursor(GLFW.GLFW_VRESIZE_CURSOR));
super.mouseClicked(mouseX, mouseY, button);
this.setSelected(null);
return true;
}
draggingObject = null;
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
this.setDragging(false);
if (draggingObject != null) {
GLFW.glfwSetCursor(minecraft.getWindow().getWindow(), GLFW.glfwCreateStandardCursor(GLFW.GLFW_ARROW_CURSOR));
if (draggingObject instanceof ResourcePackEntryDuckProvider duckProvider)
duckProvider.setBeingDragged(false);
}
draggingObject = null;
softScrollingTimer = 0;
return super.mouseReleased(mouseX, mouseY, button);
}
@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (button == 0 && updateDragEvent(mouseX, mouseY)) return true;
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
if (draggingObject != null) return true;
return super.mouseScrolled(mouseX, mouseY, amount);
}
boolean updateDragEvent(double mouseX, double mouseY) {
if (mouseX < x0) {
draggingStartX = mouseX;
}
double y = capYCoordinate((int) mouseY, true);
TransferableSelectionList.PackEntry hoveredEntry = this.getEntryAtPosition(mouseX, y);
PackSelectionModel.Entry draggingPack = draggingObject instanceof ResourcePackEntryDuckProvider duckProvider ? duckProvider.getUnderlyingPack() : null;
PackSelectionModel.Entry hoveredPack = hoveredEntry instanceof ResourcePackEntryDuckProvider duckProvider ? duckProvider.getUnderlyingPack() : null;
if (draggingPack != null && hoveredPack != null && draggingPack != hoveredPack && draggingObject instanceof ResourcePackEntryDuckProvider underlyingPackProvider) {
if (draggingPack instanceof AbstractPackDuckProvider abstractPack && dragResourcePack(underlyingPackProvider, draggingStartY, y)) {
draggingStartY = mouseY;
int z = abstractPack.getIndexInSelectedList();
draggingObject = z == -1 ? null : getEntry(z);
if (draggingObject instanceof ResourcePackEntryDuckProvider duckProvider)
duckProvider.setBeingDragged(true);
this.setSelected(null);
return true;
}
}
return false;
}
@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
if (this.draggingObject instanceof ResourcePackEntryDuckProvider duckProvider) {
int z = Mth.floor(mouseY + draggingOffsetY);
int x = Mth.floor(draggingStartX + draggingOffsetX);
int y = capYCoordinate(z);
int entryHeight = this.itemHeight - 4;
int entryWidth = this.getRowWidth();
duckProvider.renderPoppedOut(guiGraphics, 0, y, x, entryWidth, entryHeight, mouseX, mouseY, false, delta);
if (y < z) {
if (softScrollingTimer == 0) {
softScrollingTimer = Util.getMillis();
softScrollingOrigin = getScrollAmount();
}
float f = (float) (Util.getMillis() - softScrollingTimer) / 5f;
setScrollAmount(softScrollingOrigin + f);
} else if (y > z) {
if (softScrollingTimer == 0) {
softScrollingTimer = Util.getMillis();
softScrollingOrigin = getScrollAmount();
}
float f = (float) (Util.getMillis() - softScrollingTimer) / 5f;
setScrollAmount(softScrollingOrigin - f);
} else {
softScrollingTimer = 0;
}
updateDragEvent(mouseX, mouseY);
}
}
int capYCoordinate(int y, boolean useScreenSpace) {
int scrollableTop = y0 + (useScreenSpace ? 0 : (int) Math.max(headerHeight - getScrollAmount() + 2, 0)) + 2;
int scrollableHeight = y1 - y0 - (useScreenSpace ? 0 : itemHeight + (int) Math.max(headerHeight - getScrollAmount() + 2, 0));
if (y < scrollableTop) y = scrollableTop;
if (y > scrollableTop + scrollableHeight) y = scrollableTop + scrollableHeight;
return y;
}
int capYCoordinate(int y) {
return capYCoordinate(y, false);
}
boolean isCapMouseY(int y) {
return capYCoordinate(y, true) == y;
}
boolean dragResourcePack(ResourcePackEntryDuckProvider underlyingPackProvider, double draggingStartY, double mouseY) {
int m = Mth.floor(mouseY - (double) y0) - this.headerHeight + (int) this.getScrollAmount() - 4;
int n = m / this.itemHeight;
PackSelectionModel.Entry pack = underlyingPackProvider.getUnderlyingPack();
if (pack instanceof AbstractPackDuckProvider duckProvider)
duckProvider.moveTo(n);
return true;
}
boolean isValidForDragging(ResourcePackEntryDuckProvider resourcePackEntryDuckProvider) {
return !Draggable.shouldNotTouch(resourcePackEntryDuckProvider.getUnderlyingPack()) && resourcePackEntryDuckProvider.getUnderlyingPack().isSelected();
}
}
@@ -0,0 +1,97 @@
package com.mrmelon54.Draggable.mixin.packs;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mrmelon54.Draggable.Draggable;
import com.mrmelon54.Draggable.duck.ResourcePackEntryDuckProvider;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.packs.PackSelectionModel;
import net.minecraft.client.gui.screens.packs.TransferableSelectionList;
import net.minecraft.resources.ResourceLocation;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(TransferableSelectionList.PackEntry.class)
public abstract class MixinTransferableSelectionList_PackEntry extends ObjectSelectionList.Entry<TransferableSelectionList.PackEntry> implements ResourcePackEntryDuckProvider {
@Shadow
@Final
private PackSelectionModel.Entry pack;
@Shadow
@Final
private TransferableSelectionList parent;
private boolean isBeingDragged = false;
@Override
public PackSelectionModel.Entry getUnderlyingPack() {
return pack;
}
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f, CallbackInfo ci) {
if (isBeingDragged)
ci.cancel();
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 5))
public void removeOnUpArrowButtons(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableResourcePackArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 6))
public void removeOffUpArrowButtons(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableResourcePackArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 7))
public void removeOnDownArrowButtons(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableResourcePackArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 8))
public void removeOffDownArrowButtons(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableResourcePackArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true)
public void removeMoveTowardEnd(double d, double e, int i, CallbackInfoReturnable<Boolean> cir) {
if (!Draggable.getConfig().disableResourcePackArrows) return;
if (d > getRectangle().width() / 2f) return;
double f = d - (double) parent.getRowLeft();
if (f < 16 || f > 32) return;
cir.setReturnValue(true);
cir.cancel();
}
@Override
public void renderPoppedOut(GuiGraphics guiGraphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
if (!isBeingDragged) return;
isBeingDragged = false;
guiGraphics.pose().pushPose();
float z = 191f / 255f;
RenderSystem.setShaderColor(z, z, z, 0.5f);
guiGraphics.fill(x - 1, y - 1, x + entryWidth - 9, y + entryHeight + 1, 0xbfbfbfff);
render(guiGraphics, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta);
RenderSystem.setShaderColor(1, 1, 1, 1);
guiGraphics.pose().popPose();
isBeingDragged = true;
}
@Override
public void setBeingDragged(boolean v) {
isBeingDragged = v;
}
}
@@ -0,0 +1,52 @@
package com.mrmelon54.Draggable.mixin.server;
import com.mrmelon54.Draggable.duck.MultiplayerScreenDuckProvider;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
import net.minecraft.client.gui.screens.multiplayer.ServerSelectionList;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.ServerList;
import net.minecraft.network.chat.Component;
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(JoinMultiplayerScreen.class)
public abstract class MixinJoinMultiplayerScreen extends Screen implements MultiplayerScreenDuckProvider {
@Shadow
protected ServerSelectionList serverSelectionList;
@Shadow
public abstract ServerList getServers();
protected MixinJoinMultiplayerScreen(Component component) {
super(component);
}
@Inject(method = "joinSelectedServer", at = @At("HEAD"))
private void injectedConnect(CallbackInfo info) {
if (serverSelectionList.isDragging()) serverSelectionList.mouseReleased(0, 0, 0);
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (serverSelectionList.isDragging()) serverSelectionList.mouseReleased(mouseX, mouseY, button);
return super.mouseReleased(mouseX, mouseY, button);
}
@Override
public void onClose() {
if (serverSelectionList.isDragging()) serverSelectionList.mouseReleased(0, 0, 0);
super.onClose();
}
@Override
public int getIndexOfServerInfo(ServerData serverData) {
ServerList servers = getServers();
for (int i = 0; i < servers.size(); i++)
if (servers.get(i) == serverData) return i;
return -1;
}
}
@@ -0,0 +1,111 @@
package com.mrmelon54.Draggable.mixin.server;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mrmelon54.Draggable.Draggable;
import com.mrmelon54.Draggable.duck.ServerEntryDuckProvider;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
import net.minecraft.client.gui.screens.multiplayer.ServerSelectionList;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.resources.ResourceLocation;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(ServerSelectionList.OnlineServerEntry.class)
public abstract class MixinOnlineServerEntry extends ObjectSelectionList.Entry<ServerSelectionList.Entry> implements ServerEntryDuckProvider {
@Shadow
@Final
private ServerData serverData;
@Shadow
public abstract void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f);
@Shadow
@Final
private ServerSelectionList field_19117;
@Shadow
@Final
private JoinMultiplayerScreen screen;
@Shadow
private long lastClickTime;
@Shadow
public abstract boolean mouseClicked(double d, double e, int i);
private boolean isBeingDragged;
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f, CallbackInfo ci) {
if (isBeingDragged) ci.cancel();
}
@Override
public ServerData getUnderlyingServer() {
return serverData;
}
@Override
public void renderPoppedOut(GuiGraphics guiGraphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
if (!isBeingDragged) return;
isBeingDragged = false;
guiGraphics.pose().pushPose();
float z = 191f / 255f;
RenderSystem.setShaderColor(z, z, z, 0.5f);
guiGraphics.fill(x - 1, y - 1, x + entryWidth - 2, y + entryHeight + 1, 0xbfbfbfff);
render(guiGraphics, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta);
RenderSystem.setShaderColor(1, 1, 1, 1);
guiGraphics.pose().popPose();
isBeingDragged = true;
}
@Override
public void setBeingDragged(boolean v) {
isBeingDragged = v;
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 3))
public void removeUpOnButton(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableServerArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 4))
public void removeUpButton(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableServerArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 5))
public void removeDownOnButton(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableServerArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIFFIIII)V", ordinal = 6))
public void removeDownButton(GuiGraphics instance, ResourceLocation resourceLocation, int i, int j, float f, float g, int k, int l, int m, int n) {
if (Draggable.getConfig().disableServerArrows) return;
instance.blit(resourceLocation, i, j, f, g, k, l, m, n);
}
@Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true)
public void removeSwapEntries(double d, double e, int i, CallbackInfoReturnable<Boolean> cir) {
double l = field_19117.getRowLeft();
double f = d - l;
// don't click buttons left of 16 pixels
if (f <= 16) {
mouseClicked(l + 32, e, i);
cir.setReturnValue(true);
cir.cancel();
}
}
}
@@ -0,0 +1,22 @@
package com.mrmelon54.Draggable.mixin.server;
import com.mrmelon54.Draggable.duck.ServerListDuckProvider;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.ServerList;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.List;
@Mixin(ServerList.class)
public class MixinServerList implements ServerListDuckProvider {
@Shadow
@Final
private List<ServerData> serverList;
@Override
public void add(int index, ServerData serverData) {
serverList.add(index, serverData);
}
}
@@ -0,0 +1,207 @@
package com.mrmelon54.Draggable.mixin.server;
import com.mrmelon54.Draggable.duck.MultiplayerScreenDuckProvider;
import com.mrmelon54.Draggable.duck.ServerEntryDuckProvider;
import com.mrmelon54.Draggable.duck.ServerListDuckProvider;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
import net.minecraft.client.gui.screens.multiplayer.ServerSelectionList;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.ServerList;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.List;
@Mixin(ServerSelectionList.class)
public abstract class MixinServerSelectionList extends ObjectSelectionList<ServerSelectionList.Entry> {
@Shadow
@Final
private JoinMultiplayerScreen screen;
@Shadow
public abstract void setSelected(@Nullable ServerSelectionList.Entry entry);
@Shadow
@Final
private List<ServerSelectionList.OnlineServerEntry> onlineServers;
@Shadow
public abstract void updateOnlineServers(ServerList serverList);
private ServerSelectionList.OnlineServerEntry draggingObject = null;
private double draggingStartX = 0;
private double draggingStartY = 0;
private double draggingOffsetX = 0;
private double draggingOffsetY = 0;
private long softScrollingTimer = 0;
private double softScrollingOrigin = 0;
public MixinServerSelectionList(Minecraft minecraftClient, int i, int j, int k, int l, int m) {
super(minecraftClient, i, j, k, l, m);
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0 && draggingObject == null && isCapMouseY((int) mouseY)) {
ServerSelectionList.Entry a = this.getEntryAtPosition(mouseX, mouseY);
draggingObject = a instanceof ServerSelectionList.OnlineServerEntry b ? b : null;
if (draggingObject != null && draggingObject instanceof ServerEntryDuckProvider duckProvider) {
// Save the mouse origin position and the offset for the top left corner of the widget
draggingStartX = mouseX;
draggingStartY = mouseY;
draggingOffsetX = getRowLeft() - draggingStartX;
draggingOffsetY = getRowTop(this.children().indexOf(draggingObject)) - draggingStartY;
// Don't grab if inside the server icon
if (draggingOffsetX > -32f) {
draggingObject = null;
return super.mouseClicked(mouseX, mouseY, button);
}
this.setDragging(true);
this.setFocused(draggingObject);
duckProvider.setBeingDragged(true);
softScrollingTimer = 0;
GLFW.glfwSetCursor(minecraft.getWindow().getWindow(), GLFW.glfwCreateStandardCursor(GLFW.GLFW_VRESIZE_CURSOR));
super.mouseClicked(mouseX, mouseY, button);
return true;
} else {
draggingObject = null;
}
}
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
this.setDragging(false);
if (draggingObject != null) {
GLFW.glfwSetCursor(minecraft.getWindow().getWindow(), GLFW.glfwCreateStandardCursor(GLFW.GLFW_ARROW_CURSOR));
if (draggingObject instanceof ServerEntryDuckProvider duckProvider)
duckProvider.setBeingDragged(false);
}
draggingObject = null;
softScrollingTimer = 0;
return super.mouseReleased(mouseX, mouseY, button);
}
@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (button == 0 && updateDragEvent(mouseX, mouseY)) return true;
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
if (draggingObject != null) return true;
return super.mouseScrolled(mouseX, mouseY, amount);
}
boolean updateDragEvent(double mouseX, double mouseY) {
double y = capYCoordinate((int) mouseY, true);
ServerSelectionList.Entry hoveredEntry = this.getEntryAtPosition(mouseX, y);
ServerData draggingPack = draggingObject instanceof ServerEntryDuckProvider duckProvider ? duckProvider.getUnderlyingServer() : null;
ServerData hoveredPack = hoveredEntry instanceof ServerEntryDuckProvider duckProvider ? duckProvider.getUnderlyingServer() : null;
if (draggingPack != null && hoveredPack != null && draggingPack != hoveredPack && draggingObject instanceof ServerEntryDuckProvider serverEntryDuckProvider) {
if (dragServerItem(serverEntryDuckProvider, draggingStartY, y)) {
draggingStartY = mouseY;
if (screen instanceof MultiplayerScreenDuckProvider multiplayerScreenDuckProvider) {
int z = multiplayerScreenDuckProvider.getIndexOfServerInfo(serverEntryDuckProvider.getUnderlyingServer());
draggingObject = z == -1 ? null : (getEntry(z) instanceof ServerSelectionList.OnlineServerEntry b ? b : null);
if (draggingObject instanceof ServerEntryDuckProvider duckProvider)
duckProvider.setBeingDragged(true);
}
return true;
}
}
return false;
}
@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
if (this.draggingObject instanceof ServerEntryDuckProvider duckProvider) {
int z = Mth.floor(mouseY + draggingOffsetY);
int x = Mth.floor(draggingStartX + draggingOffsetX);
int y = capYCoordinate(z);
int entryHeight = this.itemHeight - 4;
int entryWidth = this.getRowWidth();
duckProvider.renderPoppedOut(guiGraphics, 0, y, x, entryWidth, entryHeight, mouseX, mouseY, false, delta);
if (y < z) {
if (softScrollingTimer == 0) {
softScrollingTimer = Util.getMillis();
softScrollingOrigin = getScrollAmount();
}
float f = (float) (Util.getMillis() - softScrollingTimer) / 5f;
setScrollAmount(softScrollingOrigin + f);
} else if (y > z) {
if (softScrollingTimer == 0) {
softScrollingTimer = Util.getMillis();
softScrollingOrigin = getScrollAmount();
}
float f = (float) (Util.getMillis() - softScrollingTimer) / 5f;
setScrollAmount(softScrollingOrigin - f);
} else {
softScrollingTimer = 0;
}
updateDragEvent(mouseX, mouseY);
}
}
int capYCoordinate(int y, boolean useScreenSpace) {
int scrollableTop = y0 + 4;
int scrollableHeight = y1 - y0 - (useScreenSpace ? 2 : itemHeight + 2);
if (y < scrollableTop) y = scrollableTop;
if (y > scrollableTop + scrollableHeight) y = scrollableTop + scrollableHeight;
return y;
}
int capYCoordinate(int y) {
return capYCoordinate(y, false);
}
boolean isCapMouseY(int y) {
return capYCoordinate(y, true) == y;
}
boolean dragServerItem(ServerEntryDuckProvider underlyingServerProvider, double draggingStartY, double mouseY) {
if (screen instanceof MultiplayerScreenDuckProvider multiplayerScreenDuckProvider) {
int i = multiplayerScreenDuckProvider.getIndexOfServerInfo(underlyingServerProvider.getUnderlyingServer());
if (i == -1) return false;
int m = Mth.floor(mouseY - (double) this.y0) - this.headerHeight + (int) this.getScrollAmount() - 4;
int n = m / this.itemHeight;
if (n >= 0 && n < onlineServers.size()) {
moveServerEntry(i, n);
return true;
}
}
return false;
}
void moveServerEntry(int a, int b) {
ServerList servers = this.screen.getServers();
if (servers instanceof ServerListDuckProvider duckProvider) {
ServerData serverData = servers.get(a);
servers.remove(serverData);
duckProvider.add(b, serverData);
servers.save();
updateOnlineServers(servers);
}
}
}
@@ -4,7 +4,15 @@
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"client": [
"MixinTitleScreen"
"packs.MixinPackSelectionModel",
"packs.MixinPackSelectionModel_EntryBase",
"packs.MixinPackSelectionScreen",
"packs.MixinTransferableSelectionList",
"packs.MixinTransferableSelectionList_PackEntry",
"server.MixinJoinMultiplayerScreen",
"server.MixinOnlineServerEntry",
"server.MixinServerList",
"server.MixinServerSelectionList"
],
"mixins": [
],
@@ -1 +1,2 @@
accessWidener v2 named
accessWidener v2 named
accessible class net/minecraft/client/gui/screens/packs/PackSelectionModel$EntryBase
+7
View File
@@ -12,5 +12,12 @@ dependencies {
// Remove the next line if you don't want to depend on the API
modApi "dev.architectury:architectury-fabric:${rootProject.architectury_version}"
modApi("me.shedaniel.cloth:cloth-config-fabric:${rootProject.cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
modApi("com.terraformersmc:modmenu:${rootProject.modmenu_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
compileClasspath(project(path: ":common", configuration: "namedElements")) { transitive false }
}
@@ -13,4 +13,4 @@ public class ModMenuIntegration implements ModMenuApi {
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> AutoConfig.getConfigScreen(ConfigStructure.class, parent).get();
}
}
}
@@ -1,15 +0,0 @@
package com.mrmelon54.Draggable.fabric;
import com.mrmelon54.Draggable.ExampleExpectPlatform;
import net.fabricmc.loader.api.FabricLoader;
import java.nio.file.Path;
public class ExampleExpectPlatformImpl {
/**
* This is our actual method to {@link ExampleExpectPlatform#getConfigDirectory()}.
*/
public static Path getConfigDirectory() {
return FabricLoader.getInstance().getConfigDir();
}
}
@@ -1,15 +0,0 @@
package com.mrmelon54.Draggable.forge;
import com.mrmelon54.Draggable.ExampleExpectPlatform;
import net.minecraftforge.fml.loading.FMLPaths;
import java.nio.file.Path;
public class ExampleExpectPlatformImpl {
/**
* This is our actual method to {@link ExampleExpectPlatform#getConfigDirectory()}.
*/
public static Path getConfigDirectory() {
return FMLPaths.CONFIGDIR.get();
}
}
@@ -1,15 +0,0 @@
package com.mrmelon54.Draggable.quilt;
import com.mrmelon54.Draggable.ExampleExpectPlatform;
import org.quiltmc.loader.api.QuiltLoader;
import java.nio.file.Path;
public class ExampleExpectPlatformImpl {
/**
* This is our actual method to {@link ExampleExpectPlatform#getConfigDirectory()}.
*/
public static Path getConfigDirectory() {
return QuiltLoader.getConfigDir();
}
}