place building preview 1.21.4
This commit is contained in:
		| @ -5,13 +5,14 @@ import com.comphenix.protocol.ProtocolLibrary | ||||
| import com.comphenix.protocol.events.PacketContainer | ||||
| import com.comphenix.protocol.wrappers.* | ||||
| import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry | ||||
| import com.destroystokyo.paper.MaterialSetTag | ||||
| import com.pobnellion.aoe.Aoe | ||||
| import com.sk89q.worldedit.bukkit.BukkitAdapter | ||||
| import com.sk89q.worldedit.extent.clipboard.Clipboard | ||||
| import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats | ||||
| import org.bukkit.Bukkit | ||||
| import org.bukkit.Location | ||||
| import org.bukkit.Material | ||||
| import org.bukkit.block.data.BlockData | ||||
| import org.bukkit.entity.EntityType | ||||
| import org.bukkit.entity.Player | ||||
| import org.bukkit.event.EventHandler | ||||
| @ -74,6 +75,10 @@ class PlaceHint { | ||||
|             playerHints.clear() | ||||
|         } | ||||
|  | ||||
|         fun contains(player: Player): Boolean { | ||||
|             return playerHints.containsKey(player.uniqueId) | ||||
|         } | ||||
|  | ||||
|         private fun createOutlineTeam(player: Player) { | ||||
|             val packet = PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM) | ||||
|             packet.strings.write(0, "placeValid") | ||||
| @ -88,21 +93,32 @@ class PlaceHint { | ||||
|             if (!playerHints.containsKey(event.player.uniqueId)) | ||||
|                 return | ||||
|  | ||||
|             val block = event.player.getTargetBlockExact(MAX_TARGET_DISTANCE); | ||||
|             val target = event.player.getTargetBlockExact(MAX_TARGET_DISTANCE); | ||||
|             val block = if (target == null) null else event.player.getTargetBlock(MaterialSetTag.REPLACEABLE.values, MAX_TARGET_DISTANCE); | ||||
|             playerHints[event.player.uniqueId]!!.moveTo(block?.location) | ||||
|         } | ||||
|  | ||||
|         @EventHandler | ||||
|         fun onLeftClick(event: PlayerInteractEvent) { | ||||
|             if (!playerHints.containsKey(event.player.uniqueId) || (event.action != Action.LEFT_CLICK_BLOCK && event.action != Action.LEFT_CLICK_AIR)) | ||||
|         fun onClick(event: PlayerInteractEvent) { | ||||
|             if (!playerHints.containsKey(event.player.uniqueId)) | ||||
|                 return | ||||
|  | ||||
|             if (!playerHints[event.player.uniqueId]!!.isValid) | ||||
|                 return | ||||
|             event.isCancelled = true | ||||
|  | ||||
|             val marker = playerHints[event.player.uniqueId]!! | ||||
|             marker.onConfirm(marker.currentLocation!!, marker.sizeX, marker.sizeY, marker.sizeZ) | ||||
|             remove(event.player) | ||||
|             // left click - place | ||||
|             if (event.action == Action.LEFT_CLICK_BLOCK || event.action == Action.LEFT_CLICK_AIR) { | ||||
|                 if (!playerHints[event.player.uniqueId]!!.isValid) | ||||
|                     return | ||||
|  | ||||
|                 val marker = playerHints[event.player.uniqueId]!! | ||||
|                 marker.onConfirm(marker.currentLocation!!, marker.sizeX, marker.sizeY, marker.sizeZ) | ||||
|                 remove(event.player) | ||||
|             } | ||||
|  | ||||
|             // right click - cancel | ||||
|             if (event.action == Action.RIGHT_CLICK_BLOCK || event.action == Action.RIGHT_CLICK_AIR) { | ||||
|                 remove(event.player) | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private val teamParamsTemplate: WrappedTeamParameters = WrappedTeamParameters.newBuilder() | ||||
| @ -153,16 +169,17 @@ class PlaceHint { | ||||
|  | ||||
|         val format = ClipboardFormats.findByFile(file) | ||||
|         val reader = format?.getReader(FileInputStream(file)) | ||||
|         val clipboard = reader?.read() | ||||
|         val clipboard = reader?.read() ?: throw NullPointerException("Could not load schematic ${file.path}. Is it present?") | ||||
|  | ||||
|         this.schematic = clipboard | ||||
|         this.sizeX = clipboard!!.dimensions.x().toFloat() | ||||
|         this.sizeX = clipboard.dimensions.x().toFloat() | ||||
|         this.sizeY = clipboard.dimensions.y().toFloat() | ||||
|         this.sizeZ = clipboard.dimensions.z().toFloat() | ||||
|     } | ||||
|  | ||||
|     private fun show(initialLocation: Location) { | ||||
|         initialLocation.add(-floor(sizeX / 2.0), 1.0, -floor(sizeZ / 2.0)) | ||||
|         currentLocation = initialLocation; | ||||
|  | ||||
|         val spawnMarkerPacket = PacketContainer(PacketType.Play.Server.SPAWN_ENTITY) | ||||
|         spawnMarkerPacket.integers.write(0, marker.first) | ||||
| @ -173,17 +190,19 @@ class PlaceHint { | ||||
|             .write(1, initialLocation.y) | ||||
|             .write(2, initialLocation.z) | ||||
|  | ||||
|         base = spawnBlockDisplay(initialLocation, Material.GRAY_STAINED_GLASS, scale = Vector3f(sizeX + 0.05f, 0.2f, sizeZ + 0.05f)) | ||||
|         base = spawnBlockDisplay(initialLocation, Material.GRAY_STAINED_GLASS.createBlockData(), | ||||
|             scale = Vector3f(sizeX + 0.1f, 0.2f, sizeZ + 0.1f), offset = Vector3f(-0.05f, -0.05f, -0.05f)) | ||||
|  | ||||
|         schematic?.iterator()?.forEach { | ||||
|             val material = BukkitAdapter.adapt(schematic.getBlock(it)).material | ||||
|         schematic?.iterator()?.forEach { blockPosition -> | ||||
|             val block = BukkitAdapter.adapt(schematic.getBlock(blockPosition)) | ||||
|  | ||||
|             val x = (it.x() - schematic.minimumPoint.x()).toFloat() | ||||
|             val y = (it.y() - schematic.minimumPoint.y()).toFloat() | ||||
|             val z = (it.z() - schematic.minimumPoint.z()).toFloat() | ||||
|             if (!block.material.isAir && !block.material.isEmpty) { | ||||
|                 val x = (blockPosition.x() - schematic.minimumPoint.x()).toFloat() | ||||
|                 val y = (blockPosition.y() - schematic.minimumPoint.y()).toFloat() | ||||
|                 val z = (blockPosition.z() - schematic.minimumPoint.z()).toFloat() | ||||
|  | ||||
|             if (!material.isAir && !material.isEmpty) | ||||
|                 displayIds.add(spawnBlockDisplay(initialLocation, material, offset = Vector3f(x, y, z), options = 0b01000000)) | ||||
|                 displayIds.add(spawnBlockDisplay(initialLocation, block, offset = Vector3f(x, y, z), options = 0b01000000)) | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         val outlineTeamPacket = PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM) | ||||
| @ -204,8 +223,6 @@ class PlaceHint { | ||||
|  | ||||
|         isValid = validate(initialLocation, sizeX, sizeY, sizeZ) | ||||
|         updateColour() | ||||
|  | ||||
|         currentLocation = initialLocation; | ||||
|     } | ||||
|  | ||||
|     private fun hide() { | ||||
| @ -234,10 +251,10 @@ class PlaceHint { | ||||
|  | ||||
|         newLocation.add(-floor(sizeX / 2.0), 1.0, -floor(sizeZ / 2.0)) | ||||
|  | ||||
|         val basePacket = PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT) | ||||
|         val basePacket = PacketContainer(PacketType.Play.Server.ENTITY_POSITION_SYNC) | ||||
|         basePacket.integers.write(0, marker.first) | ||||
|  | ||||
|         basePacket.doubles | ||||
|         basePacket.structures.values[0].structures.values[0].doubles | ||||
|             .write(0, newLocation.x) | ||||
|             .write(1, newLocation.y) | ||||
|             .write(2, newLocation.z) | ||||
| @ -261,7 +278,7 @@ class PlaceHint { | ||||
|  | ||||
|     private fun spawnBlockDisplay( | ||||
|         location: Location, | ||||
|         material: Material, | ||||
|         block: BlockData, | ||||
|         scale: Vector3f = Vector3f(1f, 1f, 1f), | ||||
|         offset: Vector3f = Vector3f(0f, 0f, 0f), | ||||
|         options: Byte = 0b00000000 | ||||
| @ -282,14 +299,14 @@ class PlaceHint { | ||||
|         dataPacket.integers.write(0, id) | ||||
|  | ||||
|         // woated | ||||
|         val blockData = BukkitConverters | ||||
|             .getWrappedBlockDataConverter() | ||||
|             .getGeneric(WrappedBlockData.createData(material)) | ||||
|         val blockData = BukkitConverters.getWrappedBlockDataConverter() | ||||
|             .getGeneric(WrappedBlockData.createData(block)) | ||||
|  | ||||
|         val watcher = WrappedDataWatcher() | ||||
|         watcher.setObject(0, Registry.get(Byte::class.javaObjectType), options) | ||||
|         watcher.setObject(11, Registry.get(Vector3f::class.javaObjectType), offset) | ||||
|         watcher.setObject(12, Registry.get(Vector3f::class.javaObjectType), scale) | ||||
|         watcher.setObject(16, Registry.get(Int::class.javaObjectType), 200) | ||||
|         watcher.setObject(23, Registry.getBlockDataSerializer(false), blockData) | ||||
|  | ||||
|         dataPacket.dataValueCollectionModifier.write(0, watcher.watchableObjects | ||||
|  | ||||
| @ -14,6 +14,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent | ||||
| import org.bukkit.event.inventory.InventoryDragEvent | ||||
| import org.bukkit.event.player.PlayerInteractEvent | ||||
| import org.bukkit.inventory.ItemStack | ||||
| import java.util.* | ||||
|  | ||||
|  | ||||
| class PlaceItem : Listener { | ||||
| @ -26,7 +27,9 @@ class PlaceItem : Listener { | ||||
|  | ||||
|     @EventHandler | ||||
|     fun onRightClickItem(event: PlayerInteractEvent) { | ||||
|         if (event.material != Material.BRICKS || (event.action != Action.RIGHT_CLICK_AIR && event.action != Action.RIGHT_CLICK_BLOCK)) | ||||
|         if (event.material != Material.BRICKS | ||||
|             || (event.action != Action.RIGHT_CLICK_AIR && event.action != Action.RIGHT_CLICK_BLOCK) | ||||
|             || PlaceHint.contains(event.player)) | ||||
|             return | ||||
|  | ||||
|         event.isCancelled = true | ||||
|  | ||||
		Reference in New Issue
	
	Block a user