Skip to content

Commit

Permalink
fix: Convert all Pair objects to Apache Commons.
Browse files Browse the repository at this point in the history
fix: Tooltip rendering for InventoryOverlayScreen handling for calling the `IRender.onRenderTooltipLast` handling
feat: Item Tooltip handlers for inserting text into Item Tooltips at 3 places; First, Middle, and Last.
feat: Migrate InventoryOverlayScreen to MaLiLib and add a common Interface for downstream mods called `IInventoryOverlayHandler` with an accompanying `IDataSyncer` interface as just a common way to call the mods' assigned Data Syncer.
  • Loading branch information
sakura-ryoko committed Dec 30, 2024
1 parent c0cdc23 commit 9d6530c
Show file tree
Hide file tree
Showing 28 changed files with 1,132 additions and 284 deletions.
6 changes: 3 additions & 3 deletions src/main/java/fi/dy/masa/malilib/MaLiLibConfigGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import fi.dy.masa.malilib.gui.button.ButtonBase;
import fi.dy.masa.malilib.gui.button.ButtonGeneric;
import fi.dy.masa.malilib.gui.button.IButtonActionListener;
import fi.dy.masa.malilib.test.TestEnumConfig;
import fi.dy.masa.malilib.test.ConfigTestEnum;
import fi.dy.masa.malilib.util.StringUtils;

public class MaLiLibConfigGui extends GuiConfigsBase
{
private static ConfigGuiTab tab = ConfigGuiTab.GENERIC;
public static ImmutableList<TestEnumConfig> TEST_ENUM_LIST = TestEnumConfig.VALUES;
public static ImmutableList<ConfigTestEnum> TEST_ENUM_LIST = ConfigTestEnum.VALUES;

public MaLiLibConfigGui()
{
Expand Down Expand Up @@ -111,7 +111,7 @@ else if (tab == ConfigGuiTab.EXPERIMENTAL && MaLiLibReference.EXPERIMENTAL_MODE)
return ConfigOptionWrapper.createFor(configs);
}

protected BooleanHotkeyGuiWrapper wrapConfig(TestEnumConfig config)
protected BooleanHotkeyGuiWrapper wrapConfig(ConfigTestEnum config)
{
return new BooleanHotkeyGuiWrapper(config.getName(), config, config.getKeybind());
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/fi/dy/masa/malilib/MaLiLibConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import fi.dy.masa.malilib.config.IConfigValue;
import fi.dy.masa.malilib.config.options.*;
import fi.dy.masa.malilib.hotkeys.IHotkey;
import fi.dy.masa.malilib.hotkeys.KeyAction;
import fi.dy.masa.malilib.hotkeys.KeybindSettings;
import fi.dy.masa.malilib.test.ConfigTestEnum;
import fi.dy.masa.malilib.test.ConfigTestLockedList;
import fi.dy.masa.malilib.test.ConfigTestOptList;
import fi.dy.masa.malilib.test.TestEnumConfig;
import fi.dy.masa.malilib.util.FileUtils;
import fi.dy.masa.malilib.util.JsonUtils;
import fi.dy.masa.malilib.util.Color4f;
Expand Down Expand Up @@ -74,6 +76,7 @@ public static class Debug
}

private static final String TEST_KEY = MaLiLibReference.MOD_ID+".config.test";
private static final KeybindSettings OVERLAY_TOGGLE = KeybindSettings.create(KeybindSettings.Context.ANY, KeyAction.PRESS, true, true, false, true);
public static class Test
{
public static final ConfigBoolean TEST_CONFIG_BOOLEAN = new ConfigBoolean("testBoolean", false, "Test Boolean").apply(TEST_KEY);
Expand All @@ -89,6 +92,9 @@ public static class Test
public static final ConfigStringList TEST_CONFIG_STRING_LIST = new ConfigStringList("testStringList", ImmutableList.of("testString1", "testString2"), "Test String List").apply(TEST_KEY);
public static final ConfigLockedList TEST_CONFIG_LOCKED_LIST = new ConfigLockedList("testLockedConfigList", ConfigTestLockedList.INSTANCE, "Test Locked List").apply(TEST_KEY);
public static final ConfigInteger TEST_BUNDLE_PREVIEW_WIDTH = new ConfigInteger("testBundlePreviewWidth", 9, 6, 9, "Test Bundle Preview Width").apply(TEST_KEY);
public static final ConfigBooleanHotkeyed TEST_INVENTORY_OVERLAY = new ConfigBooleanHotkeyed("testInventoryOverlay", false, "LEFT_ALT").apply(TEST_KEY);
public static final ConfigBooleanHotkeyed TEST_INVENTORY_OVERLAY_OG = new ConfigBooleanHotkeyed("testInventoryOverlayOG", false, "").apply(TEST_KEY);
public static final ConfigHotkey TEST_INVENTORY_OVERLAY_TOGGLE = new ConfigHotkey("testInventoryOverlayToggle", "BUTTON_3", OVERLAY_TOGGLE).apply(TEST_KEY);

public static final ImmutableList<IConfigBase> OPTIONS = ImmutableList.of(
TEST_CONFIG_BOOLEAN,
Expand All @@ -103,11 +109,17 @@ public static class Test
TEST_CONFIG_STRING,
TEST_CONFIG_STRING_LIST,
TEST_CONFIG_LOCKED_LIST,
TEST_BUNDLE_PREVIEW_WIDTH
TEST_BUNDLE_PREVIEW_WIDTH,
TEST_INVENTORY_OVERLAY,
TEST_INVENTORY_OVERLAY_OG,
TEST_INVENTORY_OVERLAY_TOGGLE
);

public static final List<IHotkey> HOTKEY_LIST = ImmutableList.of(
TEST_CONFIG_BOOLEAN_HOTKEYED
TEST_CONFIG_BOOLEAN_HOTKEYED,
TEST_INVENTORY_OVERLAY,
TEST_INVENTORY_OVERLAY_OG,
TEST_INVENTORY_OVERLAY_TOGGLE
);
}

Expand Down Expand Up @@ -136,7 +148,7 @@ public static void loadFromFile()
if (MaLiLibReference.DEBUG_MODE)
{
ConfigUtils.readConfigBase(root, "Test", Test.OPTIONS);
ConfigUtils.readHotkeyToggleOptions(root, "TestEnumHotkeys", "TestEnumToggles", TestEnumConfig.VALUES);
ConfigUtils.readHotkeyToggleOptions(root, "TestEnumHotkeys", "TestEnumToggles", ConfigTestEnum.VALUES);
}

if (MaLiLibReference.EXPERIMENTAL_MODE)
Expand All @@ -161,7 +173,7 @@ public static void saveToFile()
if (MaLiLibReference.DEBUG_MODE)
{
ConfigUtils.writeConfigBase(root, "Test", Test.OPTIONS);
ConfigUtils.writeHotkeyToggleOptions(root, "TestEnumHotkeys", "TestEnumToggles", TestEnumConfig.VALUES);
ConfigUtils.writeHotkeyToggleOptions(root, "TestEnumHotkeys", "TestEnumToggles", ConfigTestEnum.VALUES);
}

if (MaLiLibReference.EXPERIMENTAL_MODE)
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/fi/dy/masa/malilib/event/RenderEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;

import fi.dy.masa.malilib.interfaces.IRenderDispatcher;
import fi.dy.masa.malilib.interfaces.IRenderer;
import fi.dy.masa.malilib.util.InfoUtils;
Expand Down Expand Up @@ -93,6 +96,42 @@ public void onRenderGameOverlayPost(DrawContext drawContext, MinecraftClient mc,
mc.getProfiler().pop();
}

@ApiStatus.Internal
public void onRenderTooltipComponentInsertFirst(Item.TooltipContext context, ItemStack stack, List<Text> list)
{
if (this.tooltipLastRenderers.isEmpty() == false)
{
for (IRenderer renderer : this.tooltipLastRenderers)
{
renderer.onRenderTooltipComponentInsertFirst(context, stack, list);
}
}
}

@ApiStatus.Internal
public void onRenderTooltipComponentInsertMiddle(Item.TooltipContext context, ItemStack stack, List<Text> list)
{
if (this.tooltipLastRenderers.isEmpty() == false)
{
for (IRenderer renderer : this.tooltipLastRenderers)
{
renderer.onRenderTooltipComponentInsertMiddle(context, stack, list);
}
}
}

@ApiStatus.Internal
public void onRenderTooltipComponentInsertLast(Item.TooltipContext context, ItemStack stack, List<Text> list)
{
if (this.tooltipLastRenderers.isEmpty() == false)
{
for (IRenderer renderer : this.tooltipLastRenderers)
{
renderer.onRenderTooltipComponentInsertLast(context, stack, list);
}
}
}

@ApiStatus.Internal
public void onRenderTooltipLast(DrawContext drawContext, ItemStack stack, int x, int y)
{
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/fi/dy/masa/malilib/interfaces/IDataSyncer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fi.dy.masa.malilib.interfaces;

import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.inventory.Inventory;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public interface IDataSyncer
{
void reset(boolean isLogout);

void onGameInit();

void onWorldPre();

void onWorldJoin();

@Nullable
NbtCompound getFromBlockEntityCacheNbt(BlockPos pos);

@Nullable
BlockEntity getFromBlockEntityCache(BlockPos pos);

@Nullable
NbtCompound getFromEntityCacheNbt(int entityId);

@Nullable
Entity getFromEntityCache(int entityId);

Pair<BlockEntity, NbtCompound> requestBlockEntity(World world, BlockPos pos);

Pair<Entity, NbtCompound> requestEntity(int entityId);

Inventory getBlockInventory(World world, BlockPos pos, boolean useNbt);

Inventory getEntityInventory(World world, BlockPos pos, boolean useNbt);

BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullable Identifier type);

Entity handleEntityData(int entityId, NbtCompound nbt);

void handleBulkEntityData(int transactionId, NbtCompound nbt);

void handleVanillaQueryNbt(int transactionId, NbtCompound nbt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package fi.dy.masa.malilib.interfaces;

import javax.annotation.Nullable;

import org.apache.commons.lang3.tuple.Pair;

import net.minecraft.block.BlockState;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.ChestType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World;

import fi.dy.masa.malilib.render.InventoryOverlay;
import fi.dy.masa.malilib.render.InventoryOverlayScreen;

public interface IInventoryOverlayHandler
{
String getModId();

IDataSyncer getDataSyncer();

InventoryOverlay.Refresher getRefreshHandler();

boolean isEmpty();

@Nullable
InventoryOverlay.Context getRenderContextNullable();

@Nullable
InventoryOverlay.Context getRenderContext(DrawContext drawContext, Profiler profiler, MinecraftClient mc);

default void renderInventoryOverlay(InventoryOverlay.Context context, DrawContext drawContext, MinecraftClient mc, boolean shulkerBGColors)
{
var screen = new InventoryOverlayScreen(this.getModId(), context, shulkerBGColors);
screen.init(mc, 0, 0);
screen.render(drawContext, 0, 0, 0);
}

default void refreshInventoryOverlay(MinecraftClient mc, boolean shulkerBGColors)
{
//this.getTargetInventory(mc, newScreen);
this.getTargetInventory(mc);

if (!this.isEmpty())
{
mc.setScreen(new InventoryOverlayScreen(this.getModId(), this.getRenderContextNullable(), shulkerBGColors));
}
}

@Nullable
default Pair<BlockEntity, NbtCompound> requestBlockEntityAt(World world, BlockPos pos)
{
if (!(world instanceof ServerWorld))
{
Pair<BlockEntity, NbtCompound> pair = this.getDataSyncer().requestBlockEntity(world, pos);

BlockState state = world.getBlockState(pos);

if (state.getBlock() instanceof ChestBlock)
{
ChestType type = state.get(ChestBlock.CHEST_TYPE);

if (type != ChestType.SINGLE)
{
return this.getDataSyncer().requestBlockEntity(world, pos.offset(ChestBlock.getFacing(state)));
}
}

return pair;
}

return null;
}

@Nullable
InventoryOverlay.Context getTargetInventory(MinecraftClient mc);

@Nullable
InventoryOverlay.Context getTargetInventoryFromBlock(World world, BlockPos pos, @Nullable BlockEntity be, NbtCompound nbt);

@Nullable
InventoryOverlay.Context getTargetInventoryFromEntity(Entity entity, NbtCompound nbt);
}
23 changes: 23 additions & 0 deletions src/main/java/fi/dy/masa/malilib/interfaces/IRenderer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fi.dy.masa.malilib.interfaces;

import java.util.List;
import java.util.function.Supplier;
import org.joml.Matrix4f;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.profiler.Profiler;

public interface IRenderer
Expand All @@ -25,6 +28,26 @@ default void onRenderGameOverlayPost(DrawContext drawContext) {}
*/
default void onRenderWorldLast(Matrix4f posMatrix, Matrix4f projMatrix) {}

/**
* Called only after the tooltip text adds the Item Name.
* If you want to 'Modify' the item name/Title, this is where
* you should do it; or just insert text below it as normal.
*/
default void onRenderTooltipComponentInsertFirst(Item.TooltipContext context, ItemStack stack, List<Text> list) {}

/**
* Called before the regular tooltip text data components
* of an item, such as the Music Disc info, Trims, and Lore,
* but after the regular item 'additional' item tooltips.
*/
default void onRenderTooltipComponentInsertMiddle(Item.TooltipContext context, ItemStack stack, List<Text> list) {}

/**
* Called after the tooltip text components of an item has been added,
* and occurs before the item durability, id, and component count.
*/
default void onRenderTooltipComponentInsertLast(Item.TooltipContext context, ItemStack stack, List<Text> list) {}

/**
* Called after the tooltip text of an item has been rendered
*/
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/fi/dy/masa/malilib/mixin/MixinItemStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fi.dy.masa.malilib.mixin;

import java.util.List;

import javax.annotation.Nullable;

import com.llamalad7.mixinextras.sugar.Local;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.text.Text;
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 fi.dy.masa.malilib.event.RenderEventHandler;

@Mixin(ItemStack.class)
public abstract class MixinItemStack
{
// This Goes before the Item Additional Tooltips.
@Inject(method = "getTooltip",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/item/Item;appendTooltip(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/Item$TooltipContext;Ljava/util/List;Lnet/minecraft/item/tooltip/TooltipType;)V",
shift = At.Shift.BEFORE))
private void onGetTooltipComponentsFirst(Item.TooltipContext context, @Nullable PlayerEntity player, TooltipType type, CallbackInfoReturnable<List<Text>> cir,
@Local List<Text> list)
{
((RenderEventHandler) RenderEventHandler.getInstance()).onRenderTooltipComponentInsertFirst(context, (ItemStack) (Object) this, list);
}

// This Goes after the Item Additional Tooltips.
@Inject(method = "getTooltip",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;appendTooltip(Lnet/minecraft/component/ComponentType;Lnet/minecraft/item/Item$TooltipContext;Ljava/util/function/Consumer;Lnet/minecraft/item/tooltip/TooltipType;)V",
ordinal = 0,
shift = At.Shift.BEFORE))
private void onGetTooltipComponentsMiddle(Item.TooltipContext context, @Nullable PlayerEntity player, TooltipType type, CallbackInfoReturnable<List<Text>> cir,
@Local List<Text> list)
{
((RenderEventHandler) RenderEventHandler.getInstance()).onRenderTooltipComponentInsertMiddle(context, (ItemStack) (Object) this, list);
}

// This Goes before the Item durability, item id, and component count.
@Inject(method = "getTooltip",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;appendTooltip(Lnet/minecraft/component/ComponentType;Lnet/minecraft/item/Item$TooltipContext;Ljava/util/function/Consumer;Lnet/minecraft/item/tooltip/TooltipType;)V",
ordinal = 6,
shift = At.Shift.AFTER))
private void onGetTooltipComponentsLast(Item.TooltipContext context, @Nullable PlayerEntity player, TooltipType type, CallbackInfoReturnable<List<Text>> cir,
@Local List<Text> list)
{
((RenderEventHandler) RenderEventHandler.getInstance()).onRenderTooltipComponentInsertLast(context, (ItemStack) (Object) this, list);
}
}
Loading

0 comments on commit 9d6530c

Please sign in to comment.