Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 35 additions & 67 deletions src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -42,67 +40,41 @@ void onAsyncChatEventProcess(final AsyncChatEvent event) {

@EventHandler(priority = EventPriority.MONITOR)
void onAsyncChatEventRenderer(final AsyncChatEvent event) {
event.renderer(CHAT_RENDERER);
event.renderer(ChatRenderer.viewerUnaware(CHAT_RENDERER));
}

public static class PlayerChatRenderer implements ChatRenderer {
private static final TextReplacementConfig URL_REPLACEMENT_CONFIG =
TextReplacementConfig
.builder()
.match(Pattern
.compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
+ "\\.[a-zA-Z0-9]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))"))
.replacement((b, c) -> {
if (c == null) {
return null;
}

if (b.groupCount() < 1) {
return null;
}

final String content = b.group(1);
final String url;

/*
Minecraft doesn't accept "www.google.com" as a URL
in click events
*/
if (content.contains("://")) {
url = content;
} else {
url = "https://" + content;
}

return Component.text(content, NamedTextColor.BLUE)
.decorate(TextDecoration.UNDERLINED)
.clickEvent(ClickEvent.openUrl(url));
})
.build();

private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER =
LegacyComponentSerializer
.legacyAmpersand();

private Component renderVanilla(@Nonnull Component displayName,
@Nonnull Component component) {
return Component.translatable(
"chat.type.text",
displayName,
component.replaceText(URL_REPLACEMENT_CONFIG)
);
public static class PlayerChatRenderer implements ChatRenderer.ViewerUnaware {
private static final Pattern URL_PATTERN = Pattern
.compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
+ "\\.[a-zA-Z0-9]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))");
private static final Style URL_STYLE = Style.style(NamedTextColor.BLUE,
TextDecoration.UNDERLINED);

// Match vanilla by only rendering section sign legacy colors in vanilla chat
private static final LegacyComponentSerializer VANILLA_CHAT_SERIALIZER =
LegacyComponentSerializer.builder()
.character(LegacyComponentSerializer.SECTION_CHAR)
.extractUrls(URL_PATTERN, URL_STYLE)
.hexColors()
.build();

private static final LegacyComponentSerializer CHAT_SERIALIZER =
LegacyComponentSerializer.builder()
.character(LegacyComponentSerializer.AMPERSAND_CHAR)
.extractUrls(URL_PATTERN, URL_STYLE)
.hexColors()
.build();

private Component renderVanilla(final @Nonnull Component displayName,
final @Nonnull Component component) {
return Component.translatable("chat.type.text", displayName, component);
}

@Override
public @Nonnull Component render(@Nonnull Player player,
@Nonnull Component displayName,
@Nonnull Component component,
@Nonnull Audience audience) {
if (PlayerPrefix.isUsingVanillaFormat(player)) {
return renderVanilla(displayName, component);
}

Component newComponent = Component.empty();
@Nonnull Component component) {
final String message = ((TextComponent) component).content();
final Component prefix;
Component prefix1;

Expand All @@ -114,20 +86,16 @@ private Component renderVanilla(@Nonnull Component displayName,
}

prefix = prefix1;
final String message = ((TextComponent) component).content();
if (prefix == null) {
return renderVanilla(displayName, VANILLA_CHAT_SERIALIZER.deserialize(message));
}

newComponent = newComponent
return Component.empty()
.append(prefix)
.append(displayName)
.append(Component.text(":"))
.append(Component.space());

final Component messageWithColorCodes = LEGACY_COMPONENT_SERIALIZER
.deserialize(message);
final Component completedMessage = messageWithColorCodes
.replaceText(URL_REPLACEMENT_CONFIG);

return newComponent.append(completedMessage);
.append(Component.space())
.append(CHAT_SERIALIZER.deserialize(message));
}
}
}
13 changes: 3 additions & 10 deletions src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void onFoodLevelChange(final FoodLevelChangeEvent event) {
final AttributeInstance attribute = player.getAttribute(Attribute.MAX_HEALTH);
if (attribute == null) return;
if (attribute.getValue() <= 0) {
Utility.resetAttribute(attribute);
Utility.resetAttribute(player, Attribute.MAX_HEALTH);
player.setHealth(20);
}
}
Expand Down Expand Up @@ -97,11 +97,7 @@ void onPlayerDeath(final PlayerDeathEvent event) {
xp.setExperience(event.getDroppedExp());
}

final AttributeInstance attribute = player.getAttribute(Attribute.MAX_HEALTH);
if (attribute != null) {
Utility.resetAttribute(attribute);
}

Utility.resetAttribute(player, Attribute.MAX_HEALTH);
player.setHealth(20);

if (player.getRespawnLocation() != null) {
Expand All @@ -111,10 +107,7 @@ void onPlayerDeath(final PlayerDeathEvent event) {
player.teleportAsync(world.getSpawnLocation());
}
} catch (Exception exception) {
final AttributeInstance attribute = player.getAttribute(Attribute.MAX_HEALTH);
if (attribute != null) {
Utility.resetAttribute(attribute);
}
Utility.resetAttribute(player, Attribute.MAX_HEALTH);
player.setHealth(20);
}

Expand Down
36 changes: 17 additions & 19 deletions src/main/java/pw/kaboom/extras/modules/player/PlayerPrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.Nullable;
import pw.kaboom.extras.Main;

import java.io.File;
Expand Down Expand Up @@ -74,21 +75,14 @@ public static Component setPrefix(Player player, String legacyPrefix) throws IOE
return prefix;
}

public static boolean isUsingVanillaFormat(Player player) {
public static @Nullable Component getPrefix(Player player) throws IOException {
final UUID playerUUID = player.getUniqueId();
final String stringifiedUUID = playerUUID.toString();
final String legacyPrefix = PREFIX_CONFIG.getString(stringifiedUUID);

return legacyPrefix != null && legacyPrefix.equals("%");
}

public static Component getPrefix(Player player) throws IOException {
final UUID playerUUID = player.getUniqueId();
final String stringifiedUUID = playerUUID.toString();
final String legacyPrefix = PREFIX_CONFIG.getString(stringifiedUUID);

if (legacyPrefix == null) {
return player.isOp() ? OP_TAG : DE_OP_TAG;
return getDefaultPrefix(player);
} else if (legacyPrefix.equals("%")) {
return null;
}

return LegacyComponentSerializer.legacyAmpersand()
Expand All @@ -101,14 +95,18 @@ public static Component getDefaultPrefix(Player player) {
}

private static void onUpdate(Player player) throws IOException {
final Component component = Component.empty()
.append(
isUsingVanillaFormat(player) ?
Component.empty() : getPrefix(player)
)
.append(player.displayName());

player.playerListName(component);
final Component prefix = getPrefix(player);

final Component playerListName;
if (prefix != null) {
playerListName = Component.empty()
.append(prefix)
.append(player.displayName());
} else {
playerListName = player.displayName();
}

player.playerListName(playerListName);
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void onPlayerChangedWorld(final PlayerChangedWorldEvent event) {
final AttributeInstance attribute = player.getAttribute(Attribute.MAX_HEALTH);
if (attribute == null) return;
if (attribute.getValue() <= 0) {
Utility.resetAttribute(attribute);
Utility.resetAttribute(player, Attribute.MAX_HEALTH);
player.setHealth(20);
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/pw/kaboom/extras/util/Utility.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package pw.kaboom.extras.util;

import org.bukkit.Bukkit;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;
Expand All @@ -19,12 +22,22 @@ public final class Utility {
.orElse(null);
}

public static void resetAttribute(final AttributeInstance attribute) {
for (final AttributeModifier modifier: attribute.getModifiers()) {
attribute.removeModifier(modifier);
public static <T extends Entity & Attributable> void resetAttribute (final T entity,
final Attribute attrib) {
final AttributeInstance instance = entity.getAttribute(attrib);
if (instance == null) return;

for (final AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}

attribute.setBaseValue(attribute.getDefaultValue());
final AttributeInstance defaultInstance = entity.getType().getDefaultAttributes()
.getAttribute(instance.getAttribute());
if (defaultInstance != null) {
instance.setBaseValue(defaultInstance.getBaseValue());
} else {
instance.setBaseValue(instance.getDefaultValue());
}
}

// TODO: Support hex color codes, too (they aren't supported in Spigot either)
Expand Down