[Feature] Item Renderer

This commit is contained in:
Frank 2022-08-16 22:29:32 +02:00
parent 8ef0d82ed6
commit 02284d9f94
4 changed files with 66 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@ -275,5 +276,12 @@ public abstract class AbstractStack<R extends ComponentRenderer, T extends Abstr
add(c);
return c;
}
protected Item addItem(ItemStack stack) {
Item i = new Item(Value.fit(), Value.fit());
i.setItem(stack);
add(i);
return i;
}
}

View file

@ -6,6 +6,7 @@ import org.betterx.ui.vanilla.VanillaScrollerRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@ -196,4 +197,9 @@ public class HorizontalStack extends AbstractHorizontalStack<HorizontalStack> {
) {
return super.addScrollable(width, height, content);
}
@Override
public Item addItem(ItemStack stack) {
return super.addItem(stack);
}
}

View file

@ -0,0 +1,46 @@
package org.betterx.ui.layout.components;
import org.betterx.ui.layout.values.Rectangle;
import org.betterx.ui.layout.values.Value;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.world.item.ItemStack;
public class Item extends CustomRenderComponent {
private ItemStack itemStack;
public Item(Value width, Value height) {
super(width, height);
}
public Item setItem(ItemStack item) {
this.itemStack = item;
return this;
}
@Override
protected void customRender(PoseStack stack, int x, int y, float deltaTicks, Rectangle bounds, Rectangle clipRect) {
final ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
itemRenderer.renderAndDecorateItem(Minecraft.getInstance().player, itemStack, bounds.left, bounds.top, 0);
itemRenderer.renderGuiItemDecorations(
Minecraft.getInstance().font,
itemStack,
bounds.left,
bounds.top,
"" + itemStack.getCount()
);
}
@Override
public int getContentWidth() {
return 16;
}
@Override
public int getContentHeight() {
return 16;
}
}

View file

@ -7,6 +7,7 @@ import org.betterx.ui.vanilla.VanillaScrollerRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@ -208,4 +209,9 @@ public class VerticalStack extends AbstractVerticalStack<VerticalStack> {
) {
return super.addScrollable(width, height, content);
}
@Override
public Item addItem(ItemStack stack) {
return super.addItem(stack);
}
}