mirror of
https://github.com/darksaid98/nightcore-spigot.git
synced 2026-12-01 14:38:47 +00:00
synchronization fixes
This commit is contained in:
1 parent
b505735d7c
commit
454c1c92db
7 files changed
+230
-56
No files matched your search
@@ -17,6 +17,8 @@ public abstract class AbstractUser<P extends NightCorePlugin> implements DataUse
|
||||
protected long dateCreated;
|
||||
protected long lastOnline;
|
||||
protected long cachedUntil;
|
||||
protected long autoSaveIn;
|
||||
protected long nextSyncIn;
|
||||
|
||||
public AbstractUser(@NotNull P plugin, @NotNull UUID uuid, @NotNull String name, long dateCreated, long lastOnline) {
|
||||
this.plugin = plugin;
|
||||
@@ -35,18 +37,64 @@ public abstract class AbstractUser<P extends NightCorePlugin> implements DataUse
|
||||
|
||||
}
|
||||
|
||||
public boolean isCacheExpired() {
|
||||
return this.getCachedUntil() > 0 && System.currentTimeMillis() > this.getCachedUntil();
|
||||
private long getTimestamp(int seconds) {
|
||||
return seconds < 0 ? -1L : System.currentTimeMillis() + 1000L * seconds;
|
||||
}
|
||||
|
||||
public boolean isCacheExpired() {
|
||||
return this.getCachedUntil() > 0 && System.currentTimeMillis() >= this.getCachedUntil();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoSaveReady() {
|
||||
return this.getAutoSaveIn() > 0 && System.currentTimeMillis() >= this.getAutoSaveIn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSyncReady() {
|
||||
return this.getNextSyncIn() >= 0 && System.currentTimeMillis() >= this.getNextSyncIn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelAutoSave() {
|
||||
this.setAutoSaveIn(-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelSynchronization() {
|
||||
this.setNextSyncIn(-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCachedUntil() {
|
||||
return cachedUntil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCachedUntil(long cachedUntil) {
|
||||
this.cachedUntil = cachedUntil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getAutoSaveIn() {
|
||||
return autoSaveIn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAutoSaveIn(int seconds) {
|
||||
this.autoSaveIn = this.getTimestamp(seconds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getNextSyncIn() {
|
||||
return nextSyncIn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNextSyncIn(int seconds) {
|
||||
this.nextSyncIn = this.getTimestamp(seconds);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final UUID getId() {
|
||||
return this.uuid;
|
||||
|
||||
@@ -17,21 +17,24 @@ import java.util.function.Supplier;
|
||||
|
||||
public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extends DataUser> extends AbstractManager<P> {
|
||||
|
||||
private final UserdataConfig config;
|
||||
|
||||
private final Map<UUID, U> loadedById;
|
||||
private final Map<String, U> loadedByName;
|
||||
private final Set<U> scheduledSaves;
|
||||
private final Map<UUID, U> scheduledSaves;
|
||||
|
||||
public AbstractUserManager(@NotNull P plugin) {
|
||||
super(plugin);
|
||||
this.config = UserdataConfig.read(plugin);
|
||||
this.loadedById = new ConcurrentHashMap<>();
|
||||
this.loadedByName = new ConcurrentHashMap<>();
|
||||
this.scheduledSaves = ConcurrentHashMap.newKeySet();
|
||||
this.scheduledSaves = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLoad() {
|
||||
this.addListener(new UserListener<>(this.plugin));
|
||||
this.addTask(this.plugin.createAsyncTask(this::saveScheduled).setTicksInterval(20L));
|
||||
this.addTask(this.plugin.createAsyncTask(this::saveScheduled).setTicksInterval(this.config.getScheduledSaveInterval()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,15 +62,30 @@ public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extend
|
||||
public void saveScheduled() {
|
||||
if (this.scheduledSaves.isEmpty()) return;
|
||||
|
||||
this.getDataHandler().saveUsers(this.scheduledSaves);
|
||||
this.scheduledSaves.clear();
|
||||
Set<U> users = new HashSet<>();
|
||||
|
||||
this.scheduledSaves.values().forEach(user -> {
|
||||
if (user.isAutoSaveReady()) {
|
||||
users.add(user);
|
||||
}
|
||||
});
|
||||
|
||||
this.getDataHandler().saveUsers(users);
|
||||
|
||||
users.forEach(user -> {
|
||||
user.cancelAutoSave(); // Reset autosave timestamp.
|
||||
user.setNextSyncIn(this.config.getScheduledSaveSyncPause()); // Unlock synchronization only when all data was pushed to the database.
|
||||
});
|
||||
|
||||
this.scheduledSaves.values().removeAll(users);
|
||||
}
|
||||
|
||||
public void saveAll() {
|
||||
Set<U> users = new HashSet<>();
|
||||
users.addAll(this.scheduledSaves);
|
||||
users.addAll(this.scheduledSaves.values());
|
||||
users.addAll(this.getLoaded());
|
||||
this.getDataHandler().saveUsers(users);
|
||||
this.scheduledSaves.values().forEach(DataUser::cancelAutoSave);
|
||||
this.scheduledSaves.clear();
|
||||
}
|
||||
|
||||
@@ -188,41 +206,21 @@ public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extend
|
||||
@Deprecated
|
||||
public void getUserDataAndPerform(@NotNull String name, Consumer<U> consumer) {
|
||||
this.manageUserSynchronized(name, consumer);
|
||||
/*U user = this.getLoaded(name);
|
||||
if (user != null) {
|
||||
consumer.accept(user);
|
||||
}
|
||||
else this.getUserDataAsync(name).thenAccept(user2 -> this.plugin.runTask(task -> consumer.accept(user2)));*/
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void getUserDataAndPerform(@NotNull UUID uuid, Consumer<U> consumer) {
|
||||
this.manageUserSynchronized(uuid, consumer);
|
||||
/*U user = this.getLoaded(uuid);
|
||||
if (user != null) {
|
||||
consumer.accept(user);
|
||||
}
|
||||
else this.getUserDataAsync(uuid).thenAccept(user2 -> this.plugin.runTask(task -> consumer.accept(user2)));*/
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void getUserDataAndPerformAsync(@NotNull String name, Consumer<U> consumer) {
|
||||
this.manageUser(name, consumer);
|
||||
/*U user = this.getLoaded(name);
|
||||
if (user != null) {
|
||||
consumer.accept(user);
|
||||
}
|
||||
else this.getUserDataAsync(name).thenAccept(consumer);*/
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void getUserDataAndPerformAsync(@NotNull UUID uuid, Consumer<U> consumer) {
|
||||
this.manageUser(uuid, consumer);
|
||||
/*U user = this.getLoaded(uuid);
|
||||
if (user != null) {
|
||||
consumer.accept(user);
|
||||
}
|
||||
else this.getUserDataAsync(uuid).thenAccept(consumer);*/
|
||||
}
|
||||
|
||||
public final void unload(@NotNull Player player) {
|
||||
@@ -251,11 +249,13 @@ public abstract class AbstractUserManager<P extends NightDataPlugin<U>, U extend
|
||||
}
|
||||
|
||||
public void scheduleSave(@NotNull U user) {
|
||||
this.scheduledSaves.add(user);
|
||||
user.setAutoSaveIn(this.config.getScheduledSaveDelay());
|
||||
user.cancelSynchronization();
|
||||
this.scheduledSaves.put(user.getId(), user);
|
||||
}
|
||||
|
||||
public boolean isScheduledToSave(@NotNull U user) {
|
||||
return this.scheduledSaves.contains(user);
|
||||
return this.scheduledSaves.containsKey(user.getId());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
||||
@@ -15,10 +15,26 @@ public interface DataUser {
|
||||
|
||||
boolean isCacheExpired();
|
||||
|
||||
boolean isAutoSaveReady();
|
||||
|
||||
boolean isSyncReady();
|
||||
|
||||
long getCachedUntil();
|
||||
|
||||
long getAutoSaveIn();
|
||||
|
||||
long getNextSyncIn();
|
||||
|
||||
void cancelAutoSave();
|
||||
|
||||
void cancelSynchronization();
|
||||
|
||||
void setCachedUntil(long cachedUntil);
|
||||
|
||||
void setAutoSaveIn(int seconds);
|
||||
|
||||
void setNextSyncIn(int seconds);
|
||||
|
||||
@NotNull UUID getId();
|
||||
|
||||
@NotNull String getName();
|
||||
|
||||
@@ -101,7 +101,7 @@ public class DatabaseConfig {
|
||||
.read(config);
|
||||
|
||||
String mysqlHost = ConfigValue.create(path + "MySQL.Host", "localhost:3306",
|
||||
"Database address. Like http://127.0.0.1:3306/")
|
||||
"Database host. Example: localhost:3306, 127.0.0.1:3306")
|
||||
.read(config);
|
||||
|
||||
String mysqlBase = ConfigValue.create(path + "MySQL.Database", "minecraft",
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package su.nightexpress.nightcore.database;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import su.nightexpress.nightcore.NightPlugin;
|
||||
import su.nightexpress.nightcore.config.ConfigValue;
|
||||
import su.nightexpress.nightcore.config.FileConfig;
|
||||
|
||||
public class UserdataConfig {
|
||||
|
||||
private final long scheduledSaveInterval;
|
||||
private final int scheduledSaveDelay;
|
||||
private final int scheduledSaveSyncPause;
|
||||
|
||||
public UserdataConfig(long scheduledSaveInterval, int scheduledSaveDelay, int scheduledSaveSyncPause) {
|
||||
this.scheduledSaveInterval = scheduledSaveInterval;
|
||||
this.scheduledSaveDelay = scheduledSaveDelay;
|
||||
this.scheduledSaveSyncPause = scheduledSaveSyncPause;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static UserdataConfig read(@NotNull NightPlugin plugin) {
|
||||
FileConfig config = plugin.getConfig();
|
||||
|
||||
long scheduledSaveInterval = ConfigValue.create("Database.UserData.Scheduled_Save_Interval",
|
||||
20L,
|
||||
"Sets how often (in ticks) plugin will attempt to save data of users marked to be saved.",
|
||||
"This will save only users that are 'ready' to save (see 'Scheduled_Save_Delay').",
|
||||
"[DO NOT DISABLE UNDER ANY CIRCUMSTANCES!]",
|
||||
"[20 ticks = 1 second]",
|
||||
"[Default is 20]"
|
||||
).read(config);
|
||||
|
||||
int scheduledSaveDelay = ConfigValue.create("Database.UserData.Scheduled_Save_Delay",
|
||||
1,
|
||||
"Sets scheduled save delay (in seconds) for a user when marked to be saved.",
|
||||
"This means that a user will be saved X seconds later after being marked.",
|
||||
"Generally, you should keep this value less than 'Scheduled_Save_Interval' for best results,",
|
||||
"and not setting it extremely high, otherwise you may result in user data never saved & synchronized.",
|
||||
"",
|
||||
"IMPORTANT NOTE #1: When a user is marked for saving, this will prevent them from syncing until saving occurs!",
|
||||
"IMPORTANT NOTE #2: Every time a user is marked for saving, their save time will be updated with this value!",
|
||||
"",
|
||||
"Users marked for save when their data has been changed/affected in some way.",
|
||||
"However this is higly depends on the plugin.",
|
||||
"[Default is 1]"
|
||||
).read(config);
|
||||
|
||||
int scheduledSaveSynchronizationPause = ConfigValue.create("Database.UserData.Scheduled_Save_Sync_Pause",
|
||||
3,
|
||||
"Sets synchronization delay (in seconds) before the plugin can continue syncing data for a user after its scheduled save.",
|
||||
"When a user marked for saving has been saved, their synchronization will be unlocked but delayed for this value.",
|
||||
"This might be helpful to prevent possible issues when synchronization process access the database before user data was fully saved.",
|
||||
"[Default is 3]"
|
||||
).read(config);
|
||||
|
||||
|
||||
return new UserdataConfig(scheduledSaveInterval, scheduledSaveDelay, scheduledSaveSynchronizationPause);
|
||||
}
|
||||
|
||||
public long getScheduledSaveInterval() {
|
||||
return scheduledSaveInterval;
|
||||
}
|
||||
|
||||
public int getScheduledSaveDelay() {
|
||||
return scheduledSaveDelay;
|
||||
}
|
||||
|
||||
public int getScheduledSaveSyncPause() {
|
||||
return scheduledSaveSyncPause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserdataConfig{" +
|
||||
"scheduledSaveInterval=" + scheduledSaveInterval +
|
||||
", scheduledSaveDelay=" + scheduledSaveDelay +
|
||||
", scheduledSaveSyncPause=" + scheduledSaveSyncPause +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,11 @@ public class TimeUtil {
|
||||
if (!str.isEmpty()) {
|
||||
str.append(delimiter);
|
||||
}
|
||||
str.append(CoreLang.TIME_SECOND.getString().replace(Placeholders.GENERIC_AMOUNT, String.valueOf(seconds)));
|
||||
|
||||
boolean doDecimal = seconds < 5 && str.isEmpty();
|
||||
String secondsStr = doDecimal ? NumberUtil.format((double) time / 1000D) : String.valueOf(seconds);
|
||||
|
||||
str.append(CoreLang.TIME_SECOND.getString().replace(Placeholders.GENERIC_AMOUNT, secondsStr));
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
|
||||
@@ -12,7 +12,7 @@ import su.nightexpress.nightcore.util.StringUtil;
|
||||
public class UniParticle {
|
||||
|
||||
private final Particle particle;
|
||||
private final Object data;
|
||||
private Object data;
|
||||
|
||||
public UniParticle(@Nullable Particle particle, @Nullable Object data) {
|
||||
this.particle = particle;
|
||||
@@ -65,70 +65,70 @@ public class UniParticle {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static UniParticle read(@NotNull FileConfig cfg, @NotNull String path) {
|
||||
String name = cfg.getString(path + ".Name", "");
|
||||
public static UniParticle read(@NotNull FileConfig config, @NotNull String path) {
|
||||
String name = config.getString(path + ".Name", "");
|
||||
Particle particle = StringUtil.getEnum(name, Particle.class).orElse(null);
|
||||
if (particle == null) return UniParticle.of(null);
|
||||
|
||||
Class<?> dataType = particle.getDataType();
|
||||
Object data = null;
|
||||
if (dataType == BlockData.class) {
|
||||
Material material = Material.getMaterial(cfg.getString(path + ".Material", ""));
|
||||
Material material = Material.getMaterial(config.getString(path + ".Material", ""));
|
||||
data = material != null ? material.createBlockData() : Material.STONE.createBlockData();
|
||||
}
|
||||
else if (dataType == Particle.DustOptions.class) {
|
||||
Color color = StringUtil.getColor(cfg.getString(path + ".Color", ""));
|
||||
double size = cfg.getDouble(path + ".Size", 1D);
|
||||
Color color = StringUtil.getColor(config.getString(path + ".Color", ""));
|
||||
double size = config.getDouble(path + ".Size", 1D);
|
||||
data = new Particle.DustOptions(color, (float) size);
|
||||
}
|
||||
else if (dataType == Particle.DustTransition.class) {
|
||||
Color colorStart = StringUtil.getColor(cfg.getString(path + ".Color_From", ""));
|
||||
Color colorEnd = StringUtil.getColor(cfg.getString(path + ".Color_To", ""));
|
||||
double size = cfg.getDouble(path + ".Size", 1D);
|
||||
Color colorStart = StringUtil.getColor(config.getString(path + ".Color_From", ""));
|
||||
Color colorEnd = StringUtil.getColor(config.getString(path + ".Color_To", ""));
|
||||
double size = config.getDouble(path + ".Size", 1D);
|
||||
data = new Particle.DustTransition(colorStart, colorEnd, (float) size);
|
||||
}
|
||||
else if (dataType == ItemStack.class) {
|
||||
ItemStack item = cfg.getItem(path + ".Item");
|
||||
ItemStack item = config.getItem(path + ".Item");
|
||||
data = item.getType().isAir() ? new ItemStack(Material.STONE) : item;
|
||||
}
|
||||
else if (dataType == Float.class) {
|
||||
data = (float) cfg.getDouble(path + ".floatValue", 1F);
|
||||
data = (float) config.getDouble(path + ".floatValue", 1F);
|
||||
}
|
||||
else if (dataType == Integer.class) {
|
||||
data = cfg.getInt(path + ".intValue", 1);
|
||||
data = config.getInt(path + ".intValue", 1);
|
||||
}
|
||||
else if (dataType != Void.class) return UniParticle.of(Particle.REDSTONE);
|
||||
else if (dataType != Void.class) return UniParticle.of(Particle.CLOUD);
|
||||
|
||||
return UniParticle.of(particle, data);
|
||||
}
|
||||
|
||||
public void write(@NotNull FileConfig cfg, @NotNull String path) {
|
||||
cfg.set(path + ".Name", this.isEmpty() ? "null" : this.getParticle().name());
|
||||
public void write(@NotNull FileConfig config, @NotNull String path) {
|
||||
config.set(path + ".Name", this.isEmpty() ? "null" : this.getParticle().name());
|
||||
|
||||
Object data = this.getData();
|
||||
if (data instanceof BlockData blockData) {
|
||||
cfg.set(path + ".Material", blockData.getMaterial().name());
|
||||
config.set(path + ".Material", blockData.getMaterial().name());
|
||||
}
|
||||
else if (data instanceof Particle.DustTransition dustTransition) {
|
||||
Color colorStart = dustTransition.getColor();
|
||||
Color colorEnd = dustTransition.getToColor();
|
||||
cfg.set(path + ".Color_From", colorStart.getRed() + "," + colorStart.getGreen() + "," + colorStart.getBlue());
|
||||
cfg.set(path + ".Color_To", colorEnd.getRed() + "," + colorEnd.getGreen() + "," + colorEnd.getBlue());
|
||||
cfg.set(path + ".Size", dustTransition.getSize());
|
||||
config.set(path + ".Color_From", colorStart.getRed() + "," + colorStart.getGreen() + "," + colorStart.getBlue());
|
||||
config.set(path + ".Color_To", colorEnd.getRed() + "," + colorEnd.getGreen() + "," + colorEnd.getBlue());
|
||||
config.set(path + ".Size", dustTransition.getSize());
|
||||
}
|
||||
else if (data instanceof Particle.DustOptions dustOptions) {
|
||||
Color color = dustOptions.getColor();
|
||||
cfg.set(path + ".Color", color.getRed() + "," + color.getGreen() + "," + color.getBlue());
|
||||
cfg.set(path + ".Size", dustOptions.getSize());
|
||||
config.set(path + ".Color", color.getRed() + "," + color.getGreen() + "," + color.getBlue());
|
||||
config.set(path + ".Size", dustOptions.getSize());
|
||||
}
|
||||
else if (data instanceof ItemStack item) {
|
||||
cfg.setItem(path + ".Item", item);
|
||||
config.setItem(path + ".Item", item);
|
||||
}
|
||||
else if (data instanceof Float f) {
|
||||
cfg.set(path + ".floatValue", f);
|
||||
config.set(path + ".floatValue", f);
|
||||
}
|
||||
else if (data instanceof Integer i) {
|
||||
cfg.set(path + ".intValue", i);
|
||||
config.set(path + ".intValue", i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +145,32 @@ public class UniParticle {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void validateData() {
|
||||
if (this.particle == null) return;
|
||||
|
||||
Class<?> dataType = this.particle.getDataType();
|
||||
if (this.data != null && this.data.getClass() == dataType) return;
|
||||
|
||||
if (dataType == BlockData.class) {
|
||||
this.data = Material.STONE.createBlockData();
|
||||
}
|
||||
else if (dataType == Particle.DustOptions.class) {
|
||||
this.data = new Particle.DustOptions(Color.WHITE, 1F);
|
||||
}
|
||||
else if (dataType == Particle.DustTransition.class) {
|
||||
this.data = new Particle.DustTransition(Color.BLACK, Color.WHITE, 1F);
|
||||
}
|
||||
else if (dataType == ItemStack.class) {
|
||||
this.data = new ItemStack(Material.STONE);
|
||||
}
|
||||
else if (dataType == Float.class) {
|
||||
this.data = 1F;
|
||||
}
|
||||
else if (dataType == Integer.class) {
|
||||
this.data = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void play(@NotNull Location location, double speed, int amount) {
|
||||
this.play(location, 0D, speed, amount);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user