1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-20 15:27:35 +01:00
Harbor-Minecraft/src/main/java/mykyta/Harbor/Util.java

116 lines
4.1 KiB
Java
Raw Normal View History

2019-03-05 04:16:28 +01:00
package mykyta.Harbor;
import java.util.HashMap;
2019-03-06 02:47:01 +01:00
import java.util.logging.Logger;
2019-03-05 04:16:28 +01:00
2019-03-06 02:47:01 +01:00
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
2019-03-05 04:16:28 +01:00
import org.bukkit.World;
2019-03-05 16:18:05 +01:00
import org.bukkit.entity.Player;
2019-03-05 04:16:28 +01:00
2019-03-07 05:12:59 +01:00
import mykyta.Harbor.NMS.NMS;
import mykyta.Harbor.NMS.NMS_1_13_R2;
2019-03-06 02:47:01 +01:00
import net.md_5.bungee.api.ChatColor;
2019-03-05 16:18:05 +01:00
2019-03-06 02:47:01 +01:00
public class Util {
2019-03-05 16:18:05 +01:00
public static HashMap<World, Integer> sleeping = new HashMap<World, Integer>();
2019-03-07 05:12:59 +01:00
public String version = "1.4.2";
2019-03-05 16:27:22 +01:00
public static boolean debug = false;
2019-03-06 02:47:01 +01:00
private Logger log = Bukkit.getLogger();
2019-03-07 05:12:59 +01:00
private static NMS nms;
2019-03-06 02:47:01 +01:00
Config config = new Config();
2019-03-05 16:18:05 +01:00
2019-03-06 02:47:01 +01:00
/**
* 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")) {
2019-03-07 05:12:59 +01:00
nms = new NMS_1_13_R2();
2019-03-06 02:47:01 +01:00
}
}
/**
* 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);
2019-03-05 16:18:05 +01:00
}
2019-03-06 03:23:00 +01:00
/**
* Fetch the sleeping count for a world
* @param world World to fetch count for
*/
public int fetch(World world) {
int count;
try {count = Util.sleeping.get(world);}
catch (Exception e){count = 0;}
return count;
}
2019-03-05 16:18:05 +01:00
/**
* 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) {
2019-03-07 05:12:59 +01:00
nms.sendActionbar(player, message);
2019-03-05 16:18:05 +01:00
}
/**
* 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) {
2019-03-06 02:47:01 +01:00
Config config = new Config();
2019-03-07 05:12:59 +01:00
nms.sendActionbar(player, message
2019-03-05 16:18:05 +01:00
.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()))
2019-03-06 02:47:01 +01:00
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - (sleeping.get(world)).intValue())))));
2019-03-05 16:18:05 +01:00
}
2019-03-07 05:12:59 +01:00
/**
* Sends a custom JSON message to selected player
* @param player Player to send message to
* @param JSON Message in JSON format
*/
public void sendJSONMessage(Player player, String JSON) {
nms.sendJSONMessage(player, JSON);
}
/**
* Returns true if player is in survival mode
* @param player Target player
*/
public boolean isSurvival(Player player) {
if (player.getGameMode() == GameMode.SURVIVAL) return true;
else return false;
}
2019-03-05 04:16:28 +01:00
}