From 7c82d7879ac59e387a60c48cba13d0eb32535dc7 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 4 Sep 2016 21:33:53 -0400 Subject: [PATCH] changed command to "hardcore" removed debug messages, removed usage of hashmap to rely entirely on scoreboard, --- plugin.yml | 4 +-- src/iie/DeathListener.java | 65 +++-------------------------------- src/iie/HelloWorld.java | 52 ++++++++++++---------------- src/iie/HelloWorldPlugin.java | 24 +++---------- src/iie/JoinListener.java | 33 ++++++++++++++++++ 5 files changed, 66 insertions(+), 112 deletions(-) create mode 100644 src/iie/JoinListener.java diff --git a/plugin.yml b/plugin.yml index 13eb73d..787dc91 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,5 +2,5 @@ version: 1.0.0 name: HelloWorldPlugin commands: - HelloWorld: - description: Command that says Hello World! \ No newline at end of file + hardcore: + description: Command that teleports you to hardcore world \ No newline at end of file diff --git a/src/iie/DeathListener.java b/src/iie/DeathListener.java index cec2892..34a943f 100644 --- a/src/iie/DeathListener.java +++ b/src/iie/DeathListener.java @@ -8,7 +8,6 @@ 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; public class DeathListener implements Listener { @@ -20,72 +19,18 @@ public class DeathListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onHardcoreDeath(PlayerDeathEvent deathEvent){ - String timeString = String.valueOf(System.currentTimeMillis()); + int currentTime = (int) ((System.currentTimeMillis())/1000); //divided by 1000 to fit within Integer range Player player = deathEvent.getEntity(); - String playerString = (String) player.getName(); + String playername = (String) player.getName(); Location location = player.getLocation(); String worldString = (String) location.getWorld().getName(); - //player.sendMessage(timeString); - //player.sendMessage(playerString); - //player.sendMessage(worldString); - if (Objects.equals(worldString, "hardcore")){ - HelloWorldPlugin.deathMap.put(playerString, timeString); - - if (HelloWorldPlugin.hardcoreTimeDead == null){ - player.sendMessage("Objective hardcoreTimeDead was null"); - }else if (HelloWorldPlugin.hardcoreTimeDead.getScore(playerString) == null){ - HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).setScore(0); - player.sendMessage("Score for hardcoreTimeDead was null, set to " + String.valueOf(HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).getScore())); - } + if (HelloWorldPlugin.hardcoreTimeDead.getScore(playername) == null) //null check + HelloWorldPlugin.hardcoreTimeDead.getScore(playername).setScore(0); //convert null to 0 - HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).setScore((int) (System.currentTimeMillis()/1000)); - - - //player.sendMessage(HelloWorldPlugin.deathMap.toString()); - //player.sendMessage("Key saved: " + playerString); - //player.sendMessage("Data saved: " + HelloWorldPlugin.deathMap.get(playerString)); + HelloWorldPlugin.hardcoreTimeDead.getScore(playername).setScore(currentTime); } - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerJoin(PlayerJoinEvent JoinEvent){ - - - - Player player = JoinEvent.getPlayer(); - String playerString = (String) player.getName(); - - player.sendMessage(playerString + " has joined"); - - - - if (HelloWorldPlugin.hardcoreTimeDead.getScore(playerString) != null){ - HelloWorldPlugin.deathMap.put(playerString, String.valueOf((HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).getScore()) * 1000)); - player.sendMessage("getScore(playerString) was not null"); - }else{ - HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).setScore(0); - HelloWorldPlugin.deathMap.put(playerString, String.valueOf(HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).getScore())); - } - - - - //try{ - // HelloWorldPlugin.hardcoreTimeDead.getScore(playerString); - // player.sendMessage("try"); - //}catch(IllegalArgumentException e){ - // HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).setScore(1); - // player.sendMessage("catch"); - //}finally{ - // HelloWorldPlugin.deathMap.put(playerString, String.valueOf((HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).getScore()) * 1000)); - // player.sendMessage("finally"); - //} - - - - } - } diff --git a/src/iie/HelloWorld.java b/src/iie/HelloWorld.java index b479e65..dc91d63 100644 --- a/src/iie/HelloWorld.java +++ b/src/iie/HelloWorld.java @@ -1,6 +1,7 @@ package iie; import org.bukkit.Location; +import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,55 +21,46 @@ public class HelloWorld implements CommandExecutor { - + if (sender instanceof Player){ - - long currentTime = System.currentTimeMillis(); + + Player player = (Player) sender; String playername = sender.getName(); - long deathTime = 0; - //sender.sendMessage("Playername: " + playername); - //sender.sendMessage("Data Imported: " + HelloWorldPlugin.deathMap.get(playername)); + + World hardcoreWorld = player.getServer().getWorld("hardcore"); + Location location = new Location(hardcoreWorld, 1280, 71, -179); + + int currentTime = (int) ((System.currentTimeMillis())/1000); + int deathTime = 0; - if (HelloWorldPlugin.deathMap.get(playername) != null){ - deathTime = Long.parseLong(HelloWorldPlugin.deathMap.get(playername)); - //sender.sendMessage("DeathTime: " + deathTime); - }else{ - 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); - player.teleport(location); - } + if (HelloWorldPlugin.hardcoreTimeDead.getScore(playername) != null) //null check - if score exists + deathTime = HelloWorldPlugin.hardcoreTimeDead.getScore(playername).getScore(); //set deathTime to that score + - if (currentTime - deathTime >= 86400000 && deathTime != 0){ - 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); + if (currentTime - deathTime >= 86400 && deathTime != 0){ + sender.sendMessage("You died " + (86400 - (currentTime - deathTime)) /3600 + " hours ago. Ready to give it another shot?"); player.teleport(location); + // player.getWorld().playSound(player.getLocation(), Sound.AMBIENT_CAVE,0,0); I don't think this works, fix later }else if(deathTime == 0){ - sender.sendMessage("good luck"); + sender.sendMessage("You have never died in hardcore. Good luck!"); + player.teleport(location); + // player.getWorld().playSound(player.getLocation(), Sound.AMBIENT_CAVE,0,0); I don't think this works, fix later }else{ - sender.sendMessage("you are dead for the next " + (86400000 - (currentTime - deathTime) ) /3600000 + " hours"); + sender.sendMessage("you are dead for the next " + (86400 - (currentTime - deathTime) ) /3600 + " hours"); + // player.getWorld().playSound(player.getLocation(), Sound.AMBIENT_CAVE,0,0); I don't think this works, fix later + // replace sound with some other sound } - sender.sendMessage("Your hardcoreTimeDead score is " + String.valueOf(HelloWorldPlugin.hardcoreTimeDead.getScore(playername).getScore())); - - - }else{ sender.sendMessage("You must be a player to use this command!"); } - - - return false; } diff --git a/src/iie/HelloWorldPlugin.java b/src/iie/HelloWorldPlugin.java index 1d7ae4c..a7fe954 100644 --- a/src/iie/HelloWorldPlugin.java +++ b/src/iie/HelloWorldPlugin.java @@ -18,37 +18,21 @@ public class HelloWorldPlugin extends JavaPlugin { public void onEnable(){ - try{ - board.getObjective("hardcoreTimeDead"); - }catch (NullPointerException e){ - hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy"); - }finally{ - hardcoreTimeDead = board.getObjective("hardcoreTimeDead"); - } - - board = Bukkit.getServer().getScoreboardManager().getMainScoreboard(); if (board.getObjective("hardcoreTimeDead") != null){ hardcoreTimeDead = board.getObjective("hardcoreTimeDead"); }else{ hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy"); - } registerCommands(); + getServer().getPluginManager().registerEvents(new JoinListener(this), this); getServer().getPluginManager().registerEvents(new DeathListener(this), this); + } public void registerCommands(){ - getCommand("HelloWorld").setExecutor(new HelloWorld(this)); + getCommand("hardcore").setExecutor(new HelloWorld(this)); } -} - -//try{ -// board.getObjective("hardcoreTimeDead"); -//}catch (IllegalArgumentException e){ -// hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy"); -//}finally{ -// hardcoreTimeDead = board.getObjective("hardcoreTimeDead"); -//} \ No newline at end of file +} \ No newline at end of file diff --git a/src/iie/JoinListener.java b/src/iie/JoinListener.java new file mode 100644 index 0000000..86512b1 --- /dev/null +++ b/src/iie/JoinListener.java @@ -0,0 +1,33 @@ +package iie; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class JoinListener implements Listener { + + HelloWorldPlugin plugin; + public JoinListener(HelloWorldPlugin plugin){ + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent JoinEvent){ + + + Player player = JoinEvent.getPlayer(); + String playername = (String) player.getName(); + + + if (HelloWorldPlugin.hardcoreTimeDead.getScore(playername) == null) //null check + HelloWorldPlugin.hardcoreTimeDead.getScore(playername).setScore(0); //convert null to 0 + + + //HelloWorldPlugin.deathMap.put(playername, String.valueOf(HelloWorldPlugin.hardcoreTimeDead.getScore(playername).getScore())); + + + } + +}