Last active
August 29, 2015 14:03
-
-
Save MultiMote/9e322275b48c317eacab to your computer and use it in GitHub Desktop.
NBT Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.multi.microZ.utils; | |
import com.multi.microZ.Core; | |
import com.multi.microZ.data.GlobalData; | |
import com.multi.microZ.data.WorldData; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.CompressedStreamTools; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.nbt.NBTTagList; | |
import net.minecraft.world.World; | |
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Created by multimote on 09.07.14. | |
*/ | |
public class NBTConf { | |
public static NBTTagCompound get(File file){ | |
try { | |
if(!file.exists())return null; | |
DataInputStream is = new DataInputStream(new FileInputStream(file)); | |
NBTTagCompound nbtdata = CompressedStreamTools.read(is); | |
is.close(); | |
return nbtdata; | |
} catch (Exception e) { | |
Core.logger.error("I can't load config, because " + (System.currentTimeMillis()%100<2 ? "science!" : e.toString())); | |
} | |
return null; | |
} | |
public static boolean write(File file, NBTTagCompound com){ | |
try { | |
file.getParentFile().mkdirs(); | |
DataOutputStream os = new DataOutputStream(new FileOutputStream(file)); | |
CompressedStreamTools.write(com, os); | |
os.close(); | |
return true; | |
} catch (Exception e) { | |
Core.logger.error("I can't write config, because " + (System.currentTimeMillis()%100<2 ? "science!" : e.toString())); | |
} | |
return false; | |
} | |
public static void writeLocations(World w, int dim){ | |
if(w==null)return; | |
if(w.getSaveHandler().getWorldDirectory()==null)return; | |
NBTTagCompound c = new NBTTagCompound(); | |
NBTTagList nbttaglist = new NBTTagList(); | |
if(WorldData.allLocations.containsKey(dim)){ | |
for (WorldData.WorldLocation loc : WorldData.allLocations.get(dim)){ | |
NBTTagCompound loccomp = new NBTTagCompound(); | |
loc.writeNBT(loccomp); | |
nbttaglist.appendTag(loccomp); | |
} | |
c.setTag("Locations", nbttaglist); | |
write(new File(w.getSaveHandler().getWorldDirectory(), "MicroZ/Dimension/"+dim+"/MicroZ-Locations.dat"), c);} | |
} | |
public static void readLocations(World w, int dim){ | |
if(w==null)return; | |
if(w.getSaveHandler().getWorldDirectory()==null)return; | |
NBTTagCompound com = get(new File(w.getSaveHandler().getWorldDirectory(), "MicroZ/Dimension/"+dim+"/MicroZ-Locations.dat")); | |
if(com==null)return; | |
NBTTagList nbttaglist = com.getTagList("Locations", 10); | |
List<WorldData.WorldLocation> locations = new ArrayList<WorldData.WorldLocation>(); | |
for (int i = 0; i < nbttaglist.tagCount(); ++i) { | |
locations.add(WorldData.WorldLocation.readNBT(nbttaglist.getCompoundTagAt(i))); | |
Core.logger.info("Added " + locations.get(i).name + " location for dimension " + dim+".");//todo убрать, если будет бесить | |
} | |
if(WorldData.allLocations.containsKey(dim))WorldData.allLocations.remove(dim); | |
WorldData.allLocations.put(dim, locations); | |
} | |
public static void writeWorldConfig(World w, int dim){ | |
if(w==null)return; | |
if(w.getSaveHandler().getWorldDirectory()==null) return; | |
if(!WorldData.worldBorders.containsKey(dim))return; | |
NBTTagCompound c = new NBTTagCompound(); | |
WorldData.worldBorders.get(dim).writeNBT(c); | |
write(new File(w.getSaveHandler().getWorldDirectory(), "MicroZ/Dimension/" + dim + "/MicroZ-WorldConfig.dat"), c); | |
} | |
public static void readWorldConfig(World w, int dim){ | |
if(w==null)return; | |
if(w.getSaveHandler().getWorldDirectory()==null)return; | |
NBTTagCompound com = get(new File(w.getSaveHandler().getWorldDirectory(), "MicroZ/Dimension/"+dim+"/MicroZ-WorldConfig.dat")); | |
if(com==null)return; | |
WorldData.WorldBorder border = WorldData.WorldBorder.readNBT(com); | |
if(border==null)return; | |
if(WorldData.worldBorders.containsKey(dim))WorldData.worldBorders.remove(dim); | |
WorldData.worldBorders.put(dim, border); | |
} | |
public static void readGlobalConf() { | |
NBTTagCompound com = get(new File(Minecraft.getMinecraft().mcDataDir, "config/MicroZ/Global.dat")); | |
if(com==null)return; | |
List<Integer> temp = new ArrayList<Integer>(); | |
int[] blocks = com.getIntArray("WhitelistedBlocks"); | |
for(int a : blocks) temp.add(a); | |
GlobalData.breakWhitelist = temp; | |
Core.logger.info("Block whitelist: " + temp); | |
} | |
public static void writeGlobalConf() { | |
NBTTagCompound c = new NBTTagCompound(); | |
int[] blocks = new int[GlobalData.breakWhitelist.size()]; | |
for (int i = 0; i < GlobalData.breakWhitelist.size(); i++){ | |
blocks[i] = GlobalData.breakWhitelist.get(i); | |
} | |
c.setIntArray("WhitelistedBlocks", blocks); | |
write(new File(Minecraft.getMinecraft().mcDataDir, "config/MicroZ/Global.dat"), c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment