2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
import java.io.File;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
|
|
|
|
|
import codechicken.core.ServerUtils;
|
|
|
|
|
import codechicken.lib.inventory.InventoryUtils;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class PlayerSave {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
public String username;
|
|
|
|
|
|
|
|
|
|
private File saveFile;
|
|
|
|
|
private NBTTagCompound nbt;
|
|
|
|
|
|
|
|
|
|
public ItemStack[] creativeInv;
|
|
|
|
|
private boolean creativeInvDirty;
|
|
|
|
|
|
|
|
|
|
private boolean isDirty;
|
|
|
|
|
private boolean wasOp;
|
|
|
|
|
|
|
|
|
|
public PlayerSave(String playername, File saveLocation) {
|
|
|
|
|
username = playername;
|
|
|
|
|
wasOp = ServerUtils.isPlayerOP(playername);
|
|
|
|
|
|
|
|
|
|
saveFile = new File(saveLocation, username + ".dat");
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs();
|
2014-04-29 10:54:20 +10:00
|
|
|
load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void load() {
|
|
|
|
|
nbt = new NBTTagCompound();
|
|
|
|
|
try {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!saveFile.exists()) saveFile.createNewFile();
|
|
|
|
|
if (saveFile.length() > 0) nbt = NEIServerUtils.readNBT(saveFile);
|
2014-04-29 10:54:20 +10:00
|
|
|
} catch (Exception e) {
|
2022-06-24 12:32:13 +01:00
|
|
|
NEIClientConfig.logger.error("Error loading player save: " + username, e);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadCreativeInv();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadCreativeInv() {
|
|
|
|
|
creativeInv = new ItemStack[54];
|
|
|
|
|
NBTTagList itemList = nbt.getTagList("creativeitems", 10);
|
2022-06-24 12:32:13 +01:00
|
|
|
if (itemList != null) InventoryUtils.readItemStacksFromTag(creativeInv, itemList);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void save() {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!isDirty) return;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (creativeInvDirty) saveCreativeInv();
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
try {
|
2014-10-01 20:01:10 +10:00
|
|
|
NEIServerUtils.writeNBT(nbt, saveFile);
|
2014-04-29 10:54:20 +10:00
|
|
|
isDirty = false;
|
|
|
|
|
} catch (Exception e) {
|
2022-06-24 12:32:13 +01:00
|
|
|
NEIClientConfig.logger.error("Error saving player: " + username, e);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveCreativeInv() {
|
|
|
|
|
NBTTagList invsave = InventoryUtils.writeItemStacksToTag(creativeInv);
|
|
|
|
|
nbt.setTag("creativeitems", invsave);
|
|
|
|
|
|
|
|
|
|
creativeInvDirty = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCreativeDirty() {
|
|
|
|
|
creativeInvDirty = isDirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDirty() {
|
|
|
|
|
isDirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateOpChange(EntityPlayerMP player) {
|
|
|
|
|
boolean isOp = ServerUtils.isPlayerOP(username);
|
|
|
|
|
if (isOp != wasOp) {
|
|
|
|
|
NEISPH.sendHasServerSideTo(player);
|
|
|
|
|
wasOp = isOp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isActionEnabled(String name) {
|
|
|
|
|
return getEnabledActions().getBoolean(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NBTTagCompound getEnabledActions() {
|
|
|
|
|
NBTTagCompound tag = nbt.getCompoundTag("enabledActions");
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!nbt.hasKey("enabledActions")) nbt.setTag("enabledActions", tag);
|
2014-04-29 10:54:20 +10:00
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void enableAction(String name, boolean enabled) {
|
|
|
|
|
getEnabledActions().setBoolean(name, enabled);
|
|
|
|
|
NEISPH.sendActionEnabled(ServerUtils.getPlayer(username), name, enabled);
|
|
|
|
|
setDirty();
|
|
|
|
|
}
|
|
|
|
|
}
|