changed command to "hardcore"

removed debug messages, removed usage of hashmap to rely entirely on
scoreboard,
This commit is contained in:
BuildTools 2016-09-04 21:33:53 -04:00
parent 134b395ac8
commit 7c82d7879a
5 changed files with 66 additions and 112 deletions

View file

@ -2,5 +2,5 @@
version: 1.0.0 version: 1.0.0
name: HelloWorldPlugin name: HelloWorldPlugin
commands: commands:
HelloWorld: hardcore:
description: Command that says Hello World! description: Command that teleports you to hardcore world

View file

@ -8,7 +8,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class DeathListener implements Listener { public class DeathListener implements Listener {
@ -20,72 +19,18 @@ public class DeathListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onHardcoreDeath(PlayerDeathEvent deathEvent){ 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(); Player player = deathEvent.getEntity();
String playerString = (String) player.getName(); String playername = (String) player.getName();
Location location = player.getLocation(); Location location = player.getLocation();
String worldString = (String) location.getWorld().getName(); String worldString = (String) location.getWorld().getName();
//player.sendMessage(timeString);
//player.sendMessage(playerString);
//player.sendMessage(worldString);
if (Objects.equals(worldString, "hardcore")){ if (Objects.equals(worldString, "hardcore")){
HelloWorldPlugin.deathMap.put(playerString, timeString);
if (HelloWorldPlugin.hardcoreTimeDead.getScore(playername) == null) //null check
if (HelloWorldPlugin.hardcoreTimeDead == null){ HelloWorldPlugin.hardcoreTimeDead.getScore(playername).setScore(0); //convert null to 0
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()));
}
HelloWorldPlugin.hardcoreTimeDead.getScore(playerString).setScore((int) (System.currentTimeMillis()/1000)); HelloWorldPlugin.hardcoreTimeDead.getScore(playername).setScore(currentTime);
//player.sendMessage(HelloWorldPlugin.deathMap.toString());
//player.sendMessage("Key saved: " + playerString);
//player.sendMessage("Data saved: " + HelloWorldPlugin.deathMap.get(playerString));
} }
} }
@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");
//}
}
} }

View file

@ -1,6 +1,7 @@
package iie; package iie;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -20,55 +21,46 @@ public class HelloWorld implements CommandExecutor {
if (sender instanceof Player){ if (sender instanceof Player){
long currentTime = System.currentTimeMillis(); Player player = (Player) sender;
String playername = sender.getName(); String playername = sender.getName();
long deathTime = 0;
//sender.sendMessage("Playername: " + playername); World hardcoreWorld = player.getServer().getWorld("hardcore");
//sender.sendMessage("Data Imported: " + HelloWorldPlugin.deathMap.get(playername)); Location location = new Location(hardcoreWorld, 1280, 71, -179);
int currentTime = (int) ((System.currentTimeMillis())/1000);
int deathTime = 0;
if (HelloWorldPlugin.deathMap.get(playername) != null){ if (HelloWorldPlugin.hardcoreTimeDead.getScore(playername) != null) //null check - if score exists
deathTime = Long.parseLong(HelloWorldPlugin.deathMap.get(playername)); deathTime = HelloWorldPlugin.hardcoreTimeDead.getScore(playername).getScore(); //set deathTime to that score
//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 (currentTime - deathTime >= 86400000 && deathTime != 0){ if (currentTime - deathTime >= 86400 && deathTime != 0){
sender.sendMessage("You died " + (86400000 - (currentTime - deathTime) ) /3600000 + " hours ago. Ready to give it another shot?"); sender.sendMessage("You died " + (86400 - (currentTime - deathTime)) /3600 + " 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);
player.teleport(location); 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){ }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{ }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{ }else{
sender.sendMessage("You must be a player to use this command!"); sender.sendMessage("You must be a player to use this command!");
} }
return false; return false;
} }

View file

@ -18,37 +18,21 @@ public class HelloWorldPlugin extends JavaPlugin {
public void onEnable(){ 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(); board = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
if (board.getObjective("hardcoreTimeDead") != null){ if (board.getObjective("hardcoreTimeDead") != null){
hardcoreTimeDead = board.getObjective("hardcoreTimeDead"); hardcoreTimeDead = board.getObjective("hardcoreTimeDead");
}else{ }else{
hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy"); hardcoreTimeDead = board.registerNewObjective("hardcoreTimeDead", "dummy");
} }
registerCommands(); registerCommands();
getServer().getPluginManager().registerEvents(new JoinListener(this), this);
getServer().getPluginManager().registerEvents(new DeathListener(this), this); getServer().getPluginManager().registerEvents(new DeathListener(this), this);
} }
public void registerCommands(){ 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");
//}

33
src/iie/JoinListener.java Normal file
View file

@ -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()));
}
}