From 828406220fb0ef16a5b3c188ebc6ea8d7d7aa3be Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 11:38:38 +0100 Subject: [PATCH] Add CommandAccept Add CommandDecline Add CommandInvite Add InvitationSystem --- src/de/steamwar/misslewars/MWTeam.java | 23 +++++++- src/de/steamwar/misslewars/MissileWars.java | 21 +++++++- .../misslewars/commands/CommandAccept.java | 53 ++++++++++++++++++ .../misslewars/commands/CommandDecline.java | 54 +++++++++++++++++++ .../misslewars/commands/CommandInvite.java | 50 ++++++++++++++++- .../listener/ConnectionListener.java | 2 + src/plugin.yml | 4 +- 7 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 src/de/steamwar/misslewars/commands/CommandAccept.java create mode 100644 src/de/steamwar/misslewars/commands/CommandDecline.java diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 59e189b..111e30e 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars; +import de.steamwar.misslewars.items.Missile; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; @@ -132,6 +133,17 @@ public class MWTeam { if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING) MissileWars.end(WinReasons.NO_ENEMY, enemy()); } + + public void invitePlayer(Player p) { + if (enemy().openInvitations.contains(p)) return; + + openInvitations.add(p); + } + + public void acceptInvite(Player p) { + removeInvitations(p); + join(p); + } private MWTeam enemy() { if (this == MissileWars.getRedTeam()) @@ -144,12 +156,21 @@ public class MWTeam { return "§" + color.getChar(); } - public boolean hasPlayer (Player p) { + public boolean hasPlayer(Player p) { return players.contains(p); } + public boolean hasInvite(Player p) { + return openInvitations.contains(p); + } + public String getColoredName() { return color.toString() + teamName; } + public static void removeInvitations(Player p) { + MissileWars.getRedTeam().openInvitations.remove(p); + MissileWars.getBlueTeam().openInvitations.remove(p); + } + } diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index ad90c0c..f28d455 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -19,6 +19,9 @@ package de.steamwar.misslewars; +import de.steamwar.misslewars.commands.CommandAccept; +import de.steamwar.misslewars.commands.CommandDecline; +import de.steamwar.misslewars.commands.CommandInvite; import de.steamwar.misslewars.commands.CommandSpectate; import de.steamwar.misslewars.countdowns.EndCountdown; import de.steamwar.misslewars.countdowns.ItemCountdown; @@ -29,6 +32,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Sound; +import org.bukkit.command.CommandExecutor; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -62,7 +66,14 @@ public class MissileWars extends JavaPlugin { new FightListener(); new ChatListener(); getCommand("spectate").setExecutor(new CommandSpectate()); - getCommand("invite").setExecutor(new CommandSpectate()); + + // Invitation Commands + CommandInvite commandInvite = new CommandInvite(); + getCommand("invite").setExecutor(commandInvite); + getCommand("invite").setTabCompleter(commandInvite); + + getCommand("accept").setExecutor(new CommandAccept()); + getCommand("decline").setExecutor(new CommandDecline()); new WaitingCountdown(); new ItemCountdown(); @@ -137,6 +148,14 @@ public class MissileWars extends JavaPlugin { return null; } + public static MWTeam getInvitation(Player p) { + if(blueTeam.hasInvite(p)) + return blueTeam; + if(redTeam.hasInvite(p)) + return redTeam; + return null; + } + public static void join(Player p) { if (MissileWars.getRedTeam().size() < MissileWars.getBlueTeam().size()) { MissileWars.getRedTeam().join(p); diff --git a/src/de/steamwar/misslewars/commands/CommandAccept.java b/src/de/steamwar/misslewars/commands/CommandAccept.java new file mode 100644 index 0000000..26383e4 --- /dev/null +++ b/src/de/steamwar/misslewars/commands/CommandAccept.java @@ -0,0 +1,53 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.misslewars.commands; + +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MWTeam; +import de.steamwar.misslewars.MissileWars; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandAccept implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) return false; + Player player = (Player) sender; + if (!Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } + + MWTeam teamInvitation = MissileWars.getInvitation(player); + if (teamInvitation == null) { + player.sendMessage("§cDu wurdest nicht eingeladen."); + return false; + } + + teamInvitation.acceptInvite(player); + return false; + } + +} diff --git a/src/de/steamwar/misslewars/commands/CommandDecline.java b/src/de/steamwar/misslewars/commands/CommandDecline.java new file mode 100644 index 0000000..e64afab --- /dev/null +++ b/src/de/steamwar/misslewars/commands/CommandDecline.java @@ -0,0 +1,54 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.misslewars.commands; + +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MWTeam; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.items.Missile; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandDecline implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) return false; + Player player = (Player) sender; + if (!Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } + + MWTeam teamInvitation = MissileWars.getInvitation(player); + if (teamInvitation == null) { + player.sendMessage("§cDu wurdest nicht eingeladen."); + return false; + } + + MWTeam.removeInvitations(player); + return false; + } + +} diff --git a/src/de/steamwar/misslewars/commands/CommandInvite.java b/src/de/steamwar/misslewars/commands/CommandInvite.java index 8126ca8..d272fc2 100644 --- a/src/de/steamwar/misslewars/commands/CommandInvite.java +++ b/src/de/steamwar/misslewars/commands/CommandInvite.java @@ -22,12 +22,19 @@ package de.steamwar.misslewars.commands; import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MissileWars; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; -public class CommandInvite implements CommandExecutor { +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class CommandInvite implements CommandExecutor, TabCompleter { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -37,7 +44,48 @@ public class CommandInvite implements CommandExecutor { player.sendMessage("§cDieser Command ist deaktiviert."); return false; } + + if (!Config.RedLeader.equals(player.getUniqueId()) && !Config.BlueLeader.equals(player.getUniqueId())) { + player.sendMessage("§cDu kannst keine Spieler einladen."); + return false; + } + if (MissileWars.getTeam(player) == null) { + player.sendMessage("§cDu kannst keine Spieler einladen."); + return false; + } + + if (args.length != 1) { + player.sendMessage("§c/invite "); + return false; + } + + Player invitedPlayer = Bukkit.getPlayer(args[0]); + if (MissileWars.getTeam(invitedPlayer) != null) { + player.sendMessage("§cDieser Spieler ist bereits in einem Team."); + return false; + } + if (MissileWars.getInvitation(invitedPlayer) != null) { + player.sendMessage("§cDieser Spieler wurde bereits eingeladen."); + return false; + } + + MissileWars.getTeam(player).invitePlayer(invitedPlayer); + invitedPlayer.sendMessage("§7Du wurdest von §6" + player.getName() + "§7 in das Team §6" + MissileWars.getTeam(player).getColoredName() + "§7 eingeladen."); + invitedPlayer.sendMessage("§8/§6accept §8- §7Zum akzeptieren."); + invitedPlayer.sendMessage("§8/§6decline §8- §7Zum ablehnen."); return false; } + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (args.length != 1) return new ArrayList<>(); + return Bukkit.getOnlinePlayers() + .stream() + .filter(p -> MissileWars.getTeam(p) != null) + .filter(p -> MissileWars.getInvitation(p) != null) + .map(Player::getName) + .filter(s -> s.startsWith(args[0])) + .collect(Collectors.toList()); + } + } diff --git a/src/de/steamwar/misslewars/listener/ConnectionListener.java b/src/de/steamwar/misslewars/listener/ConnectionListener.java index 7c556ed..549393f 100644 --- a/src/de/steamwar/misslewars/listener/ConnectionListener.java +++ b/src/de/steamwar/misslewars/listener/ConnectionListener.java @@ -20,6 +20,7 @@ package de.steamwar.misslewars.listener; import de.steamwar.misslewars.FightState; +import de.steamwar.misslewars.MWTeam; import de.steamwar.misslewars.MissileWars; import org.bukkit.GameMode; import org.bukkit.event.EventHandler; @@ -42,6 +43,7 @@ public class ConnectionListener extends BasicListener{ @EventHandler public void onLeave(PlayerQuitEvent e) { + MWTeam.removeInvitations(e.getPlayer()); MissileWars.leave(e.getPlayer()); } diff --git a/src/plugin.yml b/src/plugin.yml index c95edb1..b05dc54 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -12,4 +12,6 @@ depend: - SpigotCore commands: spectate: - invite: \ No newline at end of file + invite: + accept: + decline: \ No newline at end of file