Skip to content

Commit

Permalink
1.19 support, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
ds58 committed Jun 7, 2022
1 parent ec41687 commit f8f82ef
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group = 'com.ruinscraft'
version = '1.7.6-booktest'
version = '1.7.7'
}

subprojects {
Expand Down
1 change: 1 addition & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation project(':panilla-craftbukkit-v1_17_R1')
implementation project(':panilla-craftbukkit-v1_18_R1')
implementation project(':panilla-craftbukkit-v1_18_R2')
implementation project(':panilla-craftbukkit-v1_19_R1')
compileOnly 'org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT' // use 1.13 Bukkit API
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ public int maxBookPages() {
packetInspector = new com.ruinscraft.panilla.craftbukkit.v1_18_R2.io.PacketInspector(this);
containerCleaner = new com.ruinscraft.panilla.craftbukkit.v1_18_R2.InventoryCleaner(this);
break imp;
case "v1_19_R1":
packetSerializerClass = com.ruinscraft.panilla.craftbukkit.v1_19_R1.io.dplx.PacketSerializer.class;
protocolConstants = new IProtocolConstants() {
@Override
public int maxBookPages() {
return 100;
}
};
playerInjector = new com.ruinscraft.panilla.craftbukkit.v1_19_R1.io.PlayerInjector();
packetInspector = new com.ruinscraft.panilla.craftbukkit.v1_19_R1.io.PacketInspector(this);
containerCleaner = new com.ruinscraft.panilla.craftbukkit.v1_19_R1.InventoryCleaner(this);
break imp;
}
default:
getLogger().warning("Unknown server implementation. " + Bukkit.getVersion() + " may not be supported by Panilla.");
Expand Down
4 changes: 4 additions & 0 deletions craftbukkit-v1_19_R1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
compileOnly project(':panilla-api')
compileOnly 'org.spigotmc:spigot:1.19-R0.1-SNAPSHOT'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ruinscraft.panilla.craftbukkit.v1_19_R1;

import com.ruinscraft.panilla.api.IInventoryCleaner;
import com.ruinscraft.panilla.api.IPanilla;
import com.ruinscraft.panilla.api.IPanillaPlayer;
import com.ruinscraft.panilla.api.exception.FailedNbt;
import com.ruinscraft.panilla.api.nbt.INbtTagCompound;
import com.ruinscraft.panilla.api.nbt.checks.NbtChecks;
import com.ruinscraft.panilla.craftbukkit.v1_19_R1.nbt.NbtTagCompound;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.inventory.Container;
import net.minecraft.world.item.ItemStack;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;

public class InventoryCleaner implements IInventoryCleaner {

private final IPanilla panilla;

public InventoryCleaner(IPanilla panilla) {
this.panilla = panilla;
}

@Override
public void clean(IPanillaPlayer player) {
CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
Container container = craftPlayer.getHandle().bT;

for (int slot = 0; slot < container.i.size(); slot++) {
ItemStack itemStack = container.b(slot).e();

if (itemStack == null || !itemStack.t()) {
continue;
}

NBTTagCompound nmsTag = itemStack.v();
INbtTagCompound tag = new NbtTagCompound(nmsTag);
String itemName = itemStack.c().a();

if (nmsTag == null || itemName == null) {
continue;
}

FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);

if (FailedNbt.failsThreshold(failedNbt)) {
container.b(slot).e().c((NBTTagCompound) null);
} else if (FailedNbt.fails(failedNbt)) {
nmsTag.r(failedNbt.key);
container.b(slot).e().c(nmsTag);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.ruinscraft.panilla.craftbukkit.v1_19_R1.io;

import com.ruinscraft.panilla.api.IPanilla;
import com.ruinscraft.panilla.api.IPanillaPlayer;
import com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException;
import com.ruinscraft.panilla.api.exception.FailedNbt;
import com.ruinscraft.panilla.api.exception.NbtNotPermittedException;
import com.ruinscraft.panilla.api.io.IPacketInspector;
import com.ruinscraft.panilla.api.nbt.INbtTagCompound;
import com.ruinscraft.panilla.api.nbt.checks.NbtChecks;
import com.ruinscraft.panilla.craftbukkit.v1_19_R1.nbt.NbtTagCompound;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.PacketPlayInSetCreativeSlot;
import net.minecraft.network.protocol.game.PacketPlayOutSetSlot;
import net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity;
import net.minecraft.network.protocol.game.PacketPlayOutWindowItems;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.EntityPlayer;
import net.minecraft.server.level.WorldServer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.EntityItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.UUID;

public class PacketInspector implements IPacketInspector {

private final IPanilla panilla;

public PacketInspector(IPanilla panilla) {
this.panilla = panilla;
}

@Override
public void checkPacketPlayInSetCreativeSlot(Object _packet) throws NbtNotPermittedException {
if (_packet instanceof PacketPlayInSetCreativeSlot) {
PacketPlayInSetCreativeSlot packet = (PacketPlayInSetCreativeSlot) _packet;

int slot = packet.b();
ItemStack itemStack = packet.c();

if (itemStack == null || !itemStack.t()) return;

NbtTagCompound tag = new NbtTagCompound(itemStack.v());
String itemClass = itemStack.c().getClass().getSimpleName();
String packetClass = packet.getClass().getSimpleName();

NbtChecks.checkPacketPlayIn(slot, tag, itemClass, packetClass, panilla);
}
}

@Override
public void checkPacketPlayOutSetSlot(Object _packet) throws NbtNotPermittedException {
if (_packet instanceof PacketPlayOutSetSlot) {
PacketPlayOutSetSlot packet = (PacketPlayOutSetSlot) _packet;

int windowId = packet.b();

// check if window is not player inventory and we are ignoring non-player inventories
if (windowId != 0 && panilla.getPConfig().ignoreNonPlayerInventories) {
return;
}

int slot = packet.c();

ItemStack itemStack = packet.d();

if (itemStack == null || !itemStack.t()) {
return;
}

NbtTagCompound tag = new NbtTagCompound(itemStack.v());
String itemClass = itemStack.getClass().getSimpleName();
String packetClass = packet.getClass().getSimpleName();

NbtChecks.checkPacketPlayOut(slot, tag, itemClass, packetClass, panilla);
}
}

@Override
public void checkPacketPlayOutWindowItems(Object _packet) throws NbtNotPermittedException {
if (_packet instanceof PacketPlayOutWindowItems) {
PacketPlayOutWindowItems packet = (PacketPlayOutWindowItems) _packet;

int windowId = packet.b();

// check if window is not player inventory
if (windowId != 0) {
return;
}

List<ItemStack> itemStacks = packet.c();

for (ItemStack itemStack : itemStacks) {
if (itemStack == null || !itemStack.t()) {
continue;
}

NbtTagCompound tag = new NbtTagCompound(itemStack.v());
String itemClass = itemStack.getClass().getSimpleName();
String packetClass = packet.getClass().getSimpleName();

NbtChecks.checkPacketPlayOut(0, tag, itemClass, packetClass, panilla); // TODO: set slot?
}
}
}

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws EntityNbtNotPermittedException {
if (_packet instanceof PacketPlayOutSpawnEntity) {
PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;

UUID entityId = packet.c();
Entity entity = null;

for (WorldServer worldServer : MinecraftServer.getServer().E()) {
entity = worldServer.P.d().a(entityId);
if (entity != null) break;
}

if (entity != null) {
if (entity instanceof EntityItem) {
EntityItem item = (EntityItem) entity;

if (item.h() == null) {
return;
}

if (!item.h().t()) {
return;
}

INbtTagCompound tag = new NbtTagCompound(item.h().v());
String itemName = item.h().c().a();
FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);

if (FailedNbt.fails(failedNbt)) {
throw new EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, entity.W().getWorld().getName());
}
}
}
}
}

@Override
public void sendPacketPlayOutSetSlotAir(IPanillaPlayer player, int slot) {
CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
EntityPlayer entityPlayer = craftPlayer.getHandle();

try {
Class<?> packetPlayOutSetSlotClass = Class.forName("net.minecraft.network.protocol.game.PacketPlayOutSetSlot");
Class<?>[] type = { int.class, int.class, int.class, ItemStack.class };
Constructor<?> constructor = packetPlayOutSetSlotClass.getConstructor(type);
Object[] params = { 0, 0, slot, new ItemStack(Blocks.a) };
Object packetPlayOutSetSlotInstance = constructor.newInstance(params);
entityPlayer.b.a((Packet<?>) packetPlayOutSetSlotInstance);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

@Override
public void stripNbtFromItemEntity(UUID entityId) {
Entity entity = null;

for (WorldServer worldServer : MinecraftServer.getServer().E()) {
entity = worldServer.P.d().a(entityId);
if (entity != null) break;
}

if (entity instanceof EntityItem) {
EntityItem item = (EntityItem) entity;
if (item.h() == null) return;
if (!item.h().t()) return;
item.h().c((NBTTagCompound) null);
}
}

@Override
public void stripNbtFromItemEntityLegacy(int entityId) {
throw new RuntimeException("cannot use #stripNbtFromItemEntityLegacy on 1.19");
}

@Override
public void validateBaseComponentParse(String string) throws Exception {
IChatBaseComponent.ChatSerializer.a(string);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ruinscraft.panilla.craftbukkit.v1_19_R1.io;

import com.ruinscraft.panilla.api.IPanillaPlayer;
import com.ruinscraft.panilla.api.io.IPlayerInjector;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import net.minecraft.network.PacketDecoder;
import net.minecraft.network.protocol.EnumProtocolDirection;
import net.minecraft.server.level.EntityPlayer;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;

import java.util.List;

public class PlayerInjector implements IPlayerInjector {

@Override
public Channel getPlayerChannel(IPanillaPlayer player) throws IllegalArgumentException {
CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
EntityPlayer entityPlayer = craftPlayer.getHandle();
return entityPlayer.b.b.m;
}

@Override
public int getCompressionLevel() {
return 256;
}

@Override
public ByteToMessageDecoder getDecompressor() {
return null;
}

@Override
public ByteToMessageDecoder getDecoder() {
return new PanillaPacketDecoder(EnumProtocolDirection.a);
}

private class PanillaPacketDecoder extends PacketDecoder {
public PanillaPacketDecoder(EnumProtocolDirection enumProtocolDirection) {
super(enumProtocolDirection);
}

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
try {
super.decode(channelHandlerContext, byteBuf, list);
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ruinscraft.panilla.craftbukkit.v1_19_R1.io.dplx;

import com.ruinscraft.panilla.api.io.IPacketSerializer;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.PacketDataSerializer;

public class PacketSerializer implements IPacketSerializer {

private final PacketDataSerializer handle;

public PacketSerializer(ByteBuf byteBuf) {
this.handle = new PacketDataSerializer(byteBuf);
}

@Override
public int readableBytes() {
return handle.readableBytes();
}

@Override
public int readVarInt() {
return handle.k();
}

@Override
public ByteBuf readBytes(int i) {
return handle.readBytes(i);
}

@Override
public ByteBuf readBytes(byte[] buffer) {
return handle.readBytes(buffer);
}

}
Loading

0 comments on commit f8f82ef

Please sign in to comment.