|
| 1 | +/* |
| 2 | + * Copyright (C) <2024> <XiaoMoMi> |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.momirealms.customnameplates.bukkit.requirement.builtin; |
| 19 | + |
| 20 | +import net.momirealms.customnameplates.api.CNPlayer; |
| 21 | +import net.momirealms.customnameplates.backend.requirement.AbstractRequirement; |
| 22 | +import org.bukkit.Bukkit; |
| 23 | +import org.bukkit.entity.Player; |
| 24 | +import org.bukkit.scoreboard.Team; |
| 25 | + |
| 26 | +public class TeammatesRequirement extends AbstractRequirement { |
| 27 | + private final boolean is; |
| 28 | + |
| 29 | + public TeammatesRequirement(int refreshInterval, boolean is) { |
| 30 | + super(refreshInterval); |
| 31 | + this.is = is; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public boolean isSatisfied(CNPlayer p1, CNPlayer p2) { |
| 36 | + Player player1 = (Player) p1.player(); |
| 37 | + Player player2 = (Player) p2.player(); |
| 38 | + Team team1 = Bukkit.getScoreboardManager().getMainScoreboard().getEntityTeam(player1); |
| 39 | + Team team2 = Bukkit.getScoreboardManager().getMainScoreboard().getEntityTeam(player2); |
| 40 | + if (this.is) { |
| 41 | + return team1 != null && team1.equals(team2); |
| 42 | + } else { |
| 43 | + return team1 == null || !team1.equals(team2); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String type() { |
| 49 | + return "teammates"; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean equals(Object o) { |
| 53 | + if (this == o) return true; |
| 54 | + if (o == null || getClass() != o.getClass()) return false; |
| 55 | + TeammatesRequirement that = (TeammatesRequirement) o; |
| 56 | + return that.is == this.is; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int hashCode() { |
| 61 | + return this.is ? 17 : 3; |
| 62 | + } |
| 63 | +} |
0 commit comments