mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 11:59:25 +00:00
Added ItemDecorator API (#8794)
This commit is contained in:
1 parent
f0ecf16c6b
commit
7a6843fa4a
7 files changed
+240
-1
No files matched your search
Executable → Regular
File mode changed.
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.client;
|
||||
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.renderer.entity.ItemRenderer;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import net.minecraftforge.client.event.RegisterItemDecorationsEvent;
|
||||
|
||||
/**
|
||||
* An ItemDecorator that is used to render something on specific items, when the DurabilityBar and StackCount is rendered.
|
||||
* Add it to an item using {@linkplain RegisterItemDecorationsEvent#register(ItemLike, IItemDecorator)}.
|
||||
*/
|
||||
public interface IItemDecorator
|
||||
{
|
||||
|
||||
/**
|
||||
* Is called after {@linkplain ItemRenderer#renderGuiItemDecorations(Font, ItemStack, int, int, String)} is done rendering.
|
||||
* The StackCount is rendered at blitOffset+200 so use the blitOffset with caution.
|
||||
* <p>
|
||||
* The RenderState during this call will be: enableTexture, enableDepthTest, enableBlend and defaultBlendFunc
|
||||
* @return true if you have modified the RenderState and it has to be reset for other ItemDecorators
|
||||
*/
|
||||
boolean render(Font font, ItemStack stack, int xOffset, int yOffset, float blitOffset);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.client;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraftforge.client.event.RegisterItemDecorationsEvent;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.fml.ModLoadingContext;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public final class ItemDecoratorHandler
|
||||
{
|
||||
private final List<IItemDecorator> itemDecorators;
|
||||
|
||||
private static Map<Item, ItemDecoratorHandler> DECORATOR_LOOKUP = ImmutableMap.of();
|
||||
|
||||
private static final ItemDecoratorHandler EMPTY = new ItemDecoratorHandler();
|
||||
private ItemDecoratorHandler()
|
||||
{
|
||||
this.itemDecorators = ImmutableList.of();
|
||||
}
|
||||
private ItemDecoratorHandler(List<IItemDecorator> itemDecorators)
|
||||
{
|
||||
this.itemDecorators = ImmutableList.copyOf(itemDecorators);
|
||||
}
|
||||
|
||||
public static void init()
|
||||
{
|
||||
var decorators = new HashMap<Item, List<IItemDecorator>>();
|
||||
var event = new RegisterItemDecorationsEvent(decorators);
|
||||
ModLoader.get().postEventWithWrapInModOrder(event, (mc, e) -> ModLoadingContext.get().setActiveContainer(mc), (mc, e) -> ModLoadingContext.get().setActiveContainer(null));
|
||||
var builder = new ImmutableMap.Builder<Item, ItemDecoratorHandler>();
|
||||
decorators.forEach((item, itemDecorators) -> builder.put(item, new ItemDecoratorHandler(itemDecorators)));
|
||||
DECORATOR_LOOKUP = builder.build();
|
||||
}
|
||||
|
||||
public static ItemDecoratorHandler of(ItemStack stack)
|
||||
{
|
||||
return DECORATOR_LOOKUP.getOrDefault(stack.getItem(), EMPTY);
|
||||
}
|
||||
|
||||
public void render(Font font, ItemStack stack, int xOffset, int yOffset, float blitOffset)
|
||||
{
|
||||
resetRenderState();
|
||||
for (IItemDecorator itemDecorator : itemDecorators)
|
||||
{
|
||||
if (itemDecorator.render(font, stack, xOffset, yOffset, blitOffset))
|
||||
resetRenderState();
|
||||
}
|
||||
}
|
||||
|
||||
private void resetRenderState()
|
||||
{
|
||||
RenderSystem.enableTexture();
|
||||
RenderSystem.enableDepthTest();
|
||||
RenderSystem.enableBlend();
|
||||
RenderSystem.defaultBlendFunc();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
package net.minecraftforge.client.event;
|
||||
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import net.minecraftforge.client.IItemDecorator;
|
||||
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.fml.event.IModBusEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Allows users to register custom {@linkplain IItemDecorator IItemDecorator} to Items.
|
||||
*
|
||||
* <p>This event is not {@linkplain Cancelable cancelable}, and does not {@linkplain HasResult have a result}.
|
||||
*
|
||||
* <p>This event is fired on the {@linkplain FMLJavaModLoadingContext#getModEventBus() mod-specific event bus},
|
||||
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
|
||||
*/
|
||||
public class RegisterItemDecorationsEvent extends Event implements IModBusEvent
|
||||
{
|
||||
|
||||
private final Map<Item, List<IItemDecorator>> decorators;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public RegisterItemDecorationsEvent(Map<Item, List<IItemDecorator>> decorators)
|
||||
{
|
||||
this.decorators = decorators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an ItemDecorator to an Item
|
||||
*/
|
||||
public void register(ItemLike itemLike, IItemDecorator decorator)
|
||||
{
|
||||
List<IItemDecorator> itemDecoratorList = decorators.computeIfAbsent(itemLike.asItem(), item -> new ArrayList<>());
|
||||
itemDecoratorList.add(decorator);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user