Files
CustomNPC-Plus/src/main/java/foxz/utils/Market.java
T

249 lines
9.4 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package foxz.utils;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.CompressedStreamTools;
2018-09-13 12:03:28 -04:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
2018-09-13 12:03:28 -04:00
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.config.ConfigMain;
2018-09-13 12:03:28 -04:00
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.roles.RoleTrader;
import noppes.npcs.util.CacheHashMap;
import noppes.npcs.util.MarketCachedObject;
2018-09-13 12:03:28 -04:00
import noppes.npcs.util.NBTJsonUtil;
2024-02-15 17:54:28 -05:00
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
public class Market {
2025-03-12 20:12:01 -04:00
private static final CacheHashMap<String, MarketCachedObject> marketCache = new CacheHashMap<>(5 * 60 * 1000, 60 * 1000);
public static void save(RoleTrader r, String name) {
2025-03-12 20:12:01 -04:00
if (name.isEmpty())
return;
NBTTagCompound roleCompound = r.writeNBT(new NBTTagCompound());
roleCompound.setString("SaveName", name);
putMarketCache(name, roleCompound);
saveFile(roleCompound);
}
public static void saveFile(NBTTagCompound compound) {
String name = compound.getString("SaveName");
File file = getFile(name + "_new");
File file1 = getFile(name);
customNPCThread.execute(() -> {
try {
2025-03-12 20:12:01 -04:00
if (ConfigMain.MarketDatFormat) {
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(file));
} else {
NBTJsonUtil.SaveFile(file, compound);
}
2025-03-12 20:12:01 -04:00
if (file1.exists()) {
file1.delete();
}
file.renameTo(file1);
} catch (Exception e) {
2024-02-15 17:54:28 -05:00
}
});
}
2025-03-12 20:12:01 -04:00
public static void getMarket(RoleTrader role, String name) {
NBTTagCompound data = getMarketCache(name);
2025-03-12 20:12:01 -04:00
if (data != null) {
role.readNBT(data);
return;
}
load(role, name);
}
public static NBTTagCompound getMarketCache(final String name) {
synchronized (marketCache) {
if (!marketCache.containsKey(name)) {
return null;
}
return marketCache.get(name).getObject();
}
}
public static void putMarketCache(final String name, final NBTTagCompound role) {
synchronized (marketCache) {
marketCache.put(name, new MarketCachedObject(role));
}
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
static public void load(RoleTrader role, String name) {
if (role.npc.worldObj.isRemote)
return;
File file = getFile(name);
2025-03-12 20:12:01 -04:00
if (!file.exists())
return;
try {
2025-03-12 20:12:01 -04:00
if (ConfigMain.MarketDatFormat) {
NBTTagCompound compound = NBTJsonUtil.loadNBTData(file);
role.readNBT(compound);
2025-03-12 20:12:01 -04:00
} else {
role.readNBT(NBTJsonUtil.LoadFile(file));
}
} catch (Exception e) {
}
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
public static File getMarketDir() {
try {
File file = new File(CustomNpcs.getWorldSaveDirectory(), "markets");
if (!file.exists())
file.mkdir();
return file;
2025-03-12 20:12:01 -04:00
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
2025-03-12 20:12:01 -04:00
public static File getNewMarketDir() {
try {
File file = new File(CustomNpcs.getWorldSaveDirectory(), "markets_new");
if (file.exists()) {
return null;
}
file.mkdir();
return file;
2025-03-12 20:12:01 -04:00
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
2025-03-12 20:12:01 -04:00
private static File getFile(String name) {
String filename = name.toLowerCase();
2025-03-12 20:12:01 -04:00
if (ConfigMain.MarketDatFormat) {
filename += ".dat";
} else {
filename += ".json";
}
return new File(getMarketDir(), filename);
2018-09-13 12:03:28 -04:00
}
static public void setMarket(EntityNPCInterface npc, String marketName) {
2025-03-12 20:12:01 -04:00
if (marketName.isEmpty())
return;
2025-03-12 20:12:01 -04:00
if (!getFile(marketName).exists())
Market.save((RoleTrader) npc.roleInterface, marketName);
2024-02-15 17:54:28 -05:00
Market.getMarket((RoleTrader) npc.roleInterface, marketName);
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
public static void convertMarketFiles(final EntityPlayerMP sender, final boolean type) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
String fileType;
2025-03-12 20:12:01 -04:00
if (type) {
fileType = ".dat";
2025-03-12 20:12:01 -04:00
} else {
fileType = ".json";
}
2025-03-12 20:12:01 -04:00
if (sender != null) {
LogWriter.info("Market Conversion queued by " + sender.getCommandSenderName());
sender.addChatMessage(new ChatComponentText("Market Conversion to " + fileType + " format"));
}
File dir = getMarketDir();
LogWriter.info("Converting Market to " + fileType + " format");
File[] files = dir.listFiles(); // Get an array of all files in the directory
2025-03-12 20:12:01 -04:00
if (files != null) {
int length = files.length;
2025-03-12 20:12:01 -04:00
if (length != 0) {
if (length > 100) {
LogWriter.info("Found " + length + " Market files... This may take a few minutes");
}
int tenPercent = (int) ((double) length * 0.1);
int progress = 0;
File saveDir = getNewMarketDir();
2025-03-12 20:12:01 -04:00
if (saveDir == null) {
if (sender != null) {
sender.addChatMessage(new ChatComponentText("markets_new folder already exists please delete it or rename it"));
}
LogWriter.error("markets_new folder already exists please delete it or rename it");
return;
}
// Load the files in parallel using a stream
2025-03-12 20:12:01 -04:00
for (int i = 0; i < length; i++) {
File file = files[i];
2025-03-12 20:12:01 -04:00
if (file.isDirectory() || (!file.getName().endsWith(".json") && !file.getName().endsWith(".dat")))
continue;
try {
String filename = "error";
boolean valid = false;
NBTTagCompound compound = new NBTTagCompound();
2025-03-12 20:12:01 -04:00
if (type) {
if (file.getName().endsWith(".json")) {
compound = NBTJsonUtil.LoadFile(file);
2025-03-12 20:12:01 -04:00
if (compound.hasKey("PlayerName")) {
filename = file.getName().substring(0, file.getName().length() - 5);
valid = true;
}
}
} else {
2025-03-12 20:12:01 -04:00
if (file.getName().endsWith(".dat")) {
compound = NBTJsonUtil.loadNBTData(file);
2025-03-12 20:12:01 -04:00
if (compound.hasKey("PlayerName")) {
filename = file.getName().substring(0, file.getName().length() - 4);
valid = true;
}
}
}
2025-03-12 20:12:01 -04:00
if (valid) {
try {
File newFile = new File(saveDir, filename + "_new" + fileType);
File oldFile = new File(saveDir, filename + fileType);
2025-03-12 20:12:01 -04:00
if (type) {
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(newFile));
} else {
NBTJsonUtil.SaveFile(newFile, compound);
}
2025-03-12 20:12:01 -04:00
if (oldFile.exists()) {
oldFile.delete();
}
newFile.renameTo(oldFile);
} catch (Exception e) {
LogWriter.except(e);
}
}
} catch (Exception e) {
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
}
2025-03-12 20:12:01 -04:00
if (tenPercent != 0) {
if (progress != 100) {
if (i % tenPercent == 0) {
progress += 10;
LogWriter.info("Converting Market: Progress: " + progress + "%");
}
}
}
}
}
}
2025-03-12 20:12:01 -04:00
if (sender != null) {
sender.addChatMessage(new ChatComponentText("Market Conversion complete"));
}
LogWriter.info("Market Converted - Please rename the markets_new folder to markets and restart");
});
executor.shutdown();
}
2018-09-13 12:03:28 -04:00
}