Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import org.bukkit.configuration.file.FileConfiguration;

import com.bencodez.advancedcore.api.user.usercache.UserDataCache;
import com.bencodez.advancedcore.api.user.usercache.change.UserDataChangeInt;
import com.bencodez.advancedcore.api.user.usercache.change.UserDataChangeString;
import com.bencodez.advancedcore.bukkit.user.BukkitUserDataReadContext;
import com.bencodez.advancedcore.core.user.UserDataReader;
import com.bencodez.advancedcore.thread.FileThread;
import com.bencodez.simpleapi.array.ArrayUtils;
import com.bencodez.simpleapi.sql.Column;
Expand All @@ -26,9 +27,11 @@ public class UserData {
private HashMap<String, DataValue> tempCache;

private AdvancedCoreUser user;
private final UserDataReader reader;

public UserData(AdvancedCoreUser user) {
this.user = user;
this.reader = new UserDataReader(new BukkitUserDataReadContext(this, user, () -> tempCache));
}

public void clearTempCache() {
Expand Down Expand Up @@ -129,126 +132,7 @@ public int getInt(String key, int def, boolean useCache, boolean waitForCache) {

@SuppressWarnings("deprecation")
public int getInt(UserStorage storage, String key, int def, UserDataFetchMode mode) {
if (key == null || key.isEmpty()) {
if (storage.equals(UserStorage.FLAT)) {
try {
return getData(user.getUUID()).getInt(key, def);
} catch (Exception ignored) {
}
}
return def;
}

// 1) Temp cache
if (mode.allowTempCache() && tempCache != null) {
DataValue v = tempCache.get(key);
if (v != null) {
if (v.isInt()) {
return v.getInt();
}
if (v.isString()) {
try {
return Integer.parseInt(v.getString());
} catch (Exception ignored) {
}
}
} else {
// If temp cache is enabled but key is absent, keep old behavior (return def)
// ONLY when temp cache is the only allowed source.
if (!mode.allowUserCache() && !mode.allowStorageLookup()) {
return def;
}
}
}

// 2) UserDataCache
if (mode.allowUserCache()) {
UserDataCache cache = user.getCache();
if (cache != null) {
// preserve previous behavior
user.cacheIfNeeded();

if (cache.isCached(key)) {
DataValue cv = cache.getCache().get(key);
if (cv != null) {
if (cv.isInt()) {
return cv.getInt();
}
String str = cv.getString();
if (str != null && !str.equalsIgnoreCase("null")) {
try {
return Integer.parseInt(str);
} catch (Exception ignored) {
}
}
}
}
} else {
user.cache();
}

if (!mode.allowStorageLookup()) {
return def;
}
} else {
if (!mode.allowStorageLookup()) {
return def;
}
}

// 3) Storage lookup
if (storage.equals(UserStorage.SQLITE)) {
List<Column> row = getSQLiteRow();
if (row != null) {
for (Column element : row) {
if (element.getName().equals(key)) {
DataValue value = element.getValue();
if (value.isInt()) {
return value.getInt();
}
if (value.isString()) {
String str = value.getString();
if (str != null) {
try {
return Integer.parseInt(str);
} catch (Exception ignored) {
}
}
return def;
}
}
}
}
} else if (storage.equals(UserStorage.MYSQL)) {
List<Column> row = getMySqlRow();
if (row != null) {
for (Column element : row) {
if (element.getName().equals(key)) {
DataValue value = element.getValue();
if (value.isInt()) {
return value.getInt();
}
if (value.isString()) {
String str = value.getString();
if (str != null) {
try {
return Integer.parseInt(str);
} catch (Exception ignored) {
}
}
return def;
}
}
}
}
} else if (storage.equals(UserStorage.FLAT)) {
try {
return getData(user.getUUID()).getInt(key, def);
} catch (Exception ignored) {
}
}

return def;
return reader.getInt(storage, key, def, mode);
}

/**
Expand Down Expand Up @@ -321,81 +205,7 @@ public String getString(String key, UserDataFetchMode mode) {

@SuppressWarnings("deprecation")
public String getString(UserStorage storage, String key, UserDataFetchMode mode) {
if (key == null || key.isEmpty()) {
return "";
}

// 1) Temp cache
if (mode.allowTempCache() && tempCache != null) {
DataValue v = tempCache.get(key);
if (v != null) {
if (v.isString() || v.isBoolean()) {
String str = v.getString();
return (str != null) ? str : "";
}
} else {
if (!mode.allowUserCache() && !mode.allowStorageLookup()) {
return "";
}
}
}

// 2) UserDataCache
if (mode.allowUserCache()) {
UserDataCache cache = user.getCache();
if (cache != null) {
if (cache.isCached(key)) {
DataValue cv = cache.getCache().get(key);
if (cv != null) {
String str = cv.getString();
return (str != null) ? str : "";
}
return "";
}
} else {
user.cache();
}

if (!mode.allowStorageLookup()) {
return "";
}
} else {
if (!mode.allowStorageLookup()) {
return "";
}
}

// 3) Storage lookup
if (storage.equals(UserStorage.SQLITE)) {
List<Column> row = getSQLiteRow();
if (row != null) {
for (Column element : row) {
if (element.getName().equals(key)
&& (element.getValue().isString() || element.getValue().isBoolean())) {
String st = element.getValue().getString();
return (st != null && !st.equalsIgnoreCase("null")) ? st : "";
}
}
}
} else if (storage.equals(UserStorage.MYSQL)) {
List<Column> row = getMySqlRow();
if (row != null) {
for (Column element : row) {
if (element.getName().equals(key)
&& (element.getValue().isString() || element.getValue().isBoolean())) {
String st = element.getValue().getString();
return (st != null && !st.equalsIgnoreCase("null")) ? st : "";
}
}
}
} else if (storage.equals(UserStorage.FLAT)) {
try {
return getData(user.getUUID()).getString(key, "");
} catch (Exception ignored) {
}
}

return "";
return reader.getString(storage, key, mode);
}

/**
Expand Down Expand Up @@ -745,4 +555,4 @@ public void updateCacheWithTemp() {
public void updateTempCacheWithColumns(ArrayList<Column> cols) {
tempCache = convert(cols);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.bencodez.advancedcore.bukkit.user;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.bencodez.advancedcore.api.user.AdvancedCoreUser;
import com.bencodez.advancedcore.api.user.UserData;
import com.bencodez.advancedcore.api.user.usercache.UserDataCache;
import com.bencodez.advancedcore.core.user.UserDataReadContext;
import com.bencodez.simpleapi.sql.Column;
import com.bencodez.simpleapi.sql.data.DataValue;

/** Bridges the existing facade, cache objects and row providers without copying their state. */
public final class BukkitUserDataReadContext implements UserDataReadContext {
private final UserData facade;
private final AdvancedCoreUser user;
private final Supplier<? extends Map<String, DataValue>> temporary;

public BukkitUserDataReadContext(UserData facade, AdvancedCoreUser user,
Supplier<? extends Map<String, DataValue>> temporary) {
this.facade = Objects.requireNonNull(facade, "facade");
// UserData historically permits construction with a null user when only
// temporary-data operations are needed. Do not eagerly dereference it.
this.user = user;
this.temporary = Objects.requireNonNull(temporary, "temporary");
}

@Override public Map<String, DataValue> tempCache() { return temporary.get(); }
@Override public Cache userCache() {
UserDataCache cache = user.getCache();
if (cache == null) return null;
// Retain this exact cache object across cacheIfNeeded, as before.
return new Cache() {
public boolean isCached(String key) { return cache.isCached(key); }
public DataValue get(String key) { return cache.getCache().get(key); }
};
}
@Override public void cacheIfNeeded() { user.cacheIfNeeded(); }
@Override public void cache() { user.cache(); }
@Override public List<Column> sqliteRow() { return facade.getSQLiteRow(); }
@Override public List<Column> mysqlRow() { return facade.getMySqlRow(); }
@SuppressWarnings("deprecation")
@Override public int flatInt(String key, int fallback) {
return facade.getData(user.getUUID()).getInt(key, fallback);
}
@SuppressWarnings("deprecation")
@Override public String flatString(String key) {
return facade.getData(user.getUUID()).getString(key, "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bencodez.advancedcore.core.user;

import java.util.List;
import java.util.Map;

import com.bencodez.simpleapi.sql.Column;
import com.bencodez.simpleapi.sql.data.DataValue;

/** Existing user/cache/storage access, supplied lazily without owning a second cache. */
public interface UserDataReadContext {
interface Cache {
boolean isCached(String key);
DataValue get(String key);
}

Map<String, DataValue> tempCache();
Cache userCache();
void cacheIfNeeded();
void cache();
List<Column> sqliteRow();
List<Column> mysqlRow();
int flatInt(String key, int fallback);
String flatString(String key);
}
Loading
Loading