Refactor snowball handling and improve player interactions

Renamed OnSnowballHit to SnowballEvent and expanded functionality to handle projectile launches. Added InventoryItemInteractionEvent to manage item pickup and drop behavior. Updated player join logic to clear inventory, assign players to teams, and teleport them appropriately based on game state.
This commit is contained in:
Teriuihi 2025-02-07 23:48:40 +01:00
parent e7ce788086
commit 6f77135426
6 changed files with 109 additions and 13 deletions

View File

@ -4,9 +4,10 @@ import com.alttd.ctf.commands.CommandManager;
import com.alttd.ctf.config.Config;
import com.alttd.ctf.config.GameConfig;
import com.alttd.ctf.config.Messages;
import com.alttd.ctf.events.InventoryItemInteractionEvent;
import com.alttd.ctf.events.OnPlayerDeath;
import com.alttd.ctf.events.OnPlayerJoin;
import com.alttd.ctf.events.OnSnowballHit;
import com.alttd.ctf.events.SnowballEvent;
import com.alttd.ctf.flag.Flag;
import com.alttd.ctf.flag.FlagTryCaptureEvent;
import com.alttd.ctf.game.GameManager;
@ -66,9 +67,10 @@ public class Main extends JavaPlugin {
private void registerEvents(Flag flag) {
PluginManager pluginManager = getServer().getPluginManager();
//TODO add event for player joining and clear their inv
pluginManager.registerEvents(new OnSnowballHit(gameManager), this);
pluginManager.registerEvents(new SnowballEvent(gameManager), this);
pluginManager.registerEvents(new FlagTryCaptureEvent(flag), this);
pluginManager.registerEvents(new OnPlayerDeath(gameManager), this);
pluginManager.registerEvents(new InventoryItemInteractionEvent(), this);
pluginManager.registerEvents(new OnPlayerJoin(gameManager, flag), this);
pluginManager.registerEvents(new GUIListener(), this);
}

View File

@ -0,0 +1,29 @@
package com.alttd.ctf.events;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAttemptPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
public class InventoryItemInteractionEvent implements Listener {
@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
Material type = event.getItemDrop().getItemStack().getType();
if (type.equals(Material.SNOWBALL) || type.equals(Material.SNOW_BLOCK)) {
return;
}
event.setCancelled(true);
}
@EventHandler
public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
Material type = event.getItem().getItemStack().getType();
if (type.equals(Material.SNOWBALL) || type.equals(Material.SNOW_BLOCK)) {
return;
}
event.setCancelled(true);
}
}

View File

@ -2,6 +2,9 @@ package com.alttd.ctf.events;
import com.alttd.ctf.flag.Flag;
import com.alttd.ctf.game.GameManager;
import com.alttd.ctf.game.GamePhase;
import com.alttd.ctf.team.Team;
import com.alttd.ctf.team.TeamPlayer;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
@ -10,13 +13,16 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.Comparator;
import java.util.Optional;
@Slf4j
public class OnPlayerJoin implements Listener {
private final GameManager gameManager;
private final Flag flag;
public OnPlayerJoin(GameManager gameManager, Flag flag) {//TODO remove player from team when they leave the game
public OnPlayerJoin(GameManager gameManager, Flag flag) {
this.gameManager = gameManager;
this.flag = flag;
}
@ -31,11 +37,31 @@ public class OnPlayerJoin implements Listener {
}
maxHealthAttribute.setBaseValue(20);
player.setHealth(20);
flag.addPlayer(player);
if (gameManager.getGamePhase().isEmpty()) {
player.getInventory().clear();
player.updateInventory();
Optional<GamePhase> optionalGamePhase = gameManager.getGamePhase();
if (optionalGamePhase.isEmpty()) {
return;
}
//TODO other stuff based on game state (like adding them to a team etc)
GamePhase gamePhase = optionalGamePhase.get();
if (gamePhase.equals(GamePhase.ENDED)) {
return;
}
Optional<TeamPlayer> optionalTeamPlayer = gameManager.getTeamPlayer(player.getUniqueId());
TeamPlayer teamPlayer;
if (optionalTeamPlayer.isEmpty()) {
Optional<Team> min = gameManager.getTeams().stream().min(Comparator.comparingInt(team -> team.getPlayers().size()));
if (min.isEmpty()) {
log.error("No team found when attempting to add freshly joined player to a team");
return;
}
teamPlayer = min.get().addPlayer(player.getUniqueId());
} else {
teamPlayer = optionalTeamPlayer.get();
}
player.teleportAsync(teamPlayer.getTeam().getSpawnLocation());
}
}

View File

@ -6,19 +6,21 @@ import com.alttd.ctf.game_class.GameClass;
import com.alttd.ctf.team.TeamPlayer;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Material;
import org.bukkit.entity.Snowball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import java.util.Optional;
@Slf4j
public class OnSnowballHit implements Listener {
public class SnowballEvent implements Listener {
private final GameManager gameManager;
public OnSnowballHit(GameManager gameManager) {
public SnowballEvent(GameManager gameManager) {
this.gameManager = gameManager;
}
@ -27,9 +29,14 @@ public class OnSnowballHit implements Listener {
void apply(Player hitPlayer, Player shooter, TeamPlayer shooterTeamPlayer);
}
@FunctionalInterface
private interface SnowballThrownConsumer {
void apply(Player shooter, TeamPlayer shooterTeamPlayer);
}
@EventHandler
public void onSnowballHit(EntityDamageByEntityEvent event) {
handle(event, (hitPlayer, shooter, shooterTeamPlayer) -> {
handleSnowballHit(event, (hitPlayer, shooter, shooterTeamPlayer) -> {
GameClass shooterClass = shooterTeamPlayer.getGameClass();
shooter.setCooldown(Material.SNOWBALL, shooterClass.getThrowTickSpeed());
@ -40,7 +47,37 @@ public class OnSnowballHit implements Listener {
});
}
private void handle(EntityDamageByEntityEvent event, SnowballHitConsumer consumer) {
@EventHandler
public void onSnowballHitBySnowball(ProjectileLaunchEvent event) {
handleSnowballThrown(event, (shooter, shooterTeamPlayer) -> {
GameClass shooterClass = shooterTeamPlayer.getGameClass();
shooter.setCooldown(Material.SNOWBALL, shooterClass.getThrowTickSpeed());
});
}
private void handleSnowballThrown(ProjectileLaunchEvent event, SnowballThrownConsumer consumer) {
Optional<GamePhase> optionalGamePhase = gameManager.getGamePhase();
if (optionalGamePhase.isEmpty()) {
return;
}
if (!(event.getEntity() instanceof Snowball snowball)) {
log.debug("Something other than a snowball was thrown");
return;
}
if (!(snowball.getShooter() instanceof Player shooter)) {
log.debug("The snowball shooter wasn't a player");
return;
}
Optional<TeamPlayer> teamPlayer = gameManager.getTeamPlayer(shooter.getUniqueId());
if (teamPlayer.isEmpty()) {
log.debug("The shooter that threw a snowball was not a team player");
return;
}
consumer.apply(shooter, teamPlayer.get());
}
private void handleSnowballHit(EntityDamageByEntityEvent event, SnowballHitConsumer consumer) {
Optional<GamePhase> optionalGamePhase = gameManager.getGamePhase();
if (optionalGamePhase.isEmpty()) {
log.debug("No game is running but player was hit by snowball");

View File

@ -39,9 +39,11 @@ public class Team {
@Getter
private TeamColor color;
public void addPlayer(UUID uuid) {
players.put(uuid, new TeamPlayer(uuid, this));
public TeamPlayer addPlayer(UUID uuid) {
TeamPlayer teamPlayer = new TeamPlayer(uuid, this);
players.put(uuid, teamPlayer);
log.debug("Added player with uuid {} to team with id {}", uuid, id);
return teamPlayer;
}
public Optional<TeamPlayer> getPlayer(@NotNull UUID uuid) {

View File

@ -1,3 +1,3 @@
#Fri Feb 07 23:09:43 CET 2025
buildNumber=8
#Fri Feb 07 23:34:22 CET 2025
buildNumber=9
version=0.1