Jump to content

Recommended Posts

Posted (edited)

Hi, I am trying to make a custom horse.

The NBT does not save. The marking data is completely different.

First attatchmet was at spawn, second was after a reload, third was after another.

 

EntityModHorse:

package mods.giantnuker.horse.entity;


import javax.annotation.Nullable;

import com.google.common.base.Predicate;

import mods.giantnuker.horse.Markings;
import mods.giantnuker.horse.ai.EntityAIHorseAttack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.ai.EntityAIFollowParent;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWanderAvoidWater;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.passive.AbstractChestHorse;
import net.minecraft.entity.passive.AbstractHorse;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;

public class EntityModHorse extends AbstractHorse {
	public int openMouthCounter = 0;
	public Markings markings = new Markings();
	public Predicate<Entity> BREEDABLE_HORSE = new Predicate<Entity>() {
		@Override
		public boolean apply(Entity input) {
			if (!(input instanceof EntityModHorse))return false;
			EntityModHorse horse = (EntityModHorse) input;
			if (!horse.canBreed(EntityModHorse.this)) return false;
			return true;
		}
		
	};
	public EntityModHorse(World world) {
		super(world);
		this.canGallop = true;
	}
	@Override
	protected void initEntityAI() {
		this.tasks.addTask(0, new EntityAISwimming(this));
		this.tasks.addTask(1, new EntityAIHorseAttack(this));
		this.tasks.addTask(2, new EntityAIMate(this, 1.0D, EntityModHorse.class));
		this.tasks.addTask(4, new EntityAIFollowParent(this, 1.3D));
		this.tasks.addTask(6, new EntityAIWanderAvoidWater(this, 0.8D));
		this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
		this.tasks.addTask(8, new EntityAILookIdle(this));
		this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true, new Class[0]));
	}
	public boolean isBreedingItem(ItemStack item) {
		return item.getItem() == Items.GOLDEN_CARROT || item.getItem() == Items.SPECKLED_MELON || item.getItem() == Items.GOLDEN_APPLE;
	}
	@Override
	protected AbstractHorse getClosestHorse(Entity entityIn, double range) {
		return closestBreedableHorse(range);
	}
	@Nullable
	public EntityModHorse closestBreedableHorse(double range) {
		EntityModHorse cHorse = null;
		double dist = Double.MAX_VALUE;
		for (Entity entity : this.world.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().expand(range, range, range), BREEDABLE_HORSE)) {
			if (this.getDistanceSqToEntity(entity) < dist) {
				cHorse = (EntityModHorse) entity;
				dist = this.getDistanceSqToEntity(entity);
			}
		}
		return cHorse;
	}
	
	public boolean canBreed(EntityModHorse breedWith) {
		return true;
	}
	@Override
	public void onUpdate() {
		super.onUpdate();
		if (this.openMouthCounter > 0) {
			this.openMouthCounter--;
			this.setHorseWatchableBoolean(64, true);
		} else if (openMouthCounter == -1) {
			this.setHorseWatchableBoolean(64, true);
		} //else {
			//this.setHorseWatchableBoolean(64, false);
		//}
	}
	/**open mouth for time, time may be -1 for until set to close*/
	public void openHorseMouth(int time) {
		if (!this.world.isRemote) {
			this.openMouthCounter = time;
			this.setHorseWatchableBoolean(64, true);
		}
	}
	
	public void closeHorseMouth() {
		if (!this.world.isRemote) {
			this.openMouthCounter = 0;
			this.setHorseWatchableBoolean(64, false);
		}
	}
	@Override
	public EntityModHorse createChild(EntityAgeable mate) {
		EntityModHorse horse = new EntityModHorse(this.world);
		horse.markings = Markings.merge(this.markings, ((EntityModHorse)mate).markings);
		return horse;
	}
	
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
        ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();

        if (flag && itemstack.getItem() == Items.SPAWN_EGG)
        {
            return super.processInteract(player, hand);
        }
        else
        {
            if (!this.isChild())
            {
                if (this.isTame() && player.isSneaking())
                {
                    this.openGUI(player);
                    return true;
                }

                if (this.isBeingRidden())
                {
                    return super.processInteract(player, hand);
                }
            }

            if (flag)
            {
                if (this.handleEating(player, itemstack))
                {
                    if (!player.capabilities.isCreativeMode)
                    {
                        itemstack.shrink(1);
                    }

                    return true;
                }

                if (itemstack.interactWithEntity(player, this, hand))
                {
                    return true;
                }

                if (!this.isTame())
                {
                    this.makeMad();
                    return true;
                }

                //boolean flag1 = HorseArmorType.getByItemStack(itemstack) != HorseArmorType.NONE;
                boolean flag2 = !this.isChild() && !this.isHorseSaddled() && itemstack.getItem() == Items.SADDLE;

                if (/*flag1 ||*/ flag2)
                {
                    this.openGUI(player);
                    return true;
                }
            }

            if (this.isChild())
            {
                return super.processInteract(player, hand);
            }
            else
            {
                this.mountTo(player);
                return true;
            }
        }
    }
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		super.writeToNBT(nbt);
		nbt.setTag("looks", markings.serializeNBT());
		return nbt;
	}
	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		super.readFromNBT(nbt);
		markings.deserializeNBT(nbt.getTag("looks"));
	}
	
	protected SoundEvent getAmbientSound()
    {
        super.getAmbientSound();
        return SoundEvents.ENTITY_HORSE_AMBIENT;
    }

    protected SoundEvent getDeathSound()
    {
        super.getDeathSound();
        return SoundEvents.ENTITY_HORSE_DEATH;
    }

    protected SoundEvent getHurtSound(DamageSource p_184601_1_)
    {
        super.getHurtSound(p_184601_1_);
        return SoundEvents.ENTITY_HORSE_HURT;
    }

    protected SoundEvent getAngrySound()
    {
        super.getAngrySound();
        return SoundEvents.ENTITY_HORSE_ANGRY;
    }

    protected ResourceLocation getLootTable()
    {
        return LootTableList.ENTITIES_HORSE;
    }
    
    public static class GroupData implements IEntityLivingData
    {
        public Markings variant;

        public GroupData(Markings variantIn)
        {
            this.variant = variantIn;
        }
    }

}

Markings

package mods.giantnuker.horse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;

import mods.giantnuker.backslash.DoubleMap;
import mods.giantnuker.backslash.ListMap;
import mods.giantnuker.horse.entity.EntityModHorse;
import mods.giantnuker.javautil.ArrayUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.LayeredTexture;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.INBTSerializable;

public class Markings implements INBTSerializable {
	public static final String KEY_BODY_PART = "body_part";
	public static final String KEY_BODY = "body";
	public static final String KEY_MARKING = "marking";
	public static final String KEY_ADDED_ALL = "add_all";
	//Registry
	public static Random rand = new Random();
	
	public static Markings merge(Markings marksA, Markings marksB) {
		if (marksB == null || marksA.equals(marksB)) return marksA.copy();
		Markings markings = new Markings();
		markings.markings.clear();
		/*if (rand.nextInt(5) != 0)*/ markings.bodyBase = rand.nextBoolean() ? marksA.bodyBase : marksB.bodyBase;
		for (Entry<String, List<String>> e : marksA.markings.entrySet()) {
			for (String s : e.getValue()) {
				if (rand.nextBoolean()) markings.markings.add(e.getKey(), s);
			}
		}
		for (Entry<String, List<String>> e : marksB.markings.entrySet()) {
			for (String s : e.getValue()) {
				if (rand.nextBoolean()) markings.markings.add(e.getKey(), s);
			}
		}
		return markings;
	}
	public static final HashMap<String, ResourceLocation> renders = new HashMap();
	public static final DoubleMap<String, String, ResourceLocation> tex_ids = new DoubleMap();
	static {
		//Body
		tex_ids.put(KEY_BODY, "black", new ResourceLocation(HorseMod.MODID, "textures/entity/body/black.png"));
		tex_ids.put(KEY_BODY, "white", new ResourceLocation(HorseMod.MODID, "textures/entity/body/white.png"));
		tex_ids.put(KEY_BODY, "creamy", new ResourceLocation(HorseMod.MODID, "textures/entity/body/creamy.png"));
		tex_ids.put(KEY_BODY, "chestnut", new ResourceLocation(HorseMod.MODID, "textures/entity/body/chestnut.png"));
		tex_ids.put(KEY_BODY, "brown", new ResourceLocation(HorseMod.MODID, "textures/entity/body/brown.png"));
		tex_ids.put(KEY_BODY, "darkbrown", new ResourceLocation(HorseMod.MODID, "textures/entity/body/darkbrown.png"));
		tex_ids.put(KEY_BODY, "gray", new ResourceLocation(HorseMod.MODID, "textures/entity/body/gray.png"));
		//Mane
		tex_ids.put(KEY_BODY_PART, "mane_black0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/black0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_black1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/black1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_white0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/white0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_white1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/white1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_creamy0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/creamy0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_creamy1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/creamy1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_chestnut0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/chestnut0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_chestnut1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/chestnut1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_brown0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/brown0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_brown1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/brown1.png"));
		//Tail
		tex_ids.put(KEY_BODY_PART, "tail_white0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/white0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_white1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/white1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_black0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/black0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_black1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/black1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_creamy_top0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/creamy_top0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_creamy_top1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/creamy_top1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut_top1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut3.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut_top0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut2.png"));
		//add to all
		tex_ids.put(KEY_ADDED_ALL, "chest", new ResourceLocation(HorseMod.MODID, "textures/entity/misc/chest.png"));
		tex_ids.put(KEY_ADDED_ALL, "riding_gear", new ResourceLocation(HorseMod.MODID, "textures/entity/misc/riding_gear.png"));
		//Additional Markings
		tex_ids.put(KEY_MARKING, "blackdots", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blackdots.png"));
		tex_ids.put(KEY_MARKING, "greyfield", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/greyfield.png"));
		tex_ids.put(KEY_MARKING, "blackfield", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blackfield.png"));
		tex_ids.put(KEY_MARKING, "blaze", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blaze.png"));
		tex_ids.put(KEY_MARKING, "white", new ResourceLocation("textures/entity/horse/horse_markings_white.png"));
		tex_ids.put(KEY_MARKING, "whitefield", new ResourceLocation("textures/entity/horse/horse_markings_whitefield.png"));
		tex_ids.put(KEY_MARKING, "whitedots", new ResourceLocation("textures/entity/horse/horse_markings_whitedots.png"));
		tex_ids.put(KEY_MARKING, "blackshag", new ResourceLocation("textures/entity/horse/horse_markings_blackdots.png"));
	}
	//Instance
	public Markings() {
		randomize();
	}
	public Markings(int oldID) {
		/*int j = (oldID & 255) % 7;
		int k = ((oldID & 65280) >> 8) % 5;
		switch (j) {
		case 1:	baseCoat = "creamy";
		case 2:	baseCoat = "chestnut";
		case 3:	baseCoat = "brown";
		case 4:	baseCoat = "black";
		case 5:	baseCoat = "gray";
		case 6:	baseCoat = "darkbrown";
		default:baseCoat = "white";
		}
		switch (k) {
		case 1: markings.add("marks_whitefield");
		case 2: markings.add("marks_whitedots");
		case 3: markings.add("marks_blackdots");
		default:markings.add("marks_white");
		}*/
	}
	public void randomize() {
		markings.clear();
		bodyBase = tex_ids.keySet(KEY_BODY).get(new Random().nextInt(tex_ids.size(KEY_BODY)));
		int bodyParts = rand.nextInt(5);
		for (int i = 0; i < bodyParts; i++) {
			String mark = tex_ids.keySet(KEY_BODY_PART).get(rand.nextInt(tex_ids.size(KEY_BODY_PART)));
			if (markings.contains(KEY_BODY_PART, mark)) continue;
			markings.add(KEY_BODY_PART, mark);
		}
		int marks = rand.nextInt(5);
		for (int i = 0; i < marks; i++) {
			String mark = tex_ids.keySet(KEY_MARKING).get(rand.nextInt(tex_ids.size(KEY_MARKING)));
			if (markings.contains(KEY_MARKING, mark)) continue;
			markings.add(KEY_MARKING, mark);
		}
	}
	public ListMap<String, String> markings = new ListMap();
	public String bodyBase = null;
	@Override
	public void deserializeNBT(NBTBase arg0) {
		if (!(arg0 instanceof NBTTagCompound)) {
			return;
		}
		NBTTagCompound nbt = (NBTTagCompound) arg0;
		System.out.println("-------------------------");
		for (String key : nbt.getKeySet()) {
			if (key == "bodyBase") bodyBase = nbt.getString(key);
			else if (nbt.getTag(key) instanceof NBTTagList){
				markings.clear(key);
				NBTTagList list = (NBTTagList) nbt.getTag(key);
				for (NBTBase nbt2 : list) {
					if (nbt2 instanceof NBTTagString) markings.add(key, ((NBTTagString)nbt2).getString());
				}
			}
		}
		System.out.println(markings);
	}
	@Override
	public NBTBase serializeNBT() {
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setString("bodyBase", bodyBase);
		for (Entry<String, List<String>> e : markings.entrySet()) {
			NBTTagList tl = new NBTTagList();
			for (String s : e.getValue()) tl.appendTag(new NBTTagString(s));
			nbt.setTag(e.getKey(), tl);
		}
		return nbt;
	}
	public ResourceLocation getTex() {
		String texId = "gianthorses:b" + bodyBase;
		for (Entry<String, List<String>> list : markings.entrySet()) {
			texId +=  "[" + list.getKey() + "]";
			for (String s : list.getValue()) texId += "(" + s + ")";
		}
		ResourceLocation tex = renders.get(texId);
		if (tex == null) {
			tex = new ResourceLocation(texId);
			ArrayList<String> tex_mp = new ArrayList();
			tex_mp.add(tex_ids.get(KEY_BODY, bodyBase).toString());
			//for (int i = 0; i < markings.size(); i++) tex_lst[i + 1] = (tex_ids.get(markings.get(i)).toString());
			for (Entry<String, List<String>> list: markings.entrySet()) {
				for (String s : list.getValue()) tex_mp.add(tex_ids.get(list.getKey(), s).toString());
			}
			for (String s : tex_ids.keySet(KEY_ADDED_ALL)) {
				tex_mp.add(tex_ids.get(KEY_ADDED_ALL, s).toString());
			}
			Minecraft.getMinecraft().getTextureManager().loadTexture(tex, new LayeredTexture(tex_mp.toArray(new String[0])));
			renders.put(texId, tex);
		}
		return tex;
	}
	public Markings copy() {
		System.out.println("cpy");
		Markings m = new Markings();
		m.markings.clear();
		m.bodyBase = this.bodyBase;
		for (Entry<String, List<String>> e : this.markings.entrySet()) {
			for (String s : e.getValue()) {
				m.markings.add(e.getKey(), s);
			}
		}
		System.out.println("B4:" + markings);
		System.out.println("AFTER:" + m.markings);
		return m;
	}
}

All my checks show that the horses have the right data, do I need to send packets to the client to tell it what the horse's NBT is?

2017-10-16_18.27.36.png

2017-10-16_18.27.58.png

2017-10-16_18.29.30.png

Edited by GiantNuker
Posted
10 hours ago, diesieben07 said:

This has nothing to do with NBT, but yes, you do need to tell the client what the texture should be.

You can either use a DataParameter or custom packets.

Thanks, Iwas wondering why horses used a data peramiter for that :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.