Jump to content

Recommended Posts

Posted

I'm trying to make a block that when the player walks on it they will be randomly teleported, so i figured i could just use the onEntityWalking method then convert the entity to an entityPlayer and then figure out how to make them teleport, but I seem to be unable to cast entity to entityPlayer. does anyone know a way to make entitys teleport or how to cast to entityPlayer

Posted

on this is the code with out me trying to teleport the player as i have not figured out how to do that yet.

public void onEntityWalk(World worldIn, BlockPos pos, Entity entityIn)
    {
      if(entityIn instanceof IMob)
      {
    	  return;
    	  
      }
      else if(entityIn instanceof IAnimals)
      {
    	  
    	  return;
      }
      else
      {
    	 EntityPlayer player = (EntityPlayer)entityIn;
    	 //the error here says,"cannot cast from Entity to EntityPlayer"
      }
    }

 

Posted

regardless if it is actually player or not it should still let me cast, but your right if i ran the code the way it is now it would probably crash

Posted

Have you definitely imported the right

Entity

class?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

regardless if it is actually player or not it should still let me cast, but your right if i ran the code the way it is now it would probably crash

 

Writing code that does a cast will always compile.  It's when it tries to cast something and it can't be that it will crash.  There's no syntax error with what you've written, but there is a fundamental problem that will cause runtime exceptions.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Point.

 

However, that's only true when the classes are not related at all.  For cases where you're taking a generic object and downcasting to a more specific one, where it will sometimes be valid, then that won't throw a pre-compile error.

 

eg

 

String s = "hello";
Object o = s;
Integer i = (Integer) o;

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Makes sense.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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