diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e0f15db..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "java.configuration.updateBuildConfiguration": "automatic" -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7a4e11d..e1ff7b0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ starlin_l2 com.github.katorly - 1.0.0 + 1.0.1 diff --git a/src/main/java/com/github/katorly/starlin_l2/EventListener.java b/src/main/java/com/github/katorly/starlin_l2/EventListener.java index ca6796b..2b15850 100644 --- a/src/main/java/com/github/katorly/starlin_l2/EventListener.java +++ b/src/main/java/com/github/katorly/starlin_l2/EventListener.java @@ -1,24 +1,63 @@ package com.github.katorly.starlin_l2; -import com.github.katorly.starlin_l2.backup.MessageSender; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +import com.github.katorly.starlin_l2.backup.configReader; +import com.github.katorly.starlin_l2.backup.messageSender; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.scheduler.BukkitRunnable; public class EventListener implements Listener { - @EventHandler //Prevent players from stucking in the Nether Portal when logging in. + @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { - Player p = e.getPlayer(); + + FileConfiguration timedata = starlin_l2.timedata.getConfig(); //Check whether player has joined before. + String u = e.getPlayer().getUniqueId().toString(); + if (!starlin_l2.timedata.getConfig().contains(u)) { //if not + timedata.set(u + ".name", e.getPlayer().getName()); + long t = System.currentTimeMillis(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm"); + String timenow = dateFormat.format(t); + timedata.set(u + ".first-time", timenow); + timedata.set(u + ".total", 0.0); + configReader.save(starlin_l2.timedata); + } + + FileConfiguration monthly = starlin_l2.monthly.getConfig(); //Record monthly players. + String pname = e.getPlayer().getName(); + long t = System.currentTimeMillis(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM"); + String timenow = dateFormat.format(t); + if (!starlin_l2.monthly.getConfig().contains(timenow)) { + List plist = new ArrayList(); + plist.add(pname); + monthly.set(timenow, plist); + configReader.save(starlin_l2.monthly); + } else { + List plist = monthly.getStringList(timenow); + if (!plist.contains(pname)) { + plist.add(pname); + monthly.set(timenow, plist); + configReader.save(starlin_l2.monthly); + } + } + + final Player p = e.getPlayer(); //Prevent players from stucking in the Nether Portal when logging in. Location l = p.getLocation(); - World w = l.getWorld(); - double x = l.getX(); double y= l.getY(); double z = l.getZ(); + final World w = l.getWorld(); + final double x = l.getX(); final double y= l.getY(); final double z = l.getZ(); Location l_xl = new Location(w, x + 1, y, z); Location l_xr = new Location(w, x - 1, y, z); Location l_up = new Location(w, x, y + 1, z); @@ -29,9 +68,16 @@ public void onPlayerJoin(PlayerJoinEvent e) { || (l_xl.getBlock().getType() == Material.NETHER_PORTAL) || (l_xr.getBlock().getType() == Material.NETHER_PORTAL) || (l_up.getBlock().getType() == Material.NETHER_PORTAL) || (l_down.getBlock().getType() == Material.NETHER_PORTAL) || (l_zl.getBlock().getType() == Material.NETHER_PORTAL) || (l_zr.getBlock().getType() == Material.NETHER_PORTAL)) { - Location spawn = w.getSpawnLocation(); - p.teleport(spawn); - MessageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7检测到您在下界门处登录, 为防止您无法正常登录, 已将您传送到出生点!"); + new BukkitRunnable() { + @Override + public void run() { + Location spawn = w.getSpawnLocation(); + p.teleport(spawn); + messageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7检测到您在下界门处登录, 为防止您无法正常登录, 已将您传送到出生点!"); + String x0 = String.format("%.2f", x); String y0 = String.format("%.2f", y); String z0 = String.format("%.2f", z); + messageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7下界门位置: " + x0 + ", " + y0 + ", " + z0); + } + }.runTaskLater(starlin_l2.INSTANCE, 4L); } } diff --git a/src/main/java/com/github/katorly/starlin_l2/backup/configReader.java b/src/main/java/com/github/katorly/starlin_l2/backup/configReader.java new file mode 100644 index 0000000..91323ca --- /dev/null +++ b/src/main/java/com/github/katorly/starlin_l2/backup/configReader.java @@ -0,0 +1,90 @@ +package com.github.katorly.starlin_l2.backup; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class configReader { + private File file; + private File filepath; + private final JavaPlugin plugin; + private final String name; + private final String path; + private FileConfiguration config; + + /** + * Create the default config if the config does not exist. + * + */ + public void saveDefaultConfig() { + if (!filepath.exists()) { + boolean success = filepath.mkdirs(); + if (!success) + Bukkit.getLogger().severe("Error creating the config. Please try again."); + } + if (!file.exists()) + this.plugin.saveResource(path + name, false); + } + + /** + * Get values in the config. + * + * @return + */ + public FileConfiguration getConfig() { + if (!filepath.exists()) + this.saveDefaultConfig(); + if (config == null) + this.reloadConfig(); + return config; + } + + /** + * Reload the config. This will remove all the comments in it. + * + */ + public void reloadConfig() { + if (filepath == null) + filepath = new File(plugin.getDataFolder(), path); + if (file == null) + file = new File(filepath, name); + config = YamlConfiguration.loadConfiguration(file); + InputStream stream = plugin.getResource(name); + if (stream != null) { + YamlConfiguration YmlFile = YamlConfiguration.loadConfiguration(new InputStreamReader(stream)); + config.setDefaults(YmlFile); + } + } + + /** + * Save the config to apply changes. + * + */ + public void saveConfig() { + if (!filepath.exists()) + this.saveDefaultConfig(); + try { + config.save(file); + } catch (Throwable t) { + Bukkit.getLogger().severe("Error saving the config. Please try again."); + } + } + + public configReader(JavaPlugin plugin, String pathname, String filename) { + this.plugin = plugin; + this.path = pathname; + this.name = filename; + this.filepath = new File(plugin.getDataFolder(), path); + this.file = new File(filepath, name); + } + + public static void save(configReader config) { + config.saveConfig(); + config.reloadConfig(); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/katorly/starlin_l2/backup/MessageSender.java b/src/main/java/com/github/katorly/starlin_l2/backup/messageSender.java similarity index 78% rename from src/main/java/com/github/katorly/starlin_l2/backup/MessageSender.java rename to src/main/java/com/github/katorly/starlin_l2/backup/messageSender.java index 2d202a5..789e894 100644 --- a/src/main/java/com/github/katorly/starlin_l2/backup/MessageSender.java +++ b/src/main/java/com/github/katorly/starlin_l2/backup/messageSender.java @@ -5,7 +5,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; -public class MessageSender { +public class messageSender { /** * Replace "&" with "§" to fix color messages. * @@ -23,7 +23,7 @@ public static String Color(String string) { * @param message */ public static void sendMessage(Player player, String message) { - player.sendMessage(MessageSender.Color(message)); + player.sendMessage(messageSender.Color(message)); } /** @@ -33,7 +33,7 @@ public static void sendMessage(Player player, String message) { */ public static void broadcastMessage(String message) { for (Player player : Bukkit.getOnlinePlayers()) { - player.sendMessage(MessageSender.Color(message)); + player.sendMessage(messageSender.Color(message)); } } @@ -43,7 +43,7 @@ public static void broadcastMessage(String message) { * @param message */ public static void broadcastMessageAll(String message) { - Bukkit.broadcastMessage(MessageSender.Color(message)); + Bukkit.broadcastMessage(messageSender.Color(message)); } /** @@ -54,7 +54,7 @@ public static void broadcastMessageAll(String message) { * @param subtitle */ public static void sendTitle(Player player, String title, String subtitle) { - player.sendTitle(MessageSender.Color(title), MessageSender.Color(subtitle), 10, 40, 20); // Show title 2s + player.sendTitle(messageSender.Color(title), messageSender.Color(subtitle), 10, 40, 20); // Show title 2s } /** @@ -65,7 +65,7 @@ public static void sendTitle(Player player, String title, String subtitle) { */ public static void broadcastTitle(String title, String subtitle) { for (Player player : Bukkit.getOnlinePlayers()) { - player.sendTitle(MessageSender.Color(title), MessageSender.Color(subtitle), 10, 40, 20); // Show title 2s + player.sendTitle(messageSender.Color(title), messageSender.Color(subtitle), 10, 40, 20); // Show title 2s } } } \ No newline at end of file diff --git a/src/main/java/com/github/katorly/starlin_l2/starlin_l2.java b/src/main/java/com/github/katorly/starlin_l2/starlin_l2.java index 78ba5cd..3a087b4 100644 --- a/src/main/java/com/github/katorly/starlin_l2/starlin_l2.java +++ b/src/main/java/com/github/katorly/starlin_l2/starlin_l2.java @@ -1,26 +1,94 @@ package com.github.katorly.starlin_l2; +import java.text.SimpleDateFormat; + +import com.github.katorly.starlin_l2.backup.configReader; + import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; public class starlin_l2 extends JavaPlugin { public static starlin_l2 INSTANCE; - public starlin_l2() { INSTANCE = this; } + + public static configReader timedata; + public static configReader monthly; @Override public void onEnable() { getServer().getPluginManager().registerEvents(new EventListener(),this); - Bukkit.getLogger().info("[Starlin_L2] Author: Katorly"); - Bukkit.getLogger().info("[Starlin_L2] Starlin_L2 enabled! Made for StarlinWorld server only."); + timedata = new configReader(this,"","timedata.yml"); + timedata.saveDefaultConfig(); + monthly = new configReader(this,"","monthly.yml"); + monthly.saveDefaultConfig(); + Bukkit.getLogger().info("[starlin_l2] Repo: https://github.com/katorlys/Starlin_L2"); + Bukkit.getLogger().info("[starlin_l2] Starlin_L2 enabled! Made for StarlinWorld server only."); + this.timeCounter(); } @Override public void onDisable() { HandlerList.unregisterAll(this); - Bukkit.getLogger().info("[Starlin_L2] Starlin_L2 disabled!"); + configReader.save(timedata); + Bukkit.getLogger().info("[starlin_l2] Starlin_L2 disabled!"); + } + + public void timeCounter() { //Counts the player's play time. + new BukkitRunnable() { + @Override + public void run() { + for (Player player : Bukkit.getOnlinePlayers()) { + FileConfiguration timedata = starlin_l2.timedata.getConfig(); + String u = player.getUniqueId().toString(); + if (!starlin_l2.timedata.getConfig().contains(u)) { //if data not exist + timedata.set(u + ".name", player.getName()); + long t = System.currentTimeMillis(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm"); + String timenow = dateFormat.format(t); + timedata.set(u + ".first-time", timenow); + timedata.set(u + ".total", 0.0); + configReader.save(starlin_l2.timedata); + } else { + Double newtotal = Double.valueOf(String.format("%.2f", timedata.getDouble(u + ".total"))) + 0.1; //if data exist + timedata.set(u + ".total", newtotal); + configReader.save(starlin_l2.timedata); + // + // Failed to achieve the following function: Count the player's play time every month. + // + // + //long t = System.currentTimeMillis(); + //SimpleDateFormat d = new SimpleDateFormat("yyyy"); + //String y = d.format(t); + //if (!starlin_l2.timedata.getConfig().contains(u + "." + y)) { //if data not exist + // String ytime = "0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0"; + // String[] mtime = ytime.split(","); + // SimpleDateFormat n = new SimpleDateFormat("M"); + // String m = n.format(t); + // int month = Integer.valueOf(m); + // mtime[month - 1] = String.valueOf(Double.valueOf(mtime[month - 1]) + 0.1); + // String newtime = String.join(",", mtime); + // timedata.set(u + "." + y, newtime); + // configReader.save(starlin_l2.timedata); + //} else { + // String ytime = timedata.getString(u + "." + y); //if data exist + // String[] mtime = ytime.split(","); + // SimpleDateFormat n = new SimpleDateFormat("M"); + // String m = n.format(t); + // int month = Integer.valueOf(m); + // mtime[month - 1] = String.valueOf(Double.valueOf(mtime[month - 1]) + 0.1); + // String newtime = String.join(",", mtime); + // timedata.set(u + "." + y, newtime); + // configReader.save(starlin_l2.timedata); + //} + } + } + } + }.runTaskTimer(this, 7200L, 7200L); } -} +} \ No newline at end of file diff --git a/src/main/resources/monthly.yml b/src/main/resources/monthly.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index db8d79f..4b5e6fd 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: starlin_l2 -version: "1.0.0" +version: "1.0.1" author: Katorly main: com.github.katorly.starlin_l2.starlin_l2 api-version: 1.18 diff --git a/src/main/resources/timedata.yml b/src/main/resources/timedata.yml new file mode 100644 index 0000000..e69de29