Skip to content

Commit

Permalink
WindLeaf来力
Browse files Browse the repository at this point in the history
  • Loading branch information
Gingmzmzx committed Jan 22, 2023
1 parent 95fe878 commit 9abf670
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 35 deletions.
10 changes: 4 additions & 6 deletions src/main/java/top/mpt/xzystudio/flywars/game/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<String> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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){
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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()));
}
}
}
22 changes: 16 additions & 6 deletions src/main/java/top/mpt/xzystudio/flywars/utils/GameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> 对象泛型
*/
public static <T> T find(Collection<T> collection, Function<T, Boolean> function) {
AtomicReference<T> 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<GameTeam, Boolean> function) {
AtomicReference<GameTeam> result = new AtomicReference<>();
Game.teams.forEach(team -> {
if (function.apply(team)) result.set(team);
});
return result.get();
return find(Game.teams, function);
}

/**
Expand All @@ -92,7 +102,7 @@ public static GameTeam getTeamBy(Function<GameTeam, Boolean> function) {
* @param consumer 获取游戏队伍后要做的事情
* @return 游戏队伍对象,不存在则为 `null`
*/
public static GameTeam getTeam(Player player, Consumer<GameTeam> consumer) {
public static GameTeam getTeamByPlayer(Player player, Consumer<GameTeam> consumer) {
GameTeam team = getTeamBy(t -> t.isPlayerInTeam(player));
if (consumer != null) consumer.accept(team);
return team;
Expand Down

0 comments on commit 9abf670

Please sign in to comment.