下面列出了net.minecraft.util.datafix.FixTypes#net.minecraft.nbt.CompressedStreamTools 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Template getBuiltinTemplate(World world, ResourceLocation templateId) {
if (templateMap.containsKey(templateId)) {
return templateMap.get(templateId);
}
Template template = new Template();
String resourcePath = "/assets/" + templateId.getResourceDomain() + "/structures/" + templateId.getResourcePath() + ".nbt";
InputStream inputStream = TemplateManager.class.getResourceAsStream(resourcePath);
if (inputStream != null) {
try {
NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(inputStream);
if (!nbttagcompound.hasKey("DataVersion", 99)) {
nbttagcompound.setInteger("DataVersion", 500);
}
DataFixer dataFixer = world.getMinecraftServer().getDataFixer();
template.read(dataFixer.process(FixTypes.STRUCTURE, nbttagcompound));
} catch (IOException exception) {
GTLog.logger.error("Failed to load builtin template {}", templateId, exception);
} finally {
IOUtils.closeQuietly(inputStream);
}
} else {
GTLog.logger.warn("Failed to find builtin structure with path {}", resourcePath);
}
templateMap.put(templateId, template);
return template;
}
public void loadServerList(List<ServerData> servers, Minecraft mc) {
try {
servers.clear();
NBTTagCompound nbttagcompound = CompressedStreamTools
.read(new File(mc.mcDataDir, "servers.dat"));
if (nbttagcompound == null) return;
NBTTagList nbttaglist = nbttagcompound.getTagList("servers", 10);
int bound = nbttaglist.tagCount();
for (int i = 0; i < bound; i++) {
ServerData serverDataFromNBTCompound = ServerData.getServerDataFromNBTCompound(nbttaglist.getCompoundTagAt(i));
servers.add(serverDataFromNBTCompound);
}
} catch (Exception exception) {
Hyperium.LOGGER.error("Failed to load server list", exception);
}
}
public void saveServerList(List<ServerData> servers, Minecraft mc) {
try {
NBTTagList nbttaglist = new NBTTagList();
for (ServerData server : servers) {
NBTTagCompound nbtCompound = server.getNBTCompound();
nbttaglist.appendTag(nbtCompound);
}
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setTag("servers", nbttaglist);
CompressedStreamTools.safeWrite(nbttagcompound, new File(mc.mcDataDir, "servers.dat"));
} catch (Exception exception) {
Hyperium.LOGGER.error("Save server list error", exception);
}
}
private void save(boolean force) {
if (!dirtyStorage.isEmpty() || force) {
for (AbstractEnderStorage inv : dirtyStorage) {
saveTag.put(inv.freq + ",type=" + inv.type(), inv.saveToTag());
inv.setClean();
}
dirtyStorage.clear();
try {
File saveFile = saveFiles[saveTo];
if (!saveFile.exists()) {
saveFile.createNewFile();
}
DataOutputStream dout = new DataOutputStream(new FileOutputStream(saveFile));
CompressedStreamTools.writeCompressed(saveTag, dout);
dout.close();
FileOutputStream fout = new FileOutputStream(saveFiles[2]);
fout.write(saveTo);
fout.close();
saveTo ^= 1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static NBTTagCompound readNBT(File file) throws IOException {
if (!file.exists()) {
return null;
}
FileInputStream in = new FileInputStream(file);
NBTTagCompound tag;
try {
tag = CompressedStreamTools.readCompressed(in);
} catch (ZipException e) {
if (e.getMessage().equals("Not in GZIP format")) {
tag = CompressedStreamTools.read(file);
} else {
throw e;
}
}
in.close();
return tag;
}
/**
* Saves NBT data in the world folder.
* @param file File to save data to
* @param data Data to save
* @return True on success.
*/
public boolean saveFile(File file, NBTTagCompound data) {
try {
File tempFile = new File(file.getParent(), file.getName().replaceFirst("\\.nbt$", ".tmp.nbt"));
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
if (file.exists()) {
file.delete();
}
tempFile.renameTo(file);
return true;
} catch (Exception e) {
Game.logger().error("Failed to queueSave {}!", file.getName());
e.printStackTrace();
return false;
}
}
/**
* Saves NBT data in the world folder.
* @param file File to save data to
* @param data Data to save
* @return True on success.
*/
public boolean saveFile(File file, NBTTagCompound data) {
try {
File tempFile = new File(file.getParent(), file.getName().replaceFirst("\\.nbt$", ".tmp.nbt"));
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
if (file.exists()) {
file.delete();
}
tempFile.renameTo(file);
return true;
} catch (Exception e) {
Game.logger().error("Failed to queueSave {}!", file.getName(), e);
e.printStackTrace();
return false;
}
}
/**
* Saves NBT data in the world folder.
* @param file File to save data to
* @param data Data to save
* @return True on success.
*/
public boolean saveFile(File file, NBTTagCompound data) {
try {
File tempFile = new File(file.getParent(), file.getName().replaceFirst("\\.nbt$", ".tmp.nbt"));
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
if (file.exists()) {
file.delete();
}
tempFile.renameTo(file);
return true;
} catch (Exception e) {
Game.logger().error("Failed to queueSave {}!", file.getName(), e);
e.printStackTrace();
return false;
}
}
public static NBTTagCompound loadNBTFromPath( File file )
{
try
{
if( file != null && file.exists() )
{
InputStream input = new BufferedInputStream( new FileInputStream( file ) );
try
{
return CompressedStreamTools.readCompressed( input );
}
finally
{
input.close();
}
}
}
catch( IOException e )
{
QCraft.log( "Warning: failed to load QCraft entanglement info" );
}
return null;
}
public static void saveNBTToPath( File file, NBTTagCompound nbt )
{
try
{
if( file != null )
{
file.getParentFile().mkdirs();
OutputStream output = new BufferedOutputStream( new FileOutputStream( file ) );
try
{
CompressedStreamTools.writeCompressed( nbt, output );
}
finally
{
output.close();
}
}
}
catch( IOException e )
{
QCraft.log( "Warning: failed to save QCraft entanglement info" );
}
}
private void save(boolean force) {
if (!dirtyStorage.isEmpty() || force) {
for (AbstractEnderStorage inv : dirtyStorage) {
saveTag.setTag(inv.freq + "|" + inv.owner + "|" + inv.type(), inv.saveToTag());
inv.setClean();
}
dirtyStorage.clear();
try {
File saveFile = saveFiles[saveTo];
if (!saveFile.exists())
saveFile.createNewFile();
DataOutputStream dout = new DataOutputStream(new FileOutputStream(saveFile));
CompressedStreamTools.writeCompressed(saveTag, dout);
dout.close();
FileOutputStream fout = new FileOutputStream(saveFiles[2]);
fout.write(saveTo);
fout.close();
saveTo ^= 1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static void writeNBTTagCompoundToBuffer(ByteBuf buf, NBTTagCompound tag)
{
if (tag == null)
{
buf.writeByte(0);
return;
}
try
{
CompressedStreamTools.write(tag, new ByteBufOutputStream(buf));
}
catch (IOException ioexception)
{
EnderUtilities.logger.error("IOException while trying to write a NBTTagCompound to ByteBuf");
throw new EncoderException(ioexception);
}
}
private static Template loadTemplate(InputStream is)
{
if (is == null)
return null;
try
{
NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
Template template = new Template();
template.read(nbt);
return template;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (is != null)
IOUtils.closeQuietly(is);
}
return null;
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buf, EntityPlayer player) throws IOException
{
super.decodeInto(ctx, buf, player);
if (ship != null)
{
DataInputStream in = new DataInputStream(new ByteBufInputStream(buf));
try
{
tagCompound = CompressedStreamTools.read(in);
} catch (IOException e)
{
throw e;
} finally
{
in.close();
}
}
}
public boolean writeToFile()
{
if (this.dirty == false)
{
return false;
}
File dir = this.getCacheDir();
File file = this.getCacheFile();
try
{
if (dir.exists() == false && dir.mkdirs() == false)
{
Litematica.logger.warn("Failed to write the material list cache to file '{}'", file.getAbsolutePath());
return false;
}
FileOutputStream os = new FileOutputStream(file);
CompressedStreamTools.writeCompressed(this.writeToNBT(), os);
os.close();
this.dirty = false;
return true;
}
catch (Exception e)
{
Litematica.logger.warn("Failed to write the material list cache to file '{}'", file.getAbsolutePath(), e);
}
return false;
}
public void readFromFile()
{
File file = this.getCacheFile();
if (file.exists() == false || file.canRead() == false)
{
return;
}
try
{
FileInputStream is = new FileInputStream(file);
NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
is.close();
if (nbt != null)
{
this.readFromNBT(nbt);
this.hasReadFromFile = true;
this.dirty = false;
}
}
catch (Exception e)
{
Litematica.logger.warn("Failed to read the material list cache from file '{}'", file.getAbsolutePath(), e);
}
}
/**
* Reads a {@link CompoundNBT} from the stream.
*
* @return The {@link CompoundNBT}.
*/
@Nullable
default CompoundNBT readCompoundNBT() {
if (!readBoolean()) {
return null;
} else {
try {
return CompressedStreamTools.read(toDataInput(), new NBTSizeTracker(2097152L));
} catch (IOException e) {
throw new EncoderException("Failed to read CompoundNBT from stream.", e);
}
}
}
/**
* Writes a {@link CompoundNBT} to the stream.
*
* @param tag The {@link CompoundNBT}.
* @return The same stream.
*/
default MCDataOutput writeCompoundNBT(CompoundNBT tag) {
if (tag == null) {
writeBoolean(false);
} else {
try {
writeBoolean(true);
CompressedStreamTools.write(tag, toDataOutput());
} catch (IOException e) {
throw new EncoderException("Failed to write CompoundNBT to stream.", e);
}
}
return this;
}
public void dumpNBT(File file) throws IOException {
NBTTagList list = new NBTTagList();
for (ItemStack stack : ItemPanel.items) {
list.appendTag(stack.writeToNBT(new NBTTagCompound()));
}
NBTTagCompound tag = new NBTTagCompound();
tag.setTag("list", list);
CompressedStreamTools.writeCompressed(tag, new FileOutputStream(file));
}
public NBTTagCompound loadFile(File file) {
try {
if (file.exists()) {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
} else {
return new NBTTagCompound();
}
} catch (Exception e) {
Game.logger().error("Failed to load {}!", file.getName());
e.printStackTrace();
return null;
}
}
public NBTTagCompound loadFile(File file) {
try {
if (file.exists()) {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
} else {
return new NBTTagCompound();
}
} catch (Exception e) {
Game.logger().error("Failed to load {}!", file.getName(), e);
e.printStackTrace();
return null;
}
}
public NBTTagCompound loadFile(File file) {
try {
if (file.exists()) {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
} else {
return new NBTTagCompound();
}
} catch (Exception e) {
Game.logger().error("Failed to load {}!", file.getName(), e);
e.printStackTrace();
return null;
}
}
@Override
public void fromBytes(ByteBuf buf) {
int size = buf.readInt();
for (int i = 0; i < size; i++) {
String key = StringHelper.readFromBuffer(buf);
byte providerId = buf.readByte();
AbstractData.AbstractDataProvider<? extends AbstractData> provider = AbstractData.Registry.getProvider(providerId);
if(provider == null) {
Gadomancy.log.warn("Provider for ID " + providerId + " doesn't exist! Skipping...");
continue;
}
NBTTagCompound cmp;
short compoundLength = buf.readShort();
byte[] abyte = new byte[compoundLength];
buf.readBytes(abyte);
try {
cmp = CompressedStreamTools.func_152457_a(abyte, new NBTSizeTracker(2097152L));
} catch (IOException e) {
Gadomancy.log.warn("Provider Compound of " + providerId + " threw an IOException! Skipping...");
Gadomancy.log.warn("Exception message: " + e.getMessage());
continue;
}
AbstractData dat = provider.provideNewInstance();
dat.readRawFromPacket(cmp);
data.put(key, dat);
}
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(data.size());
for(String key : data.keySet()) {
AbstractData dat = data.get(key);
NBTTagCompound cmp = new NBTTagCompound();
if(shouldSyncAll) {
dat.writeAllDataToPacket(cmp);
} else {
dat.writeToPacket(cmp);
}
StringHelper.writeToBuffer(buf, key);
byte providerId = dat.getProviderID();
buf.writeByte(providerId);
byte[] abyte;
try {
abyte = CompressedStreamTools.compress(cmp);
} catch (IOException e) {
Gadomancy.log.warn("Compressing the NBTTagCompound of " + providerId + " threw an IOException! Skipping...");
Gadomancy.log.warn("Exception message: " + e.getMessage());
continue;
}
buf.writeShort((short) abyte.length);
buf.writeBytes(abyte);
}
}
public static NBTTagCompound readNBT(File file) throws IOException {
if(!file.exists()) return null;
FileInputStream in = new FileInputStream(file);
NBTTagCompound tag;
try {
tag = CompressedStreamTools.readCompressed(in);
} catch(ZipException e) {
if(e.getMessage().equals("Not in GZIP format"))
tag = CompressedStreamTools.read(file);
else
throw e;
}
in.close();
return tag;
}
public void dumpNBT(File file) throws IOException {
NBTTagList list = new NBTTagList();
for (ItemStack stack : ItemPanel.items)
list.appendTag(stack.writeToNBT(new NBTTagCompound()));
NBTTagCompound tag = new NBTTagCompound();
tag.setTag("list", list);
CompressedStreamTools.writeCompressed(tag, new FileOutputStream(file));
}
public static NBTTagCompound readNBTTagCompoundFromBuffer(ByteBuf buf) throws IOException
{
int i = buf.readerIndex();
byte b0 = buf.readByte();
if (b0 == 0)
{
return null;
}
else
{
buf.readerIndex(i);
return CompressedStreamTools.read(new ByteBufInputStream(buf), new NBTSizeTracker(2097152L));
}
}
public static void readFromDisk()
{
// Clear the data structures when reading the data for a world/save, so that valid Energy Bridges
// from another world won't carry over to a world/save that doesn't have the file yet.
BRIDGE_LOCATIONS.clear();
BRIDGE_COUNTS.clear();
bridgeCountInEndDimensions = 0;
try
{
File saveDir = DimensionManager.getCurrentSaveRootDirectory();
if (saveDir == null)
{
return;
}
File file = new File(new File(saveDir, Reference.MOD_ID), "energybridges.dat");
if (file.exists() && file.isFile())
{
FileInputStream is = new FileInputStream(file);
readFromNBT(CompressedStreamTools.readCompressed(is));
is.close();
}
}
catch (Exception e)
{
EnderUtilities.logger.warn("Failed to read Energy Bridge data from file!", e);
}
}
public void readFromDisk()
{
// Clear the data structures when reading the data for a world/save, so that data
// from another world won't carry over to a world/save that doesn't have the file yet.
this.properties.clear();
this.indices.clear();
try
{
File saveDir = DimensionManager.getCurrentSaveRootDirectory();
if (saveDir == null)
{
return;
}
File file = new File(new File(saveDir, Reference.MOD_ID), FILE_NAME_BASE + ".dat");
if (file.exists() && file.isFile())
{
this.readFromNBT(CompressedStreamTools.readCompressed(new FileInputStream(file)));
}
}
catch (Exception e)
{
EnderUtilities.logger.warn("Failed to read Placement Properties data from file");
}
}
private void readTemplateFromStream(String id, InputStream stream) throws IOException
{
NBTTagCompound nbt = CompressedStreamTools.readCompressed(stream);
if (nbt.hasKey("DataVersion", Constants.NBT.TAG_ANY_NUMERIC) == false)
{
nbt.setInteger("DataVersion", 500);
}
TemplateEnderUtilities template = new TemplateEnderUtilities();
template.read(this.fixer.process(FixTypes.STRUCTURE, nbt));
this.templates.put(id, template);
}