Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Calendar(Schedule) system for features #865 and #469
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed May 6, 2021
1 parent 31fc107 commit 4ab5e04
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/maxgamer/quickshop/QuickShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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"));
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

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
}
}
132 changes: 132 additions & 0 deletions src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

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()));
}
}

0 comments on commit 4ab5e04

Please sign in to comment.