Jump to content

Recommended Posts

Posted

I am writing my first mod (yeay!) but I am really having trouble with my Entity.

 

I am extending EntityHorse with EntityPony. I would like to add one random integer variable in that class to use it as an index to get the name and texture for the Pony.

 

I first tried just making it a class member, but quickly saw it was different on the client and the server for the same entity! I have tried implementing IExtendedEntityProperties, but I got the same behavior. I also tried adding the int to dataWatcher, but that set ALL instances of my object to the same random int, which is not what I want.

 

I'm good with Java but very new to Minecraft sourcecode so any help there would really be appreciated.

Posted

I first tried just making it a class member, but quickly saw it was different on the client and the server for the same entity! I have tried implementing IExtendedEntityProperties, but I got the same behavior. I also tried adding the int to dataWatcher, but that set ALL instances of my object to the same random int, which is not what I want.

Either of those methods should be suitable; post your code?  I think you must have implemented them incorrectly somehow.

 

-TGG

 

Posted

I agree. This is the code where I try to use dataWrapper.

ponyId is the int value that I cannot get to work the way I want.

 

My main mod class

 

@SidedProxy(clientSide = "com.jedigurl.minecraft.mylittlepony.ClientProxy", serverSide = "com.jedigurl.minecraft.mylittlepony.CommonProxy") //Tells Forge the location of your proxies
   public static CommonProxy proxy;

   static SimpleNetworkWrapper wrapper = NetworkRegistry.INSTANCE.newSimpleChannel("myChannel");

   @EventHandler
   public void preinit(FMLPreInitializationEvent event)
   {
      ModBlocks.init();
   }

   @EventHandler
   public void init(FMLInitializationEvent event){ //Your main initialization method

      proxy.registerRenders();
      proxy.registerEventListeners();

       BiomeGenBase[] ponyBiomes = {BiomeGenBase.plains};
       addSpawn(EntityPony.class, 100, 1, 1, ponyBiomes);
   }
   
   @EventHandler
   public void load(FMLInitializationEvent event)
   {
      MinecraftForge.EVENT_BUS.register(new MyLittlePonyEventHandler());
   }

   public void addSpawn(Class<? extends EntityLiving> entityClass, int spawnProb, int min, int max, BiomeGenBase[] biomes) {
      if (spawnProb > 0) {
         EntityRegistry.addSpawn(entityClass, spawnProb, min, max, EnumCreatureType.creature, biomes);
      }
   }

 

 

common proxy

 

public class CommonProxy {


public void registerRenders()
{
	// NOOP on server
}
public void registerEventListeners() 
{
    System.out.println("Registering event listeners");
    MinecraftForge.EVENT_BUS.register(new MyLittlePonyEventHandler());
}


public String getCurrentLanguage()
{
	return null;
}

public void registerTileEntities(boolean b)
{
	GameRegistry.registerBlock(ModBlocks.pinkStoneBlock, "pinkStoneBlock");
	//EntityRegistry.registerGlobalEntityID(EntityPony.class, "entityPony", EntityRegistry.findGlobalUniqueEntityId(), 0xffffff, 0xbbbbbb);
	EntityRegistry.registerModEntity(EntityPony.class, "entityPony", 1, MyLittlePony.instance, 160, 5, true);
}
public boolean isRemote()
{
	return false;
}

public World getCurrentWorld()
{
	return MinecraftServer.getServer().getEntityWorld();
}

 

 

clientproxy

 

public class ClientProxy extends CommonProxy
{
public void registerRenders()
{
	RenderingRegistry.registerEntityRenderingHandler(EntityPony.class, new RenderPony(new ModelHorse(), new Float(1))); //shadow size=1
	// Get int colors for Pink egg
	int bgcolor_int = (210 << 16) + (159 <<  + 228;
	int fgcolor_int = (237 << 16) + (67 <<  + 140;
	//register Pony type and create spawn egg
	registerEntity(EntityPony.class, MyLittlePony.MODID +"_entityPony", bgcolor_int, fgcolor_int);

}

//register Entity type and create spawn egg
public void registerEntity(Class<? extends Entity> entityClass, String entityName, int bkEggColor, int fgEggColor) {
	int id = EntityRegistry.findGlobalUniqueEntityId();

	EntityRegistry.registerGlobalEntityID(entityClass, entityName, id);
	EntityList.entityEggs.put(Integer.valueOf(id), new EntityEggInfo(id, bkEggColor, fgEggColor));
}

public boolean isRemote()
{
	return true;
}
}

 

 

entity

 

public class EntityPony extends EntityHorse {
private static final MyLittlePonyList[] ponies = MyLittlePonyList.values();
private static int ponyId;

public static final int ENTITY_TYPE = 14; // define the data watcher index

@Override
protected void entityInit() {
	super.entityInit();
	dataWatcher.addObject(ENTITY_TYPE, (int) rand.nextInt(MyLittlePonyList.values().length));
	System.out.println("Init random pony ID: " + this.dataWatcher.getWatchableObjectInt(ENTITY_TYPE));
}
public EntityPony(World par1World) {
	super(par1World);
	//int ponyId = ExtendedPony.get(this).getPonyId();
	if(this.worldObj.isRemote){
          this.ponyId = this.dataWatcher.getWatchableObjectInt(ENTITY_TYPE);
          System.out.println("Client recieved ponyId " + ponyId);
	} else {
		System.out.println("Server set ponyId to " + ponyId);
	}
//		setPony(RND.nextInt(ponies.length));
	System.out.println("My pony is " + ponies[ponyId].name() + " (" + ponyId + ")");
	setCustomNameTag(ponies[ponyId].title());
	System.out.println("Her nametag is \"" + getCustomNameTag() + "\"");
	System.out.println("Her texture is " + getHorseTexture());
	setHorseTamed(true);
	setHorseSaddled(true);
	setHorseType(0); //regular horse

	setChested(true);
}

public void setPonyId(int id) {
	System.out.println("Setting ponyID " + ponyId + " to " + ponies[id]);
	dataWatcher.updateObject(ENTITY_TYPE, (int) id);
	setCustomNameTag(ponies[id].title());
}

public int getPonyId() {
	this.ponyId = this.dataWatcher.getWatchableObjectInt(ENTITY_TYPE);
                setCustomNameTag(ponies[ponyId].title());
	System.out.println("Getting ponyId " + ponyId);
	return this.ponyId;
}

/**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
@Override
    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
	super.writeEntityToNBT(par1NBTTagCompound);
    par1NBTTagCompound.setInteger("PonyId", ponyId);
    System.out.println("Saving " + ponyId);
    }

 /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
@Override
    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readEntityFromNBT(par1NBTTagCompound);
        setPonyId(par1NBTTagCompound.getInteger("PonyId"));
//        dataWatcher.updateObject(ENTITY_TYPE, (int) ponyId);
        //this.setCustomNameTag(par1NBTTagCompound.getString("PonyNameTag"));
        System.out.println("Loading " + ponyId);
    }	
@Override 
public boolean canMateWith(EntityAnimal par1EntityAnimal)
    {// never mate
	return false;
    }	
@SideOnly(Side.CLIENT)
    public String getHorseTexture()
    {
//		System.out.println("setting " + pony.texture());
	//int ponyId = ExtendedPony.get(this).getPonyId();
	return ponies[ponyId].texture();
    }

@Override
 @SideOnly(Side.CLIENT)
 public String[] getVariantTexturePaths()
 {
	 return null;
 }

}

 

 

render

 

public class RenderPony extends RenderHorse {
public RenderPony(ModelBase par1ModelBase, float par2) {
	super(par1ModelBase, par2);
}
@Override
    protected ResourceLocation getEntityTexture(EntityHorse par1EntityHorse)
    {
//		System.out.println("rendering " + par1EntityHorse.getHorseTexture());
    	return new ResourceLocation(MyLittlePony.MODID, ((EntityPony)par1EntityHorse).getHorseTexture());
    }
}

 

 

eventhandler

(not used currently, but was correctly printing debug statements, so I was getting the events)

 

@SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
	if (event.entity instanceof EntityPony && ExtendedPony.get((EntityPony) event.entity) == null)
	{
		// This is how extended properties are registered using our convenient method from earlier
		//ExtendedPony.register((EntityPony) event.entity);
		//ExtendedPony props = ExtendedPony.get((EntityPony) event.entity);
		//props.setPonyId(event.entity.worldObj.rand.nextInt(MyLittlePonyList.values().length));
		//System.out.println("Finished EntityConstructing event, created " + props.getPonyId());
	}
}

@SubscribeEvent
public void onEntityJoinWorldEvent(EntityJoinWorldEvent event)
{
	if (event.entity instanceof EntityPony)
	{
		//ExtendedPony props = ExtendedPony.get((EntityPony) event.entity);
		//props.setPonyId(event.entity.worldObj.rand.nextInt(MyLittlePonyList.values().length));
		//System.out.println("Finished EntityJoinWorldEvent on EntityPony event");
	}		
}

 

 

also unused, extended impl

 

public class ExtendedPony implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "MyLittlePony_ExtendedPony";
private final EntityPony pony;
private int ponyId;

public ExtendedPony(EntityPony pony)
{
	this.pony = pony;
	this.ponyId = pony.worldObj.rand.nextInt(MyLittlePonyList.values().length);
	String location = "client";
	if (pony.worldObj.isRemote) 
		location = "server";
	System.out.println("New pony created on " + location);

}

/**
 * Used to register these extended properties for the player during EntityConstructing event
 * This method is for convenience only; it will make your code look nicer
 */
public static final void register(EntityPony pony)
{
	pony.registerExtendedProperties(ExtendedPony.EXT_PROP_NAME, new ExtendedPony(pony));
}

/**
 * Returns ExtendedPlayer properties for player
 * This method is for convenience only; it will make your code look nicer
 */
public static final ExtendedPony get(EntityPony pony)
{
	return (ExtendedPony) pony.getExtendedProperties(EXT_PROP_NAME);
}

// Save any custom data that needs saving here
@Override
public void saveNBTData(NBTTagCompound compound)
{
	NBTTagCompound properties = new NBTTagCompound();
	properties.setInteger("PonyId", this.ponyId);
	compound.setTag(EXT_PROP_NAME, properties);
	System.out.println("[PROPS] PonyId into NBT " + this.ponyId);
}

// Load whatever data you saved
@Override
public void loadNBTData(NBTTagCompound compound)
{
	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
	this.ponyId = properties.getInteger("PonyId");
	System.out.println("[PROPS] PonyId from NBT: " + this.ponyId);
}

@Override
public void init(Entity entity, World world)
{
}

public int getPonyId() {
	return this.ponyId;
}

public void setPonyId(int ponyId) {
	System.out.println("Set ponyID " + this.ponyId + " to " + ponyId);
	this.ponyId = ponyId;
}
}

 

Posted

ExtendedPony is unneeded. You don't need to use IEEP if it's your own entity.

You are syncing the pony id (that sounds like some sort of texture ID). If that doesn't change after the entity is spawned, you just need to implement IEntityAdditionalSpawnData on your Entity and write the data you need on the client.

 

Ok, ExtenedPony is removed. Correct, ponyId is a enum mapped to a string name and string texture location. 

I looked up how to use IEntityAdditionalSpawnData and someone said it was just like EntityVilliger. I read that class and realized I should not be keeping a copy of the int locally in the method at all. I should just used getPonyId() and setPonyId() methods to access the datawatcher for my value.

 

Thanks for the help everyone! This code works:

 

public class EntityPony extends EntityHorse {// implements IEntityAdditionalSpawnData {

private static final MyLittlePonyList[] ponies = MyLittlePonyList.values();

public static final int PONY_TYPE = 14; // define the data watcher index

@Override
protected void entityInit() {
	super.entityInit();
	//Choose a random pony
	dataWatcher.addObject(PONY_TYPE, (int) rand.nextInt(MyLittlePonyList.values().length));
}

public EntityPony(World par1World) {
	super(par1World);

	System.out.println("My pony is " + ponies[getPonyId()].name() + " (" + getPonyId() + ")");
	setCustomNameTag(ponies[getPonyId()].title());
	System.out.println("Her nametag is \"" + getCustomNameTag() + "\"");
	System.out.println("Her texture is " + getHorseTexture());
	setHorseTamed(true);
	setHorseSaddled(true);
	setHorseType(0); //regular horse
	setChested(true);
	//horseChest.setInventorySlotContents(0,new ItemStack(Items.saddle));
}

public void setPonyId(int id) {
	dataWatcher.updateObject(PONY_TYPE, (int) id);
}

public int getPonyId() {
	int ponyId = this.dataWatcher.getWatchableObjectInt(PONY_TYPE);
	return ponyId;
}

/**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
@Override
    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
	super.writeEntityToNBT(par1NBTTagCompound);
    par1NBTTagCompound.setInteger("PonyId", getPonyId());
    System.out.println("Saving " + getPonyId());
    }

 /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
@Override
    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readEntityFromNBT(par1NBTTagCompound);
        setPonyId(par1NBTTagCompound.getInteger("PonyId"));
    }

@Override 
public boolean canMateWith(EntityAnimal par1EntityAnimal)
    {// never mate
	return false;
    }

@SideOnly(Side.CLIENT)
    public String getHorseTexture()
    {
	return ponies[getPonyId()].texture();
    }

@Override
 @SideOnly(Side.CLIENT)
 public String[] getVariantTexturePaths()
 {
	 return null;
 }

}

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am using forge 1.20.1 (version 47.3.0). My pc has an RTX 4080 super and an i9 14900 KF, I am on the latest Nvidia graphics driver, latest windows 10 software, I have java 23, forge 1.12.2 works and so does all vanilla versions but for some reason no version of forge 1.20.1 works and instead the game just crashes with the error code "-1." I have no mods in my mods fodler, I have deleted my options.txt and forge.cfg files in case my settings were causing a crash and have tried removing my forge version from the installations folder and reinstalling but no matter what I still crash with the same code and my log doesn't tell me anything: 18:34:53.924 game 2025-02-06 18:34:53,914 main WARN Advanced terminal features are not available in this environment 18:34:54.023 game [18:34:54] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, mrmirchi, --version, 1.20.1-forge-47.3.0, --gameDir, C:\Users\aryam\AppData\Roaming\.minecraft, --assetsDir, C:\Users\aryam\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 2db00ea8d678420a8956109a85d90e9d, --accessToken, ????????, --clientId, ZWI3NThkNzMtNmNlZS00MGI5LTgyZTgtYmZkNzcwMTM5MGMx, --xuid, 2535436222989555, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\aryam\AppData\Roaming\.minecraft\quickPlay\java\1738838092785.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.3.0, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] 18:34:54.027 game [18:34:54] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 18:34:54.132 game [18:34:54] [main/INFO] [ne.mi.fm.lo.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow 18:34:54.191 game [18:34:54] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6 18:34:54.303 game [18:34:54] [main/INFO] [EARLYDISPLAY/]: Requested GL version 4.6 got version 4.6 18:34:54.367 monitor Process Monitor Process crashed with exit code -1     screenshot of log: https://drive.google.com/file/d/1WdkH88H865XErvmIqAKjlg7yrmj8EYy7/view?usp=sharing
    • I am currently working on a big mod, but I'm having trouble with my tabs, I want to find a way to add tabs inside tabs, like how in mrcrayfishes furniture mod, his furniture tab has multiple other sub tabs to them, so i know it is possible but i just don't know how it is possible, any help would be appreciated, thanks
    • Add the crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here  
    • Make a test with adding this mod: https://www.curseforge.com/minecraft/mc-mods/betterrandomsourceconcurrencycrash If you have further issues, create an own thread
    • hi same thing happened to me this is my paste bin please help!  crash report - https://pastes.io/crash-rep
  • Topics

×
×
  • Create New...

Important Information

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