Utilize a One Thread and Synchronized attribute to better control data integrity and prevent data loss

This commit is contained in:
Kam committed 2023-05-15 19:31:40 -04:00
1 parent 60dbcc7146
commit ae1f33d327
6 files changed
+78 -45

No files matched your search

+17 -13
View File
@@ -39,6 +39,7 @@ import noppes.npcs.roles.RoleTransporter;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.ScriptSound;
import noppes.npcs.scripted.event.DialogEvent;
import noppes.npcs.util.CustomNPCsScheduler;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
@@ -172,9 +173,9 @@ public class NoppesUtilServer {
}
}
if(npc instanceof EntityDialogNpc){
if(npc instanceof EntityDialogNpc || dia.id < 0){
dialog.hideNPC = true;
Server.sendData((EntityPlayerMP)player, EnumPacketClient.DIALOG_DUMMY, npc.getCommandSenderName(), dialog.writeToNBT(new NBTTagCompound()));
Server.sendDataDelayed((EntityPlayerMP)player, EnumPacketClient.DIALOG_DUMMY, 100, npc.getCommandSenderName(), dialog.writeToNBT(new NBTTagCompound()));
}
else
Server.sendData((EntityPlayerMP)player, EnumPacketClient.DIALOG, npc.getEntityId(), dialog.writeToNBT(new NBTTagCompound()));
@@ -248,18 +249,21 @@ public class NoppesUtilServer {
setEditingNpc(player, npc);
sendExtraData(player, npc,gui, i, j, k);
if(CustomNpcs.proxy.getServerGuiElement(gui.ordinal(), player, player.worldObj, i, j, k) != null){
player.openGui(CustomNpcs.instance, gui.ordinal(), player.worldObj, i, j, k);
return;
}
else{
Server.sendData((EntityPlayerMP)player, EnumPacketClient.GUI, gui.ordinal(), i, j, k);
}
ArrayList<String> list = getScrollData(player, gui, npc);
if(list == null || list.isEmpty())
return;
Server.sendData((EntityPlayerMP)player, EnumPacketClient.SCROLL_LIST, list);
CustomNPCsScheduler.runTack(() -> {
if(CustomNpcs.proxy.getServerGuiElement(gui.ordinal(), player, player.worldObj, i, j, k) != null){
player.openGui(CustomNpcs.instance, gui.ordinal(), player.worldObj, i, j, k);
return;
}
else{
Server.sendData((EntityPlayerMP)player, EnumPacketClient.GUI, gui.ordinal(), i, j, k);
}
ArrayList<String> list = getScrollData(player, gui, npc);
if(list == null || list.isEmpty())
return;
Server.sendData((EntityPlayerMP)player, EnumPacketClient.SCROLL_LIST, list);
}, 200);
}
+27 -1
View File
@@ -13,6 +13,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.village.MerchantRecipeList;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.util.CustomNPCsScheduler;
import java.io.IOException;
import java.util.List;
@@ -31,7 +32,32 @@ public class Server {
}
return true;
}
public static void sendDataDelayed(final EntityPlayerMP player, final EnumPacketClient type, int delay, final Object... obs) {
CustomNPCsScheduler.runTack(() -> {
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
try {
if (!fillBuffer(buffer, type, obs))
return;
CustomNpcs.Channel.sendTo(new FMLProxyPacket(buffer, "CustomNPCs"), player);
} catch (IOException e) {
LogWriter.error(type + " Errored", e);
}
}, delay);
}
public static boolean sendDataChecked(EntityPlayerMP player, EnumPacketClient type, Object... obs) {
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
try {
if(!fillBuffer(buffer, type, obs))
return false;
CustomNpcs.Channel.sendTo(new FMLProxyPacket(buffer, "CustomNPCs"), player);
} catch (IOException e) {
LogWriter.error(type + " Errored", e);
}
return true;
}
public static void sendAssociatedData(Entity entity, EnumPacketClient enu, Object... obs) {
ByteBuf buffer = Unpooled.buffer();
try {
@@ -13,6 +13,7 @@ import net.minecraft.util.IChatComponent;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.controllers.data.*;
import noppes.npcs.util.CustomNPCsScheduler;
import noppes.npcs.util.NBTJsonUtil;
import org.lwjgl.Sys;
@@ -26,7 +27,6 @@ import java.util.zip.GZIPInputStream;
public class PlayerDataController {
public static PlayerDataController instance;
public HashMap<String, String> nameUUIDs;
private static final Executor playerDataThread = Executors.newSingleThreadExecutor();
public PlayerDataController(){
instance = this;
@@ -257,13 +257,14 @@ public class PlayerDataController {
return new NBTTagCompound();
}
public void savePlayerData(PlayerData data){
public synchronized void savePlayerData(PlayerData data){
NBTTagCompound compound = data.getNBT();
playerDataThread.execute(() -> {
String filename = data.uuid + ".json";
String filename = data.uuid + ".json";
CustomNPCsScheduler.runTack(() -> {
try {
File saveDir = getSaveDir();
File file = new File(saveDir, filename+"_new");
File file = new File(saveDir, filename + "_new");
File file1 = new File(saveDir, filename);
NBTJsonUtil.SaveFile(file, compound);
if(file1.exists()){
@@ -0,0 +1,17 @@
package noppes.npcs.util;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CustomNPCsScheduler {
private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
public static void runTack(Runnable task, int delay){
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
}
public static void runTack(Runnable task){
executor.schedule(task, 0, TimeUnit.MILLISECONDS);
}
}
@@ -5,6 +5,7 @@ import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import noppes.npcs.CustomItems;
import noppes.npcs.client.gui.player.GuiFaction;
import noppes.npcs.util.CustomNPCsScheduler;
public class InventoryTabFactions extends AbstractTab {
public InventoryTabFactions() {
@@ -15,19 +16,10 @@ public class InventoryTabFactions extends AbstractTab {
@Override
public void onTabClicked() {
Thread t = new Thread(){
@Override
public void run(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Minecraft mc = Minecraft.getMinecraft();
mc.displayGuiScreen(new GuiFaction());
}
};
t.start();
CustomNPCsScheduler.runTack(() -> {
Minecraft mc = Minecraft.getMinecraft();
mc.displayGuiScreen(new GuiFaction());
}, 100);
}
@Override
@@ -5,7 +5,9 @@ import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.client.C0DPacketCloseWindow;
import noppes.npcs.CustomItems;
import noppes.npcs.client.gui.player.GuiFaction;
import noppes.npcs.client.gui.player.GuiQuestLog;
import noppes.npcs.util.CustomNPCsScheduler;
public class InventoryTabQuests extends AbstractTab {
public InventoryTabQuests() {
@@ -17,19 +19,10 @@ public class InventoryTabQuests extends AbstractTab {
@Override
public void onTabClicked() {
Thread t = new Thread(){
@Override
public void run(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Minecraft mc = Minecraft.getMinecraft();
mc.displayGuiScreen(new GuiQuestLog(mc.thePlayer));
}
};
t.start();
CustomNPCsScheduler.runTack(() -> {
Minecraft mc = Minecraft.getMinecraft();
mc.displayGuiScreen(new GuiQuestLog(mc.thePlayer));
}, 100);
}
@Override