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

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
katorly committed Jan 17, 2022
1 parent 4b430ad commit 58b0cb7
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 24 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

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.0</version>
<version>1.0.1</version>

<!-- Repositories -->
<repositories>
Expand Down
62 changes: 54 additions & 8 deletions src/main/java/com/github/katorly/starlin_l2/EventListener.java
Original file line number Diff line number Diff line change
@@ -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<String> plist = new ArrayList<String>();
plist.add(pname);
monthly.set(timenow, plist);
configReader.save(starlin_l2.monthly);
} else {
List<String> 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);
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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));
}

/**
Expand All @@ -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));
}
}

Expand All @@ -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));
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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
}
}
}
78 changes: 73 additions & 5 deletions src/main/java/com/github/katorly/starlin_l2/starlin_l2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Empty file added src/main/resources/monthly.yml
Empty file.
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Empty file added src/main/resources/timedata.yml
Empty file.

0 comments on commit 58b0cb7

Please sign in to comment.