Backport whatever was from 1.17

This commit is contained in:
Telesphoreo 2022-03-03 19:54:57 -06:00
parent f4d42aab72
commit 7192c3d527
7 changed files with 116 additions and 55 deletions

View file

@ -10,7 +10,8 @@ import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.network.protocol.game.*;
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
import org.bukkit.plugin.java.JavaPlugin;
public class Blackout extends JavaPlugin
@ -21,6 +22,11 @@ public class Blackout extends JavaPlugin
@Getter
private PacketManager packetManager;
public static void debug(String message)
{
Blackout.getPlugin().getServer().getConsoleSender().sendMessage(Component.text("[Blackout Debug] ").color(NamedTextColor.GOLD).append(LegacyComponentSerializer.legacyAmpersand().deserialize(message)));
}
@Override
public void onLoad()
{
@ -39,9 +45,4 @@ public class Blackout extends JavaPlugin
this.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
}
public static void debug(String message)
{
Blackout.getPlugin().getServer().getConsoleSender().sendMessage(Component.text("[Blackout Debug] ").color(NamedTextColor.GOLD).append(LegacyComponentSerializer.legacyAmpersand().deserialize(message)));
}
}

View file

@ -2,13 +2,12 @@ package dev.plex.packet;
import com.google.common.collect.Maps;
import dev.plex.Blackout;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;
import net.minecraft.network.protocol.Packet;
import net.minecraft.world.entity.player.Player;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
public class PacketManager
{
@Getter
@ -23,7 +22,8 @@ public class PacketManager
public boolean callPacket(Player player, Packet<?> packet)
{
AtomicBoolean bool = new AtomicBoolean(true);
packetListenerMap.forEach((key, val) -> {
packetListenerMap.forEach((key, val) ->
{
if (key.isAssignableFrom(packet.getClass()))
{
bool.set(val.onReceive(player, packet));

View file

@ -2,6 +2,7 @@ package dev.plex.packet.impl;
import dev.plex.Blackout;
import dev.plex.packet.IPacketListener;
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
@ -12,28 +13,39 @@ import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PotionSplashEvent;
import java.util.concurrent.atomic.AtomicInteger;
public class EndermanPotionPatch implements IPacketListener<ClientboundUpdateMobEffectPacket>
{
@EventHandler
public void onPotionThrow(PotionSplashEvent event)
{
if (event.getAffectedEntities().isEmpty()) return;
if (event.getAffectedEntities().isEmpty())
{
return;
}
Blackout.debug("Hit entity");
if (event.getAffectedEntities().stream().noneMatch(affected -> affected.getType() == EntityType.ENDERMAN)) return;
if (event.getAffectedEntities().stream().noneMatch(affected -> affected.getType() == EntityType.ENDERMAN))
{
return;
}
Blackout.debug("Enderman");
ItemStack item = CraftItemStack.asNMSCopy(event.getPotion().getItem());
if (!item.hasTag()) return;
if (!item.hasTag())
{
return;
}
// Blackout.debug(item.getTag().getAsString());
AtomicInteger count = new AtomicInteger();
Blackout.debug(MobEffects.DAMAGE_BOOST.getDescriptionId());
if (!item.getTag().contains("CustomPotionEffects")) return;
if (!item.getTag().contains("CustomPotionEffects"))
{
return;
}
ListTag tag = (ListTag)item.getTag().get("CustomPotionEffects");
tag.forEach(effect -> {
tag.forEach(effect ->
{
CompoundTag compoundTag = (CompoundTag)effect;
Blackout.debug(compoundTag.getAsString());
if (compoundTag.getByte("Id") == 7)

View file

@ -1,6 +1,8 @@
package dev.plex.packet.impl;
import dev.plex.packet.IPacketListener;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
@ -14,21 +16,28 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.atomic.AtomicBoolean;
public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnPacket>
{
@EventHandler
public void onInventoryClick(InventoryClickEvent event)
{
if (event.getCurrentItem() == null) return;
if (event.getCurrentItem() == null)
{
return;
}
org.bukkit.inventory.@Nullable ItemStack bukkitItem = event.getCurrentItem();
if (bukkitItem.getType() != Material.KNOWLEDGE_BOOK) return;
if (bukkitItem.getType() != Material.KNOWLEDGE_BOOK)
{
return;
}
CraftItemStack craftItemStack = (CraftItemStack)bukkitItem;
ItemStack item = craftItemStack.handle;
if (!item.hasTag()) return;
if (!item.hasTag())
{
return;
}
CompoundTag tag = item.getTag();
assert tag != null;
if (tag.contains("Recipes"))
@ -36,7 +45,8 @@ public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnP
ListTag recipes = (ListTag)tag.get("Recipes");
AtomicBoolean remove = new AtomicBoolean();
assert recipes != null;
recipes.forEach(recipe -> {
recipes.forEach(recipe ->
{
if (!recipe.getAsString().startsWith("minecraft:"))
{
remove.set(true);
@ -47,7 +57,7 @@ public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnP
remove.set(true);
return;
}
if (!EnumUtils.isValidEnumIgnoreCase(Material.class, recipe.getAsString().split(":")[1]))
if (!EnumUtils.isValidEnum(Material.class, recipe.getAsString().split(":")[1].toUpperCase(Locale.ROOT)))
{
remove.set(true);
}
@ -63,14 +73,23 @@ public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnP
@EventHandler
public void onRightClick(PlayerInteractEvent event)
{
if (event.getItem() == null) return;
if (event.getItem() == null)
{
return;
}
org.bukkit.inventory.@Nullable ItemStack bukkitItem = event.getItem();
if (bukkitItem.getType() != Material.KNOWLEDGE_BOOK) return;
if (bukkitItem.getType() != Material.KNOWLEDGE_BOOK)
{
return;
}
CraftItemStack craftItemStack = (CraftItemStack)bukkitItem;
ItemStack item = craftItemStack.handle;
if (!item.hasTag()) return;
if (!item.hasTag())
{
return;
}
CompoundTag tag = item.getTag();
assert tag != null;
if (tag.contains("Recipes"))
@ -78,7 +97,8 @@ public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnP
ListTag recipes = (ListTag)tag.get("Recipes");
AtomicBoolean remove = new AtomicBoolean();
assert recipes != null;
recipes.forEach(recipe -> {
recipes.forEach(recipe ->
{
if (!recipe.getAsString().startsWith("minecraft:"))
{
remove.set(true);
@ -89,7 +109,7 @@ public class KnowledgeBookPatch implements IPacketListener<ServerboundUseItemOnP
remove.set(true);
return;
}
if (!EnumUtils.isValidEnumIgnoreCase(Material.class, recipe.getAsString().split(":")[1]))
if (!EnumUtils.isValidEnum(Material.class, recipe.getAsString().split(":")[1].toUpperCase(Locale.ROOT)))
{
remove.set(true);
}

View file

@ -2,6 +2,7 @@ package dev.plex.packet.impl;
import dev.plex.Blackout;
import dev.plex.packet.IPacketListener;
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
@ -14,8 +15,6 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.concurrent.atomic.AtomicInteger;
public class LecternPatch implements IPacketListener<ServerboundUseItemOnPacket>
{
@EventHandler
@ -24,17 +23,32 @@ public class LecternPatch implements IPacketListener<ServerboundUseItemOnPacket>
ItemStack item = ((CraftItemStack)event.getItemInHand()).handle;
Blackout.debug(item.getBukkitStack().getType().name());
Blackout.debug(String.valueOf(item.hasTag()));
if (item.getBukkitStack().getType() != Material.LECTERN) return;
if (!item.hasTag()) return;
if (item.getBukkitStack().getType() != Material.LECTERN)
{
return;
}
if (!item.hasTag())
{
return;
}
CompoundTag tag = item.getTag();
assert tag != null;
Blackout.debug(StringUtils.join(tag.getAllKeys(), ", "));
if (!tag.contains("BlockEntityTag")) return;
if (!tag.getCompound("BlockEntityTag").contains("Book")) return;
if (!tag.getCompound("BlockEntityTag").getCompound("Book").contains("tag")) return;
if (!tag.contains("BlockEntityTag"))
{
return;
}
if (!tag.getCompound("BlockEntityTag").contains("Book"))
{
return;
}
if (!tag.getCompound("BlockEntityTag").getCompound("Book").contains("tag"))
{
return;
}
CompoundTag bookTag = tag.getCompound("BlockEntityTag").getCompound("Book").getCompound("tag");
ListTag listTag = (ListTag)bookTag.get("pages");
assert listTag != null;

View file

@ -15,8 +15,14 @@ public class PaintingPatch implements IPacketListener<ServerboundUseItemOnPacket
public boolean onReceive(Player player, ServerboundUseItemOnPacket serverboundUseItemOnPacket)
{
ItemStack item = player.getItemInHand(serverboundUseItemOnPacket.getHand());
if (!item.hasTag()) return true;
if (item.getBukkitStack().getType() != Material.PAINTING) return true;
if (!item.hasTag())
{
return true;
}
if (item.getBukkitStack().getType() != Material.PAINTING)
{
return true;
}
CompoundTag tag = item.getTag();
assert tag != null;

View file

@ -11,14 +11,20 @@ import org.bukkit.entity.Player;
public class PacketInterceptorUtil
{
public static void inject(Player player) {
ChannelDuplexHandler handler = new ChannelDuplexHandler() {
public static void inject(Player player)
{
ChannelDuplexHandler handler = new ChannelDuplexHandler()
{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
if (msg instanceof Packet<?> packet)
{
boolean read = Blackout.getPlugin().getPacketManager().callPacket(((CraftPlayer)player).getHandle(), packet);
if (!read) return;
if (!read)
{
return;
}
}
super.channelRead(ctx, msg);
}
@ -28,9 +34,11 @@ public class PacketInterceptorUtil
pipeline.addBefore("packet_handler", player.getName(), handler);
}
public static void eject(Player player) {
public static void eject(Player player)
{
Channel channel = ((CraftPlayer)player).getHandle().connection.connection.channel;
channel.eventLoop().submit(() -> {
channel.eventLoop().submit(() ->
{
channel.pipeline().remove(player.getName());
});
}