This repository has been archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/maxgamer/quickshop/event/CalendarEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |