From 0f664153ec7660d08d1f3f911a18a84ac9690495 Mon Sep 17 00:00:00 2001 From: cswhite2000 <18whitechristop@gmail.com> Date: Sat, 1 Jul 2017 17:29:38 -0700 Subject: [PATCH] fix team-switch kit not always switching teams --- .../tc/oc/pgm/blitz/BlitzMatchModuleImpl.java | 2 +- .../tc/oc/pgm/ffa/FreeForAllMatchModule.java | 4 +- .../java/tc/oc/pgm/join/JoinMatchModule.java | 10 ++--- .../java/tc/oc/pgm/kits/TeamSwitchKit.java | 2 + PGM/src/main/java/tc/oc/pgm/match/Match.java | 2 +- .../main/java/tc/oc/pgm/match/MatchImpl.java | 12 +++--- PGM/src/main/java/tc/oc/pgm/spawns/Spawn.java | 8 ++++ .../tc/oc/pgm/spawns/SpawnAttributes.java | 15 ++++--- .../java/tc/oc/pgm/spawns/SpawnParser.java | 3 +- .../java/tc/oc/pgm/teams/TeamCommands.java | 2 +- .../java/tc/oc/pgm/teams/TeamMatchModule.java | 4 +- .../tc/oc/pgm/tracker/TrackerManifest.java | 4 ++ .../trackers/PlayerLocationTracker.java | 43 +++++++++++++++++++ .../anxuiz/tourney/listener/TeamListener.java | 2 +- 14 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 PGM/src/main/java/tc/oc/pgm/tracker/trackers/PlayerLocationTracker.java diff --git a/PGM/src/main/java/tc/oc/pgm/blitz/BlitzMatchModuleImpl.java b/PGM/src/main/java/tc/oc/pgm/blitz/BlitzMatchModuleImpl.java index 9e4c712..b509f41 100644 --- a/PGM/src/main/java/tc/oc/pgm/blitz/BlitzMatchModuleImpl.java +++ b/PGM/src/main/java/tc/oc/pgm/blitz/BlitzMatchModuleImpl.java @@ -236,7 +236,7 @@ public class BlitzMatchModuleImpl extends MatchModule implements BlitzMatchModul .stream() .filter(this::eliminated) .forEach(eliminated -> { - match.setPlayerParty(eliminated, match.getDefaultParty()); + match.setPlayerParty(eliminated, match.getDefaultParty(), false); world.spawnParticle(Particle.SMOKE_LARGE, eliminated.getLocation(), 5); }); victory.invalidateAndCheckEnd(); diff --git a/PGM/src/main/java/tc/oc/pgm/ffa/FreeForAllMatchModule.java b/PGM/src/main/java/tc/oc/pgm/ffa/FreeForAllMatchModule.java index c95f208..1cd6ddf 100644 --- a/PGM/src/main/java/tc/oc/pgm/ffa/FreeForAllMatchModule.java +++ b/PGM/src/main/java/tc/oc/pgm/ffa/FreeForAllMatchModule.java @@ -215,7 +215,7 @@ public class FreeForAllMatchModule extends MatchModule implements Listener, Join kickMe.sendMessage(Links.shopPlug("shop.plug.ffa.neverKicked")); kickMe.playSound(Sound.ENTITY_VILLAGER_HURT, kickMe.getBukkit().getLocation(), 1, 1); - getMatch().setPlayerParty(kickMe, getMatch().getDefaultParty()); + getMatch().setPlayerParty(kickMe, getMatch().getDefaultParty(), false); return true; } @@ -281,7 +281,7 @@ public class FreeForAllMatchModule extends MatchModule implements Listener, Join joining.sendWarning(new TranslatableComponent("command.gameplay.join.alreadyJoined"), false); } - return getMatch().setPlayerParty(joining, getTribute(joining)); + return getMatch().setPlayerParty(joining, getTribute(joining), false); } @EventHandler(priority = EventPriority.MONITOR) diff --git a/PGM/src/main/java/tc/oc/pgm/join/JoinMatchModule.java b/PGM/src/main/java/tc/oc/pgm/join/JoinMatchModule.java index a55f263..ad3ba97 100644 --- a/PGM/src/main/java/tc/oc/pgm/join/JoinMatchModule.java +++ b/PGM/src/main/java/tc/oc/pgm/join/JoinMatchModule.java @@ -179,7 +179,7 @@ public class JoinMatchModule extends MatchModule implements Listener, JoinHandle public boolean observe(MatchPlayer leaving) { final Party observers = getMatch().getDefaultParty(); leaving.sendMessage(new TranslatableComponent("team.join", observers.getComponentName())); - return getMatch().setPlayerParty(leaving, observers); + return getMatch().setPlayerParty(leaving, observers, false); } public boolean requestObserve(MatchPlayer leaving) { @@ -216,7 +216,7 @@ public class JoinMatchModule extends MatchModule implements Listener, JoinHandle } public boolean queueToJoin(MatchPlayer joining) { - boolean joined = getMatch().setPlayerParty(joining, queuedParticipants); + boolean joined = getMatch().setPlayerParty(joining, queuedParticipants, false); if(joined) { joining.sendMessage(new TranslatableComponent("ffa.join")); } @@ -255,7 +255,7 @@ public class JoinMatchModule extends MatchModule implements Listener, JoinHandle public boolean cancelQueuedJoin(MatchPlayer joining) { if(!isQueuedToJoin(joining)) return false; - if(getMatch().setPlayerParty(joining, getMatch().getDefaultParty())) { + if(getMatch().setPlayerParty(joining, getMatch().getDefaultParty(), false)) { joining.sendMessage(new Component(new TranslatableComponent("team.join.deferred.cancel"), ChatColor.YELLOW)); return true; } else { @@ -273,7 +273,7 @@ public class JoinMatchModule extends MatchModule implements Listener, JoinHandle // Send any leftover players to obs for(MatchPlayer joining : queue.getOrderedPlayers()) { - getMatch().setPlayerParty(joining, getMatch().getDefaultParty()); + getMatch().setPlayerParty(joining, getMatch().getDefaultParty(), false); } } @@ -304,7 +304,7 @@ public class JoinMatchModule extends MatchModule implements Listener, JoinHandle final Competitor competitor = getMatch().getLastCompetitor(event.getPlayerId()); if(competitor != null) { // Committed player is reconnecting - getMatch().setPlayerParty(event.getPlayer(), competitor); + getMatch().setPlayerParty(event.getPlayer(), competitor, false); return; } } diff --git a/PGM/src/main/java/tc/oc/pgm/kits/TeamSwitchKit.java b/PGM/src/main/java/tc/oc/pgm/kits/TeamSwitchKit.java index f82c78a..8cc6a78 100644 --- a/PGM/src/main/java/tc/oc/pgm/kits/TeamSwitchKit.java +++ b/PGM/src/main/java/tc/oc/pgm/kits/TeamSwitchKit.java @@ -3,6 +3,7 @@ package tc.oc.pgm.kits; import tc.oc.pgm.match.MatchPlayer; import tc.oc.pgm.teams.TeamFactory; import tc.oc.pgm.teams.TeamMatchModule; +import tc.oc.pgm.tracker.trackers.PlayerLocationTracker; public class TeamSwitchKit extends DelayedKit { private final TeamFactory team; @@ -14,6 +15,7 @@ public class TeamSwitchKit extends DelayedKit { @Override public void applyDelayed(MatchPlayer player, boolean force) { TeamMatchModule tmm = player.getMatch().needMatchModule(TeamMatchModule.class); + //PlayerLocationTracker.setLocation(player, player.getLocation()); tmm.forceJoin(player, tmm.team(team)); } diff --git a/PGM/src/main/java/tc/oc/pgm/match/Match.java b/PGM/src/main/java/tc/oc/pgm/match/Match.java index cbde497..6857bcf 100644 --- a/PGM/src/main/java/tc/oc/pgm/match/Match.java +++ b/PGM/src/main/java/tc/oc/pgm/match/Match.java @@ -612,7 +612,7 @@ public interface Match extends Audience, IMatchQuery, Filterable, M * This is the ONLY way that external code can change a player's party. * Any other methods that appear to do so are meant for internal use only. */ - boolean setPlayerParty(MatchPlayer player, Party newParty); + boolean setPlayerParty(MatchPlayer player, Party newParty, boolean force); /** * Commit the match, if it is not already committed. Commitment is a boolean diff --git a/PGM/src/main/java/tc/oc/pgm/match/MatchImpl.java b/PGM/src/main/java/tc/oc/pgm/match/MatchImpl.java index 3643dfa..d3bfb00 100644 --- a/PGM/src/main/java/tc/oc/pgm/match/MatchImpl.java +++ b/PGM/src/main/java/tc/oc/pgm/match/MatchImpl.java @@ -712,7 +712,7 @@ public class MatchImpl implements Match, ForwardingAudience { // If the player hasn't joined a party by this point, join the default party if(!player.hasParty()) { - setPlayerParty(player, getDefaultParty()); + setPlayerParty(player, getDefaultParty(), false); } return player; @@ -728,7 +728,7 @@ public class MatchImpl implements Match, ForwardingAudience { try { logger.fine("Removing player " + player); - setOrClearPlayerParty(player, null); + setOrClearPlayerParty(player, null, false); // As with enable, facets are disabled after the player is removed // from their party and all collections. @@ -811,8 +811,8 @@ public class MatchImpl implements Match, ForwardingAudience { } @Override - public boolean setPlayerParty(MatchPlayer player, Party newParty) { - return setOrClearPlayerParty(player, checkNotNull(newParty)); + public boolean setPlayerParty(MatchPlayer player, Party newParty, boolean force) { + return setOrClearPlayerParty(player, checkNotNull(newParty), force); } /** @@ -827,7 +827,7 @@ public class MatchImpl implements Match, ForwardingAudience { * - If the player is already in newParty, or if the party change is cancelled by {@link PlayerParticipationStopEvent}, * none of the above changes will happen, and the method will return false. */ - private boolean setOrClearPlayerParty(MatchPlayer player, @Nullable Party newParty) { + private boolean setOrClearPlayerParty(MatchPlayer player, @Nullable Party newParty, boolean force) { final Party oldParty = player.party; checkArgument(equals(player.getMatch()), "Player belongs to a different match"); @@ -847,7 +847,7 @@ public class MatchImpl implements Match, ForwardingAudience { throw new IllegalStateException("Nested party change: " + player + " tried to join " + newParty + " in the middle of joining " + nested); } - if(oldParty instanceof Competitor) { + if(oldParty instanceof Competitor && !force) { final PlayerParticipationStopEvent request = new PlayerParticipationStopEvent(player, (Competitor) oldParty); bukkitEventBus.callEvent(request); if(request.isCancelled() && newParty != null) { // Can't cancel this if the player is leaving the match diff --git a/PGM/src/main/java/tc/oc/pgm/spawns/Spawn.java b/PGM/src/main/java/tc/oc/pgm/spawns/Spawn.java index ba3316d..b663eed 100644 --- a/PGM/src/main/java/tc/oc/pgm/spawns/Spawn.java +++ b/PGM/src/main/java/tc/oc/pgm/spawns/Spawn.java @@ -2,6 +2,7 @@ package tc.oc.pgm.spawns; import java.util.Optional; +import org.bukkit.Bukkit; import org.bukkit.Location; import tc.oc.pgm.features.FeatureDefinition; import tc.oc.pgm.features.FeatureInfo; @@ -9,6 +10,7 @@ import tc.oc.pgm.kits.Kit; import tc.oc.pgm.kits.KitPlayerFacet; import tc.oc.pgm.match.MatchPlayer; import tc.oc.pgm.points.PointProvider; +import tc.oc.pgm.tracker.trackers.PlayerLocationTracker; @FeatureInfo(name = "spawn") public interface Spawn extends FeatureDefinition { @@ -53,6 +55,12 @@ class SpawnImpl extends FeatureDefinition.Impl implements Spawn { @Override public Location getSpawn(MatchPlayer player) { Location location = this.pointProvider.getPoint(player.getMatch(), player.getBukkit()); + if (attributes.useLastParticipatingLocation) { + Location lastParticipatingLocation = PlayerLocationTracker.getLastParticipatingLocation(player); + if (lastParticipatingLocation != null) { + location = lastParticipatingLocation; + } + } if(location == null) { player.getMatch().needMatchModule(SpawnMatchModule.class).reportFailedSpawn(this, player); } diff --git a/PGM/src/main/java/tc/oc/pgm/spawns/SpawnAttributes.java b/PGM/src/main/java/tc/oc/pgm/spawns/SpawnAttributes.java index 082ed4a..4c92374 100644 --- a/PGM/src/main/java/tc/oc/pgm/spawns/SpawnAttributes.java +++ b/PGM/src/main/java/tc/oc/pgm/spawns/SpawnAttributes.java @@ -23,8 +23,9 @@ public class SpawnAttributes extends Inspectable.Impl { public final @Inspect boolean spread; public final @Inspect boolean exclusive; public final @Inspect boolean persistent; + public final @Inspect boolean useLastParticipatingLocation; - public SpawnAttributes(PointProviderAttributes providerAttributes, Filter filter, Optional kit, boolean sequential, boolean spread, boolean exclusive, boolean persistent) { + public SpawnAttributes(PointProviderAttributes providerAttributes, Filter filter, Optional kit, boolean sequential, boolean spread, boolean exclusive, boolean persistent, boolean useLastParticipatingLocation) { this.filter = checkNotNull(filter); this.providerAttributes = checkNotNull(providerAttributes); this.kit = checkNotNull(kit); @@ -32,10 +33,11 @@ public class SpawnAttributes extends Inspectable.Impl { this.spread = spread; this.exclusive = exclusive; this.persistent = persistent; + this.useLastParticipatingLocation = useLastParticipatingLocation; } public SpawnAttributes() { - this(new PointProviderAttributes(), StaticFilter.ABSTAIN, Optional.empty(), false, false, false, false); + this(new PointProviderAttributes(), StaticFilter.ABSTAIN, Optional.empty(), false, false, false, false, false); } @Override @@ -52,7 +54,8 @@ public class SpawnAttributes extends Inspectable.Impl { this.sequential == that.sequential && this.spread == that.spread && this.exclusive == that.exclusive && - this.persistent == that.persistent + this.persistent == that.persistent && + this.useLastParticipatingLocation == that.useLastParticipatingLocation ); } @@ -62,7 +65,8 @@ public class SpawnAttributes extends Inspectable.Impl { Optional sequential, Optional spread, Optional exclusive, - Optional persistent) { + Optional persistent, + Optional useLastParticipatingLocation) { return new SpawnAttributes(providerAttributes, AllFilter.of(filter, this.filter), @@ -70,6 +74,7 @@ public class SpawnAttributes extends Inspectable.Impl { sequential.orElse(this.sequential), spread.orElse(this.spread), exclusive.orElse(this.exclusive), - persistent.orElse(this.persistent)); + persistent.orElse(this.persistent), + useLastParticipatingLocation.orElse(this.useLastParticipatingLocation)); } } diff --git a/PGM/src/main/java/tc/oc/pgm/spawns/SpawnParser.java b/PGM/src/main/java/tc/oc/pgm/spawns/SpawnParser.java index eebf1f6..baf4a22 100644 --- a/PGM/src/main/java/tc/oc/pgm/spawns/SpawnParser.java +++ b/PGM/src/main/java/tc/oc/pgm/spawns/SpawnParser.java @@ -109,7 +109,8 @@ public class SpawnParser { booleans.property(el, "sequential").optional(), booleans.property(el, "spread").optional(), booleans.property(el, "exclusive").optional(), - booleans.property(el, "persistent").optional() + booleans.property(el, "persistent").optional(), + booleans.property(el, "use-last-location").optional() ); } diff --git a/PGM/src/main/java/tc/oc/pgm/teams/TeamCommands.java b/PGM/src/main/java/tc/oc/pgm/teams/TeamCommands.java index 49483b7..b0bc5d9 100644 --- a/PGM/src/main/java/tc/oc/pgm/teams/TeamCommands.java +++ b/PGM/src/main/java/tc/oc/pgm/teams/TeamCommands.java @@ -79,7 +79,7 @@ public class TeamCommands implements NestedCommands { if(args.argsLength() >= 2) { String name = args.getString(1); if(name.trim().toLowerCase().startsWith("obs")) { - player.getMatch().setPlayerParty(player, player.getMatch().getDefaultParty()); + player.getMatch().setPlayerParty(player, player.getMatch().getDefaultParty(), false); } else { Team team = utils.teamArgument(args, 1); utils.module().forceJoin(player, team); diff --git a/PGM/src/main/java/tc/oc/pgm/teams/TeamMatchModule.java b/PGM/src/main/java/tc/oc/pgm/teams/TeamMatchModule.java index dda1cbe..51b13e9 100644 --- a/PGM/src/main/java/tc/oc/pgm/teams/TeamMatchModule.java +++ b/PGM/src/main/java/tc/oc/pgm/teams/TeamMatchModule.java @@ -260,7 +260,7 @@ public class TeamMatchModule extends MatchModule implements Listener, JoinHandle if(Optionals.equals(newTeam, player.partyMaybe())) return true; - if(getMatch().setPlayerParty(player, newTeam)) { + if(getMatch().setPlayerParty(player, newTeam, true)) { setAutoJoin(player, autoJoin); return true; } else { @@ -546,7 +546,7 @@ public class TeamMatchModule extends MatchModule implements Listener, JoinHandle if(kickTo instanceof Team) { return forceJoin(kickMe, (Team) kickTo); } else { - return getMatch().setPlayerParty(kickMe, kickTo); + return getMatch().setPlayerParty(kickMe, kickTo, false); } } diff --git a/PGM/src/main/java/tc/oc/pgm/tracker/TrackerManifest.java b/PGM/src/main/java/tc/oc/pgm/tracker/TrackerManifest.java index 6414351..93e10cd 100644 --- a/PGM/src/main/java/tc/oc/pgm/tracker/TrackerManifest.java +++ b/PGM/src/main/java/tc/oc/pgm/tracker/TrackerManifest.java @@ -1,6 +1,7 @@ package tc.oc.pgm.tracker; import com.google.inject.multibindings.Multibinder; +import tc.oc.commons.bukkit.inventory.Slot; import tc.oc.commons.core.inject.HybridManifest; import tc.oc.commons.core.inject.Manifest; import tc.oc.pgm.match.MatchScope; @@ -20,6 +21,7 @@ import tc.oc.pgm.tracker.trackers.EntityTracker; import tc.oc.pgm.tracker.trackers.FallTracker; import tc.oc.pgm.tracker.trackers.FireTracker; import tc.oc.pgm.tracker.trackers.OwnedMobTracker; +import tc.oc.pgm.tracker.trackers.PlayerLocationTracker; import tc.oc.pgm.tracker.trackers.ProjectileTracker; import tc.oc.pgm.tracker.trackers.SpleefTracker; import tc.oc.pgm.tracker.trackers.TNTTracker; @@ -43,6 +45,7 @@ public class TrackerManifest extends HybridManifest implements MatchBinders { bind(AnvilTracker.class); bind(CombatLogTracker.class); bind(DeathTracker.class); + bind(PlayerLocationTracker.class); bind(PotionDamageResolver.class); bind(ExplosionDamageResolver.class); @@ -73,6 +76,7 @@ public class TrackerManifest extends HybridManifest implements MatchBinders { matchListener(AnvilTracker.class, MatchScope.RUNNING); matchListener(CombatLogTracker.class, MatchScope.RUNNING); matchListener(DeathTracker.class, MatchScope.RUNNING); + matchListener(PlayerLocationTracker.class, MatchScope.RUNNING); // Damage resolvers - order is important! final Multibinder damageResolvers = inSet(DamageResolver.class); diff --git a/PGM/src/main/java/tc/oc/pgm/tracker/trackers/PlayerLocationTracker.java b/PGM/src/main/java/tc/oc/pgm/tracker/trackers/PlayerLocationTracker.java new file mode 100644 index 0000000..da7ac07 --- /dev/null +++ b/PGM/src/main/java/tc/oc/pgm/tracker/trackers/PlayerLocationTracker.java @@ -0,0 +1,43 @@ +package tc.oc.pgm.tracker.trackers; + +import edu.umd.cs.findbugs.detect.BadUseOfReturnValue; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import tc.oc.pgm.events.ListenerScope; +import tc.oc.pgm.events.MatchPlayerDeathEvent; +import tc.oc.pgm.events.PlayerChangePartyEvent; +import tc.oc.pgm.match.MatchPlayer; +import tc.oc.pgm.match.MatchScope; +import javax.annotation.Nullable; +import java.util.Map; +import java.util.WeakHashMap; + +@ListenerScope(MatchScope.RUNNING) +public class PlayerLocationTracker implements Listener { + + private static final Map lastParticipatingLocation = new WeakHashMap<>(); + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerChangeParty(PlayerChangePartyEvent event) { + lastParticipatingLocation.put(event.getPlayer(), event.getPlayer().getLocation()); + } + + @EventHandler + public void onPlayerDeath(MatchPlayerDeathEvent event) { + lastParticipatingLocation.put(event.getVictim(), event.getVictim().getLocation()); + } + + /* + public static void setLocation(MatchPlayer player, Location location) { + lastParticipatingLocation.put(player, location); + } + */ + + public static @Nullable Location getLastParticipatingLocation(MatchPlayer player) { + Location location = lastParticipatingLocation.get(player); + return (location != null && location.getWorldId().equals(player.getWorldId())) ? location : null; + } +} diff --git a/Tourney/src/net/anxuiz/tourney/listener/TeamListener.java b/Tourney/src/net/anxuiz/tourney/listener/TeamListener.java index b0c8b21..2ee9030 100644 --- a/Tourney/src/net/anxuiz/tourney/listener/TeamListener.java +++ b/Tourney/src/net/anxuiz/tourney/listener/TeamListener.java @@ -136,7 +136,7 @@ public class TeamListener implements Listener { final Player player = event.getPlayer().getBukkit(); Team team = teamManagerProvider.get().getTeam(player); if(team != null) { - event.getMatch().setPlayerParty(event.getPlayer(), team); + event.getMatch().setPlayerParty(event.getPlayer(), team, false); ChannelsPlugin.get().getPlayerManager().setMembershipChannel( player, event.getMatch().needMatchModule(ChannelMatchModule.class).getChannel(team)