Isolate other clientside only things (fixes GH-3 for real this time)

This commit is contained in:
Cynthia Foxwell 2024-03-18 11:27:01 -06:00
parent e323de2d57
commit a70df52234
7 changed files with 70 additions and 43 deletions

View file

@ -3,11 +3,6 @@ package pm.c7.scout;
import dev.emi.trinkets.api.SlotReference;
import dev.emi.trinkets.api.TrinketComponent;
import dev.emi.trinkets.api.TrinketsApi;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.BeaconScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
@ -130,20 +125,4 @@ public class ScoutUtil {
out.addAll(scoutScreenHandler.scout$getRightPouchSlots());
return out;
}
public static @Nullable PlayerScreenHandler getPlayerScreenHandler() {
var client = MinecraftClient.getInstance();
if (client != null && client.player != null) {
return client.player.playerScreenHandler;
}
return null;
}
// FIXME: registry system for mods to register their own blacklisted screens
public static boolean isScreenBlacklisted(Screen screen) {
return screen instanceof CreativeInventoryScreen
|| screen instanceof MerchantScreen // FIXME: needs repositioning
|| screen instanceof BeaconScreen; // FIXME: needs repositioning
}
}

View file

@ -110,7 +110,7 @@ public class ScoutClient implements ClientModInitializer {
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (screen instanceof HandledScreen<?> handledScreen && client.player != null) {
if (ScoutUtil.isScreenBlacklisted(screen)) {
if (ScoutUtilClient.isScreenBlacklisted(screen)) {
// realistically no one is going to have a screen bigger than 2147483647 pixels
for (Slot slot : ScoutUtil.getAllBagSlots(client.player.playerScreenHandler)) {
BagSlot bagSlot = (BagSlot) slot;

View file

@ -0,0 +1,28 @@
package pm.c7.scout.client;
import org.jetbrains.annotations.Nullable;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.BeaconScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.screen.PlayerScreenHandler;
public class ScoutUtilClient {
public static @Nullable PlayerScreenHandler getPlayerScreenHandler() {
var client = MinecraftClient.getInstance();
if (client != null && client.player != null) {
return client.player.playerScreenHandler;
}
return null;
}
// FIXME: registry system for mods to register their own blacklisted screens
public static boolean isScreenBlacklisted(Screen screen) {
return screen instanceof CreativeInventoryScreen
|| screen instanceof MerchantScreen // FIXME: needs repositioning
|| screen instanceof BeaconScreen; // FIXME: needs repositioning
}
}

View file

@ -15,7 +15,6 @@ 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;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import pm.c7.scout.ScoutMixin.Transformer;
import pm.c7.scout.ScoutUtil;
@ -23,18 +22,6 @@ import pm.c7.scout.ScoutUtil;
@Mixin(value = ScreenHandler.class, priority = 950)
@Transformer(ScreenHandlerTransformer.class)
public abstract class ScreenHandlerMixin {
@Inject(method = "getSlot", at = @At("HEAD"), cancellable = true)
public void scout$fixGetSlot(int index, CallbackInfoReturnable<Slot> cir) {
var playerScreenHandler = ScoutUtil.getPlayerScreenHandler();
if (ScoutUtil.isBagSlot(index)) {
if (playerScreenHandler != null) {
cir.setReturnValue(ScoutUtil.getBagSlot(index, playerScreenHandler));
} else {
cir.setReturnValue(null);
}
}
}
@Inject(method = "internalOnSlotClick", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/screen/ScreenHandler;getCursorStack()Lnet/minecraft/item/ItemStack;", ordinal = 11), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
public void scout$fixDoubleClick(int slotIndex, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci, PlayerInventory playerInventory, Slot slot3) {
var cursorStack = this.getCursorStack();

View file

@ -1,18 +1,22 @@
package pm.c7.scout.mixin;
package pm.c7.scout.mixin.client;
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 net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.collection.DefaultedList;
import pm.c7.scout.ScoutUtil;
import pm.c7.scout.client.ScoutUtilClient;
@Environment(EnvType.CLIENT)
@Mixin(DefaultedList.class)
public class DefaultedListMixin {
@Inject(method = "get", at = @At("HEAD"), cancellable = true)
public void scout$fixIndexingSlots(int index, CallbackInfoReturnable<Object> cir) {
var playerScreenHandler = ScoutUtil.getPlayerScreenHandler();
var playerScreenHandler = ScoutUtilClient.getPlayerScreenHandler();
if (ScoutUtil.isBagSlot(index)) {
if (playerScreenHandler != null) {
cir.setReturnValue(ScoutUtil.getBagSlot(index, playerScreenHandler));

View file

@ -1,6 +1,5 @@
package pm.c7.scout.mixin.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
@ -21,6 +20,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import pm.c7.scout.ScoutMixin;
import pm.c7.scout.ScoutUtil;
import pm.c7.scout.client.ScoutUtilClient;
import pm.c7.scout.item.BaseBagItem;
import pm.c7.scout.screen.BagSlot;
@ -46,7 +46,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawBackground(Lnet/minecraft/client/gui/DrawContext;FII)V"))
private void scout$drawSatchelRow(DrawContext graphics, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) {
if (this.client != null && this.client.player != null && !ScoutUtilClient.isScreenBlacklisted(this)) {
ItemStack backStack = ScoutUtil.findBagItem(this.client.player, BaseBagItem.BagType.SATCHEL, false);
if (!backStack.isEmpty()) {
BaseBagItem bagItem = (BaseBagItem) backStack.getItem();
@ -97,7 +97,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;disableDepthTest()V", remap = false))
private void scout$drawPouchSlots(DrawContext graphics, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) {
if (this.client != null && this.client.player != null && !ScoutUtilClient.isScreenBlacklisted(this)) {
ItemStack leftPouchStack = ScoutUtil.findBagItem(this.client.player, BaseBagItem.BagType.POUCH, false);
if (!leftPouchStack.isEmpty()) {
BaseBagItem bagItem = (BaseBagItem) leftPouchStack.getItem();
@ -234,7 +234,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen
@Inject(method = "isClickOutsideBounds", at = @At("TAIL"), cancellable = true)
private void scout$adjustOutsideBounds(double mouseX, double mouseY, int left, int top, int button, CallbackInfoReturnable<Boolean> callbackInfo) {
if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) {
if (this.client != null && this.client.player != null && !ScoutUtilClient.isScreenBlacklisted(this)) {
ItemStack backStack = ScoutUtil.findBagItem(this.client.player, BaseBagItem.BagType.SATCHEL, false);
if (!backStack.isEmpty()) {
BaseBagItem bagItem = (BaseBagItem) backStack.getItem();
@ -272,7 +272,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawForeground(Lnet/minecraft/client/gui/DrawContext;II)V"))
public void scout$drawOurSlots(DrawContext graphics, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) {
if (this.client != null && this.client.player != null && !ScoutUtilClient.isScreenBlacklisted(this)) {
for (int i = ScoutUtil.SATCHEL_SLOT_START; i > ScoutUtil.BAG_SLOTS_END; i--) {
BagSlot slot = (BagSlot) ScoutUtil.getBagSlot(i, this.client.player.playerScreenHandler);
if (slot != null && slot.isEnabled()) {
@ -298,7 +298,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen
@Inject(method = "getSlotAt", at = @At("RETURN"), cancellable = true)
public void scout$addSlots(double x, double y, CallbackInfoReturnable<Slot> cir) {
if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) {
if (this.client != null && this.client.player != null && !ScoutUtilClient.isScreenBlacklisted(this)) {
for (int i = ScoutUtil.SATCHEL_SLOT_START; i > ScoutUtil.BAG_SLOTS_END; i--) {
BagSlot slot = (BagSlot) ScoutUtil.getBagSlot(i, this.client.player.playerScreenHandler);
if (this.isPointOverSlot(slot, x, y) && slot != null && slot.isEnabled()) {

View file

@ -0,0 +1,29 @@
package pm.c7.scout.mixin.client;
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 net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import pm.c7.scout.ScoutUtil;
import pm.c7.scout.client.ScoutUtilClient;
@Environment(EnvType.CLIENT)
@Mixin(value = ScreenHandler.class, priority = 950)
public abstract class ScreenHandlerMixin {
@Inject(method = "getSlot", at = @At("HEAD"), cancellable = true)
public void scout$fixGetSlot(int index, CallbackInfoReturnable<Slot> cir) {
var playerScreenHandler = ScoutUtilClient.getPlayerScreenHandler();
if (ScoutUtil.isBagSlot(index)) {
if (playerScreenHandler != null) {
cir.setReturnValue(ScoutUtil.getBagSlot(index, playerScreenHandler));
} else {
cir.setReturnValue(null);
}
}
}
}