Résolu Crash LivingDeathEvent
-
J’ai vraiment du mal avec cette méthode, je comprend mal les antagonistes.
java.lang.NoClassDefFoundError: net/minecraft/client/Minecraft
at fr.powergame.modpg2.common.LivingEventHandler.onEntityKilled(LivingEventHandler.java:37)package fr.powergame.modpg2.common; import java.util.Random; import net.minecraft.client.Minecraft; import net.minecraft.entity.DataWatcher; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class LivingEventHandler { @SubscribeEvent public void onEntityKilled(LivingDeathEvent event) { Random rand = new Random(); ItemStack stack1 = new ItemStack(ModPg2.itemRandomMunition, 1); ItemStack stack2 = new ItemStack(ModPg2.itemMetadata, 1, 0); //Insigne ItemStack stack3 = new ItemStack(ModPg2.itemMetadata, 1, 1); //Dent ItemStack stack4 = new ItemStack(ModPg2.itemMetadata, 1, 2); //Collier if (event.source.getEntity() instanceof EntityPlayer) { if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt <= 5) { // 5/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 5 && randInt <= 15) { // 10/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 15) { // 84/100 event.entityLiving.entityDropItem(stack1, 1); } } if(Minecraft.getMinecraft().thePlayer != null) { int killG1 = Minecraft.getMinecraft().thePlayer.getEntityData().getInteger("killG1"); if(!Minecraft.getMinecraft().thePlayer.worldObj.isRemote) { killG1++; Minecraft.getMinecraft().thePlayer.getEntityData().setInteger("killG1", killG1); } if(killG1 == 50) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG1a); } if(killG1 == 200) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG1b); } if(killG1 == 1000) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG1c); } if(killG1 == 5000) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG1d); } } } if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster2) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt == 0) { // 1/100 event.entityLiving.entityDropItem(stack3, 1); if(Minecraft.getMinecraft().thePlayer != null) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementDentOr); } } if (randInt > 0 && randInt <= 10) { // 10/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 5 && randInt <= 15) { // 10/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 15) { // 79/100 event.entityLiving.entityDropItem(stack1, 1); } } if(Minecraft.getMinecraft().thePlayer != null) { int killG2 = Minecraft.getMinecraft().thePlayer.getEntityData().getInteger("killG2"); if(!Minecraft.getMinecraft().thePlayer.worldObj.isRemote) { killG2++; Minecraft.getMinecraft().thePlayer.getEntityData().setInteger("killG2", killG2); } if(killG2 == 25) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG2a); } if(killG2 == 100) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG2b); } if(killG2 == 500) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG2c); } if(killG2 == 1000) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG2d); } } } if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster3) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt <= 2) { // 3/100 event.entityLiving.entityDropItem(stack3, 1); if(Minecraft.getMinecraft().thePlayer != null) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementDentOr); } } if (randInt > 2 && randInt <= 12) { // 10/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 12 && randInt <= 17) { // 5/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 17) { // 82/100 event.entityLiving.entityDropItem(stack1, 1); } } } if(Minecraft.getMinecraft().thePlayer != null) { int killG3 = Minecraft.getMinecraft().thePlayer.getEntityData().getInteger("killG3"); if(!Minecraft.getMinecraft().thePlayer.worldObj.isRemote) { killG3++; Minecraft.getMinecraft().thePlayer.getEntityData().setInteger("killG3", killG3); } if(killG3 == 10) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG3a); } if(killG3 == 40) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG3b); } if(killG3 == 200) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG3c); } if(killG3 == 400) { Minecraft.getMinecraft().thePlayer.triggerAchievement(ModPg2.achievementKillG3d); } } } }
Ls crashs en boucle, ça use.
-
Salut,
LivingDeathEvent est un event serveur donc il ne faut pas utiliser les classes clients. (net/minecraft/client/Minecraft est une classe client).
Pour avoir le joueur qui a tué l’entité living il faut que tu cast EntityPlayer à event.source.getEntity() (après avoir vérifier que event.source.getEntity() est d’instance EntityPlayer, ce qui est déjà le cas dans ton code). -
“Minecraft.getMinecraft().thePlayer != null”
Cette classe n’est pas incluse dans les serveurs et comme tu l’appelles sur le serveur, ça crash.
Pour récupérer le joueur uyilises plutôt évent.entity (truc du genre) en vérifiant bie’ si l’entité est un joueur. -
Ce qui donnerait ça?
package fr.powergame.modpg2.common; import java.util.Random; import net.minecraft.client.Minecraft; import net.minecraft.entity.DataWatcher; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class LivingEventHandler { @SubscribeEvent public void onEntityKilled(LivingDeathEvent event) { Random rand = new Random(); EntityPlayer player = (EntityPlayer) event.source.getEntity(); ItemStack stack1 = new ItemStack(ModPg2.itemRandomMunition, 1); ItemStack stack2 = new ItemStack(ModPg2.itemMetadata, 1, 0); //Insigne ItemStack stack3 = new ItemStack(ModPg2.itemMetadata, 1, 1); //Dent ItemStack stack4 = new ItemStack(ModPg2.itemMetadata, 1, 2); //Collier if (event.source.getEntity() instanceof EntityPlayer) { if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt <= 5) { // 5/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 5 && randInt <= 15) { // 10/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 15) { // 84/100 event.entityLiving.entityDropItem(stack1, 1); } } if(player != null) { int killG1 = player.getEntityData().getInteger("killG1"); if(!player.worldObj.isRemote) { killG1++; player.getEntityData().setInteger("killG1", killG1); } if(killG1 == 50) { player.triggerAchievement(ModPg2.achievementKillG1a); } if(killG1 == 200) { player.triggerAchievement(ModPg2.achievementKillG1b); } if(killG1 == 1000) { player.triggerAchievement(ModPg2.achievementKillG1c); } if(killG1 == 5000) { player.triggerAchievement(ModPg2.achievementKillG1d); } } } if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster2) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt == 0) { // 1/100 event.entityLiving.entityDropItem(stack3, 1); if(player != null) { player.triggerAchievement(ModPg2.achievementDentOr); } } if (randInt > 0 && randInt <= 10) { // 10/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 5 && randInt <= 15) { // 10/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 15) { // 79/100 event.entityLiving.entityDropItem(stack1, 1); } } if(player != null) { int killG2 = player.getEntityData().getInteger("killG2"); if(!player.worldObj.isRemote) { killG2++; player.getEntityData().setInteger("killG2", killG2); } if(killG2 == 25) { player.triggerAchievement(ModPg2.achievementKillG2a); } if(killG2 == 100) { player.triggerAchievement(ModPg2.achievementKillG2b); } if(killG2 == 500) { player.triggerAchievement(ModPg2.achievementKillG2c); } if(killG2 == 1000) { player.triggerAchievement(ModPg2.achievementKillG2d); } } } if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityMobGangster3) { int randInt = rand.nextInt(100); for(int j = 0 ; j < 2; ++j) { if (randInt <= 2) { // 3/100 event.entityLiving.entityDropItem(stack3, 1); if(player != null) { player.triggerAchievement(ModPg2.achievementDentOr); } } if (randInt > 2 && randInt <= 12) { // 10/100 event.entityLiving.entityDropItem(stack2, 1); } if (randInt > 12 && randInt <= 17) { // 5/100 event.entityLiving.entityDropItem(stack4, 1); } if (randInt > 17) { // 82/100 event.entityLiving.entityDropItem(stack1, 1); } } } if(player != null) { int killG3 = player.getEntityData().getInteger("killG3"); if(!player.worldObj.isRemote) { killG3++; player.getEntityData().setInteger("killG3", killG3); } if(killG3 == 10) { player.triggerAchievement(ModPg2.achievementKillG3a); } if(killG3 == 40) { player.triggerAchievement(ModPg2.achievementKillG3b); } if(killG3 == 200) { player.triggerAchievement(ModPg2.achievementKillG3c); } if(killG3 == 400) { player.triggerAchievement(ModPg2.achievementKillG3d); } } } }
Le player.getEntityData().getInteger(“…”) me fait douter, me demande si je dois pas rajouter un null check qqpart.
-
Non ne t’inquiète pas la fonction en créer un nouveau si il est null
-
C’est ça, le seul problème c’est que cette ligne :
EntityPlayer player = (EntityPlayer) event.source.getEntity();
Doit être à l’intérieur du bloc de cette condition :
if (event.source.getEntity() instanceof EntityPlayer)
Sinon tu peux avoir un NPE ou un ClassCastException. -
Ouki merci les gars ^^