Skip to content

Commit

Permalink
feat: add PromptSign
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Jul 16, 2024
1 parent 0bbb666 commit 540c800
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.11.2"
version = "6.12.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.mori01231.lifecore.event

import net.minecraft.server.v1_15_R1.BlockPosition
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
import org.bukkit.event.Event
import org.bukkit.event.HandlerList

/**
* Called when a player tries to update a sign.
* @param player The player who tried to update the sign.
* @param pos The position of the sign.
* @param lines The lines of the sign.
*/
data class AsyncPreSignChangeEvent(
val player: Player,
val pos: Location,
val lines: List<String>,
) : Event(true), Cancellable {
private var cancelled = false

override fun getHandlers() = handlerList

companion object {
@JvmStatic
@get:JvmName("getHandlerList")
val handlerList = HandlerList()
}

override fun isCancelled(): Boolean = cancelled

override fun setCancelled(cancel: Boolean) {
this.cancelled = cancel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEntityEvent;
import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEvent;
import com.github.mori01231.lifecore.event.AsyncPreSignChangeEvent;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.PacketPlayInUpdateSign;
import net.minecraft.server.v1_15_R1.PacketPlayInUseEntity;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class PacketHandler extends ChannelDuplexHandler {
private final Player player;
private final EntityPlayer entityPlayer;
Expand All @@ -33,6 +40,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
return;
}
}
if (msg instanceof PacketPlayInUpdateSign) {
PacketPlayInUpdateSign packet = (PacketPlayInUpdateSign) msg;
Location location = new Location(player.getWorld(), packet.b().getX(), packet.b().getY(), packet.b().getZ());
List<String> lines = Arrays.asList(packet.c());
if (new AsyncPreSignChangeEvent(player, location, lines).callEvent()) {
super.channelRead(ctx, msg);
}
return;
}
super.channelRead(ctx, msg);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/util/PromptSign.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.mori01231.lifecore.util

import net.minecraft.server.v1_15_R1.BlockPosition
import net.minecraft.server.v1_15_R1.PacketPlayOutOpenSignEditor
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer
import org.bukkit.entity.Player
import java.util.*
import java.util.concurrent.ConcurrentHashMap

object PromptSign {

private val awaitingSign = ConcurrentHashMap<UUID, (List<String>) -> Unit>()

@JvmStatic
fun promptSign(player: Player, action: (List<String>) -> Unit) {
val loc0 = player.location.clone().apply { y = 0.0 }
val origBlockData = loc0.block.blockData
player.sendBlockChange(loc0, Material.AIR.createBlockData())
player.sendBlockChange(loc0, Material.OAK_SIGN.createBlockData())
awaitingSign[player.uniqueId] = {
player.sendBlockChange(loc0, origBlockData)
action(it)
}
(player as CraftPlayer).handle.playerConnection
.sendPacket(PacketPlayOutOpenSignEditor(BlockPosition(loc0.blockX, loc0.blockY, loc0.blockZ)))
}
}

0 comments on commit 540c800

Please sign in to comment.