From a566c9508787e0b15cd38cfa60fbcdf29fa61e93 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 29 Aug 2016 19:28:10 -0400 Subject: [PATCH] Crash-proofing using scoreboard --- src/iie/DeathListener.java | 44 +++++++++++++++++++++++++------ src/iie/HelloWorld.java | 10 +++---- src/iie/HelloWorldPlugin.java | 10 +++++++ src/iie/publicstaticvoidmain.java | 5 ++-- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/iie/DeathListener.java b/src/iie/DeathListener.java index 4b605af..36aa3c1 100644 --- a/src/iie/DeathListener.java +++ b/src/iie/DeathListener.java @@ -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)); } } diff --git a/src/iie/HelloWorld.java b/src/iie/HelloWorld.java index 9f08ff6..8e3d5e4 100644 --- a/src/iie/HelloWorld.java +++ b/src/iie/HelloWorld.java @@ -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); diff --git a/src/iie/HelloWorldPlugin.java b/src/iie/HelloWorldPlugin.java index 6544357..382729e 100644 --- a/src/iie/HelloWorldPlugin.java +++ b/src/iie/HelloWorldPlugin.java @@ -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 deathMap = new HashMap(); + 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)); diff --git a/src/iie/publicstaticvoidmain.java b/src/iie/publicstaticvoidmain.java index aa38426..65074d0 100644 --- a/src/iie/publicstaticvoidmain.java +++ b/src/iie/publicstaticvoidmain.java @@ -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"));