Skip to content

Commit

Permalink
Merge pull request #136 from Andre601/feature/improve-component-parser
Browse files Browse the repository at this point in the history
Improve component parser
  • Loading branch information
Andre601 authored Mar 3, 2022
2 parents 2910b13 + a5613db commit 66898ff
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.andre601.oneversionremake.core.files.ConfigHandler;
import com.andre601.oneversionremake.core.interfaces.PluginCore;
import com.andre601.oneversionremake.core.interfaces.ProxyLogger;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.plugin.Plugin;
import org.bstats.bungeecord.Metrics;
Expand All @@ -48,7 +47,6 @@ public class BungeeCore extends Plugin implements PluginCore{

@Override
public void onEnable(){
BungeeAudiences.create(this);
this.core = new OneVersionRemake(this);
}

Expand Down Expand Up @@ -142,4 +140,8 @@ public ServerPing.PlayerInfo[] getPlayers(List<String> lines, List<Integer> serv
return core.getPlayers(ServerPing.PlayerInfo.class, lines, serverProtocols, userProtocol, majorOnly)
.toArray(new ServerPing.PlayerInfo[0]);
}

public OneVersionRemake getCore(){
return core;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
package com.andre601.oneversionremake.bungeecord.commands;

import com.andre601.oneversionremake.core.CommandPermissions;
import com.andre601.oneversionremake.core.OneVersionRemake;
import com.andre601.oneversionremake.core.interfaces.CmdSender;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.CommandSender;

public class BungeeSender implements CmdSender{

private final OneVersionRemake core;
private final CommandSender sender;
private final BungeeAudiences bungeeAudiences;

public BungeeSender(CommandSender sender){
public BungeeSender(OneVersionRemake core, CommandSender sender, BungeeAudiences bungeeAudiences){
this.core = core;
this.sender = sender;
this.bungeeAudiences = bungeeAudiences;
}

@Override
Expand All @@ -50,8 +54,8 @@ public void sendMsg(String msg, Object... args){

@Override
public void sendMsg(NamedTextColor color, String msg, Object... args){
sender.sendMessage(BungeeComponentSerializer.get().serialize(
Component.text(String.format(msg, args)).color(color)
));
bungeeAudiences.sender(sender).sendMessage(
core.getComponentParser().toComponent(String.format(msg, args)).color(color)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@

import com.andre601.oneversionremake.bungeecord.BungeeCore;
import com.andre601.oneversionremake.core.CommandPermissions;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;

public class CmdOneVersionRemake extends Command{

private final BungeeCore plugin;
private final BungeeAudiences bungeeAudiences;

public CmdOneVersionRemake(BungeeCore plugin){
super("oneversionremake", CommandPermissions.ADMIN, "ovr");
this.plugin = plugin;
this.bungeeAudiences = BungeeAudiences.create(plugin);
}

@Override
public void execute(CommandSender commandSender, String[] args){
BungeeSender sender = new BungeeSender(commandSender);
BungeeSender sender = new BungeeSender(plugin.getCore(), commandSender, bungeeAudiences);

plugin.getCommandHandler().handle(sender, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onLogin(PreLoginEvent event){
kickMessage = Collections.singletonList("<red>This Server is running MC {version}! Please change your client version.");

event.setCancelReason(BungeeComponentSerializer.get().serialize(
plugin.getComponentParser().toTextComponent(kickMessage, serverProtocols, userProtocol, majorOnly)
plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly)
));
event.setCancelled(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void onProxyPing(ProxyPingEvent event){
motd = motd.subList(0, 1);

TextComponent component = new TextComponent(BungeeComponentSerializer.get().serialize(
plugin.getComponentParser().toTextComponent(motd, serverProtocols, userProtocol, majorOnly)
plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly)
));

ping.setDescriptionComponent(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public Parser(OneVersionRemake core){
this.core = core;
}

public Component toTextComponent(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return MiniMessage.get().parse(parse(String.join("\n", lines), serverProtocols, userProtocol, majorOnly));
public Component toComponent(List<String> lines, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return toComponent(parse(String.join("\n", lines), serverProtocols, userProtocol, majorOnly));
}

public Component toComponent(String text){
return MiniMessage.miniMessage().deserialize(text);
}

public String toString(String text, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
Component component = MiniMessage.get().parse(parse(text, serverProtocols, userProtocol, majorOnly));
Component component = MiniMessage.miniMessage()
.deserialize(parse(text, serverProtocols, userProtocol, majorOnly));
return LegacyComponentSerializer.legacySection().serialize(component);
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<plugin.version>3.9.0</plugin.version>
<plugin.version>3.10.0</plugin.version>
<plugin.description>Only allow specific client versions on your Network.</plugin.description>

<maven.compiler.target>11</maven.compiler.target>
Expand Down
12 changes: 3 additions & 9 deletions velocity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
<repositories>
<repository>
<id>velocity</id>
<url>https://nexus.velocitypowered.com/repository/maven-public</url>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>

Expand All @@ -54,15 +54,9 @@
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.1.0</version>
<version>3.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.10.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-velocity</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void onPreLogin(PreLoginEvent event){
kickMessage = Collections.singletonList("&cThis Server is running MC {version}! Please change your client version.");

PreLoginEvent.PreLoginComponentResult result = PreLoginEvent.PreLoginComponentResult
.denied(plugin.getComponentParser().toTextComponent(kickMessage, serverProtocols, userProtocol, majorOnly));
.denied(plugin.getComponentParser().toComponent(kickMessage, serverProtocols, userProtocol, majorOnly));

event.setResult(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void onProxyPing(ProxyPingEvent event){
if(motd.size() > 2)
motd = motd.subList(0, 1);

builder.description(plugin.getComponentParser().toTextComponent(motd, serverProtocols, userProtocol, majorOnly));
builder.description(plugin.getComponentParser().toComponent(motd, serverProtocols, userProtocol, majorOnly));
}

event.setPing(builder.build());
Expand Down

0 comments on commit 66898ff

Please sign in to comment.