Skip to content

Commit

Permalink
Reverting back to original sendTo methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
zoonie committed Jan 10, 2015
1 parent 2a7964c commit 10a37c9
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 476 deletions.
4 changes: 2 additions & 2 deletions java/com/dynious/soundscool/command/CommandSoundsCool.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;

import com.dynious.soundscool.helper.NetworkHelper;
import com.dynious.soundscool.SoundsCool;
import com.dynious.soundscool.lib.Commands;
import com.dynious.soundscool.network.packet.server.OpenGUIPacket;

Expand Down Expand Up @@ -38,7 +38,7 @@ public void processCommand(ICommandSender commandSender, String[] args)
{
if (commandSender instanceof EntityPlayer)
{
NetworkHelper.sendMessageToPlayer(new OpenGUIPacket(0), (EntityPlayerMP) commandSender);
SoundsCool.network.sendTo(new OpenGUIPacket(0), (EntityPlayerMP) commandSender);
}
}

Expand Down
2 changes: 1 addition & 1 deletion java/com/dynious/soundscool/handler/SoundHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void removeSound(Sound sound)
sounds.remove(sound);
if (FMLCommonHandler.instance().getEffectiveSide().isServer())
{
NetworkHelper.sendMessageToAll(new SoundRemovedPacket(sound.getSoundName()));
SoundsCool.network.sendToAll(new SoundRemovedPacket(sound.getSoundName()));
}
}
}
Expand Down
215 changes: 99 additions & 116 deletions java/com/dynious/soundscool/helper/NetworkHelper.java
Original file line number Diff line number Diff line change
@@ -1,116 +1,99 @@
package com.dynious.soundscool.helper;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.dynious.soundscool.SoundsCool;
import com.dynious.soundscool.handler.SoundHandler;
import com.dynious.soundscool.network.packet.SoundChunkPacket;
import com.dynious.soundscool.network.packet.SoundUploadedPacket;
import com.dynious.soundscool.network.packet.client.GetUploadedSoundsPacket;
import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket;
import com.dynious.soundscool.sound.Sound;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class NetworkHelper
{
public static final int PARTITION_SIZE = 30000;

public static void sendMessageToPlayer(IMessage message, EntityPlayerMP player)
{
SoundsCool.network.sendTo(message, player);
}

public static void sendMessageToAll(IMessage message)
{
//sendToAll causing client disconnect in MP. Iterating over players instead until reason known
Iterator playerList = MinecraftServer.getServer().getConfigurationManager().playerEntityList.iterator();
while(playerList.hasNext())
{
SoundsCool.network.sendTo(message, (EntityPlayerMP)playerList.next());
}
}

public static void syncPlayerSounds(EntityPlayer player)
{
SoundsCool.network.sendToServer(new GetUploadedSoundsPacket(player));
}

public static void syncAllPlayerSounds()
{
NetworkHelper.sendMessageToAll(new UploadedSoundsPacket());
}

@SideOnly(Side.CLIENT)
public static void clientSoundUpload(Sound sound)
{
sound.setState(Sound.SoundState.UPLOADING);
uploadSound(sound, Minecraft.getMinecraft().thePlayer.getDisplayName());
}

public static void serverSoundUpload(Sound sound, EntityPlayerMP player)
{
byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation());
for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE)
{
byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i));
SoundsCool.network.sendTo(new SoundChunkPacket(sound.getSoundName(), bytes), player);
}
SoundsCool.network.sendTo(new SoundUploadedPacket(sound.getSoundName(), MinecraftServer.getServer().getMOTD()), player);
}

private static void uploadSound(Sound sound, String category)
{
byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation());
for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE)
{
byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i));
SoundsCool.network.sendToServer(new SoundChunkPacket(sound.getSoundName(), bytes));
}
SoundsCool.network.sendToServer(new SoundUploadedPacket(sound.getSoundName(), category));
}

public static byte[] convertFileToByteArr(File file)
{
if (file != null && file.exists())
{
try
{
return FileUtils.readFileToByteArray(file);
} catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}

public static File createFileFromByteArr(byte[] byteFile, String category, String fileName)
{
if (byteFile != null && byteFile.length > 0 && !category.isEmpty() && !fileName.isEmpty())
{
File file = new File(SoundHandler.getSoundsFolder().getAbsolutePath() + File.separator + category + File.separator + fileName);
try
{
FileUtils.writeByteArrayToFile(file, byteFile);
} catch (IOException e)
{
e.printStackTrace();
}
return file;
}
return null;
}
}
package com.dynious.soundscool.helper;

import java.io.File;
import java.io.IOException;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.dynious.soundscool.SoundsCool;
import com.dynious.soundscool.handler.SoundHandler;
import com.dynious.soundscool.network.packet.SoundChunkPacket;
import com.dynious.soundscool.network.packet.SoundUploadedPacket;
import com.dynious.soundscool.network.packet.client.GetUploadedSoundsPacket;
import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket;
import com.dynious.soundscool.sound.Sound;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class NetworkHelper
{
public static final int PARTITION_SIZE = 30000;

public static void syncPlayerSounds(EntityPlayer player)
{
SoundsCool.network.sendToServer(new GetUploadedSoundsPacket(player));
}

public static void syncAllPlayerSounds()
{
SoundsCool.network.sendToAll(new UploadedSoundsPacket());
}

@SideOnly(Side.CLIENT)
public static void clientSoundUpload(Sound sound)
{
sound.setState(Sound.SoundState.UPLOADING);
uploadSound(sound, Minecraft.getMinecraft().thePlayer.getDisplayName());
}

public static void serverSoundUpload(Sound sound, EntityPlayerMP player)
{
byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation());
for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE)
{
byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i));
SoundsCool.network.sendTo(new SoundChunkPacket(sound.getSoundName(), bytes), player);
}
SoundsCool.network.sendTo(new SoundUploadedPacket(sound.getSoundName(), MinecraftServer.getServer().getMOTD()), player);
}

private static void uploadSound(Sound sound, String category)
{
byte[] soundBytes = convertFileToByteArr(sound.getSoundLocation());
for (int i = 0; i < soundBytes.length; i += PARTITION_SIZE)
{
byte[] bytes = ArrayUtils.subarray(soundBytes, i, i + Math.min(PARTITION_SIZE, soundBytes.length - i));
SoundsCool.network.sendToServer(new SoundChunkPacket(sound.getSoundName(), bytes));
}
SoundsCool.network.sendToServer(new SoundUploadedPacket(sound.getSoundName(), category));
}

public static byte[] convertFileToByteArr(File file)
{
if (file != null && file.exists())
{
try
{
return FileUtils.readFileToByteArray(file);
} catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}

public static File createFileFromByteArr(byte[] byteFile, String category, String fileName)
{
if (byteFile != null && byteFile.length > 0 && !category.isEmpty() && !fileName.isEmpty())
{
File file = new File(SoundHandler.getSoundsFolder().getAbsolutePath() + File.separator + category + File.separator + fileName);
try
{
FileUtils.writeByteArrayToFile(file, byteFile);
} catch (IOException e)
{
e.printStackTrace();
}
return file;
}
return null;
}
}
Loading

0 comments on commit 10a37c9

Please sign in to comment.