Permalink
Browse files

Implement PlayerChatEvent calling; fix ServerPingEvent handling

  • Loading branch information...
nickrobson committed Jul 26, 2017
1 parent de51fdf commit 18ac8010712f5a2a35e4c1b30613f1a876e6c4cf
@@ -219,6 +219,11 @@ private static void start(String[] args) throws Exception {
server.setup();
// -------------------------------------------------
// Load server icon ----------------- --------------
logger.log("Loading server icon");
StatusOutResponse.init();
// -------------------------------------------------
// JLine -------------------------------------------
LineReader reader = LineReaderBuilder.
builder().
@@ -278,4 +278,4 @@ public void updateMetadata() {
* for a client
*/
public abstract PacketOut getSpawnPacket();
}
}
@@ -49,14 +49,7 @@ public void read(ByteBuf buf, NetClient client) {
if (msg.startsWith("/")) {
player.runCommand(msg.replaceFirst("/", ""));
} else {
ChatComponent chat = ChatComponent.create()
.setTranslate("chat.type.text")
.addWith(ChatComponent.create()
.setText(player.getName())
.setClickEvent(ClickEvent.of(ClickAction.SUGGEST_COMMAND, "/tell " + player.getName() + " ")))
.addWith(msg);
TridentPlayer.getPlayers().values().forEach(p -> p.sendMessage(chat, ChatType.CHAT));
TridentServer.getInstance().getLogger().log(player.getName() + " [" + player.getUuid() + "]: " + msg);
player.chat(msg);
}
}
}
@@ -40,6 +40,7 @@ public void read(ByteBuf buf, NetClient client) {
boolean onGround = buf.readBoolean();
TridentPlayer player = client.getPlayer();
if (player == null) return;
player.setPosition(player.getPosition().setYaw(yaw).setPitch(pitch));
player.setOnGround(onGround);
}
@@ -18,10 +18,22 @@
import io.netty.buffer.ByteBuf;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import net.tridentsdk.event.server.ServerPingEvent;
import net.tridentsdk.server.TridentServer;
import net.tridentsdk.server.concurrent.PoolSpec;
import net.tridentsdk.server.concurrent.ServerThreadPool;
import net.tridentsdk.server.config.ServerConfig;
import net.tridentsdk.server.net.NetClient;
import net.tridentsdk.server.packet.PacketIn;
import javax.annotation.concurrent.Immutable;
import net.tridentsdk.server.player.TridentPlayer;
import net.tridentsdk.ui.chat.ChatComponent;
import static net.tridentsdk.server.packet.status.StatusOutResponse.MC_VERSION;
import static net.tridentsdk.server.packet.status.StatusOutResponse.PROTOCOL_VERSION;
/**
* This packet represents a status request that is sent
@@ -37,6 +49,27 @@ public StatusInRequest() {
@Override
public void read(ByteBuf buf, NetClient client) {
client.sendPacket(new StatusOutResponse((InetSocketAddress) client.getChannel().remoteAddress()));
ServerConfig cfg = TridentServer.cfg();
Collection<TridentPlayer> players = TridentPlayer.getPlayers().values();
int onlinePlayers = players.size();
ServerPingEvent.ServerPingResponseSample[] sample = new ServerPingEvent.ServerPingResponseSample[onlinePlayers];
int i = 0;
for (TridentPlayer player : players) {
sample[i++] = new ServerPingEvent.ServerPingResponseSample(player.getName(), player.getUuid());
}
ServerPingEvent.ServerPingResponse response = new ServerPingEvent.ServerPingResponse(
new ServerPingEvent.ServerPingResponseVersion(MC_VERSION, PROTOCOL_VERSION),
new ServerPingEvent.ServerPingResponsePlayers(onlinePlayers, cfg.maxPlayers(), sample),
ChatComponent.text(cfg.motd()),
StatusOutResponse.b64icon.get()
);
InetSocketAddress pinger = (InetSocketAddress) client.getChannel().remoteAddress();
ServerPingEvent event = new ServerPingEvent(pinger, response);
ServerThreadPool.forSpec(PoolSpec.PLUGINS)
.submit(() -> TridentServer.getInstance().getEventController()
.dispatch(event, e -> client.sendPacket(new StatusOutResponse(e)))
);
}
}
@@ -19,9 +19,11 @@
import io.netty.buffer.ByteBuf;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import net.tridentsdk.event.server.ServerPingEvent;
import net.tridentsdk.logger.Logger;
import net.tridentsdk.server.TridentServer;
import net.tridentsdk.server.concurrent.ServerThreadPool;
import net.tridentsdk.server.config.ServerConfig;
import net.tridentsdk.server.packet.PacketOut;
import net.tridentsdk.server.player.TridentPlayer;
@@ -60,10 +62,14 @@
*/
public static final int PROTOCOL_VERSION = 335;
private static final Logger logger;
private static final Path iconPath;
private static final AtomicReference<String> b64icon = new AtomicReference<>();
static {
static final AtomicReference<String> b64icon = new AtomicReference<>();
private static Logger logger;
private static Path iconPath;
private static final AtomicBoolean init = new AtomicBoolean();
public static void init() {
if (!init.compareAndSet(false, true))
return;
String userDir = System.getProperty("user.dir");
logger = Logger.get("Server Icon File Watcher");
iconPath = Paths.get("server-icon.png");
@@ -142,33 +148,15 @@ private static void loadIcon() throws IOException {
logger.log("Loaded server icon data: " + b64);
}
private final InetSocketAddress pinger;
private final ServerPingEvent event;
public StatusOutResponse(InetSocketAddress pinger) {
public StatusOutResponse(ServerPingEvent event) {
super(StatusOutResponse.class);
this.pinger = pinger;
this.event = event;
}
@Override
public void write(ByteBuf buf) {
ServerConfig cfg = TridentServer.cfg();
Collection<TridentPlayer> players = TridentPlayer.getPlayers().values();
int onlinePlayers = players.size();
ServerPingEvent.ServerPingResponseSample[] sample = new ServerPingEvent.ServerPingResponseSample[onlinePlayers];
int i = 0;
for (TridentPlayer player : players) {
sample[i++] = new ServerPingEvent.ServerPingResponseSample(player.getName(), player.getUuid());
}
ServerPingEvent.ServerPingResponse response = new ServerPingEvent.ServerPingResponse(
new ServerPingEvent.ServerPingResponseVersion(MC_VERSION, PROTOCOL_VERSION),
new ServerPingEvent.ServerPingResponsePlayers(onlinePlayers, cfg.maxPlayers(), sample),
ChatComponent.text(cfg.motd()),
b64icon.get()
);
ServerPingEvent event = new ServerPingEvent(pinger, response);
TridentServer.getInstance().getEventController().dispatch(event);
wstr(buf, event.getResponse().asJson().toString(Stringify.PLAIN));
}
}
@@ -20,6 +20,7 @@
import lombok.Setter;
import net.tridentsdk.command.CommandSourceType;
import net.tridentsdk.entity.living.Player;
import net.tridentsdk.event.player.PlayerChatEvent;
import net.tridentsdk.event.player.PlayerJoinEvent;
import net.tridentsdk.event.player.PlayerQuitEvent;
import net.tridentsdk.inventory.Inventory;
@@ -45,6 +46,8 @@
import net.tridentsdk.ui.chat.ChatColor;
import net.tridentsdk.ui.chat.ChatComponent;
import net.tridentsdk.ui.chat.ChatType;
import net.tridentsdk.ui.chat.ClickAction;
import net.tridentsdk.ui.chat.ClickEvent;
import net.tridentsdk.ui.chat.ClientChatMode;
import net.tridentsdk.ui.tablist.TabList;
import net.tridentsdk.ui.title.Title;
@@ -692,6 +695,26 @@ public void updateChunks() {
});
}
public void chat(String msg) {
ChatComponent chat = ChatComponent.create()
.setTranslate("chat.type.text")
.addWith(ChatComponent.create()
.setText(getName())
.setClickEvent(ClickEvent.of(ClickAction.SUGGEST_COMMAND, "/tell " + getName() + " ")))
.addWith(msg);
Collection<Player> recipients = new ArrayList<>(TridentPlayer.getPlayers().values());
PlayerChatEvent _event = new PlayerChatEvent(this, chat, recipients);
ServerThreadPool.forSpec(PoolSpec.PLUGINS).submit(() -> {
TridentServer.getInstance().getEventController().dispatch(_event, event -> {
if (!event.isCancelled()) {
ChatComponent chatComponent = event.getChatComponent();
event.getRecipients().forEach(p -> p.sendMessage(chatComponent, ChatType.CHAT));
}
TridentServer.getInstance().getLogger().log(getName() + " [" + getUuid() + "]: " + msg);
});
});
}
@Override
public void runCommand(String command) {
TridentServer.getInstance().getLogger().log(this.name + " issued server command: /" + command);

0 comments on commit 18ac801

Please sign in to comment.