1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-09-27 22:17:31 +02:00

Syncing changes

This commit is contained in:
Mykyta Komarn 2019-03-05 17:47:01 -08:00
parent 255d6b343f
commit 8d994a81ea
5 changed files with 128 additions and 37 deletions

View File

@ -7,19 +7,64 @@ import org.bukkit.Bukkit;
public class Config {
private Logger log = Bukkit.getLogger();
private String error = "An error occured while trying to read the configuration. The plugin may not function correctly as a result.";
private static Harbor harbor;
/**
* Gets a boolean from the configuration
* @param location Config location of the boolean
* Sets main class instance for accessing configuration
* @param instance Instance of the main class
*/
public boolean getBool(String location) {
try {
public void setInstance(Harbor instance) {
harbor = instance;
}
/**
* Report an error in reading the configuration
* @param e Exception generated from reading configuration
*/
private void error(Exception e) {
log.severe(error);
if (Util.debug) System.err.println(e);
}
/**
* Fetches a boolean from the configuration
* @param location Configuration location of the boolean
*/
public boolean getBoolean(String location) {
try {
return harbor.getConfig().getBoolean(location);
}
catch (Exception e) {
log.severe(error);
if (Util.debug) System.err.println(e);
error(e);
return false;
}
}
/**
* Fetches a string from the configuration
* @param location Configuration location of the string
*/
public String getString(String location) {
try {
return harbor.getConfig().getString(location);
}
catch (Exception e) {
error(e);
return "";
}
}
/**
* Fetches an integer from the configuration
* @param location Configuration location of the integer
*/
public int getInteger(String location) {
try {
return harbor.getConfig().getInt(location);
}
catch (Exception e) {
error(e);
return 0;
}
}
}

View File

@ -1,5 +1,6 @@
package mykyta.Harbor.Events;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
@ -8,26 +9,22 @@ 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.Config;
import mykyta.Harbor.Util;
public class BedEnter implements Listener {
Harbor harbor;
public BedEnter(Harbor instance) {
harbor = instance;
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
Util util = new Util(harbor);
Util util = new Util();
Config config = new Config();
/**
* Prevent bed entry if "blockSleep" is enabled in the config
* Prevent bed entry if "block" 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"));
if (config.getBoolean("features.block")) {
if (config.getString("messages.chat.blocked").length() > 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.blocked")));
util.sendActionbar(event.getPlayer(), config.getString("messages.actionbar.blocked"));
event.setCancelled(true);
}
@ -35,11 +32,20 @@ public class BedEnter implements Listener {
* 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);
// Add one to the sleeping list
if (!config.getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) {
util.increment(event.getPlayer().getWorld());
}
else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass")));
// Send a chat message
if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0)) {
Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.sleeping")
.replace("[sleeping]", String.valueOf(Util.sleeping.get(event.getPlayer().getWorld())))
.replace("[online]", String.valueOf(event.getPlayer().getWorld().getPlayers().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()))))));
}
else if (harbor.getConfig().getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', harbor.getConfig().getString("messages.chat.bypass")));
}
}
}

View File

@ -9,11 +9,16 @@ import mykyta.Harbor.Events.BedEnter;
public class Harbor extends JavaPlugin {
private Logger log = Bukkit.getLogger();
private Updater updater = new Updater(this);
private Updater updater = new Updater();
public void onEnable() {
Config config = new Config();
Util util = new Util();
config.setInstance(this);
saveDefaultConfig();
Bukkit.getPluginManager().registerEvents(new BedEnter(this), this);
Bukkit.getPluginManager().registerEvents(new BedEnter(), this);
util.setupNMS();
// Check for updates
if (this.getConfig().getBoolean("features.notifier")) {
@ -21,7 +26,7 @@ public class Harbor extends JavaPlugin {
updater.check();
}
// Enable debugging if set in config
// Enable debugging if set in configuration
if (this.getConfig().getBoolean("debug")) Util.debug = true;
}

View File

@ -3,7 +3,6 @@ package mykyta.Harbor;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
@ -14,11 +13,6 @@ 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/
@ -29,7 +23,7 @@ public class Updater {
URLConnection request = url.openConnection();
request.connect();
Util util = new Util(harbor);
Util util = new Util();
ArrayList<String> releases = new ArrayList<String>();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(new InputStreamReader((InputStream) request.getContent()));

View File

@ -1,21 +1,61 @@
package mykyta.Harbor;
import java.util.HashMap;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import mykyta.Harbor.Actionbar.Actionbar;
import mykyta.Harbor.Actionbar.Actionbar_1_13_R2;
import net.md_5.bungee.api.ChatColor;
public class Util implements Actionbar {
public class Util {
public static HashMap<World, Integer> sleeping = new HashMap<World, Integer>();
public String version = "1.5";
public static boolean debug = false;
private Actionbar actionbar;
private Logger log = Bukkit.getLogger();
private static Actionbar actionbar;
Config config = new Config();
Harbor harbor;
public Util(Harbor instance) {
harbor = instance;
/**
* Select the correct NMS classes for the server version
*/
public void setupNMS() {
String version = "";
try {version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];}
catch (ArrayIndexOutOfBoundsException e) {
log.severe("Could not get server version. The plugin may not function correctly as a result.");
if (Util.debug) System.err.println(e);
}
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix") + config.getString("messages.miscellaneous.running").replace("[version]", version)));
if (version.equals("v1_13_R2")) {
actionbar = new Actionbar_1_13_R2();
}
}
/**
* Increment the sleeping count for a world
* @param world World to increment count in
*/
public void increment(World world) {
int count;
try {count = Util.sleeping.get(world);}
catch (Exception e){count = 0;}
Util.sleeping.put(world, count + 1);
}
/**
* Decrement the sleeping count for a world
* @param world World to increment count in
*/
public void decrement(World world) {
int count;
try {count = Util.sleeping.get(world);}
catch (Exception e){count = 0;}
Util.sleeping.put(world, count - 1);
}
/**
@ -35,11 +75,12 @@ public class Util implements Actionbar {
* @param world World to fetch information for
*/
public void sendActionbar(Player player, String message, World world) {
Config config = new Config();
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())))));
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - (sleeping.get(world)).intValue())))));
}
}