Skip to content

Commit

Permalink
Add an option to replace mob texture and change entity scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mchorse committed Sep 11, 2021
1 parent 9fd39b2 commit 4ffbdc2
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 7 deletions.
133 changes: 126 additions & 7 deletions src/main/java/mchorse/metamorph/api/morphs/EntityMorph.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mchorse.metamorph.api.morphs;

import mchorse.mclib.client.gui.utils.GuiUtils;
import mchorse.mclib.utils.ReflectionUtils;
import mchorse.mclib.utils.resources.RLUtils;
import mchorse.metamorph.Metamorph;
import mchorse.metamorph.api.EntityUtils;
import mchorse.metamorph.api.MorphSettings;
Expand All @@ -21,6 +23,8 @@
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderLivingBase;
import net.minecraft.client.renderer.entity.layers.LayerRenderer;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
Expand Down Expand Up @@ -139,6 +143,13 @@ public class EntityMorph extends AbstractMorph implements IBodyPartProvider
@SideOnly(Side.CLIENT)
public Map<String, ModelRenderer> limbs;

public ResourceLocation userTexture;

public float scale = 1F;

@SideOnly(Side.CLIENT)
private ITextureObject lastTexture;

@Override
public BodyPartManager getBodyPart()
{
Expand Down Expand Up @@ -179,11 +190,11 @@ else if (entity.height < 0.6)
scale *= 0.5 / entity.height;
}

if (this.name.equals("Ghast"))
if (this.name.equals("minecraft:ghast"))
{
scale = 5F;
}
else if (this.name.equals("Guardian") && entity.height > 1.8)
else if (this.name.equals("minecraft:guardian") && entity.height > 1.8)
{
scale *= 1 / entity.height;
}
Expand All @@ -196,7 +207,11 @@ else if (this.name.equals("Guardian") && entity.height > 1.8)
}

this.setupBodyPart();
this.replaceUserTexture();

GuiUtils.drawEntityOnScreen(x, y, scale, entity, alpha);

this.restoreMobTexture();
}

@Override
Expand All @@ -221,6 +236,7 @@ public boolean renderHand(EntityPlayer player, EnumHand hand)
return true;
}

this.replaceUserTexture();
Minecraft.getMinecraft().renderEngine.bindTexture(this.texture);
ModelBase model = this.renderer.getMainModel();

Expand Down Expand Up @@ -272,6 +288,8 @@ public boolean renderHand(EntityPlayer player, EnumHand hand)
this.leftHand.rotationPointZ = rpz;
}

this.restoreMobTexture();

return true;
}

Expand Down Expand Up @@ -326,22 +344,27 @@ public void render(EntityLivingBase entity, double x, double y, double z, float

renderEntity = entity;
this.setupBodyPart();
this.replaceUserTexture();

GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.scale(this.scale, this.scale, this.scale);

if (this.entity instanceof EntityDragon)
{
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.rotate(180, 0.0F, 1.0F, 0.0F);

render.doRender(this.entity, 0, 0, 0, entityYaw, partialTicks);

GlStateManager.popMatrix();
}
else
{
render.doRender(this.entity, x, y, z, entityYaw, partialTicks);
render.doRender(this.entity, 0, 0, 0, entityYaw, partialTicks);
}

GlStateManager.popMatrix();

this.restoreMobTexture();

if (model instanceof ModelBiped)
{
((ModelBiped) model).isSneak = wasSneak;
Expand Down Expand Up @@ -388,6 +411,75 @@ protected void renderBodyParts(EntityLivingBase target, float partialTicks)
GlStateManager.popMatrix();
}

/**
* This is pretty ugly, but it's the only way to replace entity's textures...
*/
@SideOnly(Side.CLIENT)
private void replaceUserTexture()
{
if (this.userTexture == null)
{
return;
}

if (this.texture == null)
{
this.setupTexture();
}

if (this.texture != null)
{
if (this.userTexture.equals(this.texture))
{
return;
}

TextureManager textureManager = Minecraft.getMinecraft().renderEngine;
Map<ResourceLocation, ITextureObject> map = ReflectionUtils.getTextures(textureManager);

if (map != null)
{
ITextureObject object = map.get(this.userTexture);

if (object == null)
{
textureManager.bindTexture(this.userTexture);
object = map.get(this.userTexture);
}

if (object != null)
{
this.lastTexture = map.get(this.texture);

if (this.lastTexture == null)
{
textureManager.bindTexture(this.texture);
this.lastTexture = map.get(this.texture);
}

if (this.lastTexture != null)
{
map.put(this.texture, object);
}
}
}
}
}

@SideOnly(Side.CLIENT)
private void restoreMobTexture()
{
if (this.lastTexture != null)
{
TextureManager textureManager = Minecraft.getMinecraft().renderEngine;
Map<ResourceLocation, ITextureObject> map = ReflectionUtils.getTextures(textureManager);

map.put(this.texture, this.lastTexture);

this.lastTexture = null;
}
}

/* Other stuff */

/**
Expand Down Expand Up @@ -738,6 +830,11 @@ protected void setupRenderer()
@SuppressWarnings({"unchecked", "rawtypes"})
protected void setupTexture()
{
if (this.texture != null)
{
return;
}

Class<RenderLivingBase> clazz = (Class<RenderLivingBase>) this.renderer.getClass();

for (Method method : clazz.getDeclaredMethods())
Expand Down Expand Up @@ -888,6 +985,8 @@ public boolean equals(Object obj)

result = result && theSame;
result = result && Objects.equals(morph.parts, this.parts);
result = result && morph.scale == this.scale;
result = result && Objects.equals(morph.userTexture, this.userTexture);
}

return result;
Expand All @@ -899,6 +998,8 @@ public void reset()
this.parts.reset();
this.resetEntity();
this.entityData = null;
this.scale = 1F;
this.userTexture = null;

if (this.customSettings)
{
Expand Down Expand Up @@ -938,6 +1039,8 @@ public void copy(AbstractMorph from)

this.entityData = morph.entityData == null ? null : morph.entityData.copy();
this.parts.copy(morph.parts);
this.scale = morph.scale;
this.userTexture = RLUtils.clone(morph.userTexture);
}
}

Expand Down Expand Up @@ -1051,6 +1154,12 @@ public void toNBT(NBTTagCompound tag)
super.toNBT(tag);

tag.setTag("EntityData", this.entityData);
tag.setFloat("Scale", this.scale);

if (this.userTexture != null)
{
tag.setTag("Texture", RLUtils.writeNbt(this.userTexture));
}

NBTTagList bodyParts = this.parts.toNBT();

Expand All @@ -1067,6 +1176,16 @@ public void fromNBT(NBTTagCompound tag)

this.entityData = tag.getCompoundTag("EntityData");

if (tag.hasKey("Scale"))
{
this.scale = tag.getFloat("Scale");
}

if (tag.hasKey("Texture"))
{
this.userTexture = RLUtils.create(tag.getTag("Texture"));
}

if (tag.hasKey("BodyParts", 9))
{
this.parts.fromNBT(tag.getTagList("BodyParts", 10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mchorse.metamorph.api.morphs.EntityMorph;
import mchorse.metamorph.bodypart.GuiBodyPartEditor;
import mchorse.metamorph.client.gui.editor.GuiAbstractMorph;
import mchorse.vanilla_pack.editors.panels.GuiEntityPanel;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -23,14 +24,19 @@ public class GuiEntityMorph extends GuiAbstractMorph<EntityMorph>
{
public static final List<String> animals = Arrays.asList("minecraft:pig", "minecraft:chicken", "minecraft:cow", "minecraft:mooshroom", "minecraft:polar_bear", "minecraft:sheep", "minecraft:ocelot");

public GuiEntityPanel entityPanel;
public GuiBodyPartEditor bodyPart;

public GuiEntityMorph(Minecraft mc)
{
super(mc);

this.bodyPart = new GuiBodyPartEditor(mc, this);
this.entityPanel = new GuiEntityPanel(mc, this);

this.registerPanel(this.bodyPart, IKey.lang("metamorph.gui.body_parts.parts"), Icons.LIMB);
this.registerPanel(this.entityPanel, IKey.lang("metamorph.gui.editor.entity"), Icons.POSE);
this.defaultPanel = this.entityPanel;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mchorse.vanilla_pack.editors.panels;

import mchorse.mclib.client.gui.framework.elements.GuiElement;
import mchorse.mclib.client.gui.framework.elements.buttons.GuiButtonElement;
import mchorse.mclib.client.gui.framework.elements.input.GuiTexturePicker;
import mchorse.mclib.client.gui.framework.elements.input.GuiTrackpadElement;
import mchorse.mclib.client.gui.utils.Elements;
import mchorse.mclib.client.gui.utils.keys.IKey;
import mchorse.mclib.utils.Direction;
import mchorse.mclib.utils.resources.RLUtils;
import mchorse.metamorph.api.morphs.EntityMorph;
import mchorse.metamorph.client.gui.editor.GuiMorphPanel;
import mchorse.vanilla_pack.editors.GuiEntityMorph;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiEntityPanel extends GuiMorphPanel<EntityMorph, GuiEntityMorph>
{
public GuiButtonElement texture;
public GuiTrackpadElement scale;
public GuiTexturePicker picker;

public GuiEntityPanel(Minecraft mc, GuiEntityMorph editor)
{
super(mc, editor);

GuiElement element = new GuiElement(mc);

element.flex().relative(this).y(1F).w(130).anchor(0, 1F).column(4).vertical().stretch().padding(10);

this.scale = new GuiTrackpadElement(mc, (v) -> this.morph.scale = v.floatValue());
this.texture = new GuiButtonElement(mc, IKey.lang("metamorph.gui.editor.texture"), (b) ->
{
this.picker.refresh();
this.picker.fill(this.morph.userTexture);
this.add(this.picker);
this.picker.resize();
});
this.texture.tooltip(IKey.lang("metamorph.gui.editor.texture_tooltip"), Direction.TOP);
this.picker = new GuiTexturePicker(mc, (rl) -> this.morph.userTexture = RLUtils.clone(rl));
this.picker.flex().relative(this).wh(1F, 1F);

element.add(this.texture);
element.add(Elements.label(IKey.lang("metamorph.gui.editor.scale")).marginTop(12), this.scale);
this.add(element);
}

@Override
public void fillData(EntityMorph morph)
{
super.fillData(morph);

this.scale.setValue(morph.scale);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/assets/metamorph/lang/de_DE.lang
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ metamorph.gui.creative.context.add_category=Add a new category
metamorph.gui.creative.context.rename_category=Rename category
metamorph.gui.creative.context.rename_category_modal=Give a new name to given category...
metamorph.gui.creative.context.remove_category=Remove category
metamorph.gui.creative.context.remove_morph_modal=Are you sure you want to remove this morph? It will be gone forever...
metamorph.gui.creative.context.remove_category_modal=Are you sure you want to remove this morph category? It will be gone forever...
metamorph.gui.creative.context.clear_category=Remove all morphs
metamorph.gui.creative.context.clear_category_modal=Are you sure you want to remove all morphs in this category? It will be gone forever...
Expand All @@ -90,6 +91,7 @@ metamorph.gui.survival.keys.category=Survival morph menu
metamorph.gui.survival.keys.toggle_favorites=Toggle favorite morphs
metamorph.gui.survival.keys.focus_keybind=Focus keybind

metamorph.gui.editor.entity=Entity properties
metamorph.gui.editor.settings=Morph settings
metamorph.gui.editor.keybind=Keybind
metamorph.gui.editor.keybind_tooltip=Setting keybind here will allow you to morph into this morph by pressing this key either in the world or in survival morph menu
Expand All @@ -102,6 +104,9 @@ metamorph.gui.editor.attack=Attack
metamorph.gui.editor.action=Action
metamorph.gui.editor.reset=Reset
metamorph.gui.editor.item_morph=Item
metamorph.gui.editor.scale=Entity scale
metamorph.gui.editor.texture=Pick a texture...
metamorph.gui.editor.texture_tooltip=This feature won't work for all mobs, because some mobs can have custom rendering implementation
metamorph.gui.editor.keys.category=Morph editor keybinds
metamorph.gui.editor.keys.cycle=Cycle between morph panels

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/assets/metamorph/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ metamorph.gui.survival.keys.category=Survival morph menu
metamorph.gui.survival.keys.toggle_favorites=Toggle favorite morphs
metamorph.gui.survival.keys.focus_keybind=Focus keybind

metamorph.gui.editor.entity=Entity properties
metamorph.gui.editor.settings=Morph settings
metamorph.gui.editor.keybind=Keybind
metamorph.gui.editor.keybind_tooltip=Setting keybind here will allow you to morph into this morph by pressing this key either in the world or in survival morph menu
Expand All @@ -103,6 +104,9 @@ metamorph.gui.editor.attack=Attack
metamorph.gui.editor.action=Action
metamorph.gui.editor.reset=Reset
metamorph.gui.editor.item_morph=Item
metamorph.gui.editor.scale=Entity scale
metamorph.gui.editor.texture=Pick a texture...
metamorph.gui.editor.texture_tooltip=This feature won't work for all mobs, because some mobs can have custom rendering implementation
metamorph.gui.editor.keys.category=Morph editor keybinds
metamorph.gui.editor.keys.cycle=Cycle between morph panels

Expand Down

0 comments on commit 4ffbdc2

Please sign in to comment.