diff --git a/.idea/artifacts/PlayrimeRewards___Rewritten__jar.xml b/.idea/artifacts/PlayrimeRewards___Rewritten__jar.xml new file mode 100644 index 0000000..23384eb --- /dev/null +++ b/.idea/artifacts/PlayrimeRewards___Rewritten__jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/PlayrimeRewards___Rewritten__jar + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..59b11d1 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index c811125..93a7be0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ org.example PlayrimeRewards+ (Rewritten) 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandPlaytime.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandPlaytime.java new file mode 100644 index 0000000..2775139 --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandPlaytime.java @@ -0,0 +1,28 @@ +package dev.astridlabs.playtimerewardsplus.Commands; + +import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandPlaytime implements CommandExecutor { + private Plugin plugin; + + public CommandPlaytime(Plugin plugin){ + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] strings) { + if(sender instanceof Player){ + Player player = (Player) sender; + + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix + "&aYour current playtime is: &7"+Integer.toString(plugin.db.getPlayer(player.getUniqueId()).getPlaytime()/3600)+" Hours")); + } + + return true; + } +} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandSeen.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandSeen.java new file mode 100644 index 0000000..35a3491 --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/CommandSeen.java @@ -0,0 +1,36 @@ +package dev.astridlabs.playtimerewardsplus.Commands; + +import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandSeen implements CommandExecutor { + private Plugin plugin; + + public CommandSeen(Plugin plugin){ + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if(sender instanceof Player){ + Player player = (Player) sender; + + String pName = args[0]; + Player mentioned = Bukkit.getPlayer(pName); + + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+"&8--------&7 "+mentioned.getDisplayName()+" &8--------")); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+"&aPlayers Playtime: &7"+Integer.toString(plugin.db.getPlayer(mentioned.getUniqueId()).getPlaytime()/3600)+" Hours")); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+"&aPlayers Logoff Location (X,Y,Z): &7"+plugin.config.getSavedX(mentioned.getUniqueId())+" "+plugin.config.getSavedY(mentioned.getUniqueId())+" "+plugin.config.getSavedZ(mentioned.getUniqueId()))); + + + } + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/Debug.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/Debug.java new file mode 100644 index 0000000..f7a5340 --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Commands/Debug.java @@ -0,0 +1,42 @@ +package dev.astridlabs.playtimerewardsplus.Commands; + +import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Debug implements CommandExecutor { + private Plugin plugin; + private String DebugPrefix = "&7[&cDEBUG MODE&7] "; + + public Debug(Plugin plugin){ + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if(sender instanceof Player){ + Player player = (Player) sender; + if(args[0].equals("time")){ + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+DebugPrefix + "&aUser playtime in seconds is: "+this.plugin.db.getPlayer(player.getUniqueId()).getPlaytime())); + } + + if(args[0].equals("location")){ + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+DebugPrefix + "&aLast player locaiton coordinates (X,Y,Z): "+this.plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getX()+" "+this.plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getY()+" "+this.plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getZ())); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+DebugPrefix + "&aCurrent player locaiton coordinates (X,Y,Z): "+this.plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getX()+" "+this.plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getY()+" "+this.plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getZ())); + + } + + if(args[0].equals("location")){ + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.PluginChatPrefix+DebugPrefix + "&aForce dumping memory to disk, this could cause file corruption")); + plugin.config.savePlayers(); + + } + } + + return true; + } +} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/ConfigController.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/ConfigController.java new file mode 100644 index 0000000..58b9dbe --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/ConfigController.java @@ -0,0 +1,68 @@ +package dev.astridlabs.playtimerewardsplus.Controllers; + +import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; +import java.util.logging.Level; + +public class ConfigController { + private Plugin plugin; + private File customConfigFile; + private FileConfiguration customConfig; + + + public ConfigController(Plugin plugin){ + this.plugin = plugin; + this.customConfig = plugin.getConfig(); + } + + public void savePlayers(){ + for (PlayerData player : plugin.db.playerList) { + customConfig.set("data."+player.getUuid()+".playtime", player.getPlaytime()); + customConfig.set("data."+player.getUuid()+".displayName", player.getDisplayName()); + customConfig.set("data."+player.getUuid()+".lastLocation.X", player.getCurrentLocation().getBlockX()); + customConfig.set("data."+player.getUuid()+".lastLocation.Y", player.getCurrentLocation().getBlockY()); + customConfig.set("data."+player.getUuid()+".lastLocation.Z", player.getCurrentLocation().getBlockZ()); + } + try { + customConfig.save(customConfigFile); + } catch (IOException e) { + this.plugin.getServer().getLogger().log(Level.SEVERE, "ERROR OCCURED WHILE SAVING! DATA LOSS COULD OCCUR! ", e); + } + } + + ///////////////////////////// + // Custom Config Handler // + ///////////////////////////// + public FileConfiguration getCustomConfig() { + return this.customConfig; + } + + public void createCustomConfig() throws IOException, InvalidConfigurationException { + customConfigFile = new File(plugin.getDataFolder(), "config.yml"); + if (!customConfigFile.exists()) { + customConfigFile.getParentFile().mkdirs(); + plugin.saveResource("config.yml", false); + } + + customConfig= new YamlConfiguration(); + customConfig.load(customConfigFile); + + } + + public String getSavedX(UUID uuid){ + return this.customConfig.getString("data."+uuid.toString()+"lastLocation.X"); + } + public String getSavedY(UUID uuid){ + return this.customConfig.getString("data."+uuid.toString()+"lastLocation.Y"); + } + public String getSavedZ(UUID uuid){ + return this.customConfig.getString("data."+uuid.toString()+"lastLocation.Z"); + } +} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/GASM.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/GASM.java deleted file mode 100644 index 7cb903d..0000000 --- a/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/GASM.java +++ /dev/null @@ -1,10 +0,0 @@ -// GASM.java (General Activation Sequence Manager) -// - In charge of making sure things do their thing when they are supposed to -// -// (c) Owen Rummage 2020 - -package dev.astridlabs.playtimerewardsplus.Controllers; - -public class GASM { - -} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/PlayerDatabase.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/PlayerDatabase.java index 0065679..2447555 100644 --- a/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/PlayerDatabase.java +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Controllers/PlayerDatabase.java @@ -6,6 +6,7 @@ package dev.astridlabs.playtimerewardsplus.Controllers; import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -13,8 +14,15 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; + public class PlayerDatabase { public List playerList = new ArrayList(); + public Plugin plugin; + + public PlayerDatabase(Plugin plugin){ + this.plugin = plugin; + } + public PlayerData getPlayer(UUID uuid){ PlayerData returnedPlayer = null; diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Events/PlayerJoin.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Events/PlayerJoin.java new file mode 100644 index 0000000..e0937ff --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Events/PlayerJoin.java @@ -0,0 +1,23 @@ +package dev.astridlabs.playtimerewardsplus.Events; + +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class PlayerJoin implements Listener { + + public Plugin plugin; + + public PlayerJoin(Plugin pl){ + this.plugin = pl; + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) + { + event.setJoinMessage("Welcome, " + event.getPlayer().getName() + "!"); + plugin.db.createPlayer(event.getPlayer()); + } +} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Integrations/PlaceholderAPIIntegration.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Integrations/PlaceholderAPIIntegration.java new file mode 100644 index 0000000..342966b --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Integrations/PlaceholderAPIIntegration.java @@ -0,0 +1,86 @@ +// PlaceholderAPIIntegration.java +// - Send data to PlaceholderAPI for other plugins to use +// +// (c) Owen Rummage 2020 +package dev.astridlabs.playtimerewardsplus.Integrations; + +import dev.astridlabs.playtimerewardsplus.Plugin; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + + +public class PlaceholderAPIIntegration extends PlaceholderExpansion { + + /* + * The identifier, shouldn't contain any _ or % + */ + private Plugin plugin; + + public PlaceholderAPIIntegration(Plugin plugin){ + this.plugin = plugin; + } + + @Override + public boolean persist(){ + return true; + } + @Override + public boolean canRegister(){ + return true; + } + @Override + public String getAuthor(){ + return "O_dude"; + } + @Override + public String getIdentifier(){ + return "playtimerewards"; + } + @Override + public String getVersion(){ + return plugin.getDescription().getVersion(); + } + + + + public String onPlaceholderRequest(Player player, String identifier) { + + // We check if the player is null (not online) before any player-related placeholder + if(player == null){ + return ""; + } + + // Placeholder: %playtimerewards_prefix% + if(identifier.equals("prefix")) { + return plugin.PlaceholderChatPrefix.replace("%time%", "&a"+ Integer.toString(plugin.db.getPlayer(player.getUniqueId()).getPlaytime()/3600)+"&7h"); + } + + if(identifier.equals("playtime")) { + return Integer.toString(plugin.db.getPlayer(player.getUniqueId()).getPlaytime()); + } + + if(identifier.equals("lastloc_x")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getX()); + } + if(identifier.equals("lastloc_y")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getY()); + } + if(identifier.equals("lastloc_z")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getLastLocation().getZ()); + } + if(identifier.equals("curloc_x")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getX()); + } + if(identifier.equals("curloc_y")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getY()); + } + if(identifier.equals("curloc_z")) { + return Double.toString(plugin.db.getPlayer(player.getUniqueId()).getCurrentLocation().getZ()); + } + + + // We return null, if an invalid placeholder was called. + return null; + } +} \ No newline at end of file diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/MasterClock.java b/src/main/java/dev/astridlabs/playtimerewardsplus/MasterClock.java new file mode 100644 index 0000000..72a50a3 --- /dev/null +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/MasterClock.java @@ -0,0 +1,53 @@ +package dev.astridlabs.playtimerewardsplus; + +import dev.astridlabs.playtimerewardsplus.DataTypes.PlayerData; +import dev.astridlabs.playtimerewardsplus.Plugin; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.logging.Level; + +public class MasterClock extends BukkitRunnable { + private final Plugin plugin; + private Integer clockTicks = 0; + + public MasterClock(Plugin plugin){ + this.plugin = plugin; + } + + + @Override + public void run() { + clockTicks++; + + if((clockTicks/72000) > 1){ + this.plugin.config.savePlayers(); + this.plugin.getServer().getLogger().log(Level.INFO, "Hourly save completed! Data loss avoided"); + } + + // What you want to schedule goes here + for(Player p : plugin.getServer().getOnlinePlayers()) { + updatePlaytime(p); + updatePlayerLocations(p); + } + } + + public void updatePlaytime(Player pl){ + PlayerData player = plugin.db.getPlayer(pl.getUniqueId()); + Integer playtime = player.getPlaytime(); + Integer newplaytime = playtime+1; + player.setPlaytime(newplaytime); + } + + public void updatePlayerLocations(Player pl){ + PlayerData player = plugin.db.getPlayer(pl.getUniqueId()); + + if(player.getCurrentLocation() == null){ + player.setCurrentLocation(pl.getLocation()); + }else{ + player.setLastLocation(player.getCurrentLocation()); + player.setCurrentLocation(pl.getLocation()); + } + + } +} diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/Plugin.java b/src/main/java/dev/astridlabs/playtimerewardsplus/Plugin.java index 7b2473f..66b735b 100644 --- a/src/main/java/dev/astridlabs/playtimerewardsplus/Plugin.java +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/Plugin.java @@ -1,16 +1,66 @@ package dev.astridlabs.playtimerewardsplus; +import dev.astridlabs.playtimerewardsplus.Commands.CommandPlaytime; +import dev.astridlabs.playtimerewardsplus.Commands.CommandSeen; +import dev.astridlabs.playtimerewardsplus.Commands.Debug; +import dev.astridlabs.playtimerewardsplus.Controllers.ConfigController; +import dev.astridlabs.playtimerewardsplus.Controllers.PlayerDatabase; +import dev.astridlabs.playtimerewardsplus.Events.PlayerJoin; +import dev.astridlabs.playtimerewardsplus.Integrations.PlaceholderAPIIntegration; +import org.bukkit.Bukkit; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; + +import java.io.IOException; +import java.util.logging.Level; public class Plugin extends JavaPlugin { + //Initialize Everything + public PlayerDatabase db = new PlayerDatabase(this); + public BukkitScheduler scheduler = getServer().getScheduler(); + public ConfigController config = new ConfigController(this); + + //Config + public String PluginChatPrefix; + public String PlaceholderChatPrefix; + @Override public void onEnable(){ + //Get the clock running + new MasterClock(this).runTaskTimer(this, 0L, 20L); + + + try { + config.createCustomConfig(); + } catch (IOException e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to createCustomConfig"); + } catch (InvalidConfigurationException e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to createCustomConfig"); + } + + + + PlaceholderChatPrefix = getConfig().getString("Config.PlaceholderChatPrefix")+" "; + PluginChatPrefix = getConfig().getString("Config.PluginChatPrefix")+" "; + //Initialize Events + Bukkit.getPluginManager().registerEvents(new PlayerJoin(this), this); // This fires the event and allows any listener to listen to the event + + //Only initialize if PlaceholderAPI is actually installed + if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + new PlaceholderAPIIntegration(this).register(); + } + + //Initialize Commands + this.getCommand("pt").setExecutor(new CommandPlaytime(this)); + this.getCommand("ptdebug").setExecutor(new Debug(this)); + this.getCommand("ptseen").setExecutor(new CommandSeen(this)); } @Override public void onDisable(){ - + config.savePlayers(); } } diff --git a/src/main/java/dev/astridlabs/playtimerewardsplus/dataTypes/PlayerData.java b/src/main/java/dev/astridlabs/playtimerewardsplus/dataTypes/PlayerData.java index a1843d5..2be1820 100644 --- a/src/main/java/dev/astridlabs/playtimerewardsplus/dataTypes/PlayerData.java +++ b/src/main/java/dev/astridlabs/playtimerewardsplus/dataTypes/PlayerData.java @@ -14,7 +14,7 @@ import java.util.UUID; public class PlayerData { - private Integer Playtime; + private int Playtime = 0; private UUID uuid; private String DisplayName; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..cab13e8 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,5 @@ +Config: + PluginChatPrefix: "&7[&6PTRR+&7]" + PlaceholderChatPrefix: "&8[%time%&8]&f" + +data: {} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ca35029..46b430a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,17 @@ -name: PlaytimeRewards+(Rewritten) +main: dev.astridlabs.playtimerewardsplus.Plugin +name: PlaytimeRewardsRewrittenPlus author: Astrid (O_dude) -version: 0.1 +version: 0.1a commands: + pt: + description: This command shows your current playtime + usage: /pt + ptdebug: + description: This is a debug command for debugging stuff + usage: /ptdebug + ptseen: + description: Get information about a player + usage: /ptseen + +softdepend: [PlaceholderAPI] # This is used, if your plugin works without PlaceholderAPI.