DeathChests/src/main/java/lostcave/deathchests/util/DeathChestStorage.java

101 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package lostcave.deathchests.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class DeathChestStorage {
private static LookUpTable<int[]> deathChestLocations;
public static final String death_chests_dat_file = "deathchests.dat";
/**
* When a new death chest has been placed, add it to the LUT. This does not
* place the chest itself.
*
* @param uuid
* @param location
*/
public static void addNewDeathChestLocation(String uuid, int[] location) {
deathChestLocations.add(uuid, location);
}
/**
* Get the location of a new death chest for a given player and remove it from
* the LUT of new death chests.
*
* @param uuid
* @return the location {x, y, z, dimension}
*/
public static int[] popNewDeathChestLocation(String uuid) {
int[] location = deathChestLocations.get(uuid);
deathChestLocations.remove(uuid);
return location;
}
/**
* Checks if a given player has a new death chest that no obituary item has been
* created for.
* This is used in respawn events.
*
* @param uuid
* @return
*/
public static boolean hasNewDeathChestLocation(String uuid) {
return deathChestLocations.get(uuid) != null;
}
public static void save(File worldSaveLocation) {
Debug.out("Putting death chest LUT into a compound");
NBTTagCompound compound = new NBTTagCompound();
for (int i = 0; i < deathChestLocations.storedElements(); i++) {
String uuid = deathChestLocations.getKey(i);
compound.setIntArray(uuid, deathChestLocations.get(uuid));
}
File file = new File(worldSaveLocation, death_chests_dat_file);
Debug.out("Saving to file: " + file.toPath().toAbsolutePath().toString());
try {
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void load(File worldSaveLocation) {
File file = new File(worldSaveLocation, death_chests_dat_file);
Debug.out("Loading death chest LUT from file: " + file.toPath().toAbsolutePath().toString());
// reset the LUT in case one still exists from quitting to main menu and loading a world
deathChestLocations = new LookUpTable<int[]>();
if (file.exists()) {
try {
NBTTagCompound compound = CompressedStreamTools.readCompressed(new FileInputStream(file));
// func_150296_c() should be named getKeySet() but didnt deobfuscate properly
// This might be a problem with Forge, or it might be a problem with my Frankenstein setup.
for (String uuid : (Set<String>) compound.func_150296_c()) {
deathChestLocations.add(uuid, compound.getIntArray(uuid));
}
} catch (IOException e) {
e.printStackTrace();
}
}
Debug.out("The following entries have been loaded:");
if (Config.debug) {
for (int i = 0; i < deathChestLocations.storedElements(); i++) {
String uuid = deathChestLocations.getKey(i);
int[] location = deathChestLocations.get(uuid);
Debug.out(uuid + " - x=" + Integer.toString(location[0]) + " y=" + Integer.toString(location[1]) + " z=" + Integer.toString(location[2]) + " dim=" + Integer.toString(location[3]));
}
}
Debug.out("--");
}
}