Elevator
-
@‘XDoctorX’:
package com.XDoctorX.main; import java.io.DataInput; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ElevatorEvent { public EntityPlayer player; public World world; public PlayerMovementEvent.Type type; @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if (event.entity instanceof EntityPlayer) { int[] positionBlocCible = BlockElevator.getTargetBlock(world, (int)player.posX, (int)player.posY, (int)player.posZ, true); } } }Rassure-moi, tu le fais exprès?
Ce que je t’ai donné, c’est le code pour récupérer la position du prochain ascenseur au-dessus du joueur.
Or, là :- Tu ne vérifies même pas si le joueur est sur un bloc ascenseur
- Tu passes une variable membre non-initialisée en argument alors que la bonne variable est là, juste sous ton nez
- Tu n’utilises même pas la valeur récupérée (bon, en même temps y’a pas grand chose à récupérer vu que ça crash. Mais pourquoi ça crash? Tout simplement parce que tu passes une variable non-initialisée en argument. T’étonne pas d’avoir des NullPointerException si la valeur est null).
Tout ça ne me donne pas du tout envie de t’aider.
Comme on te l’a sûrement déjà dit : Essaie de comprendre le code avant de l’utiliser.
Apprends le Java, je sais pas, moi. Quand t’étais petit, fallait qu’on t’explique comment on empile des cubes en plastique ou bien?
Désolé si j’emploie un ton agressif mais copier-coller un bout de code au hasard ça va bien 3 minutes, mais après faut pas être surpris si on a des crashs à tout bout de champ.
T’as déjà trouvé comment récupérer la position du joueur. Ok, c’est assez facile, mais ça veut bien dire que tu peux réfléchir par toi-même et trouver une solution à partir de ce qu’on te propose. Sans créer des variables au hasard sans se demander à quoi peuvent servir celles qui existent déjà.Du coup je sais vraiment pas si je te fais la correction de ton code…
Bon, allez, histoire d’essayer de t’apprendre quelque chose :package com.XDoctorX.main; import java.io.DataInput; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ElevatorEvent { @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if (event.entity instanceof EntityPlayer && event.entity.worldObj.getBlock(event.entity.posX, event.entity.posY - 1, event.entity.posZ) == ClassePrincipale.*block_elevator*) { EntityPlayer p = (EntityPlayer)(event.entity); int[] posBlocCible = BlockElevator.getTargetBlock(world, (int)p.posX, (int)p.posY, (int)p.posZ, true); p.setPosition(posBlocCible[0], posBlocCible[1]+1, posBlocCible[2]); } } } -
sa marche pas c’est le meme crash report
-
Toujours un NPE à la ligne 27 de ta classe ElevatorEvent ?
-
Désolé, pourtant en théorie ce code marche très bien, on a remplacé les variables qui causaient l’erreur.
-
non je croit
package com.XDoctorX.main; import java.io.DataInput; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ElevatorEvent { public World world; @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if (event.entity instanceof EntityPlayer && event.entity.worldObj.getBlock((int)event.entity.posX,(int) event.entity.posY - 1,(int) event.entity.posZ) == XDoctorX.Elevator) { EntityPlayer p = (EntityPlayer)(event.entity); int[] posBlocCible = BlockElevator.getTargetBlock(world, (int)p.posX, (int)p.posY, (int)p.posZ, true); p.setPosition(posBlocCible[0], posBlocCible[1]+1, posBlocCible[2]); } } } -
public World world; n’a rien à faire là.
int[] posBlocCible = BlockElevator.getTargetBlock(world, (int)p.posX, (int)p.posY, (int)p.posZ, true);
-> event.entity.worldObj à la place de world ici. -
sa marche pas et il y a a pas de crash report
-
@‘robin4002’:
public World world; n’a rien à faire là.
int[] posBlocCible = BlockElevator.getTargetBlock(world, (int)p.posX, (int)p.posY, (int)p.posZ, true);
-> event.entity.worldObj à la place de world ici.Oups, merci Robin ^^’ Erreur d’inattention
-
je lais fait mais sa marche pas et il y a plus de crash report
-
marche pas = il se passe rien ? Tu as bien placé les blocs ? Renvoies également le code actuel.
-
Rien se passe.
code du Event :
package com.XDoctorX.main; import java.io.DataInput; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ElevatorEvent { @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if (event.entity instanceof EntityPlayer && event.entity.worldObj.getBlock((int)event.entity.posX,(int) event.entity.posY - 1,(int) event.entity.posZ) == XDoctorX.Elevator) { EntityPlayer p = (EntityPlayer)(event.entity); int[] posBlocCible = BlockElevator.getTargetBlock(event.entity.worldObj, (int)p.posX, (int)p.posY, (int)p.posZ, true); p.setPosition(posBlocCible[0], posBlocCible[1]+1, posBlocCible[2]); } } } -
Tu essaies bien en te plaçant sur un bloc ascenseur?
-
Essayes avec p.setPositionAndUpdate(posBlocCible[0], posBlocCible[1]+1, posBlocCible[2]);
-
je suis sur le bloc, rien se passe j"ai mit
p.addChatComponentMessage(new ChatComponentTranslation(“test”));
sa marche une seul fois apres sa marche plus et le code est maintenant
package com.XDoctorX.main; import java.io.DataInput; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class ElevatorEvent { @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if (event.entity instanceof EntityPlayer && event.entity.worldObj.getBlock((int)event.entity.posX,(int) event.entity.posY - 1,(int) event.entity.posZ) == XDoctorX.Elevator) { EntityPlayer p = (EntityPlayer)(event.entity); p.addChatComponentMessage(new ChatComponentTranslation("test")); int[] posBlocCible = BlockElevator.getTargetBlock(event.entity.worldObj, (int)p.posX, (int)p.posY, (int)p.posZ, true); p.setPositionAndUpdate(posBlocCible[0], posBlocCible[1]+1, posBlocCible[2]); } } } -
up up up up up up up up
-
24h avant de up, merci de lire le règlement du forum …
Envoies-moi un zip de ton dossier src je vais tester de mon côté. -
-
XDoctorX.java, fonction init, ajoute ça :
FMLCommonHandler.instance().bus().register(new ElevatorEvent());
En dessous de la ligne avec Forge.
BlockElevator :package com.XDoctorX.block; import org.lwjgl.input.Keyboard; import com.XDoctorX.main.XDoctorX; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; public class BlockElevator extends Block { public EntityPlayer player; public World world; public BlockElevator() { super(Material.circuits); } public static int getTargetBlock(World w, int x, int y, int z, boolean up) { if(up) { for(int i = y; i <= 255; i++) { if(w.getBlock(x, i, z) == XDoctorX.Elevator) { return i; } } } else { for(int i = y; i >= 0; i–) { if(w.getBlock(x, i, z) == XDoctorX.Elevator) { return i; } } } return -1; } }ElevatorEvent :
package com.XDoctorX.main; import com.XDoctorX.block.BlockElevator; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.MathHelper; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; public class ElevatorEvent { @SubscribeEvent public void onLivingJumpEvent(LivingJumpEvent event) { if(event.entity instanceof EntityPlayer) { if(event.entity.worldObj.getBlock(MathHelper.floor_double(event.entity.posX), MathHelper.floor_double(event.entity.posY - 1), MathHelper.floor_double(event.entity.posZ)) == XDoctorX.Elevator) { EntityPlayer p = (EntityPlayer)event.entity; int posBlocCible = BlockElevator.getTargetBlock(event.entity.worldObj, MathHelper.floor_double(event.entity.posX), MathHelper.floor_double(event.entity.posY), MathHelper.floor_double(event.entity.posZ), true); if(posBlocCible != -1) { p.setPositionAndUpdate(event.entity.posX, posBlocCible + 1, event.entity.posZ); } } } } @SubscribeEvent public void onPlayerTick(TickEvent.PlayerTickEvent event) { if(event.phase == TickEvent.Phase.START) { if(event.player.isSneaking()) { if(event.player.worldObj.getBlock(MathHelper.floor_double(event.player.posX), MathHelper.floor_double(event.player.posY - 1), MathHelper.floor_double(event.player.posZ)) == XDoctorX.Elevator) { int posBlocCible = BlockElevator.getTargetBlock(event.player.worldObj, MathHelper.floor_double(event.player.posX), MathHelper.floor_double(event.player.posY) - 2, MathHelper.floor_double(event.player.posZ), false); if(posBlocCible != -1) { event.player.setPositionAndUpdate(event.player.posX, posBlocCible + 1, event.player.posZ); event.player.setSneaking(false); } } } } } } -
merci
-
Balise résolu ?