Clean up the denyCommands logic.

This commit is contained in:
Olof Larsson 2013-04-18 16:35:45 +02:00
parent 3ac432ac3d
commit 5f18ef6d0c
3 changed files with 75 additions and 63 deletions

View File

@ -1,5 +1,6 @@
package com.massivecraft.factions.listeners;
import java.util.Collection;
import java.util.Iterator;
import org.bukkit.Bukkit;
@ -33,6 +34,7 @@ import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
@ -45,10 +47,13 @@ import com.massivecraft.factions.Const;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayerColl;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.util.Txt;
public class FactionsListenerMain implements Listener
{
@ -69,6 +74,69 @@ public class FactionsListenerMain implements Listener
Bukkit.getPluginManager().registerEvents(this, Factions.get());
}
// -------------------------------------------- //
// DENY COMMANDS
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void denyCommands(PlayerCommandPreprocessEvent event)
{
// If a player is trying to run a command ...
Player player = event.getPlayer();
FPlayer fplayer = FPlayerColl.get().get(player);
// ... and the player does not have adminmode ...
if (fplayer.isUsingAdminMode()) return;
// ... clean up the command ...
String command = event.getMessage();
command = Txt.removeLeadingCommandDust(command);
command = command.toLowerCase();
command = command.trim();
if (fplayer.hasFaction() && fplayer.getFaction().getFlag(FFlag.PERMANENT) && containsCommand(command, ConfServer.permanentFactionMemberDenyCommands))
{
fplayer.msg("<b>You can't use \"<h>%s<b>\" as member of a permanent faction.", command);
event.setCancelled(true);
return;
}
Rel rel = fplayer.getRelationToLocation();
if (BoardColl.get().getFactionAt(fplayer.getCurrentChunk()).isNone()) return;
if (rel == Rel.NEUTRAL && containsCommand(command, ConfServer.territoryNeutralDenyCommands))
{
fplayer.msg("<b>You can't use \"<h>%s<b>\" in neutral territory.", command);
event.setCancelled(true);
return;
}
if (rel == Rel.ENEMY && containsCommand(command, ConfServer.territoryEnemyDenyCommands))
{
fplayer.msg("<b>You can't use \"<h>%s<b>\" in enemy territory.", command);
event.setCancelled(true);
return;
}
}
private static boolean containsCommand(String needle, Collection<String> haystack)
{
if (needle == null) return false;
needle = Txt.removeLeadingCommandDust(needle);
needle = needle.toLowerCase();
for (String string : haystack)
{
if (string == null) continue;
string = Txt.removeLeadingCommandDust(string);
string = string.toLowerCase();
if (needle.startsWith(string)) return true;
}
return false;
}
// -------------------------------------------- //
// FLAG: MONSTERS
// -------------------------------------------- //

View File

@ -87,6 +87,12 @@ public class TodoFactionsEntityListener implements Listener
fplayer.msg(msg, fplayer.getPowerRounded(), fplayer.getPowerMaxRounded());
}
}
// -------------------------------------------- //
// PVP STUFF??
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onEntityDamage(EntityDamageEvent event)

View File

@ -1,8 +1,5 @@
package com.massivecraft.factions.listeners;
import java.util.Collection;
import java.util.Iterator;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -13,7 +10,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
@ -22,7 +18,6 @@ import org.bukkit.event.player.PlayerMoveEvent;
import com.massivecraft.factions.BoardColl;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayerColl;
@ -204,64 +199,7 @@ public class TodoFactionsPlayerListener implements Listener
}
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
// Get the player
Player player = event.getPlayer();
FPlayer me = FPlayerColl.get().get(player);
// With adminmode no commands are denied.
if (me.isUsingAdminMode()) return;
// The full command is converted to lowercase and does include the slash in the front
String fullCmd = event.getMessage().toLowerCase();
if (me.hasFaction() && me.getFaction().getFlag(FFlag.PERMANENT) && isCommandInList(fullCmd, ConfServer.permanentFactionMemberDenyCommands))
{
me.msg("<b>You can't use the command \""+fullCmd+"\" because you are in a permanent faction.");
event.setCancelled(true);
return;
}
Rel rel = me.getRelationToLocation();
if (BoardColl.get().getFactionAt(me.getCurrentChunk()).isNone()) return;
if (rel == Rel.NEUTRAL && isCommandInList(fullCmd, ConfServer.territoryNeutralDenyCommands))
{
me.msg("<b>You can't use the command \""+fullCmd+"\" in neutral territory.");
event.setCancelled(true);
return;
}
if (rel == Rel.ENEMY && isCommandInList(fullCmd, ConfServer.territoryEnemyDenyCommands))
{
me.msg("<b>You can't use the command \""+fullCmd+"\" in enemy territory.");
event.setCancelled(true);
return;
}
return;
}
private static boolean isCommandInList(String fullCmd, Collection<String> strings)
{
String shortCmd = fullCmd.substring(1);
Iterator<String> iter = strings.iterator();
while (iter.hasNext())
{
String cmdCheck = iter.next();
if (cmdCheck == null)
{
iter.remove();
continue;
}
cmdCheck = cmdCheck.toLowerCase();
if (fullCmd.startsWith(cmdCheck)) return true;
if (shortCmd.startsWith(cmdCheck)) return true;
}
return false;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerKick(PlayerKickEvent event)