So here’s what I have.
@Listener
public void onEntitySpawn(SpawnEntityEvent event){
//this logs the Spawn event to show that this is working.
//Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("Don't be an Idiot"));
String a = event.getEntities().iterator().next().getType().getName();
Sponge.getGame().getServer().getBroadcastChannel().send(Text.of(a));
}
Now what I’m trying to do is ad an if statement to see what type of mob was spawned. Any Suggestions for If statements? Last I had heard it was like if( entity.getType().equals(EntityTypes.Creeper)){//this ould happen if it was a creeper}
Yes, you have the code right.
K…but how do i make it the entity of the event? My brain is sooo not working this weekend lol. it’s saying it’s not initialized…so let me rephrase this. How do i initialize this entity? cause right now it’s null, so what is the game looking for?
It’s… looking for an entity? You already showed you know how to retrieve one when producing the string a.
Ok…let me recomment haha sorry, so I have it set as Entity entity; so it’s initalized…however when i run the following code line 92 flips out in the console… let me show you
ps. line 92 is the if(enityt.getEntities() portion
Entity entity;
@Listener
public void onEntitySpawn(SpawnEntityEvent event){
//this logs the Spawn event to show that this is working.
//Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("Don't be an Idiot"));
String a = event.getEntities().iterator().next().getType().getName();
if(entity.getType().equals(EntityTypes.CREEPER)){
Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("A " + a + " Just Spawned"));
}
//Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("A " + a + " Just Spawned"));
}
;that’s the code.
Here’s the fml-server log
Again, you should probably learn more of Java before you get into Sponge development. You’ve said ‘Entity e’ but you’ve never actually put a specific entity into it.
yes i know…i wish to grab the entity taht is causing the SpawnEntity Event…when i try to implement it in the method it freaks out. I’m just trying to get this to work. I learn better doing. And would like just to keep moving on. I only ask for help when I seriously need it.
Please just tell me how to make the entity be Entity type that causes the event to fire
Nevermind I figured it out. thank you for your help. But sometimes it helps just to chat with people to work out blocks in coding. If you code for a living you would know that no one knows everything there is to coding. Most of programmers time is spent researching how to do something. That’s what I was doing. I thank you for your time and assistance. Thank you
In case someother aruther was curious here’s the answer to it.
Entity entity = event.getEntities().iterator().next();
I agree that nobody knows everything there is to coding; but simple questions like how to populate a variable or how to use a data structure like a List should be part of learning basic Java.
Also, you’re assuming that only one entity is spawned in the event.
3 Likes