J’ai moi même recrée les deux méthodes (writeString() et readString()) et j’obtiens la même erreur.
En examinant les bytes avec des System.out.println(), j’ai pu voir que les coordonnées n’étaient pas présentes et que ma méthode donnait la longueur à partir de 0 et non de 1, ce qui provoquait une erreur dans ma méthode. J’ai corrigé le problème. Voici mes deux méthodes (pleinement opérationnelles), pour ceux qui auraient le même problème :
| |
| |
| |
| |
| |
| |
| public static void writeString(DataOutputStream stream, String str) throws IOException{ |
| char[] chrs = str.toCharArray(); |
| stream.writeShort(chrs.length - 1); |
| for(char i : chrs){ |
| stream.writeChar(i); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static String readString(DataInputStream stream) throws IOException{ |
| short lenght = stream.readShort(); |
| String str = ""; |
| for(short i = 0; i<=lenght; i++){ |
| str += stream.readChar(); |
| } |
| return str; |
| } |
Bref maintenant je n’ai plus d’erreurs, sauf que les valeurs de mes boutons ne s’initialisent pas correctement, (en fait elles ne s’initialisent pas du tout), comme si mon DataInputStream n’avait aucun effet… T_T
GuiSoundBlock.java
| |
| @SideOnly(Side.CLIENT) |
| public class GuiSoundBlock extends GuiContainer{ |
| |
| public static ResourceLocation texture = new ResourceLocation("wsmod", "gui/soundBlock.png"); |
| |
| private ArrayList <string>soundList = new ArrayList<string>(); |
| private TileEntitySoundBlock soundBlock; |
| private int volume = 50, range = 50, delay = 50, x, y; |
| private GuiTextField soundChooser; |
| private String sound = "wsmod:alarm-default"; |
| |
| private Logger logger; |
| |
| public GuiSoundBlock(InventoryPlayer inventory, TileEntitySoundBlock tile){ |
| super(new ContainerSoundBlock(inventory, tile)); |
| this.logger = WirestoneModLogger.getLogger(); |
| this.soundBlock = tile; |
| this.xSize = 255; |
| this.ySize = 255; |
| this.soundList = Registry.soundList; |
| this.volume = (int)tile.getVolume() * 100; |
| } |
| |
| @Override |
| public void initGui(){ |
| super.initGui(); |
| |
| this.x = (this.width) / 2; |
| this.y = (this.height - this.ySize) / 2; |
| this.buttonList.clear(); |
| |
| this.buttonList.add(new GuiButton(1, x + 60, y + 40, 20, 20, "+")); |
| this.buttonList.add(new GuiButton(2, x + 80, y + 40, 20, 20, "++")); |
| this.buttonList.add(new GuiButton(3, x - 80, y + 40, 20, 20, "-")); |
| this.buttonList.add(new GuiButton(4, x - 100, y + 40, 20, 20, "--")); |
| |
| |
| this.buttonList.add(new GuiButton(5, x + 60, y + 80, 20, 20, "+")); |
| this.buttonList.add(new GuiButton(6, x + 80, y + 80, 20, 20, "++")); |
| this.buttonList.add(new GuiButton(7, x - 80, y + 80, 20, 20, "-")); |
| this.buttonList.add(new GuiButton(8, x - 100, y + 80, 20, 20, "--")); |
| |
| |
| this.buttonList.add(new GuiButton(9, x + 60, y + 120, 20, 20, "+")); |
| this.buttonList.add(new GuiButton(10, x + 80, y + 120, 20, 20, "++")); |
| this.buttonList.add(new GuiButton(11, x - 80, y + 120, 20, 20, "-")); |
| this.buttonList.add(new GuiButton(12, x - 100, y + 120, 20, 20, "--")); |
| |
| |
| this.soundChooser = new GuiTextField(this.fontRenderer, 8, 170, 170, 20); |
| this.soundChooser.setMaxStringLength(100); |
| this.soundChooser.setFocused(true); |
| this.soundChooser.setCanLoseFocus(false); |
| this.buttonList.add(new GuiButton(13, x + 80, y + 160, 20, 20, "Ok")); |
| this.buttonList.add(new GuiButton(14, x + 40, y + 200, 100, 20, "Test")); |
| |
| |
| this.volume = (int)this.soundBlock.getVolume() * 100; |
| this.range = this.soundBlock.getRange(); |
| this.delay = this.soundBlock.getDelay(); |
| this.sound = this.soundBlock.getSound(); |
| this.soundChooser.setText(this.soundBlock.getSound().replace("wsmod:","")); |
| } |
| |
| @Override |
| protected void actionPerformed(GuiButton guiButton){ |
| String sound = "wsmod:" + this.soundChooser.getText(); |
| switch(guiButton.id){ |
| case 1: |
| if(this.volume < 100){ |
| this.volume++; |
| } |
| break; |
| case 2: |
| if(this.volume + 10 < 100){ |
| this.volume += 10; |
| } |
| else{ |
| this.volume = 100; |
| } |
| break; |
| case 3: |
| if(this.volume > 0){ |
| this.volume--; |
| } |
| break; |
| case 4: |
| if(this.volume - 10 > 0){ |
| this.volume -= 10; |
| } |
| else{ |
| this.volume = 0; |
| } |
| break; |
| case 5: |
| if(this.range < 100){ |
| this.range++; |
| } |
| break; |
| case 6: |
| if(this.range + 10 < 100){ |
| this.range += 10; |
| } |
| else{ |
| this.range = 100; |
| } |
| break; |
| case 7: |
| if(this.range > 0){ |
| this.range--; |
| } |
| break; |
| case 8: |
| if(this.range - 10 > 0){ |
| this.range -= 10; |
| } |
| else{ |
| this.range = 0; |
| } |
| break; |
| case 9: |
| if(this.delay < 100){ |
| this.delay++; |
| } |
| break; |
| case 10: |
| if(this.delay + 10 < 100){ |
| this.delay += 10; |
| } |
| else{ |
| this.delay = 100; |
| } |
| break; |
| case 11: |
| if(this.delay > 0){ |
| this.delay--; |
| } |
| break; |
| case 12: |
| if(this.delay - 10 > 0){ |
| this.delay -= 10; |
| } |
| else{ |
| this.delay = 0; |
| } |
| break; |
| case 13: |
| if(this.soundList.contains(sound)){ |
| this.sound = "" + sound; |
| } |
| else{ |
| this.soundChooser.setText(this.sound.replace("wsmod:","")); |
| } |
| break; |
| case 14: |
| this.soundBlock.worldObj.playSoundEffect(this.soundBlock.xCoord,this.soundBlock.yCoord,this.soundBlock.zCoord,this.sound,(float)this.volume/100,1.0F); |
| break; |
| } |
| for(int i = 1; i<=5; i++){ |
| ByteArrayOutputStream bos = null; |
| DataOutputStream dos = null; |
| try{ |
| bos = new ByteArrayOutputStream(); |
| dos = new DataOutputStream(bos); |
| dos.writeInt(this.soundBlock.xCoord); |
| dos.writeInt(this.soundBlock.yCoord); |
| dos.writeInt(this.soundBlock.zCoord); |
| PacketHandler.writeString(dos,this.sound); |
| dos.writeFloat((float)this.volume/100); |
| dos.writeInt(this.delay); |
| dos.writeInt(this.range); |
| this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload("wsmod|soundBlock",bos.toByteArray())); |
| break; |
| }catch(Exception e){ |
| this.logger.warning("Erreur lors de l'envoi du packet de données du SoundBlock (essai " + i + "/5)"); |
| } |
| } |
| } |
| |
| |
| |
| |
| @Override |
| public void updateScreen(){ |
| if (!this.mc.thePlayer.isEntityAlive() || this.mc.thePlayer.isDead){ |
| this.mc.thePlayer.closeScreen(); |
| } |
| this.soundChooser.updateCursorCounter(); |
| } |
| |
| @Override |
| public void keyTyped(char par1, int par2){ |
| super.keyTyped(par1,par2); |
| this.soundChooser.textboxKeyTyped(par1,par2); |
| } |
| |
| @Override |
| public void mouseClicked(int i, int j, int k){ |
| super.mouseClicked(i, j, k); |
| this.soundChooser.mouseClicked(i,j,k); |
| } |
| |
| @Override |
| protected void drawGuiContainerForegroundLayer(int par1, int par2){ |
| LanguageRegistry r = LanguageRegistry.instance(); |
| this.fontRenderer.drawString(r.getStringLocalization("container.soundblock"),8,7,0); |
| |
| this.drawCenteredString("Volume",125,30,0,false); |
| this.drawCenteredString(this.volume + "%",125,45,0,false); |
| |
| this.drawCenteredString(r.getStringLocalization("container.soundblock.range"),125,70,0,false); |
| this.drawCenteredString(this.range + " blocks",125,85,0,false); |
| |
| this.drawCenteredString(r.getStringLocalization("container.soundblock.delay"),125,110,0,false); |
| this.drawCenteredString(this.delay + " ticks",125,125,0,false); |
| |
| this.drawCenteredString(r.getStringLocalization("container.soundblock.sound"),125,150,0,false); |
| this.soundChooser.drawTextBox(); |
| |
| this.drawCenteredString(r.getStringLocalization("container.soundblock.sounds"),320,0,-6250336,true); |
| int j = 0; |
| for(String i : this.soundList){ |
| this.drawCenteredString("- " + i.replace("wsmod:",""),320,10 + j,-6250336,true); |
| j += 10; |
| } |
| } |
| |
| @Override |
| protected void drawGuiContainerBackgroundLayer(float f, int i, int j){ |
| GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); |
| this.mc.getTextureManager().bindTexture(texture); |
| int x = (this.width - this.xSize) / 2; |
| int y = (this.height - this.ySize) / 2; |
| this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize); |
| } |
| |
| private void drawCenteredString(String par2Str, int par3, int par4, int par5, boolean shadow){ |
| if(shadow){ |
| this.fontRenderer.drawStringWithShadow(par2Str, par3 - this.fontRenderer.getStringWidth(par2Str) / 2, par4, par5); |
| } |
| else{ |
| this.fontRenderer.drawString(par2Str, par3 - this.fontRenderer.getStringWidth(par2Str) / 2, par4, par5); |
| } |
| } |
| } |
Merci d’avance :)</string></string>