Affichage de capabilities
-
Salut ,
Le truc c’est que par exemple j’ai un bloc , et lorsque je clique dessus ça incrémente de 1 la capabilities .
Et je print dans le chat cette valeur . Et ça marche parfaitement même sur serveur …Donc pour moi la syncro fonctionne , mais c’est seulement l’affichage de la valeur dans le tooltip de l’item qui ne fonctionne pas …
Je ne voit donc pas comment résoudre le problème …Cordialement ,
-
Quel est le code du bloc en question ?
-
Le voici :
public class DofusCraftCraftingTable extends Block { public static final PropertyDirection FACING = BlockHorizontal.FACING; public DofusCraftCraftingTable(String unlocalizedName, String registryName) { super(Material.WOOD); this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH)); this.setUnlocalizedName(unlocalizedName); this.setRegistryName(new ResourceLocation(Reference.MODID, registryName)); this.setSoundType(SoundType.WOOD); this.setCreativeTab(DofusCraftCore.blocks); } @SuppressWarnings("deprecated") public boolean isOpaqueCube(IBlockState state) { return false; } @SuppressWarnings("deprecated") public boolean isFullCube(IBlockState state) { return false; } public boolean isPassable(IBlockAccess worldIn, BlockPos pos) { return true; } @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if( !world.isRemote ) { player.openGui(DofusCraftCore.instance, GuiHandler.guiCraftingTableID, world, pos.getX(), pos.getY(), pos.getZ()); IDofusCraftLevels cap = player.getCapability(DofusCraftLevelsProvider.LEVEL_CAP, null); cap.setLevel(cap.getLevel() + 1); String message = String.format("Hello there, you have §7%d§r levels.", (int) cap.getLevel()); player.sendMessage(new TextComponentString(message)); } return true; } @SuppressWarnings("deprecated") public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()); } public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2); } @SuppressWarnings("deprecated") public EnumBlockRenderType getRenderType(IBlockState state) { return EnumBlockRenderType.MODEL; } @SuppressWarnings("deprecated") public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING))); } @SuppressWarnings("deprecated") @Override public IBlockState getStateFromMeta(int meta) { EnumFacing facing = EnumFacing.getFront(meta); if( facing.getAxis() == EnumFacing.Axis.Y ) { facing = EnumFacing.NORTH; } return getDefaultState().withProperty(FACING, facing); } @Override public int getMetaFromState(IBlockState state) { return ((EnumFacing) state.getValue(FACING)).getIndex(); } protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[]{FACING}); } }Cordialement ,
-
Dans ce code tout ce que tu fais se passe côté serveur vu qu’il y a la condition !world.isRemote (player.sendMessage envoyer un paquet au client, qui contient ton message mais le message est généré sur le serveur et donc la valeur est aussi récupéré sur le serveur)
Cela ne permet en aucun cas de valider que la synchro fonctionne.Envoies-moi tout ton code en rapport avec la synchronisation.
-
J’ai pas de code pour la syncro car je pensais que les capabilities les intégraient de bases .
J’ai bien essayé de tenter de passer par des packets mais ça fonctionne pas ou ça crash (car il y a pas de tutoriel sur le network en 1.10 …)
Donc je suis coincé …Cordialement ,
-
Le network n’a pas changé depuis la 1.7.
Envoies le rapport de crash et explique sur quoi tu bloques. -
Voici le crash , qui se produit quand on clique sur le bloc dont le nouveau code est :
[font=Courier Newpublic class ]DofusCraftCraftingTable extends Block { public static final PropertyDirection *FACING *= BlockHorizontal.*FACING*%(#cc7832)[; ] private int level%(#cc7832)[; ] public DofusCraftCraftingTable(String unlocalizedName, String registryName) { super(Material.*WOOD*)%(#cc7832)[; ] this.setDefaultState(this.blockState.getBaseState().withProperty(*FACING*, EnumFacing.*NORTH*))%(#cc7832)[; ] this.setUnlocalizedName(unlocalizedName)%(#cc7832)[; ] this.setRegistryName(new ResourceLocation(Reference.*MODID*, registryName))%(#cc7832)[; ] this.setSoundType(SoundType.*WOOD*)%(#cc7832)[; ] this.setCreativeTab(DofusCraftCore.*blocks*)%(#cc7832)[; ] } @SuppressWarnings("deprecated") public boolean isOpaqueCube(IBlockState state) { %(#cc7832)[return false; ] } @SuppressWarnings("deprecated") public boolean isFullCube(IBlockState state) { %(#cc7832)[return false; ] } public boolean isPassable(IBlockAccess worldIn, BlockPos pos) { %(#cc7832)[return true; ] } %(#bbb529)[@Override ] public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if( !world.isRemote ) { player.openGui(DofusCraftCore.*instance*, GuiHandler.*guiCraftingTableID*, world, pos.getX(), pos.getY(), pos.getZ())%(#cc7832)[; ] IDofusCraftLevels cap = player.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null)%(#cc7832)[; ] cap.setLevel(cap.getLevel() + 1)%(#cc7832)[; ] this.level = cap.getLevel()%(#cc7832)[; ] String message = String.*format*("Hello there, you have §7%d§r levels.", (int) cap.getLevel())%(#cc7832)[; ] player.sendMessage(new TextComponentString(message))%(#cc7832)[; ] } if( world.isRemote ) { DofusCraftCore.*network*.sendTo(new PacketLevel(this.level), (EntityPlayerMP) player)%(#cc7832)[; ] } %(#cc7832)[return true; ] } @SuppressWarnings("deprecated") public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState().withProperty(*FACING*, placer.getHorizontalFacing().getOpposite())%(#cc7832)[; ] } public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { worldIn.setBlockState(pos, state.withProperty(*FACING*, placer.getHorizontalFacing().getOpposite()), 2)%(#cc7832)[; ] } @SuppressWarnings("deprecated") public EnumBlockRenderType getRenderType(IBlockState state) { return EnumBlockRenderType.*MODEL*%(#cc7832)[; ] } @SuppressWarnings("deprecated") public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(*FACING*, rot.rotate((EnumFacing) state.getValue(*FACING*)))%(#cc7832)[; ] } @SuppressWarnings("deprecated") %(#bbb529)[@Override ] public IBlockState getStateFromMeta(int meta) { EnumFacing facing = EnumFacing.*getFront*(meta)%(#cc7832)[; ] if( facing.getAxis() == EnumFacing.Axis.*Y *) { facing = EnumFacing.*NORTH*%(#cc7832)[; ] } return getDefaultState().withProperty(*FACING*, facing)%(#cc7832)[; ] } %(#bbb529)[@Override ] public int getMetaFromState(IBlockState state) { return ((EnumFacing) state.getValue(*FACING*)).getIndex()%(#cc7832)[; ] } protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[]{*FACING*})%(#cc7832)[; ] }[Netty Server IO #1/ERROR] [FML]: NetworkDispatcher exception java.io.IOException: Une connexion existante a dû être fermée par l’hôte distant at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[?:1.8.0_131] at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) ~[?:1.8.0_131] at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[?:1.8.0_131] at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[?:1.8.0_131] at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[?:1.8.0_131] at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:446) ~[UnpooledUnsafeDirectByteBuf.class:4.0.23.Final] at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881) ~[AbstractByteBuf.class:4.0.23.Final] at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:225) ~[NioSocketChannel.class:4.0.23.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119) [AbstractNioByteChannel$NioByteUnsafe.class:4.0.23.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [NioEventLoop.class:4.0.23.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [NioEventLoop.class:4.0.23.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [NioEventLoop.class:4.0.23.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [NioEventLoop.class:4.0.23.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [SingleThreadEventExecutor$2.class:4.0.23.Final] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]Cordialement ,
-
Dans le cas du bloc tu n’as pas besoin de paquet.
Il te faudrait plutôt une fonction sync() de type void dans ta capability qui envoyer le paquet.
Il faudra appeler cette fonction après modification de la valeur.La ton code n’est pas bon car tu tente d’envoyer au client un paquet, alors que dans une condition world.isRemote tu es déjà le client.
-
Salut ,
Bon le crash est résolu maintenant reste l’affichage dans le tooltip …
Je n’y arrive toujours pas à afficher la bonne valeur … Cela affiche toujours celle par défaut …Le sync :
[font=Courier Newpublic static void ]sync(EntityPlayerMP player) { if(!player.world.isRemote) { DofusCraftCore.*network*.sendTo(new PacketLevel(player.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null).getLevel()), player)%(#cc7832)[; ] } }ET le tooltip :
[font=Courier New@Override ]public void addInformation(ItemStack stack, EntityPlayer playerIn, List <string>tooltip, boolean advanced) { tooltip.add("Niveau : " + playerIn.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null).getLevel())%(#cc7832)[; ]}Je progresse mais c’est pas encore totalement fonctionnel ….
Cordialement ,</string>
-
Il faut synchroniser la valeur à la connexion du joueur (tu peux utiliser l’event PlayerEvent.PlayerLoggedInEvent) et dès que la variable est modifié.
-
Salut ,
C’est déjà le cas mais ça ne change rien ….
[font=Courier New@SubscribeEvent ]public void onPlayerLogging(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) { DofusCraftLevels.*sync*((EntityPlayerMP) event.player)%(#cc7832)[; ]}Cordialement ,
-
La fonction est bien appelé ?
Le paquet est bien enregistré ? -
Salut ,
Oui c’est une des première chose que j’ai vérifié ….
Toutefois , en creusant un peu plus , j’ai trouver cette erreur qui surviens uniquement dans la console du serveur :[Server thread/ERROR] [FML]: SimpleChannelHandlerWrapper exception java.lang.RuntimeException: Missing at net.minecraftforge.fml.server.FMLServerHandler.getClientToServerNetworkManager(FMLServerHandler.java:288) ~[FMLServerHandler.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.getClientToServerNetworkManager(FMLCommonHandler.java:545) ~[FMLCommonHandler.class:?] at net.minecraftforge.fml.common.network.FMLOutboundHandler$OutboundTarget$8.selectNetworks(FMLOutboundHandler.java:245) ~[FMLOutboundHandler$OutboundTarget$8.class:?] at net.minecraftforge.fml.common.network.FMLOutboundHandler.write(FMLOutboundHandler.java:293) ~[FMLOutboundHandler.class:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:658) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:716) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:651) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:112) ~[MessageToMessageEncoder.class:4.0.23.Final] at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:658) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:716) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:706) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:741) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:895) ~[DefaultChannelPipeline.class:4.0.23.Final] at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:240) ~[AbstractChannel.class:4.0.23.Final] at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:294) [SimpleNetworkWrapper.class:?] at fr.jeremy60800.dofuscraftcore.capabilities.DofusCraftLevels.sync(DofusCraftLevels.java:25) [DofusCraftLevels.class:?] at fr.jeremy60800.dofuscraftcore.handlers.PlayerHandler.onPlayerLogging(PlayerHandler.java:29) [PlayerHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_8_PlayerHandler_onPlayerLogging_PlayerLoggedInEvent.invoke(.dynamic) [?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) [ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) [EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.firePlayerLoggedIn(FMLCommonHandler.java:565) [FMLCommonHandler.class:?] at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:238) [PlayerList.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:263) [NetworkDispatcher.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.access$100(NetworkDispatcher.java:73) [NetworkDispatcher.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:212) [NetworkDispatcher$1.class:?] at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309) [NetworkManager.class:?] at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197) [NetworkSystem.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807) [MinecraftServer.class:?] at net.minecraft.server.dedicated.DedicatedServer.updateTimeLightAndEntities(DedicatedServer.java:408) [DedicatedServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]Et voilà les lignes qui sont désignées :
DofusCraftLevels:25
[font=Courier Newpublic void ]sync(EntityPlayerMP player) { DofusCraftCore.*network*.sendTo(new PacketLevel(player.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null).getLevel()), player)%(#cc7832)[; ]}PlayerHandler:29
[font=Courier New@SubscribeEvent ]public void onPlayerLogging(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) { new DofusCraftLevels().sync((EntityPlayerMP) event.player)%(#cc7832)[; ]}Visiblement c’est la fonction sync qui fonctionne pas …
Mais je ne voit pas pourquoi ça ne fonctionne pas …Cordialement ,
-
L’erreur ne correspond pas au code.
Dans le code tu fais un sendTo alors que dans le stacktrace on voit un sendToServer (d’où l’erreur, il y a envoie de paquet du client vers le serveur alors que tu es côté serveur).Par ailleurs, il serait mieux d’avoir une fonction sync comme ceci :
public void sync(EntityPlayerMP player) { DofusCraftCore.network.sendTo(new PacketLevel(this.level), player); }Et pour l’appeler :
@SubscribeEvent public void onPlayerLogging(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) { event.player.getCapability(DofusCraftLevelsProvider.LEVEL_CAP, null)sync((EntityPlayerMP) event.player); } -
Salut ,
J’ai donc fait les changements que tu m’a dit mais l’affichage ne fonctionne toujours pas ….
On a 1 , la valeur par défaut …Cordialement ,
-
Ton paquet est bien enregistré ?
Tu peux envoyer le code du paquet ? -
Oui il est bien enregistré et il retourne la bonne valeur .
C’est uniquement le fait de devoir l’afficher qui pose problème car l’entityplayer de la fonction addinformation() c’est un entotyplayerSP et on a besoin d’un entityplayerMP ….Sinon voici le packet :
[font=Courier Newpublic class ]PacketLevel implements IMessage { IDofusCraftLevels level%(#cc7832)[; ] public PacketLevel() { } public PacketLevel(IDofusCraftLevels level) { this.level = level%(#cc7832)[; ] } %(#bbb529)[@Override ] public void fromBytes(ByteBuf buf) { if(this.getLevel() != null) { this.getLevel().setLevel(buf.readInt())%(#cc7832)[; ] } } %(#bbb529)[@Override ] public void toBytes(ByteBuf buf) { if(this.getLevel() != null) { buf.writeInt(this.getLevel().getLevel())%(#cc7832)[; ] } } public void setLevel(IDofusCraftLevels level) { this.level = level%(#cc7832)[; ] } public IDofusCraftLevels getLevel() { return this.level%(#cc7832)[; ] } public static class PacketLevelHandlerClient implements IMessageHandler <packetlevel, imessage="">{ %(#bbb529)[@Override ] public IMessage onMessage(PacketLevel message, MessageContext ctx) { IThreadListener thread = DofusCraftCore.*proxy*.getListener(ctx)%(#cc7832)[; ] final EntityPlayer player = DofusCraftCore.*proxy*.getPlayer(ctx)%(#cc7832)[; ] final IDofusCraftLevels value = message.getLevel()%(#cc7832)[; ] thread.addScheduledTask(() -> { if(player != null) { IDofusCraftLevels capabilities = player.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null)%(#cc7832)[; ] if(capabilities != null && value != null) { capabilities.setLevel(value.getLevel())%(#cc7832)[; ] } } })%(#cc7832)[; ] %(#cc7832)[ return null; ] } } public static class PacketLevelHandlerServer implements IMessageHandler <packetlevel, imessage="">{ %(#bbb529)[@Override ] public IMessage onMessage(PacketLevel message, MessageContext ctx) { IThreadListener thread = DofusCraftCore.*proxy*.getListener(ctx)%(#cc7832)[; ] final EntityPlayer player = DofusCraftCore.*proxy*.getPlayer(ctx)%(#cc7832)[; ] final IDofusCraftLevels value = message.getLevel()%(#cc7832)[; ] thread.addScheduledTask(() -> { if(player != null) { IDofusCraftLevels capabilities = player.getCapability(DofusCraftLevelsProvider.*LEVEL_CAP*, null)%(#cc7832)[; ] if(capabilities != null && value != null) { capabilities.setLevel(value.getLevel())%(#cc7832)[; ] } } })%(#cc7832)[; ]%(#cc7832)[ return null; ] } } }Cordialement ,</packetlevel%(#cc7832)[,></packetlevel%(#cc7832)[,>
-
Non tu n’as pas besoin d’un entityplayerMP pour afficher la valeur d’une capability.
Je le fais dans plusieurs de mes mods sans problème.La synchro coince quelque part, mais je ne vois pas où car ton code est correcte (du moins pour les classes que j’ai vu).
Vérifies que toutes les fonctions sont bien un appelé (avec le débogguer + point d’arrêt, il faut vérifier toute la chaine, envoie du paquet, réception par le client, modification de la valeur de la capa sur le client) -
C’est la modification de la capa côté client qui ne fonctionne pas ……
Car le packet j’ai bien le bon niveau en réponse et l’envoi fonctionne nickel …Le truc c’est que je sais pas comment fixer le problème …
Cordialement ,
-
Je vois pourquoi.
à l’arrivé du paquet IDofusCraftLevels est null.
Il faudrait lui donner une valeur ou alors le remplacer directement par un int et changer la valeur de capabilities.getLevel().level au lieu de changer l’objet level qui se trouve dans capabilities.(d’ailleurs je ne comprends pas pourquoi tu mets ta variable level à l’intérieur d’un objet level. Si tu veux une classe à part pour la question des niveaux, il serait mieux de nommer la classe LevelMananger ou quelque chose du genre, afin d’avoir nom différent pour éviter les possibles confusions).