Moved model scale parts into a single ModelScale class, similar to ModelRotate.

Added NPC model data interfaces to the API.
This commit is contained in:
Luis C.M committed 2023-03-17 02:36:28 -05:00
1 parent 2ef93dd35f
commit f0b33cf913
63 files changed
+412 -283

No files matched your search

@@ -0,0 +1,45 @@
package noppes.npcs.entity.data;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.world.World;
import noppes.npcs.controllers.PixelmonHelper;
import noppes.npcs.entity.EntityNPCInterface;
public class ModelData extends ModelDataShared{
public EntityLivingBase getEntity(EntityNPCInterface npc){
if(entityClass == null)
return null;
if(entity == null){
try {
entity = entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {npc.worldObj});
entity.readEntityFromNBT(extra);
if(entity instanceof EntityLiving){
EntityLiving living = (EntityLiving)entity;
living.setCurrentItemOrArmor(0, npc.getHeldItem() != null?npc.getHeldItem():npc.getOffHand());
living.setCurrentItemOrArmor(1, npc.inventory.armorItemInSlot(3));
living.setCurrentItemOrArmor(2, npc.inventory.armorItemInSlot(2));
living.setCurrentItemOrArmor(3, npc.inventory.armorItemInSlot(1));
living.setCurrentItemOrArmor(4, npc.inventory.armorItemInSlot(0));
}
if(PixelmonHelper.isPixelmon(entity) && npc.worldObj.isRemote){
if(extra.hasKey("Name"))
PixelmonHelper.setName(entity, extra.getString("Name"));
else
PixelmonHelper.setName(entity, "Abra");
}
} catch (Exception e) {
}
}
return entity;
}
public ModelData copy(){
ModelData data = new ModelData();
data.readFromNBT(this.writeToNBT());
return data;
}
}
@@ -0,0 +1,207 @@
package noppes.npcs.entity.data;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.api.entity.data.IModelData;
import noppes.npcs.api.entity.data.IModelRotate;
import noppes.npcs.api.entity.data.IModelScale;
import java.util.HashMap;
public class ModelDataShared implements IModelData {
public ModelScale modelScale = new ModelScale();
public boolean enableRotation = false;
public ModelRotate rotation = new ModelRotate();
public ModelPartData legParts = new ModelPartData();
public Class<? extends EntityLivingBase> entityClass;
public EntityLivingBase entity;
public NBTTagCompound extra = new NBTTagCompound();
private HashMap<String,ModelPartData> parts = new HashMap<String,ModelPartData>();
public byte breasts = 0;
public byte headwear = 2;
public byte bodywear = 0;
public byte armwear = 0;
public byte legwear = 0;
// Solid or normal arm/legwear [0: None, 1: Both, 2: Right, 3: Left]
public byte solidArmwear = 0;
public byte solidLegwear = 0;
// Hide Body Parts [0: None, 1: Both, 2: Right, 3: Left],
public byte hideHead = 0;
public byte hideBody = 0;
public byte hideArms = 0;
public byte hideLegs = 0;
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
if(entityClass != null)
compound.setString("EntityClass", entityClass.getCanonicalName());
compound = this.modelScale.writeToNBT(compound);
compound.setTag("LegParts", legParts.writeToNBT());
compound.setByte("Headwear", headwear);
compound.setByte("Bodywear", bodywear);
compound.setByte("Armwear", armwear);
compound.setByte("Legwear", legwear);
compound.setByte("SolidArmwear", solidArmwear);
compound.setByte("SolidLegwear", solidLegwear);
compound.setByte("hideHead", hideHead);
compound.setByte("hideBody", hideBody);
compound.setByte("hideArms", hideArms);
compound.setByte("hideLegs", hideLegs);
compound.setBoolean("EnableRotation", enableRotation);
if(enableRotation)
compound.setTag("ModelRotation", rotation.writeToNBT());
compound.setByte("Breasts", breasts);
compound.setTag("ExtraData", extra);
NBTTagList list = new NBTTagList();
for(String name : parts.keySet()){
NBTTagCompound item = parts.get(name).writeToNBT();
item.setString("PartName", name);
list.appendTag(item);
}
compound.setTag("Parts", list);
return compound;
}
public void readFromNBT(NBTTagCompound compound){
setEntity(compound.getString("EntityClass"));
this.modelScale.readFromNBT(compound);
legParts.readFromNBT(compound.getCompoundTag("LegParts"));
headwear = compound.getByte("Headwear");
bodywear = compound.getByte("Bodywear");
armwear = compound.getByte("Armwear");
legwear = compound.getByte("Legwear");
solidArmwear = compound.getByte("SolidArmwear");
solidLegwear = compound.getByte("SolidLegwear");
hideHead = compound.getByte("hideHead");
hideBody = compound.getByte("hideBody");
hideArms = compound.getByte("hideArms");
hideLegs = compound.getByte("hideLegs");
enableRotation = compound.getBoolean("EnableRotation");
if(enableRotation)
rotation.readFromNBT(compound.getCompoundTag("ModelRotation"));
breasts = compound.getByte("Breasts");
extra = compound.getCompoundTag("ExtraData");
HashMap<String,ModelPartData> parts = new HashMap<String,ModelPartData>();
NBTTagList list = compound.getTagList("Parts", 10);
for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound item = list.getCompoundTagAt(i);
ModelPartData part = new ModelPartData();
part.readFromNBT(item);
parts.put(item.getString("PartName"), part);
}
this.parts = parts;
}
public void setEntityClass(Class<? extends EntityLivingBase> entityClass){
this.entityClass = entityClass;
entity = null;
extra = new NBTTagCompound();
if(entityClass == EntityHorse.class)
extra.setInteger("Type", -1);
}
public Class<? extends EntityLivingBase> getEntityClass(){
return entityClass;
}
public float offsetY() {
if(entity == null)
return -getBodyY();
return entity.height - 1.8f;
}
public void clearEntity() {
entity = null;
}
public ModelPartData getPartData(String type){
return parts.get(type);
}
public void removePart(String type) {
parts.remove(type);
}
public ModelPartData getOrCreatePart(String type) {
ModelPartData part = parts.get(type);
if(part == null)
parts.put(type, part = new ModelPartData());
return part;
}
public float getBodyY(){
if(legParts.type == 3)
return (0.9f - modelScale.body.scaleY) * 0.75f + getLegsY();
if(legParts.type == 3)
return (0.5f - modelScale.body.scaleY) * 0.75f + getLegsY();
return (1 - modelScale.body.scaleY) * 0.75f + getLegsY();
}
public float getLegsY() {
if(legParts.type == 3)
return (0.87f - modelScale.legs.scaleY);
return (1 - modelScale.legs.scaleY) * 0.75f;
}
public void enableRotation(boolean enableRotation) {
this.enableRotation = enableRotation;
}
public boolean enableRotation() {
return this.enableRotation;
}
public IModelRotate getRotation() {
return this.rotation;
}
public IModelScale getScale() {
return this.modelScale;
}
public void setEntity(String string) {
entityClass = null;
entity = null;
try {
Class<?> cls = Class.forName(string);
if (EntityLivingBase.class.isAssignableFrom(cls))
entityClass = cls.asSubclass(EntityLivingBase.class);
} catch (ClassNotFoundException ignored) {}
}
public String getEntity() {
return entityClass == null ? null : entityClass.getName();
}
}
@@ -0,0 +1,76 @@
package noppes.npcs.entity.data;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
public class ModelPartData {
public int color = 0xFFFFFF;
public String texture;
public byte type = 0;
public boolean playerTexture;
private ResourceLocation location;
public ModelPartData(){
playerTexture = true;
}
public ModelPartData(String texture) {
this.texture = texture;
playerTexture = false;
}
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("Type", type);
compound.setInteger("Color", color);
if(texture != null && !texture.isEmpty()) {
compound.setString("Texture", texture);
}
compound.setBoolean("PlayerTexture", playerTexture);
return compound;
}
public void readFromNBT(NBTTagCompound compound){
type = compound.getByte("Type");
color = compound.getInteger("Color");
texture = compound.getString("Texture");
playerTexture = compound.getBoolean("PlayerTexture");
location = null;
}
public ResourceLocation getResource(){
if(texture.isEmpty())
return null;
if(location != null)
return location;
location = new ResourceLocation(texture);
return location;
}
public void setTexture(String texture, int type) {
this.type = (byte) type;
this.location = null;
if(texture.isEmpty()){
playerTexture = true;
this.texture = texture;
}
else{
this.texture = "customnpcs:textures/parts/"+ texture + ".png";
playerTexture = false;
}
}
public String toString(){
return "Color: " + color + " Type: " + type;
}
public String getColor() {
String str = Integer.toHexString(color);
while(str.length() < 6)
str = "0" + str;
return str;
}
}
@@ -0,0 +1,88 @@
package noppes.npcs.entity.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.api.entity.data.IModelRotate;
import noppes.npcs.api.entity.data.IModelRotatePart;
import noppes.npcs.util.ValueUtil;
public class ModelRotate implements IModelRotate {
// Rotation
public boolean whileStanding = true;
public boolean whileAttacking = false;
public boolean whileMoving = false;
public ModelRotatePart head = new ModelRotatePart();
public ModelRotatePart body = new ModelRotatePart();
public ModelRotatePart larm = new ModelRotatePart();
public ModelRotatePart rarm = new ModelRotatePart();
public ModelRotatePart lleg = new ModelRotatePart();
public ModelRotatePart rleg = new ModelRotatePart();
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("PuppetStanding", whileStanding);
compound.setBoolean("PuppetAttacking", whileAttacking);
compound.setBoolean("PuppetMoving", whileMoving);
compound.setTag("PuppetHead", head.writeToNBT());
compound.setTag("PuppetBody", body.writeToNBT());
compound.setTag("PuppetRArm", rarm.writeToNBT());
compound.setTag("PuppetLArm", larm.writeToNBT());
compound.setTag("PuppetRLeg", rleg.writeToNBT());
compound.setTag("PuppetLLeg", lleg.writeToNBT());
return compound;
}
public void readFromNBT(NBTTagCompound compound){
whileStanding = compound.getBoolean("PuppetStanding");
whileAttacking = compound.getBoolean("PuppetAttacking");
whileMoving = compound.getBoolean("PuppetMoving");
head.readFromNBT(compound.getCompoundTag("PuppetHead"));
body.readFromNBT(compound.getCompoundTag("PuppetBody"));
rarm.readFromNBT(compound.getCompoundTag("PuppetRArm"));
larm.readFromNBT(compound.getCompoundTag("PuppetLArm"));
rleg.readFromNBT(compound.getCompoundTag("PuppetRLeg"));
lleg.readFromNBT(compound.getCompoundTag("PuppetLLeg"));
}
public boolean whileStanding() {
return this.whileStanding;
}
public void whileStanding(boolean whileStanding) {
this.whileStanding = whileStanding;
}
public boolean whileAttacking() {
return this.whileAttacking;
}
public void whileAttacking(boolean whileAttacking) {
this.whileAttacking = whileAttacking;
}
public boolean whileMoving() {
return this.whileMoving;
}
public void whileMoving(boolean whileMoving) {
this.whileMoving = whileMoving;
}
public IModelRotatePart getPart(int part) {
switch (ValueUtil.CorrectInt(part,0,5)) {
case 0:
return head;
case 1:
return body;
case 2:
return larm;
case 3:
return rarm;
case 4:
return lleg;
case 5:
return rleg;
}
return null;
}
}
@@ -0,0 +1,52 @@
package noppes.npcs.entity.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.api.entity.data.IModelRotatePart;
import noppes.npcs.util.ValueUtil;
public class ModelRotatePart implements IModelRotatePart {
public float rotationX = 0f, rotationY = 0f, rotationZ = 0f;
public boolean disabled = false;
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("Disabled", disabled);
compound.setFloat("RotationX", rotationX);
compound.setFloat("RotationY", rotationY);
compound.setFloat("RotationZ", rotationZ);
return compound;
}
public void readFromNBT(NBTTagCompound compound){
disabled = compound.getBoolean("Disabled");
rotationX = ValueUtil.correctFloat(compound.getFloat("RotationX"), -0.5f, 0.5f);
rotationY = ValueUtil.correctFloat(compound.getFloat("RotationY"), -0.5f, 0.5f);
rotationZ = ValueUtil.correctFloat(compound.getFloat("RotationZ"), -0.5f, 0.5f);
}
public void setRotation(float x, float y, float z) {
this.rotationX = x;
this.rotationY = y;
this.rotationZ = z;
}
public float getRotateX() {
return this.rotationX;
}
public float getRotateY() {
return this.rotationY;
}
public float getRotateZ() {
return this.rotationZ;
}
public void disabled(boolean disabled) {
this.disabled = disabled;
}
public boolean disabled() {
return this.disabled;
}
}
@@ -0,0 +1,43 @@
package noppes.npcs.entity.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.api.entity.data.IModelScale;
import noppes.npcs.api.entity.data.IModelScalePart;
import noppes.npcs.util.ValueUtil;
public class ModelScale implements IModelScale {
public ModelScalePart head = new ModelScalePart();
public ModelScalePart body = new ModelScalePart();
public ModelScalePart arms = new ModelScalePart();
public ModelScalePart legs = new ModelScalePart();
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setTag("HeadConfig", head.writeToNBT());
compound.setTag("BodyConfig", body.writeToNBT());
compound.setTag("ArmsConfig", arms.writeToNBT());
compound.setTag("LegsConfig", legs.writeToNBT());
return compound;
}
public void readFromNBT(NBTTagCompound compound) {
head.readFromNBT(compound.getCompoundTag("HeadConfig"));
body.readFromNBT(compound.getCompoundTag("BodyConfig"));
arms.readFromNBT(compound.getCompoundTag("ArmsConfig"));
legs.readFromNBT(compound.getCompoundTag("LegsConfig"));
}
@Override
public IModelScalePart getPart(int part) {
switch (ValueUtil.CorrectInt(part,0,3)) {
case 0:
return head;
case 1:
return body;
case 2:
return arms;
case 3:
return legs;
}
return null;
}
}
@@ -0,0 +1,51 @@
package noppes.npcs.entity.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.api.entity.data.IModelScalePart;
import noppes.npcs.util.ValueUtil;
public class ModelScalePart implements IModelScalePart {
public float scaleX = 1, scaleY = 1, scaleZ = 1;
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setFloat("ScaleX", scaleX);
compound.setFloat("ScaleY", scaleY);
compound.setFloat("ScaleZ", scaleZ);
return compound;
}
public void readFromNBT(NBTTagCompound compound){
scaleX = ValueUtil.correctFloat(compound.getFloat("ScaleX"), 0.5f, 1.5f);
scaleY = ValueUtil.correctFloat(compound.getFloat("ScaleY"), 0.5f, 1.5f);
scaleZ = ValueUtil.correctFloat(compound.getFloat("ScaleZ"), 0.5f, 1.5f);
}
public String toString(){
return "ScaleX: " + scaleX + " - ScaleY: " + scaleY + " - ScaleZ: " + scaleZ;
}
public void setScale(float x, float y) {
scaleZ = scaleX = x;
scaleY = y;
}
public void setScale(float x, float y, float z) {
scaleX = ValueUtil.correctFloat(x, 0.5f, 1.5f);
scaleY = ValueUtil.correctFloat(y, 0.5f, 1.5f);
scaleZ = ValueUtil.correctFloat(z, 0.5f, 1.5f);
}
public float getScaleX() {
return scaleX;
}
public float getScaleY() {
return scaleY;
}
public float getScaleZ() {
return scaleZ;
}
}