From 9abf670a208478e5270c21bb55e412867797833a Mon Sep 17 00:00:00 2001 From: xzy <2417481092@qq.com> Date: Sun, 22 Jan 2023 16:39:03 +0800 Subject: [PATCH] =?UTF-8?q?WindLeaf=E6=9D=A5=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flywars/game/gui/GuiManager.java | 10 ++++----- .../flywars/game/items/ArrowEntry.java | 4 ++-- .../game/items/arrows/ExplosiveArrow.java | 2 +- .../flywars/game/items/arrows/FireArrow.java | 2 +- .../flywars/game/items/arrows/FlagArrow.java | 3 +-- .../flywars/game/items/arrows/SlowArrow.java | 5 ++--- .../game/items/arrows/TeleportArrow.java | 2 +- .../flywars/listeners/GameEventListener.java | 4 +--- .../listeners/PlayerEventListener.java | 18 +++++++-------- .../xzystudio/flywars/utils/GameUtils.java | 22 ++++++++++++++----- 10 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/gui/GuiManager.java b/src/main/java/top/mpt/xzystudio/flywars/game/gui/GuiManager.java index 3871e6c..eb70de4 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/gui/GuiManager.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/gui/GuiManager.java @@ -32,7 +32,7 @@ public class GuiManager { * @return TeamInfo */ private static TeamInfo getInfo(Player player){ - GameTeam team = GameUtils.getTeam(player, null); + GameTeam team = GameUtils.getTeamByPlayer(player, null); return team != null ? ScoreboardManager.info.get(team) : null; } @@ -59,10 +59,8 @@ public static void openGui(Player player) { } Inventory inv = Bukkit.createInventory(player, InventoryType.CHEST, title); - items.forEach(it -> { - inv.addItem(it.getItem()); - }); - GameTeam team = GameUtils.getTeam(player, null); + items.forEach(it -> inv.addItem(it.getItem())); + GameTeam team = GameUtils.getTeamByPlayer(player, null); List lores = Arrays.asList("#GREEN#击杀数:" + info.getKillCount(), "#AQUA#所属队伍:" + (team != null ? team.getTeamDisplayName() : "[无法获取]")); inv.setItem(22, GameUtils.newItem(Material.PLAYER_HEAD, "#YELLOW#" + player.getName(), lores, 1, false, 0, null)); player.openInventory(inv); @@ -73,7 +71,7 @@ public static void processClick(InventoryClickEvent event) { if (rawSlot >= items.size()) return; GuiItem item = items.get(rawSlot); Player p = (Player) event.getWhoClicked(); - GameTeam team = GameUtils.getTeam(p, null); + GameTeam team = GameUtils.getTeamByPlayer(p, null); TeamInfo info = ScoreboardManager.info.get(team); event.getClickedInventory().addItem(item.item); diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/ArrowEntry.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/ArrowEntry.java index 8375f2d..5e82208 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/ArrowEntry.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/ArrowEntry.java @@ -22,8 +22,8 @@ public Object get(String string, Object... defaultValue){ } String path = info.path(); Object ob = ConfigUtils.getConfig(String.format("arrow.%s.%s", path, string)); - if (ob == null){ - ob = ConfigUtils.getConfig("arrow.default."+string, defaultValue); + if (ob == null) { + ob = ConfigUtils.getConfig("arrow.default." + string, defaultValue); } return ob; } diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/ExplosiveArrow.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/ExplosiveArrow.java index c40d601..f5a7635 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/ExplosiveArrow.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/ExplosiveArrow.java @@ -7,7 +7,7 @@ import top.mpt.xzystudio.flywars.game.items.ArrowEntry; import top.mpt.xzystudio.flywars.game.items.ArrowInfo; -@ArrowInfo(name = "#DARK_RED#爆炸箭", path="explosive") +@ArrowInfo(name = "#DARK_RED#爆炸箭", path = "explosive") public class ExplosiveArrow extends ArrowEntry { @Override public void run(Player shooter, Player entity, Arrow arrow) { diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FireArrow.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FireArrow.java index e458d96..5a53da6 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FireArrow.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FireArrow.java @@ -10,6 +10,6 @@ public class FireArrow extends ArrowEntry { @Override public void run(Player shooter, Player entity, Arrow arrow) { - GameUtils.getTeam(entity, t -> t.players.keySet().forEach(player -> player.setFireTicks((Integer) get("ticks")))); + GameUtils.getTeamByPlayer(entity, t -> t.players.keySet().forEach(player -> player.setFireTicks((Integer) get("ticks")))); } } diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FlagArrow.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FlagArrow.java index 76ceb85..3fcd286 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FlagArrow.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/FlagArrow.java @@ -5,8 +5,7 @@ import top.mpt.xzystudio.flywars.game.items.ArrowEntry; import top.mpt.xzystudio.flywars.game.items.ArrowInfo; -// TODO 光灵箭不是默认就可以标记玩家吗? -@ArrowInfo(name = "#YELLOW#标记箭", path="flag") +@ArrowInfo(name = "#YELLOW#标记箭", path = "flag") public class FlagArrow extends ArrowEntry { @Override public void run(Player shooter, Player entity, Arrow arrow) { diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/SlowArrow.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/SlowArrow.java index ec1d82f..97582a3 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/SlowArrow.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/SlowArrow.java @@ -8,12 +8,11 @@ import top.mpt.xzystudio.flywars.game.items.ArrowInfo; import top.mpt.xzystudio.flywars.utils.GameUtils; -// TODO 在空中飞行怎么缓慢? -@ArrowInfo(name = "#AQUA#寒冰箭", path="slow") +@ArrowInfo(name = "#AQUA#寒冰箭", path = "slow") public class SlowArrow extends ArrowEntry { @Override public void run(Player shooter, Player entity, Arrow arrow) { - GameUtils.getTeam(entity, t -> t.players.keySet().forEach(player -> player.addPotionEffect( + GameUtils.getTeamByPlayer(entity, t -> t.players.keySet().forEach(player -> player.addPotionEffect( new PotionEffect(PotionEffectType.SLOW, (Integer) get("duration"), (Integer) get("amplifier")) ))); } diff --git a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/TeleportArrow.java b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/TeleportArrow.java index 7dcf30d..1f51d55 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/TeleportArrow.java +++ b/src/main/java/top/mpt/xzystudio/flywars/game/items/arrows/TeleportArrow.java @@ -12,7 +12,7 @@ public class TeleportArrow extends ArrowEntry { @Override public void run(Player shooter, Player entity, Arrow arrow){ // 获取到P1(被骑乘者) - GameUtils.getTeam(entity, team -> { + GameUtils.getTeamByPlayer(entity, team -> { Player p1 = team.getP1(); // 传送被骑乘者到射击者那里 - 如果直接传送entity会造成entity从p1身上掉下来 p1.teleport(shooter.getLocation()); diff --git a/src/main/java/top/mpt/xzystudio/flywars/listeners/GameEventListener.java b/src/main/java/top/mpt/xzystudio/flywars/listeners/GameEventListener.java index 3bd9a34..8bd516d 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/listeners/GameEventListener.java +++ b/src/main/java/top/mpt/xzystudio/flywars/listeners/GameEventListener.java @@ -76,9 +76,7 @@ public void onGameOver(GameOverEvent event) { if (Game.resUpdater != null) Game.resUpdater.cancel(); // 清除世界内的掉落物 event.getWinner().getP1().getWorld().getEntities().forEach(it -> { - if (it.getType() == EntityType.DROPPED_ITEM){ - it.remove(); - } + if (it.getType() == EntityType.DROPPED_ITEM) it.remove(); }); // 遍历teams数组 Game.teams.forEach(team -> { diff --git a/src/main/java/top/mpt/xzystudio/flywars/listeners/PlayerEventListener.java b/src/main/java/top/mpt/xzystudio/flywars/listeners/PlayerEventListener.java index 34fe2d6..73270e3 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/listeners/PlayerEventListener.java +++ b/src/main/java/top/mpt/xzystudio/flywars/listeners/PlayerEventListener.java @@ -11,6 +11,7 @@ import org.spigotmc.event.entity.EntityDismountEvent; import top.mpt.xzystudio.flywars.events.TeamEliminatedEvent; import top.mpt.xzystudio.flywars.game.Game; +import top.mpt.xzystudio.flywars.game.gui.GuiItem; import top.mpt.xzystudio.flywars.game.gui.GuiManager; import top.mpt.xzystudio.flywars.game.team.GameTeam; import top.mpt.xzystudio.flywars.utils.GameUtils; @@ -28,7 +29,7 @@ public void onPlayerQuit(PlayerQuitEvent event) { // 当玩家退出游戏时 // 拿到玩家 Player p = event.getPlayer(); - GameUtils.getTeam(p, team -> { + GameUtils.getTeamByPlayer(p, team -> { Player op = team.getTheOtherPlayer(p); PlayerUtils.send(op, "[FlyWars] #RED#你的队友退出了游戏!"); TeamEliminatedEvent eliminatedEvent = new TeamEliminatedEvent(p, team, null); @@ -41,7 +42,7 @@ public void onEntityDamage(EntityDamageByEntityEvent event) { // 当玩家被其他玩家打的时候 if (event.getEntity().getType() == EntityType.PLAYER){ Player p = (Player) event.getEntity(); - GameUtils.getTeam(p, t -> Game.scoreboardManager.renderScoreboard()); + GameUtils.getTeamByPlayer(p, t -> Game.scoreboardManager.renderScoreboard()); } if (event.getDamager().getType() == EntityType.SPECTRAL_ARROW){ @@ -53,12 +54,9 @@ public void onEntityDamage(EntityDamageByEntityEvent event) { } Player p = (Player) event.getEntity(); Arrow arrow = (Arrow) event.getDamager(); - - GuiManager.items.forEach(it -> { - if (Objects.equals(it.name, arrow.getName())){ - it.process.run(pShoot, p, arrow); - } - }); + + GuiItem item = GameUtils.find(GuiManager.items, i -> Objects.equals(i.name, arrow.getName())); + if (item != null) item.process.run(pShoot, p, arrow); } } @@ -92,7 +90,7 @@ public void onPlayerDeath(PlayerDeathEvent event) { } } else { LoggerUtils.info("#RED#未获取到击杀者!"); - GameUtils.getTeam(p, team -> { + GameUtils.getTeamByPlayer(p, team -> { TeamEliminatedEvent eliminatedEvent = new TeamEliminatedEvent(p, team, null); GameUtils.callEvent(eliminatedEvent); }); @@ -129,7 +127,7 @@ public void onEntityDismount(EntityDismountEvent event) { public void onEntityShootBow(EntityShootBowEvent event) { Entity entity = event.getEntity(); if (entity.getType() == EntityType.PLAYER) { - GameUtils.getTeam((Player) entity, t -> event.getProjectile().setCustomName(event.getEntity().getName())); + GameUtils.getTeamByPlayer((Player) entity, t -> event.getProjectile().setCustomName(event.getEntity().getName())); } } } diff --git a/src/main/java/top/mpt/xzystudio/flywars/utils/GameUtils.java b/src/main/java/top/mpt/xzystudio/flywars/utils/GameUtils.java index 3c3df9a..b7fe31d 100644 --- a/src/main/java/top/mpt/xzystudio/flywars/utils/GameUtils.java +++ b/src/main/java/top/mpt/xzystudio/flywars/utils/GameUtils.java @@ -73,17 +73,27 @@ public static ItemStack newItem(Material type, String displayName, int amount){ return newItem(type, displayName, Collections.emptyList(), amount, false, 0, null); } + /** + * 根据所给的条件找到对应的对象 + * @param function 判断匿名函数 + * @return 对象,不存在则为 `null` + * @param 对象泛型 + */ + public static T find(Collection collection, Function function) { + AtomicReference result = new AtomicReference<>(); + collection.forEach(t -> { + if (function.apply(t)) result.set(t); + }); + return result.get(); + } + /** * 根据所给的条件找到对应的游戏队伍对象 * @param function 判断匿名函数 * @return 游戏队伍对象,不存在则为 `null` */ public static GameTeam getTeamBy(Function function) { - AtomicReference result = new AtomicReference<>(); - Game.teams.forEach(team -> { - if (function.apply(team)) result.set(team); - }); - return result.get(); + return find(Game.teams, function); } /** @@ -92,7 +102,7 @@ public static GameTeam getTeamBy(Function function) { * @param consumer 获取游戏队伍后要做的事情 * @return 游戏队伍对象,不存在则为 `null` */ - public static GameTeam getTeam(Player player, Consumer consumer) { + public static GameTeam getTeamByPlayer(Player player, Consumer consumer) { GameTeam team = getTeamBy(t -> t.isPlayerInTeam(player)); if (consumer != null) consumer.accept(team); return team;