Skip to content

Commit

Permalink
Fixed Deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
XDPXI committed Nov 27, 2024
1 parent 04cea21 commit 18e14bf
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 71 deletions.
15 changes: 14 additions & 1 deletion src/main/java/dev/xdpxi/xdlib/api/plugin/chatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.kyori.adventure.text.Component;

public class chatUtils {
public static void sendMessageToAll(String message) {
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendMessage(message);
}
}

public static void sendMessageToOps(String message) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.isOp()) {
player.sendMessage(message);
}
}
}
public static void sendMessageToAll(Component message) {
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendMessage(message);
}
}

public static void sendMessageToOps(Component message) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.isOp()) {
player.sendMessage(message);
}
}
}
}
21 changes: 17 additions & 4 deletions src/main/java/dev/xdpxi/xdlib/plugin/joinLeaveListener.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.xdpxi.xdlib.plugin;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;

public class joinLeaveListener implements Listener {
private final xdlib plugin;
Expand All @@ -16,18 +18,29 @@ public joinLeaveListener(xdlib plugin) {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.setJoinMessage(ChatColor.GREEN + ">>> " + ChatColor.WHITE + event.getPlayer().getName());
event.joinMessage(Component.text(">>> " + event.getPlayer().getName())
.color(NamedTextColor.WHITE)
.decoration(TextDecoration.BOLD, false));

Player player = event.getPlayer();
if (player.isOp()) {
if (updateChecker.isUpdate()) {
player.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + "XDLib" + ChatColor.RED + "]" + ChatColor.DARK_GRAY + " - " + ChatColor.YELLOW + "An update is available!");
player.sendMessage(Component.text()
.append(Component.text("[", NamedTextColor.RED))
.append(Component.text("XDLib", NamedTextColor.GOLD))
.append(Component.text("]", NamedTextColor.RED))
.append(Component.text(" - ", NamedTextColor.DARK_GRAY))
.append(Component.text("An update is available!", NamedTextColor.YELLOW))
.build());
}
}
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
event.setQuitMessage(ChatColor.RED + "<<< " + ChatColor.WHITE + event.getPlayer().getName());
event.quitMessage(Component.text()
.append(Component.text("<<< ", NamedTextColor.RED))
.append(Component.text(event.getPlayer().getName(), NamedTextColor.WHITE))
.build());
}
}
32 changes: 18 additions & 14 deletions src/main/java/dev/xdpxi/xdlib/plugin/tpaCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -12,6 +11,9 @@
import java.util.HashMap;
import java.util.UUID;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

public class tpaCommand implements CommandExecutor {
private final HashMap<UUID, UUID> teleportRequests = new HashMap<>();

Expand Down Expand Up @@ -48,58 +50,60 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
private void handleAccept(Player player) {
UUID requesterUUID = teleportRequests.remove(player.getUniqueId());
if (requesterUUID == null) {
player.sendMessage(ChatColor.RED + "No teleport request found.");
player.sendMessage(Component.text("No teleport request found.", NamedTextColor.RED));
return;
}

Player requester = Bukkit.getPlayer(requesterUUID);
if (requester == null) {
player.sendMessage(ChatColor.RED + "Player not found.");
player.sendMessage(Component.text("Player not found.", NamedTextColor.RED));
return;
}

requester.teleport(player.getLocation());
player.sendMessage(ChatColor.GOLD + "Teleporting...");
requester.sendMessage(ChatColor.GOLD + "Teleport request " + ChatColor.GREEN + "accepted" + ChatColor.GOLD + " by " + ChatColor.GREEN + player.getName() + ChatColor.GOLD + ".");
player.sendMessage(Component.text("Teleporting...", NamedTextColor.GOLD));
requester.sendMessage(Component.text("Teleport request accepted by " + player.getName(), NamedTextColor.GOLD));
}

private void handleDecline(Player player) {
UUID requesterUUID = teleportRequests.remove(player.getUniqueId());
if (requesterUUID == null) {
player.sendMessage(ChatColor.RED + "No teleport request found.");
player.sendMessage(Component.text("No teleport request found.", NamedTextColor.RED));
return;
}

Player requester = Bukkit.getPlayer(requesterUUID);
if (requester == null) {
player.sendMessage(ChatColor.RED + "Player not found.");
player.sendMessage(Component.text("Player not found.", NamedTextColor.RED));
return;
}

player.sendMessage(ChatColor.GOLD + "Teleport request " + ChatColor.RED + "declined" + ChatColor.GOLD + ".");
requester.sendMessage(ChatColor.GOLD + "Teleport request" + ChatColor.RED + " declined" + ChatColor.GOLD + " by " + ChatColor.GREEN + player.getName());
player.sendMessage(Component.text("Teleport request declined.", NamedTextColor.GOLD));
requester.sendMessage(Component.text("Teleport request declined by " + player.getName(), NamedTextColor.GOLD));
}

private void handleTpaRequest(Player player, String targetName) {
Player target = Bukkit.getPlayer(targetName);
if (target == null) {
player.sendMessage(ChatColor.RED + "Player not found.");
player.sendMessage(Component.text("Player not found.", NamedTextColor.RED));
return;
}

teleportRequests.put(target.getUniqueId(), player.getUniqueId());

TextComponent message = new TextComponent(ChatColor.GREEN + player.getName() + ChatColor.GOLD + " has requested to teleport to you. \n");
TextComponent accept = new TextComponent(ChatColor.GREEN + "[Accept]");
TextComponent message = new TextComponent(player.getName() + " has requested to teleport to you. \n");
TextComponent accept = new TextComponent("[Accept]");
accept.setColor(net.md_5.bungee.api.ChatColor.GREEN);
accept.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpa accept"));
TextComponent decline = new TextComponent(ChatColor.RED + "[Decline]");
TextComponent decline = new TextComponent("[Decline]");
decline.setColor(net.md_5.bungee.api.ChatColor.RED);
decline.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpa decline"));

message.addExtra(accept);
message.addExtra(" ");
message.addExtra(decline);

target.spigot().sendMessage(message);
player.sendMessage(ChatColor.GOLD + "Teleport request sent to " + ChatColor.GREEN + target.getName() + ChatColor.GOLD + ".");
player.sendMessage(Component.text("Teleport request sent to " + target.getName(), NamedTextColor.GOLD));
}
}
8 changes: 5 additions & 3 deletions src/main/java/dev/xdpxi/xdlib/plugin/updateChecker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.xdpxi.xdlib.plugin;

import dev.xdpxi.xdlib.api.plugin.chatUtils;
import org.bukkit.ChatColor;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -10,6 +9,9 @@
import java.net.HttpURLConnection;
import java.net.URI;

import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.Component;

public class updateChecker {
private final xdlib plugin;
private static boolean update = false;
Expand Down Expand Up @@ -39,12 +41,12 @@ public void checkForUpdate() {

String latestVersion = parseLatestVersion(response.toString());
latestVersion = textParser(latestVersion);
String currentVersion = plugin.getDescription().getVersion();
String currentVersion = plugin.getPluginMeta().getVersion();
currentVersion = textParser(currentVersion);

if (isVersionLower(currentVersion, latestVersion)) {
plugin.getLogger().info("[XDLib] - An update is available!");
chatUtils.sendMessageToOps(ChatColor.GOLD + "[XDLib] - An update is available!");
chatUtils.sendMessageToOps(Component.text("[XDLib] - An update is available!", NamedTextColor.GOLD));
update = true;
} else {
plugin.getLogger().info("[XDLib] - No update available!");
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/xdpxi/xdlib/plugin/welcomeListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dev.xdpxi.xdlib.plugin;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

public class welcomeListener implements Listener {
private final xdlib plugin;
Expand All @@ -15,7 +16,10 @@ public welcomeListener(xdlib plugin) {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if (!event.getPlayer().hasPlayedBefore() && plugin.getConfig().getBoolean("welcomeMessage")) {
event.getPlayer().sendMessage(ChatColor.GOLD + "Welcome " + ChatColor.GREEN + event.getPlayer().getName() + ChatColor.GOLD + " to the server!");
event.getPlayer().sendMessage(Component.text("Welcome ")
.color(NamedTextColor.GOLD)
.append(Component.text(event.getPlayer().getName()).color(NamedTextColor.GREEN))
.append(Component.text(" to the server!").color(NamedTextColor.GOLD)));
}
}
}
48 changes: 1 addition & 47 deletions src/main/java/dev/xdpxi/xdlib/plugin/xdlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.HashMap;
import java.util.UUID;

public final class xdlib extends JavaPlugin implements CommandExecutor {
public final class xdlib extends JavaPlugin {
private final HashMap<UUID, UUID> teleportRequests = new HashMap<>();
private welcomeListener WelcomeListener;
private chatListener ChatListener;
Expand Down Expand Up @@ -82,50 +82,4 @@ private void setConfig() {
public void onDisable() {
getLogger().info("[XDLib] - Disabled!");
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("xdlib")) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("xdlib.reload")) {
reloadConfig();
setConfig();
sender.sendMessage("[XDLib] - Config reloaded!");
} else {
sender.sendMessage("[XDLib] - You do not have permission to execute this command.");
}
} else if (args[0].equalsIgnoreCase("help")) {
sender.sendMessage("Available commands:\n/xdlib - Displays the configured message\n/xdlib reload - Reloads the plugin configuration\n/xdlib help - Shows this help message");
} else {
sender.sendMessage("[XDLib] - Use '/xdlib help' for a list of commands.");
}
} else {
String message = getConfig().getString("message");
sender.sendMessage(message);
}
return true;
}
return false;
}

private void unregisterCommand(String commandName) {
try {
Field commandMapField = getServer().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
CommandMap commandMap = (CommandMap) commandMapField.get(getServer());

PluginCommand command = getCommand(commandName);
if (command != null) {
command.unregister(commandMap);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean isCommandAvailable(String commandName) {
PluginCommand command = getServer().getPluginCommand(commandName);
return command != null;
}
}

0 comments on commit 18e14bf

Please sign in to comment.