package mcjty.rftools.blocks; import cpw.mods.fml.common.registry.GameData; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.play.server.S29PacketSoundEffect; import net.minecraft.util.ChunkCoordinates; import net.minecraft.world.World; import org.apache.commons.lang3.StringUtils; public class RFToolsTools { // Server side: play a sound to all nearby players public static void playSound(World worldObj, String soundName, double x, double y, double z, double volume, double pitch) { S29PacketSoundEffect soundEffect = new S29PacketSoundEffect(soundName, x, y, z, (float) volume, (float) pitch); for (int j = 0; j < worldObj.playerEntities.size(); ++j) { EntityPlayerMP entityplayermp = (EntityPlayerMP)worldObj.playerEntities.get(j); ChunkCoordinates chunkcoordinates = entityplayermp.getPlayerCoordinates(); double d7 = x - chunkcoordinates.posX; double d8 = y - chunkcoordinates.posY; double d9 = z - chunkcoordinates.posZ; double d10 = d7 * d7 + d8 * d8 + d9 * d9; if (d10 <= 256.0D) { entityplayermp.playerNetServerHandler.sendPacket(soundEffect); } } } public static StringBuffer appendIndent(StringBuffer buffer, int indent) { return buffer.append(StringUtils.repeat(' ', indent)); } public static void convertNBTtoJson(StringBuffer buffer, NBTTagList tagList, int indent) { for (int i = 0 ; i < tagList.tagCount() ; i++) { NBTTagCompound compound = tagList.getCompoundTagAt(i); appendIndent(buffer, indent).append("{\n"); convertNBTtoJson(buffer, compound, indent + 4); appendIndent(buffer, indent).append("},\n"); } } public static void convertNBTtoJson(StringBuffer buffer, NBTTagCompound tagCompound, int indent) { boolean first = true; for (Object o : tagCompound.func_150296_c()) { if (!first) { buffer.append(",\n"); } first = false; String key = (String) o; NBTBase tag = tagCompound.getTag(key); appendIndent(buffer, indent).append(key).append(':'); if (tag instanceof NBTTagCompound) { NBTTagCompound compound = (NBTTagCompound) tag; buffer.append("{\n"); convertNBTtoJson(buffer, compound, indent + 4); appendIndent(buffer, indent).append('}'); } else if (tag instanceof NBTTagList) { NBTTagList list = (NBTTagList) tag; buffer.append("[\n"); convertNBTtoJson(buffer, list, indent + 4); appendIndent(buffer, indent).append(']'); } else { buffer.append(tag); } } if (!first) { buffer.append("\n"); } } public static String getModidForBlock(Block block) { String nameForObject = GameData.getBlockRegistry().getNameForObject(block); if (nameForObject == null) { return "?"; } String[] lst = StringUtils.split(nameForObject, ":"); if (lst.length >= 2) { return lst[0]; } else { return "?"; } } }