This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
generated from katorlys-samples/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
225 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/github/katorly/starlin_l2/backup/configReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 73 additions & 5 deletions
78
src/main/java/com/github/katorly/starlin_l2/starlin_l2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.