-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move data to HappyDayData, suspend checks when nobody is on (#6)
- Loading branch information
Showing
6 changed files
with
247 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2019 dumptruckman | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package xyz.alicedtrh.happyday; | ||
|
||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.logging.Handler; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
|
||
/** | ||
* A logger handler for a bukkit plugin that will cause messages below | ||
* {@link Level#INFO} to be logged normally when the plugin's logger is set to | ||
* the level of the message or lower. | ||
* <p/> | ||
* To enable logging of messages below {@link Level#INFO}, the plugin's logger | ||
* must be set to an appropriately lower level. For example, to log all message | ||
* levels: <code>plugin.getLogger().setLevel(Level.ALL);</code> | ||
*/ | ||
public class DebugLogHandler extends Handler { | ||
|
||
private static final String DEFAULT_DEBUG_PREFIX_FORMAT = "[%1$s-%2$s] %3$s"; | ||
private static final String DEFAULT_DEBUG_LOG_PREFIX = "DEBUG"; | ||
|
||
/** | ||
* Attaches a DebugLogHandler to the given plugin's logger. | ||
* | ||
* @param plugin the plugin to add debug logging for. | ||
*/ | ||
public static void attachDebugLogger(Plugin plugin) { | ||
plugin.getLogger().addHandler(new DebugLogHandler(plugin)); | ||
} | ||
|
||
private final Plugin plugin; | ||
private final String pluginName; | ||
|
||
private DebugLogHandler(Plugin plugin) { | ||
this.plugin = plugin; | ||
String prefix = plugin.getDescription().getPrefix(); | ||
this.pluginName = prefix != null ? prefix : plugin.getDescription().getName(); | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
if (plugin.getLogger().getLevel().intValue() <= record.getLevel().intValue() | ||
&& record.getLevel().intValue() < Level.INFO.intValue()) { | ||
record.setLevel(Level.INFO); | ||
record.setMessage(String.format(DEFAULT_DEBUG_PREFIX_FORMAT, pluginName, DEFAULT_DEBUG_LOG_PREFIX, | ||
record.getMessage())); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
} | ||
|
||
@Override | ||
public void close() throws SecurityException { | ||
} | ||
} |
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
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,81 @@ | ||
package xyz.alicedtrh.happyday; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
|
||
public class HappyDayData { | ||
private static volatile HappyDayData instance; | ||
private Integer removedMobs = 0; | ||
private UpgradeableSpawnersDependency UpgradeableSpawners; | ||
private World world; | ||
private String _worldName; | ||
static final Long DELAY = 1200L; // 20 ticks (20tps) * 60 seconds = 1 minute | ||
private boolean suspended = false; | ||
|
||
private HappyDayData() { | ||
} | ||
|
||
public Integer getRemovedMobs() { | ||
return this.removedMobs; | ||
} | ||
|
||
public void increaseRemovedMobs() { | ||
this.removedMobs = this.removedMobs + 1; | ||
} | ||
|
||
public void resetRemovedMobs() { | ||
this.removedMobs = 0; | ||
} | ||
|
||
public UpgradeableSpawnersDependency getUpgradeableSpawners() { | ||
return UpgradeableSpawners; | ||
} | ||
|
||
public void setUpgradeableSpawners(UpgradeableSpawnersDependency upgradeableSpawners) { | ||
UpgradeableSpawners = upgradeableSpawners; | ||
} | ||
|
||
public World getWorld() { | ||
if (this.world == null) { | ||
this.world = Bukkit.getServer().getWorld(getWorldName()); | ||
} | ||
return world; | ||
} | ||
|
||
public String getWorldName() { | ||
if(_worldName == null) { | ||
_worldName = HappyDay.getPlugin(HappyDay.class).getConfig().getString("world", "world"); | ||
} | ||
return _worldName; | ||
} | ||
|
||
public static HappyDayData getInstance() { | ||
// By using a local variable we can prevent unneeded lookups, improving | ||
// performance up to 40%. | ||
// https://youtu.be/tSZn4wkBIu8?t=459 for more information. | ||
HappyDayData _instance = instance; | ||
if (_instance == null) { | ||
synchronized (HappyDayData.class) { | ||
// _instance is not always true. Do not remove. | ||
if (_instance == null) { | ||
instance = _instance = new HappyDayData(); | ||
} | ||
} | ||
} | ||
return _instance; | ||
} | ||
|
||
/** | ||
* @return the suspended state | ||
*/ | ||
public boolean isSuspended() { | ||
return suspended; | ||
} | ||
|
||
/** | ||
* @param suspended suspend the plugin | ||
*/ | ||
public void setSuspended(boolean suspended) { | ||
this.suspended = suspended; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/xyz/alicedtrh/happyday/HappyDayEventListener.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 @@ | ||
package xyz.alicedtrh.happyday; | ||
|
||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.event.world.TimeSkipEvent; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
|
||
public final class HappyDayEventListener implements Listener { | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onTimeSkipEvent(TimeSkipEvent event) { | ||
HappyDay happyday = HappyDay.getPlugin(HappyDay.class); | ||
happyday.getLogger().finer("Running checktask"); | ||
happyday.runCheckTask(); | ||
happyday.getLogger().finer("Running checktask*"); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onPlayerQuitEvent(PlayerQuitEvent event) { | ||
if (Bukkit.getOnlinePlayers().size() < 1) { | ||
HappyDayData.getInstance().setSuspended(true); | ||
} | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onPlayerJoinEvent(PlayerJoinEvent event) { | ||
if (HappyDayData.getInstance().isSuspended()) { | ||
return; | ||
} | ||
HappyDayData.getInstance().setSuspended(false); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/xyz/alicedtrh/happyday/TimeSkipEventListener.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.