Skip to content

Commit

Permalink
[#515] Item renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
SirEndii committed Aug 8, 2024
1 parent c81e843 commit 614b149
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.srendi.advancedperipherals.client.smartglasses;

import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Rectangle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RectangleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Text;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.TextObject;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -35,14 +36,14 @@ public static Collection<RenderableObject> getObjects() {
return objects.values();
}


public static void clear() {
objects.clear();
}

public static void registerDecodeObjects() {
ObjectDecodeRegistry.register(Rectangle.TYPE_ID, Rectangle::decode);
ObjectDecodeRegistry.register(Circle.TYPE_ID, Circle::decode);
ObjectDecodeRegistry.register(Text.TYPE_ID, Text::decode);
ObjectDecodeRegistry.register(RectangleObject.TYPE_ID, RectangleObject::decode);
ObjectDecodeRegistry.register(CircleObject.TYPE_ID, CircleObject::decode);
ObjectDecodeRegistry.register(TextObject.TYPE_ID, TextObject::decode);
ObjectDecodeRegistry.register(ItemObject.TYPE_ID, ItemObject::decode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;
Expand All @@ -21,7 +21,7 @@ public class CircleRenderer implements IObjectRenderer {
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
for (RenderableObject object : objects) {

Circle circle = (Circle) object;
CircleObject circle = (CircleObject) object;

float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.srendi.advancedperipherals.client.smartglasses.objects;

import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.List;

public class ItemRenderer implements IObjectRenderer {

@Override
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
Minecraft minecraft = Minecraft.getInstance();

for (RenderableObject object : objects) {
Item renderItem = ItemUtil.getRegistryEntry(((ItemObject) object).item, ForgeRegistries.ITEMS);
if (renderItem == null)
continue;
minecraft.getItemRenderer().renderGuiItem(new ItemStack(renderItem), object.x, object.y);
}

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

import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Text;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.TextObject;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.gui.overlay.ForgeGui;

Expand All @@ -15,7 +15,7 @@ public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack

Minecraft minecraft = Minecraft.getInstance();
for (RenderableObject object : objects) {
Text text = (Text) object;
TextObject text = (TextObject) object;
poseStack.scale(text.fontSize, text.fontSize, 1);
if (text.shadow) {
minecraft.font.drawShadow(poseStack, text.content, text.x / text.fontSize, text.y / text.fontSize, text.color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import dan200.computercraft.api.lua.MethodResult;
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Rectangle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RectangleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Text;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.TextObject;
import net.minecraft.client.Minecraft;

public class OverlayGlassesFunctions implements IModuleFunctions {
Expand All @@ -24,28 +25,36 @@ public OverlayGlassesFunctions(OverlayModule overlayModule) {

@LuaFunction
public final MethodResult createRectangle(IArguments arguments) throws LuaException {
Rectangle rectangle = new Rectangle(overlayModule, arguments);
RectangleObject rectangle = new RectangleObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(rectangle);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createCircle(IArguments arguments) throws LuaException {
Circle circle = new Circle(overlayModule, arguments);
CircleObject circle = new CircleObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(circle);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createText(IArguments arguments) throws LuaException {
Text circle = new Text(overlayModule, arguments);
TextObject circle = new TextObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(circle);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createItem(IArguments arguments) throws LuaException {
ItemObject item = new ItemObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(item);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult getObject(IArguments arguments) throws LuaException {
int id = arguments.getInt(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@

import java.util.UUID;

public class Circle extends RenderableObject {
public class CircleObject extends RenderableObject {
public static final int TYPE_ID = 1;

private final IObjectRenderer renderer = new CircleRenderer();

@FixedPointNumberProperty(min = -32767, max = 32767)
public int radius = 0;

public Circle(OverlayModule module, IArguments arguments) throws LuaException {
public CircleObject(OverlayModule module, IArguments arguments) throws LuaException {
super(module, arguments);
reflectivelyMapProperties(arguments);
}

public Circle(UUID player) {
public CircleObject(UUID player) {
super(player);
}

Expand All @@ -47,7 +47,7 @@ public void encode(FriendlyByteBuf buffer) {
buffer.writeInt(radius);
}

public static Circle decode(FriendlyByteBuf buffer) {
public static CircleObject decode(FriendlyByteBuf buffer) {
int objectId = buffer.readInt();
boolean hasValidUUID = buffer.readBoolean();
if (!hasValidUUID) {
Expand All @@ -64,7 +64,7 @@ public static Circle decode(FriendlyByteBuf buffer) {
int maxY = buffer.readInt();
int radius = buffer.readInt();

Circle clientObject = new Circle(player);
CircleObject clientObject = new CircleObject(player);
clientObject.setId(objectId);
clientObject.color = color;
clientObject.opacity = opacity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.smartglasses.objects.IObjectRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.ItemRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.OverlayModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes.StringProperty;
import net.minecraft.network.FriendlyByteBuf;

import java.util.UUID;

public class ItemObject extends RenderableObject {
public static final int TYPE_ID = 3;

private final IObjectRenderer renderer = new ItemRenderer();

@StringProperty
public String item = "minecraft:air";

public ItemObject(OverlayModule module, IArguments arguments) throws LuaException {
super(module, arguments);
reflectivelyMapProperties(arguments);
}

public ItemObject(UUID player) {
super(player);
}

public void setItem(String item) {
this.item = item;
}

public String getItem() {
return item;
}

@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeInt(TYPE_ID);
super.encode(buffer);
buffer.writeUtf(item);
}

public static ItemObject decode(FriendlyByteBuf buffer) {
int objectId = buffer.readInt();
boolean hasValidUUID = buffer.readBoolean();
if (!hasValidUUID) {
AdvancedPeripherals.exception("Tried to decode a buffer for an OverlayObject but without a valid player as target.", new IllegalArgumentException());
return null;
}
UUID player = buffer.readUUID();
int color = buffer.readInt();
float opacity = buffer.readFloat();

int x = buffer.readInt();
int y = buffer.readInt();
int maxX = buffer.readInt();
int maxY = buffer.readInt();
String item = buffer.readUtf();

ItemObject clientObject = new ItemObject(player);
clientObject.setId(objectId);
clientObject.color = color;
clientObject.opacity = opacity;
clientObject.x = x;
clientObject.y = y;
clientObject.maxX = maxX;
clientObject.maxY = maxY;
clientObject.item = item;

return clientObject;
}

@Override
public IObjectRenderer getRenderObject() {
return renderer;
}

@Override
public String toString() {
return "ItemObject{" +
"item='" + item + '\'' +
", opacity=" + opacity +
", color=" + color +
", x=" + x +
", y=" + y +
", maxX=" + maxX +
", maxY=" + maxY +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
/**
* Just a rectangle
*/
public class Rectangle extends RenderableObject {
public class RectangleObject extends RenderableObject {
public static final int TYPE_ID = 0;

private final IObjectRenderer renderer = new RectangleRenderer();

public Rectangle(OverlayModule module, IArguments arguments) throws LuaException {
public RectangleObject(OverlayModule module, IArguments arguments) throws LuaException {
super(module, arguments);
}

Expand All @@ -27,7 +27,7 @@ public Rectangle(OverlayModule module, IArguments arguments) throws LuaException
*
* @param player the target player
*/
public Rectangle(UUID player) {
public RectangleObject(UUID player) {
super(player);
}

Expand All @@ -37,7 +37,7 @@ public void encode(FriendlyByteBuf buffer) {
super.encode(buffer);
}

public static Rectangle decode(FriendlyByteBuf buffer) {
public static RectangleObject decode(FriendlyByteBuf buffer) {
int objectId = buffer.readInt();
boolean hasValidUUID = buffer.readBoolean();
if (!hasValidUUID) {
Expand All @@ -53,7 +53,7 @@ public static Rectangle decode(FriendlyByteBuf buffer) {
int sizeX = buffer.readInt();
int sizeY = buffer.readInt();

Rectangle clientObject = new Rectangle(player);
RectangleObject clientObject = new RectangleObject(player);
clientObject.setId(objectId);
clientObject.color = color;
clientObject.opacity = opacity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.util.UUID;

public class Text extends RenderableObject {
public class TextObject extends RenderableObject {
public static final int TYPE_ID = 2;

private final IObjectRenderer renderer = new TextRenderer();
Expand All @@ -28,12 +28,12 @@ public class Text extends RenderableObject {
@BooleanProperty
public boolean shadow = false;

public Text(OverlayModule module, IArguments arguments) throws LuaException {
public TextObject(OverlayModule module, IArguments arguments) throws LuaException {
super(module, arguments);
reflectivelyMapProperties(arguments);
}

public Text(UUID player) {
public TextObject(UUID player) {
super(player);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public void encode(FriendlyByteBuf buffer) {
buffer.writeBoolean(shadow);
}

public static Text decode(FriendlyByteBuf buffer) {
public static TextObject decode(FriendlyByteBuf buffer) {
int objectId = buffer.readInt();
boolean hasValidUUID = buffer.readBoolean();
if (!hasValidUUID) {
Expand All @@ -103,7 +103,7 @@ public static Text decode(FriendlyByteBuf buffer) {
float fontSize = buffer.readFloat();
boolean shadow = buffer.readBoolean();

Text clientObject = new Text(player);
TextObject clientObject = new TextObject(player);
clientObject.setId(objectId);
clientObject.color = color;
clientObject.opacity = opacity;
Expand Down

0 comments on commit 614b149

Please sign in to comment.