/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Cayde Dixon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.cazzar.mods.jukeboxreloaded.lib.util;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import java.io.*;
import java.net.URL;
public class Util {
public static void copyStream(InputStream input, OutputStream output) {
try {
final byte data[] = new byte[8192];
int count;
while ((count = input.read(data, 0, 8192)) != -1)
output.write(data, 0, count);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public static void saveUrl(String filename, URL url)
throws IOException {
//noinspection ResultOfMethodCallIgnored
new File(filename).getParentFile().mkdirs();
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(filename);
copyStream(in, fout);
} finally {
if (in != null) in.close();
if (fout != null) fout.close();
}
}
public static <T extends TileEntity> T getTileEntity(IBlockAccess world, int x, int y, int z, Class<? extends T> tileClass) {
TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z));
if (tileEntity == null) return null;
if (tileEntity.getClass().isAssignableFrom(tileClass)) //noinspection unchecked
return (T) tileEntity;
return null;
}
}