OnItemRightClick sur une entity.
-
**Hors-Sujet : ** Oui mais est-ce-que ceux qui ne prenez même pas la peine d’apprendre le Java sur OpenClassroom prendront la peine de regarder des tutoriels de Java sur MFF ? C’est la question que je me pose …
Vu que c’est du hors-sujet tu peux supprimer le message, je soulevais juste la question -
Une fois que le tutoriel sera disponible je ne vais plus m’embêter. Si quelqu’un demande de l’aide dans support pour les moddeurs et que je remarque que le minimum nécessaire pour coder en Java n’est pas acquis ça sera lock + lien vers le tutoriel.
À la base la description de la section c’est ça :Il s’agit de la section de support pour ceux qui créent des mods avec Forge. L’aide au modding va ici, cependant, gardez à l’esprit que ce n’est pas une école de programmation Java. Vous êtes censé avoir au moins certaines base de Java avant de poster ici.
-
J’ai fais comme vous avez dis mais j’ai un problème encore :
Detected ongoing potential memory leak. 4000 packets have leaked. Top offenders
[22:36:38] [Client thread/ERROR] [FML]: amm: : 4000
[22:36:38] [Client thread/ERROR] [FML]: Detected ongoing potential memory leak. 4100 packets have leaked. Top offenders
fuite de mémoire …
Class item:public class LIIT extends ItemFood { AT alcohol = new AT(); public LIIT (int itemID, int i, boolean b) { super(itemID, i, b); } public EnumAction getItemUseAction(ItemStack itemstack) { return EnumAction.drink; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.canEat(true)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 32; } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { –stack.stackSize; ExtendedEntityPropAlcool prop = ExtendedEntityPropAlcool.get(player); prop.setAlcool(prop.getAlcool() + 1); player.getFoodStats().addStats(1, 2); // nombre de demi-gigots + saturation world.playSoundAtEntity(player, "random.burp", 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addPotionEffect(new PotionEffect(Potion.confusion.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.hunger.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.weakness.id, 315,0)); return stack; } @Override public boolean hasEffect(ItemStack par1ItemStack){ return true; } }class ExtendedEntity:
public class ExtendedEntityPropAlcool implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtPropAlcool"; private int alcool = 0; private final EntityPlayer player; public ExtendedEntityPropAlcool(EntityPlayer player) { this.player = player; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("alcool", this.alcool); compound.setTag(EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.alcool = properties.getInteger("alcool"); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedEntityPropAlcool.EXT_PROP_NAME, new ExtendedEntityPropAlcool(player)); } public static final ExtendedEntityPropAlcool get(EntityPlayer player) { return (ExtendedEntityPropAlcool) player.getExtendedProperties(EXT_PROP_NAME); } public final void sync() { PacketAlcool packetAlcool = new PacketAlcool(this.alcool); AltisMineMod.network.sendToServer(packetAlcool); if (!player.worldObj.isRemote) { EntityPlayerMP playerClient = (EntityPlayerMP) this.player; AltisMineMod.network.sendTo(packetAlcool, playerClient); } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedEntityPropAlcool playerData = ExtendedEntityPropAlcool.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedEntityPropAlcool playerData = ExtendedEntityPropAlcool.get(player); NBTTagCompound savedData = CommonProxy .getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public void setAlcool(int taux) { this.alcool = taux; this.sync(); } public int getAlcool() { this.alcool = 0; if (alcool != 0) alcool = Math.abs(alcool / 2); else if (alcool == 0) { alcool = Math.abs(alcool); } return this.alcool; } } -
public final void sync() { PacketAlcool packetAlcool = new PacketAlcool(this.alcool); AltisMineMod.network.sendToServer(packetAlcool); if (!player.worldObj.isRemote) { EntityPlayerMP playerClient = (EntityPlayerMP) this.player; AltisMineMod.network.sendTo(packetAlcool, playerClient); } }Change en :
public final void sync() { PacketAlcool packetAlcool = new PacketAlcool(this.alcool); if (!player.worldObj.isRemote) { EntityPlayerMP playerClient = (EntityPlayerMP) this.player; AltisMineMod.network.sendTo(packetAlcool, playerClient); } else { AltisMineMod.network.sendToServer(packetAlcool); } } -
Merci problème réglé par conte c’est exactement pareil 0 même lorsque on boit.
Edit:
Je suis sur que le problème vient d’ici:public int getAlcool() { this.alcool = 0; if (alcool != 0) alcool = Math.abs(alcool / 2); else if (alcool == 0) { alcool = Math.abs(alcool); } return this.alcool; } } -
Mets directement return this.alcool;
-
"Je comprend pas pourquoi ça renvoie toujours 0 … : "
public int getAlcool() {
this.alcool = 0;xD
-
pareil marche toujours pas

-
Quelqu’un ?
-
Renvoies ton code actuel.
-
package com.AltisMine.mod; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; public class ExtendedEntityPropAlcool implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "ExtPropAlcool"; private int alcool = 0; private final EntityPlayer player; public ExtendedEntityPropAlcool(EntityPlayer player) { this.player = player; } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("alcool", this.alcool); compound.setTag(EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.alcool = properties.getInteger("alcool"); } @Override public void init(Entity entity, World world) { // TODO Auto-generated method stub } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedEntityPropAlcool.EXT_PROP_NAME, new ExtendedEntityPropAlcool(player)); } public static final ExtendedEntityPropAlcool get(EntityPlayer player) { return (ExtendedEntityPropAlcool) player.getExtendedProperties(EXT_PROP_NAME); } public final void sync() { PacketAlcool packetAlcool = new PacketAlcool(this.alcool); if (!player.worldObj.isRemote) { EntityPlayerMP playerClient = (EntityPlayerMP) this.player; AltisMineMod.network.sendTo(packetAlcool, playerClient); } else { AltisMineMod.network.sendToServer(packetAlcool); } } private static String getSaveKey(EntityPlayer player) { return player.getDisplayName() + ":" + EXT_PROP_NAME; } public static void saveProxyData(EntityPlayer player) { ExtendedEntityPropAlcool playerData = ExtendedEntityPropAlcool.get(player); NBTTagCompound savedData = new NBTTagCompound(); playerData.saveNBTData(savedData); CommonProxy.storeEntityData(getSaveKey(player), savedData); } public static void loadProxyData(EntityPlayer player) { ExtendedEntityPropAlcool playerData = ExtendedEntityPropAlcool.get(player); NBTTagCompound savedData = CommonProxy .getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } playerData.sync(); } public void setAlcool(int taux) { this.alcool = taux; this.sync(); } public int getAlcool() { this.alcool = 0; if (alcool != 0) alcool = Math.abs(alcool / 2); else if (alcool == 0) { alcool = Math.abs(alcool); } return this.alcool; } }Class bouteille d’alcool:
package com.AltisMine.mod; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumAction; import net.minecraft.item.ItemFood; import net.minecraft.item.ItemStack; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; import net.minecraft.world.World; public class LIIT extends ItemFood { AT alcohol = new AT(); public LIIT (int itemID, int i, boolean b) { super(itemID, i, b); } public EnumAction getItemUseAction(ItemStack itemstack) { return EnumAction.drink; } public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if (player.canEat(true)) { player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); } return stack; } public int getMaxItemUseDuration(ItemStack stack) { return 32; } public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) { –stack.stackSize; ExtendedEntityPropAlcool prop = ExtendedEntityPropAlcool.get(player); prop.setAlcool(prop.getAlcool() + 1); player.getFoodStats().addStats(1, 2); // nombre de demi-gigots + saturation world.playSoundAtEntity(player, "random.burp", 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addPotionEffect(new PotionEffect(Potion.confusion.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.hunger.id, 315,0)); player.addPotionEffect(new PotionEffect(Potion.weakness.id, 315,0)); return stack; } @Override public boolean hasEffect(ItemStack par1ItemStack){ return true; } } -
Le getAlcool tu mets juste return alcool, c’est tout ! Pourquoi tout ce code ?
-
J’essaye ça je vous dis
-
@‘Snowy_1803’:
Le getAlcool tu mets juste return alcool, c’est tout ! Pourquoi tout ce code ?
Surtout que Robin le lui a déjà dit de mettre return this.alcool (et il l’a pas fait)
@‘robin4002’:
Mets directement return this.alcool;
-
Niquel merci
-
Juste une dernière question si je veux faire baisser le niveau d’alcoolémie de 1 au bout de 15 minutes je dois faire comment ?
-
TickHandler avec la variable getTotalWorldTime() + modulo
-
TickEvent.PlayerTickEvent*
L’interface ITickHandler n’existe plus depuis la 1.7 -
J’ai trouvais ça sur internet je dois définir le nombre de tick comment?
@SubscribeEvent public void onPlayerTickEvent(TickEvent.PlayerTickEvent event) { if (event.phase == TickEvent.Phase.START && !event.player.worldObj.isRemote) { IExtendedEntityProperties properties = event.player.getExtendedProperties(PlayerExtendedProperties.ID); if (properties instanceof PlayerExtendedProperties) { PlayerExtendedProperties playerExtendedProperties = (PlayerExtendedProperties) properties; playerExtendedProperties.tick(); } properties = event.player.getExtendedProperties(PlayerPreferencesProperties.ID); if (properties instanceof PlayerPreferencesProperties) { PlayerPreferencesProperties preferencesProperties = (PlayerPreferencesProperties) properties; preferencesProperties.tick(RFToolsMessages.INSTANCE); } } } -
Le nombre de tick à la seconde est déjà défini.
Ton code va logiquement crash car worldObj sera null lorsque le menu principal sera ouvert.
Du coup null-check pour le world et pour faire vérifier un code tous les x nbre de tick, c’est comme çaif(event.player.worldObj.getTotalWorldTime() % (nbreDeSecondes * 20) == 0 { System.out.println("ton code à exécuter avec ton ExtProp"); }