Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi,

 

I'm creating a single entity but it each spawn of it will be different,  texture, drop etc. Im going to be doing all based on the 'type' of it. Which is just a string. To give each entity a type on spawn, I decided to use the on entity constructing event and then gave the string 'spiderType' a random index of an array I have. Anyway, I get a NPE

EntityOreSpider.getEntity().spiderType = EntityOreSpider.getEntity().spiderTypes[rand.nextInt(EntityOreSpider.getEntity().spiderTypes.length)];

 

And heres the full code.

 

Event Class:

package mcub.helpers;

import java.util.Random;

import mcub.entities.EntityOreSpider;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;

public class ForgeEvents 
{

private Random rand = new Random();

@ForgeSubscribe
public void oreSpiderConstruct(EntityConstructing event)
{
	if(event.entity instanceof EntityOreSpider)
	{
		EntityOreSpider.getEntity().spiderType = EntityOreSpider.getEntity().spiderTypes[rand.nextInt(EntityOreSpider.getEntity().spiderTypes.length)];
		if(EntityOreSpider.getEntity().spiderType == "diamond")
		{
			System.out.println("diamnd");
		}
		else if(EntityOreSpider.getEntity().spiderType == "gold")
		{
			System.out.println("gold");
		}
		else
		{
			System.out.println("null");
		}
	}
}
}

 

Entity Class:

package mcub.entities;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.world.World;

public class EntityOreSpider extends EntitySpider
{

private static EntityOreSpider spider;
public String[] spiderTypes = {"diamond", "gold"};
public String spiderType;
public EntityOreSpider(World world) 
{
	super(world);

}

public static EntityOreSpider getEntity()
{
	return spider;
}

}

  • Author

You seriously need to go and learn the basics of java/OOP.

Hm, why not point out what I did wrong?

  • Author

Hm, why not point out what I did wrong?

What is EntityOreSpider.getEntity() supposed to ever return other than null?

Why are the spiderTypes non-static?

Advice taken.

if(event.entity instanceof EntityOreSpider)
{
    EntityOreSpider entity = event.entity;
   if (entity.getEntity().spiderType() == "diamond")
   {

and so on...

May be wrong since i cant figure out what your getEntity() method does.

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

Remove the getEntity() method. It is 100% useless.

Use the entity instance that is given to you by the event.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.