Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some basic packet/hologram classes #25

Open
wants to merge 7 commits into
base: staging
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package io.github.zap.arenaapi.nms.v1_16_R3;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import io.github.zap.arenaapi.nms.common.ArenaNMSBridge;
import io.github.zap.arenaapi.nms.common.entity.EntityBridge;
import io.github.zap.arenaapi.nms.common.itemstack.ItemStackBridge;
import io.github.zap.arenaapi.nms.common.packet.PacketBridge;
import io.github.zap.arenaapi.nms.common.player.PlayerBridge;
import io.github.zap.arenaapi.nms.common.world.WorldBridge;
import io.github.zap.arenaapi.nms.v1_16_R3.entity.EntityBridge_v1_16_R3;
import io.github.zap.arenaapi.nms.v1_16_R3.itemstack.ItemStackBridge_v1_16_R3;
import io.github.zap.arenaapi.nms.v1_16_R3.packet.ProtocolLibPacketBridge_v1_16_R3;
import io.github.zap.arenaapi.nms.v1_16_R3.player.PlayerBridge_v1_16_R3;
import io.github.zap.arenaapi.nms.v1_16_R3.world.WorldBridge_v1_16_R3;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;

public class ArenaNMSBridge_v1_16_R3 implements ArenaNMSBridge {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, disgusting NMS class naming schemes, how i have not missed needing to use you

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalmente

public static final ArenaNMSBridge_v1_16_R3 INSTANCE = new ArenaNMSBridge_v1_16_R3();
private static final String VERSION = "v1_16_R3";

private ArenaNMSBridge_v1_16_R3() {}
private final PacketBridge packetBridge;

private ArenaNMSBridge_v1_16_R3() {
try {
Class.forName("com.comphenix.protocol.ProtocolLibrary");
}
catch (ClassNotFoundException error) {
throw new NotImplementedException("No minecraft PacketBridge implementation exists for version " +
VERSION + ", and ProtocolLibrary was not detected on the server!");
}

ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
this.packetBridge = new ProtocolLibPacketBridge_v1_16_R3(protocolManager);
}

@Override
public @NotNull String version() {
Expand All @@ -32,6 +50,11 @@ private ArenaNMSBridge_v1_16_R3() {}
return ItemStackBridge_v1_16_R3.INSTANCE;
}

@Override
public @NotNull PacketBridge packetBridge() {
return packetBridge;
}

@Override
public @NotNull PlayerBridge playerBridge() {
return PlayerBridge_v1_16_R3.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.github.zap.arenaapi.nms.v1_16_R3.packet;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.AdventureComponentConverter;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import io.github.zap.arenaapi.nms.common.packet.Packet;
import io.github.zap.arenaapi.nms.common.packet.PacketBridge;
import io.github.zap.arenaapi.nms.common.packet.ProtocolLibPacket;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

@SuppressWarnings("ClassCanBeRecord")
public class ProtocolLibPacketBridge_v1_16_R3 implements PacketBridge {

private final static byte INVISIBLE_BYTE_MASK = (byte) 0x20;

private final static byte MARKER_ARMOR_STAND_MASK = (byte) 0x10;

protected final ProtocolManager protocolManager;

public ProtocolLibPacketBridge_v1_16_R3(@NotNull ProtocolManager protocolManager) {
this.protocolManager = protocolManager;
}

@Override
public @NotNull Packet createSpawnLivingEntityPacket(int entityId, int typeId, @NotNull UUID uuid, double x,
double y, double z) {
PacketContainer packetContainer = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
packetContainer.getIntegers()
.write(0, entityId)
.write(1,typeId);
packetContainer.getUUIDs().write(0, uuid);
packetContainer.getDoubles()
.write(0, x)
.write(1, y)
.write(2, z);

return createProtocolLibPacket(packetContainer);
}

@Override
public @NotNull Packet createHologramLinePacket(int entityId, @NotNull Component line) {
PacketContainer packetContainer = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
packetContainer.getIntegers().write(0, entityId);

WrappedDataWatcher wrappedDataWatcher = new WrappedDataWatcher();

WrappedDataWatcher.Serializer byteSerializer = WrappedDataWatcher.Registry.get(Byte.class);
WrappedDataWatcher.Serializer optChatSerializer = WrappedDataWatcher.Registry
.getChatComponentSerializer(true);
WrappedDataWatcher.Serializer booleanSerializer = WrappedDataWatcher.Registry.get(Boolean.class);

WrappedDataWatcher.WrappedDataWatcherObject invisible
= new WrappedDataWatcher.WrappedDataWatcherObject(0, byteSerializer);
WrappedDataWatcher.WrappedDataWatcherObject customName
= new WrappedDataWatcher.WrappedDataWatcherObject(2, optChatSerializer);
WrappedDataWatcher.WrappedDataWatcherObject customNameVisible
= new WrappedDataWatcher.WrappedDataWatcherObject(3, booleanSerializer);
WrappedDataWatcher.WrappedDataWatcherObject marker
= new WrappedDataWatcher.WrappedDataWatcherObject(14, byteSerializer);

wrappedDataWatcher.setObject(invisible, INVISIBLE_BYTE_MASK);
wrappedDataWatcher.setObject(customName, Optional.of(AdventureComponentConverter.fromComponent(line)));
wrappedDataWatcher.setObject(customNameVisible, true);
wrappedDataWatcher.setObject(marker, MARKER_ARMOR_STAND_MASK);

packetContainer.getWatchableCollectionModifier().write(0, wrappedDataWatcher.getWatchableObjects());

return createProtocolLibPacket(packetContainer);
}

@Override
public @NotNull Packet createDestroyEntityPacket(int id) {
return createDestroyEntitiesPacket(new int[] { id });
}

@Override
public @NotNull Packet createDestroyEntitiesPacket(int[] ids) {
PacketContainer packetContainer = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
packetContainer.getIntegerArrays().write(0, ids);

return createProtocolLibPacket(packetContainer);
}

protected @NotNull Packet createProtocolLibPacket(@NotNull PacketContainer packetContainer) {
return new ProtocolLibPacket(protocolManager, packetContainer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.zap.arenaapi.nms.common.entity.EntityBridge;
import io.github.zap.arenaapi.nms.common.itemstack.ItemStackBridge;
import io.github.zap.arenaapi.nms.common.packet.PacketBridge;
import io.github.zap.arenaapi.nms.common.player.PlayerBridge;
import io.github.zap.arenaapi.nms.common.world.WorldBridge;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -37,10 +38,16 @@ private static String nmsVersion() {

/**
* Returns a bridge used to proxy methods relating to item stacks.
* @return A PlayerBridge instance
* @return A {@link ItemStackBridge} instance
*/
@NotNull ItemStackBridge itemStackBridge();

/**
* Returns a bridge used to interact with packets
* @return A {@link PacketBridge} instance
*/
@NotNull PacketBridge packetBridge();

/**
* Returns a bridge used to proxy methods relating to players.
* @return A PlayerBridge instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.zap.arenaapi.nms.common.packet;

import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
* A packet formed from many other packets
*/
@SuppressWarnings("ClassCanBeRecord")
public class MultiPacket implements Packet {

private final Packet[] packets;

public MultiPacket(@NotNull Packet... packets) {
this.packets = packets;
}

@Override
public void sendToPlayer(@NotNull Plugin plugin, @NotNull Player player) {
for (Packet packet : packets) {
packet.sendToPlayer(plugin, player);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.zap.arenaapi.nms.common.packet;

import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
* A minecraft packet used to circumvent limitations of the Bukkit API
*/
public interface Packet {

/**
* Sends the packet to a {@link Player}.
* @param plugin The {@link Plugin} from which the packet is being sent
* @param player The {@link Player} to send the packet to
*/
void sendToPlayer(@NotNull Plugin plugin, @NotNull Player player);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.zap.arenaapi.nms.common.packet;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

/**
* A bridge used for creating and interacting with packets
*/
public interface PacketBridge {

@NotNull Packet createSpawnLivingEntityPacket(int entityId, int typeId, @NotNull UUID uuid, double x, double y,
double z);

@NotNull Packet createHologramLinePacket(int entityId, @NotNull Component line);

@NotNull Packet createDestroyEntityPacket(int id);

@NotNull Packet createDestroyEntitiesPacket(int[] ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.zap.arenaapi.nms.common.packet;

import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;

/**
* A packet using {@link PacketContainer}s
*/
@SuppressWarnings("ClassCanBeRecord")
public class ProtocolLibPacket implements Packet {

private final ProtocolManager protocolManager;

private final PacketContainer handle;

public ProtocolLibPacket(@NotNull ProtocolManager protocolManager, @NotNull PacketContainer handle) {
this.protocolManager = protocolManager;
this.handle = handle;
}

@Override
public void sendToPlayer(@NotNull Plugin plugin, @NotNull Player player) {
try {
protocolManager.sendServerPacket(player, handle);
} catch (InvocationTargetException e) {
plugin.getLogger().log(Level.WARNING, "Failed to send a packet to player with UUID "
+ player.getUniqueId() + "!", e);
}
}

}
Loading