mirror of
https://github.com/nkomarn/harbor.git
synced 2024-12-19 23:08:58 +01:00
Bed leave stuff
This commit is contained in:
parent
8d994a81ea
commit
7e0646ea9b
@ -41,8 +41,10 @@ messages:
|
||||
chat: true
|
||||
# "Night skipped" chat message
|
||||
skipped: "&eThe night has been skipped."
|
||||
# Display which player went to bed in chat
|
||||
# Display which player went to bed
|
||||
sleeping: "&e[player] is now sleeping ([sleeping]/[online]. [needed] more needed to skip)."
|
||||
# Display when a player left their bed
|
||||
left: "&e[player] got out of bed ([sleeping]/[online]. [needed] more needed to skip)."
|
||||
# Shown when a player tries to sleep and sleeping is blocked
|
||||
blocked: "&cSleeping is disabled."
|
||||
# Sent to player when they sleep with "harbor.bypass" permission
|
||||
|
@ -13,38 +13,34 @@ import mykyta.Harbor.Config;
|
||||
import mykyta.Harbor.Util;
|
||||
|
||||
public class BedEnter implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
|
||||
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
|
||||
Util util = new Util();
|
||||
Config config = new Config();
|
||||
|
||||
/**
|
||||
* Prevent bed entry if "block" is enabled in the config
|
||||
*/
|
||||
//Prevent bed entry if "block" is enabled in the config
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment world's sleeping count if player isn't excluded
|
||||
*/
|
||||
if (event.getBedEnterResult() == BedEnterResult.OK) {
|
||||
// Add one to the sleeping list
|
||||
World world = event.getPlayer().getWorld();
|
||||
|
||||
// Increment the sleeping count
|
||||
if (!config.getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) {
|
||||
util.increment(event.getPlayer().getWorld());
|
||||
util.increment(world);
|
||||
}
|
||||
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("[sleeping]", String.valueOf(util.fetch(world))))
|
||||
.replace("[online]", String.valueOf(world.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()))))));
|
||||
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - util.fetch(world))))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
src/main/java/mykyta/Harbor/Events/BedLeave.java
Normal file
36
src/main/java/mykyta/Harbor/Events/BedLeave.java
Normal file
@ -0,0 +1,36 @@
|
||||
package mykyta.Harbor.Events;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
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.PlayerBedLeaveEvent;
|
||||
|
||||
import mykyta.Harbor.Config;
|
||||
import mykyta.Harbor.Util;
|
||||
|
||||
public class BedLeave implements Listener {
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onPlayerBedLeave(PlayerBedLeaveEvent event) {
|
||||
Util util = new Util();
|
||||
Config config = new Config();
|
||||
World world = event.getPlayer().getWorld();
|
||||
|
||||
// Decrement the sleeping count
|
||||
if (!config.getBoolean("features.bypass") || !event.getPlayer().hasPermission("harbor.bypass")) {
|
||||
util.decrement(world);
|
||||
}
|
||||
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.left")
|
||||
.replace("[sleeping]", String.valueOf(util.fetch(world))))
|
||||
.replace("[online]", String.valueOf(world.getPlayers().size()))
|
||||
.replace("[player]", event.getPlayer().getName())
|
||||
.replace("[needed]", String.valueOf(Math.max(0, Math.round(world.getPlayers().size() * Float.parseFloat(config.getString("values.percent")) - util.fetch(world))))));
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import mykyta.Harbor.Events.BedEnter;
|
||||
import mykyta.Harbor.Events.BedLeave;
|
||||
|
||||
public class Harbor extends JavaPlugin {
|
||||
private Logger log = Bukkit.getLogger();
|
||||
@ -17,7 +18,7 @@ public class Harbor extends JavaPlugin {
|
||||
config.setInstance(this);
|
||||
saveDefaultConfig();
|
||||
Bukkit.getPluginManager().registerEvents(new BedEnter(), this);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new BedLeave(), this);
|
||||
util.setupNMS();
|
||||
|
||||
// Check for updates
|
||||
|
@ -39,6 +39,10 @@ public class Updater {
|
||||
System.out.println("Running latest version.");
|
||||
return false;
|
||||
}
|
||||
else if (!releases.contains(util.version)) {
|
||||
System.out.println("Hmm... looks like you're using some sort of time travel technology. Welp, at least you don't have updates to worry about any time soon.");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
System.out.println("Running an outdated version! Latest is " + releases.get(releases.size() - 1));
|
||||
return true;
|
||||
|
@ -58,6 +58,17 @@ public class Util {
|
||||
Util.sleeping.put(world, count - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an actionbar message to the given player
|
||||
* @param player Player to show actionbar to
|
||||
|
Loading…
Reference in New Issue
Block a user