Crash-proofing using scoreboard

This commit is contained in:
BuildTools 2016-08-29 19:28:10 -04:00
parent c4eb6565f1
commit a566c95087
4 changed files with 54 additions and 15 deletions

View file

@ -8,6 +8,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scoreboard.Score;
public class DeathListener implements Listener {
@ -18,24 +20,50 @@ public class DeathListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onHardcoreDeath(PlayerDeathEvent deathEvent){
String timeString = String.valueOf(System.currentTimeMillis());
Player player = deathEvent.getEntity();
String playerString = (String) player.getName();
Location location = player.getLocation();
String worldString = (String) location.getWorld().getName();
player.sendMessage(timeString);
player.sendMessage(playerString);
player.sendMessage(worldString);
//player.sendMessage(timeString);
//player.sendMessage(playerString);
//player.sendMessage(worldString);
if (Objects.equals(worldString, "hardcore")){
HelloWorldPlugin.deathMap.put(playerString, timeString);
player.sendMessage(HelloWorldPlugin.deathMap.toString());
Score score = HelloWorldPlugin.hardcoreTimeDead.getScore(playerString);
score.setScore((int) (System.currentTimeMillis()/1000));
player.sendMessage("Key saved: " + playerString);
player.sendMessage("Data saved: " + HelloWorldPlugin.deathMap.get(playerString));
//player.sendMessage(HelloWorldPlugin.deathMap.toString());
//player.sendMessage("Key saved: " + playerString);
//player.sendMessage("Data saved: " + HelloWorldPlugin.deathMap.get(playerString));
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogin(PlayerJoinEvent joinEvent){
Player player = joinEvent.getPlayer();
String playerString = (String) player.getName();
Score score = null;
try{
score = HelloWorldPlugin.hardcoreTimeDead.getScore(playerString);
}catch(IllegalArgumentException throwable){
score.setScore(0);
}
if (HelloWorldPlugin.deathMap.get(playerString) == null && score.getScore() != 0){
HelloWorldPlugin.deathMap.put(playerString, String.valueOf((score.getScore()) * 1000));
}
}

View file

@ -26,14 +26,14 @@ public class HelloWorld implements CommandExecutor {
long currentTime = System.currentTimeMillis();
String playername = sender.getName();
long deathTime = 0;
sender.sendMessage("Playername: " + playername);
sender.sendMessage("Data Imported: " + HelloWorldPlugin.deathMap.get(playername));
//sender.sendMessage("Playername: " + playername);
//sender.sendMessage("Data Imported: " + HelloWorldPlugin.deathMap.get(playername));
if (HelloWorldPlugin.deathMap.get(playername) != null){
deathTime = Long.parseLong(HelloWorldPlugin.deathMap.get(playername));
sender.sendMessage("DeathTime: " + deathTime);
//sender.sendMessage("DeathTime: " + deathTime);
}else{
sender.sendMessage("you have never died in hardcore");
sender.sendMessage("You have never died in hardcore");
Player player = (Player) sender;
World hardcoreWorld = player.getServer().getWorld("hardcore");
Location location = new Location(hardcoreWorld, 1280, 71, -179);
@ -43,7 +43,7 @@ public class HelloWorld implements CommandExecutor {
if (currentTime - deathTime >= 86400000 && deathTime != 0){
sender.sendMessage((86400000 - (currentTime - deathTime) ) /3600000 + " hours have passed since you died in hardcore last");
sender.sendMessage("You died " + (86400000 - (currentTime - deathTime) ) /3600000 + " hours ago. Ready to give it another shot?");
Player player = (Player) sender;
World hardcoreWorld = player.getServer().getWorld("hardcore");
Location location = new Location(hardcoreWorld, 1280, 71, -179);

View file

@ -3,13 +3,23 @@ package iie;
import java.util.AbstractMap;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
public class HelloWorldPlugin extends JavaPlugin {
public static Scoreboard board;
public static Objective hardcoreTimeDead;
public static AbstractMap<String,String> deathMap = new HashMap<String,String>();
public void onEnable(){
registerCommands();
getServer().getPluginManager().registerEvents(new DeathListener(this), this);
board = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy");
}
public void registerCommands(){
getCommand("HelloWorld").setExecutor(new HelloWorld(this));

View file

@ -17,15 +17,16 @@ public class publicstaticvoidmain {
test = 12345;
}
test = (int) (System.currentTimeMillis()/1000);
System.out.println(test);
long currentTime = System.currentTimeMillis();
int currentTimeInt = (int) (currentTime/1000);
System.out.println(currentTime);
System.out.println(currentTime + " current time");
System.out.println(currentTimeInt);
System.out.println(Long.parseLong("1472003809703"));