mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
1 parent
44c58eeec4
commit
be5e10037b
3 files changed
+102
-19
No files matched your search
+46
@@ -0,0 +1,46 @@
|
||||
package net.draycia.carbon.bukkit.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
|
||||
public class BukkitPlayerDeathListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
|
||||
@Inject
|
||||
public BukkitPlayerDeathListener(
|
||||
final CarbonChat carbonChat,
|
||||
final PrimaryConfig primaryConfig
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.primaryConfig = primaryConfig;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDeath(final PlayerDeathEvent event) {
|
||||
// Early exit in case "hide muted join / leave messages when muted" is disabled
|
||||
if (!this.primaryConfig.hideMutedJoinLeaveQuit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult result = this.carbonChat.server().player(event.getEntity().getUniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
if (player.muted()) {
|
||||
event.deathMessage(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+50
-19
@@ -4,7 +4,9 @@ import com.google.inject.Inject;
|
||||
import java.util.Objects;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.bukkit.util.BukkitCapabilities;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
@@ -15,40 +17,69 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public class BukkitPlayerJoinListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
|
||||
@Inject
|
||||
public BukkitPlayerJoinListener(final CarbonChat carbonChat) {
|
||||
public BukkitPlayerJoinListener(
|
||||
final CarbonChat carbonChat,
|
||||
final PrimaryConfig primaryConfig
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.primaryConfig = primaryConfig;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerLogin(final PlayerJoinEvent event) {
|
||||
this.carbonChat.server()
|
||||
.player(event.getPlayer().getUniqueId()).thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
final ComponentPlayerResult result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this.carbonChat, () -> {
|
||||
if (player.hasCustomDisplayName()) {
|
||||
player.displayName(player.displayName());
|
||||
} else {
|
||||
final @Nullable Component nickname = this.createDefaultNickname(player);
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
if (nickname != null) {
|
||||
player.temporaryDisplayName(nickname);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
// Apply temporary and non temporary display names
|
||||
if (player.hasCustomDisplayName()) {
|
||||
player.displayName(player.displayName());
|
||||
} else {
|
||||
final @Nullable Component nickname = this.createDefaultNickname(player);
|
||||
|
||||
if (nickname != null) {
|
||||
player.temporaryDisplayName(nickname);
|
||||
}
|
||||
}
|
||||
|
||||
// Don't show join messages when muted
|
||||
if (this.primaryConfig.hideMutedJoinLeaveQuit() && player.muted()) {
|
||||
event.joinMessage(null);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerQuit(final PlayerQuitEvent event) {
|
||||
// Early exit in case "hide muted join / leave messages when muted" is disabled
|
||||
if (!this.primaryConfig.hideMutedJoinLeaveQuit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
// Don't show quit messages when muted
|
||||
if (player.muted()) {
|
||||
event.quitMessage(null);
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable Component createDefaultNickname(final CarbonPlayer player) {
|
||||
|
||||
@@ -17,6 +17,8 @@ public class PrimaryConfig {
|
||||
private final Key defaultChannel = Key.key("carbon", "basic");
|
||||
@Comment("The service that will be used to store and load player information.")
|
||||
private final StorageType storageType = StorageType.JSON;
|
||||
@Comment("Should we hide join/quit and death messages of muted players?")
|
||||
private final boolean hideMutedJoinLeaveQuit = false;
|
||||
|
||||
public Locale defaultLocale() {
|
||||
return this.defaultLocale;
|
||||
@@ -30,6 +32,10 @@ public class PrimaryConfig {
|
||||
return this.storageType;
|
||||
}
|
||||
|
||||
public boolean hideMutedJoinLeaveQuit() {
|
||||
return this.hideMutedJoinLeaveQuit;
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
JSON,
|
||||
MYSQL,
|
||||
|
||||
Reference in new issue
Block a user