Use buffered streams when managing NBT files

This commit is contained in:
Chicken-Bones committed 2014-10-01 20:01:10 +10:00
1 parent 8d1619589d
commit f2180dfab5
7 files changed
+51 -47

No files matched your search

+4 -28
View File
@@ -5,12 +5,8 @@ import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import java.util.List;
public class ItemStackSet implements ItemFilter
public class ItemStackSet extends ItemStackMap<ItemStack> implements ItemFilter
{
ItemStackMap<Object> underlying = new ItemStackMap<Object>();
public ItemStackSet with(ItemStack... items) {
for(ItemStack item : items)
add(item);
@@ -30,27 +26,15 @@ public class ItemStackSet implements ItemFilter
}
public void add(ItemStack item) {
underlying.put(item, new Object());
}
public boolean remove(ItemStack item) {
return underlying.remove(item) != null;
put(item, item);
}
public boolean contains(ItemStack item) {
return underlying.get(item) != null;
return get(item) != null;
}
public boolean containsAll(Item item) {
return underlying.get(ItemStackMap.wildcard(item)) != null;
}
public List<ItemStack> values() {
return underlying.keys();
}
public void clear() {
underlying.clear();
return get(ItemStackMap.wildcard(item)) != null;
}
@Override
@@ -58,14 +42,6 @@ public class ItemStackSet implements ItemFilter
return contains(item);
}
public int size() {
return underlying.size();
}
public boolean isEmpty() {
return underlying.isEmpty();
}
public static ItemStackSet of(Block... blocks) {
return new ItemStackSet().with(blocks);
}
+3 -4
View File
@@ -55,9 +55,8 @@ public class NEIServerConfig
private static void loadWorld(World world) {
try {
File file = new File(getSaveDir(CommonUtils.getDimension(world)), "world.dat");
NBTTagCompound tag = CompressedStreamTools.read(file);
if(tag == null)
tag = new NBTTagCompound();
NBTTagCompound tag = NEIServerUtils.readNBT(file);
if(tag == null) tag = new NBTTagCompound();
dimTags.put(CommonUtils.getDimension(world), tag);
} catch (Exception e) {
throw new RuntimeException(e);
@@ -103,7 +102,7 @@ public class NEIServerConfig
private static void saveWorld(int dim) {
try {
File file = new File(getSaveDir(dim), "world.dat");
CompressedStreamTools.write(dimTags.get(dim), file);
NEIServerUtils.writeNBT(dimTags.get(dim), file);
} catch (Exception e) {
throw new RuntimeException(e);
}
+25 -2
View File
@@ -13,6 +13,7 @@ import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.MinecraftServer;
@@ -24,9 +25,9 @@ import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.oredict.OreDictionary;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.*;
import java.util.*;
import java.util.zip.ZipException;
public class NEIServerUtils
{
@@ -377,4 +378,26 @@ public class NEIServerUtils
stackTraces.add(stackTrace);
}
}
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 static void writeNBT(NBTTagCompound tag, File file) throws IOException {
FileOutputStream out = new FileOutputStream(file);
CompressedStreamTools.writeCompressed(tag, out);
out.close();
}
}
+2 -2
View File
@@ -39,7 +39,7 @@ public class PlayerSave
if (!saveFile.exists())
saveFile.createNewFile();
if (saveFile.length() > 0)
nbt = CompressedStreamTools.read(saveFile);
nbt = NEIServerUtils.readNBT(saveFile);
} catch (Exception e) {
NEIClientConfig.logger.error("Error loading player save: "+username, e);
}
@@ -62,7 +62,7 @@ public class PlayerSave
saveCreativeInv();
try {
CompressedStreamTools.write(nbt, saveFile);
NEIServerUtils.writeNBT(nbt, saveFile);
isDirty = false;
} catch (Exception e) {
NEIClientConfig.logger.error("Error saving player: "+username, e);
+1 -1
View File
@@ -55,7 +55,7 @@ public class SubsetWidget extends Button implements ItemFilterProvider, ItemsLoa
SubsetTag tag = sorted.get(slot);
if (NEIClientUtils.shiftKey())
LayoutManager.searchField.setText("@" + tag.fullname);
else if (count >= 2)
else if (button == 0 && count >= 2)
SubsetWidget.showOnly(tag);
else
SubsetWidget.setHidden(tag, button == 1);
+13 -8
View File
@@ -347,19 +347,24 @@ public class ItemInfo
ItemStackSet potioningredients = new ItemStackSet();
ArrayList<ItemStackSet> creativeTabRanges = new ArrayList<ItemStackSet>(CreativeTabs.creativeTabArray.length);
List<ItemStack> stackList = new LinkedList<ItemStack>();
for (Item item : (Iterable<Item>) Item.itemRegistry) {
if (item == null)
continue;
CreativeTabs itemTab = item.getCreativeTab();
if (itemTab != null) {
while(itemTab.getTabIndex() >= creativeTabRanges.size())
creativeTabRanges.add(null);
ItemStackSet set = creativeTabRanges.get(itemTab.getTabIndex());
if(set == null)
creativeTabRanges.set(itemTab.getTabIndex(), set = new ItemStackSet());
set.with(item);
for(CreativeTabs itemTab : item.getCreativeTabs()) {
if (itemTab != null) {
while (itemTab.getTabIndex() >= creativeTabRanges.size())
creativeTabRanges.add(null);
ItemStackSet set = creativeTabRanges.get(itemTab.getTabIndex());
if (set == null)
creativeTabRanges.set(itemTab.getTabIndex(), set = new ItemStackSet());
stackList.clear();
item.getSubItems(item, itemTab, stackList);
for(ItemStack stack : stackList)
set.add(stack);
}
}
if (item.isDamageable()) {
+3 -2
View File
@@ -1,6 +1,7 @@
package codechicken.nei.config;
import codechicken.lib.config.ConfigTagParent;
import codechicken.nei.NEIServerUtils;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
@@ -26,7 +27,7 @@ public class ConfigSet
if (!nbtFile.exists())
nbtFile.createNewFile();
if (nbtFile.length() > 0)
nbt = CompressedStreamTools.read(nbtFile);
nbt = NEIServerUtils.readNBT(nbtFile);
} catch (Exception e) {
e.printStackTrace();
}
@@ -34,7 +35,7 @@ public class ConfigSet
public void saveNBT() {
try {
CompressedStreamTools.write(nbt, nbtFile);
NEIServerUtils.writeNBT(nbt, nbtFile);
} catch (Exception e) {
e.printStackTrace();
}