Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
katorly committed Jan 20, 2022
1 parent 58b0cb7 commit 8c30722
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ vscode:
- vscjava.vscode-java-pack
- vscjava.vscode-java-debug
- redhat.fabric8-analytics
- redhat.vscode-xml
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Starlin_L2
[![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)
[![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>
![Build](https://img.shields.io/github/workflow/status/katorlys/Starlin_L2/Build?style=for-the-badge)

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

Main functions:<br>
- Prevent players from stucking in the Nether Portal when logging in.
- Prevent crops from being trampled.
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_13.37.53.png)
- Prevent crops from being trampled.
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_12.58.39.png)
- Record player's first-join time and monthly online time.
- Record the server's monthly players.
![](https://cdn.jsdelivr.net/gh/katorly/Gallery001/plugins/2022-01-20_14.52.png)

## Commands
- `/l2 time [player]` Displays the first-join time and monthly online time of the excutor or the specific player.

## Config
### timedata.yml
```yml
12345678-aaaa-bbbb-cccc-123412341234: #Player's UUID
name: Katorly #Player's ID
first-time: 2022.01.01 00:00 #Player's first-join time
total: 10.5 #Player's total online time
month-time:
'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
```
This is generated by the plugin, do not edit if you don't know what you're doing.
### monthly.yml
```yml
'2022': #Year
'01': #Month
- Katorly #Playerlist
- player2
```
This is generated by the plugin, do not edit if you don't know what you're doing.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<artifactId>starlin_l2</artifactId>
<groupId>com.github.katorly</groupId>
<version>1.0.1</version>
<version>1.0.2</version>

<!-- Repositories -->
<repositories>
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/github/katorly/starlin_l2/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.katorly.starlin_l2;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.github.katorly.starlin_l2.backup.messageSender;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

public class CommandHandler implements TabExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
FileConfiguration timedata = starlin_l2.timedata.getConfig();
if (command.getName().equalsIgnoreCase("l2")) {
if (args.length < 1) {
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 <参数>. 可用参数: time"));
} else if (Objects.equals(args[0], "time")) { //Get player's play time information.
if (args.length == 1) {
if (!(sender instanceof Player)) {
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 time <玩家ID>."));
} else {
Player player = (Player) sender;
String u = player.getUniqueId().toString();
String pname = player.getName().toString();
String first_time;
if (starlin_l2.timedata.getConfig().contains(u + ".first-time")) first_time = timedata.getString(u + ".first-time");
else first_time = "未知";
String total_time;
if (starlin_l2.timedata.getConfig().contains(u + ".total")) total_time = String.format("%.1f", timedata.getDouble(u + ".total"));
else total_time = "未知";
messageSender.sendMessage(player, "&b&l星林宇宙 &r&8>> &7您 (&f" + pname + "&7) 的游玩信息:");
messageSender.sendMessage(player, " &7首次加入: &f" + first_time);
messageSender.sendMessage(player, " &7总计时长: &f" + total_time + " 小时");
}
} else if (args.length == 2) {
Player p = null;
for (Player player : Bukkit.getOnlinePlayers()) {
if (Objects.equals(args[1], player.getName())) {
p = player;
}
}
if (p != null) {
String u = p.getUniqueId().toString();
String pname = p.getName().toString();
String first_time;
if (starlin_l2.timedata.getConfig().contains(u + ".first-time")) first_time = timedata.getString(u + ".first-time");
else first_time = "未知";
String total_time;
if (starlin_l2.timedata.getConfig().contains(u + ".total")) total_time = String.format("%.1f", timedata.getDouble(u + ".total"));
else total_time = "未知";
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7玩家 &f" + pname + "&7 的游玩信息:"));
sender.sendMessage(messageSender.Color(" &7首次加入: &f" + first_time));
sender.sendMessage(messageSender.Color(" &7总计时长: &f" + total_time + " 小时"));
} else {
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7玩家不在线或不存在!"));
}
} else {
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 time [玩家ID]."));
}
} else {
sender.sendMessage(messageSender.Color("&b&l星林宇宙 &r&8>> &7用法: /l2 <参数>. 可用参数: time"));
}
}
return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command commandd, String label, String[] args) {
if (!(sender instanceof Player)) {
return null;
}
if (args.length == 1) {
List<String> sub = new ArrayList<>();
sub.add("time");
return sub;
}
if (args.length == 2) {
if (args[0] == "time") {
List<String> sub = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
sub.add(player.getName());
}
return sub;
}
}
return null;
}
}
22 changes: 19 additions & 3 deletions src/main/java/com/github/katorly/starlin_l2/EventListener.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.katorly.starlin_l2;

import java.text.ParseException;
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 com.github.katorly.starlin_l2.utils.MonthlyPlayTime;

import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -17,27 +19,36 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

public class EventListener implements Listener {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
public void onPlayerJoin(PlayerJoinEvent e) throws ParseException {

FileConfiguration timedata = starlin_l2.timedata.getConfig(); //Check whether player has joined before.
long t = System.currentTimeMillis();
SimpleDateFormat d = new SimpleDateFormat("yyyy");
String year = d.format(t);
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);
}
if (!starlin_l2.timedata.getConfig().contains(u + ".month-time." + year)) {
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");
configReader.save(starlin_l2.timedata);
}

MonthlyPlayTime.initialize(e.getPlayer()); //Get player's join time.

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)) {
Expand Down Expand Up @@ -81,6 +92,11 @@ public void run() {
}
}

@EventHandler //Count player's monthly play time.
public void onPlayerLeave(PlayerQuitEvent e) throws ParseException {
MonthlyPlayTime.settle(e.getPlayer());
}

@EventHandler //Prevent crops from being trampled.
public void onCropTrample(PlayerInteractEvent e) {
if (e.getAction() == Action.PHYSICAL && e.getClickedBlock().getType() == Material.FARMLAND) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class messageSender {
* @return
*/
public static String Color(String string) {
return Objects.requireNonNull(string).replace("&", "§");
return Objects.requireNonNull(string).replace("&", "§").replace("§§", "&");
}

/**
Expand Down
57 changes: 26 additions & 31 deletions src/main/java/com/github/katorly/starlin_l2/starlin_l2.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.github.katorly.starlin_l2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.github.katorly.starlin_l2.backup.configReader;
import com.github.katorly.starlin_l2.utils.MonthlyPlayTime;

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

public static configReader timedata;
public static configReader monthly;
public Map<UUID, Long> StartTime = new HashMap<>();

@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new EventListener(),this);
timedata = new configReader(this,"","timedata.yml");
timedata.saveDefaultConfig();
monthly = new configReader(this,"","monthly.yml");
monthly.saveDefaultConfig();
Bukkit.getPluginCommand("l2").setExecutor(new CommandHandler());
Bukkit.getPluginCommand("l2").setTabCompleter(new CommandHandler());
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.");
for (Player p : Bukkit.getOnlinePlayers()) {
try {
MonthlyPlayTime.initialize(p);
} catch (ParseException e) {
Bukkit.getLogger().severe("[starlin_l2] Error counting player's monthly play time.");
e.printStackTrace();
}
}
this.timeCounter();
}

@Override
public void onDisable() {
for (Player p : Bukkit.getOnlinePlayers()) {
try {
MonthlyPlayTime.settle(p);
} catch (ParseException e) {
Bukkit.getLogger().severe("[starlin_l2] Error counting player's monthly play time.");
e.printStackTrace();
}
}
HandlerList.unregisterAll(this);
configReader.save(timedata);
Bukkit.getLogger().info("[starlin_l2] Starlin_L2 disabled!");
Expand All @@ -55,37 +78,9 @@ public void run() {
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);
Double newtotal = Double.valueOf(String.format("%.1f", timedata.getDouble(u + ".total"))) + 0.1; //if data exist
timedata.set(u + ".total", Double.valueOf(String.format("%.1f", 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);
//}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.katorly.starlin_l2.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.github.katorly.starlin_l2.starlin_l2;
import com.github.katorly.starlin_l2.backup.configReader;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

public class MonthlyPlayTime {
public static void initialize(Player p) throws ParseException { //Get player's join time.
long t = System.currentTimeMillis();
SimpleDateFormat dnow = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date d1 = dnow.parse(dnow.format(t));
Long t1 = d1.getTime();
starlin_l2.INSTANCE.StartTime.put(p.getPlayer().getUniqueId(), t1);
}

public static void settle(Player p) throws ParseException { //Count player's monthly play time.
FileConfiguration timedata = starlin_l2.timedata.getConfig();
long t = System.currentTimeMillis();
SimpleDateFormat d = new SimpleDateFormat("yyyy");
String year = d.format(t);
String u = p.getPlayer().getUniqueId().toString();
Long minutes;
if (!starlin_l2.timedata.getConfig().contains(u + ".month-time." + year)) {
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");
configReader.save(starlin_l2.timedata);
} else {
if (starlin_l2.INSTANCE.StartTime.containsKey(p.getPlayer().getUniqueId())) {
Long t1 =starlin_l2.INSTANCE.StartTime.get(p.getPlayer().getUniqueId());
SimpleDateFormat dnow = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date d2 = dnow.parse(dnow.format(t));
Long t2 = d2.getTime();
minutes = (t2 - t1) / (1000 * 60);
starlin_l2.INSTANCE.StartTime.remove(p.getPlayer().getUniqueId());
String ytime = timedata.getString(u + ".month-time." + year);
String[] mtime = ytime.split(",");
SimpleDateFormat n = new SimpleDateFormat("M");
String m = n.format(t);
int month = Integer.valueOf(m);
mtime[month - 1] = String.format("%.1f", Double.valueOf(mtime[month - 1]) + minutes / 60.0);
String newtime = String.join(",", mtime);
timedata.set(u + ".month-time." + year, newtime);
configReader.save(starlin_l2.timedata);
}
}
}
}
6 changes: 5 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: starlin_l2
version: "1.0.1"
version: "1.0.2"
author: Katorly
main: com.github.katorly.starlin_l2.starlin_l2
api-version: 1.18
description: A Spigot plugin made for StarlinWorld.
commands:
l2:
description: The main command of Starlin_L2.
usage: /l2

0 comments on commit 8c30722

Please sign in to comment.