diff --git a/gradle.properties b/gradle.properties index 9b4170ca..3ac6130c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -38,4 +38,6 @@ deploy.imanityLibraries = true # Minecraft adventure.version = 4.9.3 -netty.version = 4.1.69.Final \ No newline at end of file +netty.version = 4.1.69.Final +packetevents.v1.version = v1.8-pre-19 +packetevents.v2.version = 2.0-SNAPSHOT \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/.gitignore b/io.fairyproject.modules/platform.mc/module.protocol/.gitignore new file mode 100644 index 00000000..f3d6549d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/.gitignore b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/.gitignore new file mode 100644 index 00000000..f3d6549d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/build.gradle b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/build.gradle new file mode 100644 index 00000000..9198e894 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/build.gradle @@ -0,0 +1,17 @@ +version = "0.0.1b1" + +dependencies { + api 'com.github.retrooper.packetevents:spigot:' + findProperty("packetevents.v2.version") + api 'com.github.artemisac.artemis-packet-api:spigot:2.0.0-beta-2' + api 'com.github.retrooper:packetevents:' + findProperty("packetevents.v1.version") + + compileOnly "io.netty:netty-all:" + findProperty("netty.version") + compileOnly project.spigot() +} + +module { + platform("mc") + + depend("platform.mc:module.protocol:mc-protocol") + depend("module.config") +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketBuilder.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketBuilder.java new file mode 100644 index 00000000..6c298c77 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketBuilder.java @@ -0,0 +1,4 @@ +package io.fairyproject.bukkit.protocol; + +public interface PacketBuilder extends PacketFactoryCreator, PacketFactoryWrapper { +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryCreator.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryCreator.java new file mode 100644 index 00000000..ff6fe853 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryCreator.java @@ -0,0 +1,7 @@ +package io.fairyproject.bukkit.protocol; + +public interface PacketFactoryCreator { + default W createEmpty() { + throw new IllegalStateException("Not Implemented."); + } +} \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryWrapper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryWrapper.java new file mode 100644 index 00000000..5ab1b3a1 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketFactoryWrapper.java @@ -0,0 +1,7 @@ +package io.fairyproject.bukkit.protocol; + +import io.fairyproject.mc.MCPlayer; + +public interface PacketFactoryWrapper { + T wrap(final W typeObj, final MCPlayer player); +} \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketManager.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketManager.java new file mode 100644 index 00000000..f9fee9c9 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketManager.java @@ -0,0 +1,62 @@ +package io.fairyproject.bukkit.protocol; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventsV2Provider; +import io.fairyproject.bukkit.protocol.provider.AbstractPacketProviderFactory; +import io.fairyproject.container.PreInitialize; +import io.fairyproject.container.Service; +import io.fairyproject.library.Library; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.InternalPacketListener; +import io.fairyproject.mc.protocol.PacketProvider; +import lombok.Getter; + +@Service +public class PacketManager { + @Getter + private PacketProvider provider; + + @PreInitialize + public void load() { + Library.builder() + .groupId("com.github.retrooper.packetevents") + .artifactId("spigot") + .version() + provider = new AbstractPacketProviderFactory() { + @Override + public PacketProvider build() { + this.verify(); + + return new PacketEventsV2Provider(packetListener, lowLevelPacketListener); + }} + .setPacketListener(new InternalPacketListener() { + @Override + public boolean onPacket(MCPlayer data, Packet packet) { + // TODO: Handle logic + + return false; + } + }) + .setLowLevelPacketListener(new InternalBufferListener() { + @Override + public boolean handle(MCPlayer data, FairyByteBuf byteBuf) { + // TODO: Handle logic + + return false; + } + }) + .build(); + + provider.load(); + } + + public void init() { + provider.init(); + } + + public void end() { + provider.quit(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketMap.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketMap.java new file mode 100644 index 00000000..ff2d31cd --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/PacketMap.java @@ -0,0 +1,11 @@ +package io.fairyproject.bukkit.protocol; + +import io.fairyproject.bukkit.protocol.packet.PacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; + +public interface PacketMap { + , K extends W> Packet wrap(MCPlayer player, I id, W obj); + + void inject(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/ProtocolConfiguration.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/ProtocolConfiguration.java new file mode 100644 index 00000000..8b72ffc6 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/ProtocolConfiguration.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol; + +import io.fairyproject.Fairy; +import io.fairyproject.config.annotation.Comment; +import io.fairyproject.config.yaml.YamlConfiguration; +import lombok.Getter; + +import java.io.File; +import java.nio.file.Path; + +@Getter +public class ProtocolConfiguration extends YamlConfiguration { + public ProtocolConfiguration() { + super(new File(Fairy.getPlatform().getDataFolder(), "modules/protocol/yml").toPath()); + } + + @Comment({ + "PacketEvents v1 configuration" + }) + + + +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/AbstractPacketMapping.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/AbstractPacketMapping.java new file mode 100644 index 00000000..6cc4f084 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/AbstractPacketMapping.java @@ -0,0 +1,75 @@ +package io.fairyproject.bukkit.protocol.packet; + +import io.fairyproject.bukkit.protocol.PacketBuilder; +import io.fairyproject.bukkit.protocol.PacketFactoryCreator; +import io.fairyproject.bukkit.protocol.PacketFactoryWrapper; +import io.fairyproject.bukkit.protocol.PacketMap; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; + +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractPacketMapping implements PacketMap { + private final Map> idToBuilders = new HashMap<>(); + private final Map>, PacketBuilder> classToBuilders = new HashMap<>(); + + protected , K extends W> PacketFactory create(final I id, final Class clazz) { + return new PacketFactory(id, clazz); + } + + protected , K extends W> void create(final I id, Class clazz, PacketBuilder generator) { + new PacketFactory(id, clazz).create(generator); + } + + @Override + public , K extends W> Packet wrap(MCPlayer player, I id, W obj) { + final PacketBuilder factory = (PacketBuilder) idToBuilders.get(id); + return factory.wrap((K) obj, player); + } + + class PacketFactory, K extends W> { + private final I id; + private final Class wrapperType; + + public PacketFactory(I id, Class wrapperType) { + this.id = id; + this.wrapperType = wrapperType; + } + + private PacketFactoryCreator creator; + private PacketFactoryWrapper wrapper; + + public PacketFactory creator(PacketFactoryCreator creator) { + this.creator = creator; + return this; + } + + public PacketFactory wrapper(PacketFactoryWrapper wrapper) { + this.wrapper = wrapper; + return this; + } + + public void create() { + final PacketBuilder builder = new PacketBuilder() { + @Override + public T createEmpty() { + return creator.createEmpty(); + } + + @Override + public T wrap(K typeObj, MCPlayer player) { + return wrapper.wrap(typeObj, player); + } + }; + + create(builder); + } + + public void create(final PacketBuilder builder) { + idToBuilders.put(id, builder); + classToBuilders.put(wrapperType, builder); + } + } + +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/PacketWrapper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/PacketWrapper.java new file mode 100644 index 00000000..2abba131 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/PacketWrapper.java @@ -0,0 +1,20 @@ +package io.fairyproject.bukkit.protocol.packet; + +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; + +public abstract class PacketWrapper implements Packet { + protected T wrapper; + protected MCPlayer player; + + public PacketWrapper(T wrapper, MCPlayer player) { + this.wrapper = wrapper; + this.player = player; + } + + @Override + public io.fairyproject.mc.MCPlayer getPlayer() { + return player; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/ArtemisPacketWrapper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/ArtemisPacketWrapper.java new file mode 100644 index 00000000..44e3b5b2 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/ArtemisPacketWrapper.java @@ -0,0 +1,11 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi; + +import io.fairyproject.bukkit.protocol.packet.PacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.MCPlayer; + +public abstract class ArtemisPacketWrapper extends PacketWrapper { + public ArtemisPacketWrapper(T wrapper, MCPlayer channel) { + super(wrapper, channel); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/PacketArtemisProvider.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/PacketArtemisProvider.java new file mode 100644 index 00000000..9d5bbb49 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/PacketArtemisProvider.java @@ -0,0 +1,72 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi; + +import io.fairyproject.bukkit.FairyBukkitPlatform; +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.netty.ArtemisChannel; +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.translate.ArtemisPacketTranslationHelper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.injector.PacketEventsInjector; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.InternalPacketListener; +import io.fairyproject.mc.protocol.PacketProvider; +import ac.artemis.packet.profile.Profile; +import ac.artemis.packet.spigot.wrappers.GPacket; +import cc.ghast.packet.PacketAPI; +import cc.ghast.packet.PacketManager; +import cc.ghast.packet.utils.Pair; + +import java.util.UUID; + +public class PacketArtemisProvider extends PacketProvider { + public PacketArtemisProvider(InternalPacketListener highListener, InternalBufferListener lowListener) { + super(highListener, lowListener, new PacketEventsInjector()); + } + + @Override + public void load() { + // Do nothing + } + + @Override + public void init() { + if (PacketManager.INSTANCE.getApi() == null) { + PacketManager.INSTANCE.init(FairyBukkitPlatform.PLUGIN); + } + + PacketAPI.addListener(new ac.artemis.packet.PacketListener() { + @Override + public void onPacket(Profile profile, ac.artemis.packet.wrapper.Packet wrapper) { + final MCPlayer data = MCPlayer.find(profile.getUuid()); + + if (data == null) { + return; + } + + final Packet packet = ArtemisPacketTranslationHelper.PACKET.transform(new Pair<>(profile, (GPacket) wrapper)); + + if (packet == null) + return; + + final UUID uuid = profile.getUuid(); + + if (!injectQueue.isEmpty() && injectQueue.contains(uuid)) { + injector.inject(data, new ArtemisChannel(packet.getPlayer().getChannel()), lowListener); + injectQueue.remove(uuid); + } + + final boolean cancel = highListener.onPacket(data, packet); + ((GPacket) wrapper).setCancelled(cancel); + } + }); + } + + @Override + public void quit() { + PacketManager.INSTANCE.destroy(); + } + + @Override + public void inject(MCPlayer data) { + injectQueue.add(data.getUUID()); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisBuffer.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisBuffer.java new file mode 100644 index 00000000..9fd34155 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisBuffer.java @@ -0,0 +1,56 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.netty; + +import cc.ghast.packet.wrapper.netty.MutableByteBuf; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; + +public class ArtemisBuffer implements FairyByteBuf { + private final MutableByteBuf byteBuf; + + public ArtemisBuffer(MutableByteBuf byteBuf) { + this.byteBuf = byteBuf; + } + + @Override + public int getSize() { + return byteBuf.readableBytes(); + } + + @Override + public byte[] getData() { + byte[] bytes = new byte[byteBuf.readableBytes()]; + int readerIndex = byteBuf.readerIndex(); + byteBuf.getBytes(readerIndex, bytes); + + return bytes; + } + + @Override + public int getReferenceCount() { + return byteBuf.refCnt(); + } + + @Override + public boolean isReadable() { + return byteBuf.isReadable(); + } + + @Override + public boolean isWriteable() { + return byteBuf.isWritable(); + } + + @Override + public int getReaderIndex() { + return byteBuf.readerIndex(); + } + + @Override + public void release() { + byteBuf.release(); + } + + @Override + public void clear() { + byteBuf.clear(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisChannel.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisChannel.java new file mode 100644 index 00000000..5e133ad0 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/netty/ArtemisChannel.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.netty; + +import io.fairyproject.mc.protocol.netty.Channel; + +public class ArtemisChannel implements Channel { + private final io.netty.channel.Channel channel; + + public ArtemisChannel(io.netty.channel.Channel channel) { + this.channel = channel; + } + + @Override + public void close() { + if (!channel.isOpen() || channel.pipeline() == null) + return; + + channel.close(); + } + + public io.netty.channel.Channel getChannel() { + return channel; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/translate/ArtemisPacketTranslationHelper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/translate/ArtemisPacketTranslationHelper.java new file mode 100644 index 00000000..67b61106 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/translate/ArtemisPacketTranslationHelper.java @@ -0,0 +1,178 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.translate; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.netty.ArtemisChannel; +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers.*; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.Channel; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.translate.Translator; +import ac.artemis.packet.profile.Profile; +import ac.artemis.packet.spigot.wrappers.GPacket; +import cc.ghast.packet.nms.EnumDirection; +import cc.ghast.packet.profile.ArtemisProfile; +import cc.ghast.packet.utils.Pair; +import cc.ghast.packet.wrapper.bukkit.BlockPosition; +import cc.ghast.packet.wrapper.bukkit.Vector3D; +import cc.ghast.packet.wrapper.mc.PlayerEnums; +import cc.ghast.packet.wrapper.packet.play.client.*; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import lombok.experimental.UtilityClass; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +@UtilityClass +public class ArtemisPacketTranslationHelper { + public final Translator CHANNEL = new Translator() { + @Override + public ArtemisChannel transform(io.netty.channel.Channel from) { + return new ArtemisChannel(from); + } + }; + + private final Map, TranslatorFunction> packetTranslatorMap = new HashMap<>(); + + void initPacket() { + packetTranslatorMap.put(GPacketPlayClientAbilities.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer channel) { + return new CPacketArtemisAbilities((GPacketPlayClientAbilities) wrapper, channel); + } + }); + packetTranslatorMap.put(GPacketPlayClientBlockPlace.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisBlockPlace((GPacketPlayClientBlockPlace) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientChat.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisChat((GPacketPlayClientChat) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientCustomPayload.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisCustomPayload((GPacketPlayClientCustomPayload) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientEntityAction.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisEntityAction((GPacketPlayClientEntityAction) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientFlying.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisFlying((GPacketPlayClientFlying) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientHeldItemSlot.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisHeldItemSlot((GPacketPlayClientHeldItemSlot) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientPosition.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisHeldItemSlot((GPacketPlayClientHeldItemSlot) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientPositionLook.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisPositionRotation((GPacketPlayClientPositionLook) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientLook.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisRotation((GPacketPlayClientLook) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientSetCreativeSlot.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisSetCreativeSlot((GPacketPlayClientSetCreativeSlot) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientSpectate.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisSpectate((GPacketPlayClientSpectate) wrapper, profile); + } + }); + packetTranslatorMap.put(GPacketPlayClientWindowClick.class, new TranslatorFunction() { + @Override + public Packet createPacket(GPacket wrapper, MCPlayer profile) { + return new CPacketArtemisWindowClick((GPacketPlayClientWindowClick) wrapper, profile); + } + }); + } + + interface TranslatorFunction extends Function, Packet> { + @Override + default Packet apply(Pair profilePair) { + final ArtemisProfile profile = (ArtemisProfile) profilePair.getK(); + return createPacket(profilePair.getV(), MCPlayer.from(profile.getUuid())); + } + + Packet createPacket(final T wrapper, final MCPlayer channel); + } + + public final Translator, Packet> PACKET = new Translator, Packet>() { + @Override + public Packet transform(Pair from) { + final TranslatorFunction translator = packetTranslatorMap.get(from.getV().getClass()); + + if (translator == null) + return null; + + return translator.apply(from); + } + }; + + public final Translator HAND = new Translator() { + @Override + public Hand transform(PlayerEnums.Hand from) { + return Hand.values()[from.ordinal()]; + } + }; + + public final Translator DIRECTION = new Translator() { + @Override + public Direction transform(EnumDirection from) { + return Direction.getDirection(from.ordinal()); + } + }; + + public final Translator PLAYER_ACTION = new Translator() { + @Override + public PlayerAction transform(PlayerEnums.PlayerAction from) { + return PlayerAction.values()[from.ordinal()]; + } + }; + + public final Translator VECTOR_3D = new Translator() { + @Override + public Vec3f transform(Vector3D from) { + // Don't question it, I'm (Ghast) brutally incompetent + return new Vec3f(from.getX(), from.getY(), from.getZ()); + } + }; + + public final Translator VECTOR_3I = new Translator() { + @Override + public Vec3i transform(BlockPosition from) { + return new Vec3i(from.getX(), from.getY(), from.getZ()); + } + }; +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisAbilities.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisAbilities.java new file mode 100644 index 00000000..c35bb172 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisAbilities.java @@ -0,0 +1,44 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketAbilities; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientAbilities; + +import java.util.Optional; + +public class CPacketArtemisAbilities extends ArtemisPacketWrapper implements CPacketAbilities { + public CPacketArtemisAbilities(GPacketPlayClientAbilities wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isFlying() { + return wrapper.isFlying(); + } + + @Override + public Optional isVulnerable() { + return wrapper.getInvulnerable(); + } + + @Override + public Optional isAllowFlight() { + return wrapper.getAllowedFlight(); + } + + @Override + public Optional isCreative() { + return wrapper.getCreativeMode(); + } + + @Override + public Optional getFlySpeed() { + return wrapper.getFlySpeed(); + } + + @Override + public Optional getWalkSpeed() { + return wrapper.getWalkSpeed(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisBlockPlace.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisBlockPlace.java new file mode 100644 index 00000000..a78edcda --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisBlockPlace.java @@ -0,0 +1,60 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.translate.ArtemisPacketTranslationHelper; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketBlockPlace; +import cc.ghast.packet.nms.EnumDirection; +import cc.ghast.packet.wrapper.bukkit.BlockPosition; +import cc.ghast.packet.wrapper.bukkit.Vector3D; +import cc.ghast.packet.wrapper.mc.PlayerEnums; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientBlockPlace; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public final class CPacketArtemisBlockPlace extends ArtemisPacketWrapper implements CPacketBlockPlace { + public CPacketArtemisBlockPlace(GPacketPlayClientBlockPlace wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public Hand getHand() { + final PlayerEnums.Hand bridge = wrapper.getHand(); + return ArtemisPacketTranslationHelper.HAND.transform(bridge); + } + + @Override + public Direction getDirection() { + final Optional bridge = wrapper.getDirection(); + + if (!bridge.isPresent()) { + return Direction.OTHER; + } + + return ArtemisPacketTranslationHelper.DIRECTION.transform(bridge.get()); + } + + @Override + public Vec3i getClickedBlock() { + final BlockPosition bridge = wrapper.getPosition(); + return ArtemisPacketTranslationHelper.VECTOR_3I.transform(bridge); + } + + @Override + public Optional getClickedOffset() { + final Vector3D optionalBridge = wrapper.getVector(); + final Vec3f translated = ArtemisPacketTranslationHelper.VECTOR_3D.transform(optionalBridge); + + return Optional.of(translated); + } + + @Override + public Optional getItemStack() { + return wrapper.getItem(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisChat.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisChat.java new file mode 100644 index 00000000..7a9d072e --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisChat.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketChat; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientChat; + +public class CPacketArtemisChat extends ArtemisPacketWrapper implements CPacketChat { + public CPacketArtemisChat(GPacketPlayClientChat wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getMessage() { + return wrapper.getMessage(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisCustomPayload.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisCustomPayload.java new file mode 100644 index 00000000..2a11880b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisCustomPayload.java @@ -0,0 +1,28 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.buffer.ByteArrayByteBuf; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.fairyproject.mc.protocol.packet.client.CPacketCustomPayload; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientCustomPayload; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +public class CPacketArtemisCustomPayload extends ArtemisPacketWrapper implements CPacketCustomPayload { + public CPacketArtemisCustomPayload(GPacketPlayClientCustomPayload wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getHeader() { + return wrapper.getHeader(); + } + + @Override + public FairyByteBuf getData() { + final byte[] data = Unpooled.buffer().readBytes((ByteBuf) wrapper.getMessage().getByteBuf().getParent()).array(); + + return new ByteArrayByteBuf(data); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisEntityAction.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisEntityAction.java new file mode 100644 index 00000000..340f0c18 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisEntityAction.java @@ -0,0 +1,21 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.translate.ArtemisPacketTranslationHelper; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketEntityAction; +import cc.ghast.packet.wrapper.mc.PlayerEnums; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientEntityAction; + +public class CPacketArtemisEntityAction extends ArtemisPacketWrapper implements CPacketEntityAction { + public CPacketArtemisEntityAction(GPacketPlayClientEntityAction wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public PlayerAction getAction() { + final PlayerEnums.PlayerAction bridge = wrapper.getAction(); + return bridge == null ? null : ArtemisPacketTranslationHelper.PLAYER_ACTION.transform(bridge); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisFlying.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisFlying.java new file mode 100644 index 00000000..5ec6083f --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisFlying.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketFlying; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientFlying; + +public class CPacketArtemisFlying extends ArtemisPacketWrapper implements CPacketFlying { + public CPacketArtemisFlying(GPacketPlayClientFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisHeldItemSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisHeldItemSlot.java new file mode 100644 index 00000000..eb02b091 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisHeldItemSlot.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketHeldItemSlot; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientHeldItemSlot; + +public class CPacketArtemisHeldItemSlot extends ArtemisPacketWrapper implements CPacketHeldItemSlot { + public CPacketArtemisHeldItemSlot(GPacketPlayClientHeldItemSlot wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getSlot(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPosition.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPosition.java new file mode 100644 index 00000000..c48c96b3 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPosition.java @@ -0,0 +1,32 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPosition; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientPosition; + +public class CPacketArtemisPosition extends ArtemisPacketWrapper implements CPacketPosition { + public CPacketArtemisPosition(GPacketPlayClientPosition wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getX(); + } + + @Override + public double getY() { + return wrapper.getY(); + } + + @Override + public double getZ() { + return wrapper.getZ(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPositionRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPositionRotation.java new file mode 100644 index 00000000..33bbbdfc --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisPositionRotation.java @@ -0,0 +1,42 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPositionRotation; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientPositionLook; + +public class CPacketArtemisPositionRotation extends ArtemisPacketWrapper implements CPacketPositionRotation { + public CPacketArtemisPositionRotation(GPacketPlayClientPositionLook wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getX(); + } + + @Override + public double getY() { + return wrapper.getY(); + } + + @Override + public double getZ() { + return wrapper.getZ(); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisRotation.java new file mode 100644 index 00000000..e3953ef9 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisRotation.java @@ -0,0 +1,27 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketRotation; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientLook; + +public class CPacketArtemisRotation extends ArtemisPacketWrapper implements CPacketRotation { + public CPacketArtemisRotation(GPacketPlayClientLook wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSetCreativeSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSetCreativeSlot.java new file mode 100644 index 00000000..a620b3c5 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSetCreativeSlot.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSetCreativeSlot; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientSetCreativeSlot; +import org.bukkit.inventory.ItemStack; + +public class CPacketArtemisSetCreativeSlot extends ArtemisPacketWrapper implements CPacketSetCreativeSlot { + public CPacketArtemisSetCreativeSlot(GPacketPlayClientSetCreativeSlot wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getSlot(); + } + + @Override + public ItemStack getItemStack() { + return wrapper.getItem(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSpectate.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSpectate.java new file mode 100644 index 00000000..d3ebe82d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisSpectate.java @@ -0,0 +1,19 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSpectate; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientSpectate; + +import java.util.UUID; + +public class CPacketArtemisSpectate extends ArtemisPacketWrapper implements CPacketSpectate { + public CPacketArtemisSpectate(GPacketPlayClientSpectate wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public UUID getSpectated() { + return wrapper.getEntityId(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisTabComplete.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisTabComplete.java new file mode 100644 index 00000000..d9cc0980 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisTabComplete.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketTabComplete; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientTabComplete; + +public class CPacketArtemisTabComplete extends ArtemisPacketWrapper implements CPacketTabComplete { + public CPacketArtemisTabComplete(GPacketPlayClientTabComplete wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getText() { + return wrapper.getValue(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisWindowClick.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisWindowClick.java new file mode 100644 index 00000000..b142eb10 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/artemispacketapi/wrappers/CPacketArtemisWindowClick.java @@ -0,0 +1,45 @@ +package io.fairyproject.bukkit.protocol.packet.artemispacketapi.wrappers; + +import io.fairyproject.bukkit.protocol.packet.artemispacketapi.ArtemisPacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketWindowClick; +import cc.ghast.packet.wrapper.packet.play.client.GPacketPlayClientWindowClick; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public class CPacketArtemisWindowClick extends ArtemisPacketWrapper implements CPacketWindowClick { + public CPacketArtemisWindowClick(GPacketPlayClientWindowClick wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getWindowId() { + return wrapper.getWindowId(); + } + + @Override + public int getWindowSlot() { + return wrapper.getSlot(); + } + + @Override + public int getWindowButton() { + return wrapper.getButton(); + } + + @Override + public Optional getActionNumber() { + return Optional.of((int) wrapper.getActionNumber()); + } + + @Override + public int getMode() { + return wrapper.getShiftedMode(); + } + + @Override + public ItemStack getItemStack() { + return wrapper.getClickedItem(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventWrapper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventWrapper.java new file mode 100644 index 00000000..84d5e705 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventWrapper.java @@ -0,0 +1,11 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1; + +import io.fairyproject.bukkit.protocol.packet.PacketWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.MCPlayer; + +public abstract class PacketEventWrapper extends PacketWrapper { + public PacketEventWrapper(T wrapper, MCPlayer channel) { + super(wrapper, channel); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventsV1Provider.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventsV1Provider.java new file mode 100644 index 00000000..579ea18e --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/PacketEventsV1Provider.java @@ -0,0 +1,74 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1; + +import io.fairyproject.bukkit.FairyBukkitPlatform; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.injector.PacketEventsInjector; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.InternalPacketListener; +import io.fairyproject.mc.protocol.PacketProvider; +import io.fairyproject.mc.protocol.packet.*; +import io.github.retrooper.packetevents.PacketEvents; +import io.github.retrooper.packetevents.event.annotation.PacketHandler; +import io.github.retrooper.packetevents.event.impl.PacketPlayReceiveEvent; +import io.github.retrooper.packetevents.utils.server.ServerVersion; + +import java.util.UUID; + +public class PacketEventsV1Provider extends PacketProvider { + public PacketEventsV1Provider(InternalPacketListener highListener, InternalBufferListener lowListener) { + super(highListener, lowListener, new PacketEventsInjector()); + } + + @Override + public void load() { + PacketEvents.create(FairyBukkitPlatform.PLUGIN); + PacketEvents.get().getSettings() + .compatInjector(false) + .checkForUpdates(false) + .bStats(false) + .fallbackServerVersion(ServerVersion.v_1_8_8); + + PacketEvents.get().loadAsyncNewThread(); + } + + @Override + public void init() { + PacketEvents.get().init(); + PacketEvents.get().getEventManager().registerListener(new io.github.retrooper.packetevents.event.PacketListener() { + @PacketHandler + private void handle(PacketPlayReceiveEvent packetPlayReceiveEvent) { + final MCPlayer data = MCPlayer.find(packetPlayReceiveEvent.getPlayer().getUniqueId()); + + if (data == null) { + return; + } + + final Packet packet = PacketEventsTranslationHelper.PACKET.transform(packetPlayReceiveEvent); + + if (packet == null) + return; + + final UUID uuid = packetPlayReceiveEvent.getPlayer().getUniqueId(); + + if (!injectQueue.isEmpty() && injectQueue.contains(uuid)) { + injector.inject(data, packet.getPlayer(), lowListener); + injectQueue.remove(uuid); + } + + final boolean cancel = highListener.onPacket(data, packet); + packetPlayReceiveEvent.setCancelled(cancel); + } + }); + } + + @Override + public void quit() { + PacketEvents.get().stop(); + } + + @Override + public void inject(MCPlayer data) { + injectQueue.add(data.getUUID()); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/injector/PacketEventsInjector.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/injector/PacketEventsInjector.java new file mode 100644 index 00000000..3d89181d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/injector/PacketEventsInjector.java @@ -0,0 +1,41 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.injector; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty.PacketEventsBuffer; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty.PacketEventsChannel; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.Channel; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.PacketInjector; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public class PacketEventsInjector implements PacketInjector { + @Override + public void inject(MCPlayer data, Channel channel, InternalBufferListener packetListener) { + final boolean instance = channel instanceof PacketEventsChannel; + + if (!instance) { + throw new IllegalStateException("Channel is of invalid type! (Type not PacketEvents channel)"); + } + + final PacketEventsChannel packetEventsChannel = (PacketEventsChannel) channel; + + packetEventsChannel.getChannel().pipeline().addBefore("decoder", "fairy-handler", + new ChannelInboundHandlerAdapter(){ + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + if (msg instanceof ByteBuf) { + final FairyByteBuf byteBuf = new PacketEventsBuffer((ByteBuf) msg); + final boolean cancel = packetListener.handle(data, byteBuf); + + if (cancel) + return; + } + + super.channelRead(ctx, msg); + } + }); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/mapping/PacketEventsV1Mapping.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/mapping/PacketEventsV1Mapping.java new file mode 100644 index 00000000..2b7303bf --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/mapping/PacketEventsV1Mapping.java @@ -0,0 +1,28 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.mapping; + +import io.fairyproject.bukkit.protocol.packet.AbstractPacketMapping; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers.*; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; +import io.github.retrooper.packetevents.packettype.PacketType; +import io.github.retrooper.packetevents.packetwrappers.WrappedPacket; + +public class PacketEventsV1Mapping extends AbstractPacketMapping { + @Override + public void inject() { + create(PacketType.Play.Client.CUSTOM_PAYLOAD, CPacketEventsCustomPayload.class, CPacketEventsCustomPayload::new); + create(PacketType.Play.Client.BLOCK_PLACE, CPacketEventsBlockPlace.class, CPacketEventsBlockPlace::new); + create(PacketType.Play.Client.WINDOW_CLICK, CPacketEventsWindowClick.class, CPacketEventsWindowClick::new); + create(PacketType.Play.Client.SET_CREATIVE_SLOT, CPacketEventsSetCreativeSlot.class, CPacketEventsSetCreativeSlot::new); + create(PacketType.Play.Client.ENTITY_ACTION, CPacketEventsEntityAction.class, CPacketEventsEntityAction::new); + create(PacketType.Play.Client.ABILITIES, CPacketEventsAbilities.class, CPacketEventsAbilities::new); + create(PacketType.Play.Client.HELD_ITEM_SLOT, CPacketEventsHeldItemSlot.class, CPacketEventsHeldItemSlot::new); + create(PacketType.Play.Client.CHAT, CPacketEventsChat.class, CPacketEventsChat::new); + create(PacketType.Play.Client.TAB_COMPLETE, CPacketEventsTabComplete.class, CPacketEventsTabComplete::new); + create(PacketType.Play.Client.SPECTATE, CPacketEventsSpectate.class, CPacketEventsSpectate::new); + create(PacketType.Play.Client.FLYING, CPacketEventsFlying.class, CPacketEventsFlying::new); + create(PacketType.Play.Client.POSITION, CPacketEventsPosition.class, CPacketEventsPosition::new); + create(PacketType.Play.Client.POSITION_LOOK, CPacketEventsPositionRotation.class, CPacketEventsPositionRotation::new); + create(PacketType.Play.Client.LOOK, CPacketEventsRotation.class, CPacketEventsRotation::new); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsBuffer.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsBuffer.java new file mode 100644 index 00000000..8b53776f --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsBuffer.java @@ -0,0 +1,56 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty; + +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.netty.buffer.ByteBuf; + +public class PacketEventsBuffer implements FairyByteBuf { + private final ByteBuf byteBuf; + + public PacketEventsBuffer(ByteBuf byteBuf) { + this.byteBuf = byteBuf; + } + + @Override + public int getSize() { + return byteBuf.readableBytes(); + } + + @Override + public byte[] getData() { + byte[] bytes = new byte[byteBuf.readableBytes()]; + int readerIndex = byteBuf.readerIndex(); + byteBuf.getBytes(readerIndex, bytes); + + return bytes; + } + + @Override + public int getReferenceCount() { + return byteBuf.refCnt(); + } + + @Override + public boolean isReadable() { + return byteBuf.isReadable(); + } + + @Override + public boolean isWriteable() { + return byteBuf.isWritable(); + } + + @Override + public int getReaderIndex() { + return byteBuf.readerIndex(); + } + + @Override + public void release() { + byteBuf.release(); + } + + @Override + public void clear() { + byteBuf.clear(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsChannel.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsChannel.java new file mode 100644 index 00000000..6b328f3f --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/netty/PacketEventsChannel.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty; + +import io.fairyproject.mc.protocol.netty.Channel; + +public class PacketEventsChannel implements Channel { + private final io.netty.channel.Channel channel; + + public PacketEventsChannel(io.netty.channel.Channel channel) { + this.channel = channel; + } + + @Override + public void close() { + if (!channel.isOpen() || channel.pipeline() == null) + return; + + channel.close(); + } + + public io.netty.channel.Channel getChannel() { + return channel; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/translate/PacketEventsTranslationHelper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/translate/PacketEventsTranslationHelper.java new file mode 100644 index 00000000..c5c38434 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/translate/PacketEventsTranslationHelper.java @@ -0,0 +1,100 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.translate; + +import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import io.fairyproject.bukkit.protocol.PacketBuilder; +import io.fairyproject.bukkit.protocol.PacketFactoryCreator; +import io.fairyproject.bukkit.protocol.PacketFactoryWrapper; +import io.fairyproject.bukkit.protocol.PacketMap; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.mapping.PacketEventsV1Mapping; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty.PacketEventsChannel; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers.*; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers.CPacketEventsRotation; +import io.fairyproject.bukkit.reflection.wrapper.ConstructorWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.translate.Translator; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import io.github.retrooper.packetevents.PacketEvents; +import io.github.retrooper.packetevents.event.impl.PacketPlayReceiveEvent; +import io.github.retrooper.packetevents.packettype.PacketType; +import io.github.retrooper.packetevents.packetwrappers.NMSPacket; +import io.github.retrooper.packetevents.packetwrappers.play.in.custompayload.WrappedPacketInCustomPayload; +import io.github.retrooper.packetevents.packetwrappers.play.in.entityaction.WrappedPacketInEntityAction; +import io.github.retrooper.packetevents.utils.vector.Vector3f; +import io.github.retrooper.packetevents.utils.vector.Vector3i; +import lombok.experimental.UtilityClass; +import lombok.val; + +import java.lang.reflect.Constructor; +import java.lang.reflect.ParameterizedType; +import java.util.HashMap; +import java.util.Map; + +@UtilityClass +public class PacketEventsTranslationHelper { + public final Translator CHANNEL = new Translator() { + @Override + public PacketEventsChannel transform(io.netty.channel.Channel from) { + return new PacketEventsChannel(from); + } + }; + + private final PacketEventsV1Mapping mappings = new PacketEventsV1Mapping(); + + public final Translator PACKET = new Translator() { + @Override + public Packet transform(PacketPlayReceiveEvent from) { + MCPlayer player = MCPlayer.from(from.getPlayer()); + mappings.wrap(player, from.getPacketId(), from.getNMSPacket()); + val packetId = from.getPacketId(); + + final PacketEventsTranslationHelper.PacketGenerator generator = generators.get(packetId); + + if (generator == null) { + return null; + } + + return generator.build(from.getNMSPacket(),player)); + } + }; + + public final Translator HAND = new Translator() { + @Override + public Hand transform(io.github.retrooper.packetevents.utils.player.Hand from) { + return Hand.values()[from.ordinal()]; + } + }; + + public final Translator DIRECTION = new Translator() { + @Override + public Direction transform(io.github.retrooper.packetevents.utils.player.Direction from) { + return Direction.getDirection(from.getFaceValue()); + } + }; + + public final Translator PLAYER_ACTION = new Translator() { + @Override + public PlayerAction transform(WrappedPacketInEntityAction.PlayerAction from) { + return PlayerAction.values()[from.ordinal()]; + } + }; + + public final Translator VECTOR_3F = new Translator() { + @Override + public Vec3f transform(Vector3f from) { + return new Vec3f(from.getX(), from.getY(), from.getZ()); + } + }; + + public final Translator VECTOR_3I = new Translator() { + @Override + public Vec3i transform(Vector3i from) { + return new Vec3i(from.getX(), from.getY(), from.getZ()); + } + }; +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsAbilities.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsAbilities.java new file mode 100644 index 00000000..bc918484 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsAbilities.java @@ -0,0 +1,44 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketAbilities; +import io.github.retrooper.packetevents.packetwrappers.play.in.abilities.WrappedPacketInAbilities; + +import java.util.Optional; + +public class CPacketEventsAbilities extends PacketEventWrapper implements CPacketAbilities { + public CPacketEventsAbilities(WrappedPacketInAbilities wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isFlying() { + return wrapper.isFlying(); + } + + @Override + public Optional isVulnerable() { + return wrapper.isVulnerable(); + } + + @Override + public Optional isAllowFlight() { + return wrapper.isFlightAllowed(); + } + + @Override + public Optional isCreative() { + return wrapper.canInstantlyBuild(); + } + + @Override + public Optional getFlySpeed() { + return wrapper.getFlySpeed(); + } + + @Override + public Optional getWalkSpeed() { + return wrapper.getWalkSpeed(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsBlockPlace.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsBlockPlace.java new file mode 100644 index 00000000..7d6d3578 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsBlockPlace.java @@ -0,0 +1,57 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketBlockPlace; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import io.github.retrooper.packetevents.packetwrappers.play.in.blockplace.WrappedPacketInBlockPlace; +import io.github.retrooper.packetevents.utils.vector.Vector3f; +import io.github.retrooper.packetevents.utils.vector.Vector3i; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public final class CPacketEventsBlockPlace extends PacketEventWrapper implements CPacketBlockPlace { + public CPacketEventsBlockPlace(WrappedPacketInBlockPlace wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public Hand getHand() { + final io.github.retrooper.packetevents.utils.player.Hand bridge = wrapper.getHand(); + return PacketEventsTranslationHelper.HAND.transform(bridge); + } + + @Override + public Direction getDirection() { + final io.github.retrooper.packetevents.utils.player.Direction bridge = wrapper.getDirection(); + return PacketEventsTranslationHelper.DIRECTION.transform(bridge); + } + + @Override + public Vec3i getClickedBlock() { + final Vector3i bridge = wrapper.getBlockPosition(); + return PacketEventsTranslationHelper.VECTOR_3I.transform(bridge); + } + + @Override + public Optional getClickedOffset() { + final Optional optionalBridge = wrapper.getCursorPosition(); + if (!optionalBridge.isPresent()) + return Optional.empty(); + + final Vector3f bridge = optionalBridge.get(); + final Vec3f translated = PacketEventsTranslationHelper.VECTOR_3F.transform(bridge); + + return Optional.of(translated); + } + + @Override + public Optional getItemStack() { + return wrapper.getItemStack(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsChat.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsChat.java new file mode 100644 index 00000000..67d2fcad --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsChat.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketChat; +import io.github.retrooper.packetevents.packetwrappers.play.in.chat.WrappedPacketInChat; + +public class CPacketEventsChat extends PacketEventWrapper implements CPacketChat { + public CPacketEventsChat(WrappedPacketInChat wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getMessage() { + return wrapper.getMessage(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsCustomPayload.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsCustomPayload.java new file mode 100644 index 00000000..554793c7 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsCustomPayload.java @@ -0,0 +1,26 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.buffer.ByteArrayByteBuf; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.fairyproject.mc.protocol.packet.client.CPacketCustomPayload; +import io.github.retrooper.packetevents.packetwrappers.play.in.custompayload.WrappedPacketInCustomPayload; + +public class CPacketEventsCustomPayload extends PacketEventWrapper implements CPacketCustomPayload { + public CPacketEventsCustomPayload(WrappedPacketInCustomPayload wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getHeader() { + return wrapper.getChannelName(); + } + + @Override + public FairyByteBuf getData() { + final byte[] data = wrapper.getData(); + + return new ByteArrayByteBuf(data); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsEntityAction.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsEntityAction.java new file mode 100644 index 00000000..cf9a3eae --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsEntityAction.java @@ -0,0 +1,20 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketEntityAction; +import io.github.retrooper.packetevents.packetwrappers.play.in.entityaction.WrappedPacketInEntityAction; + +public class CPacketEventsEntityAction extends PacketEventWrapper implements CPacketEntityAction { + public CPacketEventsEntityAction(WrappedPacketInEntityAction wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public PlayerAction getAction() { + final WrappedPacketInEntityAction.PlayerAction bridge = wrapper.getAction(); + return bridge == null ? null : PacketEventsTranslationHelper.PLAYER_ACTION.transform(bridge); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsFlying.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsFlying.java new file mode 100644 index 00000000..1d6a2692 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsFlying.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketFlying; +import io.github.retrooper.packetevents.packetwrappers.play.in.flying.WrappedPacketInFlying; + +public class CPacketEventsFlying extends PacketEventWrapper implements CPacketFlying { + public CPacketEventsFlying(WrappedPacketInFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsHeldItemSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsHeldItemSlot.java new file mode 100644 index 00000000..b7e21518 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsHeldItemSlot.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketHeldItemSlot; +import io.github.retrooper.packetevents.packetwrappers.play.in.helditemslot.WrappedPacketInHeldItemSlot; + +public class CPacketEventsHeldItemSlot extends PacketEventWrapper implements CPacketHeldItemSlot { + public CPacketEventsHeldItemSlot(WrappedPacketInHeldItemSlot wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getCurrentSelectedSlot(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPosition.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPosition.java new file mode 100644 index 00000000..71292be3 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPosition.java @@ -0,0 +1,32 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPosition; +import io.github.retrooper.packetevents.packetwrappers.play.in.flying.WrappedPacketInFlying; + +public class CPacketEventsPosition extends PacketEventWrapper implements CPacketPosition { + public CPacketEventsPosition(WrappedPacketInFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getPosition().getX(); + } + + @Override + public double getY() { + return wrapper.getPosition().getY(); + } + + @Override + public double getZ() { + return wrapper.getPosition().getZ(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPositionRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPositionRotation.java new file mode 100644 index 00000000..2a762196 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsPositionRotation.java @@ -0,0 +1,42 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPositionRotation; +import io.github.retrooper.packetevents.packetwrappers.play.in.flying.WrappedPacketInFlying; + +public class CPacketEventsPositionRotation extends PacketEventWrapper implements CPacketPositionRotation { + public CPacketEventsPositionRotation(WrappedPacketInFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getPosition().getX(); + } + + @Override + public double getY() { + return wrapper.getPosition().getY(); + } + + @Override + public double getZ() { + return wrapper.getPosition().getZ(); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsRotation.java new file mode 100644 index 00000000..1dd04b39 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsRotation.java @@ -0,0 +1,27 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketRotation; +import io.github.retrooper.packetevents.packetwrappers.play.in.flying.WrappedPacketInFlying; + +public class CPacketEventsRotation extends PacketEventWrapper implements CPacketRotation { + public CPacketEventsRotation(WrappedPacketInFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSetCreativeSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSetCreativeSlot.java new file mode 100644 index 00000000..a5b3e065 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSetCreativeSlot.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSetCreativeSlot; +import io.github.retrooper.packetevents.packetwrappers.play.in.setcreativeslot.WrappedPacketInSetCreativeSlot; +import org.bukkit.inventory.ItemStack; + +public class CPacketEventsSetCreativeSlot extends PacketEventWrapper implements CPacketSetCreativeSlot { + public CPacketEventsSetCreativeSlot(WrappedPacketInSetCreativeSlot wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getSlot(); + } + + @Override + public ItemStack getItemStack() { + return wrapper.getClickedItem(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSpectate.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSpectate.java new file mode 100644 index 00000000..38c40f34 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsSpectate.java @@ -0,0 +1,19 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSpectate; +import io.github.retrooper.packetevents.packetwrappers.play.in.spectate.WrappedPacketInSpectate; + +import java.util.UUID; + +public class CPacketEventsSpectate extends PacketEventWrapper implements CPacketSpectate { + public CPacketEventsSpectate(WrappedPacketInSpectate wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public UUID getSpectated() { + return wrapper.getUUID(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsTabComplete.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsTabComplete.java new file mode 100644 index 00000000..5d95dc40 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsTabComplete.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketTabComplete; +import io.github.retrooper.packetevents.packetwrappers.play.in.tabcomplete.WrappedPacketInTabComplete; + +public class CPacketEventsTabComplete extends PacketEventWrapper implements CPacketTabComplete { + public CPacketEventsTabComplete(WrappedPacketInTabComplete wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getText() { + return wrapper.getText(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsWindowClick.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsWindowClick.java new file mode 100644 index 00000000..68ffd27a --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v1/wrappers/CPacketEventsWindowClick.java @@ -0,0 +1,45 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v1.wrappers; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventWrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketWindowClick; +import io.github.retrooper.packetevents.packetwrappers.play.in.windowclick.WrappedPacketInWindowClick; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public class CPacketEventsWindowClick extends PacketEventWrapper implements CPacketWindowClick { + public CPacketEventsWindowClick(WrappedPacketInWindowClick wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getWindowId() { + return wrapper.getWindowId(); + } + + @Override + public int getWindowSlot() { + return wrapper.getWindowSlot(); + } + + @Override + public int getWindowButton() { + return wrapper.getWindowButton(); + } + + @Override + public Optional getActionNumber() { + return Optional.of(wrapper.getActionNumber()); + } + + @Override + public int getMode() { + return wrapper.getMode(); + } + + @Override + public ItemStack getItemStack() { + return wrapper.getClickedItemStack(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventV2Wrapper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventV2Wrapper.java new file mode 100644 index 00000000..53ff6fd3 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventV2Wrapper.java @@ -0,0 +1,10 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2; + +import io.fairyproject.bukkit.protocol.packet.PacketWrapper; +import io.fairyproject.mc.MCPlayer; + +public abstract class PacketEventV2Wrapper extends PacketWrapper { + public PacketEventV2Wrapper(T wrapper, MCPlayer channel) { + super(wrapper, channel); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventsV2Provider.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventsV2Provider.java new file mode 100644 index 00000000..7f59c958 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/PacketEventsV2Provider.java @@ -0,0 +1,77 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2; + +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.event.PacketListenerPriority; +import com.github.retrooper.packetevents.event.SimplePacketListenerAbstract; +import com.github.retrooper.packetevents.event.simple.PacketPlayReceiveEvent; +import io.fairyproject.bukkit.FairyBukkitPlatform; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.injector.PacketEventsInjector; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.InternalPacketListener; +import io.fairyproject.mc.protocol.PacketProvider; +import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; + +import java.util.UUID; + +public class PacketEventsV2Provider extends PacketProvider { + public PacketEventsV2Provider(InternalPacketListener highListener, InternalBufferListener lowListener) { + super(highListener, lowListener, new PacketEventsInjector()); + } + + @Override + public void load() { + PacketEvents.setAPI(SpigotPacketEventsBuilder.build(FairyBukkitPlatform.PLUGIN)); + PacketEvents.getAPI().load(); + } + + @Override + public void init() { + PacketEvents.getAPI().getSettings() + .debug(false) + .bStats(false) + .checkForUpdates(true); + PacketEvents.getAPI().init(); + + SimplePacketListenerAbstract listener = new SimplePacketListenerAbstract(PacketListenerPriority.HIGH, + true, false) { + @Override + public void onPacketPlayReceive(PacketPlayReceiveEvent event) { + final UUID uuid = event.getUser().getProfile().getUUID(); + final MCPlayer data = MCPlayer.find(uuid); + + if (data == null) { + return; + } + + final Packet packet = PacketEventsTranslationHelper.PACKET.transform(event); + + if (packet == null) + return; + + + if (!injectQueue.isEmpty() && injectQueue.contains(uuid)) { + injector.inject(data, packet.getPlayer(), lowListener); + injectQueue.remove(uuid); + } + + final boolean cancel = highListener.onPacket(data, packet); + event.setCancelled(cancel); + } + }; + + PacketEvents.getAPI().getEventManager().registerListener(listener); + } + + @Override + public void quit() { + PacketEvents.getAPI().terminate(); + } + + @Override + public void inject(MCPlayer data) { + injectQueue.add(data.getUUID()); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/injector/PacketEventsInjector.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/injector/PacketEventsInjector.java new file mode 100644 index 00000000..8a79b5ee --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/injector/PacketEventsInjector.java @@ -0,0 +1,29 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.injector; + +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.event.PacketListenerAbstract; +import com.github.retrooper.packetevents.event.PacketReceiveEvent; +import com.github.retrooper.packetevents.event.PacketSendEvent; +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.netty.PacketEventsBuffer; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.PacketInjector; +import io.fairyproject.mc.protocol.netty.Channel; +import io.netty.buffer.ByteBuf; + +public class PacketEventsInjector implements PacketInjector { + @Override + public void inject(MCPlayer data, Channel channel, InternalBufferListener packetListener) { + PacketEvents.getAPI().getEventManager().registerListener(new PacketListenerAbstract() { + @Override + public void onPacketReceive(PacketReceiveEvent event) { + packetListener.handle(data, new PacketEventsBuffer((ByteBuf) event.getByteBuf())); + } + + @Override + public void onPacketSend(PacketSendEvent event) { + packetListener.handle(data, new PacketEventsBuffer((ByteBuf) event.getByteBuf())); + } + }); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/mapping/PacketEventsV2Mapping.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/mapping/PacketEventsV2Mapping.java new file mode 100644 index 00000000..6374f352 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/mapping/PacketEventsV2Mapping.java @@ -0,0 +1,27 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.mapping; + +import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; +import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import io.fairyproject.bukkit.protocol.packet.AbstractPacketMapping; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers.*; + +public class PacketEventsV2Mapping extends AbstractPacketMapping, PacketTypeCommon> { + @Override + public void inject() { + create(PacketType.Play.Client.PLUGIN_MESSAGE, CPacketEventsCustomPayload.class, CPacketEventsCustomPayload::new); + create(PacketType.Play.Client.PLAYER_BLOCK_PLACEMENT, CPacketEventsBlockPlace.class, CPacketEventsBlockPlace::new); + create(PacketType.Play.Client.CLICK_WINDOW, CPacketEventsWindowClick.class, CPacketEventsWindowClick::new); + create(PacketType.Play.Client.CREATIVE_INVENTORY_ACTION, CPacketEventsSetCreativeSlot.class, CPacketEventsSetCreativeSlot::new); + create(PacketType.Play.Client.ENTITY_ACTION, CPacketEventsEntityAction.class, CPacketEventsEntityAction::new); + create(PacketType.Play.Client.PLAYER_ABILITIES, CPacketEventsAbilities.class, CPacketEventsAbilities::new); + create(PacketType.Play.Client.HELD_ITEM_SLOT, CPacketEventsHeldItemSlot.class, CPacketEventsHeldItemSlot::new); + create(PacketType.Play.Client.CHAT, CPacketEventsChat.class, CPacketEventsChat::new); + create(PacketType.Play.Client.TAB_COMPLETE, CPacketEventsTabComplete.class, CPacketEventsTabComplete::new); + create(PacketType.Play.Client.SPECTATE, CPacketEventsSpectate.class, CPacketEventsSpectate::new); + create(PacketType.Play.Client.FLYING, CPacketEventsFlying.class, CPacketEventsFlying::new); + create(PacketType.Play.Client.POSITION, CPacketEventsPosition.class, CPacketEventsPosition::new); + create(PacketType.Play.Client.POSITION_LOOK, CPacketEventsPositionRotation.class, CPacketEventsPositionRotation::new); + create(PacketType.Play.Client.LOOK, CPacketEventsRotation.class, CPacketEventsRotation::new); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsBuffer.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsBuffer.java new file mode 100644 index 00000000..e5705000 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsBuffer.java @@ -0,0 +1,56 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.netty; + +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.netty.buffer.ByteBuf; + +public class PacketEventsBuffer implements FairyByteBuf { + private final ByteBuf byteBuf; + + public PacketEventsBuffer(ByteBuf byteBuf) { + this.byteBuf = byteBuf; + } + + @Override + public int getSize() { + return byteBuf.readableBytes(); + } + + @Override + public byte[] getData() { + byte[] bytes = new byte[byteBuf.readableBytes()]; + int readerIndex = byteBuf.readerIndex(); + byteBuf.getBytes(readerIndex, bytes); + + return bytes; + } + + @Override + public int getReferenceCount() { + return byteBuf.refCnt(); + } + + @Override + public boolean isReadable() { + return byteBuf.isReadable(); + } + + @Override + public boolean isWriteable() { + return byteBuf.isWritable(); + } + + @Override + public int getReaderIndex() { + return byteBuf.readerIndex(); + } + + @Override + public void release() { + byteBuf.release(); + } + + @Override + public void clear() { + byteBuf.clear(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsChannel.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsChannel.java new file mode 100644 index 00000000..1e859551 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/netty/PacketEventsChannel.java @@ -0,0 +1,23 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.netty; + +import io.fairyproject.mc.MCPlayer; + +public class PacketEventsChannel implements Channel { + private final io.netty.channel.Channel channel; + + public PacketEventsChannel(io.netty.channel.Channel channel) { + this.channel = channel; + } + + @Override + public void close() { + if (!channel.isOpen() || channel.pipeline() == null) + return; + + channel.close(); + } + + public io.netty.channel.Channel getChannel() { + return channel; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/translate/PacketEventsTranslationHelper.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/translate/PacketEventsTranslationHelper.java new file mode 100644 index 00000000..350a2f82 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/translate/PacketEventsTranslationHelper.java @@ -0,0 +1,166 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.translate; + +import com.github.retrooper.packetevents.event.PacketEvent; +import com.github.retrooper.packetevents.event.PacketReceiveEvent; +import com.github.retrooper.packetevents.event.PacketSendEvent; +import com.github.retrooper.packetevents.event.simple.PacketPlayReceiveEvent; +import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; +import com.github.retrooper.packetevents.protocol.player.InteractionHand; +import com.github.retrooper.packetevents.protocol.world.BlockFace; +import com.github.retrooper.packetevents.util.Vector3f; +import com.github.retrooper.packetevents.util.Vector3i; +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEntityAction; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerScoreboardObjective; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.netty.PacketEventsChannel; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers.*; +import io.fairyproject.bukkit.reflection.wrapper.ConstructorWrapper; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.mcp.ObjectiveActionType; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.protocol.item.ObjectiveRenderType; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.mc.protocol.packet.client.CPacket; +import io.fairyproject.mc.protocol.translate.Translator; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import lombok.experimental.UtilityClass; +import lombok.val; + +import java.lang.reflect.Constructor; +import java.lang.reflect.ParameterizedType; +import java.util.HashMap; +import java.util.Map; + +@UtilityClass +public class PacketEventsTranslationHelper { + public final Translator CHANNEL = new Translator() { + @Override + public PacketEventsChannel transform(io.netty.channel.Channel from) { + return new PacketEventsChannel(from); + } + }; + + private final Map> generators = new HashMap<>(); + + static class PacketGenerator> { + private final ConstructorWrapper typeConstructor; + private final ConstructorWrapper wrapperConstructor; + + public PacketGenerator(Class typeClass, Class wrapperClass, boolean client) { + Constructor _typeConstructor; + try { + _typeConstructor = typeClass.getDeclaredConstructor(client ? PacketReceiveEvent.class : PacketSendEvent.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("Failed to create generator", e); + } + this.typeConstructor = new ConstructorWrapper<>(_typeConstructor); + + Constructor _wrapperConstructor; + try { + _wrapperConstructor = wrapperClass.getDeclaredConstructor(typeClass, MCPlayer.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("Failed to create generator", e); + } + this.wrapperConstructor = new ConstructorWrapper<>(_wrapperConstructor); + + } + + public W build(final PacketEvent event, final MCPlayer channel) { + final T instance = typeConstructor.newInstance(event); + return wrapperConstructor.newInstance(instance, channel); + } + + static > PacketGenerator create(Class wrapperClass) { + final Class typeClass = (Class) (ParameterizedType.class.cast(wrapperClass.getGenericSuperclass())).getActualTypeArguments()[0]; + return new PacketGenerator<>(typeClass, wrapperClass, CPacket.class.isAssignableFrom(wrapperClass)); + } + + static { + generators.put(PacketType.Play.Client.PLUGIN_MESSAGE, create(CPacketEventsCustomPayload.class)); + generators.put(PacketType.Play.Client.PLAYER_BLOCK_PLACEMENT, create(CPacketEventsBlockPlace.class)); + generators.put(PacketType.Play.Client.CLICK_WINDOW, create(CPacketEventsWindowClick.class)); + generators.put(PacketType.Play.Client.CREATIVE_INVENTORY_ACTION, create(CPacketEventsSetCreativeSlot.class)); + generators.put(PacketType.Play.Client.ENTITY_ACTION, create(CPacketEventsEntityAction.class)); + generators.put(PacketType.Play.Client.PLAYER_ABILITIES, create(CPacketEventsAbilities.class)); + generators.put(PacketType.Play.Client.HELD_ITEM_CHANGE, create(CPacketEventsHeldItemSlot.class)); + generators.put(PacketType.Play.Client.CHAT_MESSAGE, create(CPacketEventsChat.class)); + generators.put(PacketType.Play.Client.TAB_COMPLETE, create(CPacketEventsTabComplete.class)); + generators.put(PacketType.Play.Client.SPECTATE, create(CPacketEventsSpectate.class)); + generators.put(PacketType.Play.Client.PLAYER_FLYING, create(CPacketEventsFlying.class)); + generators.put(PacketType.Play.Client.PLAYER_POSITION, create(CPacketEventsPosition.class)); + generators.put(PacketType.Play.Client.PLAYER_POSITION_AND_ROTATION, create(CPacketEventsPositionRotation.class)); + generators.put(PacketType.Play.Client.PLAYER_ROTATION, create(CPacketEventsRotation.class)); + + generators.put(PacketType.Play.Server.SCOREBOARD_OBJECTIVE, create(SPacketEventsScoreboardObjective.class)); + } + } + + public final Translator PACKET = new Translator() { + @Override + public Packet transform(PacketPlayReceiveEvent from) { + val player = from.getPlayer(); + val packetId = from.getPacketType(); + + final PacketGenerator generator = generators.get(packetId); + + if (generator == null) { + return null; + } + + return generator.build(from, MCPlayer.from(player)); + } + }; + + public final Translator HAND = new Translator() { + @Override + public Hand transform(InteractionHand from) { + return Hand.values()[from.ordinal()]; + } + }; + + public final Translator DIRECTION = new Translator() { + @Override + public Direction transform(BlockFace from) { + return Direction.getDirection(from.getFaceValue()); + } + }; + + public final Translator PLAYER_ACTION = new Translator() { + @Override + public PlayerAction transform(WrapperPlayClientEntityAction.Action from) { + return PlayerAction.values()[from.ordinal()]; + } + }; + + public final Translator SCOREBOARD_DISPLAY_TYPE = new Translator() { + @Override + public ObjectiveRenderType transform(WrapperPlayServerScoreboardObjective.HealthDisplay from) { + return from == WrapperPlayServerScoreboardObjective.HealthDisplay.HEARTS ? ObjectiveRenderType.HEARTS : ObjectiveRenderType.INTEGER; + } + }; + + public final Translator SCOREBOARD_ACTION_TYPE = new Translator() { + @Override + public ObjectiveActionType transform(WrapperPlayServerScoreboardObjective.ObjectiveMode from) { + return ObjectiveActionType.values()[from.ordinal()]; + } + }; + + public final Translator VECTOR_3F = new Translator() { + @Override + public Vec3f transform(Vector3f from) { + return new Vec3f(from.getX(), from.getY(), from.getZ()); + } + }; + + public final Translator VECTOR_3I = new Translator() { + @Override + public Vec3i transform(Vector3i from) { + return new Vec3i(from.getX(), from.getY(), from.getZ()); + } + }; +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsAbilities.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsAbilities.java new file mode 100644 index 00000000..3e1ce97b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsAbilities.java @@ -0,0 +1,44 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerAbilities; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketAbilities; + +import java.util.Optional; + +public class CPacketEventsAbilities extends PacketEventV2Wrapper implements CPacketAbilities { + public CPacketEventsAbilities(WrapperPlayClientPlayerAbilities wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isFlying() { + return wrapper.isFlying(); + } + + @Override + public Optional isVulnerable() { + return wrapper.isInGodMode(); + } + + @Override + public Optional isAllowFlight() { + return wrapper.isFlightAllowed(); + } + + @Override + public Optional isCreative() { + return wrapper.isInCreativeMode(); + } + + @Override + public Optional getFlySpeed() { + return wrapper.getFlySpeed(); + } + + @Override + public Optional getWalkSpeed() { + return wrapper.getWalkSpeed(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsBlockPlace.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsBlockPlace.java new file mode 100644 index 00000000..77ddafe0 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsBlockPlace.java @@ -0,0 +1,59 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.protocol.player.InteractionHand; +import com.github.retrooper.packetevents.protocol.world.BlockFace; +import com.github.retrooper.packetevents.util.Vector3f; +import com.github.retrooper.packetevents.util.Vector3i; +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerBlockPlacement; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketBlockPlace; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public final class CPacketEventsBlockPlace extends PacketEventV2Wrapper implements CPacketBlockPlace { + public CPacketEventsBlockPlace(WrapperPlayClientPlayerBlockPlacement wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public Hand getHand() { + final InteractionHand bridge = wrapper.getHand(); + return PacketEventsTranslationHelper.HAND.transform(bridge); + } + + @Override + public Direction getDirection() { + final BlockFace bridge = wrapper.getFace(); + return PacketEventsTranslationHelper.DIRECTION.transform(bridge); + } + + @Override + public Vec3i getClickedBlock() { + final Vector3i bridge = wrapper.getBlockPosition(); + return PacketEventsTranslationHelper.VECTOR_3I.transform(bridge); + } + + @Override + public Optional getClickedOffset() { + final Vector3f optionalBridge = wrapper.getCursorPosition(); + if (optionalBridge == null) + return Optional.empty(); + + final Vec3f translated = PacketEventsTranslationHelper.VECTOR_3F.transform(optionalBridge); + + return Optional.of(translated); + } + + @Override + public Optional getItemStack() { + // TODO: ItemWrapper to ItemStack conversion + throw new IllegalStateException("Todo!"); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsChat.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsChat.java new file mode 100644 index 00000000..dc2b264b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsChat.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientChatMessage; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketChat; + +public class CPacketEventsChat extends PacketEventV2Wrapper implements CPacketChat { + public CPacketEventsChat(WrapperPlayClientChatMessage wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getMessage() { + return wrapper.getMessage(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsCustomPayload.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsCustomPayload.java new file mode 100644 index 00000000..8cf7b864 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsCustomPayload.java @@ -0,0 +1,26 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPluginMessage; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.buffer.ByteArrayByteBuf; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; +import io.fairyproject.mc.protocol.packet.client.CPacketCustomPayload; + +public class CPacketEventsCustomPayload extends PacketEventV2Wrapper implements CPacketCustomPayload { + public CPacketEventsCustomPayload(WrapperPlayClientPluginMessage wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getHeader() { + return wrapper.getChannelName(); + } + + @Override + public FairyByteBuf getData() { + final byte[] data = wrapper.getData(); + + return new ByteArrayByteBuf(data); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsEntityAction.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsEntityAction.java new file mode 100644 index 00000000..5fef8b65 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsEntityAction.java @@ -0,0 +1,20 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEntityAction; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.mcp.PlayerAction; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketEntityAction; + +public class CPacketEventsEntityAction extends PacketEventV2Wrapper implements CPacketEntityAction { + public CPacketEventsEntityAction(WrapperPlayClientEntityAction wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public PlayerAction getAction() { + final WrapperPlayClientEntityAction.Action bridge = wrapper.getAction(); + return bridge == null ? null : PacketEventsTranslationHelper.PLAYER_ACTION.transform(bridge); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsFlying.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsFlying.java new file mode 100644 index 00000000..7e1212ef --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsFlying.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerFlying; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketFlying; + +public class CPacketEventsFlying extends PacketEventV2Wrapper implements CPacketFlying { + public CPacketEventsFlying(WrapperPlayClientPlayerFlying wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsHeldItemSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsHeldItemSlot.java new file mode 100644 index 00000000..2019c479 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsHeldItemSlot.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientHeldItemChange; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketHeldItemSlot; + +public class CPacketEventsHeldItemSlot extends PacketEventV2Wrapper implements CPacketHeldItemSlot { + public CPacketEventsHeldItemSlot(WrapperPlayClientHeldItemChange wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getSlot(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPosition.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPosition.java new file mode 100644 index 00000000..225813e9 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPosition.java @@ -0,0 +1,32 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerPosition; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPosition; + +public class CPacketEventsPosition extends PacketEventV2Wrapper implements CPacketPosition { + public CPacketEventsPosition(WrapperPlayClientPlayerPosition wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getPosition().getX(); + } + + @Override + public double getY() { + return wrapper.getPosition().getY(); + } + + @Override + public double getZ() { + return wrapper.getPosition().getZ(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPositionRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPositionRotation.java new file mode 100644 index 00000000..51e4ecaf --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsPositionRotation.java @@ -0,0 +1,42 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerPositionAndRotation; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketPositionRotation; + +public class CPacketEventsPositionRotation extends PacketEventV2Wrapper implements CPacketPositionRotation { + public CPacketEventsPositionRotation(WrapperPlayClientPlayerPositionAndRotation wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public double getX() { + return wrapper.getPosition().getX(); + } + + @Override + public double getY() { + return wrapper.getPosition().getY(); + } + + @Override + public double getZ() { + return wrapper.getPosition().getZ(); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsRotation.java new file mode 100644 index 00000000..6067a4c3 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsRotation.java @@ -0,0 +1,27 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerRotation; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketRotation; + +public class CPacketEventsRotation extends PacketEventV2Wrapper implements CPacketRotation { + public CPacketEventsRotation(WrapperPlayClientPlayerRotation wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public float getYaw() { + return wrapper.getYaw(); + } + + @Override + public float getPitch() { + return wrapper.getPitch(); + } + + @Override + public boolean isGround() { + return wrapper.isOnGround(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSetCreativeSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSetCreativeSlot.java new file mode 100644 index 00000000..8fb1ffb8 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSetCreativeSlot.java @@ -0,0 +1,24 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientCreativeInventoryAction; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSetCreativeSlot; +import org.bukkit.inventory.ItemStack; + +public class CPacketEventsSetCreativeSlot extends PacketEventV2Wrapper implements CPacketSetCreativeSlot { + public CPacketEventsSetCreativeSlot(WrapperPlayClientCreativeInventoryAction wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getSlot() { + return wrapper.getSlot(); + } + + @Override + public ItemStack getItemStack() { + // TODO: ItemWrapper to ItemStack conversion + throw new IllegalStateException("Todo!"); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSpectate.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSpectate.java new file mode 100644 index 00000000..5a556d9c --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsSpectate.java @@ -0,0 +1,19 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientSpectate; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketSpectate; + +import java.util.UUID; + +public class CPacketEventsSpectate extends PacketEventV2Wrapper implements CPacketSpectate { + public CPacketEventsSpectate(WrapperPlayClientSpectate wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public UUID getSpectated() { + return wrapper.getTargetUUID(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsTabComplete.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsTabComplete.java new file mode 100644 index 00000000..16b7e2a1 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsTabComplete.java @@ -0,0 +1,17 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientTabComplete; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketTabComplete; + +public class CPacketEventsTabComplete extends PacketEventV2Wrapper implements CPacketTabComplete { + public CPacketEventsTabComplete(WrapperPlayClientTabComplete wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getText() { + return wrapper.getText(); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsWindowClick.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsWindowClick.java new file mode 100644 index 00000000..3ea7ea3b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/CPacketEventsWindowClick.java @@ -0,0 +1,46 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClickWindow; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.client.CPacketWindowClick; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public class CPacketEventsWindowClick extends PacketEventV2Wrapper implements CPacketWindowClick { + public CPacketEventsWindowClick(WrapperPlayClientClickWindow wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public int getWindowId() { + return wrapper.getWindowId(); + } + + @Override + public int getWindowSlot() { + return wrapper.getSlot(); + } + + @Override + public int getWindowButton() { + return wrapper.getButton(); + } + + @Override + public Optional getActionNumber() { + return wrapper.getStateId(); + } + + @Override + public int getMode() { + return wrapper.getWindowClickType().ordinal(); + } + + @Override + public ItemStack getItemStack() { + // TODO: ItemWrapper to ItemStack conversion + throw new IllegalStateException("Todo!"); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/SPacketEventsScoreboardObjective.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/SPacketEventsScoreboardObjective.java new file mode 100644 index 00000000..2bd8d505 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/packet/packetevents/v2/wrappers/SPacketEventsScoreboardObjective.java @@ -0,0 +1,48 @@ +package io.fairyproject.bukkit.protocol.packet.packetevents.v2.wrappers; + +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerScoreboardObjective; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.PacketEventV2Wrapper; +import io.fairyproject.bukkit.protocol.packet.packetevents.v2.translate.PacketEventsTranslationHelper; +import io.fairyproject.mc.MCAdventure; +import io.fairyproject.mc.mcp.ObjectiveActionType; +import io.fairyproject.mc.protocol.MCProtocol; +import io.fairyproject.mc.protocol.MCVersion; +import io.fairyproject.mc.protocol.item.ObjectiveRenderType; +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.server.SPacketScoreboardObjective; +import net.kyori.adventure.text.Component; + +import java.util.Optional; + +public class SPacketEventsScoreboardObjective extends PacketEventV2Wrapper implements SPacketScoreboardObjective { + public SPacketEventsScoreboardObjective(WrapperPlayServerScoreboardObjective wrapper, MCPlayer channel) { + super(wrapper, channel); + } + + @Override + public String getObjectiveName() { + return wrapper.getName(); + } + + @Override + public Component getDisplayName() { + if (!wrapper.getDisplayName().isPresent()) { + return Component.empty(); + } else { + final String message = wrapper.getDisplayName().get(); + return MCProtocol.INSTANCE.version().below(MCVersion.V1_13) + ? MCAdventure.LEGACY.deserialize(message) + : MCAdventure.GSON.deserialize(message); + } + } + + @Override + public Optional getRenderType() { + return wrapper.getDisplay().map(PacketEventsTranslationHelper.SCOREBOARD_DISPLAY_TYPE::transform); + } + + @Override + public ObjectiveActionType getMethod() { + return PacketEventsTranslationHelper.SCOREBOARD_ACTION_TYPE.transform(wrapper.getMode()); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/provider/AbstractPacketProviderFactory.java b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/provider/AbstractPacketProviderFactory.java new file mode 100644 index 00000000..83a3443a --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/bukkit-protocol/src/main/java/io/fairyproject/bukkit/protocol/provider/AbstractPacketProviderFactory.java @@ -0,0 +1,32 @@ +package io.fairyproject.bukkit.protocol.provider; + +import io.fairyproject.bukkit.protocol.packet.packetevents.v1.PacketEventsV1Provider; +import io.fairyproject.mc.protocol.InternalBufferListener; +import io.fairyproject.mc.protocol.InternalPacketListener; +import io.fairyproject.mc.protocol.PacketProvider; + +public abstract class AbstractPacketProviderFactory { + protected InternalPacketListener packetListener; + protected InternalBufferListener lowLevelPacketListener; + + public AbstractPacketProviderFactory setPacketListener(InternalPacketListener packetListener) { + this.packetListener = packetListener; + return this; + } + + public AbstractPacketProviderFactory setLowLevelPacketListener(InternalBufferListener lowLevelPacketListener) { + this.lowLevelPacketListener = lowLevelPacketListener; + return this; + } + + protected void verify() { + assert packetListener != null : "PacketListener cannot be null!"; + assert lowLevelPacketListener != null : "BufferListener cannot be null!"; + } + + public PacketProvider build() { + this.verify(); + + return new PacketEventsV1Provider(packetListener, lowLevelPacketListener); + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/.gitignore b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/.gitignore new file mode 100644 index 00000000..f3d6549d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/build.gradle b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/build.gradle new file mode 100644 index 00000000..0d69248d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/build.gradle @@ -0,0 +1,9 @@ +version = "0.0.1b1" + +dependencies { + compileOnly project.spigot() +} + +module { + platform("mc") +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalBufferListener.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalBufferListener.java new file mode 100644 index 00000000..0989730f --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalBufferListener.java @@ -0,0 +1,8 @@ +package io.fairyproject.mc.protocol; + +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; + +public interface InternalBufferListener { + boolean handle(final MCPlayer data, final FairyByteBuf byteBuf); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalPacketListener.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalPacketListener.java new file mode 100644 index 00000000..8b35daa2 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/InternalPacketListener.java @@ -0,0 +1,8 @@ +package io.fairyproject.mc.protocol; + +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.packet.Packet; + +public interface InternalPacketListener { + boolean onPacket(final MCPlayer data, final Packet packet); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketInjector.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketInjector.java new file mode 100644 index 00000000..9d2b7967 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketInjector.java @@ -0,0 +1,8 @@ +package io.fairyproject.mc.protocol; + +import io.fairyproject.mc.MCPlayer; +import io.fairyproject.mc.protocol.netty.Channel; + +public interface PacketInjector { + void inject(final MCPlayer data, final Channel channel, final InternalBufferListener packetListener); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketListenerService.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketListenerService.java new file mode 100644 index 00000000..a72d15ff --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketListenerService.java @@ -0,0 +1,104 @@ +package io.fairyproject.mc.protocol; + +import io.fairyproject.container.ComponentHolder; +import io.fairyproject.container.ComponentRegistry; +import io.fairyproject.container.PreInitialize; +import io.fairyproject.container.Service; +import io.fairyproject.mc.protocol.annotation.Listen; +import io.fairyproject.mc.protocol.listener.PacketListener; +import io.fairyproject.mc.protocol.packet.Packet; +import io.fairyproject.reflect.Reflect; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + +@Service +public final class PacketListenerService { + private final Map> objectListenerCache = new HashMap<>(); + private final Queue listeners = new PriorityQueue<>((a, b) -> Integer.compare(b.getPriority(), a.getPriority())); + + @PreInitialize + public void preInit() { + ComponentRegistry.registerComponentHolder(ComponentHolder.builder() + .type(PacketListener.class) + .onEnable(obj -> { + final PacketListener packetListener = (PacketListener) obj; + final List methodList = Reflect.getDeclaredMethods(obj.getClass()); + for (Method declaredMethod : methodList) { + try { + Reflect.setAccessible(declaredMethod); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException("Failed to initiate reflection on object", e); + } + } + + final List internalListeners = new ArrayList<>(); + + for (Method method : methodList) { + final Listen listen = method.getAnnotation(Listen.class); + + if (listen == null) + continue; + + if (method.getParameterCount() != 1) { + throw new IllegalStateException("PacketListener can only listen to a packet"); + } + + final Class type = method.getParameters()[0].getType(); + final InternalListener listener = new InternalListener(type, packetListener, method, listen.priority()); + internalListeners.add(listener); + } + + objectListenerCache.put(packetListener, internalListeners); + listeners.addAll(internalListeners); + }) + .onDisable(obj -> { + final List internalListeners = objectListenerCache.get((PacketListener) obj); + objectListenerCache.remove(obj); + listeners.removeAll(internalListeners); + }).build()); + } + + public void call(final Packet packet) { + listeners.stream() + .filter(e -> e.getType().isAssignableFrom(packet.getClass())) + .forEach(e -> { + try { + e.getMethod().invoke(e.getObj(), packet); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new IllegalStateException("Failed to invoke packet listener", ex); + } + }); + } + + static class InternalListener { + private final Class type; + private final PacketListener obj; + private final Method method; + private final int priority; + + public InternalListener(Class type, PacketListener obj, Method method, int priority) { + this.type = type; + this.obj = obj; + this.method = method; + this.priority = priority; + } + + public Class getType() { + return type; + } + + public PacketListener getObj() { + return obj; + } + + public Method getMethod() { + return method; + } + + public int getPriority() { + return priority; + } + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProvider.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProvider.java new file mode 100644 index 00000000..16da81c7 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProvider.java @@ -0,0 +1,50 @@ +package io.fairyproject.mc.protocol; + +import io.fairyproject.mc.MCPlayer; + +import java.util.HashSet; +import java.util.UUID; + +public abstract class PacketProvider { + protected final InternalPacketListener highListener; + protected final InternalBufferListener lowListener; + protected final PacketInjector injector; + protected final InjectQueue injectQueue; + + public PacketProvider(InternalPacketListener highListener, InternalBufferListener lowListener, PacketInjector injector) { + this.highListener = highListener; + this.lowListener = lowListener; + this.injector = injector; + this.injectQueue = new InjectQueue(); + } + + public abstract void load(); + + public abstract void init(); + + public abstract void quit(); + + public abstract void inject(final MCPlayer data); + + protected static class InjectQueue extends HashSet { + @Override + public boolean isEmpty() { + return super.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return super.contains(o); + } + + @Override + public boolean add(UUID uuid) { + return super.add(uuid); + } + + @Override + public boolean remove(Object o) { + return super.remove(o); + } + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProviderFactory.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProviderFactory.java new file mode 100644 index 00000000..69dc7424 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/PacketProviderFactory.java @@ -0,0 +1,11 @@ +package io.fairyproject.mc.protocol; + +interface PacketProviderFactory { + PacketProviderFactory setPacketListener(InternalPacketListener packetListener); + + PacketProviderFactory setLowLevelPacketListener(InternalBufferListener bufferListener); + + void verify(); + + PacketProvider build(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/annotation/Listen.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/annotation/Listen.java new file mode 100644 index 00000000..8ad58edf --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/annotation/Listen.java @@ -0,0 +1,13 @@ +package io.fairyproject.mc.protocol.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Listen { + // TODO + int priority(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/listener/PacketListener.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/listener/PacketListener.java new file mode 100644 index 00000000..a65c009e --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/listener/PacketListener.java @@ -0,0 +1,4 @@ +package io.fairyproject.mc.protocol.listener; + +public interface PacketListener { +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/Channel.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/Channel.java new file mode 100644 index 00000000..479db49c --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/Channel.java @@ -0,0 +1,5 @@ +package io.fairyproject.mc.protocol.netty; + +public interface Channel { + void close(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/ByteArrayByteBuf.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/ByteArrayByteBuf.java new file mode 100644 index 00000000..25708e58 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/ByteArrayByteBuf.java @@ -0,0 +1,51 @@ +package io.fairyproject.mc.protocol.netty.buffer; + +import java.util.Arrays; + +public class ByteArrayByteBuf implements FairyByteBuf { + private byte[] data; + + public ByteArrayByteBuf(byte[] data) { + this.data = data; + } + + @Override + public int getReferenceCount() { + return 1; + } + + @Override + public boolean isReadable() { + return true; + } + + @Override + public boolean isWriteable() { + return true; + } + + @Override + public int getReaderIndex() { + return 0; + } + + @Override + public void release() { + data = null; + } + + @Override + public void clear() { + Arrays.fill(data, (byte) 0); + } + + @Override + public int getSize() { + return data.length; + } + + @Override + public byte[] getData() { + return data; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/FairyByteBuf.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/FairyByteBuf.java new file mode 100644 index 00000000..8b499a64 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/netty/buffer/FairyByteBuf.java @@ -0,0 +1,19 @@ +package io.fairyproject.mc.protocol.netty.buffer; + +public interface FairyByteBuf { + int getSize(); + + int getReferenceCount(); + + boolean isReadable(); + + boolean isWriteable(); + + int getReaderIndex(); + + byte[] getData(); + + void release(); + + void clear(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/Packet.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/Packet.java new file mode 100644 index 00000000..14469ad3 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/Packet.java @@ -0,0 +1,19 @@ +package io.fairyproject.mc.protocol.packet; + +import io.fairyproject.mc.MCPlayer; + +public interface Packet { + /** + * Fancy name without the "PacketPlay(in/out)" prefix and instead annotated with + * something more friendly for UI use. + * @return String object representing the friendly name + */ + String getFancyName(); + + /** + * Returns the Player of which the packet is provided from. This is most usually + * going to be as the format of a MCPlayer. + * @return MCPlayer object of the packet + */ + MCPlayer getPlayer(); +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacket.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacket.java new file mode 100644 index 00000000..eafde94e --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacket.java @@ -0,0 +1,6 @@ +package io.fairyproject.mc.protocol.packet.client; + +import io.fairyproject.mc.protocol.packet.Packet; + +public interface CPacket extends Packet { +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketAbilities.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketAbilities.java new file mode 100644 index 00000000..e36823ff --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketAbilities.java @@ -0,0 +1,22 @@ +package io.fairyproject.mc.protocol.packet.client; + +import java.util.Optional; + +public interface CPacketAbilities extends CPacket { + boolean isFlying(); + + Optional isVulnerable(); + + Optional isAllowFlight(); + + Optional isCreative(); + + Optional getFlySpeed(); + + Optional getWalkSpeed(); + + @Override + default String getFancyName() { + return "Abilities"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketBlockPlace.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketBlockPlace.java new file mode 100644 index 00000000..d1890f02 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketBlockPlace.java @@ -0,0 +1,37 @@ +package io.fairyproject.mc.protocol.packet.client; + +import io.fairyproject.mc.mcp.Direction; +import io.fairyproject.mc.mcp.Hand; +import io.fairyproject.mc.util.Vec3f; +import io.fairyproject.mc.util.Vec3i; + +import java.util.Optional; + +public interface CPacketBlockPlace extends CPacket { + + Hand getHand(); + + Direction getDirection(); + + Vec3i getClickedBlock(); + + /** + * + * @return + */ + Optional getClickedOffset(); + + /** + * Returns the ItemStack type used for placing a block. This can vary depending the + * scenario (such as direction 255) as to where the following won't be provided. + * + * @param Type parameter representing the ItemStack type defined by the wrapper + * @return ItemStack used for the block place. This can be empty for hand clicks. + */ + Optional getItemStack(); + + @Override + default String getFancyName() { + return "BlockPlace"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketChat.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketChat.java new file mode 100644 index 00000000..993b0dab --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketChat.java @@ -0,0 +1,10 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketChat extends CPacket { + String getMessage(); + + @Override + default String getFancyName() { + return "Chat"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketCustomPayload.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketCustomPayload.java new file mode 100644 index 00000000..905f262b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketCustomPayload.java @@ -0,0 +1,15 @@ +package io.fairyproject.mc.protocol.packet.client; + + +import io.fairyproject.mc.protocol.netty.buffer.FairyByteBuf; + +public interface CPacketCustomPayload extends CPacket { + String getHeader(); + + FairyByteBuf getData(); + + @Override + default String getFancyName() { + return "CustomPayload"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketEntityAction.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketEntityAction.java new file mode 100644 index 00000000..06150247 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketEntityAction.java @@ -0,0 +1,26 @@ +package io.fairyproject.mc.protocol.packet.client; + +import io.fairyproject.mc.mcp.PlayerAction; + +public interface CPacketEntityAction extends CPacket { + /** + * Returns the player action which was executed; The following actions are valid depending on + * the player's protocol version: + * 0 -> START_SNEAKING + * 1 -> STOP_SNEAKING + * 2 -> STOP_SLEEPING + * 3 -> START_SPRINTING + * 4 -> STOP_SPRINTING + * 5 -> START_RIDING_JUMP + * 6 -> STOP_RIDING_JUMP + * 7 -> OPEN_INVENTORY + * 8 -> START_FALL_FLYING + * @return Returns the player action + */ + PlayerAction getAction(); + + @Override + default String getFancyName() { + return "EntityAction"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketFlying.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketFlying.java new file mode 100644 index 00000000..ebd1f2ac --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketFlying.java @@ -0,0 +1,10 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketFlying extends CPacket { + boolean isGround(); + + @Override + default String getFancyName() { + return "CPacketFlying"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketHeldItemSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketHeldItemSlot.java new file mode 100644 index 00000000..cb7f8aff --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketHeldItemSlot.java @@ -0,0 +1,10 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketHeldItemSlot extends CPacket{ + int getSlot(); + + @Override + default String getFancyName() { + return "ItemSlot"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPosition.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPosition.java new file mode 100644 index 00000000..a7803418 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPosition.java @@ -0,0 +1,15 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketPosition extends CPacketFlying { + + double getX(); + + double getY(); + + double getZ(); + + @Override + default String getFancyName() { + return "Position"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPositionRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPositionRotation.java new file mode 100644 index 00000000..83e888fc --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketPositionRotation.java @@ -0,0 +1,8 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketPositionRotation extends CPacketPosition, CPacketRotation { + @Override + default String getFancyName() { + return "PositionRotation"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketRotation.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketRotation.java new file mode 100644 index 00000000..630b0165 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketRotation.java @@ -0,0 +1,12 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketRotation extends CPacketFlying { + float getYaw(); + + float getPitch(); + + @Override + default String getFancyName() { + return "Rotation"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSetCreativeSlot.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSetCreativeSlot.java new file mode 100644 index 00000000..2782f8de --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSetCreativeSlot.java @@ -0,0 +1,12 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketSetCreativeSlot extends CPacket { + int getSlot(); + + T getItemStack(); + + @Override + default String getFancyName() { + return "SetCreativeSlot"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSpectate.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSpectate.java new file mode 100644 index 00000000..74160991 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketSpectate.java @@ -0,0 +1,13 @@ +package io.fairyproject.mc.protocol.packet.client; + +import java.util.UUID; + +public interface CPacketSpectate extends CPacket { + + UUID getSpectated(); + + @Override + default String getFancyName() { + return "Spectate"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketTabComplete.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketTabComplete.java new file mode 100644 index 00000000..1d35c51f --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketTabComplete.java @@ -0,0 +1,11 @@ +package io.fairyproject.mc.protocol.packet.client; + +public interface CPacketTabComplete extends CPacket { + + String getText(); + + @Override + default String getFancyName() { + return "TabComplete"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketWindowClick.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketWindowClick.java new file mode 100644 index 00000000..65d1f37b --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/client/CPacketWindowClick.java @@ -0,0 +1,22 @@ +package io.fairyproject.mc.protocol.packet.client; + +import java.util.Optional; + +public interface CPacketWindowClick extends CPacket { + int getWindowId(); + + int getWindowSlot(); + + int getWindowButton(); + + Optional getActionNumber(); + + int getMode(); + + T getItemStack(); + + @Override + default String getFancyName() { + return "WindowClick"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacket.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacket.java new file mode 100644 index 00000000..41d92ee2 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacket.java @@ -0,0 +1,6 @@ +package io.fairyproject.mc.protocol.packet.server; + +import io.fairyproject.mc.protocol.packet.Packet; + +public interface SPacket extends Packet { +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacketScoreboardObjective.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacketScoreboardObjective.java new file mode 100644 index 00000000..fafb346d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/packet/server/SPacketScoreboardObjective.java @@ -0,0 +1,22 @@ +package io.fairyproject.mc.protocol.packet.server; + +import io.fairyproject.mc.mcp.ObjectiveActionType; +import io.fairyproject.mc.protocol.item.ObjectiveRenderType; +import net.kyori.adventure.text.Component; + +import java.util.Optional; + +public interface SPacketScoreboardObjective extends SPacket { + String getObjectiveName(); + + Component getDisplayName(); + + Optional getRenderType(); + + ObjectiveActionType getMethod(); + + @Override + default String getFancyName() { + return "ScoreboardObjective"; + } +} diff --git a/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/translate/Translator.java b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/translate/Translator.java new file mode 100644 index 00000000..83bfdbd8 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.protocol/mc-protocol/src/main/java/io/fairyproject/mc/protocol/translate/Translator.java @@ -0,0 +1,5 @@ +package io.fairyproject.mc.protocol.translate; + +public interface Translator { + T transform(final F from); +} diff --git a/io.fairyproject.modules/platform.mc/module.schematic/.gitignore b/io.fairyproject.modules/platform.mc/module.schematic/.gitignore new file mode 100644 index 00000000..f3d6549d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.schematic/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/.gitignore b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/.gitignore new file mode 100644 index 00000000..f3d6549d --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/build.gradle b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/build.gradle new file mode 100644 index 00000000..5e6b9822 --- /dev/null +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/build.gradle @@ -0,0 +1,9 @@ +version = "0.0.1b1" + +dependencies { + compileOnly project.spigot() + compileOnly "com.sk89q.worldedit:worldedit-bukkit:6.1.5" + compileOnly "com.sk89q.worldedit:worldedit-core:6.0.0-SNAPSHOT" +} + +module {} \ No newline at end of file diff --git a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/Schematic.java b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/Schematic.java similarity index 95% rename from io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/Schematic.java rename to io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/Schematic.java index fb41defe..51b75e22 100644 --- a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/Schematic.java +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/Schematic.java @@ -22,15 +22,15 @@ * SOFTWARE. */ -package io.fairyproject.bukkit.util.schematic; +package io.fairyproject.mc.schematic.schematic; +import io.fairyproject.mc.schematic.schematic.impl.FAWESchematic; +import io.fairyproject.mc.schematic.schematic.impl.WorldEditSchematic; import lombok.Getter; import lombok.Setter; import org.bukkit.Location; import org.bukkit.World; import io.fairyproject.mc.util.BlockPosition; -import io.fairyproject.bukkit.util.schematic.impl.FAWESchematic; -import io.fairyproject.bukkit.util.schematic.impl.WorldEditSchematic; import java.io.File; import java.io.IOException; diff --git a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/SchematicType.java b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/SchematicType.java similarity index 96% rename from io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/SchematicType.java rename to io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/SchematicType.java index d1adaf7d..6047d82c 100644 --- a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/SchematicType.java +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/SchematicType.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package io.fairyproject.bukkit.util.schematic; +package io.fairyproject.mc.schematic.schematic; public enum SchematicType { diff --git a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/FAWESchematic.java b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/FAWESchematic.java similarity index 97% rename from io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/FAWESchematic.java rename to io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/FAWESchematic.java index 29824955..1b4332a8 100644 --- a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/FAWESchematic.java +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/FAWESchematic.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package io.fairyproject.bukkit.util.schematic.impl; +package io.fairyproject.mc.schematic.schematic.impl; import com.google.common.base.Preconditions; import com.sk89q.worldedit.Vector; diff --git a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/WorldEditSchematic.java b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/WorldEditSchematic.java similarity index 97% rename from io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/WorldEditSchematic.java rename to io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/WorldEditSchematic.java index 87fcbca0..76600e09 100644 --- a/io.fairyproject.platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/schematic/impl/WorldEditSchematic.java +++ b/io.fairyproject.modules/platform.mc/module.schematic/bukkit-schematic/src/main/java/io/fairyproject/mc/schematic/schematic/impl/WorldEditSchematic.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package io.fairyproject.bukkit.util.schematic.impl; +package io.fairyproject.mc.schematic.schematic.impl; import com.google.common.base.Preconditions; import com.sk89q.worldedit.EditSession; @@ -41,7 +41,7 @@ import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.registry.WorldData; -import io.fairyproject.bukkit.util.schematic.Schematic; +import io.fairyproject.mc.schematic.schematic.Schematic; import io.fairyproject.mc.util.BlockPosition; import org.bukkit.Location; diff --git a/io.fairyproject.platforms/bukkit-platform/build.gradle b/io.fairyproject.platforms/bukkit-platform/build.gradle index fd32f3fc..162167b2 100644 --- a/io.fairyproject.platforms/bukkit-platform/build.gradle +++ b/io.fairyproject.platforms/bukkit-platform/build.gradle @@ -33,9 +33,6 @@ dependencies { compileOnly "io.netty:netty-all:4.1.60.Final" compileOnly "com.viaversion:viaversion:4.0.1" compileOnly "com.github.dmulloy2:ProtocolLib:4.6.0" - compileOnly "com.boydti:fawe-api:latest" - compileOnly "com.sk89q.worldedit:worldedit-bukkit:6.1.5" - compileOnly "com.sk89q.worldedit:worldedit-core:6.0.0-SNAPSHOT" compileOnly name: "ProtocolSupport" compileOnly "org.ow2.asm:asm:" + findProperty("asm.version") compileOnly "org.ow2.asm:asm-commons:" + findProperty("asm.version") diff --git a/io.fairyproject.platforms/core-platform/src/main/java/io/fairyproject/library/LibraryRepository.java b/io.fairyproject.platforms/core-platform/src/main/java/io/fairyproject/library/LibraryRepository.java index 63f16d75..53d3f154 100644 --- a/io.fairyproject.platforms/core-platform/src/main/java/io/fairyproject/library/LibraryRepository.java +++ b/io.fairyproject.platforms/core-platform/src/main/java/io/fairyproject/library/LibraryRepository.java @@ -29,6 +29,8 @@ public class LibraryRepository { public static final LibraryRepository MAVEN_CENTRAL = new LibraryRepository("https://repo1.maven.org/maven2/"); + public static final LibraryRepository JITPACK = new LibraryRepository("https://jitpack.io/"); + public static final LibraryRepository IMANITY = new LibraryRepository("https://maven.imanity.dev/repository/imanity-libraries/"); @Getter private final String url; diff --git a/io.fairyproject.platforms/mc-platform/build.gradle b/io.fairyproject.platforms/mc-platform/build.gradle index 601ac951..9218bfc5 100644 --- a/io.fairyproject.platforms/mc-platform/build.gradle +++ b/io.fairyproject.platforms/mc-platform/build.gradle @@ -1,6 +1,9 @@ dependencies { compileOnly "io.netty:netty-all:" + findProperty("netty.version") + // PacketEvents, for packet stuff + api 'com.github.retrooper.packetevents:api:' + findProperty("packetevents.v2.version") + // Adventure, for user-interface api "net.kyori:adventure-api:" + findProperty("adventure.version") api "net.kyori:adventure-text-minimessage:4.2.0-SNAPSHOT" diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Direction.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Direction.java new file mode 100644 index 00000000..0b196a32 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Direction.java @@ -0,0 +1,35 @@ +package io.fairyproject.mc.mcp; + +public enum Direction { + DOWN, + UP, + NORTH, + SOUTH, + WEST, + EAST, + OTHER((short)255), + INVALID; + + private final short face; + + Direction(short face) { + this.face = face; + } + + Direction() { + this.face = (short)this.ordinal(); + } + + public static Direction getDirection(int face) { + if (face == 255) { + return OTHER; + } else { + return face >= 0 && face <= 5 ? values()[face] : INVALID; + } + } + + public short getFaceValue() { + return this.face; + } +} + diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/GameMode.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/GameMode.java new file mode 100644 index 00000000..111bf18e --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/GameMode.java @@ -0,0 +1,66 @@ +package io.fairyproject.mc.mcp; + +import java.util.HashMap; +import java.util.Map; + +public enum GameMode { + /** + * Creative mode may fly, build instantly, become invulnerable and create + * free items. + */ + CREATIVE(1), + + /** + * Survival mode is the "normal" gameplay type, with no special features. + */ + SURVIVAL(0), + + /** + * Adventure mode cannot break blocks without the correct tools. + */ + ADVENTURE(2), + + /** + * Spectator mode cannot interact with the world in anyway and is + * invisible to normal players. This grants the player the + * ability to no-clip through the world. + */ + SPECTATOR(3); + + private final int value; + private final static Map BY_ID = new HashMap<>(); + + GameMode(final int value) { + this.value = value; + } + + /** + * Gets the mode value associated with this GameMode + * + * @return An integer value of this gamemode + * @deprecated Magic value + */ + @Deprecated + public int getValue() { + return value; + } + + /** + * Gets the GameMode represented by the specified value + * + * @param value Value to check + * @return Associative {@link GameMode} with the given value, or null if + * it doesn't exist + * @deprecated Magic value + */ + @Deprecated + public static GameMode getByValue(final int value) { + return BY_ID.get(value); + } + + static { + for (GameMode mode : values()) { + BY_ID.put(mode.getValue(), mode); + } + } +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Hand.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Hand.java new file mode 100644 index 00000000..f7845d01 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/Hand.java @@ -0,0 +1,6 @@ +package io.fairyproject.mc.mcp; + +public enum Hand { + MAIN_HAND, + OFF_HAND; +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/InventoryType.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/InventoryType.java new file mode 100644 index 00000000..cb273495 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/InventoryType.java @@ -0,0 +1,129 @@ +package io.fairyproject.mc.mcp; + +public enum InventoryType { + + /** + * A chest inventory, with 0, 9, 18, 27, 36, 45, or 54 slots of type + * CONTAINER. + */ + CHEST(27,"Chest"), + /** + * A dispenser inventory, with 9 slots of type CONTAINER. + */ + DISPENSER(9,"Dispenser"), + /** + * A dropper inventory, with 9 slots of type CONTAINER. + */ + DROPPER(9, "Dropper"), + /** + * A furnace inventory, with a RESULT slot, a CRAFTING slot, and a FUEL + * slot. + */ + FURNACE(3,"Furnace"), + /** + * A workbench inventory, with 9 CRAFTING slots and a RESULT slot. + */ + WORKBENCH(10,"Crafting"), + /** + * A player's crafting inventory, with 4 CRAFTING slots and a RESULT slot. + * Also implies that the 4 ARMOR slots are accessible. + */ + CRAFTING(5,"Crafting"), + /** + * An enchantment table inventory, with two CRAFTING slots and three + * enchanting buttons. + */ + ENCHANTING(2,"Enchanting"), + /** + * A brewing stand inventory, with one FUEL slot and three CRAFTING slots. + */ + BREWING(5,"Brewing"), + /** + * A player's inventory, with 9 QUICKBAR slots, 27 CONTAINER slots, 4 ARMOR + * slots and 1 offhand slot. The ARMOR and offhand slots may not be visible + * to the player, though. + */ + PLAYER(41,"Player"), + /** + * The creative mode inventory, with only 9 QUICKBAR slots and nothing + * else. (The actual creative interface with the items is client-side and + * cannot be altered by the server.) + */ + CREATIVE(9,"Creative"), + /** + * The merchant inventory, with 2 TRADE-IN slots, and 1 RESULT slot. + */ + MERCHANT(3,"Villager"), + /** + * The ender chest inventory, with 27 slots. + */ + ENDER_CHEST(27,"Ender Chest"), + /** + * An anvil inventory, with 2 CRAFTING slots and 1 RESULT slot + */ + ANVIL(3, "Repairing"), + /** + * A beacon inventory, with 1 CRAFTING slot + */ + BEACON(1, "container.beacon"), + /** + * A hopper inventory, with 5 slots of type CONTAINER. + */ + HOPPER(5, "Item Hopper"), + /** + * A shulker box inventory, with 27 slots of type CONTAINER. + */ + SHULKER_BOX(27, "Shulker Box"), + ; + + private final int size; + private final String title; + + InventoryType(int defaultSize, String defaultTitle) { + size = defaultSize; + title = defaultTitle; + } + + public int getDefaultSize() { + return size; + } + + public String getDefaultTitle() { + return title; + } + + public enum SlotType { + /** + * A result slot in a furnace or crafting inventory. + */ + RESULT, + /** + * A slot in the crafting matrix, or the input slot in a furnace + * inventory, the potion slot in the brewing stand, or the enchanting + * slot. + */ + CRAFTING, + /** + * An armour slot in the player's inventory. + */ + ARMOR, + /** + * A regular slot in the container or the player's inventory; anything + * not covered by the other enum values. + */ + CONTAINER, + /** + * A slot in the bottom row or quickbar. + */ + QUICKBAR, + /** + * A pseudo-slot representing the area outside the inventory window. + */ + OUTSIDE, + /** + * The fuel slot in a furnace inventory, or the ingredient slot in a + * brewing stand inventory. + */ + FUEL; + } +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/ObjectiveActionType.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/ObjectiveActionType.java new file mode 100644 index 00000000..90b85b63 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/ObjectiveActionType.java @@ -0,0 +1,7 @@ +package io.fairyproject.mc.mcp; + +public enum ObjectiveActionType { + CREATE, + REMOVE, + UPDATE; +} \ No newline at end of file diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/PlayerAction.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/PlayerAction.java new file mode 100644 index 00000000..56d7f1b6 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/mcp/PlayerAction.java @@ -0,0 +1,36 @@ +package io.fairyproject.mc.mcp; + +public enum PlayerAction { + START_SNEAKING((byte)0, "PRESS_SHIFT_KEY"), + STOP_SNEAKING((byte)1, "RELEASE_SHIFT_KEY"), + STOP_SLEEPING((byte)2), + START_SPRINTING((byte)3), + STOP_SPRINTING((byte)4), + START_RIDING_JUMP((byte)5), + STOP_RIDING_JUMP((byte)6), + OPEN_INVENTORY((byte)7), + START_FALL_FLYING((byte)8), + @Deprecated + RIDING_JUMP((byte)5); + + private final byte actionID; + private final String alias; + + PlayerAction(byte actionID) { + this.actionID = actionID; + this.alias = "empty"; + } + + PlayerAction(byte actionID, String alias) { + this.actionID = actionID; + this.alias = alias; + } + + public String getAlias() { + return alias; + } + + public byte getActionValue() { + return this.actionID; + } +} \ No newline at end of file diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCPacket.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCPacket.java index 328ed4cd..87eb78bf 100644 --- a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCPacket.java +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCPacket.java @@ -1,11 +1,5 @@ package io.fairyproject.mc.protocol; -import io.fairyproject.mc.protocol.netty.FriendlyByteBuf; - public interface MCPacket { - void read(FriendlyByteBuf byteBuf); - - void write(FriendlyByteBuf byteBuf); - } diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java index 15e8e862..0b2ca58e 100644 --- a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java @@ -1,12 +1,15 @@ package io.fairyproject.mc.protocol; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.PacketEventsAPI; import io.fairyproject.mc.MCAdventure; +import io.fairyproject.plugin.Plugin; import lombok.Getter; import io.fairyproject.mc.protocol.mapping.MCProtocolMapping; import io.fairyproject.mc.protocol.netty.NettyInjector; @Getter -public class MCProtocol { +public class MCProtocol extends PacketEventsAPI { public static MCProtocol INSTANCE; public static void initialize(NettyInjector injector, MCProtocolMapping protocolMapping) { diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec2i.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec2i.java new file mode 100644 index 00000000..0242bc8d --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec2i.java @@ -0,0 +1,58 @@ +package io.fairyproject.mc.util; + +import java.util.Objects; + +public class Vec2i { + private int x; + private int y; + + /** + * @param x X coordinate of the vector + * @param y Y coordinate of the vector + */ + public Vec2i(int x, int y) { + this.x = x; + this.y = y; + } + + /** + * @return Return X coordinate of the vector + */ + public int getX() { + return x; + } + + /** + * @param x Set X coordinate of the vector + */ + public void setX(int x) { + this.x = x; + } + + /** + * @return Return Y coordinate of the vector + */ + public int getY() { + return y; + } + + /** + * @param y Set Y coordinate of the vector + */ + public void setY(int y) { + this.y = y; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Vec2i)) return false; + Vec2i vec2i = (Vec2i) o; + return x == vec2i.x && y == vec2i.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3d.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3d.java new file mode 100644 index 00000000..a984233d --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3d.java @@ -0,0 +1,75 @@ +package io.fairyproject.mc.util; + +import java.util.Objects; + +public class Vec3d { + private double x; + private double y; + private double z; + + /** + * @param x X coordinate of the vector + * @param y Y coordinate of the vector + * @param z Z coordinate of the vector + */ + public Vec3d(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * @return Return X coordinate of the vector + */ + public double getX() { + return x; + } + + /** + * @param x Set X coordinate of the vector + */ + public void setX(double x) { + this.x = x; + } + + /** + * @return Return Y coordinate of the vector + */ + public double getY() { + return y; + } + + /** + * @param y Set Y coordinate of the vector + */ + public void setY(double y) { + this.y = y; + } + + /** + * @return Return Z coordinate of the vector + */ + public double getZ() { + return z; + } + + /** + * @param z Set Z coordinate of the vector + */ + public void setZ(double z) { + this.z = z; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Vec3d)) return false; + Vec3d vec3i = (Vec3d) o; + return x == vec3i.x && y == vec3i.y && z == vec3i.z; + } + + @Override + public int hashCode() { + return Objects.hash(x, y, z); + } +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3f.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3f.java new file mode 100644 index 00000000..29cc31b3 --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3f.java @@ -0,0 +1,75 @@ +package io.fairyproject.mc.util; + +import java.util.Objects; + +public class Vec3f { + private float x; + private float y; + private float z; + + /** + * @param x X coordinate of the vector + * @param y Y coordinate of the vector + * @param z Z coordinate of the vector + */ + public Vec3f(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * @return Return X coordinate of the vector + */ + public float getX() { + return x; + } + + /** + * @param x Set X coordinate of the vector + */ + public void setX(float x) { + this.x = x; + } + + /** + * @return Return Y coordinate of the vector + */ + public float getY() { + return y; + } + + /** + * @param y Set Y coordinate of the vector + */ + public void setY(float y) { + this.y = y; + } + + /** + * @return Return Z coordinate of the vector + */ + public float getZ() { + return z; + } + + /** + * @param z Set Z coordinate of the vector + */ + public void setZ(float z) { + this.z = z; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Vec3f)) return false; + Vec3f vec3i = (Vec3f) o; + return x == vec3i.x && y == vec3i.y && z == vec3i.z; + } + + @Override + public int hashCode() { + return Objects.hash(x, y, z); + } +} diff --git a/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3i.java b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3i.java new file mode 100644 index 00000000..b3129aea --- /dev/null +++ b/io.fairyproject.platforms/mc-platform/src/main/java/io/fairyproject/mc/util/Vec3i.java @@ -0,0 +1,75 @@ +package io.fairyproject.mc.util; + +import java.util.Objects; + +public class Vec3i { + private int x; + private int y; + private int z; + + /** + * @param x X coordinate of the vector + * @param y Y coordinate of the vector + * @param z Z coordinate of the vector + */ + public Vec3i(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * @return Return X coordinate of the vector + */ + public int getX() { + return x; + } + + /** + * @param x Set X coordinate of the vector + */ + public void setX(int x) { + this.x = x; + } + + /** + * @return Return Y coordinate of the vector + */ + public int getY() { + return y; + } + + /** + * @param y Set Y coordinate of the vector + */ + public void setY(int y) { + this.y = y; + } + + /** + * @return Return Z coordinate of the vector + */ + public int getZ() { + return z; + } + + /** + * @param z Set Z coordinate of the vector + */ + public void setZ(int z) { + this.z = z; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Vec3i)) return false; + Vec3i vec3i = (Vec3i) o; + return x == vec3i.x && y == vec3i.y && z == vec3i.z; + } + + @Override + public int hashCode() { + return Objects.hash(x, y, z); + } +} diff --git a/settings.gradle b/settings.gradle index 2b9fabb2..e54cf6ec 100644 --- a/settings.gradle +++ b/settings.gradle @@ -51,11 +51,14 @@ void initModules() { include "io.fairyproject.modules:platform.mc:module.sidebar" include "io.fairyproject.modules:platform.mc:module.tablist" include "io.fairyproject.modules:platform.mc:module.bossbar:bukkit-bossbar" + include "io.fairyproject.modules:platform.mc:module.schematic:bukkit-schematic" + include "io.fairyproject.modules:platform.mc:module.nametag" + include "io.fairyproject.modules:platform.mc:module.protocol:mc-protocol" + include "io.fairyproject.modules:platform.mc:module.protocol:bukkit-protocol" include "io.fairyproject.modules:module.discord" include "io.fairyproject.modules:module.config" - include "io.fairyproject.modules:platform.mc:module.nametag" include "io.fairyproject.modules:module.command:core-command" include "io.fairyproject.modules:module.command:bukkit-command"