From 4ab5e043d59bed45fdc2df47f3f4879e3dad0114 Mon Sep 17 00:00:00 2001
From: Ghost_chu <2908803755@qq.com>
Date: Thu, 6 May 2021 17:00:21 +0800
Subject: [PATCH] Calendar(Schedule) system for features #865 and #469
---
.../org/maxgamer/quickshop/QuickShop.java | 10 +-
.../quickshop/event/CalendarEvent.java | 35 +++++
.../quickshop/watcher/CalendarWatcher.java | 132 ++++++++++++++++++
3 files changed, 175 insertions(+), 2 deletions(-)
create mode 100644 src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java
create mode 100644 src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java
diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java
index 6576374198..12e9065672 100644
--- a/src/main/java/org/maxgamer/quickshop/QuickShop.java
+++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java
@@ -247,6 +247,8 @@ public class QuickShop extends JavaPlugin {
private String currency = null;
@Getter
private ShopControlPanel shopControlPanelManager;
+ @Getter
+ private CalendarWatcher calendarWatcher;
@NotNull
public static QuickShop getInstance() {
@@ -541,6 +543,7 @@ public void onLoad() {
public void onDisable() {
this.integrationHelper.callIntegrationsUnload(IntegrateStage.onUnloadBegin);
getLogger().info("QuickShop is finishing remaining work, this may need a while...");
+ calendarWatcher.stop();
Util.debugLog("Unloading all shops...");
try {
this.getShopManager().getLoadedShops().forEach(Shop::onUnload);
@@ -808,7 +811,8 @@ public void run() {
loadEcon();
}
}.runTaskLater(this, 1);
- Util.debugLog("Registering shop watcher...");
+ Util.debugLog("Registering watchers...");
+ calendarWatcher = new CalendarWatcher(this);
// shopVaildWatcher.runTaskTimer(this, 0, 20 * 60); // Nobody use it
signUpdateWatcher.runTaskTimer(this, 0, 10);
shopContainerWatcher.runTaskTimer(this, 0, 5); // Nobody use it
@@ -826,6 +830,8 @@ public void run() {
getLogger().info("Ongoing fee feature is enabled.");
ongoingFeeWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.ongoing-fee.ticks"));
}
+
+
integrationHelper.searchAndRegisterPlugins();
this.integrationHelper.callIntegrationsLoad(IntegrateStage.onEnableAfter);
new BukkitRunnable() {
@@ -840,7 +846,7 @@ public void run() {
} else {
loaded = true;
}
-
+ calendarWatcher.start();
Util.debugLog("Now using display-type: " + DisplayItem.getNowUsing().name());
// sentryErrorReporter.sendError(new IllegalAccessError("no fucking way"));
}
diff --git a/src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java b/src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java
new file mode 100644
index 0000000000..8b5448745e
--- /dev/null
+++ b/src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java
@@ -0,0 +1,35 @@
+/*
+ * This file is a part of project QuickShop, the name is CalendarEvent.java
+ * Copyright (C) PotatoCraft Studio and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+package org.maxgamer.quickshop.event;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+@EqualsAndHashCode(callSuper = true)
+@AllArgsConstructor
+@Data
+public class CalendarEvent extends QSEvent {
+ private CalendarTriggerType calendarTriggerType;
+
+ public enum CalendarTriggerType {
+ SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR, NOTHING_CHANGED
+ }
+}
diff --git a/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java b/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java
new file mode 100644
index 0000000000..3062abd236
--- /dev/null
+++ b/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java
@@ -0,0 +1,132 @@
+/*
+ * This file is a part of project QuickShop, the name is CalendarWatcher.java
+ * Copyright (C) PotatoCraft Studio and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+package org.maxgamer.quickshop.watcher;
+
+import lombok.Getter;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.maxgamer.quickshop.QuickShop;
+import org.maxgamer.quickshop.event.CalendarEvent;
+import org.maxgamer.quickshop.util.Util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.logging.Level;
+
+public class CalendarWatcher extends TimerTask {
+ @Getter
+ private final File calendarFile = new File(Util.getCacheFolder(), "calendar.cache");
+ @Getter
+ private final YamlConfiguration configuration;
+ private final QuickShop plugin;
+ @Getter
+ private final Timer timer = new Timer("QuickShop Calendar Watcher");
+
+ public CalendarWatcher(QuickShop plugin) {
+ this.plugin = plugin;
+ if (!calendarFile.exists()) {
+ try {
+ calendarFile.createNewFile();
+ } catch (IOException ioException) {
+ plugin.getLogger().log(Level.WARNING, "Cannot create calendar cache file at " + calendarFile.getAbsolutePath() + ", scheduled tasks may cannot or execute wrongly!", ioException);
+ }
+ }
+ configuration = YamlConfiguration.loadConfiguration(calendarFile);
+
+ }
+
+ public void start() {
+ timer.schedule(this, 60 * 1000);
+ }
+
+ public void stop() {
+ this.timer.cancel();
+ }
+
+ public CalendarEvent.CalendarTriggerType getAndUpdate() {
+ Calendar c = Calendar.getInstance();
+ CalendarEvent.CalendarTriggerType type = CalendarEvent.CalendarTriggerType.NOTHING_CHANGED;
+ int secondRecord = configuration.getInt("second");
+ int minuteRecord = configuration.getInt("minute");
+ int hourRecord = configuration.getInt("hour");
+ int dayRecord = configuration.getInt("day");
+ int weekRecord = configuration.getInt("week");
+ int monthRecord = configuration.getInt("month");
+ int yearRecord = configuration.getInt("year");
+ int secondNow = c.get(Calendar.SECOND);
+ int minuteNow = c.get(Calendar.MINUTE);
+ int hourNow = c.get(Calendar.HOUR_OF_DAY);
+ int dayNow = c.get(Calendar.DAY_OF_MONTH);
+ int weekNow = c.get(Calendar.WEEK_OF_MONTH);
+ int monthNow = c.get(Calendar.MONTH);
+ int yearNow = c.get(Calendar.YEAR);
+ if (secondNow != secondRecord)
+ type = CalendarEvent.CalendarTriggerType.SECOND;
+ if (minuteNow != minuteRecord)
+ type = CalendarEvent.CalendarTriggerType.MINUTE;
+ if (hourNow != hourRecord)
+ type = CalendarEvent.CalendarTriggerType.HOUR;
+ if (dayNow != dayRecord)
+ type = CalendarEvent.CalendarTriggerType.DAY;
+ if (weekNow != weekRecord)
+ type = CalendarEvent.CalendarTriggerType.WEEK;
+ if (monthNow != monthRecord)
+ type = CalendarEvent.CalendarTriggerType.MONTH;
+ if (yearNow != yearRecord)
+ type = CalendarEvent.CalendarTriggerType.YEAR;
+
+
+ configuration.set("second", secondNow);
+ configuration.set("minute", minuteNow);
+ configuration.set("hour", hourNow);
+ configuration.set("day", dayNow);
+ configuration.set("week", weekNow);
+ configuration.set("month", monthNow);
+ configuration.set("year", yearNow);
+ //We can use ordinal() to check we need update or not
+ //If we need update every minute, use type.ordinal() >= MINUTE
+
+
+ if (type.ordinal() >= CalendarEvent.CalendarTriggerType.HOUR.ordinal()) {
+ save();
+ }
+
+ return type;
+ }
+
+ public void save() {
+ try {
+ configuration.save(calendarFile);
+ } catch (IOException ioException) {
+ plugin.getLogger().log(Level.WARNING, "Cannot save calendar cache file at " + calendarFile.getAbsolutePath() + ", scheduled tasks may cannot or execute wrongly!", ioException);
+ }
+ }
+
+ /**
+ * The action to be performed by this timer task.
+ */
+ @Override
+ public void run() {
+ Bukkit.getPluginManager().callEvent(new CalendarEvent(getAndUpdate()));
+ }
+}