Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/no/runsafe/combatcooldown/CombatMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

public class CombatMonitor implements IPluginDisabled
{
public CombatMonitor(IScheduler scheduler, CombatCooldownConfig config)
public CombatMonitor(IScheduler scheduler, Config config)
{
this.scheduler = scheduler;
this.config = config;
}

public void leaveCombat(IPlayer player)
{
if (this.combatTimers.containsKey(player))
{
this.combatTimers.remove(player);
player.sendColouredMessage(config.getLeavingCombatMessage());
}
if (!this.combatTimers.containsKey(player))
return;

this.combatTimers.remove(player);
player.sendColouredMessage(config.getLeavingCombatMessage());
}

public boolean isInCombat(IPlayer player)
Expand Down Expand Up @@ -51,14 +51,23 @@ public void engageInDergonCombat(IPlayer player)

public void engageInCombat(IPlayer firstPlayer, IPlayer secondPlayer)
{
if (this.monitoringWorld(firstPlayer.getWorld()) && this.monitoringWorld(secondPlayer.getWorld()))
{
if (firstPlayer.isPvPFlagged() && secondPlayer.isPvPFlagged())
{
this.engagePlayer(firstPlayer);
this.engagePlayer(secondPlayer);
}
}
if (!this.monitoringWorld(firstPlayer.getWorld()) || !this.monitoringWorld(secondPlayer.getWorld()))
return;

Plugin.Debugger.debugFine("engageInCombat world check succeeded");

if (!firstPlayer.isPvPFlagged())
return;

Plugin.Debugger.debugFine("Player %s is pvp flagged", firstPlayer);

if (!secondPlayer.isPvPFlagged())
return;

Plugin.Debugger.debugFine("Player %s is pvp flagged", secondPlayer);

this.engagePlayer(firstPlayer);
this.engagePlayer(secondPlayer);
}

private void engagePlayer(IPlayer player)
Expand All @@ -72,13 +81,12 @@ private void engagePlayer(IPlayer player)
private void registerPlayerTimer(final IPlayer player)
{
if (this.combatTimers.containsKey(player))
{
this.scheduler.cancelTask(this.combatTimers.get(player));
}

this.combatTimers.put(player, this.scheduler.startSyncTask(() -> leaveCombat(player), config.getCombatTime()));
}

private final ConcurrentHashMap<IPlayer, Integer> combatTimers = new ConcurrentHashMap<>();
private final IScheduler scheduler;
private final CombatCooldownConfig config;
private final Config config;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.List;

public class CombatCooldownConfig implements IConfigurationChanged
public class Config implements IConfigurationChanged
{
@Override
public void OnConfigurationChanged(IConfiguration configuration)
Expand Down
38 changes: 7 additions & 31 deletions src/no/runsafe/combatcooldown/EntityListener.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package no.runsafe.combatcooldown;

import no.runsafe.framework.api.IServer;
import no.runsafe.framework.api.event.entity.IEntityDamageByEntityEvent;
import no.runsafe.framework.api.event.player.IPlayerCustomEvent;
import no.runsafe.framework.api.log.IDebug;
import no.runsafe.framework.api.player.IPlayer;
import no.runsafe.framework.minecraft.entity.ProjectileEntity;
import no.runsafe.framework.minecraft.entity.RunsafeEntity;
import no.runsafe.framework.minecraft.entity.RunsafeLivingEntity;
import no.runsafe.framework.minecraft.entity.RunsafeProjectile;
import no.runsafe.framework.minecraft.event.entity.RunsafeEntityDamageByEntityEvent;
import no.runsafe.framework.minecraft.event.player.RunsafeCustomEvent;

import java.util.List;

public class EntityListener implements IEntityDamageByEntityEvent, IPlayerCustomEvent
{
public EntityListener(CombatMonitor combatMonitor, IDebug debugger, IServer server)
public EntityListener(CombatMonitor combatMonitor)
{
this.combatMonitor = combatMonitor;
this.debugger = debugger;
this.server = server;
}

@Override
Expand All @@ -34,7 +27,7 @@ public void OnPlayerCustomEvent(RunsafeCustomEvent event)
return;

combatMonitor.engageInDergonCombat(player);
this.debugger.debugFine(String.format(
Plugin.Debugger.debugFine(String.format(
"Player %s is being picked up by a Dergon - Blocking Commands if able.",
player.getName()
));
Expand All @@ -53,7 +46,7 @@ public void OnEntityDamageByEntity(RunsafeEntityDamageByEntityEvent event)
IPlayer attackingPlayer = null;
RunsafeEntity attacker = event.getDamageActor();

this.debugger.debugFine(String.format(
Plugin.Debugger.debugFine(String.format(
"Player %s is being attacked by a %s.",
victim.getName(),
attacker.getEntityType().getName()
Expand All @@ -70,40 +63,23 @@ else if (attacker instanceof RunsafeProjectile)

if (attackingPlayer == null)
{
this.debugger.debugFine("Victim is not being attacked by a player.");
Plugin.Debugger.debugFine("Victim is not being attacked by a player.");
return;
}

if (attackingPlayer.isVanished() || attackingPlayer.shouldNotSee(victim) || isSamePlayer(victim, attackingPlayer))
if (attackingPlayer.isVanished() || attackingPlayer.shouldNotSee(victim) || victim.equals(attackingPlayer))
{
this.debugger.debugFine("Victim being attacked by exempted player.");
Plugin.Debugger.debugFine("Victim being attacked by exempted player.");
return;
}

this.combatMonitor.engageInCombat(attackingPlayer, victim);
this.debugger.debugFine(String.format(
Plugin.Debugger.debugFine(String.format(
"Player %s engaged in PvP with %s - Blocking commands",
attackingPlayer.getName(),
victim.getName()
));
}

private boolean isSamePlayer(IPlayer one, IPlayer two)
{
return one.equals(two);
}

private IPlayer findPlayer(RunsafeLivingEntity entity)
{
List<IPlayer> onlinePlayers = server.getOnlinePlayers();
for (IPlayer player : onlinePlayers)
if (entity != null && player != null && entity.getEntityId() == player.getEntityId())
return player;

return null;
}

private final CombatMonitor combatMonitor;
private final IDebug debugger;
private final IServer server;
}
35 changes: 16 additions & 19 deletions src/no/runsafe/combatcooldown/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import no.runsafe.framework.api.event.player.IPlayerCommandPreprocessEvent;
import no.runsafe.framework.api.event.player.IPlayerDeathEvent;
import no.runsafe.framework.api.event.player.IPlayerQuitEvent;
import no.runsafe.framework.api.log.IDebug;
import no.runsafe.framework.api.player.IPlayer;
import no.runsafe.framework.minecraft.Item;
import no.runsafe.framework.minecraft.WorldBlockEffect;
Expand All @@ -16,10 +15,9 @@

public class PlayerListener implements IPlayerCommandPreprocessEvent, IPlayerDeathEvent, IPlayerQuitEvent
{
public PlayerListener(CombatMonitor combatMonitor, IDebug console, CombatCooldownConfig config)
public PlayerListener(CombatMonitor combatMonitor, Config config)
{
this.combatMonitor = combatMonitor;
this.debugger = console;
this.config = config;
effect = new WorldBlockEffect(WorldBlockEffectType.BLOCK_DUST, Item.BuildingBlock.Bedrock);
}
Expand All @@ -31,26 +29,26 @@ public void OnBeforePlayerCommand(RunsafePlayerCommandPreprocessEvent event)
String playerName = player.getName();
String commandString = event.getMessage();

debugger.debugFine("Checking if %s is engaged in combat", playerName);
if (this.combatMonitor.isInCombat(player) && !canRunCommand(player, commandString))
{
debugger.debugFine("Blocking %s from running command %s during combat", playerName, commandString);
event.cancel();
player.sendColouredMessage(config.getNoCommandsInCombatMessage());
}
Plugin.Debugger.debugFine("Checking if %s is engaged in combat", playerName);
if (!this.combatMonitor.isInCombat(player) || canRunCommand(player, commandString))
return;

Plugin.Debugger.debugFine("Blocking %s from running command %s during combat", playerName, commandString);
event.cancel();
player.sendColouredMessage(config.getNoCommandsInCombatMessage());
}

@Override
public void OnPlayerQuit(RunsafePlayerQuitEvent event)
{
IPlayer player = event.getPlayer();
if (combatMonitor.isInCombat(player))
{
player.damage(500D); // This should kill them
ILocation location = player.getLocation();
if (location != null)
location.playEffect(effect, 0.3F, 100, 50);
}
if (!combatMonitor.isInCombat(player))
return;

player.setHealth(0); // This should kill them
ILocation location = player.getLocation();
if (location != null)
location.playEffect(effect, 0.3F, 100, 50);
}

@Override
Expand All @@ -66,7 +64,6 @@ private boolean canRunCommand(IPlayer player, String commandString)
}

private final CombatMonitor combatMonitor;
private final IDebug debugger;
private final CombatCooldownConfig config;
private final Config config;
private final IWorldEffect effect;
}
7 changes: 6 additions & 1 deletion src/no/runsafe/combatcooldown/Plugin.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package no.runsafe.combatcooldown;

import no.runsafe.framework.RunsafeConfigurablePlugin;
import no.runsafe.framework.api.log.IDebug;
import no.runsafe.framework.features.Events;

public class Plugin extends RunsafeConfigurablePlugin
{
public static IDebug Debugger;

@Override
protected void pluginSetup()
{
this.addComponent(CombatCooldownConfig.class);
Debugger = getComponent(IDebug.class);

this.addComponent(Config.class);
this.addComponent(Events.class);
this.addComponent(CombatMonitor.class);
this.addComponent(EntityListener.class);
Expand Down