Skip to content

Commit

Permalink
1.1-R12 - Improvements for Update Messages -w-
Browse files Browse the repository at this point in the history
  • Loading branch information
CroaBeast committed Nov 17, 2021
1 parent 45ab2dd commit 46c045f
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 106 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>me.croabeast</groupId>
<artifactId>SIR_Plugin</artifactId>
<version>1.1-R8</version>
<version>1.1-R12</version>
<packaging>jar</packaging>

<name>SIR_Plugin</name> <!--Simple In-game Receptionist-->
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/me/croabeast/cupdater/KeyBuilder.java
Expand Up @@ -72,11 +72,6 @@ public void removeLastKey() {
builder.replace(minIndex, builder.length(), "");
}

public static boolean isSubKeyOf(String parentKey, String subKey, char separator) {
if (parentKey.isEmpty()) return false;
return subKey.startsWith(parentKey) && subKey.startsWith(String.valueOf(separator), parentKey.length());
}

@Override
public String toString() {
return builder.toString();
Expand All @@ -86,4 +81,9 @@ public String toString() {
protected KeyBuilder clone() throws CloneNotSupportedException {
return new KeyBuilder((KeyBuilder) super.clone());
}

public static boolean isSubKeyOf(String parentKey, String subKey, char separator) {
if (parentKey.isEmpty()) return false;
return subKey.startsWith(parentKey) && subKey.startsWith(String.valueOf(separator), parentKey.length());
}
}
5 changes: 4 additions & 1 deletion src/main/java/me/croabeast/sircore/Application.java
Expand Up @@ -19,6 +19,7 @@ public final class Application extends JavaPlugin {
private TextUtils text;
private EventUtils utils;
private Announcer announcer;
private DoUpdate doUpdate;

public String PLUGIN_VERSION;
public int GET_VERSION;
Expand All @@ -40,6 +41,7 @@ public void onEnable() {
text = new TextUtils(main);
utils = new EventUtils(main);
announcer = new Announcer(main);
doUpdate = new DoUpdate(main);

init.startMetrics(); // The bStats method for Metrics class
new Executor(main); // Register the main cmd for the plugin
Expand All @@ -66,7 +68,7 @@ public void onEnable() {
);
records.rawRecord("");

init.startUpdater();
doUpdate.initUpdater(null);
}

@Override
Expand Down Expand Up @@ -106,6 +108,7 @@ public FileConfiguration getMessages() {
public TextUtils getTextUtils() { return text; }
public EventUtils getEventUtils() { return utils; }
public Announcer getAnnouncer() { return announcer; }
public DoUpdate getDoUpdate() { return doUpdate; }

public Plugin getPlugin(String name) {
return Bukkit.getPluginManager().getPlugin(name);
Expand Down
46 changes: 1 addition & 45 deletions src/main/java/me/croabeast/sircore/Initializer.java
Expand Up @@ -3,6 +3,7 @@
import me.croabeast.sircore.listeners.*;
import me.croabeast.sircore.objects.*;
import net.milkbowl.vault.permission.*;
import org.bukkit.entity.Player;
import org.bukkit.plugin.*;

import java.util.*;
Expand Down Expand Up @@ -194,51 +195,6 @@ public void setPluginHooks() {
}
}

public void startUpdater() {
Updater.init(main, 96378).updateCheck().whenComplete(((updateResult, throwable) -> {
if (!main.getTextUtils().getOption(4, "on-start")) return;

String latest = updateResult.getNewestVersion();

records.rawRecord("");
switch (updateResult.getReason()) {
case NEW_UPDATE:
records.doRecord(
"&4NEW UPDATE!",
"&cYou don't have the latest version of S.I.R. installed.",
"&cRemember, older versions won't receive any support.",
"&7New Version: &a" + latest + "&7 - Your Version: &e" + main.PLUGIN_VERSION,
"&7Link:&b https://www.spigotmc.org/resources/96378/"
);
break;
case UP_TO_DATE:
records.doRecord(
"&eYou have the latest version of S.I.R. &7(" + latest + ")",
"&7I would appreciate if you keep updating &c<3"
);
break;
case UNRELEASED_VERSION:
records.doRecord(
"&4DEVELOPMENT BUILD:",
"&cYou have a newer version of S.I.R. installed.",
"&cErrors might occur in this build.",
"Spigot Version: &a" + updateResult.getSpigotVersion()
+ "&7 - Your Version: &e" + main.PLUGIN_VERSION
);
break;
default:
records.rawRecord(
"&4WARNING!",
"&cCould not check for a new version of S.I.R.",
"&7Please check your connection and restart the server.",
"&7Possible reason: &e" + updateResult.getReason()
);
break;
}
records.rawRecord("");
}));
}

public void registerListeners() {
records.doRecord("", "&bLoading all the listeners...");
new PlayerListener(main);
Expand Down
Expand Up @@ -30,7 +30,7 @@ private void onLogin(LoginEvent event) {
if (!text.getOption(2, "enabled")) return;
if (utils.isVanished(player, true) && text.getOption(3, "silent")) return;

utils.loggedPlayers.add(player);
utils.LOGGED_PLAYERS.add(player);
utils.runEvent(id, player, true, !text.getOption(2, "enabled"), true);
}
}
54 changes: 5 additions & 49 deletions src/main/java/me/croabeast/sircore/listeners/PlayerListener.java
@@ -1,7 +1,6 @@
package me.croabeast.sircore.listeners;

import me.croabeast.sircore.*;
import me.croabeast.sircore.objects.*;
import me.croabeast.sircore.utilities.*;
import org.bukkit.configuration.*;
import org.bukkit.entity.*;
Expand All @@ -11,65 +10,19 @@
public class PlayerListener implements Listener {

private final Application main;
private final Records records;
private final Initializer init;
private final TextUtils text;
private final EventUtils utils;

public PlayerListener(Application main) {
this.main = main;
this.records = main.getRecords();
this.init = main.getInitializer();
this.text = main.getTextUtils();
this.utils = main.getEventUtils();
main.getServer().getPluginManager().registerEvents(this, main);
init.LISTENERS++;
}

private void adminUpdateChecker(Player player) {
Updater.init(main, 96378).updateCheck().whenComplete(((updateResult, throwable) -> {
if (!text.getOption(4, "send-op") ||
!utils.hasPerm(player, "sir.admin.updater")) return;

String latest = updateResult.getNewestVersion();

switch (updateResult.getReason()) {
case NEW_UPDATE:
records.playerRecord(player,
"", " &4NEW UPDATE!",
" &cYou don't have the latest version of S.I.R. installed.",
" &cRemember, older versions won't receive any support.",
" &7New Version: &a" + latest + "&7 - Your Version: &e" + main.PLUGIN_VERSION,
" &7Link:&b https://www.spigotmc.org/resources/96378/", ""
);
break;
case UP_TO_DATE:
records.playerRecord(player,
"", " &eYou have the latest version of S.I.R. &7(" + latest + ")",
" &7I would appreciate if you keep updating &c<3", ""
);
break;
case UNRELEASED_VERSION:
records.playerRecord(player,
"", " &4DEVELOPMENT BUILD:",
" &cYou have a newer version of S.I.R. installed.",
" &cErrors might occur in this build.",
" Spigot Version: &a" + updateResult.getSpigotVersion()
+ "&7 - Your Version: &e" + main.PLUGIN_VERSION, ""
);
break;
default:
records.playerRecord(player,
"", " &4WARNING!",
" &cCould not check for a new version of S.I.R.",
" &7Please check your connection and restart the server.",
" &7Possible reason: &e" + updateResult.getReason(), ""
);
break;
}
}));
}

@EventHandler
private void onJoin(PlayerJoinEvent event) {
if (!text.getOption(1, "enabled")) return;
Expand All @@ -78,7 +31,7 @@ private void onJoin(PlayerJoinEvent event) {

Player player = event.getPlayer();
ConfigurationSection id = utils.lastSection(player, true);
adminUpdateChecker(player);
main.getDoUpdate().initUpdater(null);

if (init.HAS_LOGIN && text.getOption(2, "enabled")) {
if (text.getOption(2, "spawn-before")) utils.goSpawn(id, player);
Expand All @@ -101,7 +54,10 @@ private void onQuit(PlayerQuitEvent event) {

if (utils.isVanished(player, false) &&
text.getOption(3, "silent")) return;
if (init.HAS_LOGIN) utils.loggedPlayers.remove(player);
if (init.HAS_LOGIN) {
if (!utils.LOGGED_PLAYERS.contains(player)) return;
utils.LOGGED_PLAYERS.remove(player);
}

utils.runEvent(id, player, false, false, false);
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ private void onVanish(VanishEvent event) {
ConfigurationSection id = utils.lastSection(player, vanish ? "join" : "quit");

if (!init.HAS_VANISH || !text.getOption(3, "enabled")) return;
if(init.HAS_LOGIN && !utils.loggedPlayers.contains(player)) utils.loggedPlayers.add(player);
if(init.HAS_LOGIN && !utils.LOGGED_PLAYERS.contains(player)) utils.LOGGED_PLAYERS.add(player);

utils.runEvent(id, player, vanish, text.getOption(3, "use-spawn"), false);
}
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/me/croabeast/sircore/utilities/DoUpdate.java
@@ -0,0 +1,102 @@
package me.croabeast.sircore.utilities;

import com.google.common.collect.*;
import me.croabeast.sircore.*;
import me.croabeast.sircore.objects.*;
import org.bukkit.entity.*;

import java.util.*;

public class DoUpdate {

private final Application main;
private final Records records;
private final TextUtils text;

public DoUpdate(Application main) {
this.main = main;
this.records = main.getRecords();
this.text = main.getTextUtils();
}

private void updateLogger(Player player, String... lines) {
List<String> list = Lists.newArrayList(lines);

if (player != null) {
if (list.get(0).equals("RR")) list.remove(0);
list.forEach(s ->
records.playerRecord(player, " " + s)
);
}
else {
if (list.get(0).equals("RR")) {
list.remove(0);
list.forEach(records::rawRecord);
}
else list.forEach(records::doRecord);
}
}

private void runUpdater(Player player) {
Updater.init(main, 96378).updateCheck().whenComplete((result, e) -> {
String latest = result.getNewestVersion();

updateLogger(player, "RR", "");
switch (result.getReason()) {
case NEW_UPDATE:
updateLogger(player,
"&4NEW UPDATE!",
"&cYou don't have the latest version of S.I.R. installed.",
"&cRemember, older versions won't receive any support.",
"&7New Version: &a" + latest +
"&7 - Your Version: &e" + main.PLUGIN_VERSION,
"&7Link:&b https://www.spigotmc.org/resources/96378/"
);
break;

case UP_TO_DATE:
updateLogger(player,
"&eYou have the latest version of S.I.R. &7(" + latest + ")",
"&7I would appreciate if you keep updating &c<3"
);
break;

case UNRELEASED_VERSION:
updateLogger(player,
"&4DEVELOPMENT BUILD:",
"&cYou have a newer version of S.I.R. installed.",
"&cErrors might occur in this build.",
"Spigot Version: &a" + result.getSpigotVersion()
+ "&7 - Your Version: &e" + main.PLUGIN_VERSION
);
break;

default:
updateLogger(player,
"&4WARNING!",
"&cCould not check for a new version of S.I.R.",
"&7Please check your connection and restart the server.",
"&7Possible reason: &e" + result.getReason()
);
break;
}

updateLogger(player, "RR", "");
});
}

public void initUpdater(Player player) {
if (player == null) {
if (!text.getOption(4, "on-start"))
return;
runUpdater(null);
}
else {
boolean perm = main.getEventUtils().
hasPerm(player, "sir.admin.updater");
if (!text.getOption(4, "send-op") || !perm)
return;
runUpdater(player);
}
}
}
Expand Up @@ -23,7 +23,7 @@ public EventUtils(Application main) {
this.text = main.getTextUtils();
}

public List<Player> loggedPlayers = new ArrayList<>();
public List<Player> LOGGED_PLAYERS = new ArrayList<>();

public String doFormat(String line, Player player, boolean isColor) {
String message = StringUtils.replaceEach(line,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/croabeast/sircore/utilities/TextUtils.java
Expand Up @@ -38,10 +38,10 @@ public TextUtils(Application main) {
public boolean getOption(int i, String path) {
String P;

if (i == 4) P = "updater.plugin.";
if (i == 1) P = "options.";
else if (i == 2) P = "login.";
else if (i == 3) P = "vanish.";
else if (i == 1) P = "options.";
else if (i == 4) P = "updater.plugin.";
else P = "";

return main.getConfig().getBoolean(P + path);
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/motd.yml
@@ -0,0 +1,6 @@
########################
# SIR by CroaBeast #
# Depressed since 2004 #
########################
# Want to use unicode characters? Check this site: https://coolsymbol.com

0 comments on commit 46c045f

Please sign in to comment.