diff --git a/java/com/dynious/soundscool/command/CommandSoundsCool.java b/java/com/dynious/soundscool/command/CommandSoundsCool.java index 0e8b5a7..85e82d6 100644 --- a/java/com/dynious/soundscool/command/CommandSoundsCool.java +++ b/java/com/dynious/soundscool/command/CommandSoundsCool.java @@ -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; @@ -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); } } diff --git a/java/com/dynious/soundscool/handler/SoundHandler.java b/java/com/dynious/soundscool/handler/SoundHandler.java index 426e1ff..1404175 100644 --- a/java/com/dynious/soundscool/handler/SoundHandler.java +++ b/java/com/dynious/soundscool/handler/SoundHandler.java @@ -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())); } } } diff --git a/java/com/dynious/soundscool/helper/NetworkHelper.java b/java/com/dynious/soundscool/helper/NetworkHelper.java index 114a0e8..1643bc0 100644 --- a/java/com/dynious/soundscool/helper/NetworkHelper.java +++ b/java/com/dynious/soundscool/helper/NetworkHelper.java @@ -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; + } +} diff --git a/java/com/dynious/soundscool/network/packet/SoundUploadedPacket.java b/java/com/dynious/soundscool/network/packet/SoundUploadedPacket.java index d57d4b1..e6f3991 100644 --- a/java/com/dynious/soundscool/network/packet/SoundUploadedPacket.java +++ b/java/com/dynious/soundscool/network/packet/SoundUploadedPacket.java @@ -1,96 +1,97 @@ -package com.dynious.soundscool.network.packet; - -import io.netty.buffer.ByteBuf; - -import java.io.File; - -import net.minecraft.client.Minecraft; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -import com.dynious.soundscool.handler.DelayedPlayHandler; -import com.dynious.soundscool.handler.NetworkHandler; -import com.dynious.soundscool.handler.SoundHandler; -import com.dynious.soundscool.helper.NetworkHelper; -import com.dynious.soundscool.network.packet.server.SoundReceivedPacket; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.network.simpleimpl.IMessage; -import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; -import cpw.mods.fml.common.network.simpleimpl.MessageContext; - -public class SoundUploadedPacket implements IMessage -{ - String category; - String soundName; - public SoundUploadedPacket() - { - } - - public SoundUploadedPacket(String soundName, String category) - { - this.category = category; - this.soundName = soundName; - } - - @Override - public void fromBytes(ByteBuf bytes) - { - int catLength = bytes.readInt(); - char[] catCars = new char[catLength]; - for (int i = 0; i < catLength; i++) - { - catCars[i] = bytes.readChar(); - } - category = String.valueOf(catCars); - if (FMLCommonHandler.instance().getEffectiveSide().isClient() && (category.equalsIgnoreCase("null") || category.isEmpty())) - { - category = Minecraft.getMinecraft().func_147104_D().serverName; - } - - int fileLength = bytes.readInt(); - char[] fileCars = new char[fileLength]; - for (int i = 0; i < fileLength; i++) - { - fileCars[i] = bytes.readChar(); - } - soundName = String.valueOf(fileCars); - - File soundFile = NetworkHelper.createFileFromByteArr(NetworkHandler.soundUploaded(soundName), category, soundName); - SoundHandler.addLocalSound(soundName, soundFile); - if (FMLCommonHandler.instance().getEffectiveSide().isClient()) - { - DelayedPlayHandler.onSoundReceived(soundName); - } - else - { - EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(category); - if (player != null) - { - NetworkHelper.sendMessageToPlayer(new SoundReceivedPacket(SoundHandler.getSound(soundName)), player); - } - } - } - - @Override - public void toBytes(ByteBuf bytes) - { - bytes.writeInt(category.length()); - for (char c : category.toCharArray()) - { - bytes.writeChar(c); - } - bytes.writeInt(soundName.length()); - for (char c : soundName.toCharArray()) - { - bytes.writeChar(c); - } - } - - public static class Handler implements IMessageHandler { - @Override - public IMessage onMessage(SoundUploadedPacket message, MessageContext ctx) { - return null; - } - } -} +package com.dynious.soundscool.network.packet; + +import io.netty.buffer.ByteBuf; + +import java.io.File; + +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; + +import com.dynious.soundscool.SoundsCool; +import com.dynious.soundscool.handler.DelayedPlayHandler; +import com.dynious.soundscool.handler.NetworkHandler; +import com.dynious.soundscool.handler.SoundHandler; +import com.dynious.soundscool.helper.NetworkHelper; +import com.dynious.soundscool.network.packet.server.SoundReceivedPacket; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +public class SoundUploadedPacket implements IMessage +{ + String category; + String soundName; + public SoundUploadedPacket() + { + } + + public SoundUploadedPacket(String soundName, String category) + { + this.category = category; + this.soundName = soundName; + } + + @Override + public void fromBytes(ByteBuf bytes) + { + int catLength = bytes.readInt(); + char[] catCars = new char[catLength]; + for (int i = 0; i < catLength; i++) + { + catCars[i] = bytes.readChar(); + } + category = String.valueOf(catCars); + if (FMLCommonHandler.instance().getEffectiveSide().isClient() && (category.equalsIgnoreCase("null") || category.isEmpty())) + { + category = Minecraft.getMinecraft().func_147104_D().serverName; + } + + int fileLength = bytes.readInt(); + char[] fileCars = new char[fileLength]; + for (int i = 0; i < fileLength; i++) + { + fileCars[i] = bytes.readChar(); + } + soundName = String.valueOf(fileCars); + + File soundFile = NetworkHelper.createFileFromByteArr(NetworkHandler.soundUploaded(soundName), category, soundName); + SoundHandler.addLocalSound(soundName, soundFile); + if (FMLCommonHandler.instance().getEffectiveSide().isClient()) + { + DelayedPlayHandler.onSoundReceived(soundName); + } + else + { + EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(category); + if (player != null) + { + SoundsCool.network.sendTo(new SoundReceivedPacket(SoundHandler.getSound(soundName)), player); + } + } + } + + @Override + public void toBytes(ByteBuf bytes) + { + bytes.writeInt(category.length()); + for (char c : category.toCharArray()) + { + bytes.writeChar(c); + } + bytes.writeInt(soundName.length()); + for (char c : soundName.toCharArray()) + { + bytes.writeChar(c); + } + } + + public static class Handler implements IMessageHandler { + @Override + public IMessage onMessage(SoundUploadedPacket message, MessageContext ctx) { + return null; + } + } +} diff --git a/java/com/dynious/soundscool/network/packet/client/CheckPresencePacket.java b/java/com/dynious/soundscool/network/packet/client/CheckPresencePacket.java index 4fe82f6..644d270 100644 --- a/java/com/dynious/soundscool/network/packet/client/CheckPresencePacket.java +++ b/java/com/dynious/soundscool/network/packet/client/CheckPresencePacket.java @@ -1,82 +1,83 @@ -package com.dynious.soundscool.network.packet.client; - -import io.netty.buffer.ByteBuf; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraftforge.common.DimensionManager; - -import com.dynious.soundscool.handler.SoundHandler; -import com.dynious.soundscool.helper.NetworkHelper; -import com.dynious.soundscool.network.packet.server.SoundNotFoundPacket; -import com.dynious.soundscool.sound.Sound; - -import cpw.mods.fml.common.network.simpleimpl.IMessage; -import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; -import cpw.mods.fml.common.network.simpleimpl.MessageContext; - -public class CheckPresencePacket implements IMessage -{ - String fileName; - int entityID; - int worldID; - - public CheckPresencePacket() - { - } - - public CheckPresencePacket(String soundName, EntityPlayer player) - { - this.fileName = soundName; - this.entityID = player.getEntityId(); - this.worldID = player.getEntityWorld().provider.dimensionId; - } - - @Override - public void fromBytes(ByteBuf bytes) - { - int fileLength = bytes.readInt(); - char[] fileCars = new char[fileLength]; - for (int i = 0; i < fileLength; i++) - { - fileCars[i] = bytes.readChar(); - } - fileName = String.valueOf(fileCars); - entityID = bytes.readInt(); - worldID = bytes.readInt(); - - Entity entity = DimensionManager.getWorld(worldID).getEntityByID(entityID); - if (entity != null && entity instanceof EntityPlayer) - { - Sound sound = SoundHandler.getSound(fileName); - - if (sound != null) - { - NetworkHelper.serverSoundUpload(sound, (EntityPlayerMP) entity); - } - else - { - NetworkHelper.sendMessageToPlayer(new SoundNotFoundPacket(fileName), (EntityPlayerMP)entity); - } - } - } - - @Override - public void toBytes(ByteBuf bytes) - { - bytes.writeInt(fileName.length()); - for (char c : fileName.toCharArray()) - { - bytes.writeChar(c); - } - bytes.writeInt(entityID); - bytes.writeInt(worldID); - } - - public static class Handler implements IMessageHandler { - @Override - public IMessage onMessage(CheckPresencePacket message, MessageContext ctx) { - return null; - } - } -} +package com.dynious.soundscool.network.packet.client; + +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.common.DimensionManager; + +import com.dynious.soundscool.SoundsCool; +import com.dynious.soundscool.handler.SoundHandler; +import com.dynious.soundscool.helper.NetworkHelper; +import com.dynious.soundscool.network.packet.server.SoundNotFoundPacket; +import com.dynious.soundscool.sound.Sound; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +public class CheckPresencePacket implements IMessage +{ + String fileName; + int entityID; + int worldID; + + public CheckPresencePacket() + { + } + + public CheckPresencePacket(String soundName, EntityPlayer player) + { + this.fileName = soundName; + this.entityID = player.getEntityId(); + this.worldID = player.getEntityWorld().provider.dimensionId; + } + + @Override + public void fromBytes(ByteBuf bytes) + { + int fileLength = bytes.readInt(); + char[] fileCars = new char[fileLength]; + for (int i = 0; i < fileLength; i++) + { + fileCars[i] = bytes.readChar(); + } + fileName = String.valueOf(fileCars); + entityID = bytes.readInt(); + worldID = bytes.readInt(); + + Entity entity = DimensionManager.getWorld(worldID).getEntityByID(entityID); + if (entity != null && entity instanceof EntityPlayer) + { + Sound sound = SoundHandler.getSound(fileName); + + if (sound != null) + { + NetworkHelper.serverSoundUpload(sound, (EntityPlayerMP) entity); + } + else + { + SoundsCool.network.sendTo(new SoundNotFoundPacket(fileName), (EntityPlayerMP)entity); + } + } + } + + @Override + public void toBytes(ByteBuf bytes) + { + bytes.writeInt(fileName.length()); + for (char c : fileName.toCharArray()) + { + bytes.writeChar(c); + } + bytes.writeInt(entityID); + bytes.writeInt(worldID); + } + + public static class Handler implements IMessageHandler { + @Override + public IMessage onMessage(CheckPresencePacket message, MessageContext ctx) { + return null; + } + } +} diff --git a/java/com/dynious/soundscool/network/packet/client/GetUploadedSoundsPacket.java b/java/com/dynious/soundscool/network/packet/client/GetUploadedSoundsPacket.java index 67641d7..6238683 100644 --- a/java/com/dynious/soundscool/network/packet/client/GetUploadedSoundsPacket.java +++ b/java/com/dynious/soundscool/network/packet/client/GetUploadedSoundsPacket.java @@ -1,56 +1,57 @@ -package com.dynious.soundscool.network.packet.client; - -import io.netty.buffer.ByteBuf; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraftforge.common.DimensionManager; - -import com.dynious.soundscool.helper.NetworkHelper; -import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket; - -import cpw.mods.fml.common.network.simpleimpl.IMessage; -import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; -import cpw.mods.fml.common.network.simpleimpl.MessageContext; - -public class GetUploadedSoundsPacket implements IMessage -{ - int entityID; - int worldID; - - public GetUploadedSoundsPacket() - { - } - - public GetUploadedSoundsPacket(EntityPlayer player) - { - this.entityID = player.getEntityId(); - this.worldID = player.getEntityWorld().provider.dimensionId; - } - - @Override - public void fromBytes(ByteBuf bytes) - { - entityID = bytes.readInt(); - worldID = bytes.readInt(); - - Entity entity = DimensionManager.getWorld(worldID).getEntityByID(entityID); - if (entity != null && entity instanceof EntityPlayer) - { - NetworkHelper.sendMessageToAll(new UploadedSoundsPacket()); - } - } - - @Override - public void toBytes(ByteBuf bytes) - { - bytes.writeInt(entityID); - bytes.writeInt(worldID); - } - - public static class Handler implements IMessageHandler { - @Override - public IMessage onMessage(GetUploadedSoundsPacket message, MessageContext ctx) { - return null; - } - } -} +package com.dynious.soundscool.network.packet.client; + +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.common.DimensionManager; + +import com.dynious.soundscool.SoundsCool; +import com.dynious.soundscool.network.packet.server.UploadedSoundsPacket; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +public class GetUploadedSoundsPacket implements IMessage +{ + int entityID; + int worldID; + + public GetUploadedSoundsPacket() + { + } + + public GetUploadedSoundsPacket(EntityPlayer player) + { + this.entityID = player.getEntityId(); + this.worldID = player.getEntityWorld().provider.dimensionId; + } + + @Override + public void fromBytes(ByteBuf bytes) + { + entityID = bytes.readInt(); + worldID = bytes.readInt(); + + Entity entity = DimensionManager.getWorld(worldID).getEntityByID(entityID); + if (entity != null && entity instanceof EntityPlayer) + { + SoundsCool.network.sendTo(new UploadedSoundsPacket(), (EntityPlayerMP) entity); + } + } + + @Override + public void toBytes(ByteBuf bytes) + { + bytes.writeInt(entityID); + bytes.writeInt(worldID); + } + + public static class Handler implements IMessageHandler { + @Override + public IMessage onMessage(GetUploadedSoundsPacket message, MessageContext ctx) { + return null; + } + } +} diff --git a/java/com/dynious/soundscool/tileentity/TileSoundPlayer.java b/java/com/dynious/soundscool/tileentity/TileSoundPlayer.java index 7638baa..1136d80 100644 --- a/java/com/dynious/soundscool/tileentity/TileSoundPlayer.java +++ b/java/com/dynious/soundscool/tileentity/TileSoundPlayer.java @@ -1,123 +1,128 @@ -package com.dynious.soundscool.tileentity; - -import java.util.UUID; - -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.NetworkManager; -import net.minecraft.network.Packet; -import net.minecraft.network.play.server.S35PacketUpdateTileEntity; -import net.minecraft.tileentity.TileEntity; - -import com.dynious.soundscool.SoundsCool; -import com.dynious.soundscool.handler.SoundHandler; -import com.dynious.soundscool.helper.NetworkHelper; -import com.dynious.soundscool.helper.SoundHelper; -import com.dynious.soundscool.network.packet.client.SoundPlayerSelectPacket; -import com.dynious.soundscool.network.packet.server.ServerPlaySoundPacket; -import com.dynious.soundscool.network.packet.server.StopSoundPacket; -import com.dynious.soundscool.sound.Sound; - -public class TileSoundPlayer extends TileEntity -{ - private boolean isPowered = false; - private Sound selectedSound; - private String lastSoundIdentifier; - private long timeSoundFinishedPlaying; - - public void setPowered(boolean powered) - { - if (!isPowered && powered) - { - playCurrentSound(); - isPowered = true; - } - else if (isPowered && !powered) - { - isPowered = false; - } - } - - public void selectSound(String soundName) - { - this.selectedSound = SoundHandler.getSound(soundName); - - if (this.getWorldObj().isRemote) - { - SoundsCool.network.sendToServer(new SoundPlayerSelectPacket(this)); - } - } - - public Sound getSelectedSound() - { - return selectedSound; - } - - public void playCurrentSound() - { - NetworkHelper.syncAllPlayerSounds(); - if (selectedSound != null) - { - if (timeSoundFinishedPlaying < System.currentTimeMillis()) - { - if (SoundHandler.getSound(selectedSound.getSoundName()) != null) - { - lastSoundIdentifier = UUID.randomUUID().toString(); - timeSoundFinishedPlaying = (long)(SoundHelper.getSoundLength(selectedSound.getSoundLocation())*1000) + System.currentTimeMillis(); - NetworkHelper.sendMessageToAll(new ServerPlaySoundPacket(selectedSound.getSoundName(), lastSoundIdentifier, xCoord, yCoord, zCoord)); - } - else - { - selectedSound = null; - } - } - else - { - stopCurrentSound(); - } - } - } - - public void stopCurrentSound() - { - if (System.currentTimeMillis() < timeSoundFinishedPlaying) - { - NetworkHelper.sendMessageToAll(new StopSoundPacket(lastSoundIdentifier)); - timeSoundFinishedPlaying = 0; - } - } - - @Override - public void readFromNBT(NBTTagCompound compound) - { - super.readFromNBT(compound); - selectedSound = SoundHandler.getSound(compound.getString("selectedSound")); - } - - @Override - public void writeToNBT(NBTTagCompound compound) - { - super.writeToNBT(compound); - if (selectedSound != null) - { - compound.setString("selectedSound", selectedSound.getSoundName()); - } - } - - @Override - public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) - { - String soundName = pkt.func_148857_g().getString("selected"); - this.selectedSound = SoundHandler.getSound(soundName); - } - - @Override - public Packet getDescriptionPacket() - { - NBTTagCompound compound = new NBTTagCompound(); - if (selectedSound != null) - { - compound.setString("selectedSound", selectedSound.getSoundName()); - } - return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, compound); - } -} +package com.dynious.soundscool.tileentity; + +import java.util.UUID; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +import net.minecraft.network.play.server.S35PacketUpdateTileEntity; +import net.minecraft.tileentity.TileEntity; + +import com.dynious.soundscool.SoundsCool; +import com.dynious.soundscool.handler.SoundHandler; +import com.dynious.soundscool.helper.NetworkHelper; +import com.dynious.soundscool.helper.SoundHelper; +import com.dynious.soundscool.network.packet.client.SoundPlayerSelectPacket; +import com.dynious.soundscool.network.packet.server.ServerPlaySoundPacket; +import com.dynious.soundscool.network.packet.server.StopSoundPacket; +import com.dynious.soundscool.sound.Sound; + +import cpw.mods.fml.common.network.NetworkRegistry; + +public class TileSoundPlayer extends TileEntity +{ + private boolean isPowered = false; + private Sound selectedSound; + private String lastSoundIdentifier; + private long timeSoundFinishedPlaying; + + public void setPowered(boolean powered) + { + if (!isPowered && powered) + { + playCurrentSound(); + isPowered = true; + } + else if (isPowered && !powered) + { + isPowered = false; + } + } + + public void selectSound(String soundName) + { + this.selectedSound = SoundHandler.getSound(soundName); + + if (this.getWorldObj().isRemote) + { + SoundsCool.network.sendToServer(new SoundPlayerSelectPacket(this)); + } + } + + public Sound getSelectedSound() + { + return selectedSound; + } + + public void playCurrentSound() + { + NetworkHelper.syncAllPlayerSounds(); + if (selectedSound != null) + { + if (timeSoundFinishedPlaying < System.currentTimeMillis()) + { + if (SoundHandler.getSound(selectedSound.getSoundName()) != null) + { + lastSoundIdentifier = UUID.randomUUID().toString(); + timeSoundFinishedPlaying = (long)(SoundHelper.getSoundLength(selectedSound.getSoundLocation())*1000) + System.currentTimeMillis(); + SoundsCool.network.sendToAllAround( + new ServerPlaySoundPacket(selectedSound.getSoundName(), lastSoundIdentifier, xCoord, yCoord, zCoord), + new NetworkRegistry.TargetPoint(getWorldObj().provider.dimensionId, xCoord, yCoord, zCoord, 64)); + } + else + { + selectedSound = null; + } + } + else + { + stopCurrentSound(); + } + } + } + + public void stopCurrentSound() + { + if (System.currentTimeMillis() < timeSoundFinishedPlaying) + { + SoundsCool.network.sendToAllAround(new StopSoundPacket(lastSoundIdentifier), + new NetworkRegistry.TargetPoint(getWorldObj().provider.dimensionId, xCoord, yCoord, zCoord, 64)); + timeSoundFinishedPlaying = 0; + } + } + + @Override + public void readFromNBT(NBTTagCompound compound) + { + super.readFromNBT(compound); + selectedSound = SoundHandler.getSound(compound.getString("selectedSound")); + } + + @Override + public void writeToNBT(NBTTagCompound compound) + { + super.writeToNBT(compound); + if (selectedSound != null) + { + compound.setString("selectedSound", selectedSound.getSoundName()); + } + } + + @Override + public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) + { + String soundName = pkt.func_148857_g().getString("selected"); + this.selectedSound = SoundHandler.getSound(soundName); + } + + @Override + public Packet getDescriptionPacket() + { + NBTTagCompound compound = new NBTTagCompound(); + if (selectedSound != null) + { + compound.setString("selectedSound", selectedSound.getSoundName()); + } + return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, compound); + } +}