Jump to content

Spawn entity from block [Solved]


Spaceboy Ross

Recommended Posts

I'm trying to spawn an entity from a button being clicked in a gui container. The entity appears for a second then despawns. Does anyone know how to fix this?

Code:

try {
	MobileSuit.MSMob mob = ms.MOB.getConstructor(World.class).newInstance(this.mc.world);
	mob.posX = this.mc.player.posX+1.0;
	mob.posY = this.mc.player.posY+1.0;
	mob.posZ = this.mc.player.posZ+1.0;
	if(!this.mc.world.isRemote) this.mc.world.spawnEntity(mob);
} catch(InstantiationException e) {
	e.printStackTrace();
} catch(IllegalAccessException e) {
	e.printStackTrace();
} catch(IllegalArgumentException e) {
	e.printStackTrace();
} catch(InvocationTargetException e) {
	e.printStackTrace();
} catch(NoSuchMethodException e) {
	e.printStackTrace();
} catch(SecurityException e) {
	e.printStackTrace();
}

 

Edited by Spaceboy Ross
Got the code to work.

The official YouTuber Spaceboy Ross

Link to comment
Share on other sites

You can't spawn an entity on the client. You must do that on a server. Use packets to communicate the button press to a server.

On a completely unrelated note: What on earth is this:

9 minutes ago, Spaceboy Ross said:

MobileSuit.MSMob mob = ms.MOB.getConstructor(World.class).newInstance(this.mc.world);

 

9 minutes ago, Spaceboy Ross said:

catch(InstantiationException e) { e.printStackTrace(); } catch(IllegalAccessException e) { e.printStackTrace(); } catch(IllegalArgumentException e) { e.printStackTrace(); } catch(InvocationTargetException e) { e.printStackTrace(); } catch(NoSuchMethodException e) { e.printStackTrace(); } catch(SecurityException e) { e.printStackTrace(); }

Do you know how to instantinate things in java? Why are you using reflection? Also this is the worst way to handle exceptions. If something went wrong don't just print it and carry on. Minecraft crashes for a reason and so should you.

Edited by V0idWa1k3r
Link to comment
Share on other sites

3 minutes ago, Spaceboy Ross said:

That is a special thing internally used in my mod

This explanation doesn't even mean anything. How is it special? What does internally mean in this context? This is not within your API package so there is zero reason to use reflection. I've looked at your github and I still fail to see a reason to use reflection. If you want to create something dynamic that may or may not be overriden by something else use a factory, not reflection.

 

Link to comment
Share on other sites

It was the only way for me to get it to work also I have it designed that way so I can easily make more mobile suits using the same class. The entity still doesn't spawn.

private void handle(PacketMobileSuit message,MessageContext ctx) {
	EntityPlayerMP playerEntity = ctx.getServerHandler().player;
	World world = playerEntity.getEntityWorld();
	MobileSuit ms = MSRegistry.getMobileSuitFromIndex(message.index);
	try {
		MobileSuit.MSMob mob = ms.MOB.getConstructor(World.class).newInstance(world);
		mob.posX = message.pos.x+1.0;
		mob.posY = message.pos.y+1.0;
		mob.posZ = message.pos.z+1.0;
		world.spawnEntity(mob);
	} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
		e.printStackTrace();
	}
}

 

The official YouTuber Spaceboy Ross

Link to comment
Share on other sites

As I've said please stop using reflection here. It's not the use-case for reflection. Use a factory. Reflection wastes a lot of CPU cycles doing lookups for things. Modded minecraft is laggy as is, don't add more lag to it when you can avoid it.

Packets are handled on a separate network thread. You must encapsulate whathever you are doing in a runnable and use IThreadListener#addScheduledTask. On the server your listener is the WorldServer instance. On the client it's Minecraft.

Also don't blindly trust the client. Always assume that the client lies and perform additional checks in your handler. With your current code anyone could spam the server with artficial spawn packets and spawn thousands upon thousands of your entities whereever they want them to be.

Also that is not the descriptor for IMessageHandler#onMessage. If you are calling this from your packet handler please post the whole code so it is easier to spot what's wrong.

Link to comment
Share on other sites

Why are you using ByteBuf#getIntLE/writeIntLE? Why not writeInt/readInt?

Why are you using ByteBuf#getX in the first place? You should be using ByteBuf#readX. The fact that this

this.index = buf.getIntLE(this.index);

even works is a miracle that only happens because index is 0 by default.

 

FMLCommonHandler.instance().getWorldThread(ctx.netHandler)

=>

ctx.getServerHandler().player.getServerWorld()

Don't use FML internals when it's not necessary.

 

I also have no idea what this

MobileSuit ms = MSRegistry.getMobileSuitFromIndex(message.index);

returns. I assume you've used the debugger and made sure this isn't the cause of your problem?

 

mob.posX = message.pos.x+1.0;
mob.posY = message.pos.y+1.0;
mob.posZ = message.pos.z+1.0;

=> Entity#setLocationAndAngles. Or at the very least Entity#setPosition.

 

How and where are you constructing/sending your packet? Are your fields set to the correct values?

Also make sure that your packet is registered correctly.

Step through your handle method in the debugger and see the values on the fields in your message. Something might be wrong there. Also make sure that the handle method is called in the first place.

 

18 minutes ago, Spaceboy Ross said:

Eclipse wouldn't let me not use a reflection

I don't even... What? What does your IDE have to do with this?

Edited by V0idWa1k3r
Link to comment
Share on other sites

If I didn't use getIntLE/writeIntLE, then I get an exception and the index value would be set to a negative number. Also buf.getIntLE(this.index) was a typo. Also, I was following the code from https://github.com/McJtyMods/ModTutorials/blob/1.12/src/main/java/mcjty/modtut/network/PacketSendKey.java#L46. The MSRegistry is required for my mod to add the entities that I was trying to spawn from a block. All fields for the PacketMobileSuit are correct. Eclipse doesn't like me and when I tried to use a different method it was giving me errors.

The official YouTuber Spaceboy Ross

Link to comment
Share on other sites

9 minutes ago, Spaceboy Ross said:

If I didn't use getIntLE/writeIntLE, then I get an exception and the index value would be set to a negative number.

Then you are doing something wrong. readInt/writeInt should work just fine. What exception are you getting and what number are you trying to write into the buffer(and how)?

 

9 minutes ago, Spaceboy Ross said:

Also, I was following the code from

There is no reason to use Forge's methods here. You know which side the packet is recieved on. The same goes for the code you've linked as far as I am concerned.

 

9 minutes ago, Spaceboy Ross said:

Eclipse doesn't like me and when I tried to use a different method it was giving me errors.

Well you are a programmer. You should be able to fix whatever errors your IDE throws at you. 

 

I'll try debugging your code locally.

Edited by V0idWa1k3r
Link to comment
Share on other sites

As I've said

39 minutes ago, V0idWa1k3r said:

Why are you using ByteBuf#getIntLE/writeIntLE? Why not writeInt/readInt?

Why are you using ByteBuf#getX in the first place? You should be using ByteBuf#readX

As soon as I changed getX to readX and writeIntLE/getIntLE to writeInt/readInt a thing was spawned. The reason is that this

buf.getDouble(1),buf.getDouble(2),buf.getDouble(3)

is not how you read data from a packet and it was reading a 0 for all your positions.

Link to comment
Share on other sites

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.