Skip to content

Commit

Permalink
Add ItemTrophyModelHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
glowredman committed Nov 12, 2023
1 parent dbbaa26 commit 01f6a73
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 9 deletions.
7 changes: 4 additions & 3 deletions run/config/amazingtrophies/trophies/test/lightning.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"type": "lightning"
},
"model": {
"type": "basic",
"model": "model.obj",
"texture": "texture.png"
"type": "item",
"item": "minecraft:lit_furnace",
"meta": 2,
"yawOffset": -90.0
}
}
5 changes: 2 additions & 3 deletions run/config/amazingtrophies/trophies/test/xp.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"amount": 5.0
},
"model": {
"type": "basic",
"model": "model.obj",
"texture": "texture.png"
"type": "item",
"item": "minecraft:diamond_pickaxe"
}
}
2 changes: 2 additions & 0 deletions src/main/java/glowredman/amazingtrophies/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import glowredman.amazingtrophies.api.AmazingTrophiesAPI;
import glowredman.amazingtrophies.model.BasicTrophyModelHandler;
import glowredman.amazingtrophies.model.EntityTrophyModelHandler;
import glowredman.amazingtrophies.model.ItemTrophyModelHandler;
import glowredman.amazingtrophies.model.PedestalTrophyModelHandler;
import glowredman.amazingtrophies.trophy.RendererTrophy;
import glowredman.amazingtrophies.trophy.TileEntityTrophy;
Expand All @@ -37,6 +38,7 @@ static void registerTrophyModelHandlers() {
AmazingTrophiesAPI.registerTrophyModelHandlerProvider(PedestalTrophyModelHandler.ID, PedestalTrophyModelHandler::new);
AmazingTrophiesAPI.registerTrophyModelHandlerProvider(BasicTrophyModelHandler.ID, BasicTrophyModelHandler::new);
AmazingTrophiesAPI.registerTrophyModelHandlerProvider(EntityTrophyModelHandler.ID, EntityTrophyModelHandler::new);
AmazingTrophiesAPI.registerTrophyModelHandlerProvider(ItemTrophyModelHandler.ID, ItemTrophyModelHandler::new);
// spotless:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ public ItemConditionHandler() {

@Override
public void parse(String id, JsonObject json) {
String registryName = ConfigHandler.getStringProperty(json, PROPERTY_ITEM, id);
String registryName = ConfigHandler.getStringProperty(json, PROPERTY_ITEM);
int meta = ConfigHandler.getIntegerProperty(json, PROPERTY_META, OreDictionary.WILDCARD_VALUE);
if (meta < 0 || meta > OreDictionary.WILDCARD_VALUE) {
throw new IllegalArgumentException("Illegal meta value (" + meta + ")!");
}
String nbt = ConfigHandler.getStringProperty(json, PROPERTY_NBT, null);
ItemStack stack = GameRegistry.makeItemStack(registryName, meta, 0, nbt);
if (stack == null) {
throw new IllegalArgumentException(
"Could not find item " + registryName + " for condition of \"" + id + "\"!");
throw new IllegalArgumentException("Could not find item " + registryName + "!");
}
Map<ItemStack, Set<String>> map = this.getMap(meta, nbt);
Set<String> ids = map.get(stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package glowredman.amazingtrophies.model;

import javax.annotation.Nullable;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import org.lwjgl.opengl.GL11;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import cpw.mods.fml.common.registry.GameRegistry;
import glowredman.amazingtrophies.ConfigHandler;

public class ItemTrophyModelHandler extends PedestalTrophyModelHandler {

public static final String ID = "item";
public static final String PROPERTY_ITEM = "item";
public static final String PROPERTY_META = "meta";
public static final String PROPERTY_NBT = "nbt";
public static final String PROPERTY_Y_OFFSET = "yOffset";
public static final String PROPERTY_YAW_OFFSET = "yawOffset";
public static final String PROPERTY_SCALE = "scale";
private static final Render RENDER = new Render();

private EntityItem item;
private double yOffset = Double.NaN;
private float yawOffset = 0.0f;
private float scale = Float.NaN;

public ItemTrophyModelHandler() {}

public ItemTrophyModelHandler(ItemStack item) {
this.setItem(item);
this.calculateScaleAndYOffset(item.getItem());
}

public ItemTrophyModelHandler(ItemStack item, float yawOffset) {
this(item);
this.yawOffset = yawOffset;
}

@Override
public void parse(String id, JsonObject json) throws JsonSyntaxException {
String registryName = ConfigHandler.getStringProperty(json, PROPERTY_ITEM);
int meta = ConfigHandler.getIntegerProperty(json, PROPERTY_META, 0);
if (meta < 0 || meta > OreDictionary.WILDCARD_VALUE) {
throw new IllegalArgumentException("Illegal meta value (" + meta + ")!");
}
String nbt = ConfigHandler.getStringProperty(json, PROPERTY_NBT, null);
ItemStack stack = GameRegistry.makeItemStack(registryName, meta, 0, nbt);
if (stack == null) {
throw new IllegalArgumentException("Could not find item " + registryName + "!");
}
this.yOffset = ConfigHandler.getDoubleProperty(json, PROPERTY_Y_OFFSET, this.yOffset);
this.yawOffset = ConfigHandler.getFloatProperty(json, PROPERTY_YAW_OFFSET, this.yawOffset);
this.scale = ConfigHandler.getFloatProperty(json, PROPERTY_SCALE, this.scale);
this.setItem(stack);
this.calculateScaleAndYOffset(stack.getItem());
}

@Override
public void render(double x, double y, double z, int rotation, @Nullable String name, long time,
float partialTickTime) {
super.render(x, y, z, rotation, name, time, partialTickTime);

if (RENDER.getFontRendererFromRenderManager() == null) {
return;
}

GL11.glPushMatrix();
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glTranslated(x, y + this.yOffset, z);
GL11.glRotatef(22.5f * rotation + this.yawOffset, 0.0f, 1.0f, 0.0f);
GL11.glScalef(this.scale, this.scale, this.scale);
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
GL11.glEnable(GL11.GL_TEXTURE_2D);
OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);

synchronized (this.item) {
this.item.setWorld(Minecraft.getMinecraft().theWorld);
RENDER.doRender(this.item, 0.0, 0.0, 0.0, 0.0f, 0.0f);
}

GL11.glPopAttrib();
GL11.glPopMatrix();
}

private void setItem(ItemStack item) {
this.item = new EntityItem(null);
this.item.setEntityItemStack(item);
this.item.hoverStart = 0.0f;
}

private void calculateScaleAndYOffset(Item item) {
if (Float.isNaN(this.scale)) {
// RenderItem.doRender() scales blocks by 0.25 and items by 0.5
this.scale = item instanceof ItemBlock ? 1.375f : 0.6875f;
}
if (Double.isNaN(this.yOffset)) {
// RenderItem.doRender() translates blocks by -0.5 and items by -0.25 in the Y direction
// in both cases, an additional scaled translation by 0.125 is needed
this.yOffset = (double) this.scale * 0.125 - 0.1875;
}
}

private static class Render extends RenderItem {

public Render() {
this.setRenderManager(RenderManager.instance);
}

@Override
public boolean shouldBob() {
return false;
}

}

}

0 comments on commit 01f6a73

Please sign in to comment.