From 634d8751a57cd0e445187fd860ecfa07d22fc15c Mon Sep 17 00:00:00 2001 From: Mads Boddum Date: Mon, 17 Oct 2016 19:36:20 +0200 Subject: [PATCH] Renamed variables across methods in FactionService --- src/services/faction/FactionService.java | 117 +++++++++++------------ 1 file changed, 56 insertions(+), 61 deletions(-) diff --git a/src/services/faction/FactionService.java b/src/services/faction/FactionService.java index a249c7a6..fc4c3de4 100644 --- a/src/services/faction/FactionService.java +++ b/src/services/faction/FactionService.java @@ -99,40 +99,40 @@ public final class FactionService extends Service { target.getOwner().sendPacket(new ChatSystemMessage(SystemChatType.SCREEN_AND_CHAT, message)); } - private String getBeginMessage(PvpStatus currentStatus, PvpStatus targetStatus) { + private String getBeginMessage(PvpStatus oldStatus, PvpStatus newStatus) { String message = "@faction_recruiter:"; - if(currentStatus == PvpStatus.ONLEAVE && targetStatus == PvpStatus.COMBATANT) + if(oldStatus == PvpStatus.ONLEAVE && newStatus == PvpStatus.COMBATANT) message += "on_leave_to_covert"; - else if(currentStatus == PvpStatus.COMBATANT && targetStatus == PvpStatus.SPECIALFORCES) + else if(oldStatus == PvpStatus.COMBATANT && newStatus == PvpStatus.SPECIALFORCES) message += "covert_to_overt"; - else if(currentStatus == PvpStatus.SPECIALFORCES && targetStatus == PvpStatus.COMBATANT) + else if(oldStatus == PvpStatus.SPECIALFORCES && newStatus == PvpStatus.COMBATANT) message += "overt_to_covert"; return message; } - private String getCompletionMessage(PvpStatus currentStatus, PvpStatus targetStatus) { + private String getCompletionMessage(PvpStatus oldStatus, PvpStatus newStatus) { String message = "@faction_recruiter:"; - if((currentStatus == PvpStatus.ONLEAVE || currentStatus == PvpStatus.SPECIALFORCES) && targetStatus == PvpStatus.COMBATANT) + if((oldStatus == PvpStatus.ONLEAVE || oldStatus == PvpStatus.SPECIALFORCES) && newStatus == PvpStatus.COMBATANT) message += "covert_complete"; - else if(currentStatus == PvpStatus.COMBATANT && targetStatus == PvpStatus.SPECIALFORCES) + else if(oldStatus == PvpStatus.COMBATANT && newStatus == PvpStatus.SPECIALFORCES) message += "overt_complete"; - else if(currentStatus == PvpStatus.COMBATANT && targetStatus == PvpStatus.ONLEAVE ) + else if(oldStatus == PvpStatus.COMBATANT && newStatus == PvpStatus.ONLEAVE ) message += "on_leave_complete"; return message; } - private long getDelay(PvpStatus currentStatus, PvpStatus targetStatus) { + private long getDelay(PvpStatus oldStatus, PvpStatus newStatus) { long delay = 0; - if(currentStatus == PvpStatus.ONLEAVE && targetStatus == PvpStatus.COMBATANT) + if(oldStatus == PvpStatus.ONLEAVE && newStatus == PvpStatus.COMBATANT) delay = 1; - else if(currentStatus == PvpStatus.COMBATANT && targetStatus == PvpStatus.SPECIALFORCES) + else if(oldStatus == PvpStatus.COMBATANT && newStatus == PvpStatus.SPECIALFORCES) delay = 30; - else if(currentStatus == PvpStatus.SPECIALFORCES && targetStatus == PvpStatus.COMBATANT) + else if(oldStatus == PvpStatus.SPECIALFORCES && newStatus == PvpStatus.COMBATANT) delay = 300; return delay; @@ -143,43 +143,36 @@ public final class FactionService extends Service { PvpFaction newFaction = fi.getNewFaction(); target.setPvpFaction(newFaction); - target.setPvpStatus(PvpStatus.COMBATANT); + handleFlagChange(target); - handleFlagChange(target); // We don't want to remain attackable if we leave our current faction. - - if(target instanceof CreatureObject) { - // We're given rank 1 upon joining a PvP faction - ((CreatureObject) target).setFactionRank((byte) (newFaction != PvpFaction.NEUTRAL ? 1 : 0)); + if(target instanceof CreatureObject && ((CreatureObject) target).getPvpFaction() != PvpFaction.NEUTRAL) { + // We're given rank 1 upon joining a non-neutral faction + ((CreatureObject) target).setFactionRank(1); } } private void handleSwitchChange(FactionIntent fi) { final PvpFlag pvpFlag; final TangibleObject target = fi.getTarget(); - final PvpStatus currentStatus = target.getPvpStatus(); - final PvpStatus targetStatus; + final PvpStatus oldStatus = target.getPvpStatus(); + final PvpStatus newStatus; if(target.hasPvpFlag(PvpFlag.GOING_COVERT) || target.hasPvpFlag(PvpFlag.GOING_OVERT)) { sendSystemMessage(target, "@faction_recruiter:pvp_status_changing"); } else { - if(currentStatus == PvpStatus.COMBATANT) { + if(oldStatus == PvpStatus.COMBATANT) { pvpFlag = PvpFlag.GOING_OVERT; - targetStatus = PvpStatus.SPECIALFORCES; + newStatus = PvpStatus.SPECIALFORCES; } else { // Covers both ONLEAVE and SPECIALFORCES pvpFlag = PvpFlag.GOING_COVERT; - targetStatus = PvpStatus.COMBATANT; + newStatus = PvpStatus.COMBATANT; } target.setPvpFlags(pvpFlag); - sendSystemMessage(target, getBeginMessage(currentStatus, targetStatus)); - statusChangers.put(target, executor.schedule(new Runnable() { - @Override - public void run() { - statusChangers.remove(target); - changeStatusWithMessage(target, targetStatus, getCompletionMessage(currentStatus, targetStatus)); - target.clearPvpFlags(pvpFlag); - } - }, getDelay(currentStatus, targetStatus), TimeUnit.SECONDS)); + sendSystemMessage(target, getBeginMessage(oldStatus, newStatus)); + synchronized (statusChangers) { + statusChangers.put(target, executor.schedule(() -> completeChange(target, pvpFlag, oldStatus, newStatus), getDelay(oldStatus, newStatus), TimeUnit.SECONDS)); + } } } @@ -194,64 +187,66 @@ public final class FactionService extends Service { return; // Let's clear PvP flags in case they were in the middle of going covert/overt - Future future = statusChangers.remove(target); - - if (future != null) { - if (future.cancel(false)) { - target.clearPvpFlags(PvpFlag.GOING_COVERT, PvpFlag.GOING_OVERT); - } else if (target.getPvpStatus() != newStatus) { - // Their new status does not equal the one we want - apply the new one - target.setPvpStatus(newStatus); + synchronized (statusChangers) { + Future future = statusChangers.remove(target); + + if (future != null) { + if (future.cancel(false)) { + target.clearPvpFlags(PvpFlag.GOING_COVERT, PvpFlag.GOING_OVERT); + } else if (target.getPvpStatus() != newStatus) { + // Their new status does not equal the one we want - apply the new one + changeStatus(target, newStatus); + } + } else { + // They're not currently waiting to switch to a new status - change now + changeStatus(target, newStatus); } - } else { - // They're not currently waiting to switch to a new status - change now - target.setPvpStatus(newStatus); } } - private void handleFlagChange(TangibleObject object) { - Player objOwner = object.getOwner(); + private void handleFlagChange(TangibleObject target) { + Player objOwner = target.getOwner(); - for (SWGObject o : object.getObservers()) { + for (SWGObject o : target.getObservers()) { if (!(o instanceof TangibleObject)) continue; TangibleObject observer = (TangibleObject) o; Player obsOwner = observer.getOwner(); - int pvpBitmask = getPvpBitmask(object, observer); + int pvpBitmask = getPvpBitmask(target, observer); if (objOwner != null) // Send the PvP information about this observer to the owner objOwner.sendPacket(createPvpStatusMessage(observer, observer.getPvpFlags() | pvpBitmask)); if (obsOwner != null) // Send the pvp information about the owner to this observer - obsOwner.sendPacket(createPvpStatusMessage(object, object.getPvpFlags() | pvpBitmask)); + obsOwner.sendPacket(createPvpStatusMessage(target, target.getPvpFlags() | pvpBitmask)); } } - private void timedStatusChange() { + private void completeChange(TangibleObject target, PvpFlag pvpFlag, PvpStatus oldStatus, PvpStatus newStatus) { + synchronized (statusChangers) { + statusChangers.remove(target); + } + new ChatBroadcastIntent(target.getOwner(), getCompletionMessage(oldStatus, newStatus)).broadcast(); + target.clearPvpFlags(pvpFlag); } - private void changeStatus(TangibleObject object, PvpStatus newStatus) { - object.setPvpStatus(newStatus); - handleFlagChange(object); + private void changeStatus(TangibleObject target, PvpStatus newStatus) { + target.setPvpStatus(newStatus); + handleFlagChange(target); } - private void changeStatusWithMessage(TangibleObject object, PvpStatus newStatus, String systemMessage) { - changeStatus(object, newStatus); - new ChatBroadcastIntent(object.getOwner(), systemMessage).broadcast(); - } - - private UpdatePvpStatusMessage createPvpStatusMessage(TangibleObject object, int flags) { + private UpdatePvpStatusMessage createPvpStatusMessage(TangibleObject target, int flags) { Set flagSet = PvpFlag.getFlags(flags); - return new UpdatePvpStatusMessage(object.getPvpFaction(), object.getObjectId(), flagSet.toArray(new PvpFlag[flagSet.size()])); + return new UpdatePvpStatusMessage(target.getPvpFaction(), target.getObjectId(), flagSet.toArray(new PvpFlag[flagSet.size()])); } - private int getPvpBitmask(TangibleObject object1, TangibleObject object2) { + private int getPvpBitmask(TangibleObject target, TangibleObject observer) { int pvpBitmask = 0; - if(object1.isEnemy(object2)) { + if(target.isEnemy(observer)) { pvpBitmask |= PvpFlag.AGGRESSIVE.getBitmask() | PvpFlag.ATTACKABLE.getBitmask(); }