1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-18 22:37:36 +01:00

Update notifier option

This commit is contained in:
BuildTools 2019-03-05 07:18:05 -08:00
parent 6cd5580508
commit 429b960ec1
6 changed files with 86 additions and 18 deletions

View File

@ -4,8 +4,8 @@
# / __ / (_| | | | |_) | (_) | |
# \/ /_/ \__,_|_| |_.__/ \___/|_|
#
# An open-source project by TechToolbox
# https://techtoolbox.tk/
# An open-source project by Mykyta (TechToolbox)
# https://mykyta.tk/
#
# Ahoy, matey! You've arrived at the configuration file, where things get scary.
# Every single thing within this plugin is customizable (at least I tried to make it that way),
@ -26,10 +26,14 @@ features:
clearWeather: true
# Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count
bypass: false
# Prevent any players from entering a bed (1.13.2 OR NEWER)
blockSleep: false
# Prevent all players from entering a bed
block: false
# Display debug information in console
debug: false
# Displays a notification when a new update is released
notifier: true
# Automatically updates the plugin when a newer version is available
updater: true
messages:
chat:

View File

@ -0,0 +1,5 @@
package mykyta.Harbor;
public class Config {
}

View File

@ -1,12 +1,15 @@
package mykyta.Harbor.Events;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedEnterEvent.BedEnterResult;
import mykyta.Harbor.Harbor;
import mykyta.Harbor.Util;
public class BedEnter implements Listener {
@ -17,16 +20,26 @@ public class BedEnter implements Listener {
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
// Prevent bed entry if "blockSleep" is enabled in the config
if (harbor.getConfig().getBoolean("features.blockSleep")) {
if (harbor.getConfig().getString("messages.chat.blocked").length() > 0) {
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', harbor.getConfig().getString("messages.chat.blocked")));
}
//Main.sendActionbar("sleepingBlocked", null, event.getPlayer())
Util util = new Util(harbor);
/**
* Prevent bed entry if "blockSleep" is enabled in the config
*/
if (harbor.getConfig().getBoolean("features.block")) {
if (harbor.getConfig().getString("messages.chat.blocked").length() > 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', harbor.getConfig().getString("messages.chat.blocked")));
util.sendActionbar(event.getPlayer(), harbor.getConfig().getString("messages.actionbar.blocked"));
event.setCancelled(true);
return;
}
/**
* Increment world's sleeping count if player isn't excluded
*/
if (event.getBedEnterResult() == BedEnterResult.OK) {
if (!harbor.getConfig().getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) {
World world = event.getPlayer().getWorld();
Util.sleeping.put(world, Util.sleeping.get(world) + 1);
}
else if (harbor.getConfig().getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', harbor.getConfig().getString("messages.chat.bypass")));
}
}
}

View File

@ -1,18 +1,25 @@
package mykyta.Harbor;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import mykyta.Harbor.Events.BedEnter;
public class Harbor extends JavaPlugin {
Updater updater = new Updater();
private Logger log = Bukkit.getLogger();
private Updater updater = new Updater(this);
public void onEnable() {
saveDefaultConfig();
Bukkit.getPluginManager().registerEvents(new BedEnter(this), this);
System.out.println("Checking for updates");
updater.check();
// Check for updates
if (this.getConfig().getBoolean("features.notifier")) {
System.out.println("Checking for updates.");
updater.check();
}
}
public void onDisable() {

View File

@ -14,6 +14,11 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Updater {
Harbor harbor;
public Updater(Harbor instance) {
harbor = instance;
}
/**
* Checks for an update using the Spiget API
* @see https://spiget.org/
@ -24,7 +29,7 @@ public class Updater {
URLConnection request = url.openConnection();
request.connect();
Util util = new Util();
Util util = new Util(harbor);
ArrayList<String> releases = new ArrayList<String>();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(new InputStreamReader((InputStream) request.getContent()));

View File

@ -3,8 +3,42 @@ package mykyta.Harbor;
import java.util.HashMap;
import org.bukkit.World;
import org.bukkit.entity.Player;
public class Util {
public static HashMap<World, Integer> worlds = new HashMap<World, Integer>();
import mykyta.Harbor.Actionbar.Actionbar;
public class Util implements Actionbar {
public static HashMap<World, Integer> sleeping = new HashMap<World, Integer>();
public String version = "1.5";
private Actionbar actionbar;
Harbor harbor;
public Util(Harbor instance) {
harbor = instance;
}
/**
* Sends an actionbar message to the given player
* @param player Player to show actionbar to
* @param message Actionbar message with color codes
*/
public void sendActionbar(Player player, String message) {
actionbar.sendActionbar(player, message);
}
/**
* Sends actionbar with world information
* @see sendActionbar(Player player, String message)
* @param player Player to show actionbar to
* @param message Actionbar message with color codes
* @param world World to fetch information for
*/
public void sendActionbar(Player player, String message, World world) {
actionbar.sendActionbar(player, message
.replace("[sleeping]", String.valueOf(sleeping.get(world)))
//TODO add bypassers functionaliyt .replace("[online]", String.valueOf(world.getPlayers().size() - bypassers.size()))
// .replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(plugin.getConfig().getString("values.percent")) - bypassers.size() - ((Integer)worlds.get(world)).intValue())))));
.replace("[online]", String.valueOf(world.getPlayers().size()))
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(harbor.getConfig().getString("values.percent")) - (sleeping.get(world)).intValue())))));
}
}