diff --git a/run/config/amazingtrophies/trophies/test/lightning.json b/run/config/amazingtrophies/trophies/test/lightning.json index 03f92e0..69e9023 100644 --- a/run/config/amazingtrophies/trophies/test/lightning.json +++ b/run/config/amazingtrophies/trophies/test/lightning.json @@ -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 } } diff --git a/run/config/amazingtrophies/trophies/test/xp.json b/run/config/amazingtrophies/trophies/test/xp.json index eca8c7c..875125b 100644 --- a/run/config/amazingtrophies/trophies/test/xp.json +++ b/run/config/amazingtrophies/trophies/test/xp.json @@ -6,8 +6,7 @@ "amount": 5.0 }, "model": { - "type": "basic", - "model": "model.obj", - "texture": "texture.png" + "type": "item", + "item": "minecraft:diamond_pickaxe" } } diff --git a/src/main/java/glowredman/amazingtrophies/ClientHandler.java b/src/main/java/glowredman/amazingtrophies/ClientHandler.java index 66ffa39..0433a42 100644 --- a/src/main/java/glowredman/amazingtrophies/ClientHandler.java +++ b/src/main/java/glowredman/amazingtrophies/ClientHandler.java @@ -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; @@ -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 } diff --git a/src/main/java/glowredman/amazingtrophies/condition/ItemConditionHandler.java b/src/main/java/glowredman/amazingtrophies/condition/ItemConditionHandler.java index 615b89a..1febe6f 100644 --- a/src/main/java/glowredman/amazingtrophies/condition/ItemConditionHandler.java +++ b/src/main/java/glowredman/amazingtrophies/condition/ItemConditionHandler.java @@ -42,7 +42,7 @@ 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 + ")!"); @@ -50,8 +50,7 @@ public void parse(String id, JsonObject json) { 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> map = this.getMap(meta, nbt); Set ids = map.get(stack); diff --git a/src/main/java/glowredman/amazingtrophies/model/ItemTrophyModelHandler.java b/src/main/java/glowredman/amazingtrophies/model/ItemTrophyModelHandler.java new file mode 100644 index 0000000..5174426 --- /dev/null +++ b/src/main/java/glowredman/amazingtrophies/model/ItemTrophyModelHandler.java @@ -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; + } + + } + +}