Implement the usage of Scoreboards, make the language configurable.

Closes #15
This commit is contained in:
graywolf336 2014-03-13 12:59:47 -05:00
parent 6ce05dbe7d
commit ec1e91cda2
9 changed files with 178 additions and 0 deletions

View File

@ -8,6 +8,7 @@ Beta 2 Changes
===
*Changes since Beta 1*
* Fix the default Jail Stick not being loaded correctly, [#21](https://github.com/graywolf336/Jail/issues/21)
* Implement Scoreboards, with title and time configurable. ([#15](https://github.com/graywolf336/Jail/issues/15))
Beta 1 Changes
===

View File

@ -6,6 +6,7 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.CommandHandler;
import com.graywolf336.jail.command.JailHandler;
import com.graywolf336.jail.enums.Settings;
@ -33,6 +34,7 @@ public class JailMain extends JavaPlugin {
private JailStickManager jsm;
private JailTimer jt;
private PrisonerManager pm;
private ScoreBoardManager sbm;
private boolean debug = false;
public void onEnable() {
@ -79,6 +81,7 @@ public class JailMain extends JavaPlugin {
}
jt = new JailTimer(this);
sbm = new ScoreBoardManager(this);
getLogger().info("Completed enablement.");
}
@ -96,6 +99,7 @@ public class JailMain extends JavaPlugin {
io.closeConnection();
jt = null;
sbm = null;
cmdHand = null;
pm = null;
jm = null;
@ -140,6 +144,23 @@ public class JailMain extends JavaPlugin {
return true;//Always return true here, that way we can handle the help and command usage ourself.
}
/** Reloads the scoreboard manager class, useful when something is changed int he config about it. */
public void reloadScoreBoardManager() {
this.sbm.removeAllScoreboards();
this.sbm = null;
this.sbm = new ScoreBoardManager(this);
if(getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
for(Jail j : jm.getJails()) {
for(Prisoner p : j.getAllPrisoners()) {
if(getServer().getPlayerExact(p.getName()) != null) {
this.sbm.addScoreBoard(getServer().getPlayerExact(p.getName()), p);
}
}
}
}
}
/** Gets the {@link HandCuffManager} instance. */
public HandCuffManager getHandCuffManager() {
return this.hcm;
@ -165,6 +186,11 @@ public class JailMain extends JavaPlugin {
return this.jsm;
}
/** Gets the {@link ScoreBoardManager} instance. */
public ScoreBoardManager getScoreBoardManager() {
return this.sbm;
}
/** Returns if the plugin is in debug state or not. */
public boolean inDebug() {
return this.debug;

View File

@ -267,6 +267,11 @@ public class PrisonerManager {
pl.getServer().dispatchCommand(pl.getServer().getConsoleSender(), command);
}
//Add the scoreboard to them if it is enabled
if(pl.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
pl.getScoreBoardManager().addScoreBoard(player, prisoner);
}
//Call our custom event for when a prisoner is actually jailed.
PrisonerJailedEvent event = new PrisonerJailedEvent(jail, cell, prisoner, player);
pl.getServer().getPluginManager().callEvent(event);
@ -398,6 +403,11 @@ public class PrisonerManager {
command = command.replaceAll("%p%", player.getName());
pl.getServer().dispatchCommand(pl.getServer().getConsoleSender(), command);
}
//Remove the scoreboard to them if it is enabled
if(pl.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
pl.getScoreBoardManager().removeScoreBoard(player);
}
//Call the prisoner released event as we have released them.
PrisonerReleasedEvent event = new PrisonerReleasedEvent(jail, cell, prisoner, player);

View File

@ -0,0 +1,101 @@
package com.graywolf336.jail;
import java.util.HashMap;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.enums.Settings;
public class ScoreBoardManager {
private JailMain pl;
private ScoreboardManager man;
private HashMap<String, Scoreboard> boards;
private OfflinePlayer time;
public ScoreBoardManager(JailMain plugin) {
this.pl = plugin;
this.man = plugin.getServer().getScoreboardManager();
this.boards = new HashMap<String, Scoreboard>();
this.time = plugin.getServer().getOfflinePlayer(Util.getColorfulMessage(pl.getConfig().getString(Settings.SCOREBOARDTIME.getPath())));
//Start the task if it is enabled
if(plugin.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
public void run() {
updatePrisonersTime();
}
}, 200L, 100L);
}
}
/**
* Adds the jailing score board to the player if they don't have one, otherwise it just updates it.
*
* @param player of whom to add the scoreboard to.
* @param pris data for the provided prisoner
*/
public void addScoreBoard(Player player, Prisoner pris) {
if(!boards.containsKey(player.getName())) {
boards.put(player.getName(), man.getNewScoreboard());
Objective o = boards.get(player.getName()).registerNewObjective("test", "dummy");
o.setDisplaySlot(DisplaySlot.SIDEBAR);
o.setDisplayName(Util.getColorfulMessage(pl.getConfig().getString(Settings.SCOREBOARDTITLE.getPath())));
o.getScore(time).setScore(pris.getRemainingTimeInMinutesInt());
player.setScoreboard(boards.get(player.getName()));
}else {
updatePrisonersBoard(player, pris);
}
}
/**
* Removes a player's jail scoreboard for their jail time and sets it to the main one.
*
* @param player of whom to remove the scoreboard for.
*/
public void removeScoreBoard(Player player) {
boards.remove(player.getName());
//TODO: See if this works or if we need to set it to a new one
player.setScoreboard(man.getMainScoreboard());
}
public void removeAllScoreboards() {
HashMap<String, Scoreboard> temp = new HashMap<String, Scoreboard>(boards);
for(String s : temp.keySet()) {
Player p = pl.getServer().getPlayerExact(s);
if(p != null) {
p.setScoreboard(man.getMainScoreboard());
}
boards.remove(s);
}
}
private void updatePrisonersTime() {
for(Jail j : pl.getJailManager().getJails()) {
for(Prisoner p : j.getAllPrisoners()) {
if(pl.getServer().getPlayerExact(p.getName()) != null) {
addScoreBoard(pl.getServer().getPlayerExact(p.getName()), p);
}
}
}
}
/**
* Updates a player's time in the scoreboard.
*
* @param player of whom to update the scoreboard for.
* @param pris data for the player
*/
private void updatePrisonersBoard(Player player, Prisoner pris) {
boards.get(player.getName()).getObjective("test").getScore(time).setScore(pris.getRemainingTimeInMinutesInt());
}
}

View File

@ -90,6 +90,11 @@ public class Prisoner {
return TimeUnit.MINUTES.convert(time, TimeUnit.MILLISECONDS);
}
/** Gets the remaining time the prison has in minutes except only in int format. */
public int getRemainingTimeInMinutesInt() {
return (int) this.getRemainingTimeInMinutes();
}
/**
* Sets the remaining time the prisoner has left.
*

View File

@ -20,6 +20,7 @@ public class JailReloadCommand implements Command {
jm.getPlugin().reloadConfig();
jm.getPlugin().getJailIO().loadLanguage();
jm.getPlugin().getJailIO().loadJails();
jm.getPlugin().reloadScoreBoardManager();
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PLUGINRELOADED));
return true;

View File

@ -41,6 +41,9 @@ public enum Settings {
RECIEVEMESSAGES("jailing.during.recieveMessages"),
RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"),
RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"),
SCOREBOARDENABLED("jailing.during.scoreboard.enabled"),
SCOREBOARDTITLE("jailing.during.scoreboard.title"),
SCOREBOARDTIME("jailing.during.scoreboard.time"),
TELEPORTONRELEASE("jailing.release.teleport"),
UPDATENOTIFICATIONS("system.updateNotifications"),
USEBUKKITTIMER("system.useBukkitTimer");

View File

@ -15,6 +15,8 @@ import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import com.graywolf336.jail.JailMain;
@ -120,6 +122,11 @@ public class PlayerListener implements Listener {
}
}
//Add the scoreboard to them if it is enabled
if(pl.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
pl.getScoreBoardManager().addScoreBoard(event.getPlayer(), p);
}
//if we are ignoring a prisoner's sleeping state, then let's set that
if(pl.getConfig().getBoolean(Settings.IGNORESLEEPINGSTATE.getPath())) {
event.getPlayer().setSleepingIgnored(true);
@ -127,6 +134,26 @@ public class PlayerListener implements Listener {
}
}
@EventHandler
public void handleGoingOffline(PlayerQuitEvent event) {
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//Remove the scoreboard to them if it is enabled
if(pl.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
pl.getScoreBoardManager().removeScoreBoard(event.getPlayer());
}
}
}
@EventHandler(ignoreCancelled=true)
public void handleGettingKicked(PlayerKickEvent event) {
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//Remove the scoreboard to them if it is enabled
if(pl.getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) {
pl.getScoreBoardManager().removeScoreBoard(event.getPlayer());
}
}
}
@EventHandler(priority = EventPriority.LOW)
public void foodControl(FoodLevelChangeEvent event) {
if(pl.getConfig().getBoolean(Settings.FOODCONTROL.getPath())) {

View File

@ -40,6 +40,10 @@ jailing:
preventInteractionItems: []
preventInteractionItemsPenalty: 5m
recieveMessages: true
scoreboard:
enabled: true
title: 'Jail Info'
time: '&aTime:'
jail:
automaticMute: true
broadcastJailing: false