From 51b6ae7010c6efb9f7147805543a46720251ae36 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Wed, 6 Mar 2024 16:05:27 +0800 Subject: [PATCH] Integrate entity editing with PlotSquared --- .../axiom/blueprint/BlueprintHeader.java | 3 +- .../packet/DeleteEntityPacketListener.java | 10 +++++- .../ManipulateEntityPacketListener.java | 35 +++++++++++++++++-- .../packet/SpawnEntityPacketListener.java | 9 ++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/blueprint/BlueprintHeader.java b/src/main/java/com/moulberry/axiom/blueprint/BlueprintHeader.java index cb4e126..48936a7 100644 --- a/src/main/java/com/moulberry/axiom/blueprint/BlueprintHeader.java +++ b/src/main/java/com/moulberry/axiom/blueprint/BlueprintHeader.java @@ -23,7 +23,8 @@ public record BlueprintHeader(String name, String author, List tags, flo public static BlueprintHeader read(FriendlyByteBuf friendlyByteBuf) { String name = friendlyByteBuf.readUtf(); String author = friendlyByteBuf.readUtf(); - List tags = friendlyByteBuf.readList(FriendlyByteBuf::readUtf); + List tags = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), + FriendlyByteBuf::readUtf); int blockCount = friendlyByteBuf.readInt(); return new BlueprintHeader(name, author, tags, 0, 0, true, blockCount); } diff --git a/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java index 2fe253c..bc7c9f6 100644 --- a/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java @@ -1,6 +1,7 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; +import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -20,6 +21,7 @@ import org.bukkit.plugin.messaging.PluginMessageListener; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -45,7 +47,8 @@ public class DeleteEntityPacketListener implements PluginMessageListener { } FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); - List delete = friendlyByteBuf.readList(FriendlyByteBuf::readUUID); + List delete = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), + FriendlyByteBuf::readUUID); ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle(); @@ -61,6 +64,11 @@ public class DeleteEntityPacketListener implements PluginMessageListener { if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(type)) continue; if (blacklistedEntities.contains(type)) continue; + if (!PlotSquaredIntegration.canBreakBlock(player, + player.getWorld().getBlockAt(entity.getBlockX(), entity.getBlockY(), entity.getBlockZ()))) { + continue; + } + entity.remove(Entity.RemovalReason.DISCARDED); } } diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index 0cb5769..29a66a9 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -2,6 +2,7 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; +import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -17,12 +18,14 @@ import net.minecraft.world.entity.decoration.ItemFrame; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Rotation; import net.minecraft.world.phys.Vec3; +import org.bukkit.Location; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.UUID; @@ -63,7 +66,8 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { PassengerManipulation passengerManipulation = friendlyByteBuf.readEnum(PassengerManipulation.class); List passengers = List.of(); if (passengerManipulation == PassengerManipulation.ADD_LIST || passengerManipulation == PassengerManipulation.REMOVE_LIST) { - passengers = friendlyByteBuf.readList(FriendlyByteBuf::readUUID); + passengers = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), + FriendlyByteBuf::readUUID); } return new ManipulateEntry(uuid, relativeMovementSet, position, yaw, pitch, nbt, @@ -88,7 +92,8 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { } FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); - List entries = friendlyByteBuf.readList(ManipulateEntry::read); + List entries = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), + ManipulateEntry::read); ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle(); @@ -104,6 +109,14 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(type)) continue; if (blacklistedEntities.contains(type)) continue; + Vec3 position = entity.position(); + BlockPos containing = BlockPos.containing(position.x, position.y, position.z); + + if (!PlotSquaredIntegration.canPlaceBlock(player, new Location(player.getWorld(), + containing.getX(), containing.getY(), containing.getZ()))) { + continue; + } + if (entry.merge != null && !entry.merge.isEmpty()) { NbtSanitization.sanitizeEntity(entry.merge); @@ -112,6 +125,8 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { entity.load(compoundTag); } + entity.setPosRaw(position.x, position.y, position.z); + Vec3 entryPos = entry.position(); if (entryPos != null && entry.relativeMovementSet != null) { double newX = entry.relativeMovementSet.contains(RelativeMovement.X) ? entity.position().x + entryPos.x : entryPos.x; @@ -130,7 +145,13 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { } } - entity.teleportTo(serverLevel, newX, newY, newZ, Set.of(), newYaw, newPitch); + containing = BlockPos.containing(newX, newY, newZ); + + if (PlotSquaredIntegration.canPlaceBlock(player, new Location(player.getWorld(), + containing.getX(), containing.getY(), containing.getZ()))) { + entity.teleportTo(serverLevel, newX, newY, newZ, Set.of(), newYaw, newPitch); + } + entity.setYHeadRot(newYaw); } @@ -154,6 +175,14 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { continue; } + position = passenger.position(); + containing = BlockPos.containing(position.x, position.y, position.z); + + if (!PlotSquaredIntegration.canPlaceBlock(player, new Location(player.getWorld(), + containing.getX(), containing.getY(), containing.getZ()))) { + continue; + } + passenger.startRiding(entity, true); } } diff --git a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java index ea6f385..5c34fd1 100644 --- a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; import com.moulberry.axiom.event.AxiomTeleportEvent; import com.moulberry.axiom.event.AxiomUnknownTeleportEvent; +import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -31,6 +32,7 @@ import org.bukkit.plugin.messaging.PluginMessageListener; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -67,7 +69,7 @@ public class SpawnEntityPacketListener implements PluginMessageListener { } FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); - List entries = friendlyByteBuf.readList(SpawnEntry::new); + List entries = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), SpawnEntry::new); ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle(); @@ -82,6 +84,11 @@ public class SpawnEntityPacketListener implements PluginMessageListener { continue; } + if (!PlotSquaredIntegration.canPlaceBlock(player, new Location(player.getWorld(), + blockPos.getX(), blockPos.getY(), blockPos.getZ()))) { + continue; + } + if (serverLevel.getEntity(entry.newUuid) != null) continue; CompoundTag tag = entry.tag == null ? new CompoundTag() : entry.tag;