Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created May 8, 2012 18:34
Show Gist options
  • Save NeatMonster/2638309 to your computer and use it in GitHub Desktop.
Save NeatMonster/2638309 to your computer and use it in GitHub Desktop.
package fr.neatmonster.nocheatplushook;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;
import fr.neatmonster.nocheatplus.actions.types.ActionList;
import fr.neatmonster.nocheatplus.checks.moving.MovingCheck;
import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
import fr.neatmonster.nocheatplus.checks.moving.RunningCheck.RunningCheckEvent;
import fr.neatmonster.nocheatplus.players.NCPPlayer;
/**
* Example usage of NoCheatPlus' API.
*/
public class NoCheatPlusHook extends JavaPlugin implements Listener {
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
}
// A list storing the names of the players allowed to fly.
public List<String> exempted = new ArrayList<String>();
// I was too lazy to register a command in the plugin.yml so I used the PlayerCommandPreprocessEvent. Do not use it
// for your commands. :D
@EventHandler(
ignoreCancelled = true)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
// If the command is /fly + something else optional.
if (event.getMessage().split(" ")[0].equalsIgnoreCase("/fly")) {
// If the name of the player isn't contained in the list...
if (!exempted.contains(event.getPlayer().getName())) {
// ...add it to allow him to fly.
exempted.add(event.getPlayer().getName());
// And display a nice green message.
event.getPlayer().sendMessage(ChatColor.GREEN + "Fly enabled!");
}
// If the name of the player is contained in the list...
else {
// ...remove it to disallow him to fly.
exempted.remove(event.getPlayer().getName());
// And display a red ugly message.
event.getPlayer().sendMessage(ChatColor.RED + "Fly disabled!");
}
// Cancel the event to do not send it the other plugins.
event.setCancelled(true);
}
}
// Here is the important part, the EventHandler handling the RunningCheckEvent triggered by the player if he fails
// the check.
@EventHandler(
ignoreCancelled = true)
public void onRunFlyCheck(final RunningCheckEvent event) {
// If the list contains his name...
if (exempted.contains(event.getPlayer().getName()))
// Cancel the event, so the actions won't be executed.
event.setCancelled(true);
/**
* All the stuff we can get using a NoCheatPlus' event.
*/
// The player who has triggered the event.
final NCPPlayer player = event.getPlayer();
// How to get a Player instead of a NCPPlayer?
final Player bukkitPlayer = player.getBukkitPlayer();
// The check which has triggered this event.
final MovingCheck check = event.getCheck();
// It can be used to get the player's configuration,
final MovingConfig config = check.getConfig(player);
// or his data.
final MovingData data = check.getData(player);
// The actions which are going to be executed if the event isn't cancelled.
final ActionList actionsList = event.getActions();
// The current violation level of the player for this check.
final double vL = event.getVL();
// And of course this event implements cancellable so we can get his status
final boolean cancel = event.isCancelled();
// or decide to cancel it or not.
event.setCancelled(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment