Skip to content

Commit 8c30722

Browse files
committed
v1.0.2
1 parent 58b0cb7 commit 8c30722

File tree

9 files changed

+230
-39
lines changed

9 files changed

+230
-39
lines changed

.gitpod.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ vscode:
88
- vscjava.vscode-java-pack
99
- vscjava.vscode-java-debug
1010
- redhat.fabric8-analytics
11+
- redhat.vscode-xml

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
# Starlin_L2
2-
[![License](https://img.shields.io/badge/license-CC%20BY--NC--ND--4.0-green?style=for-the-badge)](http://creativecommons.org/licenses/by-nc-nd/4.0) ![Pull Requests](https://img.shields.io/github/issues-pr-closed/katorlys/Starlin_L2?style=for-the-badge) ![Issues](https://img.shields.io/github/issues-closed/katorlys/Starlin_L2?style=for-the-badge)
2+
[![License](https://img.shields.io/badge/license-CC%20BY--NC--ND--4.0-green?style=for-the-badge)](http://creativecommons.org/licenses/by-nc-nd/4.0) ![Pull Requests](https://img.shields.io/github/issues-pr-closed/katorlys/Starlin_L2?style=for-the-badge) ![Issues](https://img.shields.io/github/issues-closed/katorlys/Starlin_L2?style=for-the-badge)<br>
3+
![Build](https://img.shields.io/github/workflow/status/katorlys/Starlin_L2/Build?style=for-the-badge)
34

45
## Introduction
56
Starlin_L2 Minecraft Spigot plugin made for StarlinWorld (星林宇宙) server.<br>
67

78
Main functions:<br>
89
- Prevent players from stucking in the Nether Portal when logging in.
9-
- Prevent crops from being trampled.
10+
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_13.37.53.png)
11+
- Prevent crops from being trampled.
12+
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_12.58.39.png)
13+
- Record player's first-join time and monthly online time.
14+
- Record the server's monthly players.
15+
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_14.52.png)
16+
17+
## Commands
18+
- `/l2 time [player]` Displays the first-join time and monthly online time of the excutor or the specific player.
19+
20+
## Config
21+
### timedata.yml
22+
```yml
23+
12345678-aaaa-bbbb-cccc-123412341234: #Player's UUID
24+
name: Katorly #Player's ID
25+
first-time: 2022.01.01 00:00 #Player's first-join time
26+
total: 10.5 #Player's total online time
27+
month-time:
28+
'2022': 19.2,11.5,0.7,8.0,3.5,0.0,7.2,9.9,48.3,0.0,1.1,0.6
29+
```
30+
This is generated by the plugin, do not edit if you don't know what you're doing.
31+
### monthly.yml
32+
```yml
33+
'2022': #Year
34+
'01': #Month
35+
- Katorly #Playerlist
36+
- player2
37+
```
38+
This is generated by the plugin, do not edit if you don't know what you're doing.

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.1</version>
7+
<version>1.0.2</version>
88

99
<!-- Repositories -->
1010
<repositories>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.github.katorly.starlin_l2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
7+
import com.github.katorly.starlin_l2.backup.messageSender;
8+
9+
import org.bukkit.Bukkit;
10+
import org.bukkit.command.Command;
11+
import org.bukkit.command.CommandSender;
12+
import org.bukkit.command.TabExecutor;
13+
import org.bukkit.configuration.file.FileConfiguration;
14+
import org.bukkit.entity.Player;
15+
16+
public class CommandHandler implements TabExecutor {
17+
@Override
18+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
19+
FileConfiguration timedata = starlin_l2.timedata.getConfig();
20+
if (command.getName().equalsIgnoreCase("l2")) {
21+
if (args.length < 1) {
22+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 <参数>. 可用参数: time"));
23+
} else if (Objects.equals(args[0], "time")) { //Get player's play time information.
24+
if (args.length == 1) {
25+
if (!(sender instanceof Player)) {
26+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 time <玩家ID>."));
27+
} else {
28+
Player player = (Player) sender;
29+
String u = player.getUniqueId().toString();
30+
String pname = player.getName().toString();
31+
String first_time;
32+
if (starlin_l2.timedata.getConfig().contains(u + ".first-time")) first_time = timedata.getString(u + ".first-time");
33+
else first_time = "未知";
34+
String total_time;
35+
if (starlin_l2.timedata.getConfig().contains(u + ".total")) total_time = String.format("%.1f", timedata.getDouble(u + ".total"));
36+
else total_time = "未知";
37+
messageSender.sendMessage(player, "&b&l星林宇宙 &r&8>> &7您 (&f" + pname + "&7) 的游玩信息:");
38+
messageSender.sendMessage(player, " &7首次加入: &f" + first_time);
39+
messageSender.sendMessage(player, " &7总计时长: &f" + total_time + " 小时");
40+
}
41+
} else if (args.length == 2) {
42+
Player p = null;
43+
for (Player player : Bukkit.getOnlinePlayers()) {
44+
if (Objects.equals(args[1], player.getName())) {
45+
p = player;
46+
}
47+
}
48+
if (p != null) {
49+
String u = p.getUniqueId().toString();
50+
String pname = p.getName().toString();
51+
String first_time;
52+
if (starlin_l2.timedata.getConfig().contains(u + ".first-time")) first_time = timedata.getString(u + ".first-time");
53+
else first_time = "未知";
54+
String total_time;
55+
if (starlin_l2.timedata.getConfig().contains(u + ".total")) total_time = String.format("%.1f", timedata.getDouble(u + ".total"));
56+
else total_time = "未知";
57+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7玩家 &f" + pname + "&7 的游玩信息:"));
58+
sender.sendMessage(messageSender.Color(" &7首次加入: &f" + first_time));
59+
sender.sendMessage(messageSender.Color(" &7总计时长: &f" + total_time + " 小时"));
60+
} else {
61+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7玩家不在线或不存在!"));
62+
}
63+
} else {
64+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 time [玩家ID]."));
65+
}
66+
} else {
67+
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 <参数>. 可用参数: time"));
68+
}
69+
}
70+
return true;
71+
}
72+
73+
@Override
74+
public List<String> onTabComplete(CommandSender sender, Command commandd, String label, String[] args) {
75+
if (!(sender instanceof Player)) {
76+
return null;
77+
}
78+
if (args.length == 1) {
79+
List<String> sub = new ArrayList<>();
80+
sub.add("time");
81+
return sub;
82+
}
83+
if (args.length == 2) {
84+
if (args[0] == "time") {
85+
List<String> sub = new ArrayList<>();
86+
for (Player player : Bukkit.getOnlinePlayers()) {
87+
sub.add(player.getName());
88+
}
89+
return sub;
90+
}
91+
}
92+
return null;
93+
}
94+
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.github.katorly.starlin_l2;
22

3+
import java.text.ParseException;
34
import java.text.SimpleDateFormat;
45
import java.util.ArrayList;
56
import java.util.List;
67

78
import com.github.katorly.starlin_l2.backup.configReader;
89
import com.github.katorly.starlin_l2.backup.messageSender;
10+
import com.github.katorly.starlin_l2.utils.MonthlyPlayTime;
911

1012
import org.bukkit.Location;
1113
import org.bukkit.Material;
@@ -17,27 +19,36 @@
1719
import org.bukkit.event.block.Action;
1820
import org.bukkit.event.player.PlayerInteractEvent;
1921
import org.bukkit.event.player.PlayerJoinEvent;
22+
import org.bukkit.event.player.PlayerQuitEvent;
2023
import org.bukkit.scheduler.BukkitRunnable;
2124

2225
public class EventListener implements Listener {
26+
2327
@EventHandler
24-
public void onPlayerJoin(PlayerJoinEvent e) {
28+
public void onPlayerJoin(PlayerJoinEvent e) throws ParseException {
2529

2630
FileConfiguration timedata = starlin_l2.timedata.getConfig(); //Check whether player has joined before.
31+
long t = System.currentTimeMillis();
32+
SimpleDateFormat d = new SimpleDateFormat("yyyy");
33+
String year = d.format(t);
2734
String u = e.getPlayer().getUniqueId().toString();
2835
if (!starlin_l2.timedata.getConfig().contains(u)) { //if not
2936
timedata.set(u + ".name", e.getPlayer().getName());
30-
long t = System.currentTimeMillis();
3137
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
3238
String timenow = dateFormat.format(t);
3339
timedata.set(u + ".first-time", timenow);
3440
timedata.set(u + ".total", 0.0);
3541
configReader.save(starlin_l2.timedata);
3642
}
43+
if (!starlin_l2.timedata.getConfig().contains(u + ".month-time." + year)) {
44+
timedata.set(u + ".month-time." + year, "0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0");
45+
configReader.save(starlin_l2.timedata);
46+
}
47+
48+
MonthlyPlayTime.initialize(e.getPlayer()); //Get player's join time.
3749

3850
FileConfiguration monthly = starlin_l2.monthly.getConfig(); //Record monthly players.
3951
String pname = e.getPlayer().getName();
40-
long t = System.currentTimeMillis();
4152
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM");
4253
String timenow = dateFormat.format(t);
4354
if (!starlin_l2.monthly.getConfig().contains(timenow)) {
@@ -81,6 +92,11 @@ public void run() {
8192
}
8293
}
8394

95+
@EventHandler //Count player's monthly play time.
96+
public void onPlayerLeave(PlayerQuitEvent e) throws ParseException {
97+
MonthlyPlayTime.settle(e.getPlayer());
98+
}
99+
84100
@EventHandler //Prevent crops from being trampled.
85101
public void onCropTrample(PlayerInteractEvent e) {
86102
if (e.getAction() == Action.PHYSICAL && e.getClickedBlock().getType() == Material.FARMLAND) {

src/main/java/com/github/katorly/starlin_l2/backup/messageSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class messageSender {
1313
* @return
1414
*/
1515
public static String Color(String string) {
16-
return Objects.requireNonNull(string).replace("&", "§");
16+
return Objects.requireNonNull(string).replace("&", "§").replace("§§", "&");
1717
}
1818

1919
/**

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

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

3+
import java.text.ParseException;
34
import java.text.SimpleDateFormat;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.UUID;
48

59
import com.github.katorly.starlin_l2.backup.configReader;
10+
import com.github.katorly.starlin_l2.utils.MonthlyPlayTime;
611

712
import org.bukkit.Bukkit;
813
import org.bukkit.configuration.file.FileConfiguration;
@@ -19,21 +24,39 @@ public starlin_l2() {
1924

2025
public static configReader timedata;
2126
public static configReader monthly;
27+
public Map<UUID, Long> StartTime = new HashMap<>();
2228

2329
@Override
2430
public void onEnable() {
2531
getServer().getPluginManager().registerEvents(new EventListener(),this);
2632
timedata = new configReader(this,"","timedata.yml");
2733
timedata.saveDefaultConfig();
2834
monthly = new configReader(this,"","monthly.yml");
29-
monthly.saveDefaultConfig();
35+
Bukkit.getPluginCommand("l2").setExecutor(new CommandHandler());
36+
Bukkit.getPluginCommand("l2").setTabCompleter(new CommandHandler());
3037
Bukkit.getLogger().info("[starlin_l2] Repo: https://github.com/katorlys/Starlin_L2");
3138
Bukkit.getLogger().info("[starlin_l2] Starlin_L2 enabled! Made for StarlinWorld server only.");
39+
for (Player p : Bukkit.getOnlinePlayers()) {
40+
try {
41+
MonthlyPlayTime.initialize(p);
42+
} catch (ParseException e) {
43+
Bukkit.getLogger().severe("[starlin_l2] Error counting player's monthly play time.");
44+
e.printStackTrace();
45+
}
46+
}
3247
this.timeCounter();
3348
}
3449

3550
@Override
3651
public void onDisable() {
52+
for (Player p : Bukkit.getOnlinePlayers()) {
53+
try {
54+
MonthlyPlayTime.settle(p);
55+
} catch (ParseException e) {
56+
Bukkit.getLogger().severe("[starlin_l2] Error counting player's monthly play time.");
57+
e.printStackTrace();
58+
}
59+
}
3760
HandlerList.unregisterAll(this);
3861
configReader.save(timedata);
3962
Bukkit.getLogger().info("[starlin_l2] Starlin_L2 disabled!");
@@ -55,37 +78,9 @@ public void run() {
5578
timedata.set(u + ".total", 0.0);
5679
configReader.save(starlin_l2.timedata);
5780
} else {
58-
Double newtotal = Double.valueOf(String.format("%.2f", timedata.getDouble(u + ".total"))) + 0.1; //if data exist
59-
timedata.set(u + ".total", newtotal);
81+
Double newtotal = Double.valueOf(String.format("%.1f", timedata.getDouble(u + ".total"))) + 0.1; //if data exist
82+
timedata.set(u + ".total", Double.valueOf(String.format("%.1f", newtotal)));
6083
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-
//}
8984
}
9085
}
9186
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.katorly.starlin_l2.utils;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
import com.github.katorly.starlin_l2.starlin_l2;
8+
import com.github.katorly.starlin_l2.backup.configReader;
9+
10+
import org.bukkit.configuration.file.FileConfiguration;
11+
import org.bukkit.entity.Player;
12+
13+
public class MonthlyPlayTime {
14+
public static void initialize(Player p) throws ParseException { //Get player's join time.
15+
long t = System.currentTimeMillis();
16+
SimpleDateFormat dnow = new SimpleDateFormat("yyyy-MM-dd HH:mm");
17+
Date d1 = dnow.parse(dnow.format(t));
18+
Long t1 = d1.getTime();
19+
starlin_l2.INSTANCE.StartTime.put(p.getPlayer().getUniqueId(), t1);
20+
}
21+
22+
public static void settle(Player p) throws ParseException { //Count player's monthly play time.
23+
FileConfiguration timedata = starlin_l2.timedata.getConfig();
24+
long t = System.currentTimeMillis();
25+
SimpleDateFormat d = new SimpleDateFormat("yyyy");
26+
String year = d.format(t);
27+
String u = p.getPlayer().getUniqueId().toString();
28+
Long minutes;
29+
if (!starlin_l2.timedata.getConfig().contains(u + ".month-time." + year)) {
30+
timedata.set(u + ".month-time." + year, "0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0");
31+
configReader.save(starlin_l2.timedata);
32+
} else {
33+
if (starlin_l2.INSTANCE.StartTime.containsKey(p.getPlayer().getUniqueId())) {
34+
Long t1 =starlin_l2.INSTANCE.StartTime.get(p.getPlayer().getUniqueId());
35+
SimpleDateFormat dnow = new SimpleDateFormat("yyyy-MM-dd HH:mm");
36+
Date d2 = dnow.parse(dnow.format(t));
37+
Long t2 = d2.getTime();
38+
minutes = (t2 - t1) / (1000 * 60);
39+
starlin_l2.INSTANCE.StartTime.remove(p.getPlayer().getUniqueId());
40+
String ytime = timedata.getString(u + ".month-time." + year);
41+
String[] mtime = ytime.split(",");
42+
SimpleDateFormat n = new SimpleDateFormat("M");
43+
String m = n.format(t);
44+
int month = Integer.valueOf(m);
45+
mtime[month - 1] = String.format("%.1f", Double.valueOf(mtime[month - 1]) + minutes / 60.0);
46+
String newtime = String.join(",", mtime);
47+
timedata.set(u + ".month-time." + year, newtime);
48+
configReader.save(starlin_l2.timedata);
49+
}
50+
}
51+
}
52+
}

src/main/resources/plugin.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: starlin_l2
2-
version: "1.0.1"
2+
version: "1.0.2"
33
author: Katorly
44
main: com.github.katorly.starlin_l2.starlin_l2
55
api-version: 1.18
66
description: A Spigot plugin made for StarlinWorld.
7+
commands:
8+
l2:
9+
description: The main command of Starlin_L2.
10+
usage: /l2

0 commit comments

Comments
 (0)