diff --git a/api b/api index 5671ddf1..4f86de87 160000 --- a/api +++ b/api @@ -1 +1 @@ -Subproject commit 5671ddf122422e4c0d5efd7606f460ad5b36356d +Subproject commit 4f86de872a76ec8abdceb40c38ae8380a25f8c36 diff --git a/build.gradle.kts b/build.gradle.kts index a7395fc9..b55d9684 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -90,7 +90,8 @@ allprojects { dependencies { implementation(project(":core")) implementation(project(":v1_20_1")) - implementation(project(":v1_21")) + implementation(project(":v1_21_paper")) + implementation(project(":v1_21_spigot")) } tasks.shadowJar { diff --git a/core/src/main/java/cn/lunadeer/dominion/Cache.java b/core/src/main/java/cn/lunadeer/dominion/Cache.java index 1c15bd3d..18752ea0 100644 --- a/core/src/main/java/cn/lunadeer/dominion/Cache.java +++ b/core/src/main/java/cn/lunadeer/dominion/Cache.java @@ -240,15 +240,15 @@ public DominionDTO getPlayerCurrentDominion(@NotNull Player player) { } // 如果玩家上次所在领地和当前所在领地不同,则触发玩家跨领地边界事件 - new PlayerCrossDominionBorderEvent(player, last_dominion, current_dominion).callEvent(); + new PlayerCrossDominionBorderEvent(player, last_dominion, current_dominion).call(); // 如果上次记录的领地不为空,则触发玩家离开领地事件 if (last_dom_id != -1) { - new PlayerMoveOutDominionEvent(player, last_dominion).callEvent(); + new PlayerMoveOutDominionEvent(player, last_dominion).call(); } // 如果当前领地不为空,则触发玩家进入领地事件 if (current_dom_id != -1) { - new PlayerMoveInDominionEvent(player, current_dominion).callEvent(); + new PlayerMoveInDominionEvent(player, current_dominion).call(); } // 更新玩家当前所在领地缓存 if (current_dominion == null) { diff --git a/core/src/main/java/cn/lunadeer/dominion/EventsRegister.java b/core/src/main/java/cn/lunadeer/dominion/EventsRegister.java index ac9b15e0..e07a9a34 100644 --- a/core/src/main/java/cn/lunadeer/dominion/EventsRegister.java +++ b/core/src/main/java/cn/lunadeer/dominion/EventsRegister.java @@ -20,13 +20,16 @@ public EventsRegister(JavaPlugin plugin) { switch (version) { case v1_21: XLogger.debug("Load API version: 1.21"); - registerEvents("cn.lunadeer.dominion.events_v1_21.PlayerEvents"); - registerEvents("cn.lunadeer.dominion.events_v1_21.EnvironmentEvents"); - registerEvents("cn.lunadeer.dominion.events_v1_21.SelectPointEvents"); if (Common.isPaper()) { - registerEvents("cn.lunadeer.dominion.events_v1_21.special.Paper"); + XLogger.debug("Load Paper special events"); + registerEvents("cn.lunadeer.dominion.events_v1_21_paper.PlayerEvents"); + registerEvents("cn.lunadeer.dominion.events_v1_21_paper.EnvironmentEvents"); + registerEvents("cn.lunadeer.dominion.events_v1_21_paper.SelectPointEvents"); } else { - registerEvents("cn.lunadeer.dominion.events_v1_21.special.Spigot"); + XLogger.debug("Load Spigot special events"); + registerEvents("cn.lunadeer.dominion.events_v1_21_spigot.PlayerEvents"); + registerEvents("cn.lunadeer.dominion.events_v1_21_spigot.EnvironmentEvents"); + registerEvents("cn.lunadeer.dominion.events_v1_21_spigot.SelectPointEvents"); } break; case v1_20_1: @@ -54,6 +57,7 @@ enum APIVersion { private static APIVersion GetAPIVersion(JavaPlugin plugin) { String version = plugin.getServer().getBukkitVersion(); + XLogger.debug("API version: %s", version); if (version.contains("1.21")) { return APIVersion.v1_21; } else if (version.contains("1.20.1") diff --git a/settings.gradle.kts b/settings.gradle.kts index eba69c68..19a9cc3e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,5 +4,6 @@ include( "api", "core", "v1_20_1", - "v1_21" + "v1_21_paper", + "v1_21_spigot" ) \ No newline at end of file diff --git a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Paper.java b/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Paper.java deleted file mode 100644 index 8664960b..00000000 --- a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Paper.java +++ /dev/null @@ -1,38 +0,0 @@ -package cn.lunadeer.dominion.events_v1_21.special; - -import cn.lunadeer.dominion.Cache; -import cn.lunadeer.dominion.dtos.DominionDTO; -import cn.lunadeer.dominion.dtos.Flag; -import com.destroystokyo.paper.event.entity.EntityPathfindEvent; -import org.bukkit.entity.Animals; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Monster; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; - -import static cn.lunadeer.dominion.utils.EventUtils.checkFlag; - -public class Paper implements Listener { - - @EventHandler(priority = EventPriority.HIGHEST) // monster_move - public void onMonsterPathfinding(EntityPathfindEvent event) { - Entity entity = event.getEntity(); - if (!(entity instanceof Monster)) { - return; - } - DominionDTO dom = Cache.instance.getDominionByLoc(event.getLoc()); - checkFlag(dom, Flag.MONSTER_MOVE, event); - } - - @EventHandler(priority = EventPriority.HIGHEST) // animal_move - public void onAnimalPathfinding(EntityPathfindEvent event) { - Entity entity = event.getEntity(); - if (!(entity instanceof Animals)) { - return; - } - DominionDTO dom = Cache.instance.getDominionByLoc(event.getLoc()); - checkFlag(dom, Flag.ANIMAL_MOVE, event); - } - -} diff --git a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Spigot.java b/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Spigot.java deleted file mode 100644 index 3f8dc988..00000000 --- a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/special/Spigot.java +++ /dev/null @@ -1,58 +0,0 @@ -package cn.lunadeer.dominion.events_v1_21.special; - -import cn.lunadeer.dominion.Cache; -import cn.lunadeer.dominion.Dominion; -import cn.lunadeer.dominion.dtos.DominionDTO; -import cn.lunadeer.dominion.dtos.Flag; -import cn.lunadeer.minecraftpluginutils.Scheduler; -import org.bukkit.Location; -import org.bukkit.entity.Animals; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Monster; -import org.bukkit.event.Listener; - -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; - -import static cn.lunadeer.dominion.utils.EventUtils.checkFlag; - -public class Spigot implements Listener { - - static { - ConcurrentHashMap entityMap = new ConcurrentHashMap<>(); - Scheduler.runTaskRepeat(() -> { - Dominion.instance.getServer().getWorlds().forEach(world -> { - world.getEntities().forEach(entity -> { - if (!(entity instanceof LivingEntity)) { - return; - } - if (!entityMap.containsKey(entity.getUniqueId())) { - entityMap.put(entity.getUniqueId(), entity.getLocation()); - } else { - Location lastLoc = entityMap.get(entity.getUniqueId()); - Location currentLoc = entity.getLocation(); - DominionDTO dom = Cache.instance.getDominionByLoc(currentLoc); - if (!checkFlag(dom, Flag.ANIMAL_MOVE, null) && entity instanceof Animals) { - entity.teleport(lastLoc); - } else if (!checkFlag(dom, Flag.MONSTER_MOVE, null) && entity instanceof Monster) { - entity.teleport(lastLoc); - } else { - entityMap.put(entity.getUniqueId(), currentLoc); - } - } - }); - }); - }, 0, 30); - - Scheduler.runTaskRepeat(() -> { - entityMap.forEach((uuid, loc) -> { - Entity e = Dominion.instance.getServer().getEntity(uuid); - if (e == null || e.isDead()) { - entityMap.remove(uuid); - } - }); - }, 0, 6000); - } - -} diff --git a/v1_21/build.gradle.kts b/v1_21_paper/build.gradle.kts similarity index 100% rename from v1_21/build.gradle.kts rename to v1_21_paper/build.gradle.kts diff --git a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/EnvironmentEvents.java b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/EnvironmentEvents.java similarity index 94% rename from v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/EnvironmentEvents.java rename to v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/EnvironmentEvents.java index 24bc243c..15aba772 100644 --- a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/EnvironmentEvents.java +++ b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/EnvironmentEvents.java @@ -1,9 +1,10 @@ -package cn.lunadeer.dominion.events_v1_21; +package cn.lunadeer.dominion.events_v1_21_paper; import cn.lunadeer.dominion.Cache; import cn.lunadeer.dominion.dtos.DominionDTO; import cn.lunadeer.dominion.dtos.Flag; import cn.lunadeer.minecraftpluginutils.XLogger; +import com.destroystokyo.paper.event.entity.EntityPathfindEvent; import org.bukkit.Location; import org.bukkit.Tag; import org.bukkit.block.Block; @@ -401,5 +402,25 @@ public void onGravityBlockFalling(EntityChangeBlockEvent event) { // gravity_b } } + @EventHandler(priority = EventPriority.HIGHEST) // monster_move + public void onMonsterPathfinding(EntityPathfindEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof Monster)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getLoc()); + checkFlag(dom, Flag.MONSTER_MOVE, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // animal_move + public void onAnimalPathfinding(EntityPathfindEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof Animals)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getLoc()); + checkFlag(dom, Flag.ANIMAL_MOVE, event); + } + private static final Map fallingBlockMap = new java.util.HashMap<>(); } diff --git a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/PlayerEvents.java b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/PlayerEvents.java similarity index 98% rename from v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/PlayerEvents.java rename to v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/PlayerEvents.java index e0d1f1c5..a87b2f8a 100644 --- a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/PlayerEvents.java +++ b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/PlayerEvents.java @@ -1,10 +1,9 @@ -package cn.lunadeer.dominion.events_v1_21; +package cn.lunadeer.dominion.events_v1_21_paper; import cn.lunadeer.dominion.Cache; import cn.lunadeer.dominion.dtos.DominionDTO; import cn.lunadeer.dominion.dtos.Flag; import cn.lunadeer.dominion.dtos.PlayerDTO; -import cn.lunadeer.minecraftpluginutils.Common; import cn.lunadeer.minecraftpluginutils.Notification; import cn.lunadeer.minecraftpluginutils.Teleport; import com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent; @@ -649,12 +648,7 @@ public void onPlayerMove(PlayerMoveEvent event) { if (bed == null) { bed = player.getWorld().getSpawnLocation(); } - if (Common.isPaper()) { - player.teleportAsync(bed, PlayerTeleportEvent.TeleportCause.PLUGIN); - } else { - player.teleport(bed, PlayerTeleportEvent.TeleportCause.PLUGIN); - } - + player.teleportAsync(bed, PlayerTeleportEvent.TeleportCause.PLUGIN); } }); } diff --git a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/SelectPointEvents.java b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/SelectPointEvents.java similarity index 99% rename from v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/SelectPointEvents.java rename to v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/SelectPointEvents.java index 84b39953..31b4361a 100644 --- a/v1_21/src/main/java/cn/lunadeer/dominion/events_v1_21/SelectPointEvents.java +++ b/v1_21_paper/src/main/java/cn/lunadeer/dominion/events_v1_21_paper/SelectPointEvents.java @@ -1,4 +1,4 @@ -package cn.lunadeer.dominion.events_v1_21; +package cn.lunadeer.dominion.events_v1_21_paper; import cn.lunadeer.dominion.Cache; import cn.lunadeer.dominion.Dominion; diff --git a/v1_21_spigot/build.gradle.kts b/v1_21_spigot/build.gradle.kts new file mode 100644 index 00000000..0f1d4a49 --- /dev/null +++ b/v1_21_spigot/build.gradle.kts @@ -0,0 +1,18 @@ +plugins { + id("java") +} + +java { + toolchain.languageVersion.set(JavaLanguageVersion.of(21)) +} + +// utf-8 +tasks.withType { + options.encoding = "UTF-8" +} + +dependencies { + compileOnly(project(":api")) + compileOnly(project(":core")) + compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT") +} \ No newline at end of file diff --git a/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/EnvironmentEvents.java b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/EnvironmentEvents.java new file mode 100644 index 00000000..9d482ca8 --- /dev/null +++ b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/EnvironmentEvents.java @@ -0,0 +1,444 @@ +package cn.lunadeer.dominion.events_v1_21_spigot; + +import cn.lunadeer.dominion.Cache; +import cn.lunadeer.dominion.Dominion; +import cn.lunadeer.dominion.dtos.DominionDTO; +import cn.lunadeer.dominion.dtos.Flag; +import cn.lunadeer.minecraftpluginutils.Scheduler; +import cn.lunadeer.minecraftpluginutils.XLogger; +import org.bukkit.Location; +import org.bukkit.Tag; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; +import org.bukkit.event.entity.*; +import org.bukkit.event.hanging.HangingBreakByEntityEvent; +import org.bukkit.event.hanging.HangingBreakEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import static cn.lunadeer.dominion.utils.EventUtils.checkFlag; +import static org.bukkit.Material.FARMLAND; + +public class EnvironmentEvents implements Listener { + @EventHandler(priority = EventPriority.HIGHEST) // creeper_explode + public void onEntityExplode(EntityExplodeEvent event) { + Entity entity = event.getEntity(); + XLogger.debug("EntityExplodeEvent: " + entity.getType()); + if (isNotExplodeEntity(entity)) { + return; + } + XLogger.debug("blockList" + event.blockList().size()); + event.blockList().removeIf(block -> { + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + return !checkFlag(dom, Flag.CREEPER_EXPLODE, null); + }); + } + + @EventHandler(priority = EventPriority.HIGHEST) // creeper_explode - bed anchor + public void onBedAnchorExplosion(BlockExplodeEvent event) { + event.blockList().removeIf(blockState -> { + DominionDTO dom = Cache.instance.getDominionByLoc(blockState.getLocation()); + return !checkFlag(dom, Flag.CREEPER_EXPLODE, null); + }); + } + + @EventHandler(priority = EventPriority.HIGHEST) // creeper_explode - item frame + public void onItemFrameExploded(HangingBreakByEntityEvent event) { + Entity entity = event.getEntity(); + if (event.getCause() != HangingBreakEvent.RemoveCause.EXPLOSION) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.CREEPER_EXPLODE, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // creeper_explode - item frame + public void onItemFrameShot(ProjectileHitEvent event) { + Entity hit = event.getHitEntity(); + if (hit == null) { + return; + } + if (event.getEntity().getShooter() instanceof Player) { + return; + } + if (!(hit instanceof Hanging)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(hit.getLocation()); + checkFlag(dom, Flag.CREEPER_EXPLODE, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // creeper_explode - armor stand + public void onArmorStandExploded(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.ARMOR_STAND) { + return; + } + if (isNotExplodeEntity(event.getDamager())) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.CREEPER_EXPLODE, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // item_frame_proj_damage + public void removeSomeOnItemFrameByArrow(HangingBreakByEntityEvent event) { + if (event.getCause() != HangingBreakEvent.RemoveCause.ENTITY) { + return; + } + Entity remover = event.getRemover(); + if (!(remover instanceof Projectile projectile)) { + return; + } + if (!(projectile.getShooter() instanceof Player)) { + // 玩家破坏由 玩家 break 权限控制 + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getEntity().getLocation()); + checkFlag(dom, Flag.ITEM_FRAME_PROJ_DAMAGE, event); + } + + private static boolean isNotExplodeEntity(Entity damager) { + return damager.getType() != EntityType.CREEPER + && damager.getType() != EntityType.WITHER_SKULL + && damager.getType() != EntityType.FIREBALL + && damager.getType() != EntityType.END_CRYSTAL + && damager.getType() != EntityType.SMALL_FIREBALL + && damager.getType() != EntityType.DRAGON_FIREBALL; + } + + @EventHandler(priority = EventPriority.HIGHEST) // dragon_break_block + public void onDragonBreakBlock(EntityExplodeEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.ENDER_DRAGON) { + return; + } + event.blockList().removeIf(block -> { + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + return !checkFlag(dom, Flag.DRAGON_BREAK_BLOCK, null); + }); + } + + @EventHandler(priority = EventPriority.HIGHEST) // fire_spread + public void onFireSpread(BlockIgniteEvent event) { + Player player = event.getPlayer(); + if (player != null) { + // 如果点燃事件没有玩家触发,那么就是火焰蔓延 + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getBlock().getLocation()); + checkFlag(dom, Flag.FIRE_SPREAD, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // flow_in_protection + public void onLiquidFlowIn(BlockFromToEvent event) { + Location from = event.getBlock().getLocation(); + Location to = event.getToBlock().getLocation(); + DominionDTO dom_to = Cache.instance.getDominionByLoc(to); + if (dom_to == null) { + return; + } + DominionDTO dom_from = Cache.instance.getDominionByLoc(from); + if (dom_from != null) { + if (Objects.equals(dom_from.getId(), dom_to.getId())) { + return; + } + } + checkFlag(dom_to, Flag.FLOW_IN_PROTECTION, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // mob_drop_item + public void onMobDropItem(EntityDeathEvent event) { + Entity entity = event.getEntity(); + if (entity instanceof Player) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + if (dom == null) { + return; + } + if (!Flag.MOB_DROP_ITEM.getEnable()) { + return; + } + if (dom.getFlagValue(Flag.MOB_DROP_ITEM)) { + return; + } + event.getDrops().clear(); + } + + @EventHandler(priority = EventPriority.HIGHEST) // tnt_explode + public void onTntExplode(EntityExplodeEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.TNT_MINECART && entity.getType() != EntityType.TNT) { + return; + } + event.blockList().removeIf(block -> { + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + return !checkFlag(dom, Flag.TNT_EXPLODE, null); + }); + } + + @EventHandler(priority = EventPriority.HIGHEST) // tnt_explode - entity + public void onArmorStandExplodedByTnt(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + Entity harmer = event.getDamager(); + if (harmer.getType() != EntityType.TNT_MINECART && harmer.getType() != EntityType.TNT) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.TNT_EXPLODE, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // trample + public void onFarmlandTrample(PlayerInteractEvent event) { + if (event.getAction() != Action.PHYSICAL) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (block.getType() != FARMLAND) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.TRAMPLE, event); + } + + /* + TRIG_PRESSURE_PROJ("trig_pressure_proj", "投掷物触发压力板", "投掷物(箭/风弹/雪球)是否可以触发压力板", false, true, true), + TRIG_PRESSURE_MOB("trig_pressure_mob", "生物触发压力板", "生物(不包含玩家)是否可以触发压力板", false, true, true), + TRIG_PRESSURE_DROP("trig_pressure_drop", "掉落物触发压力板", "掉落物是否可以触发压力板", false, true, true), + */ + @EventHandler(priority = EventPriority.HIGHEST) // trig_pressure_proj + public void onPressurePlateTriggeredByProjectile(EntityInteractEvent event) { + if (!(event.getEntity() instanceof Projectile)) { + return; + } + Block block = event.getBlock(); + if (!Tag.PRESSURE_PLATES.isTagged(block.getType())) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.TRIG_PRESSURE_PROJ, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // trig_pressure_mob + public void onPressurePlateTriggeredByMob(EntityInteractEvent event) { + if (!(event.getEntity() instanceof Mob)) { + return; + } + Block block = event.getBlock(); + if (!Tag.PRESSURE_PLATES.isTagged(block.getType())) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.TRIG_PRESSURE_MOB, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // trig_pressure_drop + public void onPressurePlateTriggeredByDrop(EntityInteractEvent event) { + if (!(event.getEntity() instanceof Item)) { + return; + } + Block block = event.getBlock(); + if (!Tag.PRESSURE_PLATES.isTagged(block.getType())) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.TRIG_PRESSURE_DROP, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // wither_spawn + public void onWitherSpawn(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.WITHER) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.WITHER_SPAWN, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // wither_spawn - explode + public void onWitherSpawnExplode(EntityExplodeEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.WITHER) { + return; + } + event.blockList().removeIf(block -> { + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + return !checkFlag(dom, Flag.WITHER_SPAWN, null); + }); + } + + @EventHandler(priority = EventPriority.HIGHEST) // ender_man spawn + public void onEnderManSpawn(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.ENDERMAN) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.ENDER_MAN, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // ender_man escape + public void onEnderManEscape(EntityTeleportEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.ENDERMAN) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.ENDER_MAN, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // monster_spawn + public void onMonsterSpawn(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof Monster)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.MONSTER_SPAWN, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // animal_spawn + public void onAnimalSpawn(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof Animals)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.ANIMAL_SPAWN, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // villager_spawn + public void onVillagerSpawn(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + if (entity.getType() != EntityType.VILLAGER) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.VILLAGER_SPAWN, event); + } + + + @EventHandler(priority = EventPriority.HIGHEST) + public void onHopper(InventoryMoveItemEvent event) { // hopper_outside + Inventory hopper = event.getDestination(); + Inventory inventory = event.getSource(); + DominionDTO hopperDom = Cache.instance.getDominionByLoc(hopper.getLocation()); + DominionDTO inventoryDom = Cache.instance.getDominionByLoc(inventory.getLocation()); + if (hopperDom == null && inventoryDom != null) { + checkFlag(inventoryDom, Flag.HOPPER_OUTSIDE, event); + } + if (hopperDom != null && inventoryDom != null) { + if (!hopperDom.getId().equals(inventoryDom.getId())) { + checkFlag(inventoryDom, Flag.HOPPER_OUTSIDE, event); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onBlockPushedByPiston(BlockPistonExtendEvent event) { // piston_outside + Block piston = event.getBlock(); + DominionDTO pistonDom = Cache.instance.getDominionByLoc(piston.getLocation()); + BlockFace direction = event.getDirection(); + Block endBlockAfterPush = piston.getRelative(direction, event.getBlocks().size() + 1); + DominionDTO endBlockDom = Cache.instance.getDominionByLoc(endBlockAfterPush.getLocation()); + if (pistonDom != null && endBlockDom == null) { + checkFlag(pistonDom, Flag.PISTON_OUTSIDE, event); + } + if (pistonDom == null && endBlockDom != null) { + checkFlag(endBlockDom, Flag.PISTON_OUTSIDE, event); + } + if (pistonDom != null && endBlockDom != null) { + if (!pistonDom.getId().equals(endBlockDom.getId())) { + if (!endBlockDom.getFlagValue(Flag.PISTON_OUTSIDE) || !pistonDom.getFlagValue(Flag.PISTON_OUTSIDE)) { + event.setCancelled(true); + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onGravityBlockFalling(EntityChangeBlockEvent event) { // gravity_block + Entity entity = event.getEntity(); + if (!(entity instanceof FallingBlock)) { + return; + } + Block block = event.getBlock(); + if (event.getTo().isAir()) { + fallingBlockMap.put(entity.getUniqueId(), block.getLocation()); + } else { + Location locStart = fallingBlockMap.get(entity.getUniqueId()); + if (locStart == null) { + return; + } + fallingBlockMap.remove(entity.getUniqueId()); + Location locEnd = block.getLocation(); + DominionDTO domStart = Cache.instance.getDominionByLoc(locStart); + DominionDTO domEnd = Cache.instance.getDominionByLoc(locEnd); + if (domEnd == null) { + return; + } + if (domStart != null && domStart.getId().equals(domEnd.getId())) { + return; + } + if (!checkFlag(domEnd, Flag.GRAVITY_BLOCK, null)) { + event.setCancelled(true); + locEnd.getWorld().dropItemNaturally(locEnd, new ItemStack(((FallingBlock) entity).getBlockData().getMaterial())); + entity.remove(); + } + } + } + + static { + ConcurrentHashMap entityMap = new ConcurrentHashMap<>(); + Scheduler.runTaskRepeat(() -> { + Dominion.instance.getServer().getWorlds().forEach(world -> { + world.getEntities().forEach(entity -> { + if (!(entity instanceof LivingEntity)) { + return; + } + if (!entityMap.containsKey(entity.getUniqueId())) { + entityMap.put(entity.getUniqueId(), entity.getLocation()); + } else { + Location lastLoc = entityMap.get(entity.getUniqueId()); + Location currentLoc = entity.getLocation(); + DominionDTO dom = Cache.instance.getDominionByLoc(currentLoc); + if (!checkFlag(dom, Flag.ANIMAL_MOVE, null) && entity instanceof Animals) { + entity.teleport(lastLoc); + } else if (!checkFlag(dom, Flag.MONSTER_MOVE, null) && entity instanceof Monster) { + entity.teleport(lastLoc); + } else { + entityMap.put(entity.getUniqueId(), currentLoc); + } + } + }); + }); + }, 0, 30); + + Scheduler.runTaskRepeat(() -> { + entityMap.forEach((uuid, loc) -> { + Entity e = Dominion.instance.getServer().getEntity(uuid); + if (e == null || e.isDead()) { + entityMap.remove(uuid); + } + }); + }, 0, 6000); + } + + private static final Map fallingBlockMap = new java.util.HashMap<>(); +} diff --git a/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/PlayerEvents.java b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/PlayerEvents.java new file mode 100644 index 00000000..3feec3cc --- /dev/null +++ b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/PlayerEvents.java @@ -0,0 +1,843 @@ +package cn.lunadeer.dominion.events_v1_21_spigot; + +import cn.lunadeer.dominion.Cache; +import cn.lunadeer.dominion.dtos.DominionDTO; +import cn.lunadeer.dominion.dtos.Flag; +import cn.lunadeer.dominion.dtos.PlayerDTO; +import cn.lunadeer.minecraftpluginutils.Notification; +import cn.lunadeer.minecraftpluginutils.Teleport; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Tag; +import org.bukkit.block.Block; +import org.bukkit.entity.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; +import org.bukkit.event.entity.*; +import org.bukkit.event.hanging.HangingBreakByEntityEvent; +import org.bukkit.event.hanging.HangingBreakEvent; +import org.bukkit.event.hanging.HangingPlaceEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.*; +import org.bukkit.event.vehicle.VehicleDestroyEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.material.Colorable; + +import static cn.lunadeer.dominion.utils.EventUtils.checkFlag; +import static cn.lunadeer.dominion.utils.EventUtils.getInvDominion; + +public class PlayerEvents implements Listener { + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player bukkitPlayer = event.getPlayer(); + PlayerDTO player = PlayerDTO.get(bukkitPlayer); + player.onJoin(bukkitPlayer.getName()); // update name + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + Player bukkitPlayer = event.getPlayer(); + Cache.instance.onPlayerQuit(bukkitPlayer); + } + + @EventHandler(priority = EventPriority.HIGHEST) // anchor + public void onRespawnAnchor(PlayerRespawnEvent event) { + Player bukkitPlayer = event.getPlayer(); + if (!event.isAnchorSpawn()) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getRespawnLocation()); + if (!checkFlag(dom, Flag.ANCHOR, bukkitPlayer, null)) { + if (bukkitPlayer.getRespawnLocation() != null) { + event.setRespawnLocation(bukkitPlayer.getRespawnLocation()); + } else { + event.setRespawnLocation(bukkitPlayer.getWorld().getSpawnLocation()); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) // anchor + public void onAnchorInteractive(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = block.getType(); + if (clicked != Material.RESPAWN_ANCHOR) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.ANCHOR, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // animal_killing + public void onAnimalKilling(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player bukkitPlayer)) { + return; + } + // 如果不是动物 则不处理 + if (!(event.getEntity() instanceof Animals)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getEntity().getLocation()); + checkFlag(dom, Flag.ANIMAL_KILLING, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // anvil + public void onAnvilUse(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.ANVIL) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = Cache.instance.getPlayerCurrentDominion(bukkitPlayer); + checkFlag(dom, Flag.ANVIL, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // beacon + public void onBeaconUse(InventoryOpenEvent event) { + Inventory inv = event.getInventory(); + if (inv.getType() != InventoryType.BEACON) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + if (inv.getLocation() == null) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(inv.getLocation()); + checkFlag(dom, Flag.BEACON, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // bed + public void onBedUse(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Player bukkitPlayer = event.getPlayer(); + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (!(Tag.BEDS.isTagged(block.getType()))) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.BED, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // brew + public void onBrewUse(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.BREWING) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = Cache.instance.getPlayerCurrentDominion(bukkitPlayer); + checkFlag(dom, Flag.BREW, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // break + public void onBlockBreak(BlockBreakEvent event) { + Player player = event.getPlayer(); + if (onBreak(player, event.getBlock().getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // break - item frame + public void onItemFrameBreak(HangingBreakByEntityEvent event) { + Entity entity = event.getEntity(); + if (event.getCause() != HangingBreakEvent.RemoveCause.ENTITY) { + return; + } + if (entity instanceof ItemFrame) { + if (((ItemFrame) entity).getItem().getType() != Material.AIR) { + if (!hasContainerPermission((Player) event.getRemover(), entity.getLocation())) { + event.setCancelled(true); + return; + } + } + } + if (onBreak((Player) event.getRemover(), entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // break - armor stand + public void onArmorStandBreak(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof ArmorStand)) { + return; + } + if (!(event.getDamager() instanceof Player)) { + return; + } + if (onBreak((Player) event.getDamager(), entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + public static boolean onBreak(Player player, Location location) { + DominionDTO dom = Cache.instance.getDominionByLoc(location); + return checkFlag(dom, Flag.BREAK_BLOCK, player, null); + } + + @EventHandler(priority = EventPriority.HIGHEST) // button + public void onButton(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Player player = event.getPlayer(); + if (event.getClickedBlock() == null) { + return; + } + Block block = event.getClickedBlock(); + if (!Tag.BUTTONS.isTagged(block.getType())) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.BUTTON, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // cake + public void eatCake(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = block.getType(); + if (clicked != Material.CAKE) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.CAKE, player, event); + } + + // 检查是否有容器权限 + private static boolean hasContainerPermission(Player player, Location loc) { + DominionDTO dom; + if (loc == null) { + dom = null; + } else { + dom = Cache.instance.getDominionByLoc(loc); + } + return checkFlag(dom, Flag.CONTAINER, player, null); + } + + @EventHandler(priority = EventPriority.HIGHEST) // container + public void openContainer(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.CHEST && + event.getInventory().getType() != InventoryType.BARREL && + event.getInventory().getType() != InventoryType.SHULKER_BOX) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + if (hasContainerPermission(bukkitPlayer, event.getInventory().getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // container (armor stand) + public void manipulateArmorStand(PlayerArmorStandManipulateEvent event) { + Player bukkitPlayer = event.getPlayer(); + if (hasContainerPermission(bukkitPlayer, event.getRightClicked().getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // container (item frame put) + public void putSomeOnItemFrame(PlayerInteractEntityEvent event) { + Entity entity = event.getRightClicked(); + if (!(entity instanceof ItemFrame itemFrame)) { + return; + } + if (itemFrame.getItem().getType() != Material.AIR) { + return; + } + Player bukkitPlayer = event.getPlayer(); + if (hasContainerPermission(bukkitPlayer, entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // container (item frame get) + public void removeSomeOnItemFrame(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof ItemFrame itemFrame)) { + return; + } + if (itemFrame.getItem().getType() == Material.AIR) { + return; + } + if (!(event.getDamager() instanceof Player bukkitPlayer)) { + return; + } + if (hasContainerPermission(bukkitPlayer, entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // container (item frame get) + public void removeSomeOnItemFrameByArrow(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof ItemFrame itemFrame)) { + return; + } + if (itemFrame.getItem().getType() == Material.AIR) { + return; + } + if (!(event.getDamager() instanceof Arrow arrow)) { + return; + } + if (!(arrow.getShooter() instanceof Player bukkitPlayer)) { + return; + } + if (hasContainerPermission(bukkitPlayer, itemFrame.getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // craft + public void onCraft(InventoryOpenEvent event) { + Inventory inv = event.getInventory(); + if (inv.getType() != InventoryType.WORKBENCH) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = getInvDominion(bukkitPlayer, inv); + checkFlag(dom, Flag.CRAFT, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // crafter + public void onCrafterOpen(InventoryOpenEvent event) { + Inventory inv = event.getInventory(); + // InventoryType.CRAFTER; + if (!inv.getType().name().contains("CRAFTER")) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = getInvDominion(bukkitPlayer, inv); + checkFlag(dom, Flag.CRAFTER, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // comparer + public void comparerChange(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = event.getClickedBlock().getType(); + if (clicked != Material.COMPARATOR) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(event.getClickedBlock().getLocation()); + checkFlag(dom, Flag.COMPARER, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // door + public void doorUse(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (!Tag.DOORS.isTagged(block.getType()) && !Tag.TRAPDOORS.isTagged(block.getType())) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(event.getClickedBlock().getLocation()); + checkFlag(dom, Flag.DOOR, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // dragon_egg + public void touchDragonEdd(PlayerInteractEvent event) { + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (block.getType() != Material.DRAGON_EGG) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.DRAGON_EGG, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // dye + public void dyeEvent(PlayerInteractEntityEvent event) { + Player player = event.getPlayer(); + Entity entity = event.getRightClicked(); + if (!(entity instanceof Colorable)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.DYE, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // edit sign + public void onSignOpen(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Player player = event.getPlayer(); + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (!(Tag.SIGNS.isTagged(block.getType()))) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.EDIT_SIGN, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // edit sign + public void onSignEdit(SignChangeEvent event) { + Player player = event.getPlayer(); + Block block = event.getBlock(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.EDIT_SIGN, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // egg + public void onThrowingEgg(ProjectileHitEvent event) { + Projectile projectile = event.getEntity(); + if (!(projectile.getShooter() instanceof Player player)) { + return; + } + if (projectile.getType() != EntityType.EGG) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(projectile.getLocation()); + checkFlag(dom, Flag.EGG, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // enchant + public void onEnchant(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.ENCHANTING) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = getInvDominion(bukkitPlayer, event.getInventory()); + checkFlag(dom, Flag.ENCHANT, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // ender_pearl + public void onThrowingEndPearl(ProjectileHitEvent event) { + Projectile projectile = event.getEntity(); + if (!(projectile.getShooter() instanceof Player player)) { + return; + } + if (projectile.getType() != EntityType.ENDER_PEARL) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(projectile.getLocation()); + checkFlag(dom, Flag.ENDER_PEARL, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // feed + public void onFeedAnimal(PlayerInteractEntityEvent event) { + if (!(event.getRightClicked() instanceof Animals)) { + return; + } + // if shearing sheep instead + if (event.getPlayer().getInventory().getItem(event.getHand()).getType() == Material.SHEARS) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(event.getRightClicked().getLocation()); + checkFlag(dom, Flag.FEED, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // harvest + public void onHarvest(BlockBreakEvent event) { + Block block = event.getBlock(); + if (block.getType() != Material.COCOA && + block.getType() != Material.WHEAT && + block.getType() != Material.CARROTS && + block.getType() != Material.POTATOES && + block.getType() != Material.BEETROOTS && + block.getType() != Material.NETHER_WART && + block.getType() != Material.SWEET_BERRY_BUSH && + block.getType() != Material.MELON && + block.getType() != Material.PUMPKIN && + block.getType() != Material.SUGAR_CANE && + block.getType() != Material.BAMBOO && + block.getType() != Material.CACTUS && + block.getType() != Material.CHORUS_PLANT && + block.getType() != Material.CHORUS_FLOWER && + block.getType() != Material.KELP && + block.getType() != Material.KELP_PLANT) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.HARVEST, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // honey + public void honeyInteractive(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = block.getType(); + if (clicked != Material.BEEHIVE && clicked != Material.BEE_NEST) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.HONEY, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // hook + public void onHook(PlayerFishEvent event) { + Entity caught = event.getCaught(); + if (caught == null) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(caught.getLocation()); + checkFlag(dom, Flag.HOOK, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // hopper + public void openHopper(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.HOPPER && + event.getInventory().getType() != InventoryType.DROPPER && + event.getInventory().getType() != InventoryType.DISPENSER && + event.getInventory().getType() != InventoryType.FURNACE && + event.getInventory().getType() != InventoryType.BLAST_FURNACE && + event.getInventory().getType() != InventoryType.SMOKER + ) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = getInvDominion(bukkitPlayer, event.getInventory()); + checkFlag(dom, Flag.HOPPER, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // ignite + public void onPlayerIgnite(BlockIgniteEvent event) { + Player player = event.getPlayer(); + if (player == null) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getBlock().getLocation()); + checkFlag(dom, Flag.IGNITE, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // item_frame_interactive + public void onItemFrameInteractive(PlayerInteractEntityEvent event) { + Entity entity = event.getRightClicked(); + if (!(entity instanceof ItemFrame itemFrame)) { + return; + } + if (itemFrame.getItem().getType() == Material.AIR) { + // 为空则当作容器处理见 putSomeOnItemFrame + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.ITEM_FRAME_INTERACTIVE, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // lever + public void onLever(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = block.getType(); + if (clicked != Material.LEVER) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.LEVER, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // monster_killing + public void onMonsterKilling(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player bukkitPlayer)) { + return; + } + // 如果不是怪物 则不处理 + Entity entity = event.getEntity(); + if (!(entity instanceof Monster)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.MONSTER_KILLING, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // move + public void onPlayerMove(PlayerMoveEvent event) { + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getPlayerCurrentDominion(player); + if (!checkFlag(dom, Flag.MOVE, player, null)) { + Location to = player.getLocation(); + int x1 = Math.abs(to.getBlockX() - dom.getX1()); + int x2 = Math.abs(to.getBlockX() - dom.getX2()); + int z1 = Math.abs(to.getBlockZ() - dom.getZ1()); + int z2 = Math.abs(to.getBlockZ() - dom.getZ2()); + // find min distance + int min = Math.min(Math.min(x1, x2), Math.min(z1, z2)); + if (min == x1) { + to.setX(dom.getX1() - 2); + } else if (min == x2) { + to.setX(dom.getX2() + 2); + } else if (min == z1) { + to.setZ(dom.getZ1() - 2); + } else { + to.setZ(dom.getZ2() + 2); + } + Teleport.doTeleportSafely(player, to).thenAccept((success) -> { + if (!success) { + Notification.warn(player, "传送失败,你将被传送到复活点"); + Location bed = player.getRespawnLocation(); + if (bed == null) { + bed = player.getWorld().getSpawnLocation(); + } + player.teleport(bed, PlayerTeleportEvent.TeleportCause.PLUGIN); + } + }); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) // note_block + public void onNoteBlockClicked(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Material clicked = block.getType(); + if (clicked != Material.NOTE_BLOCK) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.NOTE_BLOCK, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // place + public void onPlaceBlock(BlockPlaceEvent event) { + Player player = event.getPlayer(); + if (onPlace(player, event.getBlock().getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // place - lava or water + public void onPlaceLavaOrWater(PlayerBucketEmptyEvent event) { + Player player = event.getPlayer(); + if (onPlace(player, event.getBlock().getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // place - item frame + public void placeItemFrame(HangingPlaceEvent event) { + Entity entity = event.getEntity(); + Player player = event.getPlayer(); + if (player == null) { + return; + } + if (onPlace(player, entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) // place - armor stand + public void placeArmorStand(EntityPlaceEvent event) { + Player player = event.getPlayer(); + if (player == null) { + return; + } + Entity entity = event.getEntity(); + if (!(entity instanceof ArmorStand)) { + return; + } + if (onPlace(player, entity.getLocation())) { + return; + } + event.setCancelled(true); + } + + public static boolean onPlace(Player player, Location location) { + DominionDTO dom = Cache.instance.getDominionByLoc(location); + return checkFlag(dom, Flag.PLACE, player, null); + } + + @EventHandler(priority = EventPriority.HIGHEST) // pressure + public void onPressure(PlayerInteractEvent event) { + if (event.getAction() != Action.PHYSICAL) { + return; + } + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + if (!Tag.PRESSURE_PLATES.isTagged(block.getType())) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.PRESSURE, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // riding + public void onRiding(EntityMountEvent event) { + if (!(event.getEntity() instanceof Player player)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getMount().getLocation()); + checkFlag(dom, Flag.RIDING, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // repeater + public void onRepeaterChange(PlayerInteractEvent event) { + if (event.getClickedBlock() == null) { + return; + } + Block block = event.getClickedBlock(); + Material clicked = block.getType(); + if (clicked != Material.REPEATER) { + return; + } + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(block.getLocation()); + checkFlag(dom, Flag.REPEATER, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // shear + public void onShear(PlayerShearEntityEvent event) { + Player player = event.getPlayer(); + DominionDTO dom = Cache.instance.getDominionByLoc(event.getEntity().getLocation()); + checkFlag(dom, Flag.SHEAR, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // shoot + public void onShootArrowSnowball(ProjectileHitEvent event) { + Projectile projectile = event.getEntity(); + if (!(projectile.getShooter() instanceof Player player)) { + return; + } + if (projectile.getType() == EntityType.ENDER_PEARL || projectile.getType() == EntityType.EGG) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(projectile.getLocation()); + checkFlag(dom, Flag.SHOOT, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // shoot - wind_charge knock back + public void onWindChargeKnockBack(EntityKnockbackByEntityEvent event) { + Entity entity = event.getSourceEntity(); + if (!(entity instanceof WindCharge windCharge)) { + return; + } + if (!(windCharge.getShooter() instanceof Player player)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(windCharge.getLocation()); + checkFlag(dom, Flag.SHOOT, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // trade + public void onTrade(InventoryOpenEvent event) { + if (event.getInventory().getType() != InventoryType.MERCHANT) { + return; + } + if (!(event.getPlayer() instanceof Player bukkitPlayer)) { + return; + } + DominionDTO dom = getInvDominion(bukkitPlayer, event.getInventory()); + checkFlag(dom, Flag.TRADE, bukkitPlayer, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // vehicle_destroy + public void onVehicleDestroy(VehicleDestroyEvent event) { + if (!(event.getAttacker() instanceof Player player)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getVehicle().getLocation()); + checkFlag(dom, Flag.VEHICLE_DESTROY, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // vehicle_spawn + public void onVehicleSpawn(EntityPlaceEvent event) { + Player player = event.getPlayer(); + if (player == null) { + return; + } + Entity entity = event.getEntity(); + if (!(entity instanceof Vehicle)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(entity.getLocation()); + checkFlag(dom, Flag.VEHICLE_SPAWN, player, event); + } + + @EventHandler(priority = EventPriority.HIGHEST) // villager_killing + public void onVillagerKilling(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player player)) { + return; + } + if (!(event.getEntity() instanceof Villager)) { + return; + } + DominionDTO dom = Cache.instance.getDominionByLoc(event.getEntity().getLocation()); + checkFlag(dom, Flag.VILLAGER_KILLING, player, event); + } +} diff --git a/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/SelectPointEvents.java b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/SelectPointEvents.java new file mode 100644 index 00000000..6e648272 --- /dev/null +++ b/v1_21_spigot/src/main/java/cn/lunadeer/dominion/events_v1_21_spigot/SelectPointEvents.java @@ -0,0 +1,131 @@ +package cn.lunadeer.dominion.events_v1_21_spigot; + +import cn.lunadeer.dominion.Cache; +import cn.lunadeer.dominion.Dominion; +import cn.lunadeer.dominion.dtos.DominionDTO; +import cn.lunadeer.dominion.managers.Translation; +import cn.lunadeer.dominion.utils.Particle; +import cn.lunadeer.dominion.utils.VaultConnect.VaultConnect; +import cn.lunadeer.minecraftpluginutils.Notification; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.Map; + +public class SelectPointEvents implements Listener { + @EventHandler(priority = EventPriority.HIGHEST) + public void selectPoint(PlayerInteractEvent event) { + Player player = event.getPlayer(); + ItemStack item = player.getInventory().getItemInMainHand(); + + if (item.getType() != Dominion.config.getTool()) { + return; + } + Block block = event.getClickedBlock(); + Action action = event.getAction(); + if (block == null) { + return; + } + + Map points = Dominion.pointsSelect.get(player.getUniqueId()); + if (points == null) { + points = new HashMap<>(); + } + + if (action == Action.LEFT_CLICK_BLOCK) { + event.setCancelled(true); + Notification.info(player, Translation.Tool_SelectFirstPoint, block.getX(), block.getY(), block.getZ()); + Location loc = block.getLocation(); + if (Dominion.config.getLimitVert(player)) { + loc.setY(Dominion.config.getLimitMinY(player)); + } + points.put(0, loc); + } else if (action == Action.RIGHT_CLICK_BLOCK) { + event.setCancelled(true); + Notification.info(player, Translation.Tool_SelectSecondPoint, block.getX(), block.getY(), block.getZ()); + Location loc = block.getLocation(); + if (Dominion.config.getLimitVert(player)) { + loc.setY(Dominion.config.getLimitMaxY(player) - 1); + } + points.put(1, loc); + } else { + return; + } + Dominion.pointsSelect.put(player.getUniqueId(), points); + + if (points.size() == 2) { + World world = points.get(0).getWorld(); + if (world == null) { + return; + } + if (!points.get(0).getWorld().equals(points.get(1).getWorld())) { + Notification.warn(player, Translation.Tool_NotSameWorld); + return; + } + Notification.info(player, Translation.Tool_SelectTwoPoints); + Location loc1 = points.get(0); + Location loc2 = points.get(1); + int minX = Math.min(loc1.getBlockX(), loc2.getBlockX()); + int minY = Math.min(loc1.getBlockY(), loc2.getBlockY()); + int minZ = Math.min(loc1.getBlockZ(), loc2.getBlockZ()); + int maxX = Math.max(loc1.getBlockX(), loc2.getBlockX()) + 1; + int maxY = Math.max(loc1.getBlockY(), loc2.getBlockY()) + 1; + int maxZ = Math.max(loc1.getBlockZ(), loc2.getBlockZ()) + 1; + DominionDTO dominion = new DominionDTO(player.getUniqueId(), "", loc1.getWorld(), + minX, minY, minZ, maxX, maxY, maxZ); + if (Dominion.config.getEconomyEnable()) { + if (!VaultConnect.instance.economyAvailable()) { + Notification.error(player, Translation.Messages_NoEconomyPlugin); + return; + } + int count; + if (Dominion.config.getEconomyOnlyXZ(player)) { + count = dominion.getSquare(); + } else { + count = dominion.getVolume(); + } + float price = count * Dominion.config.getEconomyPrice(player); + Notification.info(player, Translation.Tool_CreateDominionPrice, price, VaultConnect.instance.currencyNamePlural()); + } + Particle.showBorder(player, dominion); + Notification.info(player, Translation.Tool_DominionSize, dominion.getWidthX(), dominion.getHeight(), dominion.getWidthZ()); + Notification.info(player, Translation.Tool_DominionSquare, dominion.getSquare()); + Notification.info(player, Translation.Tool_DominionHeight, dominion.getHeight()); + Notification.info(player, Translation.Tool_DominionVolume, dominion.getVolume()); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void selectBlockToShowInfo(PlayerInteractEvent event) { + Player player = event.getPlayer(); + ItemStack item = player.getInventory().getItemInMainHand(); + + if (item.getType() != Dominion.config.getInfoTool()) { + return; + } + if (event.getAction() != Action.LEFT_CLICK_BLOCK) { + return; + } + event.setCancelled(true); + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + DominionDTO dominion = Cache.instance.getDominionByLoc(block.getLocation()); + if (dominion == null) { + Notification.info(player, Translation.Tool_LocationNotInDominion, block.getX(), block.getY(), block.getZ()); + } else { + Notification.info(player, Translation.Tool_LocationInDominion, block.getX(), block.getY(), block.getZ(), dominion.getName()); + Notification.info(player, Translation.Tool_DominionOwner, Cache.instance.getPlayerName(dominion.getOwner())); + } + } +}