Skip to content

Commit

Permalink
1.16.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ds58 committed Aug 18, 2020
1 parent 7041ec6 commit e959820
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 0 deletions.
1 change: 1 addition & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation project(':panilla-craftbukkit-v1_14_R1')
implementation project(':panilla-craftbukkit-v1_15_R1')
implementation project(':panilla-craftbukkit-v1_16_R1')
implementation project(':panilla-craftbukkit-v1_16_R2')
implementation project(':panilla-glowstone-r2018_9_0')
compileOnly 'org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT' // use 1.12 Bukkit API
}
Expand Down
4 changes: 4 additions & 0 deletions craftbukkit-v1_16_R2/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.16.2-R0.1-SNAPSHOT'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ruinscraft.panilla.craftbukkit.v1_16_R2;

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_16_R2.nbt.NbtTagCompound;
import net.minecraft.server.v1_16_R2.Container;
import net.minecraft.server.v1_16_R2.ItemStack;
import net.minecraft.server.v1_16_R2.NBTTagCompound;
import org.bukkit.craftbukkit.v1_16_R2.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().activeContainer;

for (int slot = 0; slot < container.slots.size(); slot++) {
ItemStack itemStack = container.getSlot(slot).getItem();

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

NBTTagCompound nmsTag = itemStack.getTag();
INbtTagCompound tag = new NbtTagCompound(nmsTag);
String itemName = itemStack.getItem().getName();
FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);

if (FailedNbt.failsThreshold(failedNbt)) {
container.getSlot(slot).getItem().setTag(null);
} else if (FailedNbt.fails(failedNbt)) {
nmsTag.remove(failedNbt.key);
container.getSlot(slot).getItem().setTag(nmsTag);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.ruinscraft.panilla.craftbukkit.v1_16_R2.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_16_R2.nbt.NbtTagCompound;
import net.minecraft.server.v1_16_R2.*;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer;

import java.lang.reflect.Field;
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.getItemStack();

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

NbtTagCompound tag = new NbtTagCompound(itemStack.getTag());
String itemClass = itemStack.getItem().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;

try {
Field windowIdField = PacketPlayOutSetSlot.class.getDeclaredField("a");

windowIdField.setAccessible(true);

int windowId = (int) windowIdField.get(packet);

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

Field slotField = PacketPlayOutSetSlot.class.getDeclaredField("b");
Field itemStackField = PacketPlayOutSetSlot.class.getDeclaredField("c");

slotField.setAccessible(true);
itemStackField.setAccessible(true);

int slot = (int) slotField.get(packet);
ItemStack itemStack = (ItemStack) itemStackField.get(packet);

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

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

NbtChecks.checkPacketPlayOut(slot, tag, itemClass, packetClass, panilla);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

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

try {
Field windowIdField = PacketPlayOutWindowItems.class.getDeclaredField("a");

windowIdField.setAccessible(true);

int windowId = (int) windowIdField.get(packet);

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

Field itemStacksField = PacketPlayOutWindowItems.class.getDeclaredField("b");

itemStacksField.setAccessible(true);

List<ItemStack> itemStacks = (List<ItemStack>) itemStacksField.get(packet);

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

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

NbtChecks.checkPacketPlayOut(0, tag, itemClass, packetClass, panilla); // TODO: set slot?
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

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

try {
Field typeField = PacketPlayOutSpawnEntity.class.getDeclaredField("b");

typeField.setAccessible(true);

UUID entityId = (UUID) typeField.get(packet);
org.bukkit.entity.Entity bukkitEntity = org.bukkit.Bukkit.getEntity(entityId);
CraftEntity craftEntity = (CraftEntity) bukkitEntity;

if (craftEntity == null) {
return;
}

Entity entity = craftEntity.getHandle();

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

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

if (!item.getItemStack().hasTag()) {
return;
}

INbtTagCompound tag = new NbtTagCompound(item.getItemStack().getTag());
String itemName = item.getItemStack().getItem().getName();
FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);

if (FailedNbt.fails(failedNbt)) {
throw new EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, bukkitEntity.getWorld().getName());
}
}
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

@Override
public void sendPacketPlayOutSetSlotAir(IPanillaPlayer player, int slot) {
CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
EntityPlayer entityPlayer = craftPlayer.getHandle();
PacketPlayOutSetSlot packet = new PacketPlayOutSetSlot(0, slot, new ItemStack(Blocks.AIR));
entityPlayer.playerConnection.sendPacket(packet);
}

@Override
public void stripNbtFromItemEntity(UUID entityId) {
org.bukkit.entity.Entity bukkitEntity = Bukkit.getServer().getEntity(entityId);

if (bukkitEntity instanceof CraftEntity) {
CraftEntity craftEntity = (CraftEntity) bukkitEntity;
Entity entity = craftEntity.getHandle();

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

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

if (!item.getItemStack().hasTag()) {
return;
}

item.getItemStack().setTag(null);
}
}
}

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

@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,30 @@
package com.ruinscraft.panilla.craftbukkit.v1_16_R2.io;

import com.ruinscraft.panilla.api.IPanillaPlayer;
import com.ruinscraft.panilla.api.io.IPlayerInjector;
import io.netty.channel.Channel;
import io.netty.handler.codec.ByteToMessageDecoder;
import net.minecraft.server.v1_16_R2.EntityPlayer;
import net.minecraft.server.v1_16_R2.PacketDecompressor;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer;

public class PlayerInjector implements IPlayerInjector {

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

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

@Override
public ByteToMessageDecoder getDecompressor() {
return new PacketDecompressor(getCompressionLevel());
}

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

import com.ruinscraft.panilla.api.io.IPacketSerializer;
import io.netty.buffer.ByteBuf;
import net.minecraft.server.v1_16_R2.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.i();
}

@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 e959820

Please sign in to comment.