Merge remote-tracking branch 'origin/master'

This commit is contained in:
Ghost_chu committed 2021-06-17 16:22:22 +08:00
commit a9657ec103
7 files changed
+108 -64

No files matched your search

@@ -947,7 +947,7 @@ public class QuickShop extends JavaPlugin {
calendarWatcher = new CalendarWatcher(this);
calendarWatcher.start();
Util.debugLog("Now using display-type: " + DisplayItem.getNowUsing().name());
getLogger().info("QuickShop Loaded! " + enableTimer.endTimer() + " ms.");
getLogger().info("QuickShop Loaded! " + enableTimer.stopAndGetTimePassed() + " ms.");
}
private void loadItemMatcher() {
@@ -171,7 +171,7 @@ public class DatabaseManager {
}
task.run(connection);
long tookTime = timer.endTimer();
long tookTime = timer.stopAndGetTimePassed();
if (tookTime > 300) {
warningSender.sendWarn(
"Database performance warning: It took too long time ("
@@ -183,7 +183,7 @@ public class DatabaseManager {
connection.commit();
connection.setAutoCommit(true);
}
long tookTime = ctimer.endTimer();
long tookTime = ctimer.stopAndGetTimePassed();
if (tookTime > 5500) {
warningSender.sendWarn(
"Database performance warning: It took too long time ("
@@ -821,7 +821,8 @@ public class ContainerShop implements Shop {
return;
}
this.isLoaded = true;
plugin.getShopManager().loadShop(this.getLocation().getWorld().getName(), this);
//Shop manger done this already
//plugin.getShopManager().loadShop(this.getLocation().getWorld().getName(), this);
plugin.getShopManager().getLoadedShops().add(this);
plugin.getShopContainerWatcher().scheduleCheck(this);
@@ -53,15 +53,13 @@ import java.util.logging.Logger;
public class ShopLoader {
private final List<Long> loadTimes = new ArrayList<>();
private final Map<Timer, Double> costCache = new HashMap<>();
private final Map<Timer, Double> timeCostCache = new HashMap<>();
private final QuickShop plugin;
/* This may contains broken shop, must use null check before load it. */
private final List<Shop> shopsInDatabase = new CopyOnWriteArrayList<>();
private final List<ShopDatabaseInfoOrigin> originShopsInDatabase = new CopyOnWriteArrayList<>();
private final List<ShopRawDatabaseInfo> shopRawDatabaseInfoList = new CopyOnWriteArrayList<>();
private int errors;
private int loadAfterChunkLoaded = 0;
private int loadAfterWorldLoaded = 0;
private int totalLoaded = 0;
//private final WarningSender warningSender;
@@ -80,28 +78,36 @@ public class ShopLoader {
}
/**
* Load all shops
* Load all shops in the specified world
*
* @param worldName The world name
* @param worldName The world name, null if load all shops
*/
public void loadShops(@Nullable String worldName) {
//boolean backupedDatabaseInDeleteProcess = false;
Timer totalLoadTimer = new Timer(true);
this.plugin.getLogger().info("Loading shops from the database...");
Timer fetchTimer = new Timer(true);
int loadAfterChunkLoaded = 0;
int loadAfterWorldLoaded = 0;
try (WarpedResultSet warpRS = plugin.getDatabaseHelper().selectAllShops(); ResultSet rs = warpRS.getResultSet()) {
this.plugin
.getLogger()
.info("Used " + fetchTimer.endTimer() + "ms to fetch all shops from the database.");
.info("Used " + fetchTimer.stopAndGetTimePassed() + "ms to fetch all shops from the database.");
while (rs.next()) {
Timer singleShopLoadTimer = new Timer(true);
ShopDatabaseInfoOrigin origin = new ShopDatabaseInfoOrigin(rs);
originShopsInDatabase.add(origin);
ShopRawDatabaseInfo origin = new ShopRawDatabaseInfo(rs);
shopRawDatabaseInfoList.add(origin);
if (worldName != null && !origin.getWorld().equals(worldName)) {
singleShopLoaded(singleShopLoadTimer);
continue;
}
ShopDatabaseInfo data = new ShopDatabaseInfo(origin);
//World unloaded and not found
if (data.getWorld() == null) {
++loadAfterWorldLoaded;
singleShopLoaded(singleShopLoadTimer);
continue;
}
Shop shop =
new ContainerShop(plugin,
data.getLocation(),
@@ -115,7 +121,7 @@ public class ShopLoader {
shop.setDirty();
}
shopsInDatabase.add(shop);
this.costCalc(singleShopLoadTimer);
this.calcTimeCost(singleShopLoadTimer);
if (shopNullCheck(shop)) {
if (plugin.getConfig().getBoolean("debug.delete-corrupt-shops", false)) {
plugin.getLogger().warning("Deleting shop " + shop + " caused by corrupted.");
@@ -124,7 +130,12 @@ public class ShopLoader {
Util.debugLog("Trouble database loading debug: " + data);
Util.debugLog("Somethings gone wrong, skipping the loading...");
}
loadAfterWorldLoaded++;
singleShopLoaded(singleShopLoadTimer);
continue;
}
//World unloaded but found
if (!Util.isWorldLoaded(shop.getLocation())) {
++loadAfterWorldLoaded;
singleShopLoaded(singleShopLoadTimer);
continue;
}
@@ -148,7 +159,7 @@ public class ShopLoader {
}
singleShopLoaded(singleShopLoadTimer);
}
long totalUsedTime = totalLoadTimer.endTimer();
long totalUsedTime = totalLoadTimer.stopAndGetTimePassed();
long avgPerShop = mean(loadTimes.toArray(new Long[0]));
this.plugin
.getLogger()
@@ -160,9 +171,9 @@ public class ShopLoader {
+ "ms, Avg "
+ avgPerShop
+ "ms per shop)");
this.plugin.getLogger().info(this.loadAfterChunkLoaded
this.plugin.getLogger().info(loadAfterChunkLoaded
+ " shops will load after chunk have loaded, "
+ this.loadAfterWorldLoaded
+ loadAfterWorldLoaded
+ " shops will load after the world has loaded.");
} catch (Exception e) {
exceptionHandler(e, null);
@@ -171,7 +182,7 @@ public class ShopLoader {
private void singleShopLoaded(@NotNull Timer singleShopLoadTimer) {
totalLoaded++;
long singleShopLoadTime = singleShopLoadTimer.endTimer();
long singleShopLoadTime = singleShopLoadTimer.stopAndGetTimePassed();
loadTimes.add(singleShopLoadTime);
Util.debugLog("Loaded shop used time " + singleShopLoadTime + "ms");
// if (singleShopLoadTime > 1500) {
@@ -179,35 +190,31 @@ public class ShopLoader {
// }
}
private double costCalc(@NotNull Timer timer) {
costCache.putIfAbsent(timer, (double) timer.getTimer());
return timer.getTimer() - costCache.get(timer);
private double calcTimeCost(@NotNull Timer timer) {
timeCostCache.putIfAbsent(timer, (double) timer.getPassedTime());
return timer.getPassedTime() - timeCostCache.get(timer);
}
@SuppressWarnings("ConstantConditions")
private boolean shopNullCheck(@Nullable Shop shop) {
if (shop == null) {
Util.debugLog("Shop Object is null");
Util.debugLog("Shop object is null");
return true;
}
if (shop.getItem() == null) {
Util.debugLog("Shop ItemStack is null");
Util.debugLog("Shop itemStack is null");
return true;
}
if (shop.getItem().getType() == Material.AIR) {
Util.debugLog("Shop ItemStack type can't be AIR");
Util.debugLog("Shop itemStack type can't be AIR");
return true;
}
if (shop.getLocation() == null) {
Util.debugLog("Shop Location is null");
return true;
}
if (shop.getLocation().getWorld() == null) {
Util.debugLog("Shop World is null");
Util.debugLog("Shop location is null");
return true;
}
if (shop.getOwner() == null) {
Util.debugLog("Shop Owner is null");
Util.debugLog("Shop owner is null");
return true;
}
if (plugin.getServer().getOfflinePlayer(shop.getOwner()).getName() == null) {
@@ -294,8 +301,8 @@ public class ShopLoader {
String shopStr = shopsPlain[i].trim();
boolean success = false;
try {
ShopDatabaseInfoOrigin shopDatabaseInfoOrigin = gson.fromJson(shopStr, ShopDatabaseInfoOrigin.class);
originShopsInDatabase.add(shopDatabaseInfoOrigin);
ShopRawDatabaseInfo shopDatabaseInfoOrigin = gson.fromJson(shopStr, ShopRawDatabaseInfo.class);
shopRawDatabaseInfoList.add(shopDatabaseInfoOrigin);
ShopDatabaseInfo data = new ShopDatabaseInfo(shopDatabaseInfoOrigin);
Shop shop =
new ContainerShop(plugin,
@@ -331,12 +338,12 @@ public class ShopLoader {
public void removeShopFromShopLoader(Shop shop) {
if (this.shopsInDatabase.remove(shop)) {
for (ShopDatabaseInfoOrigin origin : this.originShopsInDatabase) {
if (Objects.equals(shop.getLocation().getWorld().getName(), origin.getWorld())) {
if (shop.getLocation().getBlockX() == origin.getX()) {
if (shop.getLocation().getBlockY() == origin.getY()) {
if (shop.getLocation().getBlockZ() == origin.getZ()) {
this.originShopsInDatabase.remove(origin);
for (ShopRawDatabaseInfo rawDatabaseInfo : this.shopRawDatabaseInfoList) {
if (Objects.equals(shop.getLocation().getWorld().getName(), rawDatabaseInfo.getWorld())) {
if (shop.getLocation().getBlockX() == rawDatabaseInfo.getX()) {
if (shop.getLocation().getBlockY() == rawDatabaseInfo.getY()) {
if (shop.getLocation().getBlockZ() == rawDatabaseInfo.getZ()) {
this.shopRawDatabaseInfoList.remove(rawDatabaseInfo);
break;
}
}
@@ -347,13 +354,13 @@ public class ShopLoader {
}
@NotNull
public List<ShopDatabaseInfoOrigin> getOriginShopsInDatabase() {
return new ArrayList<>(originShopsInDatabase);
public List<ShopRawDatabaseInfo> getOriginShopsInDatabase() {
return new ArrayList<>(shopRawDatabaseInfoList);
}
@Getter
@Setter
static public class ShopDatabaseInfoOrigin {
static public class ShopRawDatabaseInfo {
private String item;
private String moderators;
@@ -374,7 +381,7 @@ public class ShopLoader {
private String extra;
ShopDatabaseInfoOrigin(ResultSet rs) throws SQLException {
ShopRawDatabaseInfo(ResultSet rs) throws SQLException {
this.x = rs.getInt("x");
this.y = rs.getInt("y");
this.z = rs.getInt("z");
@@ -391,7 +398,7 @@ public class ShopLoader {
}
}
ShopDatabaseInfoOrigin(int x, int y, int z, String world, String itemConfig, String owner, double price, int type, boolean unlimited, String extra) {
ShopRawDatabaseInfo(int x, int y, int z, String world, String itemConfig, String owner, double price, int type, boolean unlimited, String extra) {
this.x = x;
this.y = y;
this.z = z;
@@ -404,7 +411,7 @@ public class ShopLoader {
this.extra = extra;
}
ShopDatabaseInfoOrigin() {
ShopRawDatabaseInfo() {
}
@@ -441,7 +448,7 @@ public class ShopLoader {
private AtomicBoolean needUpdate = new AtomicBoolean(false);
ShopDatabaseInfo(ShopDatabaseInfoOrigin origin) {
ShopDatabaseInfo(ShopRawDatabaseInfo origin) {
try {
this.x = origin.getX();
this.y = origin.getY();
@@ -32,21 +32,23 @@ import lombok.ToString;
@ToString
public class Timer {
private long startTime;
private long passedTime;
private boolean isPaused = false;
/**
* Create a empty time, use setTimer to start
* Create a empty timer, use setTimer to start
*/
public Timer() {
}
/**
* Create a empty time, auto start if autoSet is true
* Create a empty timer, auto start if autoStart is true
*
* @param autoSet Auto set the timer
* @param autoStart Auto set the timer
*/
public Timer(boolean autoSet) {
if (autoSet) {
startTime = System.currentTimeMillis();
public Timer(boolean autoStart) {
if (autoStart) {
start();
}
}
@@ -64,27 +66,47 @@ public class Timer {
*
* @return time
*/
public long endTimer() {
long time = System.currentTimeMillis() - startTime;
public long stopAndGetTimePassed() {
long time = getPassedTime();
startTime = 0;
return time;
}
/**
* Return how long time running after atTimeS. THIS NOT WILL DESTORY AND STOP THE TIMER
* Return how long time running after a specified time. THIS NOT WILL DESTORY AND STOP THE TIMER
*
* @param atTime The inited time
* @param atTime The specified time
* @return time
*/
public long getTimerAt(long atTime) {
return atTime - startTime;
public long getPassedTimeOffsetFrom(long atTime) {
return (atTime - startTime) + passedTime;
}
/**
* Create a Timer. Time Unit: ms
* Start the timer. Time Unit: ms
*/
public void setTimer() {
public void start() {
this.startTime = System.currentTimeMillis();
isPaused = false;
}
/**
* Pause the timer. Time Unit: ms
*/
public void pause() {
this.passedTime = getPassedTime();
isPaused = true;
}
/**
* Resume the timer. Time Unit: ms
*/
public void resume() {
if (isPaused) {
this.startTime = System.currentTimeMillis() - passedTime;
passedTime = 0;
isPaused = false;
}
}
/**
@@ -92,8 +114,12 @@ public class Timer {
*
* @return time
*/
public long getTimer() {
return System.currentTimeMillis() - startTime;
public long getPassedTime() {
if (isPaused) {
return passedTime;
} else {
return System.currentTimeMillis() - startTime;
}
}
}
@@ -872,6 +872,16 @@ public class Util {
}
}
/**
* Returns true if the world of given location is loaded or not.
*
* @param loc The location containing world
* @return true if the world of given location is loaded or not.
*/
public static boolean isWorldLoaded(Location loc) {
return (getNMSVersion().contains("1_13") && loc.getWorld() != null) || loc.isWorldLoaded();
}
/**
* Returns true if the given location is loaded or not.
*
@@ -879,7 +889,7 @@ public class Util {
* @return true if the given location is loaded or not.
*/
public static boolean isLoaded(@NotNull Location loc) {
if ((!getNMSVersion().contains("1_13") && !loc.isWorldLoaded()) || loc.getWorld() == null) {
if (!isWorldLoaded(loc)) {
return false;
}
// Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT
@@ -216,7 +216,7 @@ public class CollectorAdapter {
.getShopLoader()
.getOriginShopsInDatabase()
.stream()
.map(ShopLoader.ShopDatabaseInfoOrigin::toString)
.map(ShopLoader.ShopRawDatabaseInfo::toString)
.toArray());
map.put("memory", plugin.getShopManager().getAllShops().stream().map(Shop::toString).toArray());
return map;