started scoreboard handling
This commit is contained in:
parent
ccacff562e
commit
2ffb8bbe14
3 changed files with 142 additions and 2 deletions
|
|
@ -29,12 +29,14 @@ public class MiscListener implements Listener {
|
||||||
private LogHandler logger;
|
private LogHandler logger;
|
||||||
private Configuration config;
|
private Configuration config;
|
||||||
private Data data;
|
private Data data;
|
||||||
|
private ScoreboardHandler scoreboardHandler;
|
||||||
|
|
||||||
|
|
||||||
public MiscListener(LogHandler logger, Configuration config, Data data) {
|
public MiscListener(LogHandler logger, Configuration config, Data data, ScoreboardHandler scoreboardHandler) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
this.scoreboardHandler = scoreboardHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -45,6 +47,7 @@ public class MiscListener implements Listener {
|
||||||
*/
|
*/
|
||||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||||
public void onAutosave(WorldSaveEvent event) {
|
public void onAutosave(WorldSaveEvent event) {
|
||||||
|
scoreboardHandler.updateScores();
|
||||||
data.saveData();
|
data.saveData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,135 @@
|
||||||
|
package io.github.J0hnL0cke.egghunt.Controller;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.time.temporal.TemporalUnit;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.time.temporal.TemporalUnit;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.Statistic;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.scoreboard.Criteria;
|
||||||
|
import org.bukkit.scoreboard.Objective;
|
||||||
|
import org.bukkit.scoreboard.RenderType;
|
||||||
|
import org.bukkit.scoreboard.Score;
|
||||||
|
import org.bukkit.scoreboard.Scoreboard;
|
||||||
|
import org.bukkit.scoreboard.ScoreboardManager;
|
||||||
|
|
||||||
|
import io.github.J0hnL0cke.egghunt.Model.Configuration;
|
||||||
|
import io.github.J0hnL0cke.egghunt.Model.Data;
|
||||||
|
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles interactions with the scoreboard
|
||||||
|
*/
|
||||||
|
public class ScoreboardHandler {
|
||||||
|
|
||||||
|
private Data data;
|
||||||
|
private Configuration config;
|
||||||
|
private LogHandler logger;
|
||||||
|
private ScoreboardManager manager;
|
||||||
|
private Scoreboard board;
|
||||||
|
private Objective eggObjective;
|
||||||
|
private Instant lastUpdate;
|
||||||
|
private static final String OBJECTIVE_KEY = "eggy";
|
||||||
|
|
||||||
|
public ScoreboardHandler(Data data, Configuration config, LogHandler logger) {
|
||||||
|
this.data = data;
|
||||||
|
this.config = config;
|
||||||
|
this.logger = logger;
|
||||||
|
manager = Bukkit.getScoreboardManager();
|
||||||
|
board = manager.getMainScoreboard(); //possibly switch to new board? would need to save/load independently of server
|
||||||
|
|
||||||
|
|
||||||
|
eggObjective = board.getObjective(OBJECTIVE_KEY);
|
||||||
|
if (eggObjective == null) {
|
||||||
|
//TODO
|
||||||
|
logger.info("No scoreboard objective for owning egg found, creating new objective");
|
||||||
|
logger.log("TODO");
|
||||||
|
}
|
||||||
|
|
||||||
|
lastUpdate = Instant.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the score for the current egg owner
|
||||||
|
*
|
||||||
|
* For accurate scoring, this should be updated at least every time egg ownership changes, and
|
||||||
|
* every time the plugin is enabled/disabled
|
||||||
|
* @param data
|
||||||
|
*/
|
||||||
|
public void updateScores() {
|
||||||
|
|
||||||
|
OfflinePlayer owner = data.getEggOwner();
|
||||||
|
|
||||||
|
if (owner != null) {
|
||||||
|
|
||||||
|
if (data.getEggEntity() != null) {
|
||||||
|
//TODO remove later, this is just for testing
|
||||||
|
addScore(data.getEggEntity(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Instant newUpdate = Instant.now(); //when writing tests, switch this to using an injected Clock instance
|
||||||
|
|
||||||
|
//YOU WILL REMEMBER ME! REMEMBER ME FOR
|
||||||
|
//ChronoUnit.CENTURIES
|
||||||
|
|
||||||
|
long mins = ChronoUnit.MINUTES.between(lastUpdate, newUpdate); //this is the floored duration in minutes
|
||||||
|
|
||||||
|
int minInt = Long.valueOf(mins).intValue();
|
||||||
|
|
||||||
|
addScore(data.getEggOwner(), minInt);
|
||||||
|
|
||||||
|
lastUpdate = newUpdate;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeScoreboard() {
|
||||||
|
|
||||||
|
|
||||||
|
Objective newObj = board.registerNewObjective(OBJECTIVE_KEY, Criteria.DUMMY, "Minutes owning egg",
|
||||||
|
RenderType.INTEGER);
|
||||||
|
|
||||||
|
//newObj.isModifiable();
|
||||||
|
|
||||||
|
//setScore(null, 0);
|
||||||
|
|
||||||
|
//manager.
|
||||||
|
|
||||||
|
//Criteria c = Criteria.statistic(Statistic.);
|
||||||
|
|
||||||
|
//Criteria d = Criteria.create("MinsHeldEgg");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addScore(OfflinePlayer p, int score) {
|
||||||
|
setScore(p, getScore(p) + score); //maybe do this more efficiently
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addScore(Entity e, int score) { //TODO testing method, remove
|
||||||
|
String key = e.getUniqueId().toString();
|
||||||
|
if (e.getCustomName() != null) {
|
||||||
|
key = e.getCustomName();
|
||||||
|
}
|
||||||
|
|
||||||
|
Score s = eggObjective.getScore(key);
|
||||||
|
|
||||||
|
s.setScore(s.getScore() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setScore(OfflinePlayer p, int score) {
|
||||||
|
Score s = eggObjective.getScore(p.getName());
|
||||||
|
s.setScore(score);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getScore(OfflinePlayer p) {
|
||||||
|
Score s = eggObjective.getScore(p.getName());
|
||||||
|
return s.getScore();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ import io.github.J0hnL0cke.egghunt.Controller.CommandHandler;
|
||||||
import io.github.J0hnL0cke.egghunt.Controller.EggController;
|
import io.github.J0hnL0cke.egghunt.Controller.EggController;
|
||||||
import io.github.J0hnL0cke.egghunt.Controller.EggDestroyListener;
|
import io.github.J0hnL0cke.egghunt.Controller.EggDestroyListener;
|
||||||
import io.github.J0hnL0cke.egghunt.Controller.MiscListener;
|
import io.github.J0hnL0cke.egghunt.Controller.MiscListener;
|
||||||
|
import io.github.J0hnL0cke.egghunt.Controller.ScoreboardHandler;
|
||||||
import io.github.J0hnL0cke.egghunt.Controller.EventScheduler;
|
import io.github.J0hnL0cke.egghunt.Controller.EventScheduler;
|
||||||
import io.github.J0hnL0cke.egghunt.Controller.InventoryListener;
|
import io.github.J0hnL0cke.egghunt.Controller.InventoryListener;
|
||||||
import io.github.J0hnL0cke.egghunt.Model.Configuration;
|
import io.github.J0hnL0cke.egghunt.Model.Configuration;
|
||||||
|
|
@ -45,7 +46,8 @@ public final class egghunt extends JavaPlugin {
|
||||||
logger.setDebug(config.getDebugEnabled());
|
logger.setDebug(config.getDebugEnabled());
|
||||||
|
|
||||||
//create controller instances
|
//create controller instances
|
||||||
MiscListener miscListener = new MiscListener(logger, config, data);
|
ScoreboardHandler scoreboardHandler = new ScoreboardHandler(data, config, logger);
|
||||||
|
MiscListener miscListener = new MiscListener(logger, config, data, scoreboardHandler);
|
||||||
InventoryListener inventoryListener = new InventoryListener(logger, config, data);
|
InventoryListener inventoryListener = new InventoryListener(logger, config, data);
|
||||||
EggDestroyListener destroyListener = new EggDestroyListener(logger, config, data);
|
EggDestroyListener destroyListener = new EggDestroyListener(logger, config, data);
|
||||||
EventScheduler scheduler = new EventScheduler(config, data, logger);
|
EventScheduler scheduler = new EventScheduler(config, data, logger);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue