Skip to content

Commit

Permalink
chore: refactor tooltip style providers (screen & item)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Jan 19, 2024
1 parent fb0e810 commit 143f18d
Show file tree
Hide file tree
Showing 32 changed files with 73 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.*;
import com.github.elenterius.biomancy.init.client.ClientSetupHandler;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.item.MaykerBannerPatternItem;
import com.github.elenterius.biomancy.item.SerumItem;
import com.github.elenterius.biomancy.item.livingtool.LivingToolState;
Expand Down Expand Up @@ -103,7 +103,7 @@ private <T extends Item> void addItem(Supplier<T> supplier, String name, String
T item = supplier.get();
add(item.getDescriptionId(), name);

if (item instanceof CustomTooltipProvider provider) {
if (item instanceof ItemTooltipStyleProvider provider) {
add(provider.getTooltipKey(new ItemStack(item)), tooltip);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.component.CustomEditBox;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.client.util.ClientSoundUtil;
import com.github.elenterius.biomancy.client.util.GuiRenderUtil;
import com.github.elenterius.biomancy.client.util.GuiUtil;
Expand All @@ -28,7 +29,7 @@
import java.util.Locale;
import java.util.Objects;

public class BioForgeScreen extends AbstractContainerScreen<BioForgeMenu> {
public class BioForgeScreen extends AbstractContainerScreen<BioForgeMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_bio_forge.png");
private BioForgeScreenController recipeBook;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.elenterius.biomancy.client.gui;

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.client.util.GuiRenderUtil;
import com.github.elenterius.biomancy.client.util.GuiUtil;
import com.github.elenterius.biomancy.menu.BioLabMenu;
Expand All @@ -10,7 +11,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class BioLabScreen extends AbstractContainerScreen<BioLabMenu> {
public class BioLabScreen extends AbstractContainerScreen<BioLabMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_bio_lab.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.elenterius.biomancy.client.gui;

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.client.util.GuiRenderUtil;
import com.github.elenterius.biomancy.client.util.GuiUtil;
import com.github.elenterius.biomancy.menu.DecomposerMenu;
Expand All @@ -10,7 +11,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class DecomposerScreen extends AbstractContainerScreen<DecomposerMenu> {
public class DecomposerScreen extends AbstractContainerScreen<DecomposerMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_decomposer.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.elenterius.biomancy.client.gui;

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.client.util.GuiRenderUtil;
import com.github.elenterius.biomancy.client.util.GuiUtil;
import com.github.elenterius.biomancy.menu.DigesterMenu;
Expand All @@ -10,7 +11,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class DigesterScreen extends AbstractContainerScreen<DigesterMenu> {
public class DigesterScreen extends AbstractContainerScreen<DigesterMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_digester.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.elenterius.biomancy.client.gui;

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.menu.FleshkinChestMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class FleshkinChestScreen extends AbstractContainerScreen<FleshkinChestMenu> {
public class FleshkinChestScreen extends AbstractContainerScreen<FleshkinChestMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_fleshkin_chest.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.elenterius.biomancy.client.gui;

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.client.gui.tooltip.ScreenTooltipStyleProvider;
import com.github.elenterius.biomancy.menu.StorageSacMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class StorageSacScreen extends AbstractContainerScreen<StorageSacMenu> {
public class StorageSacScreen extends AbstractContainerScreen<StorageSacMenu> implements ScreenTooltipStyleProvider {

private static final ResourceLocation BACKGROUND_TEXTURE = BiomancyMod.createRL("textures/gui/menu_storage_sac.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.elenterius.biomancy.client.gui.tooltip;

import com.github.elenterius.biomancy.styles.ColorStyles;

public interface ScreenTooltipStyleProvider {

default ColorStyles.ITooltipStyle getTooltipStyle() {
return ColorStyles.GENERIC_TOOLTIP;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.init.ModRarities;
import com.github.elenterius.biomancy.init.client.ModScreens;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.styles.ColorStyles;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.tooltip.EmptyLineTooltipComponent;
import com.github.elenterius.biomancy.tooltip.TooltipContents;
import com.mojang.datafixers.util.Either;
Expand Down Expand Up @@ -38,17 +36,17 @@ private TooltipRenderHandler() {}
public static void onRenderTooltipColor(final RenderTooltipEvent.Color tooltipEvent) {
ItemStack stack = tooltipEvent.getItemStack();

if (stack.isEmpty() && ModScreens.isBiomancyScreen(Minecraft.getInstance().screen)) {
ColorStyles.GENERIC_TOOLTIP.applyColorTo(tooltipEvent);
if (stack.isEmpty() && Minecraft.getInstance().screen instanceof ScreenTooltipStyleProvider screenStyleProvider) {
screenStyleProvider.getTooltipStyle().applyColorTo(tooltipEvent);
}
else if (stack.getItem() instanceof CustomTooltipProvider iTooltip) {
iTooltip.getTooltipStyle().applyColorTo(tooltipEvent);
else if (stack.getItem() instanceof ItemTooltipStyleProvider itemStyleProvider) {
itemStyleProvider.getTooltipStyle().applyColorTo(tooltipEvent);
}
}

@SubscribeEvent
public static void onGatherTooltipComponents(final RenderTooltipEvent.GatherComponents event) {
final boolean isTooltip = event.getItemStack().getItem() instanceof CustomTooltipProvider;
final boolean isTooltip = event.getItemStack().getItem() instanceof ItemTooltipStyleProvider;

List<Either<FormattedText, TooltipComponent>> tooltipElements = event.getTooltipElements();
for (int i = 0; i < tooltipElements.size(); i++) {
Expand All @@ -72,7 +70,7 @@ else if (isTooltip && component == CommonComponents.EMPTY) { //vanilla bugfix: f

public static void onPostRenderTooltip(ItemStack stack, List<ClientTooltipComponent> components, Font font, GuiGraphics guiGraphics, int posX, int posY, int tooltipWidth, int tooltipHeight) {
if (!components.isEmpty()) {
int color = stack.getItem() instanceof CustomTooltipProvider provider ? provider.getTooltipColorWithAlpha(stack) : ModRarities.getARGBColor(stack);
int color = stack.getItem() instanceof ItemTooltipStyleProvider provider ? provider.getTooltipColorWithAlpha(stack) : ModRarities.getARGBColor(stack);

int y = posY;
for (int i = 0; i < components.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.elenterius.biomancy.BiomancyMod;
import com.github.elenterius.biomancy.init.client.ClientSetupHandler;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.styles.TextComponentUtil;
import com.github.elenterius.biomancy.styles.TextStyles;
import com.github.elenterius.biomancy.util.ComponentUtil;
Expand Down Expand Up @@ -43,7 +43,7 @@ public static void appendItemInfoTooltip(Item item, List<Component> tooltip) {
private static MutableComponent getItemTooltip(ItemStack stack) {
Item item = stack.getItem();

if (item instanceof CustomTooltipProvider iTooltip) {
if (item instanceof ItemTooltipStyleProvider iTooltip) {
return iTooltip.getTooltipText(stack);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,23 @@
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.registries.RegistryObject;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;


public final class ModScreens {

private static final Set<Class<? extends Screen>> SCREENS = new HashSet<>();

private ModScreens() {}

public static boolean isBiomancyScreen(@Nullable Screen screen) {
return screen != null && SCREENS.stream().anyMatch(screenClass -> screenClass.isInstance(screen));
static void registerMenuScreens() {
registerMenuScreen(ModMenuTypes.DECOMPOSER, DecomposerScreen::new);
registerMenuScreen(ModMenuTypes.BIO_LAB, BioLabScreen::new);
registerMenuScreen(ModMenuTypes.STORAGE_SAC, StorageSacScreen::new);
registerMenuScreen(ModMenuTypes.FLESHKIN_CHEST, FleshkinChestScreen::new);
registerMenuScreen(ModMenuTypes.DIGESTER, DigesterScreen::new);
registerMenuScreen(ModMenuTypes.BIO_FORGE, BioForgeScreen::new);
}

private static <M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void registerMenuScreen(RegistryObject<MenuType<M>> registryObject, MenuScreens.ScreenConstructor<M, U> factory, Class<U> clazz) {
private static <M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void registerMenuScreen(RegistryObject<MenuType<M>> registryObject, MenuScreens.ScreenConstructor<M, U> factory) {
MenuScreens.register(registryObject.get(), factory);
SCREENS.add(clazz);
}

static void registerMenuScreens() {
registerMenuScreen(ModMenuTypes.DECOMPOSER, DecomposerScreen::new, DecomposerScreen.class);
registerMenuScreen(ModMenuTypes.BIO_LAB, BioLabScreen::new, BioLabScreen.class);
registerMenuScreen(ModMenuTypes.STORAGE_SAC, StorageSacScreen::new, StorageSacScreen.class);
registerMenuScreen(ModMenuTypes.FLESHKIN_CHEST, FleshkinChestScreen::new, FleshkinChestScreen.class);
registerMenuScreen(ModMenuTypes.DIGESTER, DigesterScreen::new, DigesterScreen.class);
registerMenuScreen(ModMenuTypes.BIO_FORGE, BioForgeScreen::new, BioForgeScreen.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;
import java.util.function.Consumer;

public class BEWLBlockItem extends BlockItem implements CustomTooltipProvider {
public class BEWLBlockItem extends BlockItem implements ItemTooltipStyleProvider {

private final Lazy<BlockEntity> cachedBlockEntityWithoutLevel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.List;
import java.util.Optional;

public class BioExtractorItem extends Item implements KeyPressListener, CustomTooltipProvider {
public class BioExtractorItem extends Item implements KeyPressListener, ItemTooltipStyleProvider {

public BioExtractorItem(Properties properties) {
super(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Optional;
import java.util.UUID;

public class EssenceItem extends Item implements CustomTooltipProvider {
public class EssenceItem extends Item implements ItemTooltipStyleProvider {

public static final String NBT_KEY_ENTITY_TYPE = "EntityType";
public static final String KEY_DATA = "EssenceData";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;
import java.util.Optional;

public class FertilizerItem extends Item implements CustomTooltipProvider {
public class FertilizerItem extends Item implements ItemTooltipStyleProvider {

public FertilizerItem(Properties properties) {
super(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.ItemStack;

public interface CustomTooltipProvider {
public interface ItemTooltipStyleProvider {

default String getTooltipKey(ItemStack stack) {
return TextComponentUtil.getItemTooltipKey(stack.getItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;
import java.util.Objects;

public class MobLootItem extends Item implements CustomTooltipProvider {
public class MobLootItem extends Item implements ItemTooltipStyleProvider {

private final ITag<EntityType<?>> taggedEntities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.util.List;

public class ObfuscatedTooltipBlockItem extends BlockItem implements CustomTooltipProvider {
public class ObfuscatedTooltipBlockItem extends BlockItem implements ItemTooltipStyleProvider {

public ObfuscatedTooltipBlockItem(Block block, Properties properties) {
super(block, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;
import java.util.function.Supplier;

public class SerumItem extends Item implements SerumContainer, CustomTooltipProvider {
public class SerumItem extends Item implements SerumContainer, ItemTooltipStyleProvider {

private final Supplier<? extends Serum> serumSupplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.util.List;

public class SimpleBlockItem extends BlockItem implements CustomTooltipProvider {
public class SimpleBlockItem extends BlockItem implements ItemTooltipStyleProvider {

public SimpleBlockItem(Block block, Properties properties) {
super(block, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.annotation.Nullable;
import java.util.List;

public class SimpleItem extends Item implements CustomTooltipProvider {
public class SimpleItem extends Item implements ItemTooltipStyleProvider {

public SimpleItem(Properties properties) {
super(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.List;
import java.util.Optional;

public class StorageSacBlockItem extends BlockItem implements CustomTooltipProvider {
public class StorageSacBlockItem extends BlockItem implements ItemTooltipStyleProvider {

public StorageSacBlockItem(Block block, Properties properties) {
super(block, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.inventory.InjectorItemInventory;
import com.github.elenterius.biomancy.inventory.itemhandler.LargeSingleItemStackHandler;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.item.KeyPressListener;
import com.github.elenterius.biomancy.styles.TextComponentUtil;
import com.github.elenterius.biomancy.util.CombatUtil;
Expand Down Expand Up @@ -72,7 +72,7 @@
import java.util.Optional;
import java.util.function.Consumer;

public class InjectorItem extends Item implements SerumInjector, CustomTooltipProvider, KeyPressListener, GeoItem {
public class InjectorItem extends Item implements SerumInjector, ItemTooltipStyleProvider, KeyPressListener, GeoItem {

public static final short MAX_SLOT_SIZE = 16;
public static final String INVENTORY_TAG = "inventory";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.entity.MobUtil;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.item.KeyPressListener;
import com.github.elenterius.biomancy.styles.ColorStyles;
import com.github.elenterius.biomancy.styles.TextComponentUtil;
Expand Down Expand Up @@ -32,7 +32,7 @@

import java.util.List;

public class LivingSwordItem extends SwordItem implements CustomTooltipProvider, LivingTool, KeyPressListener {
public class LivingSwordItem extends SwordItem implements ItemTooltipStyleProvider, LivingTool, KeyPressListener {

private final int maxNutrients;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.github.elenterius.biomancy.client.render.item.dev.BileSpitterRenderer;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModProjectiles;
import com.github.elenterius.biomancy.item.CustomTooltipProvider;
import com.github.elenterius.biomancy.item.IArmPoseProvider;
import com.github.elenterius.biomancy.item.ItemTooltipStyleProvider;
import com.github.elenterius.biomancy.tooltip.HrTooltipComponent;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
Expand Down Expand Up @@ -39,7 +39,7 @@
import java.util.function.Predicate;

@Deprecated(forRemoval = true)
public class BileSpitterItem extends ProjectileWeaponItem implements CustomTooltipProvider, GeoItem, IArmPoseProvider {
public class BileSpitterItem extends ProjectileWeaponItem implements ItemTooltipStyleProvider, GeoItem, IArmPoseProvider {

public final float drawTime = 50f;
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
Expand Down
Loading

0 comments on commit 143f18d

Please sign in to comment.