Skip to content

Commit

Permalink
Revamp UChat; Add UChat#combined for both MiniMessage & legacy support
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberedCake committed Jun 28, 2023
1 parent 92c58b2 commit 64d0094
Show file tree
Hide file tree
Showing 9 changed files with 922 additions and 67 deletions.
59 changes: 30 additions & 29 deletions bungee/src/main/java/net/cybercake/cyberapi/bungee/CyberAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import net.cybercake.cyberapi.bungee.basic.BetterStackTraces;
import net.cybercake.cyberapi.bungee.chat.Broadcast;
import net.cybercake.cyberapi.bungee.chat.Log;
import net.cybercake.cyberapi.bungee.chat.UChat;
import net.cybercake.cyberapi.bungee.config.Config;
Expand Down Expand Up @@ -597,47 +598,47 @@ public void sendTitle(ProxiedPlayer player, String title, String subtitle) {
* Broadcast a message to all online players and logs to console
* @param message the message to send
* @since 1
* @see UChat#broadcast(String, String)
* @see UChat#broadcast(Component, Predicate)
* @see Broadcast#chat(String, String)
* @see Broadcast#chat(String, Predicate)
*/
public static void broadcast(String message) {
UChat.broadcast(message);
Broadcast.chat(message);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send
* @param permission the permission required to see the message
* @since 1
* @see UChat#broadcast(String)
* @see UChat#broadcast(String, Predicate)
* @see Broadcast#chat(String)
* @see Broadcast#chat(String, Predicate)
*/
public static void broadcast(String message, @Nullable String permission) {
UChat.broadcast(message, permission);
Broadcast.chat(message, permission);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send
* @param filter the filter of command senders that can see this message, note: only {@link ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126
* @see UChat#broadcast(String)
* @see UChat#broadcast(String, String)
* @see Broadcast#chat(String)
* @see Broadcast#chat(String, String)
*/
public static void broadcast(String message, @Nullable Predicate<? super CommandSender> filter) {
UChat.broadcast(message, filter);
Broadcast.chat(message, filter);
}

/**
* Broadcast a message to all online players and logs to console
* @param message the message to send, as a {@link Component}
* @since 125
* @apiNote requires Adventure API support
* @see UChat#broadcast(Component, String)
* @see UChat#broadcast(Component, Predicate)
* @see Broadcast#component(Component, String)
* @see Broadcast#component(Component, Predicate)
*/
public static void broadcast(Component message) {
UChat.broadcast(message);
Broadcast.component(message);
}

/**
Expand All @@ -646,11 +647,11 @@ public static void broadcast(Component message) {
* @param permission the permission required to see the message
* @since 125
* @apiNote requires Adventure API Support
* @see UChat#broadcast(Component)
* @see UChat#broadcast(Component, Predicate)
* @see Broadcast#component(Component)
* @see Broadcast#component(Component, Predicate)
*/
public static void broadcast(Component message, @Nullable String permission) {
UChat.broadcast(message, permission);
Broadcast.component(message, permission);
}

/**
Expand All @@ -659,46 +660,46 @@ public static void broadcast(Component message, @Nullable String permission) {
* @param filter the filter of command senders that can see this message, note: only {@link ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126
* @apiNote requires Adventure API support
* @see UChat#broadcast(Component)
* @see UChat#broadcast(Component, String)
* @see Broadcast#component(Component)
* @see Broadcast#component(Component, String)
*/
public static void broadcast(Component message, @Nullable Predicate<? super CommandSender> filter) {
UChat.broadcast(message, filter);
Broadcast.component(message, filter);
}

/**
* Broadcast a message to all online players and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @since 125
* @see UChat#broadcast(BaseComponent, String)
* @see UChat#broadcast(BaseComponent, Predicate)
* @see Broadcast#bComponent(BaseComponent, String)
* @see Broadcast#bComponent(BaseComponent, Predicate)
*/
public static void broadcast(BaseComponent message) {
UChat.broadcast(message);
Broadcast.bComponent(message);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @param permission the permission required to see the message
* @since 125
* @see UChat#broadcast(BaseComponent)
* @see UChat#broadcast(BaseComponent, Predicate)
* @see Broadcast#bComponent(BaseComponent)
* @see Broadcast#bComponent(BaseComponent, Predicate)
*/
public static void broadcast(BaseComponent message, @Nullable String permission) {
UChat.broadcast(message, permission);
Broadcast.bComponent(message, permission);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @param filter the filter of command senders that can see this message, note: only {@link ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126
* @see UChat#broadcast(BaseComponent)
* @see UChat#broadcast(BaseComponent, String)
* @see Broadcast#bComponent(BaseComponent)
* @see Broadcast#bComponent(BaseComponent, String)
*/
public static void broadcast(BaseComponent message, @Nullable Predicate<? super CommandSender> filter) {
UChat.broadcast(message, filter);
Broadcast.bComponent(message, filter);
}

/**
Expand Down Expand Up @@ -1203,8 +1204,8 @@ public void technoblade() {
UUID uuid = UUID.fromString("b876ec32-e396-476b-a115-8438d83c67d4"); // Technoblade UUID
try {
CyberPlayer cyberPlayer = new CyberPlayer(uuid);
UChat.broadcast("&d&lA TRIBUTE TO TECHNOBLADE:");
UChat.broadcast(cyberPlayer.getUserHead(
Broadcast.chat("&d&lA TRIBUTE TO TECHNOBLADE:");
Broadcast.chat(cyberPlayer.getUserHead(
UserHeadSettings.builder()
.showHelmet(true)
.character('\u2B1B')
Expand Down
129 changes: 129 additions & 0 deletions bungee/src/main/java/net/cybercake/cyberapi/bungee/chat/Broadcast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package net.cybercake.cyberapi.bungee.chat;

import net.cybercake.cyberapi.bungee.CyberAPI;
import net.cybercake.cyberapi.bungee.Validators;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.Nullable;

import java.util.function.Predicate;

public class Broadcast {

/**
* Broadcast a message to all online players and logs to console
* @param message the message to send
* @since 1 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#chat(String, String)
* @see Broadcast#chat(String, Predicate)
*/
public static void chat(String message) { chat(message, (Predicate<? super CommandSender>) null); }

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send
* @param permission the permission required to see the message
* @since 1 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#chat(String)
* @see Broadcast#chat(String, Predicate)
*/
public static void chat(String message, @Nullable String permission) {
chat(message, player ->
permission == null
|| permission.strip().equalsIgnoreCase("")
|| player.hasPermission(permission)
);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send
* @param filter the filter of command senders that can see this message, note: only {@link net.md_5.bungee.api.connection.ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#chat(String)
* @see Broadcast#chat(String, String)
*/
public static void chat(String message, @Nullable Predicate<? super CommandSender> filter) {
for(ProxiedPlayer player : CyberAPI.getInstance().getOnlinePlayers()) {
if(filter != null && !filter.test(player)) continue;
player.sendMessage(UChat.bComponent(message));
}
if(filter == null || filter.test(CyberAPI.getInstance().getProxy().getConsole()))
Log.info(message);
}

/**
* Broadcast a message to all online players and logs to console
* @param message the message to send, as a {@link Component}
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @apiNote requires Adventure API support
* @see Broadcast#component(Component, String)
* @see Broadcast#component(Component, Predicate)
*/
public static void component(Component message) { component(message, (Predicate<? super CommandSender>) null); }

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link Component}
* @param permission the permission required to see the message
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @apiNote requires Adventure API Support
* @see Broadcast#component(Component)
* @see Broadcast#component(Component, Predicate)
*/
public static void component(Component message, @Nullable String permission) {
Validators.validateAdventureSupport();
chat(LegacyComponentSerializer.legacyAmpersand().serialize(message), permission);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link Component}
* @param filter the filter of command senders that can see this message, note: only {@link ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @apiNote requires Adventure API support
* @see Broadcast#component(Component)
* @see Broadcast#component(Component, String)
*/
public static void component(Component message, @Nullable Predicate<? super CommandSender> filter) {
Validators.validateAdventureSupport();
chat(LegacyComponentSerializer.legacyAmpersand().serialize(message), filter);
}

/**
* Broadcast a message to all online players and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#bComponent(BaseComponent, String)
* @see Broadcast#bComponent(BaseComponent, Predicate)
*/
public static void bComponent(BaseComponent message) { bComponent(message, (Predicate<? super CommandSender>) null); }

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @param permission the permission required to see the message
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#bComponent(BaseComponent)
* @see Broadcast#bComponent(BaseComponent, Predicate)
*/
public static void bComponent(BaseComponent message, @Nullable String permission) {
chat(BaseComponent.toLegacyText(message), permission);
}

/**
* Broadcast a message to all online players if they have a specified permission and logs to console
* @param message the message to send, as a {@link BaseComponent}
* @param filter the filter of command senders that can see this message, note: only {@link ProxiedPlayer players} and {@link CommandSender console} are checked!
* @since 126 (creation), 139 (moved to {@link Broadcast})
* @see Broadcast#bComponent(BaseComponent)
* @see Broadcast#bComponent(BaseComponent, String)
*/
public static void bComponent(BaseComponent message, @Nullable Predicate<? super CommandSender> filter) {
chat(BaseComponent.toLegacyText(message), filter);
}

}
Loading

0 comments on commit 64d0094

Please sign in to comment.