Skip to content

Commit

Permalink
Store container on close
Browse files Browse the repository at this point in the history
  • Loading branch information
Protonull committed Oct 1, 2024
1 parent 828baec commit 814899a
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package uk.protonull.civ.chesttracker;

import java.util.List;
import java.util.Optional;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.CommonButtons;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.BeaconScreen;
import net.minecraft.client.gui.screens.inventory.CartographyTableScreen;
import net.minecraft.client.gui.screens.inventory.EffectRenderingInventoryScreen;
Expand All @@ -8,18 +13,27 @@
import net.minecraft.client.gui.screens.inventory.ItemCombinerScreen;
import net.minecraft.client.gui.screens.inventory.LoomScreen;
import net.minecraft.client.gui.screens.inventory.StonecutterScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import red.jackf.chesttracker.api.ChestTrackerPlugin;
import red.jackf.chesttracker.api.gui.ScreenBlacklist;
import red.jackf.chesttracker.api.memory.Memory;
import red.jackf.chesttracker.api.providers.MemoryLocation;
import red.jackf.chesttracker.api.providers.ProviderUtils;
import red.jackf.chesttracker.api.providers.ServerProvider;
import red.jackf.chesttracker.api.providers.context.ScreenCloseContext;
import red.jackf.chesttracker.api.providers.context.ScreenOpenContext;
import red.jackf.chesttracker.impl.memory.MemoryBankImpl;
import red.jackf.chesttracker.impl.memory.MemoryKeyImpl;
import red.jackf.jackfredlib.client.api.gps.Coordinate;
import uk.protonull.civ.chesttracker.gui.screens.ContainerLocationDeciderScreen;
import uk.protonull.civ.chesttracker.mixing.InventoryWindow;
import uk.protonull.civ.chesttracker.utilities.Shortcuts;

public final class CivChestTrackerPlugin implements ChestTrackerPlugin {
public static final String ID = "civchesttracker";

/**
* Pretty heavily based on {@link red.jackf.chesttracker.impl.DefaultChestTrackerPlugin}
*/
Expand Down Expand Up @@ -53,21 +67,77 @@ public ResourceLocation id() {
public boolean appliesTo(
final @NotNull Coordinate coordinate
) {
CommonButtons lol;

return true; // Apply to singleplayer, multiplayer, etc
}

@Override
public void onScreenOpen(
final @NotNull ScreenOpenContext context
) {

final AbstractContainerScreen<?> screen = context.getScreen();
if (ScreenBlacklist.isBlacklisted(screen.getClass())) {
return;
}
final MemoryBankImpl serverStorage = Shortcuts.getChestTrackerStorage();
if (serverStorage == null) {
return;
}
final ResourceLocation dimensionKey = Shortcuts.getDimensionKeyFromInventoryWindow(screen);
final MemoryKeyImpl dimensionStorage = serverStorage.getKeyInternal(dimensionKey).orElse(null);
if (dimensionStorage == null) {
return;
}
final Memory foundContainer = Shortcuts.destructivelyFindFirstContainerWithName(
dimensionStorage,
context.getScreen().getTitle()
);
if (foundContainer == null) {
return;
}
InventoryWindow.setIdentifier(screen, MemoryLocation.inWorld(
dimensionKey,
Shortcuts.getMemoryLocation(foundContainer)
));
}

@Override
public void onScreenClose(
final ScreenCloseContext context
final @NotNull ScreenCloseContext context
) {

final MemoryBankImpl serverStorage = Shortcuts.getChestTrackerStorage();
if (serverStorage == null) {
return;
}
final MemoryLocation inventoryIdentifier = InventoryWindow.getIdentifier(context.getScreen());
if (inventoryIdentifier == null) {
return;
}
final boolean alreadyExisted = serverStorage.getMemory(inventoryIdentifier).isPresent();
final var memory = new Memory(
context.getItems(),
context.getTitle(),
List.of(),
Optional.empty(),
Memory.UNKNOWN_LOADED_TIMESTAMP,
Memory.UNKNOWN_WORLD_TIMESTAMP,
Memory.UNKNOWN_REAL_TIMESTAMP
);
Shortcuts.setMemoryLocation(memory, inventoryIdentifier.position());
serverStorage.addMemory(
inventoryIdentifier.memoryKey(),
inventoryIdentifier.position(),
memory
);
Shortcuts.printMessage(
Component
.literal(alreadyExisted
? "Updated inventory '" + context.getTitle().getString() + "'"
: "Now tracking inventory '" + context.getTitle().getString() + "'"
)
.withStyle(ChatFormatting.GREEN)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import uk.protonull.civ.chesttracker.gui.widgets.TextRenderable;
import uk.protonull.civ.chesttracker.mixing.InventoryWindow;
import uk.protonull.civ.chesttracker.utilities.Shortcuts;

public class ContainerLocationDeciderScreen extends Screen {
Expand All @@ -43,11 +44,11 @@ public ContainerLocationDeciderScreen(
final @NotNull AbstractContainerScreen<?> parent,
final @NotNull Consumer<@NotNull BlockPos> setInventoryLocation
) {
super(Component.empty());
super(Component.translatable("civchesttracker.picker.header"));
this.parent = Objects.requireNonNull(parent);
this.setInventoryLocation = Objects.requireNonNull(setInventoryLocation);
{
final LocalPlayer player = Shortcuts.getPlayerFromInventoryWindow(parent);
final LocalPlayer player = InventoryWindow.getPlayer(parent);
this.playerEyePosition = player.getEyePosition();
this.playerLookDirection = player.getLookAngle().normalize();
}
Expand All @@ -60,7 +61,7 @@ protected void init() {
this.font,
this.width / 2,
20,
Component.literal("Where is this inventory?"),
Component.translatable("civchesttracker.picker.header"),
PICKER_COLOUR_RBG
));

Expand All @@ -69,7 +70,7 @@ protected void init() {
this.height - 40 - Button.DEFAULT_HEIGHT,
Button.DEFAULT_WIDTH,
Button.DEFAULT_HEIGHT,
Component.literal("Confirm"),
Component.translatable("civchesttracker.picker.confirm"),
(button) -> {
final BlockPos blockPos = currentPickerLocation;
if (blockPos != null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package uk.protonull.civ.chesttracker.gui.widgets;

import com.google.common.util.concurrent.Runnables;
import java.util.function.Consumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import red.jackf.chesttracker.api.providers.MemoryLocation;
import red.jackf.chesttracker.impl.gui.invbutton.ui.SecondaryButton;
import red.jackf.chesttracker.impl.gui.screen.MemoryBankManagerScreen;
import red.jackf.chesttracker.impl.memory.MemoryBankImpl;
import red.jackf.chesttracker.impl.util.GuiUtil;
import uk.protonull.civ.chesttracker.CivChestTrackerPlugin;
import uk.protonull.civ.chesttracker.gui.screens.ContainerLocationDeciderScreen;
import uk.protonull.civ.chesttracker.utilities.Shortcuts;

public final class OpenedInventoryButtons {
public static final SecondaryButton BLACKLISTED = new SecondaryButton(
new WidgetSprites(
ResourceLocation.fromNamespaceAndPath(CivChestTrackerPlugin.ID, "cannot"),
ResourceLocation.fromNamespaceAndPath(CivChestTrackerPlugin.ID, "cannot_highlighted")
),
Component.translatable("civchesttracker.inventoryButton.blacklisted"),
Runnables.doNothing()
);

public static final class NoMemoryBank extends SecondaryButton {
private static final WidgetSprites SPRITES = new WidgetSprites(
ResourceLocation.fromNamespaceAndPath(CivChestTrackerPlugin.ID, "unknown"),
ResourceLocation.fromNamespaceAndPath(CivChestTrackerPlugin.ID, "unknown_highlighted")
);
public NoMemoryBank(
final @NotNull AbstractContainerScreen<?> parent
) {
super(
SPRITES,
Component.translatable("civchesttracker.inventoryButton.nomemorybank"),
() -> Minecraft.getInstance().setScreen(new MemoryBankManagerScreen(parent, Runnables.doNothing()))
);
}
}

public static final class ForgetContainer extends SecondaryButton {
private static final WidgetSprites SPRITES = GuiUtil.twoSprite("inventory_button/forget");
public ForgetContainer(
final @NotNull AbstractContainerScreen<?> parent,
final @NotNull MemoryBankImpl serverStorage,
final @NotNull MemoryLocation target
) {
super(
SPRITES,
Component.translatable("civchesttracker.inventoryButton.forgetcontainer"),
() -> {
serverStorage.removeMemory(target.memoryKey(), target.position());
Shortcuts.forciblyRefreshScreen(parent);
}
);
}
}

public static final class TrackContainer extends SecondaryButton {
private static final WidgetSprites SPRITES = GuiUtil.twoSprite("inventory_button/remember_container/never");
public TrackContainer(
final @NotNull AbstractContainerScreen<?> parent,
final @NotNull Consumer<@NotNull BlockPos> setInventoryLocation
) {
super(
SPRITES,
Component.translatable("civchesttracker.inventoryButton.trackcontainer"),
() -> Minecraft.getInstance().setScreen(new ContainerLocationDeciderScreen(
parent,
setInventoryLocation
))
);
}
}
}

This file was deleted.

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

import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.player.LocalPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import red.jackf.chesttracker.api.providers.MemoryLocation;

public interface InventoryWindow {
@NotNull LocalPlayer civchesttracker$getPlayer();

@Nullable MemoryLocation civchesttracker$getInventoryIdentifier();

void civchesttracker$setInventoryIdentifier(
MemoryLocation inventoryIdentifier
);

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

static @Nullable MemoryLocation getIdentifier(
final @NotNull AbstractContainerScreen<?> screen
) {
return ((InventoryWindow) screen).civchesttracker$getInventoryIdentifier();
}

static void setIdentifier(
final @NotNull AbstractContainerScreen<?> screen,
final MemoryLocation inventoryIdentifier
) {
((InventoryWindow) screen).civchesttracker$setInventoryIdentifier(inventoryIdentifier);
}
}
Loading

0 comments on commit 814899a

Please sign in to comment.