1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-11-28 04:45:11 +01:00

Update 1.4

This commit is contained in:
Mykyta Komarn 2018-10-04 20:19:46 -07:00
parent 2e35634acd
commit d3addd1b5a
9 changed files with 394 additions and 0 deletions

61
Harbor/config.yml Normal file
View File

@ -0,0 +1,61 @@
# _
# /\ /\__ _ _ __| |__ ___ _ __
# / /_/ / _` | '__| '_ \ / _ \| '__|
# / __ / (_| | | | |_) | (_) | |
# \/ /_/ \__,_|_| |_.__/ \___/|_|
#
# An open-source project by TechToolbox
# https://techtoolbox.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),
# so every message and plugin module can be modified here. If you would like to report a bug
# or suggest a feature, make sure to add an issue on the GitHub page for this plugin!
# GitHub URL: https://github.com/nkomarn/Harbor/issues
values:
# How often (in seconds) to check if a player is in a bed (increase for slower servers)
check: 1
# Percent of players that need to sleep to skip night (must be 0.0 to 1.0)
percent: 0.5
features:
# Toggle night skipping feature. Configure amount of players needed to skip above (percent)
skipNight: true
# Clear weather when skipping night
clearWeather: true
# Display debug information in console
debug: false
messages:
chat:
# Toggle chat messages
chat: true
# "Night skipped" chat message
skipped: "&eThe night has been skipped."
# Display which player went to bed in chat
sleeping: "&e[player] is now sleeping ([sleeping]/[online]. [needed] more needed to skip)."
# Sent to player when they sleep with "harbor.bypass" permission
bypass: "&eYou've been excluded from the sleep count."
actionbar:
# Enable/Disable actionbar message
actionbar: true
# Shown when some players are in bed
sleeping: "&e[sleeping] out of [online] players are sleeping ([needed] more needed to skip)."
# Shown when all players are in bed
everyone: "&eEveryone is sleeping. Sweet dreams!"
miscellaneous:
# Prefix for Harbor command messages
prefix: "&8&l(&6&lHarbor&8&l) "
# Display server version in console
running: "&7Running on version [version]."
# Sent in console when the plugin isn't compatible with the server
incompatible: "&7Spigot version [version] isn't compatible with Harbor."
# Harbor reload message
reloaded: "&7Reloaded Harbor"
reloadError: "&7Error reloading Harbor."
# Sent when player doesn't have permissions to run command
permission: "&7Insufficient permissions."
# Version identifier (do not change)
version: 1.4

12
Harbor/plugin.yml Normal file
View File

@ -0,0 +1,12 @@
name: Harbor
version: 1.4
main: techtoolbox.Harbor.Main
description: Ahoy, matey! Harbor is a Spigot plugin that redefines how sleep works in your server, making it easier for all the online players to get in bed quick and skip through the night!
author: TechToolbox
website: https://techtoolbox.tk
commands:
harbor:
description: List information and control Harbor.
usage: /<command>

View File

@ -0,0 +1,17 @@
package techtoolbox.Harbor;
import org.bukkit.Bukkit;
public class ActionBarMessage implements Runnable {
@Override
public void run() {
for (int i = 0; Main.worlds.size() > i; i++) {
if (((Main.worlds.get(Bukkit.getServer().getWorlds().get(i))).intValue() > 0) && ((Main.worlds.get(Bukkit.getServer().getWorlds().get(i))).intValue() < (Bukkit.getServer().getWorlds().get(i)).getPlayers().size() - Main.bypassers.size())) {
Main.sendActionbar("playersInBed", Bukkit.getServer().getWorlds().get(i));
}
else if (((Main.worlds.get(Bukkit.getServer().getWorlds().get(i))).intValue() == (Bukkit.getServer().getWorlds().get(i)).getPlayers().size() - Main.bypassers.size()) && ((Bukkit.getServer().getWorlds().get(i)).getPlayers().size() > 1)) {
Main.sendActionbar("everyoneSleeping", Bukkit.getServer().getWorlds().get(i));
}
}
}
}

View File

@ -0,0 +1,36 @@
package techtoolbox.Harbor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class Command implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) {
if (args.length < 1) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.miscellaneous.prefix") + "&7Version " + Main.version + " by TechToolbox."));
}
else if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("harbor.admin")) {
try {
Main.bypassers.clear();
Main.plugin.reloadConfig();
Bukkit.getServer().getPluginManager().disablePlugin(Main.plugin);
Bukkit.getServer().getPluginManager().enablePlugin(Main.plugin);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.miscellaneous.prefix") + Main.plugin.getConfig().getString("messages.miscellaneous.reloaded")));
}
catch (Exception e) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.miscellaneous.prefix") + Main.plugin.getConfig().getString("messages.miscellaneous.reloadError")));
if (!Main.plugin.getConfig().getBoolean("features.debug")) {
e.printStackTrace();
}
}
}
else {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.miscellaneous.prefix") + Main.plugin.getConfig().getString("messages.miscellaneous.permission")));
}
}
return true;
}
}

View File

@ -0,0 +1,59 @@
package techtoolbox.Harbor.Listeners;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import techtoolbox.Harbor.Main;
public class BedEnterEvent implements Listener {
@EventHandler
public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
Main.bypassers.clear();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("harbor.bypass")) {
Main.bypassers.add(player.getName());
}
}
if (!event.getPlayer().hasPermission("harbor.bypass")) {
Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() + 1));
// Chat messages
if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.sleeping").length() != 0)) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.sleeping")
.replace("[sleeping]", String.valueOf(Main.worlds.get(event.getPlayer().getWorld())))
.replace("[online]", String.valueOf(event.getPlayer().getWorld().getPlayers().size() - Main.bypassers.size()))
.replace("[player]", event.getPlayer().getName())
.replace("[needed]", String.valueOf(Math.max(0, Math.round(event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size() - (Main.worlds.get(event.getPlayer().getWorld())).intValue()))))));
}
// Skip night feature
if (Main.plugin.getConfig().getBoolean("features.skipNight") && ((Main.worlds.get(event.getPlayer().getWorld())).intValue() >= event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size()) && (event.getPlayer().getWorld().getPlayers().size() > 1)) {
Bukkit.getServer().getWorld(event.getPlayer().getWorld().getName()).setTime(1000L);
// Weather clear feature
if (Main.plugin.getConfig().getBoolean("features.clearWeather")) {
event.getPlayer().getWorld().setStorm(false);
event.getPlayer().getWorld().setThundering(false);
}
// Night skipped message
if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.skipped").length() != 0)) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.skipped")));
}
}
}
else if (Main.plugin.getConfig().getString("messages.chat.bypass").length() != 0) {
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.bypass")));
}
// Reset value if below zero (plugin glitch)
if ((Main.worlds.get(event.getPlayer().getWorld())).intValue() < 0) {
Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf(0));
}
}
}

View File

@ -0,0 +1,30 @@
package techtoolbox.Harbor.Listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import techtoolbox.Harbor.Main;
public class BedLeaveEvent implements Listener {
@EventHandler
public void onPlayerBedLeave(PlayerBedLeaveEvent event) {
Main.bypassers.clear();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("harbor.bypass")) {
Main.bypassers.add(player.getName());
}
}
if (!event.getPlayer().hasPermission("harbor.bypass")) {
Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() - 1));
}
// Reset value if below zero (plugin glitch)
if ((Main.worlds.get(event.getPlayer().getWorld())).intValue() < 0) {
Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf(0));
}
}
}

View File

@ -0,0 +1,21 @@
package techtoolbox.Harbor.Listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import techtoolbox.Harbor.Main;
public class JoinEvent implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Main.bypassers.clear();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("harbor.bypass")) {
Main.bypassers.add(player.getName());
}
}
}
}

View File

@ -0,0 +1,21 @@
package techtoolbox.Harbor.Listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import techtoolbox.Harbor.Main;
public class QuitEvent implements Listener{
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Main.bypassers.clear();
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
if (player.hasPermission("harbor.bypass")) {
Main.bypassers.add(player.getName());
}
}
}
}

View File

@ -0,0 +1,137 @@
package techtoolbox.Harbor;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import techtoolbox.Harbor.Actionbar.Actionbar;
import techtoolbox.Harbor.Actionbar.Actionbar_1_10_R1;
import techtoolbox.Harbor.Actionbar.Actionbar_1_11_R1;
import techtoolbox.Harbor.Actionbar.Actionbar_1_12_R1;
import techtoolbox.Harbor.Actionbar.Actionbar_1_13_R1;
import techtoolbox.Harbor.Actionbar.Actionbar_1_13_R2;
import techtoolbox.Harbor.Actionbar.Actionbar_1_8_R1;
import techtoolbox.Harbor.Actionbar.Actionbar_1_8_R2;
import techtoolbox.Harbor.Actionbar.Actionbar_1_8_R3;
import techtoolbox.Harbor.Actionbar.Actionbar_1_9_R1;
import techtoolbox.Harbor.Listeners.BedEnterEvent;
import techtoolbox.Harbor.Listeners.BedLeaveEvent;
import techtoolbox.Harbor.Listeners.JoinEvent;
import techtoolbox.Harbor.Listeners.QuitEvent;
public class Main extends JavaPlugin implements Listener {
public static Main plugin;
public static String version = "1.4";
public static HashMap<World, Integer> worlds = new HashMap<World, Integer>();
public static ArrayList<String> bypassers = new ArrayList<String>();
public static Actionbar actionbar;
public void onEnable() {
plugin = this;
saveDefaultConfig();
getCommand("harbor").setExecutor(new Command());
Bukkit.getPluginManager().registerEvents(new JoinEvent(), this);
Bukkit.getPluginManager().registerEvents(new QuitEvent(), this);
Bukkit.getPluginManager().registerEvents(new BedEnterEvent(), this);
Bukkit.getPluginManager().registerEvents(new BedLeaveEvent(), this);
// Detect old configuration version
try {
if (Float.valueOf(getConfig().getString("version")) > Float.valueOf(version)) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + "&7Configuration version is incompatible. Running Harbor version " + version + " while the configuration version is " + getConfig().getString("version") + "."));
}
}
catch (Exception e) {
// Config older than version 1.4
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + "&7Incompatible configuration. Delete the current configuration to generate a valid one."));
}
// Fill up worlds list
for (int i = 0; Bukkit.getServer().getWorlds().size() > i; i++) {
worlds.put(Bukkit.getServer().getWorlds().get(i), Integer.valueOf(0));
}
// Start actionbar message check
if (getConfig().getBoolean("messages.actionbar.actionbar")) {
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new ActionBarMessage(), 0L, getConfig().getInt("values.check") * 20);
}
// Set up actionbar NMS
if (setupActionbar()) {
Bukkit.getPluginManager().registerEvents(this, this);
} else {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.miscellaneous.incompatible").replace("[version]", version)));
Bukkit.getPluginManager().disablePlugin(this);
}
}
public void onDisable() {
// Knock players out of bed
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
player.damage(0.000001);
}
}
public static void sendActionbar(String type, World world) {
if (type.equals("playersInBed")) {
for (Player p : world.getPlayers()) {
actionbar.sendActionbar(p, plugin.getConfig().getString("messages.actionbar.sleeping")
.replace("[sleeping]", String.valueOf(worlds.get(world)))
.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())))));
}
}
else if (type.equals("everyoneSleeping")) {
for (Player p : world.getPlayers()) {
actionbar.sendActionbar(p, plugin.getConfig().getString("messages.actionbar.everyone")
.replace("[sleeping]", String.valueOf(worlds.get(world)))
.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())))));
}
}
}
private boolean setupActionbar() {
String version;
try {
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}
catch (ArrayIndexOutOfBoundsException e) {
if (plugin.getConfig().getBoolean("features.debug")) {
e.printStackTrace();
}
return false;
}
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + plugin.getConfig().getString("messages.miscellaneous.running").replace("[version]", version)));
if (version.equals("v1_8_R1")) {
actionbar = new Actionbar_1_8_R1();
}else if (version.equals("v1_8_R2")) {
actionbar = new Actionbar_1_8_R2();
}else if (version.equals("v1_8_R3")) {
actionbar = new Actionbar_1_8_R3();
}else if (version.equals("v1_9_R1")) {
actionbar = new Actionbar_1_9_R1();
}else if (version.equals("v1_10_R1")) {
actionbar = new Actionbar_1_10_R1();
}else if (version.equals("v1_11_R1")) {
actionbar = new Actionbar_1_11_R1();
}else if (version.equals("v1_12_R1")) {
actionbar = new Actionbar_1_12_R1();
}else if (version.equals("v1_13_R1")) {
actionbar = new Actionbar_1_13_R1();
} else if (version.equals("v1_13_R2")) {
actionbar = new Actionbar_1_13_R2();
}
return actionbar != null;
}
}