Skip to content

Commit

Permalink
feat: ✨ Added contents tooltip to minecarts so that Shulker minecarts…
Browse files Browse the repository at this point in the history
… and packed minecarts can show their contents in tooltip similar to how backpacks do it
  • Loading branch information
P3pp3rF1y committed Jan 16, 2025
1 parent 061ee72 commit b53b0e9
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedstorageinmotion
mod_group_id=sophisticatedstorageinmotion
mod_version=0.7.4
mod_version=0.8.0
sonar_project_key=sophisticatedstorageinmotion:SophisticatedStorageInMotion
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedStorageInMotion

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.client;

import net.minecraftforge.client.event.RegisterClientTooltipComponentFactoriesEvent;
import net.minecraftforge.client.event.RegisterGuiOverlaysEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.p3pp3rf1y.sophisticatedstorageinmotion.client.gui.MovingStorageScreen;
import net.p3pp3rf1y.sophisticatedstorageinmotion.client.gui.PaintbrushMovingStorageOverlay;
import net.p3pp3rf1y.sophisticatedstorageinmotion.item.MovingStorageItem;

public class ClientEventHandler {
private ClientEventHandler() {
}

public static void registerHandlers(IEventBus modBus) {
modBus.addListener(ClientEventHandler::registerOverlay);
modBus.addListener(ClientEventHandler::registerTooltipComponent);

IEventBus eventBus = MinecraftForge.EVENT_BUS;
eventBus.addListener(ClientMovingStorageContentsTooltip::onWorldLoad);

net.p3pp3rf1y.sophisticatedstorage.client.ClientEventHandler.addSortScreenMatcher(screen -> screen instanceof MovingStorageScreen);
}

private static void registerOverlay(RegisterGuiOverlaysEvent event) {
event.registerAbove(VanillaGuiOverlay.HOTBAR.id(), "paintbrush_moving_storage_info", PaintbrushMovingStorageOverlay.HUD_PAINTBRUSH_INFO);
}

private static void registerTooltipComponent(RegisterClientTooltipComponentFactoriesEvent event) {
event.register(MovingStorageItem.MovingStorageContentsTooltip.class, ClientMovingStorageContentsTooltip::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.client;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.level.LevelEvent;
import net.p3pp3rf1y.sophisticatedcore.client.render.ClientStorageContentsTooltipBase;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorageinmotion.item.MovingStorageItem;
import net.p3pp3rf1y.sophisticatedstorageinmotion.network.RequestMovingStorageInventoryContentsMessage;
import net.p3pp3rf1y.sophisticatedstorageinmotion.network.StorageInMotionPacketHandler;

import java.util.UUID;

public class ClientMovingStorageContentsTooltip extends ClientStorageContentsTooltipBase {
private final ItemStack movingStorage;

@SuppressWarnings("unused")
//parameter needs to be there so that addListener logic would know which event this method listens to
public static void onWorldLoad(LevelEvent.Load event) {
refreshContents();
lastRequestTime = 0;
}

@Override
public void renderImage(Font font, int leftX, int topY, GuiGraphics guiGraphics) {
renderTooltip(MovingStorageWrapper.fromStack(movingStorage, () -> {}, () -> {}), font, leftX, topY, guiGraphics);
}

public ClientMovingStorageContentsTooltip(MovingStorageItem.MovingStorageContentsTooltip tooltip) {
movingStorage = tooltip.getMovingStorage();
}

@Override
protected void sendInventorySyncRequest(UUID uuid) {
StorageInMotionPacketHandler.INSTANCE.sendToServer(new RequestMovingStorageInventoryContentsMessage(uuid));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.p3pp3rf1y.sophisticatedcore.Config;
import net.p3pp3rf1y.sophisticatedcore.api.IStashStorageItem;
import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory;
import net.p3pp3rf1y.sophisticatedcore.util.ColorHelper;
import net.p3pp3rf1y.sophisticatedcore.util.ItemBase;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
Expand All @@ -15,17 +19,19 @@
import net.p3pp3rf1y.sophisticatedstorage.block.ITintableBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedstorage.item.BarrelBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.ShulkerBoxItem;
import net.p3pp3rf1y.sophisticatedstorage.item.StorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.WoodStorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.EntityStorageHolder;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageWrapper;

import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

import static net.p3pp3rf1y.sophisticatedstorage.block.DecorationTableBlockEntity.STORAGE_DECORATOR;

public abstract class MovingStorageItem extends ItemBase {
public abstract class MovingStorageItem extends ItemBase implements IStashStorageItem {
public MovingStorageItem(Properties properties) {
super(properties);
}
Expand Down Expand Up @@ -89,6 +95,43 @@ public Component getName(ItemStack stack) {
return NBTHelper.getCompound(stack, EntityStorageHolder.STORAGE_ITEM_TAG).map(ItemStack::of).<Component>map(storageItem -> Component.translatable(getDescriptionId(), storageItem.getHoverName())).orElse(super.getName(stack));
}

@Override
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
if (FMLEnvironment.dist.isClient()) {
return Optional.ofNullable(MovingStorageItemClient.getTooltipImage(getStorageItem(stack)));
}
return Optional.empty();
}

@Override
public Optional<TooltipComponent> getInventoryTooltip(ItemStack stack) {
return Optional.of(new MovingStorageContentsTooltip(stack));
}

@Override
public StashResult getItemStashable(ItemStack storageStack, ItemStack stack) {
if (getStorageItemType(stack).map(item -> item instanceof ShulkerBoxItem).orElse(false)) {
MovingStorageWrapper wrapper = MovingStorageWrapper.fromStack(getStorageItem(storageStack), () -> {}, () -> {});

if (wrapper.getInventoryForUpgradeProcessing().insertItem(stack, true).getCount() == stack.getCount()) {
return StashResult.NO_SPACE;
}
if (wrapper.getInventoryHandler().getSlotTracker().getItems().contains(stack.getItem()) || wrapper.getSettingsHandler().getTypeCategory(MemorySettingsCategory.class).matchesFilter(stack)) {
return StashResult.MATCH_AND_SPACE;
}

return StashResult.SPACE;
}

return StashResult.NO_SPACE;
}

public record MovingStorageContentsTooltip(ItemStack movingStorage) implements TooltipComponent {
public ItemStack getMovingStorage() {
return movingStorage;
}
}

static {
DecorationTableBlockEntity.registerItemDecorator(stack -> stack.getItem() instanceof MovingStorageItem, new DecorationTableBlockEntity.IItemDecorator() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.item;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;

import javax.annotation.Nullable;

public class MovingStorageItemClient {
@Nullable
public static TooltipComponent getTooltipImage(ItemStack stack) {
Minecraft mc = Minecraft.getInstance();
if (Screen.hasShiftDown() || (mc.player != null && !mc.player.containerMenu.getCarried().isEmpty())) {
return new MovingStorageItem.MovingStorageContentsTooltip(stack);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import net.p3pp3rf1y.sophisticatedcore.client.render.ClientStorageContentsTooltipBase;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageData;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -36,5 +37,6 @@ private static void handleMessage(MovingStorageContentsMessage msg) {
}

MovingStorageData.get(msg.storageUuid).setContents(msg.storageUuid, msg.contents);
ClientStorageContentsTooltipBase.refreshContents();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.network;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeHandler;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageWrapper;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageData;

import javax.annotation.Nullable;
import java.util.UUID;
import java.util.function.Supplier;

public class RequestMovingStorageInventoryContentsMessage {
private final UUID storageUuid;

public RequestMovingStorageInventoryContentsMessage(UUID storageUuid) {
this.storageUuid = storageUuid;
}

public static void encode(RequestMovingStorageInventoryContentsMessage msg, FriendlyByteBuf packetBuffer) {
packetBuffer.writeUUID(msg.storageUuid);
}

public static RequestMovingStorageInventoryContentsMessage decode(FriendlyByteBuf packetBuffer) {
return new RequestMovingStorageInventoryContentsMessage(packetBuffer.readUUID());
}

static void onMessage(RequestMovingStorageInventoryContentsMessage msg, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
context.enqueueWork(() -> handleMessage(context.getSender(), msg));
context.setPacketHandled(true);
}

public static void handleMessage(@Nullable ServerPlayer player, RequestMovingStorageInventoryContentsMessage msg) {
if (player == null) {
return;
}

CompoundTag baseContentsTag = MovingStorageData.get(msg.storageUuid).getContents();
if (!baseContentsTag.contains(StorageWrapper.CONTENTS_TAG)) {
return;
}
CompoundTag contentsTag = baseContentsTag.getCompound(StorageWrapper.CONTENTS_TAG);

CompoundTag inventoryContents = new CompoundTag();
Tag inventoryNbt = contentsTag.get(InventoryHandler.INVENTORY_TAG);
if (inventoryNbt != null) {
inventoryContents.put(InventoryHandler.INVENTORY_TAG, inventoryNbt);
}
Tag upgradeNbt = contentsTag.get(UpgradeHandler.UPGRADE_INVENTORY_TAG);
if (upgradeNbt != null) {
inventoryContents.put(UpgradeHandler.UPGRADE_INVENTORY_TAG, upgradeNbt);
}
CompoundTag newBaseContentsTag = new CompoundTag();
newBaseContentsTag.put(StorageWrapper.CONTENTS_TAG, inventoryContents);
StorageInMotionPacketHandler.INSTANCE.sendToClient(player, new MovingStorageContentsMessage(msg.storageUuid, newBaseContentsTag));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ private StorageInMotionPacketHandler(String modId) {
public void init() {
registerMessage(OpenMovingStorageInventoryMessage.class, OpenMovingStorageInventoryMessage::encode, OpenMovingStorageInventoryMessage::decode, OpenMovingStorageInventoryMessage::onMessage);
registerMessage(MovingStorageContentsMessage.class, MovingStorageContentsMessage::encode, MovingStorageContentsMessage::decode, MovingStorageContentsMessage::onMessage);
registerMessage(RequestMovingStorageInventoryContentsMessage.class, RequestMovingStorageInventoryContentsMessage::encode, RequestMovingStorageInventoryContentsMessage::decode, RequestMovingStorageInventoryContentsMessage::onMessage);
}
}

0 comments on commit b53b0e9

Please sign in to comment.