Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Plugin: add messages for console
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Nov 5, 2023
1 parent 3f962a9 commit 1316580
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class FPlayerComponent extends FComponent{

public FPlayerComponent(@Nullable CommandSender sender, @NotNull CommandSender recipient, @NotNull String text) {
public FPlayerComponent(@Nullable CommandSender sender, @Nullable CommandSender recipient, @NotNull String text) {
super(text);

if (!(sender instanceof Player player) || !(recipient instanceof Player recip)) return;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/flectone/chat/model/player/FPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.flectone.chat.util.MessageUtil;
import net.flectone.chat.util.PlayerUtil;
import net.flectone.chat.util.TimeUtil;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -342,4 +343,12 @@ public void playSound(@Nullable Player sender, @NotNull Player recipient, @NotNu
public void playSound(@Nullable Player sender, @NotNull Collection<Player> recipients, @NotNull String action) {
recipients.forEach(recipient -> playSound(sender, recipient, action));
}

public static void sendToConsole(@NotNull String message) {
Bukkit.getConsoleSender().sendMessage(MessageUtil.formatAll(null, message));
}

public static void sendToConsole(@NotNull BaseComponent[] baseComponents) {
Bukkit.getConsoleSender().spigot().sendMessage(baseComponents);
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/flectone/chat/module/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void sendGlobalMessage(@NotNull Collection<Player> recipients, @Nullable
@Nullable ItemStack itemStack, @NotNull String format,
@NotNull String message, boolean isClickable) {

FPlayer.sendToConsole(format.replace("<message>", message));

if (player != null) {
message = IntegrationsModule.interactiveChatMark(message, player.getUniqueId());
}
Expand All @@ -133,7 +135,6 @@ public void sendGlobalMessage(@NotNull Collection<Player> recipients, @Nullable
soundsModule.play(new FSound(player, recipient, this.toString()));
}
});

}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

Expand Down Expand Up @@ -75,29 +76,39 @@ public void sendAll(@NotNull Player sender, @NotNull FAdvancement fAdvancement,

if (fPlayer.getIgnoreList().contains(sender.getUniqueId())) return;

FComponentBuilder fComponentBuilder = new FComponentBuilder(message);
sendMessage(sender, player, fAdvancement, message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));
fPlayer.playSound(sender, player, this.toString());
});

fComponentBuilder.replace("<advancement>", ((componentBuilder, color) -> {
FComponent advancementComponent = new FLocaleComponent(fAdvancement.getTranslateKey());
sendMessage(sender, null, fAdvancement, message);
}

String hover = locale.getVaultString(sender, this + "." + fAdvancement.getType() + ".hover");
FComponentBuilder hoverComponentBuilder = new FComponentBuilder(hover);
hoverComponentBuilder.replace("<name>", (hoverBuilder, hoverColor) ->
hoverBuilder.append(new FColorComponent(new FLocaleComponent(fAdvancement.getTranslateKey()), hoverColor).get()));
hoverComponentBuilder.replace("<description>", (hoverBuilder, hoverColor) ->
hoverBuilder.append(new FColorComponent(new FLocaleComponent(fAdvancement.getTranslateDesc()), hoverColor).get()));
public void sendMessage(@NotNull Player sender, @Nullable Player player, @NotNull FAdvancement fAdvancement, @NotNull String message) {
FComponentBuilder fComponentBuilder = new FComponentBuilder(message);

advancementComponent.addHoverText(hoverComponentBuilder.build(sender, player));
fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

componentBuilder.append(advancementComponent.get());
}));
fComponentBuilder.replace("<advancement>", ((componentBuilder, color) -> {
FComponent advancementComponent = new FLocaleComponent(fAdvancement.getTranslateKey());

player.spigot().sendMessage(fComponentBuilder.build(sender, player));
String hover = locale.getVaultString(sender, this + "." + fAdvancement.getType() + ".hover");
FComponentBuilder hoverComponentBuilder = new FComponentBuilder(hover);
hoverComponentBuilder.replace("<name>", (hoverBuilder, hoverColor) ->
hoverBuilder.append(new FColorComponent(new FLocaleComponent(fAdvancement.getTranslateKey()), hoverColor).get()));
hoverComponentBuilder.replace("<description>", (hoverBuilder, hoverColor) ->
hoverBuilder.append(new FColorComponent(new FLocaleComponent(fAdvancement.getTranslateDesc()), hoverColor).get()));

fPlayer.playSound(sender, player, this.toString());
});
advancementComponent.addHoverText(hoverComponentBuilder.build(sender, player));

componentBuilder.append(advancementComponent.get());
}));

if (player != null) {
player.spigot().sendMessage(fComponentBuilder.build(sender, player));
} else {
FPlayer.sendToConsole(fComponentBuilder.build(sender, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,57 +49,68 @@ public void sendAll(@NotNull Player sender, @NotNull PlayerDamager playerDamager

if (fPlayer.getIgnoreList().contains(sender.getUniqueId())) return;

FComponentBuilder fComponentBuilder = new FComponentBuilder(message);
sendMessage(sender, player, playerDamager, message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));
fPlayer.playSound(sender, player, this.toString());
});

fComponentBuilder.replace("<projectile>", ((componentBuilder, color) ->
createEntityComponent(sender, player, playerDamager.getFinalEntity(), componentBuilder, color)));
sendMessage(sender, null, playerDamager, message);
}

fComponentBuilder.replace("<killer>", ((componentBuilder, color) ->
createEntityComponent(sender, player, playerDamager.getFinalEntity(), componentBuilder, color)));
public void sendMessage(@NotNull Player sender, @Nullable Player player, @NotNull PlayerDamager playerDamager, @NotNull String message) {
FComponentBuilder fComponentBuilder = new FComponentBuilder(message);

fComponentBuilder.replace("<block>", ((componentBuilder, color) -> {
if (!playerDamager.isFinalBlock()) return;
componentBuilder.append(new FColorComponent(new FLocaleComponent(playerDamager), color).get());
}));
fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

fComponentBuilder.replace("<due_to>", ((componentBuilder, color) -> {
if (playerDamager.getKiller() == null
|| playerDamager.getKiller().equals(playerDamager.getFinalEntity())
|| (playerDamager.getFinalEntity() != null && playerDamager.getKiller().getType().equals(playerDamager.getFinalEntity().getType()))) {
return;
}
fComponentBuilder.replace("<projectile>", ((componentBuilder, color) ->
createEntityComponent(sender, player, playerDamager.getFinalEntity(), componentBuilder, color)));

String formatDueToMessage = locale.getVaultString(player, this + ".due-to");
fComponentBuilder.replace("<killer>", ((componentBuilder, color) ->
createEntityComponent(sender, player, playerDamager.getFinalEntity(), componentBuilder, color)));

FComponentBuilder dueToComponentBuilder = new FComponentBuilder(formatDueToMessage);
fComponentBuilder.replace("<block>", ((componentBuilder, color) -> {
if (!playerDamager.isFinalBlock()) return;
componentBuilder.append(new FColorComponent(new FLocaleComponent(playerDamager), color).get());
}));

dueToComponentBuilder.replace("<killer>", (dueToBuilder, dueToColor) ->
createEntityComponent(sender, player, playerDamager.getKiller(), dueToBuilder, dueToColor));
fComponentBuilder.replace("<due_to>", ((componentBuilder, color) -> {
if (playerDamager.getKiller() == null
|| playerDamager.getKiller().equals(playerDamager.getFinalEntity())
|| (playerDamager.getFinalEntity() != null && playerDamager.getKiller().getType().equals(playerDamager.getFinalEntity().getType()))) {
return;
}

componentBuilder.append(dueToComponentBuilder.build(sender, player));
}));
String formatDueToMessage = locale.getVaultString(player, this + ".due-to");

fComponentBuilder.replace("<by_item>", (componentBuilder, color) -> {
if (playerDamager.getKillerItemName() == null) return;
FComponentBuilder dueToComponentBuilder = new FComponentBuilder(formatDueToMessage);

String formatMessage = locale.getVaultString(sender, this + ".by-item");
FComponentBuilder byItemComponentBuilder = new FComponentBuilder(formatMessage);
dueToComponentBuilder.replace("<killer>", (dueToBuilder, dueToColor) ->
createEntityComponent(sender, player, playerDamager.getKiller(), dueToBuilder, dueToColor));

byItemComponentBuilder.replace("<item>", (byItemBuilder, byItemColor) ->
byItemBuilder.append(new FColorComponent(new FLocaleComponent(playerDamager.getKillerItem()), byItemColor).get()));
componentBuilder.append(dueToComponentBuilder.build(sender, player));
}));

componentBuilder.append(byItemComponentBuilder.build(sender, player));
});
fComponentBuilder.replace("<by_item>", (componentBuilder, color) -> {
if (playerDamager.getKillerItemName() == null) return;

player.spigot().sendMessage(fComponentBuilder.build(sender, player));
fPlayer.playSound(sender, player, this.toString());
String formatMessage = locale.getVaultString(sender, this + ".by-item");
FComponentBuilder byItemComponentBuilder = new FComponentBuilder(formatMessage);

byItemComponentBuilder.replace("<item>", (byItemBuilder, byItemColor) ->
byItemBuilder.append(new FColorComponent(new FLocaleComponent(playerDamager.getKillerItem()), byItemColor).get()));

componentBuilder.append(byItemComponentBuilder.build(sender, player));
});

if (player != null) {
player.spigot().sendMessage(fComponentBuilder.build(sender, player));
} else {
FPlayer.sendToConsole(fComponentBuilder.build(sender, null));
}
}

public void createEntityComponent(@NotNull Player sender, @NotNull Player player, @Nullable Entity entity, @NotNull ComponentBuilder componentBuilder, String color) {
public void createEntityComponent(@NotNull Player sender, @Nullable Player player, @Nullable Entity entity, @NotNull ComponentBuilder componentBuilder, String color) {
if (entity == null) return;

if (entity instanceof Player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.flectone.chat.util.PlayerUtil;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JoinModule extends FModule {
public JoinModule(FModule module, String name) {
Expand Down Expand Up @@ -37,13 +38,24 @@ public void sendAll(@NotNull Player sender, @NotNull String message) {

if (fPlayer.getIgnoreList().contains(sender.getUniqueId())) return;

FComponentBuilder fComponentBuilder = new FComponentBuilder(message);
sendMessage(sender, player, message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

player.spigot().sendMessage(fComponentBuilder.build(sender, player));
fPlayer.playSound(sender, player, this.toString());
});

sendMessage(sender, null, message);
}

public void sendMessage(@NotNull Player sender, @Nullable Player player, @NotNull String message) {
FComponentBuilder fComponentBuilder = new FComponentBuilder(message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

if (player != null) {
player.spigot().sendMessage(fComponentBuilder.build(sender, player));
} else {
FPlayer.sendToConsole(fComponentBuilder.build(sender, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.flectone.chat.util.PlayerUtil;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class QuitModule extends FModule {
public QuitModule(FModule module, String name) {
Expand Down Expand Up @@ -37,13 +38,24 @@ public void sendAll(@NotNull Player sender, @NotNull String message) {

if (fPlayer.getIgnoreList().contains(sender.getUniqueId())) return;

FComponentBuilder fComponentBuilder = new FComponentBuilder(message);
sendMessage(sender, player, message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

player.spigot().sendMessage(fComponentBuilder.build(sender, player));
fPlayer.playSound(sender, player, this.toString());
});

sendMessage(sender, null, message);
}

public void sendMessage(@NotNull Player sender, @Nullable Player player, @NotNull String message) {
FComponentBuilder fComponentBuilder = new FComponentBuilder(message);

fComponentBuilder.replace("<player>", (componentBuilder, color) ->
componentBuilder.append(new FPlayerComponent(sender, player, color + sender.getName()).get()));

if (player != null) {
player.spigot().sendMessage(fComponentBuilder.build(sender, player));
} else {
FPlayer.sendToConsole(fComponentBuilder.build(sender, null));
}
}
}

0 comments on commit 1316580

Please sign in to comment.