feat: add team chat func + handle water bucket in court

This commit is contained in:
hexlocation pc 2024-07-24 20:32:05 +02:00
parent 55fc458c02
commit fc27e415f4
6 changed files with 99 additions and 3 deletions

View file

@ -2,18 +2,22 @@ package rip.iwakura.civilcore;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
import rip.iwakura.civilcore.commands.TeamChatCommand;
import rip.iwakura.civilcore.commands.TeamCommand;
import rip.iwakura.civilcore.discord.Discord;
public class CivilCore extends JavaPlugin {
public Database db = new Database();
public Discord bot;
public ArrayList<Player> teamChatRegister = new ArrayList<>();
@Override
public void onEnable() {
@ -41,5 +45,6 @@ public class CivilCore extends JavaPlugin {
getServer().getPluginManager().registerEvents(new Court(), this);
this.getCommand("team").setExecutor(new TeamCommand(this));
this.getCommand("court").setExecutor(new Court());
this.getCommand("tc").setExecutor(new TeamChatCommand(this));
}
}

View file

@ -15,12 +15,20 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
public class Court implements Listener,CommandExecutor{
private World court_world = Bukkit.getWorld("court");
private Location court_location = new Location(court_world, 4.5, 70.0, 0.5, -90f, 1.0f);
@EventHandler
public void PlayerPlaceWater(PlayerBucketEmptyEvent e) {
Player p = (Player) e.getPlayer();
if (!p.getLocation().getWorld().getName().equals("court")) return;
e.setCancelled(true);
}
@EventHandler
public void PlayerHurt(EntityDamageEvent e) {
if (!(e.getEntity() instanceof Player)) return;

View file

@ -82,6 +82,17 @@ public class Database {
rs.close();
}
public List<DbPlayer> getPlayersInTeam(String team_name) {
List<DbPlayer> filtered = players.stream().filter(
s -> {
if (s.team.name == null) return false;
return s.team.name.equals(team_name);
})
.collect(Collectors.toList());
if(filtered.isEmpty()) return null;
return filtered;
}
public void refreshPlayers() throws SQLException {
players.clear();
String sql = "SELECT players.NAME AS player_name, teams.NAME AS team_name, teams.TEAM_ID as t_team_id, teams.PREFIX AS team_prefix " +

View file

@ -1,6 +1,7 @@
package rip.iwakura.civilcore;
import java.sql.SQLException;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -26,14 +27,34 @@ public class PlayerHandler implements Listener {
Player p = e.getPlayer();
String player_name = p.getName();
boolean inTeamChat = civilCore.teamChatRegister.contains(p);
DbPlayer dbPlayer = civilCore.db.getPlayerByName(player_name);
String prefix = dbPlayer.team.prefix == null ? "" : ChatColor.translateAlternateColorCodes('&', dbPlayer.team.prefix) + " ";
String prefix = (inTeamChat ? ChatColor.DARK_BLUE + "TC " + ChatColor.RESET : "")
+ (dbPlayer.team.prefix == null ?
"" :
ChatColor.translateAlternateColorCodes('&', dbPlayer.team.prefix) + " ");
String message = String.format("%s%s: %s", prefix, player_name, e.getMessage());
Bukkit.broadcastMessage(String.format("%s%s: %s", prefix, player_name, e.getMessage()));
if (!inTeamChat) {
Bukkit.broadcastMessage(message);
civilCore.bot.sendMessage(e);
} else {
List<DbPlayer> players = civilCore.db.getPlayersInTeam(dbPlayer.team.name);
if (players == null) return;
for (DbPlayer team_dbp : players) {
Player team_p = Bukkit.getPlayer(team_dbp.name);
if (team_p == null) continue;
team_p.sendMessage(message);
}
}
civilCore.bot.sendMessage(e);
}
@EventHandler
public void PlayerJoinEvent(PlayerJoinEvent e) {
Player p = e.getPlayer();

View file

@ -0,0 +1,48 @@
package rip.iwakura.civilcore.commands;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerEvent;
import net.md_5.bungee.api.ChatColor;
import rip.iwakura.civilcore.Utils;
import rip.iwakura.civilcore.CivilCore;
import rip.iwakura.civilcore.types.DbPlayer;
import rip.iwakura.civilcore.types.Team;
public class TeamChatCommand implements CommandExecutor {
private CivilCore civilCore = null;
public TeamChatCommand(CivilCore civilCore) {
this.civilCore = civilCore;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
args = Utils.parser(args).toArray(new String[0]);
if (!(sender instanceof Player)) return true;
Player p = (Player) sender;
if (civilCore.teamChatRegister.contains(p)) {
civilCore.teamChatRegister.remove(p);
p.sendMessage(ChatColor.RED + "Team chat has been disabled.");
return true;
}
if (civilCore.db.getPlayerByName(p.getName()).team.name == null) {
p.sendMessage(ChatColor.RED + "You aren't in a team :(");
return true;
}
civilCore.teamChatRegister.add(p);
p.sendMessage(ChatColor.GREEN + "Team chat has been enabled.");
return true;
}
}

View file

@ -12,3 +12,6 @@ commands:
court:
description: Court Manager
usage: /<command> <enter|leave>
tc:
description: Team Chat
usage: /<command>