Skip to main content
fixed unsafe cast
Source Link
WildWeazel
  • 306
  • 1
  • 10

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayerif( playerpar7EntityLiving =instanceof (EntityPlayer ) par7EntityLiving;{
if(  EntityPlayer player != null (EntityPlayer) {par7EntityLiving;
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If the playerinstanceof is nullcheck fails, then the cast failed because the parameter did not represent an EntityPlayer object and the block break was caused by something else.

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayer player = (EntityPlayer) par7EntityLiving;
if( player != null ) {
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If player is null, then the cast failed because the parameter did not represent an EntityPlayer object and the block break was caused by something else.

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

if( par7EntityLiving instanceof EntityPlayer ) {
  EntityPlayer player = (EntityPlayer) par7EntityLiving;
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If the instanceof check fails, then the parameter did not represent an EntityPlayer object and the block break was caused by something else.

derp, typo
Source Link
WildWeazel
  • 306
  • 1
  • 10

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayer player = (EntityPlayer) par7EntityLiving;
if( player != null ) {
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If player is null, then the cast failed because EntityPlayerthe parameter did not represent aan EntityPlayer object and the block break was caused by something else.

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayer player = (EntityPlayer) par7EntityLiving;
if( player != null ) {
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If player is null, then the cast failed because EntityPlayer did not represent a EntityPlayer object and the block break was caused by something else.

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayer player = (EntityPlayer) par7EntityLiving;
if( player != null ) {
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If player is null, then the cast failed because the parameter did not represent an EntityPlayer object and the block break was caused by something else.

Source Link
WildWeazel
  • 306
  • 1
  • 10

I'm going to assume you're using the Forge API, given the parameters you listed.

EntityLiving is an abstract type representing living entities like players and mobs. When it is passed into your block break event, it indicates the entity which broke the block. It's not guaranteed to be a player, so you need to try to cast it to the subtype that represents a player:

EntityPlayer player = (EntityPlayer) par7EntityLiving;
if( player != null ) {
  player.addExperience(-1);
  // OR
  player.addExperienceLevel(-1);
}

If player is null, then the cast failed because EntityPlayer did not represent a EntityPlayer object and the block break was caused by something else.