Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Branchyz committed Apr 18, 2022
1 parent b928646 commit dd85f21
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.Branchyz</groupId>
<artifactId>WayPointChat</artifactId>
<version>1.3.2</version>
<version>1.4.1</version>
<packaging>jar</packaging>

<name>WayPointChat</name>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/me/branchyz/waypointchat/WayPointChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public void onEnable() {
Metrics metrics = new Metrics(this, 14695);
log("Metrics initialized!", Level.INFO);

latestVersion = !UpdateChecker.hasUpdate(100846, this);
if(!latestVersion) log("An update is available!", Level.WARNING);
log("Checking for updates...", Level.INFO);
latestVersion = UpdateChecker.isLatestVersion(100846, this);
if (!latestVersion) log("An update is available!", Level.WARNING);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public void onJoin(PlayerJoinEvent e) {
if(!e.getPlayer().hasPermission("waypoint.version")) return;
if(plugin.isLatestVersion()) return;

final TextComponent msg = new TextComponent(Messages.getPluginPrefix() + format("&cA new version is available, click here to download!"));
final TextComponent msg = new TextComponent(format(Messages.getPluginPrefix() + "&cA &cnew &cversion &cis &cavailable, &cclick &chere &cto &cdownload!"));

msg.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/waypointchat-1-12-2-1-18-2.100846/"));
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(format("Visit the Spigot website!")).create()));
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(format("&6Visit &6the &6Spigot &6website!")).create()));
// Color code before every word? TextComponent bug fix (Colors not displaying on a new line)

p.spigot().sendMessage(msg);
}
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/me/branchyz/waypointchat/runnable/AutoBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
Expand All @@ -14,16 +15,24 @@ public class AutoBroadcast extends BukkitRunnable {
private final String[] messages;
private static ArrayList<AutoBroadcast> broadcasts = new ArrayList<>();

private AutoBroadcast(String[] messages, int interval, int start, WayPointChat plugin) {
private final String world;

private AutoBroadcast(String[] messages, int interval, int start, String world, WayPointChat plugin) {
this.messages = messages;
this.world = world;
this.runTaskTimerAsynchronously(plugin, (long) start * 20 * 60, (long) interval * 20 * 60);
}

@Override
public void run() {
for (String msg : messages) {
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg));
}
if(world == null)
for (String msg : messages)
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg));
else
for(Player p : Bukkit.getOnlinePlayers())
if(p.getWorld().getName().equals(world))
for (String msg : messages)
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg));
}

public static void initialize(WayPointChat plugin) {
Expand All @@ -33,9 +42,10 @@ public static void initialize(WayPointChat plugin) {
continue;
final int start = sec.getInt(key + ".start");
final int interval = sec.getInt(key + ".interval");
final String world = sec.getString(key + ".world");
final String[] messages = sec.getStringList(key + ".messages").toArray(new String[0]);

broadcasts.add(new AutoBroadcast(messages, interval, start, plugin));
broadcasts.add(new AutoBroadcast(messages, interval, start, world, plugin));
plugin.log("Registered the \"" + key + "\" auto-broadcast!", Level.INFO);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package me.branchyz.waypointchat.util;

import me.branchyz.waypointchat.WayPointChat;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.net.URL;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.logging.Level;

public class UpdateChecker {
private static void getVersion(int id, JavaPlugin plugin, final Consumer<String> consumer) {
Expand All @@ -21,10 +23,10 @@ private static void getVersion(int id, JavaPlugin plugin, final Consumer<String>
});
}

public static boolean hasUpdate(int id, JavaPlugin plugin) {
public static boolean isLatestVersion(int id, WayPointChat plugin) {
final boolean[] result = new boolean[1];
getVersion(id, plugin, version -> {
result[0] = plugin.getDescription().getVersion().equalsIgnoreCase(version);
result[0] = plugin.getDescription().getVersion().equals(version);
});

return result[0];
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/auto-broadcast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# "start" is how long it takes in minutes to broadcast for the first time.
# With "start" you can time the broadcasts, so that they don't all broadcast at the same time.
# "interval" is the amount of time in minutes between broadcasts.
# "world" is an optional parameter, if set, the message will only be sent to players in that world.

auto-broadcast:
discord:
Expand All @@ -21,6 +22,7 @@ auto-broadcast:
- "&6https://discord.gg/spigotmc"
start: 0 #Minutes
interval: 10 #Minutes
world: "discord-world" # Name of the world, this auto-broadcast will only display in the world named "discord-world".
website:
messages:
- "&6Don't forget to visit our website!"
Expand Down

0 comments on commit dd85f21

Please sign in to comment.