Skip to content

Commit

Permalink
add TNTRun party invitation string to messages.yml, refactor textComp…
Browse files Browse the repository at this point in the history
…onent methods
  • Loading branch information
steve4744 committed Jul 30, 2024
1 parent 841871d commit 75dc5c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
12 changes: 12 additions & 0 deletions src/main/java/tntrun/messages/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public class Messages {
public static String partynotexist = "&c The selected party does not exist";
public static String partynoinvite = "&c You do not have an invitation to this party";
public static String partynotenabled = "&c TNTRun parties are not currently enabled";
public static String partyclickaccept = "&6&lAccept";
public static String partyclickdecline = "&6&lDecline";
public static String partyaccepttext = "&7Click to &baccept &7the invitation";
public static String partydeclinetext = "&7Click to &cdecline &7the invitation";

public static void sendMessage(CommandSender sender, String message) {
sendMessage(sender, message, true);
Expand Down Expand Up @@ -381,6 +385,10 @@ public static void loadMessages(TNTRun plugin) {
partynotexist = config.getString("partynotexist", partynotexist);
partynoinvite = config.getString("partynoinvite", partynoinvite);
partynotenabled = config.getString("partynotenabled", partynotenabled);
partyclickaccept = config.getString("partyclickaccept", partyclickaccept);
partyclickdecline = config.getString("partyclickdecline", partyclickdecline);
partyaccepttext = config.getString("partyaccepttext", partyaccepttext);
partydeclinetext = config.getString("partydeclinetext", partydeclinetext);

saveMessages(messageconfig);
}
Expand Down Expand Up @@ -548,6 +556,10 @@ private static void saveMessages(File messageconfig) {
config.set("partynotexist", partynotexist);
config.set("partynoinvite", partynoinvite);
config.set("partynotenabled", partynotenabled);
config.set("partyclickaccept", partyclickaccept);
config.set("partyclickdecline", partyclickdecline);
config.set("partyaccepttext", partyaccepttext);
config.set("partydeclinetext", partydeclinetext);
try {
config.save(messageconfig);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tntrun/parties/Parties.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void inviteToParty(Player player, String targetName) {
invitedMap.computeIfAbsent(player.getName(), k -> new ArrayList<>()).add(targetName);

Messages.sendMessage(Bukkit.getPlayer(targetName), Messages.partyinvite.replace("{PLAYER}", player.getName()));
Utils.displayPartyInvite(player, targetName, "");
Utils.displayPartyInvite(player, targetName);
}

/**
Expand Down
64 changes: 40 additions & 24 deletions src/main/java/tntrun/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,22 @@ public static void displayHelp(Player player) {
player.spigot().sendMessage(getTextComponent("/trsetup finish {arena}", true), getTextComponent(Messages.setupfinish));
}

/**
* Display a clickable invitation message when the first player joins an arena.
*
* @param player first player that joins an arena
* @param arenaname
* @param joinMessage
*/
public static void displayJoinMessage(Player player, String arenaname, String joinMessage) {
final String command = "/tntrun joinorspectate ";
final String border = FormattingCodesParser.parseFormattingCodes(Messages.playerborderinvite);
TextComponent jointc = new TextComponent(TextComponent.fromLegacy(border + "\n"));
jointc.addExtra(getJoinTextComponent(joinMessage, arenaname));
jointc.addExtra(new TextComponent(TextComponent.fromLegacy("\n" + border)));
player.spigot().sendMessage(jointc);
}

private static TextComponent getJoinTextComponent(String text, String arenaname) {
String hoverMessage = FormattingCodesParser.parseFormattingCodes(Messages.playerclickinvite.replace("{ARENA}", arenaname));
Content content = new Text(hoverMessage);
TextComponent component = new TextComponent(TextComponent.fromLegacy(ChatColor.translateAlternateColorCodes('&', text)));
jointc.addExtra(buildComponent(joinMessage, Messages.playerclickinvite.replace("{ARENA}", arenaname), arenaname, command));

component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tntrun joinorspectate " + arenaname));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, content));
return component;
jointc.addExtra(new TextComponent(TextComponent.fromLegacy("\n" + border)));
player.spigot().sendMessage(jointc);
}

public static String getTitleCase(String input) {
Expand Down Expand Up @@ -410,21 +410,37 @@ public static int getAllowedDoubleJumps(Player player, int max) {
return max;
}

public static void displayPartyInvite(Player player, String target, String joinMessage) {
TextComponent partytc = new TextComponent(TextComponent.fromLegacy("Would you like to "));
partytc.addExtra(getPartyInviteComponent("Accept", "Click to Accept", player, target));
partytc.addExtra(" or ");
partytc.addExtra(getPartyInviteComponent("Decline", "Click to Decline", player, target));
partytc.addExtra(" the party invitation?");
Bukkit.getPlayer(target).spigot().sendMessage(partytc);
/**
* Display a clickable invitation to join a TNTRun party.
*
* @param player party leader
* @param target player being invited
*/
public static void displayPartyInvite(Player player, String target) {
final String command1 = "/tntrun party accept ";
final String command2 = "/tntrun party decline ";

TextComponent accept = buildComponent(Messages.partyclickaccept, Messages.partyaccepttext, player.getName(), command1);
TextComponent decline = buildComponent(Messages.partyclickdecline, Messages.partydeclinetext, player.getName(), command2);
accept.addExtra(" | ");

Bukkit.getPlayer(target).spigot().sendMessage(accept, decline);
}

private static TextComponent getPartyInviteComponent(String text, String hoverMessage, Player player, String target) {
Content content = new Text(hoverMessage);
TextComponent component = new TextComponent(text);
component.setColor(ChatColor.GOLD);
component.setBold(true);
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tntrun party " + text + " " + player.getName()));
/**
* Add the click event and hover text to the text component.
*
* @param text
* @param hoverMessage
* @param target target of command, e.g. party leader or arena
* @param command
* @return
*/
private static TextComponent buildComponent(String text, String hoverMessage, String target, String command) {
Content content = new Text(ChatColor.translateAlternateColorCodes('&', hoverMessage));
TextComponent component = new TextComponent(ChatColor.translateAlternateColorCodes('&', text));

component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command + target));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, content));
return component;
}
Expand Down

0 comments on commit 75dc5c3

Please sign in to comment.