Skip to content

Commit

Permalink
feat: add "petpickup" town config
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 20, 2023
1 parent ad53eed commit cc819ad
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
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.7.12"
version = "6.8.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit cc819ad

Please sign in to comment.