Skip to content

Commit

Permalink
Retain a reference to the player through the inventory window
Browse files Browse the repository at this point in the history
  • Loading branch information
Protonull committed Oct 1, 2024
1 parent ff06871 commit 828baec
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.debug.DebugRenderer;
import net.minecraft.core.BlockPos;
Expand All @@ -31,23 +32,25 @@ public class ContainerLocationDeciderScreen extends Screen {
private static final int MIN_DISTANCE = 1;
private static final int MAX_DISTANCE = 5;

private final Screen parent;
private final AbstractContainerScreen<?> parent;
private final Consumer<BlockPos> setInventoryLocation;
private final Vec3 playerEyePosition;
private final Vec3 playerLookDirection;

private int distance = 1;

public ContainerLocationDeciderScreen(
final @NotNull Screen parent,
final @NotNull Consumer<@NotNull BlockPos> setInventoryLocation,
final @NotNull LocalPlayer player
final @NotNull AbstractContainerScreen<?> parent,
final @NotNull Consumer<@NotNull BlockPos> setInventoryLocation
) {
super(Component.empty());
this.parent = Objects.requireNonNull(parent);
this.setInventoryLocation = Objects.requireNonNull(setInventoryLocation);
this.playerEyePosition = player.getEyePosition();
this.playerLookDirection = player.getLookAngle().normalize();
{
final LocalPlayer player = Shortcuts.getPlayerFromInventoryWindow(parent);
this.playerEyePosition = player.getEyePosition();
this.playerLookDirection = player.getLookAngle().normalize();
}
calculateNewDeciderLocation();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.protonull.civ.chesttracker.mixing;

import net.minecraft.client.player.LocalPlayer;
import org.jetbrains.annotations.NotNull;

public interface InventoryWindow {
@NotNull LocalPlayer civchesttracker$getPlayer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ private void addExtraButtons(
return;
}
// TODO: Lol
final LocalPlayer player = Minecraft.getInstance().player;
if (player == null) {
return;
}
final LocalPlayer player = Shortcuts.getPlayerFromInventoryWindow(parent);
final ResourceLocation dimensionKey = player.clientLevel.dimension().location();
final MemoryKeyImpl dimensionStorage = Shortcuts.getStorageForDimension(serverStorage, dimensionKey);
if (dimensionStorage == null) {
Expand All @@ -105,8 +102,7 @@ private void addExtraButtons(
this.secondaryButtons.add(new TrackContainerButton(() -> {
Minecraft.getInstance().setScreen(new ContainerLocationDeciderScreen(
parent,
(blockPos) -> setRestoreLocation(parent, MemoryLocation.inWorld(dimensionKey, blockPos)),
player
(blockPos) -> setRestoreLocation(parent, MemoryLocation.inWorld(dimensionKey, blockPos))
));
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.llamalad7.mixinextras.sugar.Local;
import java.util.Optional;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -40,10 +39,7 @@ public abstract class SetBlockPositionBasedOnMemoryMixin {
if (serverStorage == null) {
return Optional.empty();
}
final LocalPlayer player = Minecraft.getInstance().player;
if (player == null) {
return Optional.empty();
}
final LocalPlayer player = Shortcuts.getPlayerFromInventoryWindow(context.getScreen());
final MemoryKeyImpl dimensionStorage = Shortcuts.getStorageForDimension(
serverStorage,
player.clientLevel.dimension().location()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.protonull.civ.chesttracker.mixins.retention;

import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import uk.protonull.civ.chesttracker.mixing.InventoryWindow;

@Mixin(AbstractContainerScreen.class)
public abstract class RetainPlayerOnInventoryWindowMixin implements InventoryWindow {
@Unique
private LocalPlayer player;

@Redirect(
method = "<init>",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/player/Inventory;getDisplayName()Lnet/minecraft/network/chat/Component;"
)
)
protected @NotNull Component civchesttracker$interceptPlayerInstance(
final @NotNull Inventory playerInventory
) {
this.player = (LocalPlayer) playerInventory.player;
return playerInventory.getDisplayName();
}

@Override
public @NotNull LocalPlayer civchesttracker$getPlayer() {
return this.player;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.phys.Vec3;
Expand All @@ -11,6 +13,7 @@
import red.jackf.chesttracker.impl.memory.MemoryBankAccessImpl;
import red.jackf.chesttracker.impl.memory.MemoryBankImpl;
import red.jackf.chesttracker.impl.memory.MemoryKeyImpl;
import uk.protonull.civ.chesttracker.mixing.InventoryWindow;
import uk.protonull.civ.chesttracker.mixins.MemoryAccessor;

public final class Shortcuts {
Expand Down Expand Up @@ -48,6 +51,12 @@ public static void forciblyRefreshScreen(
);
}

public static @NotNull LocalPlayer getPlayerFromInventoryWindow(
final @NotNull AbstractContainerScreen<?> screen
) {
return ((InventoryWindow) screen).civchesttracker$getPlayer();
}

public static @NotNull BlockPos asBlockPos(
final @NotNull Vec3 vec3
) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/civchesttracker.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"illegal.NukeConnectedBlocksGrabberMixin",
"illegal.NukeInteractionTrackerImplMixin",

"retention.RetainPlayerOnInventoryWindowMixin",

"InventoryContextButtonsMixin",
"MemoryAccessor",
"MemoryMixin",
Expand Down

0 comments on commit 828baec

Please sign in to comment.