Skip to content

Commit

Permalink
Merge branch 'chore/1-20-6' into feat/sse-events
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed May 1, 2024
2 parents 06c8a68 + 024e464 commit 803c5a2
Show file tree
Hide file tree
Showing 59 changed files with 870 additions and 1,195 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/setup-java@v2
with:
distribution: temurin
java-version: 17
java-version: 21
- uses: actions/setup-node@v3
with:
node-version: 16
Expand Down
13 changes: 13 additions & 0 deletions .tokeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# do not count these lines
.github/
.editorconfig
.gitattributes
.gitignore
.tokeignore
LICENSE
gradle/
*.gradle*
*.json
*.accesswidener
*.yml
*.config.js
21 changes: 14 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,28 @@ allprojects {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

repositories {
mavenCentral()
mavenLocal()
maven {
url 'https://repo.granny.dev/snapshots'
}
maven {
url = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
mavenContent { snapshotsOnly() }
}
maven { url = 'https://jitpack.io' }
}

dependencies {
implementation "cloud.commandframework:cloud-core:$cloudVersion"
implementation "cloud.commandframework:cloud-brigadier:$cloudVersion"
implementation "cloud.commandframework:cloud-paper:$cloudVersion"
implementation("cloud.commandframework:cloud-minecraft-extras:$cloudVersion") {
implementation "org.incendo:cloud-core:$cloudVersion"
implementation "org.incendo:cloud-brigadier:$cloudVersion"
implementation "org.incendo:cloud-paper:$cloudVersion"
implementation "org.incendo:cloud-processors-confirmation:1.0.0-beta.1"
implementation("org.incendo:cloud-minecraft-extras:$cloudVersion") {
exclude group: 'net.kyori', module: '*'
}

Expand Down Expand Up @@ -120,7 +127,7 @@ allprojects {

compileJava {
options.encoding = 'UTF-8'
options.release.set(17)
options.release.set(21)
}

processResources {
Expand Down
114 changes: 64 additions & 50 deletions bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@
*/
package net.pl3x.map.bukkit;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.pl3x.map.bukkit.network.ClientboundMapPayload;
import net.pl3x.map.bukkit.network.ClientboundServerPayload;
import net.pl3x.map.bukkit.network.ServerboundMapPayload;
import net.pl3x.map.bukkit.network.ServerboundServerPayload;
import net.pl3x.map.core.configuration.Config;
import net.pl3x.map.core.network.Constants;
import net.pl3x.map.core.network.Network;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R3.map.CraftMapRenderer;
import org.bukkit.entity.Player;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.jetbrains.annotations.NotNull;

public class BukkitNetwork extends Network {
private final Pl3xMapBukkit plugin;
Expand All @@ -42,69 +48,77 @@ public BukkitNetwork(Pl3xMapBukkit plugin) {
}

public void register() {
Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, Network.CHANNEL);
Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, Network.CHANNEL,
Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, ClientboundServerPayload.TYPE.id().toString());
Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, ClientboundMapPayload.TYPE.id().toString());
Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundServerPayload.TYPE.id().toString(),
(channel, player, bytes) -> {
ByteArrayDataInput in = in(bytes);
int protocol = in.readInt();
if (protocol != Constants.PROTOCOL) {
ClientboundServerPayload payload = new ClientboundServerPayload(Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, Config.WEB_ADDRESS);
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer());
ClientboundServerPayload.STREAM_CODEC.encode(friendlyByteBuf, payload);
sendCustomPayloadPacket(player, payload, friendlyByteBuf);
}
);
Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundMapPayload.TYPE.id().toString(),
(channel, player, bytes) -> {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(bytes));
ServerboundMapPayload payload = ServerboundMapPayload.STREAM_CODEC.decode(byteBuf);

MapView map = Bukkit.getMap(payload.mapId());
if (map == null) {
ClientboundMapPayload customPacketPayload = new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_MAP, payload.mapId());
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer(bytes.length));
ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload);
sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf);
return;
}
int action = in.readInt();
switch (action) {
case Constants.SERVER_DATA -> sendServerData(player);
case Constants.MAP_DATA -> sendMapData(player, in.readInt());

World world = map.getWorld();
if (world == null) {
ClientboundMapPayload customPacketPayload = new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_WORLD, payload.mapId());
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer());
ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload);
sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf);
return;
}

ClientboundMapPayload customPacketPayload = new ClientboundMapPayload(
Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, payload.mapId(),
getScale(map), map.getCenterX(), map.getCenterZ(), world.getName()
);
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer());
ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload);
sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf);
}
);
}

@NotNull
private void sendCustomPayloadPacket(Player player, CustomPacketPayload customPacketPayload, FriendlyByteBuf friendlyByteBuf) {
byte[] byteArray = new byte[friendlyByteBuf.readableBytes()];
friendlyByteBuf.readBytes(byteArray);
player.sendPluginMessage(this.plugin, customPacketPayload.type().id().toString(), byteArray);
}

public void unregister() {
Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, Network.CHANNEL);
Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, Network.CHANNEL);
Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, ClientboundServerPayload.TYPE.id().toString());
Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, ClientboundMapPayload.TYPE.id().toString());
Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, ServerboundServerPayload.TYPE.id().toString());
Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, ServerboundMapPayload.TYPE.id().toString());
}

@Override
protected <T> void sendServerData(T player) {

}

@Override
protected <T> void sendMapData(T player, int id) {
ByteArrayDataOutput out = out();

out.writeInt(Constants.PROTOCOL);
out.writeInt(Constants.MAP_DATA);
out.writeInt(Constants.RESPONSE_SUCCESS);

MapView map = Bukkit.getMap(id);
if (map == null) {
out.writeInt(Constants.ERROR_NO_SUCH_MAP);
out.writeInt(id);
return;
}

World world = map.getWorld();
if (world == null) {
out.writeInt(Constants.ERROR_NO_SUCH_WORLD);
out.writeInt(id);
return;
}

for (MapRenderer renderer : map.getRenderers()) {
if (!renderer.getClass().getName().equals(CraftMapRenderer.class.getName())) {
out.writeInt(Constants.ERROR_NOT_VANILLA_MAP);
out.writeInt(id);
return;
}
}

out.writeInt(id);
out.writeByte(getScale(map));
out.writeInt(map.getCenterX());
out.writeInt(map.getCenterZ());
out.writeUTF(world.getName());

send(player, out);

}

@Override
protected <T> void send(T player, ByteArrayDataOutput out) {
((Player) player).sendPluginMessage(this.plugin, Network.CHANNEL, out.toByteArray());

}

@SuppressWarnings("deprecation")
Expand Down
4 changes: 2 additions & 2 deletions bukkit/src/main/java/net/pl3x/map/bukkit/BukkitPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
2 changes: 1 addition & 1 deletion bukkit/src/main/java/net/pl3x/map/bukkit/BukkitWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public BukkitWorld(@NotNull ServerLevel level, @NotNull String name) {
super(
name,
level.getSeed(),
Point.of(level.getLevelData().getXSpawn(), level.getLevelData().getZSpawn()),
Point.of(level.getLevelData().getSpawnPos().getX(), level.getLevelData().getSpawnPos().getZ()),
Type.get(level.dimension().location().toString()),
level.convertable.getDimensionPath(level.dimension()).resolve("region")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import net.pl3x.map.core.player.PlayerListener;
import net.pl3x.map.core.player.PlayerRegistry;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand Down
2 changes: 1 addition & 1 deletion bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import net.pl3x.map.core.registry.BlockRegistry;
import net.pl3x.map.core.world.World;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@
*/
package net.pl3x.map.bukkit.command;

import cloud.commandframework.Command;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager;
import net.pl3x.map.core.command.CommandHandler;
import net.pl3x.map.core.command.Sender;
import net.pl3x.map.core.command.parser.PlatformParsers;
import org.bukkit.plugin.Plugin;
import org.incendo.cloud.Command;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.PaperCommandManager;
import org.jetbrains.annotations.NotNull;

public class BukkitCommandManager implements CommandHandler {
private final PaperCommandManager<@NotNull Sender> manager;
private final Command.Builder<@NotNull Sender> root;

public BukkitCommandManager(@NotNull Plugin plugin) throws Exception {
this.manager = new PaperCommandManager<>(plugin, CommandExecutionCoordinator.simpleCoordinator(), BukkitSender::create, Sender::getSender);
this.manager = new PaperCommandManager<Sender>(plugin,
ExecutionCoordinator.simpleCoordinator(),
SenderMapper.create(BukkitSender::create, Sender::getSender));

if (getManager().hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
getManager().registerBrigadier();
Expand All @@ -64,6 +68,11 @@ public BukkitCommandManager(@NotNull Plugin plugin) throws Exception {
return this.manager;
}

@Override
public @NotNull PlatformParsers getPlatformParsers() {
return new BukkitParsers();
}

@Override
public Command.@NotNull Builder<@NotNull Sender> getRoot() {
return this.root;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.pl3x.map.bukkit.command;

import net.minecraft.world.phys.Vec3;
import net.pl3x.map.bukkit.BukkitPlayer;
import net.pl3x.map.core.Pl3xMap;
import net.pl3x.map.core.command.Sender;
import net.pl3x.map.core.command.parser.PlatformParsers;
import net.pl3x.map.core.markers.Point;
import net.pl3x.map.core.player.Player;
import org.incendo.cloud.bukkit.data.SinglePlayerSelector;
import org.incendo.cloud.bukkit.parser.location.Location2D;
import org.incendo.cloud.bukkit.parser.location.Location2DParser;
import org.incendo.cloud.bukkit.parser.selector.SinglePlayerSelectorParser;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.parser.ParserDescriptor;

public class BukkitParsers implements PlatformParsers {
@Override
public ParserDescriptor<Sender, ?> columnPosParser() {
return Location2DParser.location2DParser();
}

@Override
public Point resolvePointFromColumnPos(String name, CommandContext<Sender> context) {
Location2D location2D = context.<Location2D>getOrDefault(name, null);
if (location2D == null) {
return Point.ZERO;
}
return Point.of(location2D.blockX(), location2D.blockZ());
}

@Override
public ParserDescriptor<Sender, ?> playerSelectorParser() {
return SinglePlayerSelectorParser.singlePlayerSelectorParser();
}

@Override
public Player resolvePlayerFromPlayerSelector(String name, CommandContext<Sender> context) {
Sender sender = context.sender();
SinglePlayerSelector playerSelector = context.getOrDefault(name, null);
if (playerSelector == null) {
if (sender instanceof Sender.Player<?> senderPlayer) {
Player player = Pl3xMap.api().getPlayerRegistry().get(senderPlayer.getUUID());
if (player != null) {
return player;
}
}
return null;
}
return new BukkitPlayer(playerSelector.single());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.pl3x.map.bukkit.network;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.pl3x.map.core.network.Constants;

public record ClientboundMapPayload(int protocol, int response, int mapId, byte scale, int centerX, int centerZ, String worldName) implements CustomPacketPayload {
public static final StreamCodec<FriendlyByteBuf, ClientboundMapPayload> STREAM_CODEC = CustomPacketPayload.codec(ClientboundMapPayload::write, ClientboundMapPayload::new);
public static final Type<ClientboundMapPayload> TYPE = new Type<>(new ResourceLocation(Constants.MODID, "client_map_data"));

public ClientboundMapPayload(int protocol, int response, int mapId) {
this(protocol, response, mapId, (byte) 0, 0, 0, null);
}

public ClientboundMapPayload(FriendlyByteBuf friendlyByteBuf) {
this(friendlyByteBuf.readInt(), friendlyByteBuf.readInt(), friendlyByteBuf.readInt(), friendlyByteBuf.readByte(), friendlyByteBuf.readInt(), friendlyByteBuf.readInt(), friendlyByteBuf.readUtf());
}

private void write(FriendlyByteBuf friendlyByteBuf) {
friendlyByteBuf.writeInt(protocol);
friendlyByteBuf.writeInt(response);
friendlyByteBuf.writeInt(mapId);
friendlyByteBuf.writeByte(scale);
friendlyByteBuf.writeInt(centerX);
friendlyByteBuf.writeInt(centerZ);
friendlyByteBuf.writeUtf(worldName);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
Loading

0 comments on commit 803c5a2

Please sign in to comment.