Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

Commit

Permalink
Updater performance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicofisi committed Mar 25, 2016
1 parent 81d4c56 commit 07bec8f
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 203 deletions.
Binary file modified bin/pl/pickaxe/largesk/LargeSk.class
Binary file not shown.
2 changes: 1 addition & 1 deletion lastest.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.0.2
3 changes: 2 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
main: pl.pickaxe.largesk.LargeSk
name: LargeSk
version: 1.0.1
version: 1.0.2
description: An addon for Skript with some cool features.
author: Nicofisi
website: http://github.com/Nicofisi/LargeSk
depend: [Skript]
softdepend: [AAC]
loadbefore: [Umbaska]

commands:
largesk:
Expand Down
108 changes: 12 additions & 96 deletions src/pl/pickaxe/largesk/LargeSk.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package pl.pickaxe.largesk;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Objects;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import ch.njol.skript.Skript;
import pl.pickaxe.largesk.util.Xlog;
import pl.pickaxe.largesk.util.Copier;
import pl.pickaxe.largesk.util.LargeConfig;
import pl.pickaxe.largesk.util.Metrics;
import pl.pickaxe.largesk.util.SkAddons;
import pl.pickaxe.largesk.util.Updater;

public class LargeSk extends JavaPlugin implements Listener {

public static boolean debug = false;
public static int lastestConfigVersion = 5;
public static boolean updateSession = false;

public static File configf;
public static FileConfiguration config;
public static boolean debug = true;

public static LargeSk getPlugin() {
return LargeSk.getPlugin(LargeSk.class);
Expand All @@ -40,46 +29,9 @@ public void onEnable() {
long eTime = System.currentTimeMillis();
Xlog.logInfo(ChatColor.YELLOW + "=== ENABLE " + ChatColor.GREEN + "START" + ChatColor.YELLOW + " ===");

if (Bukkit.getPluginManager().isPluginEnabled("LargeSkUpdater"))
{
Xlog.logInfo("The LargeSkUpdater is enabled! So you were updating the plugin? Nice!");
Plugin lsu = Bukkit.getPluginManager().getPlugin("LargeSkUpdater");
Bukkit.getPluginManager().disablePlugin(lsu);
}

//Configs
configf = new File(getDataFolder(), "config.yml");

//Defines the lastest config version

if ( ! configf.exists() || lastestConfigVersion != getConfig().getInt("configVersion"))
{
configf.getParentFile().mkdirs();
if (configf.exists()) {
Xlog.logWarning("Your config.yml file is outdated.");
Xlog.logInfo("I'll copy the default one from the plugin's .jar file in a moment.");
String path = this.getDataFolder() + "/config-old-ver" + getConfig().getInt("configVersion") + "-" + System.currentTimeMillis();
File oldConfig = new File(path);
configf.renameTo(oldConfig);
Xlog.logInfo("Your old configuration was moved to " + path);
}
else
{
Xlog.logWarning("The config.yml file does not exist.");
Xlog.logInfo("I'll copy the default config from the plugin's .jar file now.");
}
Copier cp = new Copier();
cp.copy(getResource("config.yml"), configf);
reloadConfig();
Xlog.logInfo("Done.");
Xlog.logInfo("You are now using DEFAULT configuration of the plugin.");
if ( ! configf.exists() || lastestConfigVersion != getConfig().getInt("configVersion"))
{
getLogger().info(getConfig().getInt("configVersion") + lastestConfigVersion + "");
Xlog.logError("Whooops! The default config file is broken. It's not compatybile with the current version of the plugin.");
Xlog.logError("All you may do is to contact the developer or use older version of the plugin.");
}
}
//Config
LargeConfig conf = new LargeConfig();
conf.load();

//Registring Skript addon
Skript.registerAddon(this);
Expand All @@ -100,29 +52,22 @@ public void onEnable() {

//Announcing how much time enabling took
eTime = System.currentTimeMillis() - eTime;

Xlog.logInfo(ChatColor.YELLOW + "=== ENABLE " + ChatColor.GREEN + "COMPLETE" + ChatColor.YELLOW + " (Took " + ChatColor.LIGHT_PURPLE + eTime + "ms" + ChatColor.YELLOW + ") ===");

//Update check schedule
if (getConfig().getConfigurationSection("updates").getBoolean("check"))
{
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::checkUpdates, 1L, getConfig().getConfigurationSection("updates").getInt("frequency")*1200L);
}
else
{
Xlog.logInfo("Checking for updates is disabled in config, so I'll check it only once,");
Xlog.logInfo("And I won't disturb you anymore, okay?");
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkUpdates);
}
Updater updater = new Updater();
updater.scheduleUpdates();
}

//On disable
@Override
public void onDisable() {
Xlog.logInfo("Bye, Senpai!");
if (debug) Xlog.logInfo("Cancelling tasks..");
Bukkit.getScheduler().cancelTasks(this);

if (getConfig().getBoolean("enableMetrics"))
{
if (debug) Xlog.logInfo("Disabling Metrics..");
try {
Metrics metrics = new Metrics(this);
metrics.disable();
Expand All @@ -133,35 +78,6 @@ public void onDisable() {
e.printStackTrace();
}
}
}

//Checking updates, run on server startup and later by the Bukkit scheduler
public void checkUpdates()
{
if (updateSession) return;

Xlog.logUpdater("Checking for updates..");
String newVersion = "";
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new URL("https://raw.githubusercontent.com/Nicofisi/LargeSk/master/lastest.version").openStream()));
newVersion = in.readLine();
in.close();
}
catch (Exception e)
{
Xlog.logError(e.getCause().getMessage());
}
String currentVersion = this.getDescription().getVersion();
if ( ! Objects.equals(currentVersion, newVersion))
{
Xlog.logUpdater("LargeSk " + newVersion + " was released! You are using " + currentVersion + ".");
Xlog.logUpdater("Download the update from https://github.com/Nicofisi/LargeSk/releases");

}
else
{
Xlog.logUpdater("It seems like your using the latest version of the plugin.");
}
Xlog.logInfo("Bye, Senpai!");
}
}
13 changes: 11 additions & 2 deletions src/pl/pickaxe/largesk/LargeSkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,17 @@ else if (args[0].equalsIgnoreCase("check"))
else if (args[0].equalsIgnoreCase("update"))
{
sender.sendMessage(ChatColor.GRAY + "[LargeSk]> " + ChatColor.YELLOW + "The update progress will be shown in console");
Updater upd = new Updater();
upd.runUpdate();
Updater u = new Updater();
u.runUpdate();
}
else if (args[0].equalsIgnoreCase("xlogtest"))
{
Xlog.logInfo("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
Xlog.logWarning("Ut porttitor feugiat mi a vehicula. Curabitur sed urna quam.");
Xlog.logError("Morbi et rhoncus dolor, vitae commodo enim.");
Xlog.logUpdater("Mauris ac odio mattis, fringilla nisi molestie, blandit dolor.");
Xlog.logDefault("Phasellus non sem eget dolor ultrices commodo et dignissim dolor.");
Xlog.logRaw("Nunc cursus ex vitae diam mollis, ut faucibus neque fringilla.");
}
else if (args[0].equalsIgnoreCase("debug"))
{
Expand Down
152 changes: 152 additions & 0 deletions src/pl/pickaxe/largesk/util/ConsoleLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package pl.pickaxe.largesk.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.message.Message;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;

public class ConsoleLogger extends JavaPlugin {
public void onEnable() {

reloadConfig();
reloadFilter();

Bukkit.getScheduler().runTaskLater(this, new Runnable() {
@Override
public void run() {
reloadConfig();
reloadFilter();
log("&3Filters now turned on.");
}
}, 100);

try {
Metrics metrics = new Metrics(this);
metrics.start();
} catch (IOException e) {
log("&4Failed to start Metrics: &e" + e.getMessage());
}

log(getName() + " v" + getDescription().getVersion() + " by BillyGalbreath enabled!");
}

public void log (Object obj) {
if (getConfig().getBoolean("color-logs", true))
getServer().getConsoleSender().sendMessage(colorize("&3[&d" + getName() + "&3]&r " + obj));
else
Bukkit.getLogger().log(java.util.logging.Level.INFO, "[" + getName() + "] " + ((String) obj).replaceAll("(?)\u00a7([a-f0-9k-or])", ""));
}

public String colorize(String str) {
return str.replaceAll("(?i)&([a-f0-9k-or])", "\u00a7$1");
}

public void reloadFilter() {
org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
Filter filter = new Filter() {
@Override
public Result filter(LogEvent event) {
ConfigurationSection filters = getConfig().getConfigurationSection("filters");
for (String level : filters.getKeys(false)) {
if (!event.getLevel().equals(Level.valueOf(level)))
continue;
ConfigurationSection filterLevels = filters.getConfigurationSection(level);
for (String filterType: filterLevels.getKeys(false)) {
List<String> msgs = filterLevels.getStringList(filterType);
for (String msg : msgs) {
if (filterType.equalsIgnoreCase("equals"))
if (event.getMessage().toString().equals(msg))
return Result.DENY;
if (filterType.equalsIgnoreCase("equalsignorecase"))
if (event.getMessage().toString().equalsIgnoreCase(msg))
return Result.DENY;
if (filterType.equalsIgnoreCase("contains"))
if (event.getMessage().toString().contains(msg))
return Result.DENY;
if (filterType.equalsIgnoreCase("endswith"))
if (event.getMessage().toString().endsWith(msg))
return Result.DENY;
if (filterType.equalsIgnoreCase("startswith"))
if (event.getMessage().toString().startsWith(msg))
return Result.DENY;
if (filterType.equalsIgnoreCase("matches"))
if (event.getMessage().toString().matches(msg))
return Result.DENY;
}
}
}
return null;
}
@Override
public Result filter(Logger arg0, Level arg1, Marker arg2, String arg3, Object... arg4) {
return null;
}
@Override
public Result filter(Logger arg0, Level arg1, Marker arg2, Object arg3, Throwable arg4) {
return null;
}
@Override
public Result filter(Logger arg0, Level arg1, Marker arg2, Message arg3, Throwable arg4) {
return null;
}
@Override
public Result getOnMatch() {
return null;
}
@Override
public Result getOnMismatch() {
return null;
}
@Override
public State getState()
{
return null;
}
@Override
public void initialize()
{

}
@Override
public boolean isStarted()
{
return false;
}
@Override
public boolean isStopped()
{
return false;
}
@Override
public void start()
{

}
@Override
public void stop()
{

}
};
boolean alreadyLoaded = false;
Iterator<Filter> iter = coreLogger.getFilters();
while (iter.hasNext()) {
if (filter.equals(iter.next())) {
alreadyLoaded = true;
break;
}
}
if (!alreadyLoaded)
coreLogger.addFilter(filter);
}
}
Loading

0 comments on commit 07bec8f

Please sign in to comment.