|
1 |
| -package com.dynious.soundscool.helper; |
2 |
| - |
3 |
| -import java.io.File; |
4 |
| -import java.io.IOException; |
5 |
| -import java.util.Iterator; |
6 |
| - |
7 |
| -import net.minecraft.client.Minecraft; |
8 |
| -import net.minecraft.entity.player.EntityPlayer; |
9 |
| -import net.minecraft.entity.player.EntityPlayerMP; |
10 |
| -import net.minecraft.server.MinecraftServer; |
11 |
| - |
12 |
| -import org.apache.commons.io.FileUtils; |
13 |
| -import org.apache.commons.lang3.ArrayUtils; |
14 |
| - |
15 |
| -import com.dynious.soundscool.SoundsCool; |
16 |
| -import com.dynious.soundscool.handler.SoundHandler; |
17 |
| -import com.dynious.soundscool.network.packet.SoundChunkPacket; |
18 |
| -import com.dynious.soundscool.network.packet.SoundUploadedPacket; |
19 |
| -import com.dynious.soundscool.network.packet.client.GetUploadedSoundsPacket; |
20 |
| -import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket; |
21 |
| -import com.dynious.soundscool.sound.Sound; |
22 |
| - |
23 |
| -import cpw.mods.fml.common.network.simpleimpl.IMessage; |
24 |
| -import cpw.mods.fml.relauncher.Side; |
25 |
| -import cpw.mods.fml.relauncher.SideOnly; |
26 |
| - |
27 |
| -public class NetworkHelper |
28 |
| -{ |
29 |
| - public static final int PARTITION_SIZE = 30000; |
30 |
| - |
31 |
| - public static void sendMessageToPlayer(IMessage message, EntityPlayerMP player) |
32 |
| - { |
33 |
| - SoundsCool.network.sendTo(message, player); |
34 |
| - } |
35 |
| - |
36 |
| - public static void sendMessageToAll(IMessage message) |
37 |
| - { |
38 |
| - //sendToAll causing client disconnect in MP. Iterating over players instead until reason known |
39 |
| - Iterator playerList = MinecraftServer.getServer().getConfigurationManager().playerEntityList.iterator(); |
40 |
| - while(playerList.hasNext()) |
41 |
| - { |
42 |
| - SoundsCool.network.sendTo(message, (EntityPlayerMP)playerList.next()); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - public static void syncPlayerSounds(EntityPlayer player) |
47 |
| - { |
48 |
| - SoundsCool.network.sendToServer(new GetUploadedSoundsPacket(player)); |
49 |
| - } |
50 |
| - |
51 |
| - public static void syncAllPlayerSounds() |
52 |
| - { |
53 |
| - NetworkHelper.sendMessageToAll(new UploadedSoundsPacket()); |
54 |
| - } |
55 |
| - |
56 |
| - @SideOnly(Side.CLIENT) |
57 |
| - public static void clientSoundUpload(Sound sound) |
58 |
| - { |
59 |
| - sound.setState(Sound.SoundState.UPLOADING); |
60 |
| - uploadSound(sound, Minecraft.getMinecraft().thePlayer.getDisplayName()); |
61 |
| - } |
62 |
| - |
63 |
| - public static void serverSoundUpload(Sound sound, EntityPlayerMP player) |
64 |
| - { |
65 |
| - byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation()); |
66 |
| - for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE) |
67 |
| - { |
68 |
| - byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i)); |
69 |
| - SoundsCool.network.sendTo(new SoundChunkPacket(sound.getSoundName(), bytes), player); |
70 |
| - } |
71 |
| - SoundsCool.network.sendTo(new SoundUploadedPacket(sound.getSoundName(), MinecraftServer.getServer().getMOTD()), player); |
72 |
| - } |
73 |
| - |
74 |
| - private static void uploadSound(Sound sound, String category) |
75 |
| - { |
76 |
| - byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation()); |
77 |
| - for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE) |
78 |
| - { |
79 |
| - byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i)); |
80 |
| - SoundsCool.network.sendToServer(new SoundChunkPacket(sound.getSoundName(), bytes)); |
81 |
| - } |
82 |
| - SoundsCool.network.sendToServer(new SoundUploadedPacket(sound.getSoundName(), category)); |
83 |
| - } |
84 |
| - |
85 |
| - public static byte[] convertFileToByteArr(File file) |
86 |
| - { |
87 |
| - if (file != null && file.exists()) |
88 |
| - { |
89 |
| - try |
90 |
| - { |
91 |
| - return FileUtils.readFileToByteArray(file); |
92 |
| - } catch (IOException e) |
93 |
| - { |
94 |
| - e.printStackTrace(); |
95 |
| - } |
96 |
| - } |
97 |
| - return null; |
98 |
| - } |
99 |
| - |
100 |
| - public static File createFileFromByteArr(byte[] byteFile, String category, String fileName) |
101 |
| - { |
102 |
| - if (byteFile != null && byteFile.length > 0 && !category.isEmpty() && !fileName.isEmpty()) |
103 |
| - { |
104 |
| - File file = new File(SoundHandler.getSoundsFolder().getAbsolutePath() + File.separator + category + File.separator + fileName); |
105 |
| - try |
106 |
| - { |
107 |
| - FileUtils.writeByteArrayToFile(file, byteFile); |
108 |
| - } catch (IOException e) |
109 |
| - { |
110 |
| - e.printStackTrace(); |
111 |
| - } |
112 |
| - return file; |
113 |
| - } |
114 |
| - return null; |
115 |
| - } |
116 |
| -} |
| 1 | +package com.dynious.soundscool.helper; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import net.minecraft.client.Minecraft; |
| 7 | +import net.minecraft.entity.player.EntityPlayer; |
| 8 | +import net.minecraft.entity.player.EntityPlayerMP; |
| 9 | +import net.minecraft.server.MinecraftServer; |
| 10 | + |
| 11 | +import org.apache.commons.io.FileUtils; |
| 12 | +import org.apache.commons.lang3.ArrayUtils; |
| 13 | + |
| 14 | +import com.dynious.soundscool.SoundsCool; |
| 15 | +import com.dynious.soundscool.handler.SoundHandler; |
| 16 | +import com.dynious.soundscool.network.packet.SoundChunkPacket; |
| 17 | +import com.dynious.soundscool.network.packet.SoundUploadedPacket; |
| 18 | +import com.dynious.soundscool.network.packet.client.GetUploadedSoundsPacket; |
| 19 | +import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket; |
| 20 | +import com.dynious.soundscool.sound.Sound; |
| 21 | + |
| 22 | +import cpw.mods.fml.relauncher.Side; |
| 23 | +import cpw.mods.fml.relauncher.SideOnly; |
| 24 | + |
| 25 | +public class NetworkHelper |
| 26 | +{ |
| 27 | + public static final int PARTITION_SIZE = 30000; |
| 28 | + |
| 29 | + public static void syncPlayerSounds(EntityPlayer player) |
| 30 | + { |
| 31 | + SoundsCool.network.sendToServer(new GetUploadedSoundsPacket(player)); |
| 32 | + } |
| 33 | + |
| 34 | + public static void syncAllPlayerSounds() |
| 35 | + { |
| 36 | + SoundsCool.network.sendToAll(new UploadedSoundsPacket()); |
| 37 | + } |
| 38 | + |
| 39 | + @SideOnly(Side.CLIENT) |
| 40 | + public static void clientSoundUpload(Sound sound) |
| 41 | + { |
| 42 | + sound.setState(Sound.SoundState.UPLOADING); |
| 43 | + uploadSound(sound, Minecraft.getMinecraft().thePlayer.getDisplayName()); |
| 44 | + } |
| 45 | + |
| 46 | + public static void serverSoundUpload(Sound sound, EntityPlayerMP player) |
| 47 | + { |
| 48 | + byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation()); |
| 49 | + for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE) |
| 50 | + { |
| 51 | + byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i)); |
| 52 | + SoundsCool.network.sendTo(new SoundChunkPacket(sound.getSoundName(), bytes), player); |
| 53 | + } |
| 54 | + SoundsCool.network.sendTo(new SoundUploadedPacket(sound.getSoundName(), MinecraftServer.getServer().getMOTD()), player); |
| 55 | + } |
| 56 | + |
| 57 | + private static void uploadSound(Sound sound, String category) |
| 58 | + { |
| 59 | + byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation()); |
| 60 | + for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE) |
| 61 | + { |
| 62 | + byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i)); |
| 63 | + SoundsCool.network.sendToServer(new SoundChunkPacket(sound.getSoundName(), bytes)); |
| 64 | + } |
| 65 | + SoundsCool.network.sendToServer(new SoundUploadedPacket(sound.getSoundName(), category)); |
| 66 | + } |
| 67 | + |
| 68 | + public static byte[] convertFileToByteArr(File file) |
| 69 | + { |
| 70 | + if (file != null && file.exists()) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + return FileUtils.readFileToByteArray(file); |
| 75 | + } catch (IOException e) |
| 76 | + { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + public static File createFileFromByteArr(byte[] byteFile, String category, String fileName) |
| 84 | + { |
| 85 | + if (byteFile != null && byteFile.length > 0 && !category.isEmpty() && !fileName.isEmpty()) |
| 86 | + { |
| 87 | + File file = new File(SoundHandler.getSoundsFolder().getAbsolutePath() + File.separator + category + File.separator + fileName); |
| 88 | + try |
| 89 | + { |
| 90 | + FileUtils.writeByteArrayToFile(file, byteFile); |
| 91 | + } catch (IOException e) |
| 92 | + { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + return file; |
| 96 | + } |
| 97 | + return null; |
| 98 | + } |
| 99 | +} |
0 commit comments