Skip to content

Commit 58b0cb7

Browse files
committed
v1.0.1
1 parent 4b430ad commit 58b0cb7

File tree

9 files changed

+225
-24
lines changed

9 files changed

+225
-24
lines changed

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<artifactId>starlin_l2</artifactId>
66
<groupId>com.github.katorly</groupId>
7-
<version>1.0.0</version>
7+
<version>1.0.1</version>
88

99
<!-- Repositories -->
1010
<repositories>

src/main/java/com/github/katorly/starlin_l2/EventListener.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
11
package com.github.katorly.starlin_l2;
22

3-
import com.github.katorly.starlin_l2.backup.MessageSender;
3+
import java.text.SimpleDateFormat;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.github.katorly.starlin_l2.backup.configReader;
8+
import com.github.katorly.starlin_l2.backup.messageSender;
49

510
import org.bukkit.Location;
611
import org.bukkit.Material;
712
import org.bukkit.World;
13+
import org.bukkit.configuration.file.FileConfiguration;
814
import org.bukkit.entity.Player;
915
import org.bukkit.event.EventHandler;
1016
import org.bukkit.event.Listener;
1117
import org.bukkit.event.block.Action;
1218
import org.bukkit.event.player.PlayerInteractEvent;
1319
import org.bukkit.event.player.PlayerJoinEvent;
20+
import org.bukkit.scheduler.BukkitRunnable;
1421

1522
public class EventListener implements Listener {
16-
@EventHandler //Prevent players from stucking in the Nether Portal when logging in.
23+
@EventHandler
1724
public void onPlayerJoin(PlayerJoinEvent e) {
18-
Player p = e.getPlayer();
25+
26+
FileConfiguration timedata = starlin_l2.timedata.getConfig(); //Check whether player has joined before.
27+
String u = e.getPlayer().getUniqueId().toString();
28+
if (!starlin_l2.timedata.getConfig().contains(u)) { //if not
29+
timedata.set(u + ".name", e.getPlayer().getName());
30+
long t = System.currentTimeMillis();
31+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
32+
String timenow = dateFormat.format(t);
33+
timedata.set(u + ".first-time", timenow);
34+
timedata.set(u + ".total", 0.0);
35+
configReader.save(starlin_l2.timedata);
36+
}
37+
38+
FileConfiguration monthly = starlin_l2.monthly.getConfig(); //Record monthly players.
39+
String pname = e.getPlayer().getName();
40+
long t = System.currentTimeMillis();
41+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM");
42+
String timenow = dateFormat.format(t);
43+
if (!starlin_l2.monthly.getConfig().contains(timenow)) {
44+
List<String> plist = new ArrayList<String>();
45+
plist.add(pname);
46+
monthly.set(timenow, plist);
47+
configReader.save(starlin_l2.monthly);
48+
} else {
49+
List<String> plist = monthly.getStringList(timenow);
50+
if (!plist.contains(pname)) {
51+
plist.add(pname);
52+
monthly.set(timenow, plist);
53+
configReader.save(starlin_l2.monthly);
54+
}
55+
}
56+
57+
final Player p = e.getPlayer(); //Prevent players from stucking in the Nether Portal when logging in.
1958
Location l = p.getLocation();
20-
World w = l.getWorld();
21-
double x = l.getX(); double y= l.getY(); double z = l.getZ();
59+
final World w = l.getWorld();
60+
final double x = l.getX(); final double y= l.getY(); final double z = l.getZ();
2261
Location l_xl = new Location(w, x + 1, y, z);
2362
Location l_xr = new Location(w, x - 1, y, z);
2463
Location l_up = new Location(w, x, y + 1, z);
@@ -29,9 +68,16 @@ public void onPlayerJoin(PlayerJoinEvent e) {
2968
|| (l_xl.getBlock().getType() == Material.NETHER_PORTAL) || (l_xr.getBlock().getType() == Material.NETHER_PORTAL)
3069
|| (l_up.getBlock().getType() == Material.NETHER_PORTAL) || (l_down.getBlock().getType() == Material.NETHER_PORTAL)
3170
|| (l_zl.getBlock().getType() == Material.NETHER_PORTAL) || (l_zr.getBlock().getType() == Material.NETHER_PORTAL)) {
32-
Location spawn = w.getSpawnLocation();
33-
p.teleport(spawn);
34-
MessageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7检测到您在下界门处登录, 为防止您无法正常登录, 已将您传送到出生点!");
71+
new BukkitRunnable() {
72+
@Override
73+
public void run() {
74+
Location spawn = w.getSpawnLocation();
75+
p.teleport(spawn);
76+
messageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7检测到您在下界门处登录, 为防止您无法正常登录, 已将您传送到出生点!");
77+
String x0 = String.format("%.2f", x); String y0 = String.format("%.2f", y); String z0 = String.format("%.2f", z);
78+
messageSender.sendMessage(p, "&b&l星林宇宙 &r&8>> &7下界门位置: " + x0 + ", " + y0 + ", " + z0);
79+
}
80+
}.runTaskLater(starlin_l2.INSTANCE, 4L);
3581
}
3682
}
3783

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.github.katorly.starlin_l2.backup;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.configuration.file.FileConfiguration;
5+
import org.bukkit.configuration.file.YamlConfiguration;
6+
import org.bukkit.plugin.java.JavaPlugin;
7+
8+
import java.io.File;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
12+
public class configReader {
13+
private File file;
14+
private File filepath;
15+
private final JavaPlugin plugin;
16+
private final String name;
17+
private final String path;
18+
private FileConfiguration config;
19+
20+
/**
21+
* Create the default config if the config does not exist.
22+
*
23+
*/
24+
public void saveDefaultConfig() {
25+
if (!filepath.exists()) {
26+
boolean success = filepath.mkdirs();
27+
if (!success)
28+
Bukkit.getLogger().severe("Error creating the config. Please try again.");
29+
}
30+
if (!file.exists())
31+
this.plugin.saveResource(path + name, false);
32+
}
33+
34+
/**
35+
* Get values in the config.
36+
*
37+
* @return
38+
*/
39+
public FileConfiguration getConfig() {
40+
if (!filepath.exists())
41+
this.saveDefaultConfig();
42+
if (config == null)
43+
this.reloadConfig();
44+
return config;
45+
}
46+
47+
/**
48+
* Reload the config. This will remove all the comments in it.
49+
*
50+
*/
51+
public void reloadConfig() {
52+
if (filepath == null)
53+
filepath = new File(plugin.getDataFolder(), path);
54+
if (file == null)
55+
file = new File(filepath, name);
56+
config = YamlConfiguration.loadConfiguration(file);
57+
InputStream stream = plugin.getResource(name);
58+
if (stream != null) {
59+
YamlConfiguration YmlFile = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
60+
config.setDefaults(YmlFile);
61+
}
62+
}
63+
64+
/**
65+
* Save the config to apply changes.
66+
*
67+
*/
68+
public void saveConfig() {
69+
if (!filepath.exists())
70+
this.saveDefaultConfig();
71+
try {
72+
config.save(file);
73+
} catch (Throwable t) {
74+
Bukkit.getLogger().severe("Error saving the config. Please try again.");
75+
}
76+
}
77+
78+
public configReader(JavaPlugin plugin, String pathname, String filename) {
79+
this.plugin = plugin;
80+
this.path = pathname;
81+
this.name = filename;
82+
this.filepath = new File(plugin.getDataFolder(), path);
83+
this.file = new File(filepath, name);
84+
}
85+
86+
public static void save(configReader config) {
87+
config.saveConfig();
88+
config.reloadConfig();
89+
}
90+
}

src/main/java/com/github/katorly/starlin_l2/backup/MessageSender.java renamed to src/main/java/com/github/katorly/starlin_l2/backup/messageSender.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.bukkit.Bukkit;
66
import org.bukkit.entity.Player;
77

8-
public class MessageSender {
8+
public class messageSender {
99
/**
1010
* Replace "&" with "§" to fix color messages.
1111
*
@@ -23,7 +23,7 @@ public static String Color(String string) {
2323
* @param message
2424
*/
2525
public static void sendMessage(Player player, String message) {
26-
player.sendMessage(MessageSender.Color(message));
26+
player.sendMessage(messageSender.Color(message));
2727
}
2828

2929
/**
@@ -33,7 +33,7 @@ public static void sendMessage(Player player, String message) {
3333
*/
3434
public static void broadcastMessage(String message) {
3535
for (Player player : Bukkit.getOnlinePlayers()) {
36-
player.sendMessage(MessageSender.Color(message));
36+
player.sendMessage(messageSender.Color(message));
3737
}
3838
}
3939

@@ -43,7 +43,7 @@ public static void broadcastMessage(String message) {
4343
* @param message
4444
*/
4545
public static void broadcastMessageAll(String message) {
46-
Bukkit.broadcastMessage(MessageSender.Color(message));
46+
Bukkit.broadcastMessage(messageSender.Color(message));
4747
}
4848

4949
/**
@@ -54,7 +54,7 @@ public static void broadcastMessageAll(String message) {
5454
* @param subtitle
5555
*/
5656
public static void sendTitle(Player player, String title, String subtitle) {
57-
player.sendTitle(MessageSender.Color(title), MessageSender.Color(subtitle), 10, 40, 20); // Show title 2s
57+
player.sendTitle(messageSender.Color(title), messageSender.Color(subtitle), 10, 40, 20); // Show title 2s
5858
}
5959

6060
/**
@@ -65,7 +65,7 @@ public static void sendTitle(Player player, String title, String subtitle) {
6565
*/
6666
public static void broadcastTitle(String title, String subtitle) {
6767
for (Player player : Bukkit.getOnlinePlayers()) {
68-
player.sendTitle(MessageSender.Color(title), MessageSender.Color(subtitle), 10, 40, 20); // Show title 2s
68+
player.sendTitle(messageSender.Color(title), messageSender.Color(subtitle), 10, 40, 20); // Show title 2s
6969
}
7070
}
7171
}
Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,94 @@
11
package com.github.katorly.starlin_l2;
22

3+
import java.text.SimpleDateFormat;
4+
5+
import com.github.katorly.starlin_l2.backup.configReader;
6+
37
import org.bukkit.Bukkit;
8+
import org.bukkit.configuration.file.FileConfiguration;
9+
import org.bukkit.entity.Player;
410
import org.bukkit.event.HandlerList;
511
import org.bukkit.plugin.java.JavaPlugin;
12+
import org.bukkit.scheduler.BukkitRunnable;
613

714
public class starlin_l2 extends JavaPlugin {
815
public static starlin_l2 INSTANCE;
9-
1016
public starlin_l2() {
1117
INSTANCE = this;
1218
}
19+
20+
public static configReader timedata;
21+
public static configReader monthly;
1322

1423
@Override
1524
public void onEnable() {
1625
getServer().getPluginManager().registerEvents(new EventListener(),this);
17-
Bukkit.getLogger().info("[Starlin_L2] Author: Katorly");
18-
Bukkit.getLogger().info("[Starlin_L2] Starlin_L2 enabled! Made for StarlinWorld server only.");
26+
timedata = new configReader(this,"","timedata.yml");
27+
timedata.saveDefaultConfig();
28+
monthly = new configReader(this,"","monthly.yml");
29+
monthly.saveDefaultConfig();
30+
Bukkit.getLogger().info("[starlin_l2] Repo: https://github.com/katorlys/Starlin_L2");
31+
Bukkit.getLogger().info("[starlin_l2] Starlin_L2 enabled! Made for StarlinWorld server only.");
32+
this.timeCounter();
1933
}
2034

2135
@Override
2236
public void onDisable() {
2337
HandlerList.unregisterAll(this);
24-
Bukkit.getLogger().info("[Starlin_L2] Starlin_L2 disabled!");
38+
configReader.save(timedata);
39+
Bukkit.getLogger().info("[starlin_l2] Starlin_L2 disabled!");
40+
}
41+
42+
public void timeCounter() { //Counts the player's play time.
43+
new BukkitRunnable() {
44+
@Override
45+
public void run() {
46+
for (Player player : Bukkit.getOnlinePlayers()) {
47+
FileConfiguration timedata = starlin_l2.timedata.getConfig();
48+
String u = player.getUniqueId().toString();
49+
if (!starlin_l2.timedata.getConfig().contains(u)) { //if data not exist
50+
timedata.set(u + ".name", player.getName());
51+
long t = System.currentTimeMillis();
52+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
53+
String timenow = dateFormat.format(t);
54+
timedata.set(u + ".first-time", timenow);
55+
timedata.set(u + ".total", 0.0);
56+
configReader.save(starlin_l2.timedata);
57+
} else {
58+
Double newtotal = Double.valueOf(String.format("%.2f", timedata.getDouble(u + ".total"))) + 0.1; //if data exist
59+
timedata.set(u + ".total", newtotal);
60+
configReader.save(starlin_l2.timedata);
61+
//
62+
// Failed to achieve the following function: Count the player's play time every month.
63+
//
64+
//
65+
//long t = System.currentTimeMillis();
66+
//SimpleDateFormat d = new SimpleDateFormat("yyyy");
67+
//String y = d.format(t);
68+
//if (!starlin_l2.timedata.getConfig().contains(u + "." + y)) { //if data not exist
69+
// 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";
70+
// String[] mtime = ytime.split(",");
71+
// SimpleDateFormat n = new SimpleDateFormat("M");
72+
// String m = n.format(t);
73+
// int month = Integer.valueOf(m);
74+
// mtime[month - 1] = String.valueOf(Double.valueOf(mtime[month - 1]) + 0.1);
75+
// String newtime = String.join(",", mtime);
76+
// timedata.set(u + "." + y, newtime);
77+
// configReader.save(starlin_l2.timedata);
78+
//} else {
79+
// String ytime = timedata.getString(u + "." + y); //if data exist
80+
// String[] mtime = ytime.split(",");
81+
// SimpleDateFormat n = new SimpleDateFormat("M");
82+
// String m = n.format(t);
83+
// int month = Integer.valueOf(m);
84+
// mtime[month - 1] = String.valueOf(Double.valueOf(mtime[month - 1]) + 0.1);
85+
// String newtime = String.join(",", mtime);
86+
// timedata.set(u + "." + y, newtime);
87+
// configReader.save(starlin_l2.timedata);
88+
//}
89+
}
90+
}
91+
}
92+
}.runTaskTimer(this, 7200L, 7200L);
2593
}
26-
}
94+
}

src/main/resources/monthly.yml

Whitespace-only changes.

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: starlin_l2
2-
version: "1.0.0"
2+
version: "1.0.1"
33
author: Katorly
44
main: com.github.katorly.starlin_l2.starlin_l2
55
api-version: 1.18

src/main/resources/timedata.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)