From cc819adefe095508834ede08d3fa7da9b3e45da8 Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Mon, 20 Nov 2023 11:44:04 +0900 Subject: [PATCH] feat: add "petpickup" town config --- build.gradle.kts | 2 +- .../mori01231/lifecore/config/TownConfig.kt | 1 + .../AsyncPlayerPreInteractEntityEvent.java | 53 +++++++++++++++++++ .../event/AsyncPlayerPreInteractEvent.java | 4 ++ .../listener/CancelPetClickListener.java | 4 +- .../lifecore/listener/TownSpecificListener.kt | 25 +++++++++ .../lifecore/network/PacketHandler.java | 4 +- 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEntityEvent.java diff --git a/build.gradle.kts b/build.gradle.kts index 2478633..3dff052 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.azisaba" -version = "6.7.12" +version = "6.8.0" java { toolchain.languageVersion.set(JavaLanguageVersion.of(8)) diff --git a/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt b/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt index 980236f..702fd85 100644 --- a/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt +++ b/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt @@ -8,6 +8,7 @@ import org.bukkit.Location data class TownConfig( @JvmField var allowInvisibility: Boolean = true, @JvmField var allowSit: Boolean = true, + @JvmField var allowPetPickup: Boolean = true, ) { companion object { fun getTownConfigAt(lifeCoreConfig: LifeCoreConfig, location: Location): TownConfig? { diff --git a/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEntityEvent.java b/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEntityEvent.java new file mode 100644 index 0000000..20e577d --- /dev/null +++ b/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEntityEvent.java @@ -0,0 +1,53 @@ +package com.github.mori01231.lifecore.event; + +import net.minecraft.server.v1_15_R1.Entity; +import net.minecraft.server.v1_15_R1.PacketPlayInUseEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +public class AsyncPlayerPreInteractEntityEvent extends PlayerEvent implements Cancellable { + private static final HandlerList HANDLER_LIST = new HandlerList(); + private final PacketPlayInUseEntity.EnumEntityUseAction action; + private final Entity interactedEntity; + private boolean cancelled = false; + + public AsyncPlayerPreInteractEntityEvent(@NotNull Player player, @NotNull PacketPlayInUseEntity.EnumEntityUseAction action, @NotNull Entity interactedEntity) { + super(player, true); + this.action = action; + this.interactedEntity = interactedEntity; + } + + @NotNull + public PacketPlayInUseEntity.EnumEntityUseAction getAction() { + return action; + } + + @NotNull + public Entity getInteractedEntity() { + return interactedEntity; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + @SuppressWarnings("unused") // required for bukkit + @NotNull + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEvent.java b/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEvent.java index a30eb88..36f0ca8 100644 --- a/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEvent.java +++ b/src/main/java/com/github/mori01231/lifecore/event/AsyncPlayerPreInteractEvent.java @@ -8,6 +8,10 @@ import org.bukkit.event.player.PlayerEvent; import org.jetbrains.annotations.NotNull; +/** + * @deprecated misspelled + */ +@Deprecated public class AsyncPlayerPreInteractEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private final PacketPlayInUseEntity.EnumEntityUseAction action; diff --git a/src/main/java/com/github/mori01231/lifecore/listener/CancelPetClickListener.java b/src/main/java/com/github/mori01231/lifecore/listener/CancelPetClickListener.java index a853f11..883fef1 100644 --- a/src/main/java/com/github/mori01231/lifecore/listener/CancelPetClickListener.java +++ b/src/main/java/com/github/mori01231/lifecore/listener/CancelPetClickListener.java @@ -1,7 +1,7 @@ package com.github.mori01231.lifecore.listener; import com.github.mori01231.lifecore.config.PetClickFile; -import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEvent; +import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEntityEvent; import de.Keyle.MyPet.MyPetApi; import de.Keyle.MyPet.api.entity.MyPet; import de.Keyle.MyPet.api.entity.MyPetBukkitEntity; @@ -12,7 +12,7 @@ public class CancelPetClickListener implements Listener { @EventHandler - public void onClickEntity(AsyncPlayerPreInteractEvent e) { + public void onClickEntity(AsyncPlayerPreInteractEntityEvent e) { if (!PetClickFile.isDisabled(e.getPlayer().getUniqueId()) && e.getPlayer().isSneaking()) { return; } diff --git a/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt b/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt index 72f9ecb..48e756e 100644 --- a/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt +++ b/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt @@ -3,16 +3,26 @@ package com.github.mori01231.lifecore.listener import com.github.mori01231.lifecore.LifeCore import com.github.mori01231.lifecore.config.TownConfig import com.github.mori01231.lifecore.util.runTaskTimer +import de.Keyle.MyPet.MyPetApi +import de.Keyle.MyPet.api.skill.skills.Backpack +import de.Keyle.MyPet.api.skill.skills.Pickup import org.bukkit.Bukkit import org.bukkit.ChatColor import org.bukkit.entity.LivingEntity +import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.entity.PotionSplashEvent import org.bukkit.event.player.PlayerCommandPreprocessEvent import org.bukkit.potion.PotionEffectType +import java.lang.reflect.Field class TownSpecificListener(private val plugin: LifeCore) : Listener { + private val pickupPickupField: Field = + Class.forName("de.Keyle.MyPet.skill.skills.PickupImpl") + .getDeclaredField("pickup") + .apply { isAccessible = true } + fun startTask() { Bukkit.getScheduler().runTaskTimer(plugin, 20, 20) { Bukkit.getOnlinePlayers().forEach { player -> @@ -24,6 +34,21 @@ class TownSpecificListener(private val plugin: LifeCore) : Listener { player.removePotionEffect(PotionEffectType.INVISIBILITY) player.sendMessage("${ChatColor.RED}この町での透明化は許可されていません。") } + if (Bukkit.getPluginManager().isPluginEnabled("MyPet")) { + runMyPet(config, player) + } + } + } + } + + private fun runMyPet(config: TownConfig, player: Player) { + if (!config.allowPetPickup) { + MyPetApi.getMyPetManager().getMyPet(player)?.let { pet -> + val skill = pet.skills.get(Pickup::class.java) + if (skill.isActive && pet.skills.isActive(Backpack::class.java) && (pickupPickupField[skill] as Boolean)) { + player.sendMessage("${ChatColor.RED}この町でのpetpickupは許可されていません。") + skill.activate() // this toggles the "pickup" status and sends a message to the player + } } } } diff --git a/src/main/java/com/github/mori01231/lifecore/network/PacketHandler.java b/src/main/java/com/github/mori01231/lifecore/network/PacketHandler.java index 7bdf4e7..772c5d1 100644 --- a/src/main/java/com/github/mori01231/lifecore/network/PacketHandler.java +++ b/src/main/java/com/github/mori01231/lifecore/network/PacketHandler.java @@ -1,5 +1,6 @@ package com.github.mori01231.lifecore.network; +import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEntityEvent; import com.github.mori01231.lifecore.event.AsyncPlayerPreInteractEvent; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; @@ -25,7 +26,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception PacketPlayInUseEntity packet = (PacketPlayInUseEntity) msg; Entity entity = packet.a(entityPlayer.world); if (entity != null) { - if (new AsyncPlayerPreInteractEvent(player, packet.b(), entity).callEvent()) { + if (new AsyncPlayerPreInteractEvent(player, packet.b(), entity).callEvent() && + new AsyncPlayerPreInteractEntityEvent(player, packet.b(), entity).callEvent()) { super.channelRead(ctx, msg); } return;