Skip to content

Commit

Permalink
Merge pull request #1385 from Dans-Plugins/fixing-damage-handler
Browse files Browse the repository at this point in the history
Fixing factionless players being unable to damage entities
  • Loading branch information
dmccoystephenson authored Mar 27, 2022
2 parents d4e4e75 + 181a2f5 commit 3cc2e96
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void removeFaction(int i, OfflinePlayer disbandingPlayer) {
);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
Logger.getInstance().log("Disband event was cancelled.");
Logger.getInstance().debug("Disband event was cancelled.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private void forceJoin(CommandSender sender, String[] args) {
FactionJoinEvent joinEvent = new FactionJoinEvent(faction, player);
Bukkit.getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) {
Logger.getInstance().log("Join event was cancelled.");
Logger.getInstance().debug("Join event was cancelled.");
return;
}
messageFaction(faction, translate("&a" + getText("HasJoined", player.getName(), faction.getName())));
Expand Down Expand Up @@ -262,7 +262,7 @@ private void forceKick(CommandSender sender, String[] args) {
FactionKickEvent kickEvent = new FactionKickEvent(faction, target, null); // no kicker so null is used
Bukkit.getPluginManager().callEvent(kickEvent);
if (kickEvent.isCancelled()) {
Logger.getInstance().log("Kick event was cancelled.");
Logger.getInstance().debug("Kick event was cancelled.");
return;
}
if (faction.isOfficer(targetUUID)) {
Expand Down Expand Up @@ -434,7 +434,7 @@ private void forceRename(CommandSender sender, String[] args) {
final FactionRenameEvent renameEvent = new FactionRenameEvent(faction, oldName, newName);
Bukkit.getPluginManager().callEvent(renameEvent);
if (renameEvent.isCancelled()) {
Logger.getInstance().log("Rename event was cancelled.");
Logger.getInstance().debug("Rename event was cancelled.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void execute(Player player, String[] args, String key) {
FactionJoinEvent joinEvent = new FactionJoinEvent(faction, player);
Bukkit.getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) {
Logger.getInstance().log("Join event was cancelled.");
Logger.getInstance().debug("Join event was cancelled.");
return;
}
messageFaction(target, translate("&a" + getText("HasJoined", player.getName(), target.getName())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void execute(Player player, String[] args, String key) {
FactionKickEvent kickEvent = new FactionKickEvent(faction, target, player);
Bukkit.getPluginManager().callEvent(kickEvent);
if (kickEvent.isCancelled()) {
Logger.getInstance().log("Kick event was cancelled.");
Logger.getInstance().debug("Kick event was cancelled.");
return;
}
if (faction.isOfficer(targetUUID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void execute(Player player, String[] args, String key) {
FactionLeaveEvent leaveEvent = new FactionLeaveEvent(faction, player);
Bukkit.getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) {
Logger.getInstance().log("Leave event was cancelled.");
Logger.getInstance().debug("Leave event was cancelled.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void execute(Player player, String[] args, String key) {
final FactionRenameEvent renameEvent = new FactionRenameEvent(faction, oldName, newName);
Bukkit.getPluginManager().callEvent(renameEvent);
if (renameEvent.isCancelled()) {
Logger.getInstance().log("Rename event was cancelled.");
Logger.getInstance().debug("Rename event was cancelled.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void execute(Player player, String[] args, String key) {
// make sure this vassalization won't result in a vassalization loop
final int loopCheck = willVassalizationResultInLoop(faction, target);
if (loopCheck == 1 || loopCheck == 2) {
Logger.getInstance().log("Vassalization was cancelled due to potential loop");
Logger.getInstance().debug("Vassalization was cancelled due to potential loop");
return;
}
// add faction to attemptedVassalizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ private void removeChunk(ClaimedChunk chunkToRemove, Player unclaimingPlayer, Fa
FactionUnclaimEvent unclaimEvent = new FactionUnclaimEvent(holdingFaction, unclaimingPlayer, chunkToRemove.getChunk());
Bukkit.getPluginManager().callEvent(unclaimEvent);
if (unclaimEvent.isCancelled()) {
Logger.getInstance().log("Unclaim event was cancelled.");
Logger.getInstance().debug("Unclaim event was cancelled.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void handle(EntityDamageByEntityEvent event) {
Player victim = getVictim(event);

if (attacker == null || victim == null) {
Logger.getInstance().log("Attacker and/or victim was null in the DamageHandler class.");
Logger.getInstance().debug("Attacker and/or victim was null in the DamageHandler class.");
return;
}

Expand All @@ -60,32 +60,42 @@ public void handle(EntityDamageByEntityEvent event) {
* 4) Players are not in the same faction but are not enemies.
*/
private void handlePlayerVersusPlayer(Player attacker, Player victim, EntityDamageByEntityEvent event) {
Logger.getInstance().debug("Handling damage between players.");

// case 1
if (arePlayersDueling(attacker, victim)) {
Logger.getInstance().debug("Players are dueling. Ending if necessary.");
endDuelIfNecessary(attacker, victim, event);
return;
}

// case 2
if (RelationChecker.getInstance().playerNotInFaction(attacker) || RelationChecker.getInstance().playerNotInFaction(victim)) {
Logger.getInstance().debug("Attacker or victim is not in a faction. Returning.");
// allow since factionless don't have PVP restrictions
return;
}

// case 3
if (RelationChecker.getInstance().arePlayersInSameFaction(attacker, victim)){
Logger.getInstance().debug("Players are in the same faction. Handling friendly fire.");
handleFriendlyFire(event, attacker, victim);
return;
}

// case 4
if (RelationChecker.getInstance().arePlayersFactionsNotEnemies(attacker, victim)) {
Logger.getInstance().debug("Players factions are not enemies. Handling non-enemy fire.");
handleNonEnemyFire(event, attacker, victim);
}
}

private void handleEntityDamage(Player attacker, EntityDamageByEntityEvent event) {
Logger.getInstance().debug("Handling entity damage.");
if (event.getEntity() instanceof Player) {
Logger.getInstance().debug("Entity is an instance of a player. Returning.");
return;
}
Faction playersFaction = PersistentData.getInstance().getPlayersFaction(attacker.getUniqueId());
if (playersFaction == null) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ private void assignPlayerToRandomFaction(Player player) {
FactionJoinEvent joinEvent = new FactionJoinEvent(faction, player);
Bukkit.getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) {
Logger.getInstance().log("Join event was cancelled.");
Logger.getInstance().debug("Join event was cancelled.");
return;
}
Messenger.getInstance().sendAllPlayersInFactionMessage(faction, String.format(ChatColor.GREEN + "" + Locale.get("HasJoined"), player.getName(), faction.getName()));
faction.addMember(player.getUniqueId());
player.sendMessage(ChatColor.GREEN + "" + Locale.get("AssignedToRandomFaction"));

Logger.getInstance().log(player.getName() + " has been randomly assigned to " + faction.getName() + "!");
Logger.getInstance().debug(player.getName() + " has been randomly assigned to " + faction.getName() + "!");
} else {
Logger.getInstance().log("Attempted to assign " + player.getName() + " to a random faction, but no factions are existent.");
Logger.getInstance().debug("Attempted to assign " + player.getName() + " to a random faction, but no factions are existent.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public DynmapIntegrator() {
dynmap = pm.getPlugin("dynmap");

if (!isDynmapPresent()) {
Logger.getInstance().log(Locale.get("CannotFindDynmap"));
Logger.getInstance().debug(Locale.get("CannotFindDynmap"));
} else {
try {
dynmapAPI = (DynmapCommonAPI) dynmap; /* Get API */
markerAPI = dynmapAPI.getMarkerAPI();
initializeMarkerSets();
Logger.getInstance().log(Locale.get("DynmapIntegrationSuccessful"));
Logger.getInstance().debug(Locale.get("DynmapIntegrationSuccessful"));
} catch (Exception e) {
Logger.getInstance().log(Locale.get("ErrorIntegratingWithDynmap") + e.getMessage());
Logger.getInstance().debug(Locale.get("ErrorIntegratingWithDynmap") + e.getMessage());
}
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ private MarkerSet initializeMarkerSet(MarkerSet set, String markerLabel) {
if (set == null) {
set = markerAPI.createMarkerSet(getDynmapPluginSetId(markerLabel), getDynmapPluginLayer(), null, false);
if (set == null) {
Logger.getInstance().log(Locale.get("ErrorCreatingMarkerSet") + ": markerLabel = " + markerLabel);
Logger.getInstance().debug(Locale.get("ErrorCreatingMarkerSet") + ": markerLabel = " + markerLabel);
return set;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,21 @@ public void setFlag(String flag, String value, Player player) {

public Object getFlag(String flag) {
if (!isFlag(flag)) {
Logger.getInstance().log(String.format("[DEBUG] Flag '%s' was not found!", flag));
Logger.getInstance().debug(String.format("[DEBUG] Flag '%s' was not found!", flag));
return false;
}

if (integerValues.containsKey(flag)) {
Logger.getInstance().log(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, integerValues.get(flag)));
Logger.getInstance().debug(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, integerValues.get(flag)));
return integerValues.get(flag);
} else if (booleanValues.containsKey(flag)) {
Logger.getInstance().log(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, booleanValues.get(flag)));
Logger.getInstance().debug(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, booleanValues.get(flag)));
return booleanValues.get(flag);
} else if (doubleValues.containsKey(flag)) {
Logger.getInstance().log(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, doubleValues.get(flag)));
Logger.getInstance().debug(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, doubleValues.get(flag)));
return doubleValues.get(flag);
} else if (stringValues.containsKey(flag)) {
Logger.getInstance().log(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, stringValues.get(flag)));
Logger.getInstance().debug(String.format("[DEBUG] Flag '%s' was found! Value: '%s'", flag, stringValues.get(flag)));
return stringValues.get(flag);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public boolean isOutsiderInteractionAllowed(Player player, ClaimedChunk chunk, F
boolean allyInteractionAllowed = (boolean) chunkHolder.getFlags().getFlag("alliesCanInteractWithLand");
boolean vassalageTreeInteractionAllowed = (boolean) chunkHolder.getFlags().getFlag("vassalageTreeCanInteractWithLand");

Logger.getInstance().log("allyInteractionAllowed: " + allyInteractionAllowed);
Logger.getInstance().log("vassalageTreeInteractionAllowed: " + vassalageTreeInteractionAllowed);
Logger.getInstance().debug("allyInteractionAllowed: " + allyInteractionAllowed);
Logger.getInstance().debug("vassalageTreeInteractionAllowed: " + vassalageTreeInteractionAllowed);

boolean allowed = allyInteractionAllowed && isAlly;

Expand Down
24 changes: 22 additions & 2 deletions src/main/java/dansplugins/factionsystem/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ public static Logger getInstance() {
return instance;
}

public void log(String message) {
/**
* Log a debug message if the debug flag is enabled.
* @param message The message to log.
*/
public void debug(String message) {
if (MedievalFactions.getInstance().isDebugEnabled()) {
MedievalFactions.getInstance().getLogger().log(Level.INFO, "[Medieval Factions] " + message);
MedievalFactions.getInstance().getLogger().log(Level.INFO, "[Medieval Factions DEBUG] " + message);
}
}

/**
* Log a message to the console.
* @param message The message to log.
*/
public void print(String message) {
MedievalFactions.getInstance().getLogger().log(Level.INFO, "[Medieval Factions] " + message);
}

/**
* Log an error to the console.
* @param message The message to log.
*/
public void error(String message) {
MedievalFactions.getInstance().getLogger().log(Level.SEVERE, "[Medieval Factions ERROR] " + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static PlayerTeleporter getInstance() {
}

public boolean teleportPlayer(Player player, Location location) {
Logger.getInstance().log("Attempting to teleport " + player.getName() + " to " + location.toString());
Logger.getInstance().debug("Attempting to teleport " + player.getName() + " to " + location.toString());
boolean success = player.teleport(location);
if (success) {
Logger.getInstance().log("Successfully teleported " + player.getName());
Logger.getInstance().debug("Successfully teleported " + player.getName());
}
else {
Logger.getInstance().log("Failed to teleport " + player.getName());
Logger.getInstance().debug("Failed to teleport " + player.getName());
}
return success;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,39 @@ public static Scheduler getInstance() {
}

public void scheduleAutosave() {
Logger.getInstance().log(Locale.get("SchedulingHourlyAutoSave"));
Logger.getInstance().debug(Locale.get("SchedulingHourlyAutoSave"));
int delay = 60 * 60; // 1 hour
int secondsUntilRepeat = 60 * 60; // 1 hour
Bukkit.getScheduler().scheduleSyncRepeatingTask(MedievalFactions.getInstance(), new Runnable() {
@Override
public void run() {
Logger.getInstance().log(Locale.get("HourlySaveAlert"));
Logger.getInstance().debug(Locale.get("HourlySaveAlert"));
PersistentData.getInstance().getLocalStorageService().save();
}
}, delay * 20, secondsUntilRepeat * 20);
}

public void schedulePowerIncrease() {
Logger.getInstance().log(Locale.get("SchedulingPowerIncrease"));
Logger.getInstance().debug(Locale.get("SchedulingPowerIncrease"));
int delay = MedievalFactions.getInstance().getConfig().getInt("minutesBeforeInitialPowerIncrease") * 60; // 30 minutes
int secondsUntilRepeat = MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerIncreases") * 60; // 1 hour
Bukkit.getScheduler().scheduleSyncRepeatingTask(MedievalFactions.getInstance(), new Runnable() {
@Override
public void run() {
Logger.getInstance().log(String.format((Locale.get("AlertIncreasingThePowerOfEveryPlayer")) + "%n", MedievalFactions.getInstance().getConfig().getInt("powerIncreaseAmount"), MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerIncreases")));
Logger.getInstance().debug(String.format((Locale.get("AlertIncreasingThePowerOfEveryPlayer")) + "%n", MedievalFactions.getInstance().getConfig().getInt("powerIncreaseAmount"), MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerIncreases")));
PersistentData.getInstance().initiatePowerIncreaseForAllPlayers();
}
}, delay * 20L, secondsUntilRepeat * 20L);
}

public void schedulePowerDecrease() {
Logger.getInstance().log(Locale.get("SchedulingPowerDecrease"));
Logger.getInstance().debug(Locale.get("SchedulingPowerDecrease"));
int delay = MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerDecreases") * 60;
int secondsUntilRepeat = MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerDecreases") * 60;
Bukkit.getScheduler().scheduleSyncRepeatingTask(MedievalFactions.getInstance(), new Runnable() {
@Override
public void run() {
Logger.getInstance().log(String.format((Locale.get("AlertDecreasingThePowerOfInactivePlayers")) + "%n", MedievalFactions.getInstance().getConfig().getInt("powerDecreaseAmount"), MedievalFactions.getInstance().getConfig().getInt("minutesBeforePowerDecrease"), MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerDecreases")));
Logger.getInstance().debug(String.format((Locale.get("AlertDecreasingThePowerOfInactivePlayers")) + "%n", MedievalFactions.getInstance().getConfig().getInt("powerDecreaseAmount"), MedievalFactions.getInstance().getConfig().getInt("minutesBeforePowerDecrease"), MedievalFactions.getInstance().getConfig().getInt("minutesBetweenPowerDecreases")));

PersistentData.getInstance().decreasePowerForInactivePlayers();

Expand Down Expand Up @@ -131,7 +131,7 @@ public void run() {
delay();
} catch(Exception e) {
player.sendMessage(ChatColor.RED + "Something went wrong.");
Logger.getInstance().log("Something went wrong running a delayed teleport task.");
Logger.getInstance().debug("Something went wrong running a delayed teleport task.");
return;
}

Expand Down

0 comments on commit 3cc2e96

Please sign in to comment.