This commit is contained in:
FancyBelt798245 2023-05-21 12:23:09 -04:00 committed by GitHub
commit 85e13cab62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
54 changed files with 5208 additions and 4527 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target/
.classpath
.DS_Store
.project
.settings/org.eclipse.core.resources.prefs
*.prefs

View File

@ -1,8 +0,0 @@
/.settings
/.classpath
/.project
/jar_desc.jardesc
/target
/bin
/.gitignore
/local-maven/

View File

@ -1,121 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CompatNoCheatPlus</groupId>
<artifactId>CompatNoCheatPlus</artifactId>
<version>6.6.6-SNAPSHOT</version>
<name>CompatNoCheatPlus</name>
<!-- Source code -->
<scm>
<developerConnection>scm:git:git@github.com:asofold/${project.name}.git</developerConnection>
<connection>scm:git:git://github.com/asofold/${project.name}.git</connection>
<url>https://github.com/asofold/${project.name}</url>
</scm>
<!-- Repositories -->
<repositories>
<repository>
<id>spigot-repo</id>
<url>http://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>md_5-snapshots</id>
<url>http://repo.md-5.net/content/repositories/snapshots/</url>
</repository>
<repository>
<id>md_5-releases</id>
<url>http://repo.md-5.net/content/repositories/releases/</url>
</repository>
<repository>
<id>cititensnpcs</id>
<url>http://repo.citizensnpcs.co/</url>
</repository>
<repository>
<id>drtshock-repo</id>
<url>http://ci.drtshock.net/plugin/repository/everything/</url>
</repository>
</repositories>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId>
<version>1.5.04-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fr.neatmonster</groupId>
<artifactId>nocheatplus</artifactId>
<version>3.16.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizensapi</artifactId>
<version>2.0.16-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Building -->
<build>
<defaultGoal>clean package</defaultGoal>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}</directory>
<includes>
<include>plugin.yml</include>
<include>LICENSE.txt</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>cncp</finalName>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<pomPropertiesFile>false</pomPropertiesFile>
<manifest>
<addDefaultSpecificationEntries>false</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>false</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<!-- Properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<BUILD_SERIES>?</BUILD_SERIES>
<BUILD_NUMBER>?</BUILD_NUMBER>
</properties>
</project>

View File

@ -1,68 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
/**
* Wrap block break events to exempt players from checks by comparison of event class names.
* @author mc_dev
*
*/
public class HookBlockBreak extends ClassExemptionHook implements Listener {
public HookBlockBreak() {
super("block-break.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MachinaCraft
"ArtificialBlockBreakEvent",
// mcMMO
"FakeBlockBreakEvent",
// MagicSpells
"MagicSpellsBlockBreakEvent"
}));
}
@Override
public String getHookName() {
return "BlockBreak(default)";
}
@Override
public String getHookVersion() {
return "1.1";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onBlockBreakLowest(final BlockBreakEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKBREAK);
}
@EventHandler(priority = EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onBlockBreakMonitor(final BlockBreakEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKBREAK);
}
}

View File

@ -1,66 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
/**
* Wrap block place events to exempt players from checks by comparison of event class names.
* @author mc_dev
*
*/
public class HookBlockPlace extends ClassExemptionHook implements Listener{
public HookBlockPlace() {
super("block-place.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MachinaCraft
"ArtificialBlockPlaceEvent",
// MagicSpells
"MagicSpellsBlockPlaceEvent"
}));
}
@Override
public String getHookName() {
return "BlockPlace(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onBlockPlaceLowest(final BlockPlaceEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKPLACE);
}
@EventHandler(priority = EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onBlockPlaceMonitor(final BlockPlaceEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKPLACE);
}
}

View File

@ -1,70 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
public class HookEntityDamageByEntity extends ClassExemptionHook implements
Listener {
public HookEntityDamageByEntity() {
super("entity-damage-by-entity.");
defaultClasses.addAll(Arrays.asList(new String[] {
// CrackShot
"WeaponDamageEntityEvent",
// MagicSpells
"MagicSpellsEntityDamageByEntityEvent" }));
}
@Override
public String getHookName() {
return "EntityDamageByEntity(default)";
}
@Override
public String getHookVersion() {
return "0.0";
}
@Override
public Listener[] getListeners() {
return new Listener[] { this };
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty())
enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onDamageLowest(final EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
if (damager instanceof Player) {
checkExempt((Player) damager, event.getClass(), CheckType.FIGHT);
}
}
@EventHandler(priority = EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onDamageMonitor(final EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
if (damager instanceof Player) {
checkUnexempt((Player) damager, event.getClass(), CheckType.FIGHT);
}
}
}

View File

@ -1,176 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
import fr.neatmonster.nocheatplus.utilities.TickTask;
public class HookInstaBreak extends AbstractHook implements ConfigurableHook, Listener {
public static interface InstaExemption{
public void addExemptNext(CheckType[] types);
public Set<CheckType> getExemptNext();
}
public static class StackEntry{
public final CheckType[] checkTypes;
public final int tick;
public final Player player;
public boolean used = false;
public StackEntry(final Player player , final CheckType[] checkTypes){
this.player = player;
this.checkTypes = checkTypes;
tick = TickTask.getTick();
}
public boolean isOutdated(final int tick){
return tick != this.tick;
}
}
protected static InstaExemption runtime = null;
public static void addExemptNext(final CheckType[] types){
runtime.addExemptNext(types);
}
protected final ExemptionManager exMan = new ExemptionManager();
protected boolean enabled = true;
protected final List<StackEntry> stack = new LinkedList<StackEntry>();
@Override
public String getHookName() {
return "InstaBreak(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + "insta-break.enabled", true);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + "insta-break.enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public CheckType[] getCheckTypes() {
return null;
}
@Override
public Listener[] getListeners() {
runtime = new InstaExemption() {
protected final Set<CheckType> types = new HashSet<CheckType>();
@Override
public final void addExemptNext(final CheckType[] types) {
for (int i = 0; i < types.length; i++){
this.types.add(types[i]);
}
}
@Override
public Set<CheckType> getExemptNext() {
return types;
}
};
return new Listener[]{this};
}
protected CheckType[] fetchTypes(){
final Set<CheckType> types = runtime.getExemptNext();
final CheckType[] a = new CheckType[types.size()];
if (!types.isEmpty()) types.toArray(a);
types.clear();
return a;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public void onBlockDamage(final BlockDamageEvent event){
checkStack();
if (!event.isCancelled() && event.getInstaBreak()){
stack.add(new StackEntry(event.getPlayer(), fetchTypes()));
}
else{
runtime.getExemptNext().clear();
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public void onBlockBreakLowest(final BlockBreakEvent event){
checkStack();
if (!stack.isEmpty()){
final Player player = event.getPlayer();
final StackEntry entry = stack.get(stack.size() - 1);
if (player.equals(entry.player)) addExemption(entry);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public void onBlockBreakMONITOR(final BlockBreakEvent event){
if (!stack.isEmpty()){
final Player player = event.getPlayer();
final StackEntry entry = stack.get(stack.size() - 1);
if (player.equals(entry.player)) removeExemption(stack.remove(stack.size() - 1));
}
}
public void addExemption(final StackEntry entry){
entry.used = true;
for (int i = 0; i < entry.checkTypes.length; i++){
exMan.addExemption(entry.player, entry.checkTypes[i]);
}
}
public void removeExemption(final StackEntry entry){
if (!entry.used) return;
for (int i = 0; i < entry.checkTypes.length; i++){
exMan.removeExemption(entry.player, entry.checkTypes[i]);
}
}
public void checkStack(){
if (stack.isEmpty()) return;
Iterator<StackEntry> it = stack.iterator();
final int tick = TickTask.getTick();
while (it.hasNext()){
final StackEntry entry = it.next();
if (entry.isOutdated(tick)) it.remove();
if (entry.used) removeExemption(entry);
}
}
}

View File

@ -1,62 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.Arrays;
/**
* Wrap player interact events to exempt players from checks by comparison of event class names.
* Uses mc_dev's format for exemption based upon class names.
*
*/
public class HookPlayerInteract extends ClassExemptionHook implements Listener{
public HookPlayerInteract() {
super("player-interact.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MagicSpells
"MagicSpellsPlayerInteractEvent"
}));
}
@Override
public String getHookName() {
return "Interact(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onPlayerInteractLowest(final PlayerInteractEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKINTERACT);
}
@EventHandler(priority = EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onPlayerInteractMonitor(final PlayerInteractEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKINTERACT);
}
}

View File

@ -1,109 +0,0 @@
package me.asofold.bpl.cncp.hooks.generic;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
public class HookSetSpeed extends AbstractHook implements Listener, ConfigurableHook{
private static final float defaultFlySpeed = 0.1f;
private static final float defaultWalkSpeed = 0.2f;
protected float flySpeed = defaultFlySpeed;
protected float walkSpeed = defaultWalkSpeed;
protected boolean enabled = false;
// private String allowFlightPerm = "cncp.allow-flight";
public HookSetSpeed() throws SecurityException, NoSuchMethodException{
Player.class.getDeclaredMethod("setFlySpeed", float.class);
}
public void init(){
for (final Player player : Bukkit.getOnlinePlayers()){
setSpeed(player);
}
}
@Override
public String getHookName() {
return "SetSpeed(default)";
}
@Override
public String getHookVersion() {
return "2.2";
}
@Override
public CheckType[] getCheckTypes() {
return new CheckType[0];
}
@Override
public Listener[] getListeners() {
try{
// Initialize here, at the end of enable.
init();
}
catch (Throwable t){}
return new Listener[]{this} ;
}
public final void setSpeed(final Player player){
// if (allowFlightPerm.equals("") || player.hasPermission(allowFlightPerm)) player.setAllowFlight(true);
player.setWalkSpeed(walkSpeed);
player.setFlySpeed(flySpeed);
}
@EventHandler(priority=EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onPlayerJoin(final PlayerJoinEvent event){
setSpeed(event.getPlayer());
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + "set-speed.enabled", false);
flySpeed = cfg.getDouble(prefix + "set-speed.fly-speed", (double) defaultFlySpeed).floatValue();
walkSpeed = cfg.getDouble(prefix + "set-speed.walk-speed", (double) defaultWalkSpeed).floatValue();
// allowFlightPerm = cfg.getString(prefix + "set-speed.allow-flight-permission", ref.allowFlightPerm);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + "set-speed.enabled", false);
defaults.set(prefix + "set-speed.fly-speed", defaultFlySpeed);
defaults.set(prefix + "set-speed.walk-speed", defaultWalkSpeed);
// cfg.set(prefix + "set-speed.allow-flight-permission", ref.allowFlightPerm);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
// public String getAllowFlightPerm() {
// return allowFlightPerm;
// }
// public void setAllowFlightPerm(String allowFlightPerm) {
// this.allowFlightPerm = allowFlightPerm;
// }
}

View File

@ -1,186 +0,0 @@
package me.asofold.bpl.cncp.hooks.mcmmo;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
import me.asofold.bpl.cncp.utils.PluginGetter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder.RegisterMethodWithOrder;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
public final class HookmcMMO extends AbstractHook implements Listener, ConfigurableHook {
/**
* To let the listener access this.
* @author mc_dev
*
*/
public static interface HookFacade{
public void damageLowest(Player player);
public void damageMonitor(Player player);
public void blockDamageLowest(Player player);
public void blockDamageMonitor(Player player);
/**
* If to cancel the event.
* @param player
* @return
*/
public boolean blockBreakLowest(Player player);
public void blockBreakMontitor(Player player);
}
protected HookFacade ncpHook = null;
protected boolean enabled = true;
protected String configPrefix = "mcmmo.";
protected boolean useInstaBreakHook = true;
public HookmcMMO(){
assertPluginPresent("mcMMO");
}
protected final PluginGetter<mcMMO> fetch = new PluginGetter<mcMMO>("mcMMO");
protected int blocksPerSecond = 30;
@Override
public String getHookName() {
return "mcMMO(default)";
}
@Override
public String getHookVersion() {
return "2.1";
}
@Override
public CheckType[] getCheckTypes() {
return new CheckType[]{
CheckType.BLOCKBREAK_FASTBREAK, CheckType.BLOCKBREAK_NOSWING, // old ones
// CheckType.BLOCKBREAK_DIRECTION, CheckType.BLOCKBREAK_FREQUENCY,
// CheckType.BLOCKBREAK_WRONGBLOCK, CheckType.BLOCKBREAK_REACH,
//
// CheckType.FIGHT_ANGLE, CheckType.FIGHT_SPEED, // old ones
//
// CheckType.FIGHT_DIRECTION, CheckType.FIGHT_NOSWING,
// CheckType.FIGHT_REACH,
};
}
@Override
public Listener[] getListeners() {
fetch.fetchPlugin();
return new Listener[]{this, fetch};
}
@Override
public NCPHook getNCPHook() {
if (ncpHook == null){
ncpHook = new HookFacadeImpl(useInstaBreakHook, blocksPerSecond);
}
return (NCPHook) ncpHook;
}
///////////////////////////
// Damage (fight)
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onDamageLowest(final FakeEntityDamageByEntityEvent event){
final Entity entity = event.getDamager();
if (entity instanceof Player)
ncpHook.damageLowest((Player) entity);
}
@EventHandler(priority=EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onDamageMonitor(final FakeEntityDamageByEntityEvent event){
final Entity entity = event.getDamager();
if (entity instanceof Player)
ncpHook.damageMonitor((Player) entity);
}
///////////////////////////
// Block damage
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onBlockDamageLowest(final FakeBlockDamageEvent event){
ncpHook.blockDamageLowest(event.getPlayer());
}
@EventHandler(priority=EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onBlockDamageMonitor(final FakeBlockDamageEvent event){
ncpHook.blockDamageMonitor(event.getPlayer());
}
///////////////////////////
// Block break
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onBlockBreakLowest(final FakeBlockBreakEvent event){
if (ncpHook.blockBreakLowest(event.getPlayer())){
event.setCancelled(true);
// System.out.println("Cancelled for frequency.");
}
}
@EventHandler(priority=EventPriority.MONITOR)
@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onBlockBreakLMonitor(final FakeBlockBreakEvent event){
ncpHook.blockBreakMontitor(event.getPlayer());
}
/////////////////////////////////
// Config
/////////////////////////////////
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
useInstaBreakHook = cfg.getBoolean(prefix + configPrefix + "use-insta-break-hook", true);
blocksPerSecond = cfg.getInt(prefix + configPrefix + "clickspersecond", 20);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true);
defaults.set(prefix + configPrefix + "use-insta-break-hook", true);
defaults.set(prefix + configPrefix + "clickspersecond", 20);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
}

File diff suppressed because it is too large Load Diff

4
README.md Normal file
View File

@ -0,0 +1,4 @@
CompatNoCheatPlus
---------
CompatNoCheatPlus (cncp) provides compatibility between the anti cheat plugin NoCheatPlus and other plugins that add game mechanics different to the vanilla game behavior, such as mcMMO or plugins that add npcs such as Citizens and protocol hack like Geyser for cross-platform to play with.

4
bungee.yml Normal file
View File

@ -0,0 +1,4 @@
name: CompatNoCheatPlus
main: me.asofold.bpl.cncp.bungee.CompatNoCheatPlus
version: ${project.version}-${buildDescription}
author: asofold, xaw3ep

View File

@ -1,72 +1,72 @@
CompatNoCheatPlus lists file CompatNoCheatPlus lists file
------------------------------------------------------- -------------------------------------------------------
Compatibility hooks for NoCheatPlus! Compatibility hooks for NoCheatPlus!
LICENSE LICENSE
------------------------- -------------------------
LICENSE.txt (Same as NoCheatPlus). LICENSE.txt (Same as NoCheatPlus).
STACK STACK
--------- ---------
?(add) more intricate load order stuff ? [best would be to force managelisteners with ncp] ?(add) more intricate load order stuff ? [best would be to force managelisteners with ncp]
*** ***
!(add) reload command !(add) reload command
? another sequence number (for standard events) ? another sequence number (for standard events)
*** ADD MORE GENERIC HOOKS *** ADD MORE GENERIC HOOKS
!add check type and permission hooks, also for worldguard regions. !add check type and permission hooks, also for worldguard regions.
Citizens2 / Player class: make configurable (hidden) Or do internally: List of checks to use , exclude moving if possible. Citizens2 / Player class: make configurable (hidden) Or do internally: List of checks to use , exclude moving if possible.
Generic abstract class for the mcMMO style cancelling of next x events + ticks alive etc Generic abstract class for the mcMMO style cancelling of next x events + ticks alive etc
add stats hook ? add stats hook ?
add a good mechanism for adding external configurable hooks (read automatically from the cncp config). add a good mechanism for adding external configurable hooks (read automatically from the cncp config).
!(add) Use some exemption mechanism for npcs (generic player class hook + citizens). !(add) Use some exemption mechanism for npcs (generic player class hook + citizens).
!consider remove: clearing the VL ? => probably not, needs redesign to also monitor block break. + only clear the necessary bits (not frequency) !consider remove: clearing the VL ? => probably not, needs redesign to also monitor block break. + only clear the necessary bits (not frequency)
! try: insta break: keep exemption (unless cancelled) for next block break event (!). -> maybe ncp ! try: insta break: keep exemption (unless cancelled) for next block break event (!). -> maybe ncp
? HookInstaBreak : add static method to sset check types to exempt from for next break ? ? HookInstaBreak : add static method to sset check types to exempt from for next break ?
cncp: check at least for logs / leaves for skill specific block types cncp: check at least for logs / leaves for skill specific block types
hookiinstabreak: let hooks fail completely if listeners are failing to register ? hookiinstabreak: let hooks fail completely if listeners are failing to register ?
*** CLEANUP THIS, BETTER METHODS, INTEGRATE SOME INTO NCP MAYBE *** CLEANUP THIS, BETTER METHODS, INTEGRATE SOME INTO NCP MAYBE
* Auto detection of unknown events + log on disable + info command. * Auto detection of unknown events + log on disable + info command.
? analysis tools like event-Mirror ? ? analysis tools like event-Mirror ?
* More generic hooks, clean methods! * More generic hooks, clean methods!
! Strip down mcMMO hook to add a new one / integrate into instabreak ! Strip down mcMMO hook to add a new one / integrate into instabreak
! Clean up hooks, use TickTask and "next" to confine fuzzy unexemption to a minimum. ! Clean up hooks, use TickTask and "next" to confine fuzzy unexemption to a minimum.
-------------------------- --------------------------
support for instant spells ? [internalName or name ? <- guess: name] support for instant spells ? [internalName or name ? <- guess: name]
? register listeners with NCP directly? also use annotations ? ? register listeners with NCP directly? also use annotations ?
! Smarter way to enable / disable PlayerClassHoook (disable by default for now). ! Smarter way to enable / disable PlayerClassHoook (disable by default for now).
! if SpoutPlugin is present: add blocks to ignorepassable automatically / adjust flags ? [generic block setup] ! if SpoutPlugin is present: add blocks to ignorepassable automatically / adjust flags ? [generic block setup]
? add info command: show all hooks and so on. ? add info command: show all hooks and so on.
add class-name inspection methods (!). add class-name inspection methods (!).
set-speed: per world options ? [per world config concept] set-speed: per world options ? [per world config concept]
set-speed: add option to set speed to default speed on player quit/kick set-speed: add option to set speed to default speed on player quit/kick

BIN
libs/CMIAPI7.6.2.0.jar Normal file

Binary file not shown.

BIN
libs/GravityTubes.jar Normal file

Binary file not shown.

BIN
libs/mcMMO-2.1.158.jar Normal file

Binary file not shown.

View File

@ -1,19 +1,21 @@
name: CompatNoCheatPlus name: CompatNoCheatPlus
main: me.asofold.bpl.cncp.CompatNoCheatPlus main: me.asofold.bpl.cncp.CompatNoCheatPlus
version: ${project.version}-s${BUILD_SERIES}-b${BUILD_NUMBER} version: ${project.version}-${buildDescription}
dev-url: http://dev.bukkit.org/server-mods/compatnocheatplus-cncp/ dev-url: http://dev.bukkit.org/server-mods/compatnocheatplus-cncp/
api-version: 1.13
depend: folia-supported: true
- NoCheatPlus
softdepend: loadbefore:
- mcMMO - NoCheatPlus
- Citizens softdepend:
- MagicSpells - mcMMO
- Citizens
commands: - MagicSpells
compatnocheatplus:
description: 'Show general version information of cncp and dependencies.' commands:
usage: '/<command>' compatnocheatplus:
permission: 'cncp.cmd.info' description: 'Show general version information of cncp and dependencies.'
aliases: usage: '/<command>'
- cncp permission: 'cncp.cmd.info'
aliases:
- cncp

187
pom.xml Normal file
View File

@ -0,0 +1,187 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CompatNoCheatPlus</groupId>
<artifactId>CompatNoCheatPlus</artifactId>
<version>6.6.7-SNAPSHOT</version>
<name>CompatNoCheatPlus</name>
<!-- Source code -->
<scm>
<developerConnection>scm:git:git@github.com:asofold/${project.name}.git</developerConnection>
<connection>scm:git:git://github.com/asofold/${project.name}.git</connection>
<url>https://github.com/asofold/${project.name}</url>
</scm>
<!-- Repositories -->
<repositories>
<repository>
<id>opencollab-snapshot-repo</id>
<url>https://repo.opencollab.dev/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>viaversion-repo</id>
<url>https://repo.viaversion.com</url>
</repository>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId>
<version>2.1.158</version>
<scope>system</scope>
<systemPath>${basedir}/libs/mcMMO-2.1.158.jar</systemPath>
</dependency>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizensapi</artifactId>
<version>2.0.26-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.benzoft.gravitytubes</groupId>
<artifactId>gravitytubes</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/GravityTubes.jar</systemPath>
</dependency>
<dependency>
<groupId>com.Zrips.CMI</groupId>
<artifactId>CMI</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/CMIAPI7.6.2.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.github.updated-nocheatplus.nocheatplus</groupId>
<artifactId>nocheatplus</artifactId>
<version>-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.geysermc</groupId>
<artifactId>connector</artifactId>
<version>1.4.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.viaversion</groupId>
<artifactId>viaversion-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-event</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- Build Description Profiles -->
<profiles>
<profile>
<id>timestamp</id>
<activation>
<property>
<name>!env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildDescription>${maven.build.timestamp}</buildDescription>
</properties>
</profile>
<profile>
<id>dynamic_build_number</id>
<activation>
<property>
<name>env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildDescription>b${env.BUILD_NUMBER}</buildDescription>
</properties>
</profile>
</profiles>
<!-- Building -->
<build>
<defaultGoal>clean package</defaultGoal>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}</directory>
<includes>
<include>plugin.yml</include>
<include>LICENSE.txt</include>
<include>bungee.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>cncp</finalName>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<pomPropertiesFile>false</pomPropertiesFile>
<manifest>
<addDefaultSpecificationEntries>false</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>false</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<!-- Properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyy_MM_dd-HH_mm</maven.build.timestamp.format>
</properties>
</project>

View File

@ -0,0 +1,50 @@
package me.asofold.bpl.cncp.ClientVersion;
import java.lang.reflect.Method;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import com.viaversion.viaversion.api.Via;
import fr.neatmonster.nocheatplus.compat.Folia;
import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.players.IPlayerData;
import fr.neatmonster.nocheatplus.utilities.ReflectionUtil;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
public class ClientVersionListener implements Listener {
private Plugin ViaVersion = Bukkit.getPluginManager().getPlugin("ViaVersion");
private Plugin ProtocolSupport = Bukkit.getPluginManager().getPlugin("ProtocolSupport");
private final Class<?> ProtocolSupportAPIClass = ReflectionUtil.getClass("protocolsupport.api.ProtocolSupportAPI");
private final Class<?> ProtocolVersionClass = ReflectionUtil.getClass("protocolsupport.api.ProtocolVersion");
private final Method getProtocolVersion = ProtocolSupportAPIClass == null ? null : ReflectionUtil.getMethod(ProtocolSupportAPIClass, "getProtocolVersion", Player.class);
@SuppressWarnings("unchecked")
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
Folia.runSyncDelayedTask(CompatNoCheatPlus.getInstance(), (arg) -> {
final IPlayerData pData = DataManager.getPlayerData(player);
if (pData != null) {
if (ViaVersion != null && ViaVersion.isEnabled()) {
// Give precedence to ViaVersion
pData.setClientVersionID(Via.getAPI().getPlayerVersion(player));
}
else if (ProtocolSupport != null && getProtocolVersion != null && ProtocolSupport.isEnabled()) {
// Fallback to PS (reflectively, due to PS not having a valid mvn repo)
Object protocolVersion = ReflectionUtil.invokeMethod(getProtocolVersion, null, player);
Method getId = ReflectionUtil.getMethodNoArgs(ProtocolVersionClass, "getId", int.class);
int version = (int) ReflectionUtil.invokeMethodNoArgs(getId, protocolVersion);
pData.setClientVersionID(version);
}
// (Client version stays unknown (-1))
}
}, 20); // Wait 20 ticks before setting client data
}
}

View File

@ -1,423 +1,503 @@
package me.asofold.bpl.cncp; package me.asofold.bpl.cncp;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedHashSet;
import java.util.List; import java.util.LinkedList;
import java.util.Set; import java.util.List;
import java.util.logging.Level; import java.util.Set;
import java.util.logging.Level;
import org.bukkit.Bukkit; import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.Server;
import org.bukkit.plugin.Plugin; import org.bukkit.command.Command;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import fr.neatmonster.nocheatplus.NCPAPIProvider; import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.components.registry.feature.IDisableListener; import org.bukkit.event.server.PluginEnableEvent;
import fr.neatmonster.nocheatplus.components.registry.order.RegistrationOrder; import org.bukkit.plugin.Plugin;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import org.bukkit.plugin.PluginDescriptionFile;
import fr.neatmonster.nocheatplus.hooks.NCPHookManager; import org.bukkit.plugin.PluginManager;
import me.asofold.bpl.cncp.config.Settings; import org.bukkit.plugin.java.JavaPlugin;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import org.bukkit.scheduler.BukkitScheduler;
import me.asofold.bpl.cncp.config.compatlayer.NewConfig; import fr.neatmonster.nocheatplus.NCPAPIProvider;
import me.asofold.bpl.cncp.hooks.Hook; import fr.neatmonster.nocheatplus.compat.Folia;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook; import fr.neatmonster.nocheatplus.components.registry.feature.IDisableListener;
import me.asofold.bpl.cncp.hooks.generic.HookBlockBreak; import fr.neatmonster.nocheatplus.hooks.NCPHook;
import me.asofold.bpl.cncp.hooks.generic.HookBlockPlace; import fr.neatmonster.nocheatplus.hooks.NCPHookManager;
import me.asofold.bpl.cncp.hooks.generic.HookEntityDamageByEntity; import me.asofold.bpl.cncp.bedrock.BedrockPlayerListener;
import me.asofold.bpl.cncp.hooks.generic.HookInstaBreak; import me.asofold.bpl.cncp.ClientVersion.ClientVersionListener;
import me.asofold.bpl.cncp.hooks.generic.HookPlayerClass; import me.asofold.bpl.cncp.config.Settings;
import me.asofold.bpl.cncp.hooks.generic.HookPlayerInteract; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.utils.TickTask2; import me.asofold.bpl.cncp.config.compatlayer.NewConfig;
import me.asofold.bpl.cncp.hooks.Hook;
/** import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
* Quick attempt to provide compatibility to NoCheatPlus (by NeatMonster) for import me.asofold.bpl.cncp.hooks.generic.HookBlockBreak;
* some other plugins that change the vanilla game mechanichs, for instance by import me.asofold.bpl.cncp.hooks.generic.HookBlockPlace;
* fast block breaking. import me.asofold.bpl.cncp.hooks.generic.HookEntityDamageByEntity;
* import me.asofold.bpl.cncp.hooks.generic.HookInstaBreak;
* @author asofold import me.asofold.bpl.cncp.hooks.generic.HookPlayerClass;
* import me.asofold.bpl.cncp.hooks.generic.HookPlayerInteract;
*/ import me.asofold.bpl.cncp.utils.TickTask2;
public class CompatNoCheatPlus extends JavaPlugin { import me.asofold.bpl.cncp.utils.Utils;
//TODO: Adjust, once NCP has order everywhere (generic + ncp-specific?). /**
* Quick attempt to provide compatibility to NoCheatPlus (by NeatMonster) for some other plugins that change the vanilla game mechanichs, for instance by fast block breaking.
public static final String tagEarlyFeature = "cncp.feature.early"; * @author mc_dev
public static final String beforeTagEarlyFeature = ".*nocheatplus.*|.*NoCheatPlus.*"; *
*/
public static final String tagLateFeature = "cncp.feature.late"; public class CompatNoCheatPlus extends JavaPlugin implements Listener {
public static final String afterTagLateFeature = "cncp.system.early.*|.*nocheatplus.*|.*NoCheatPlus.*)";
private static CompatNoCheatPlus instance = null;
public static final String tagEarlySystem = "cncp.system.early";
public static final String beforeTagEarlySystem = beforeTagEarlyFeature; // ... private final Settings settings = new Settings();
public static final RegistrationOrder defaultOrderSystemEarly = new RegistrationOrder( private boolean bungee;
tagEarlySystem,
beforeTagEarlySystem, /** Hooks registered with cncp */
null); private static final Set<Hook> registeredHooks = new HashSet<Hook>();
public static final RegistrationOrder defaultOrderFeatureEarly = new RegistrationOrder( private final List<Hook> builtinHooks = new LinkedList<Hook>();
tagEarlyFeature,
beforeTagEarlyFeature, /**
"cncp.system.early.*"); * Flag if plugin is enabled.
*/
public static final RegistrationOrder defaultOrderFeatureLate = new RegistrationOrder( private static boolean enabled = false;
tagLateFeature,
null, /**
afterTagLateFeature); * Experimental: static method to enable this plugin, only enables if it is not already enabled.
* @return
private final Settings settings = new Settings(); */
public static boolean enableCncp(){
/** Hooks registered with cncp */ if (enabled) return true;
private static final Set<Hook> registeredHooks = new HashSet<Hook>(); return enablePlugin("CompatNoCheatPlus");
}
private final List<Hook> builtinHooks = new LinkedList<Hook>();
/**
/** * Static method to enable a plugin (might also be useful for hooks).
* Flag if plugin is enabled. * @param plgName
*/ * @return
private static boolean enabled = false; */
public static boolean enablePlugin(String plgName) {
/** PluginManager pm = Bukkit.getPluginManager();
* Static method to enable a plugin (might also be useful for hooks). Plugin plugin = pm.getPlugin(plgName);
* @param plgName if (plugin == null) return false;
* @return if (pm.isPluginEnabled(plugin)) return true;
*/ pm.enablePlugin(plugin);
public static boolean enablePlugin(String plgName) { return true;
PluginManager pm = Bukkit.getPluginManager(); }
Plugin plugin = pm.getPlugin(plgName);
if (plugin == null) return false; /**
if (pm.isPluginEnabled(plugin)) return true; * Static method to disable a plugin (might also be useful for hooks).
pm.enablePlugin(plugin); * @param plgName
return true; * @return
} */
public static boolean disablePlugin(String plgName){
/** PluginManager pm = Bukkit.getPluginManager();
* Static method to disable a plugin (might also be useful for hooks). Plugin plugin = pm.getPlugin(plgName);
* @param plgName if (plugin == null) return false;
* @return if (!pm.isPluginEnabled(plugin)) return true;
*/ pm.disablePlugin(plugin);
public static boolean disablePlugin(String plgName){ return true;
PluginManager pm = Bukkit.getPluginManager(); }
Plugin plugin = pm.getPlugin(plgName);
if (plugin == null) return false; /**
if (!pm.isPluginEnabled(plugin)) return true; * Get the plugin instance.
pm.disablePlugin(plugin); * @return
return true; */
} public static CompatNoCheatPlus getInstance(){
return instance;
/** }
* Get the plugin instance.
* @return /**
*/ * API to add a hook. Adds the hook AND registers listeners if enabled. Also respects the configuration for preventing hooks.<br>
public static CompatNoCheatPlus getInstance(){ * If you want to not register the listeners use NCPHookManager.
return CompatNoCheatPlus.getPlugin(CompatNoCheatPlus.class); * @param hook
} * @return
*/
/** public static boolean addHook(Hook hook){
* API to add a hook. Adds the hook AND registers listeners if enabled. Also respects the configuration for preventing hooks.<br> if (Settings.preventAddHooks.contains(hook.getHookName())){
* If you want to not register the listeners use NCPHookManager. Bukkit.getLogger().info("[CompatNoCheatPlus] Prevented adding hook: "+hook.getHookName() + " / " + hook.getHookVersion());
* @param hook return false;
* @return }
*/ registeredHooks.add(hook);
public static boolean addHook(Hook hook){ if (enabled) registerListeners(hook);
if (Settings.preventAddHooks.contains(hook.getHookName())){ boolean added = checkAddNCPHook(hook); // Add if plugin is present, otherwise queue for adding.
Bukkit.getLogger().info("[cncp] Prevented adding hook: "+hook.getHookName() + " / " + hook.getHookVersion()); Bukkit.getLogger().info("[CompatNoCheatPlus] Registered hook"+(added?"":"(NCPHook might get added later)")+": "+hook.getHookName() + " / " + hook.getHookVersion());
return false; return true;
} }
registeredHooks.add(hook);
if (enabled) registerListeners(hook); /**
boolean added = checkAddNCPHook(hook); // Add if plugin is present, otherwise queue for adding. * If already added to NCP
Bukkit.getLogger().info("[cncp] Registered hook"+(added?"":"(NCPHook might get added later)")+": "+hook.getHookName() + " / " + hook.getHookVersion()); * @param hook
return true; * @return
} */
private static boolean checkAddNCPHook(Hook hook) {
/** PluginManager pm = Bukkit.getPluginManager();
* If already added to NCP Plugin plugin = pm.getPlugin("NoCheatPlus");
* @param hook if (plugin == null || !pm.isPluginEnabled(plugin))
* @return return false;
*/ NCPHook ncpHook = hook.getNCPHook();
private static boolean checkAddNCPHook(Hook hook) { if (ncpHook != null)
PluginManager pm = Bukkit.getPluginManager(); NCPHookManager.addHook(hook.getCheckTypes(), ncpHook);
Plugin plugin = pm.getPlugin("NoCheatPlus"); return true;
if (plugin == null || !pm.isPluginEnabled(plugin)) }
return false;
NCPHook ncpHook = hook.getNCPHook(); /**
if (ncpHook != null) * Conveniently register the listeners, do not use if you add/added the hook with addHook.
NCPHookManager.addHook(hook.getCheckTypes(), ncpHook); * @param hook
return true; * @return
} */
public static boolean registerListeners(Hook hook) {
/** if (!enabled) return false;
* Conveniently register the listeners, do not use if you add/added the hook with addHook. Listener[] listeners = hook.getListeners();
* @param hook if (listeners != null){
* @return // attempt to register events:
*/ PluginManager pm = Bukkit.getPluginManager();
public static boolean registerListeners(Hook hook) { Plugin plg = pm.getPlugin("CompatNoCheatPlus");
if (!enabled) { if (plg == null) return false;
return false; for (Listener listener : listeners) {
} pm.registerEvents(listener, plg);
Listener[] listeners = hook.getListeners(); }
if (listeners != null){ }
// attempt to register events: return true;
Plugin plg = CompatNoCheatPlus.getPlugin(CompatNoCheatPlus.class); }
if (plg == null) {
return false; /**
} * Called before loading settings, adds available hooks into a list, so they will be able to read config.
for (Listener listener : listeners) { */
NCPAPIProvider.getNoCheatPlusAPI().getEventRegistry().register(listener, private void setupBuiltinHooks() {
defaultOrderFeatureEarly, plg); builtinHooks.clear();
} // Might-fail hooks:
} // Set speed
return true; try {
} builtinHooks.add(new me.asofold.bpl.cncp.hooks.generic.HookSetSpeed());
}
// ---- catch (Throwable t) {}
// Citizens 2
/** try {
* Called before loading settings, adds available hooks into a list, so they will be able to read config. builtinHooks.add(new me.asofold.bpl.cncp.hooks.citizens2.HookCitizens2());
*/ }
private void setupBuiltinHooks() { catch (Throwable t) {}
builtinHooks.clear(); // mcMMO
// Might-fail hooks: try {
// Set speed builtinHooks.add(new me.asofold.bpl.cncp.hooks.mcmmo.HookmcMMO());
try{ }
builtinHooks.add(new me.asofold.bpl.cncp.hooks.generic.HookSetSpeed()); catch (Throwable t) {}
} // GravityTubes
catch (Throwable t){} try {
// Citizens 2 builtinHooks.add(new me.asofold.bpl.cncp.hooks.GravityTubes.HookGravityTubes());
try{ }
builtinHooks.add(new me.asofold.bpl.cncp.hooks.citizens2.HookCitizens2()); catch (Throwable t) {}
} // CMI
catch (Throwable t){} try {
// mcMMO builtinHooks.add(new me.asofold.bpl.cncp.hooks.CMI.HookCMI());
try{ }
builtinHooks.add(new me.asofold.bpl.cncp.hooks.mcmmo.HookmcMMO()); catch (Throwable t){}
} // // MagicSpells
catch (Throwable t){} // try{
// // MagicSpells // builtinHooks.add(new me.asofold.bpl.cncp.hooks.magicspells.HookMagicSpells());
// try{ // }
// builtinHooks.add(new me.asofold.bpl.cncp.hooks.magicspells.HookMagicSpells()); // catch (Throwable t){}
// } // Simple generic hooks
// catch (Throwable t){} for (Hook hook : new Hook[]{
// Simple generic hooks new HookPlayerClass(),
for (Hook hook : new Hook[]{ new HookBlockBreak(),
new HookPlayerClass(), new HookBlockPlace(),
new HookBlockBreak(), new HookInstaBreak(),
new HookBlockPlace(), new HookEntityDamageByEntity(),
new HookInstaBreak(), new HookPlayerInteract()
new HookEntityDamageByEntity(), }){
new HookPlayerInteract() builtinHooks.add(hook);
}){ }
builtinHooks.add(hook); }
}
} /**
* Add standard hooks if enabled.
/** */
* Add standard hooks if enabled. private void addAvailableHooks() {
*/
private void addAvailableHooks() { // Add built in hooks:
for (Hook hook : builtinHooks){
// Add built in hooks: boolean add = true;
for (Hook hook : builtinHooks){ if (hook instanceof ConfigurableHook){
boolean add = true; if (!((ConfigurableHook)hook).isEnabled()) add = false;
if (hook instanceof ConfigurableHook){ }
if (!((ConfigurableHook)hook).isEnabled()) add = false; if (add){
} try{
if (add){ addHook(hook);
try{ }
addHook(hook); catch (Throwable t){}
} }
catch (Throwable t){} }
} }
}
} @Override
public void onEnable() {
@Override enabled = false; // make sure
public void onEnable() { instance = this;
enabled = false; // make sure // (no cleanup)
// (no cleanup)
// Settings:
// Settings: settings.clear();
settings.clear(); setupBuiltinHooks();
setupBuiltinHooks(); loadSettings();
loadSettings(); // Register own listener:
// Register own listener: final PluginManager pm = getServer().getPluginManager();
//NCPAPIProvider.getNoCheatPlusAPI().getEventRegistry().register(this, defaultOrderSystemEarly, this); pm.registerEvents(this, this);
super.onEnable(); pm.registerEvents(new BedrockPlayerListener(), this);
pm.registerEvents(new ClientVersionListener(), this);
// Add Hooks: getServer().getMessenger().registerIncomingPluginChannel(this, "cncp:geyser", new BedrockPlayerListener());
addAvailableHooks(); // add before enable is set to not yet register listeners. try {
enabled = true; bungee = getServer().spigot().getConfig().getBoolean("settings.bungeecord");
// register all listeners: // sometimes not work, try the hard way
for (Hook hook : registeredHooks){ if (!bungee) {
registerListeners(hook); bungee = YamlConfiguration.loadConfiguration(new File("spigot.yml")).getBoolean("settings.bungeecord");
} }
} catch (Throwable t) {
// Register to remove hooks when NCP is disabling. bungee = false;
NCPAPIProvider.getNoCheatPlusAPI().addComponent(new IDisableListener(){ }
@Override super.onEnable();
public void onDisable() {
// Remove all registered cncp hooks: // Add Hooks:
unregisterNCPHooks(); addAvailableHooks(); // add before enable is set to not yet register listeners.
} enabled = true;
});
if (!registeredHooks.isEmpty()) { // register all listeners:
registerHooks(); for (Hook hook : registeredHooks){
} registerListeners(hook);
}
// Start ticktask 2
// TODO: Replace by using TickTask ? Needs order (depend is used now)? // Start ticktask 2
getServer().getScheduler().scheduleSyncRepeatingTask(this, new TickTask2(), 1, 1); Folia.runSyncRepatingTask(this, (arg) -> new TickTask2().run(), 1, 1);
// Finished. // Check for the NoCheatPlus plugin.
getLogger().info(getDescription().getFullName() + " is enabled. Some hooks might get registered with NoCheatPlus later on."); Plugin plugin = pm.getPlugin("NoCheatPlus");
} if (plugin == null) {
getLogger().severe("[CompatNoCheatPlus] The NoCheatPlus plugin is not present.");
public boolean loadSettings() { }
// Read and apply config to settings: else if (plugin.isEnabled()) {
File file = new File(getDataFolder() , "cncp.yml"); getLogger().severe("[CompatNoCheatPlus] The NoCheatPlus plugin already is enabled, this might break several hooks.");
CompatConfig cfg = new NewConfig(file); }
cfg.load();
boolean changed = false; // Finished.
// General settings: getLogger().info(getDescription().getFullName() + " is enabled. Some hooks might get registered with NoCheatPlus later on.");
if (Settings.addDefaults(cfg)) { }
changed = true;
} public boolean loadSettings() {
settings.fromConfig(cfg); final Set<String> oldForceEnableLater = new LinkedHashSet<String>();
// Settings for builtin hooks: oldForceEnableLater.addAll(settings.forceEnableLater);
for (Hook hook : builtinHooks){ // Read and apply config to settings:
if (hook instanceof ConfigurableHook){ File file = new File(getDataFolder() , "cncp.yml");
try{ CompatConfig cfg = new NewConfig(file);
ConfigurableHook cfgHook = (ConfigurableHook) hook; cfg.load();
if (cfgHook.updateConfig(cfg, "hooks.")) changed = true; boolean changed = false;
cfgHook.applyConfig(cfg, "hooks."); // General settings:
} if (Settings.addDefaults(cfg)) changed = true;
catch (Throwable t){ settings.fromConfig(cfg);
getLogger().severe("[cncp] Hook failed to process config ("+hook.getHookName() +" / " + hook.getHookVersion()+"): " + t.getClass().getSimpleName() + ": "+t.getMessage()); // Settings for builtin hooks:
t.printStackTrace(); for (Hook hook : builtinHooks){
} if (hook instanceof ConfigurableHook){
} try{
} ConfigurableHook cfgHook = (ConfigurableHook) hook;
// save back config if changed: if (cfgHook.updateConfig(cfg, "hooks.")) changed = true;
if (changed) { cfgHook.applyConfig(cfg, "hooks.");
cfg.save(); }
} catch (Throwable t){
getLogger().severe("[CompatNoCheatPlus] Hook failed to process config ("+hook.getHookName() +" / " + hook.getHookVersion()+"): " + t.getClass().getSimpleName() + ": "+t.getMessage());
return true; t.printStackTrace();
} }
}
@Override }
public void onDisable() { // save back config if changed:
unregisterNCPHooks(); // Just in case. if (changed) cfg.save();
enabled = false;
super.onDisable();
}
// Re-enable plugins that were not yet on the list:
protected int unregisterNCPHooks() { Server server = getServer();
// TODO: Clear list here !? Currently done externally... Logger logger = server.getLogger();
int n = 0; for (String plgName : settings.loadPlugins){
for (Hook hook : registeredHooks) { try{
String hookDescr = null; if (CompatNoCheatPlus.enablePlugin(plgName)){
try { System.out.println("[CompatNoCheatPlus] Ensured that the following plugin is enabled: " + plgName);
NCPHook ncpHook = hook.getNCPHook(); }
if (ncpHook != null){ }
hookDescr = ncpHook.getHookName() + ": " + ncpHook.getHookVersion(); catch (Throwable t){
NCPHookManager.removeHook(ncpHook); logger.severe("[CompatNoCheatPlus] Failed to enable the plugin: " + plgName);
n ++; logger.severe(Utils.toString(t));
} }
} catch (Throwable e) }
{ BukkitScheduler sched = server.getScheduler();
if (hookDescr != null) { for (String plgName : settings.forceEnableLater){
// Some error with removing a hook. if (!oldForceEnableLater.remove(plgName)) oldForceEnableLater.add(plgName);
getLogger().log(Level.WARNING, "Failed to unregister hook: " + hookDescr, e); }
} if (!oldForceEnableLater.isEmpty()){
} System.out.println("[CompatNoCheatPlus] Schedule task to re-enable plugins later...");
} sched.scheduleSyncDelayedTask(this, new Runnable() {
getLogger().info("[cncp] Removed "+n+" registered hooks from NoCheatPlus."); @Override
registeredHooks.clear(); public void run() {
return n; // (Later maybe re-enabling this plugin could be added.)
} // TODO: log levels !
for (String plgName : oldForceEnableLater){
private int registerHooks() { try{
int n = 0; if (disablePlugin(plgName)){
for (Hook hook : registeredHooks){ if (enablePlugin(plgName)) System.out.println("[CompatNoCheatPlus] Re-enabled plugin: " + plgName);
// TODO: try catch else System.out.println("[CompatNoCheatPlus] Could not re-enable plugin: "+plgName);
NCPHook ncpHook = hook.getNCPHook(); }
if (ncpHook == null) continue; else{
NCPHookManager.addHook(hook.getCheckTypes(), ncpHook); System.out.println("[CompatNoCheatPlus] Could not disable plugin (already disabled?): "+plgName);
n ++; }
} }
getLogger().info("[cncp] Added "+n+" registered hooks to NoCheatPlus."); catch (Throwable t){
return n; // TODO: maybe log ?
} }
}
/* (non-Javadoc) }
* @see org.bukkit.plugin.java.JavaPlugin#onCommand(org.bukkit.command.CommandSender, org.bukkit.command.Command, java.lang.String, java.lang.String[]) });
*/ }
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return true;
// Permission has already been checked. }
sendInfo(sender);
return true; @Override
} public void onDisable() {
unregisterNCPHooks(); // Just in case.
/** enabled = false;
* Send general version and hooks info. instance = null; // Set last.
* @param sender getServer().getMessenger().unregisterIncomingPluginChannel(this, "cncp:geyser");
*/ super.onDisable();
private void sendInfo(CommandSender sender) { }
List<String> infos = new LinkedList<String>();
infos.add("---- Version infomation ----"); protected int unregisterNCPHooks() {
// Server // TODO: Clear list here !? Currently done externally...
infos.add("#### Server ####"); int n = 0;
infos.add(getServer().getVersion()); for (Hook hook : registeredHooks) {
// Core plugins (NCP + cncp) String hookDescr = null;
infos.add("#### Core plugins ####"); try {
infos.add(getDescription().getFullName()); NCPHook ncpHook = hook.getNCPHook();
String temp = getOtherVersion("NoCheatPlus"); if (ncpHook != null){
infos.add(temp.isEmpty() ? "NoCheatPlus is missing or not yet enabled." : temp); hookDescr = ncpHook.getHookName() + ": " + ncpHook.getHookVersion();
infos.add("#### Typical plugin dependencies ####"); NCPHookManager.removeHook(ncpHook);
for (String pluginName : new String[]{ n ++;
"mcMMO", "Citizens", "MachinaCraft", "MagicSpells", }
// TODO: extend } catch (Throwable e)
}){ {
temp = getOtherVersion(pluginName); if (hookDescr != null) {
if (!temp.isEmpty()) infos.add(temp); // Some error with removing a hook.
} getLogger().log(Level.WARNING, "Failed to unregister hook: " + hookDescr, e);
// Hooks }
infos.add("#### Registered hooks (cncp) ###"); }
for (final Hook hook : registeredHooks){ }
temp = hook.getHookName() + ": " + hook.getHookVersion(); getLogger().info("[CompatNoCheatPlus] Removed "+n+" registered hooks from NoCheatPlus.");
if (hook instanceof ConfigurableHook){ registeredHooks.clear();
temp += ((ConfigurableHook) hook).isEnabled() ? " (enabled)" : " (disabled)"; return n;
} }
infos.add(temp);
} private int registerHooks() {
// TODO: Registered hooks (ncp) ? int n = 0;
infos.add("#### Registered hooks (ncp) ####"); for (Hook hook : registeredHooks){
for (final NCPHook hook : NCPHookManager.getAllHooks()){ // TODO: try catch
infos.add(hook.getHookName() + ": " + hook.getHookVersion()); NCPHook ncpHook = hook.getNCPHook();
} if (ncpHook == null) continue;
final String[] a = new String[infos.size()]; NCPHookManager.addHook(hook.getCheckTypes(), ncpHook);
infos.toArray(a); n ++;
sender.sendMessage(a); }
} getLogger().info("[CompatNoCheatPlus] Added "+n+" registered hooks to NoCheatPlus.");
return n;
/** }
*
* @param pluginName Empty string or "name: version". @EventHandler(priority = EventPriority.NORMAL)
*/ void onPluginEnable(PluginEnableEvent event){
private String getOtherVersion(String pluginName){ Plugin plugin = event.getPlugin();
Plugin plg = getServer().getPluginManager().getPlugin(pluginName); if (!plugin.getName().equals("NoCheatPlus")) {
if (plg == null) return ""; return;
PluginDescriptionFile pdf = plg.getDescription(); }
return pdf.getFullName(); // Register to remove hooks when NCP is disabling.
} NCPAPIProvider.getNoCheatPlusAPI().addComponent(new IDisableListener(){
@Override
} public void onDisable() {
// Remove all registered cncp hooks:
unregisterNCPHooks();
}
});
if (registeredHooks.isEmpty()) {
return;
}
registerHooks();
}
/* (non-Javadoc)
* @see org.bukkit.plugin.java.JavaPlugin#onCommand(org.bukkit.command.CommandSender, org.bukkit.command.Command, java.lang.String, java.lang.String[])
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Permission has already been checked.
sendInfo(sender);
return true;
}
/**
* Send general version and hooks info.
* @param sender
*/
private void sendInfo(CommandSender sender) {
List<String> infos = new LinkedList<String>();
infos.add("---- Version infomation ----");
// Server
infos.add("#### Server ####");
infos.add(getServer().getVersion());
// Core plugins (NCP + cncp)
infos.add("#### Core plugins ####");
infos.add(getDescription().getFullName());
String temp = getOtherVersion("NoCheatPlus");
infos.add(temp.isEmpty() ? "NoCheatPlus is missing or not yet enabled." : temp);
infos.add("#### Typical plugin dependencies ####");
for (String pluginName : new String[]{
"mcMMO", "Citizens", "MachinaCraft", "MagicSpells", "ViaVersion", "ProtocolSupport", "GravityTubes", "Geyser-Spigot", "floodgate", "CMI", "Geyser-BungeeCord"
}){
temp = getOtherVersion(pluginName);
if (!temp.isEmpty()) infos.add(temp);
}
// Hooks
infos.add("#### Registered hooks (cncp) ###");
for (final Hook hook : registeredHooks){
temp = hook.getHookName() + ": " + hook.getHookVersion();
if (hook instanceof ConfigurableHook){
temp += ((ConfigurableHook) hook).isEnabled() ? " (enabled)" : " (disabled)";
}
infos.add(temp);
}
// TODO: Registered hooks (ncp) ?
infos.add("#### Registered hooks (ncp) ####");
for (final NCPHook hook : NCPHookManager.getAllHooks()){
infos.add(hook.getHookName() + ": " + hook.getHookVersion());
}
final String[] a = new String[infos.size()];
infos.toArray(a);
sender.sendMessage(a);
}
/**
*
* @param pluginName Empty string or "name: version".
*/
private String getOtherVersion(String pluginName){
Plugin plg = getServer().getPluginManager().getPlugin(pluginName);
if (plg == null) return "";
PluginDescriptionFile pdf = plg.getDescription();
return pdf.getFullName();
}
public Settings getSettings() {
return settings;
}
public boolean isBungeeEnabled() {
return bungee;
}
}

View File

@ -0,0 +1,72 @@
package me.asofold.bpl.cncp.bedrock;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.floodgate.api.FloodgateApi;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.players.IPlayerData;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.Settings;
public class BedrockPlayerListener implements Listener, PluginMessageListener {
private Plugin floodgate = Bukkit.getPluginManager().getPlugin("floodgate");
private Plugin geyser = Bukkit.getPluginManager().getPlugin("Geyser-Spigot");
private final Settings settings = CompatNoCheatPlus.getInstance().getSettings();
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
if (floodgate != null && floodgate.isEnabled()) {
if (FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId())) {
processExemption(player);
}
}
else if (geyser != null && geyser.isEnabled()) {
try {
GeyserSession session = GeyserConnector.getInstance().getPlayerByUuid(player.getUniqueId());
if (session != null) processExemption(player);
}
catch (NullPointerException e) {}
}
}
private void processExemption(final Player player) {
final IPlayerData pData = DataManager.getPlayerData(player);
if (pData != null) {
for (CheckType check : settings.extemptChecks) pData.exempt(check);
pData.setBedrockPlayer(true);
}
}
private void processExemption(final String playername) {
final IPlayerData pData = DataManager.getPlayerData(playername);
if (pData != null) {
for (CheckType check : settings.extemptChecks) pData.exempt(check);
pData.setBedrockPlayer(true);
}
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] data) {
if (CompatNoCheatPlus.getInstance().isBungeeEnabled() && channel.equals("cncp:geyser")) {
geyser = null;
floodgate = null;
ByteArrayDataInput input = ByteStreams.newDataInput(data);
String playerName = input.readUTF();
processExemption(playerName);
}
}
}

View File

@ -0,0 +1,86 @@
package me.asofold.bpl.cncp.bungee;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.floodgate.api.FloodgateApi;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.ServerSwitchEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
public class CompatNoCheatPlus extends Plugin implements Listener {
private boolean floodgate;
private boolean geyser;
@Override
public void onEnable() {
geyser = checkGeyser();
floodgate = checkFloodgate();
getLogger().info("Registering listeners");
getProxy().getPluginManager().registerListener(this, this);
getProxy().registerChannel("cncp:geyser");
getLogger().info("cncp Bungee mode with Geyser : " + geyser + ", Floodgate : " + floodgate);
}
@EventHandler
public void onMessageReceive(PluginMessageEvent event) {
if (event.getTag().equalsIgnoreCase("cncp:geyser")) {
// Message sent from client, cancel it
if (event.getSender() instanceof ProxiedPlayer) {
event.setCancelled(true);
}
}
}
private boolean checkFloodgate() {
return ProxyServer.getInstance().getPluginManager().getPlugin("floodgate") != null;
}
private boolean checkGeyser() {
return ProxyServer.getInstance().getPluginManager().getPlugin("Geyser-BungeeCord") != null;
}
@SuppressWarnings("deprecation")
private boolean isBedrockPlayer(ProxiedPlayer player) {
if (floodgate) {
return FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId());
}
if (geyser) {
try {
GeyserSession session = GeyserConnector.getInstance().getPlayerByUuid(player.getUniqueId());
return session != null;
} catch (NullPointerException e) {
return false;
}
}
return false;
}
@EventHandler
public void onChangeServer(ServerSwitchEvent event) {
ProxiedPlayer player = event.getPlayer();
Server server = player.getServer();
if (!isBedrockPlayer(player)) return;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
try {
dataOutputStream.writeUTF(player.getName());
} catch (IOException e) {
e.printStackTrace();
}
getProxy().getScheduler().schedule(this, () -> {
server.sendData("cncp:geyser", outputStream.toByteArray());
}, 1L, TimeUnit.SECONDS);
}
}

View File

@ -1,67 +1,92 @@
package me.asofold.bpl.cncp.config; package me.asofold.bpl.cncp.config;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedHashSet;
import java.util.Set; import java.util.LinkedList;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Bukkit;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import fr.neatmonster.nocheatplus.checks.CheckType;
import me.asofold.bpl.cncp.config.compatlayer.NewConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
public class Settings { import me.asofold.bpl.cncp.config.compatlayer.NewConfig;
public static final int configVersion = 3;
public class Settings {
public static Set<String> preventAddHooks = new HashSet<String>(); public static final int configVersion = 2;
public static CompatConfig getDefaultConfig(){ public Set<String> forceEnableLater = new LinkedHashSet<String>();
CompatConfig cfg = new NewConfig(null); public Set<String> loadPlugins = new LinkedHashSet<String>();
cfg.set("plugins.force-enable-later", new LinkedList<String>()); // ConfigUtil.asList(new String[]{ "NoCheatPlus" })); public Set<CheckType> extemptChecks = new LinkedHashSet<CheckType>();
cfg.set("plugins.ensure-enable", new LinkedList<String>()); // ConfigUtil.asList(new String[]{ "WorldGuard" }));
cfg.set("hooks.prevent-add", new LinkedList<String>()); public static Set<String> preventAddHooks = new HashSet<String>();
cfg.set("configversion", configVersion);
return cfg; public static CompatConfig getDefaultConfig(){
} CompatConfig cfg = new NewConfig(null);
cfg.set("plugins.force-enable-later", new LinkedList<String>()); // ConfigUtil.asList(new String[]{ "NoCheatPlus" }));
public static boolean addDefaults(CompatConfig cfg){ cfg.set("plugins.ensure-enable", new LinkedList<String>()); // ConfigUtil.asList(new String[]{ "WorldGuard" }));
boolean changed = false; cfg.set("plugins.bedrock-extempt-checks", ConfigUtil.asList(new String[]{
if (cfg.getInt("configversion", 0) == 0){ "ALL",
cfg.remove("plugins"); "BLOCKINTERACT_VISIBLE",
cfg.set("configversion", configVersion); "BLOCKINTERACT_DIRECTION",
changed = true; "BLOCKINTERACT_REACH",
} "BLOCKBREAK_DIRECTION",
if (cfg.getInt("configversion", 0) <= 1){ "BLOCKBREAK_NOSWING",
if (cfg.getDouble("hooks.set-speed.fly-speed", 0.1) != 0.1){ "BLOCKBREAK_REACH",
changed = true; "BLOCKPLACE_NOSWING",
cfg.set("hooks.set-speed.fly-speed", 0.1); "BLOCKPLACE_DIRECTION",
Bukkit.getLogger().warning("[cncp] Reset fly-speed for the set-speed hook to 0.1 (default) as a safety measure."); "BLOCKPLACE_REACH",
} "BLOCKPLACE_SCAFFOLD",
if (cfg.getDouble("hooks.set-speed.walk-speed", 0.2) != 0.2){ "FIGHT_DIRECTION",
changed = true; }));
cfg.set("hooks.set-speed.walk-speed", 0.2); cfg.set("hooks.prevent-add", new LinkedList<String>());
Bukkit.getLogger().warning("[cncp] Reset walk-speed for the set-speed hook to 0.2 (default) as a safety measure."); cfg.set("configversion", configVersion);
} return cfg;
} }
if (ConfigUtil.forceDefaults(getDefaultConfig(), cfg)) changed = true;
if (cfg.getInt("configversion", 0) != configVersion){ public static boolean addDefaults(CompatConfig cfg){
cfg.set("configversion", configVersion); boolean changed = false;
changed = true; if (cfg.getInt("configversion", 0) == 0){
} cfg.remove("plugins");
return changed; cfg.set("configversion", configVersion);
} changed = true;
}
public boolean fromConfig(CompatConfig cfg){ if (cfg.getInt("configversion", 0) <= 1){
// Settings ref = new Settings(); if (cfg.getDouble("hooks.set-speed.fly-speed", 0.1) != 0.1){
changed = true;
cfg.set("hooks.set-speed.fly-speed", 0.1);
// General Bukkit.getLogger().warning("[CompatNoCheatPlus] Reset fly-speed for the set-speed hook to 0.1 (default) as a safety measure.");
ConfigUtil.readStringSetFromList(cfg, "hooks.prevent-add", preventAddHooks, true, true, false); }
return true; if (cfg.getDouble("hooks.set-speed.walk-speed", 0.2) != 0.2){
} changed = true;
cfg.set("hooks.set-speed.walk-speed", 0.2);
public void clear() { Bukkit.getLogger().warning("[CompatNoCheatPlus] Reset walk-speed for the set-speed hook to 0.2 (default) as a safety measure.");
// TODO: clear something !? }
} }
if (ConfigUtil.forceDefaults(getDefaultConfig(), cfg)) changed = true;
} if (cfg.getInt("configversion", 0) != configVersion){
cfg.set("configversion", configVersion);
changed = true;
}
return changed;
}
public boolean fromConfig(CompatConfig cfg){
// Settings ref = new Settings();
// plugins to force enabling after this plugin.
ConfigUtil.readStringSetFromList(cfg, "plugins.force-enable-later", forceEnableLater, true, true, false);
ConfigUtil.readStringSetFromList(cfg, "plugins.ensure-enable", loadPlugins, true, true, false);
ConfigUtil.readCheckTypeSetFromList(cfg, "plugins.bedrock-extempt-checks", extemptChecks, true, true, true);
// General
ConfigUtil.readStringSetFromList(cfg, "hooks.prevent-add", preventAddHooks, true, true, false);
return true;
}
public void clear() {
forceEnableLater.clear();
}
}

View File

@ -1,213 +1,213 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Some generic checks and stuff using getString, hasEntry, getStringList, ... * Some generic checks and stuff using getString, hasEntry, getStringList, ...
* @author mc_dev * @author mc_dev
* *
*/ */
public abstract class AbstractConfig implements CompatConfig { public abstract class AbstractConfig implements CompatConfig {
protected char sep = '.'; protected char sep = '.';
@Override @Override
public Boolean getBoolean(String path, Boolean defaultValue) { public Boolean getBoolean(String path, Boolean defaultValue) {
String val = getString(path, null); String val = getString(path, null);
if ( val == null ) return defaultValue; if ( val == null ) return defaultValue;
try{ try{
// return Boolean.parseBoolean(val); // return Boolean.parseBoolean(val);
String t = val.trim().toLowerCase(); String t = val.trim().toLowerCase();
if ( t.equals("true")) return true; if ( t.equals("true")) return true;
else if ( t.equals("false")) return false; else if ( t.equals("false")) return false;
else return defaultValue; else return defaultValue;
} catch( NumberFormatException exc){ } catch( NumberFormatException exc){
return defaultValue; return defaultValue;
} }
} }
@Override @Override
public Double getDouble(String path, Double defaultValue) { public Double getDouble(String path, Double defaultValue) {
String val = getString(path, null); String val = getString(path, null);
if ( val == null ) return defaultValue; if ( val == null ) return defaultValue;
try{ try{
return Double.parseDouble(val); return Double.parseDouble(val);
} catch( NumberFormatException exc){ } catch( NumberFormatException exc){
return defaultValue; return defaultValue;
} }
} }
@Override @Override
public Long getLong(String path, Long defaultValue) { public Long getLong(String path, Long defaultValue) {
String val = getString(path, null); String val = getString(path, null);
if ( val == null ) return defaultValue; if ( val == null ) return defaultValue;
try{ try{
return Long.parseLong(val); return Long.parseLong(val);
} catch( NumberFormatException exc){ } catch( NumberFormatException exc){
return defaultValue; return defaultValue;
} }
} }
@Override @Override
public Integer getInt(String path, Integer defaultValue) { public Integer getInt(String path, Integer defaultValue) {
String val = getString(path, null); String val = getString(path, null);
if ( val == null ) return defaultValue; if ( val == null ) return defaultValue;
try{ try{
return Integer.parseInt(val); return Integer.parseInt(val);
} catch( NumberFormatException exc){ } catch( NumberFormatException exc){
return defaultValue; return defaultValue;
} }
} }
@Override @Override
public List<Integer> getIntList(String path, List<Integer> defaultValue){ public List<Integer> getIntList(String path, List<Integer> defaultValue){
if ( !hasEntry(path) ) return defaultValue; if ( !hasEntry(path) ) return defaultValue;
List<String> strings = getStringList(path, null); List<String> strings = getStringList(path, null);
if ( strings == null ) return defaultValue; if ( strings == null ) return defaultValue;
List<Integer> out = new LinkedList<Integer>(); List<Integer> out = new LinkedList<Integer>();
for ( String s : strings){ for ( String s : strings){
try{ try{
out.add(Integer.parseInt(s)); out.add(Integer.parseInt(s));
} catch(NumberFormatException exc){ } catch(NumberFormatException exc){
// ignore // ignore
} }
} }
return out; return out;
} }
@Override @Override
public List<Double> getDoubleList(String path, List<Double> defaultValue) { public List<Double> getDoubleList(String path, List<Double> defaultValue) {
if ( !hasEntry(path) ) return defaultValue; if ( !hasEntry(path) ) return defaultValue;
List<String> strings = getStringList(path, null); List<String> strings = getStringList(path, null);
if ( strings == null ) return defaultValue; if ( strings == null ) return defaultValue;
List<Double> out = new LinkedList<Double>(); List<Double> out = new LinkedList<Double>();
for ( String s : strings){ for ( String s : strings){
try{ try{
out.add(Double.parseDouble(s)); out.add(Double.parseDouble(s));
} catch(NumberFormatException exc){ } catch(NumberFormatException exc){
// ignore // ignore
} }
} }
return out; return out;
} }
@Override @Override
public Set<String> getStringKeys(String path, boolean deep) { public Set<String> getStringKeys(String path, boolean deep) {
if (deep) return getStringKeysDeep(path); if (deep) return getStringKeysDeep(path);
Set<String> keys = new HashSet<String>(); Set<String> keys = new HashSet<String>();
keys.addAll(getStringKeys(path)); keys.addAll(getStringKeys(path));
return keys; return keys;
} }
@Override @Override
public Set<String> getStringKeysDeep(String path) { public Set<String> getStringKeysDeep(String path) {
// NOTE: pretty inefficient, but aimed at seldomly read sections. // NOTE: pretty inefficient, but aimed at seldomly read sections.
Map<String, Object> values = getValuesDeep(); Map<String, Object> values = getValuesDeep();
Set<String> out = new HashSet<String>(); Set<String> out = new HashSet<String>();
final int len = path.length(); final int len = path.length();
for (String key : values.keySet()){ for (String key : values.keySet()){
if (!key.startsWith(path)) continue; if (!key.startsWith(path)) continue;
else if (key.length() == len) continue; else if (key.length() == len) continue;
else if (key.charAt(len) == sep) out.add(key); else if (key.charAt(len) == sep) out.add(key);
} }
return out; return out;
} }
@Override @Override
public Object get(String path, Object defaultValue) { public Object get(String path, Object defaultValue) {
return getProperty(path, defaultValue); return getProperty(path, defaultValue);
} }
@Override @Override
public void set(String path, Object value) { public void set(String path, Object value) {
setProperty(path, value); setProperty(path, value);
} }
@Override @Override
public void remove(String path) { public void remove(String path) {
removeProperty(path); removeProperty(path);
} }
@Override @Override
public Boolean getBoolean(String path) { public Boolean getBoolean(String path) {
return getBoolean(path, null); return getBoolean(path, null);
} }
@Override @Override
public Double getDouble(String path) { public Double getDouble(String path) {
return getDouble(path, null); return getDouble(path, null);
} }
@Override @Override
public List<Double> getDoubleList(String path) { public List<Double> getDoubleList(String path) {
return getDoubleList(path, null); return getDoubleList(path, null);
} }
@Override @Override
public Integer getInt(String path) { public Integer getInt(String path) {
return getInt(path, null); return getInt(path, null);
} }
@Override @Override
public List<Integer> getIntList(String path) { public List<Integer> getIntList(String path) {
return getIntList(path, null); return getIntList(path, null);
} }
@Override @Override
public Integer getInteger(String path) { public Integer getInteger(String path) {
return getInt(path, null); return getInt(path, null);
} }
@Override @Override
public List<Integer> getIntegerList(String path) { public List<Integer> getIntegerList(String path) {
return getIntList(path, null); return getIntList(path, null);
} }
@Override @Override
public String getString(String path) { public String getString(String path) {
return getString(path, null); return getString(path, null);
} }
@Override @Override
public List<String> getStringList(String path) { public List<String> getStringList(String path) {
return getStringList(path, null); return getStringList(path, null);
} }
@Override @Override
public Object get(String path) { public Object get(String path) {
return getProperty(path, null); return getProperty(path, null);
} }
@Override @Override
public Object getProperty(String path) { public Object getProperty(String path) {
return getProperty(path, null); return getProperty(path, null);
} }
@Override @Override
public boolean contains(String path) { public boolean contains(String path) {
return hasEntry(path); return hasEntry(path);
} }
@Override @Override
public Integer getInteger(String path, Integer defaultValue) { public Integer getInteger(String path, Integer defaultValue) {
return getInt(path, defaultValue); return getInt(path, defaultValue);
} }
@Override @Override
public List<Integer> getIntegerList(String path, List<Integer> defaultValue) { public List<Integer> getIntegerList(String path, List<Integer> defaultValue) {
return getIntList(path, defaultValue); return getIntList(path, defaultValue);
} }
@Override @Override
public Long getLong(String path) { public Long getLong(String path) {
return getLong(path, null); return getLong(path, null);
} }
} }

View File

@ -1,237 +1,237 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.io.File; import java.io.File;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationOptions; import org.bukkit.configuration.ConfigurationOptions;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration; import org.bukkit.configuration.MemoryConfiguration;
public abstract class AbstractNewConfig extends AbstractConfig { public abstract class AbstractNewConfig extends AbstractConfig {
File file = null; File file = null;
MemoryConfiguration config = null; MemoryConfiguration config = null;
public AbstractNewConfig(File file){ public AbstractNewConfig(File file){
setFile(file); setFile(file);
} }
public void setFile(File file) { public void setFile(File file) {
this.file = file; this.file = file;
this.config = new MemoryConfiguration(); this.config = new MemoryConfiguration();
setOptions(config); setOptions(config);
} }
@Override @Override
public boolean hasEntry(String path) { public boolean hasEntry(String path) {
return config.contains(path) || (config.get(path) != null); return config.contains(path) || (config.get(path) != null);
} }
@Override @Override
public String getString(String path, String defaultValue) { public String getString(String path, String defaultValue) {
if (!hasEntry(path)) return defaultValue; if (!hasEntry(path)) return defaultValue;
return config.getString(path, defaultValue); return config.getString(path, defaultValue);
} }
@Override @Override
public List<String> getStringKeys(String path) { public List<String> getStringKeys(String path) {
// TODO policy: only strings or all keys as strings ? // TODO policy: only strings or all keys as strings ?
List<String> out = new LinkedList<String>(); List<String> out = new LinkedList<String>();
List<Object> keys = getKeys(path); List<Object> keys = getKeys(path);
if ( keys == null ) return out; if ( keys == null ) return out;
for ( Object obj : keys){ for ( Object obj : keys){
if ( obj instanceof String ) out.add((String) obj); if ( obj instanceof String ) out.add((String) obj);
else{ else{
try{ try{
out.add(obj.toString()); out.add(obj.toString());
} catch ( Throwable t){ } catch ( Throwable t){
// ignore. // ignore.
} }
} }
} }
return out; return out;
} }
@Override @Override
public List<Object> getKeys(String path) { public List<Object> getKeys(String path) {
List<Object> out = new LinkedList<Object>(); List<Object> out = new LinkedList<Object>();
Set<String> keys; Set<String> keys;
if ( path == null) keys = config.getKeys(false); if ( path == null) keys = config.getKeys(false);
else{ else{
ConfigurationSection sec = config.getConfigurationSection(path); ConfigurationSection sec = config.getConfigurationSection(path);
if (sec == null) return out; if (sec == null) return out;
keys = sec.getKeys(false); keys = sec.getKeys(false);
} }
if ( keys == null) return out; if ( keys == null) return out;
out.addAll(keys); out.addAll(keys);
return out; return out;
} }
@Override @Override
public List<Object> getKeys() { public List<Object> getKeys() {
return getKeys(null); return getKeys(null);
} }
@Override @Override
public Object getProperty(String path, Object defaultValue) { public Object getProperty(String path, Object defaultValue) {
Object obj = config.get(path); Object obj = config.get(path);
if ( obj == null ) return defaultValue; if ( obj == null ) return defaultValue;
else return obj; else return obj;
} }
@Override @Override
public List<String> getStringKeys() { public List<String> getStringKeys() {
return getStringKeys(null); return getStringKeys(null);
} }
@Override @Override
public void setProperty(String path, Object obj) { public void setProperty(String path, Object obj) {
config.set(path, obj); config.set(path, obj);
} }
@Override @Override
public List<String> getStringList(String path, List<String> defaultValue) { public List<String> getStringList(String path, List<String> defaultValue) {
if ( !hasEntry(path)) return defaultValue; if ( !hasEntry(path)) return defaultValue;
List<String> out = new LinkedList<String>(); List<String> out = new LinkedList<String>();
List<String> entries = config.getStringList(path); List<String> entries = config.getStringList(path);
if ( entries == null ) return defaultValue; if ( entries == null ) return defaultValue;
for ( String entry : entries){ for ( String entry : entries){
if ( entry instanceof String) out.add(entry); if ( entry instanceof String) out.add(entry);
else{ else{
try{ try{
out.add(entry.toString()); out.add(entry.toString());
} catch (Throwable t){ } catch (Throwable t){
// ignore // ignore
} }
} }
} }
return out; return out;
} }
@Override @Override
public void removeProperty(String path) { public void removeProperty(String path) {
if (path.startsWith(".")) path = path.substring(1); if (path.startsWith(".")) path = path.substring(1);
// VERY EXPENSIVE // VERY EXPENSIVE
MemoryConfiguration temp = new MemoryConfiguration(); MemoryConfiguration temp = new MemoryConfiguration();
setOptions(temp); setOptions(temp);
Map<String, Object> values = config.getValues(true); Map<String, Object> values = config.getValues(true);
if (values.containsKey(path)) values.remove(path); if (values.containsKey(path)) values.remove(path);
else{ else{
final String altPath = "."+path; final String altPath = "."+path;
if (values.containsKey(altPath)) values.remove(altPath); if (values.containsKey(altPath)) values.remove(altPath);
} }
for ( String _p : values.keySet()){ for ( String _p : values.keySet()){
Object v = values.get(_p); Object v = values.get(_p);
if (v == null) continue; if (v == null) continue;
else if (v instanceof ConfigurationSection) continue; else if (v instanceof ConfigurationSection) continue;
String p; String p;
if (_p.startsWith(".")) p = _p.substring(1); if (_p.startsWith(".")) p = _p.substring(1);
else p = _p; else p = _p;
if (p.startsWith(path)) continue; if (p.startsWith(path)) continue;
temp.set(p, v); temp.set(p, v);
} }
config = temp; config = temp;
} }
@Override @Override
public Boolean getBoolean(String path, Boolean defaultValue) { public Boolean getBoolean(String path, Boolean defaultValue) {
if (!config.contains(path)) return defaultValue; if (!config.contains(path)) return defaultValue;
String val = config.getString(path, null); String val = config.getString(path, null);
if (val != null){ if (val != null){
if (val.equalsIgnoreCase("true")) return true; if (val.equalsIgnoreCase("true")) return true;
else if (val.equalsIgnoreCase("false")) return false; else if (val.equalsIgnoreCase("false")) return false;
else return defaultValue; else return defaultValue;
} }
Boolean res = defaultValue; Boolean res = defaultValue;
if ( val == null ){ if ( val == null ){
if ( defaultValue == null) defaultValue = false; if ( defaultValue == null) defaultValue = false;
res = config.getBoolean(path, defaultValue); res = config.getBoolean(path, defaultValue);
} }
return res; return res;
} }
@Override @Override
public Double getDouble(String path, Double defaultValue) { public Double getDouble(String path, Double defaultValue) {
if (!config.contains(path)) return defaultValue; if (!config.contains(path)) return defaultValue;
Double res = super.getDouble(path, null); Double res = super.getDouble(path, null);
if ( res == null ) res = config.getDouble(path, ConfigUtil.canaryDouble); if ( res == null ) res = config.getDouble(path, ConfigUtil.canaryDouble);
if ( res == ConfigUtil.canaryDouble) return defaultValue; if ( res == ConfigUtil.canaryDouble) return defaultValue;
return res; return res;
} }
@Override @Override
public Long getLong(String path, Long defaultValue) { public Long getLong(String path, Long defaultValue) {
if (!config.contains(path)) return defaultValue; if (!config.contains(path)) return defaultValue;
Long res = super.getLong(path, null); Long res = super.getLong(path, null);
if ( res == null ) res = config.getLong(path, ConfigUtil.canaryLong); if ( res == null ) res = config.getLong(path, ConfigUtil.canaryLong);
if ( res == ConfigUtil.canaryLong) return defaultValue; if ( res == ConfigUtil.canaryLong) return defaultValue;
return res; return res;
} }
@Override @Override
public Integer getInt(String path, Integer defaultValue) { public Integer getInt(String path, Integer defaultValue) {
if (!config.contains(path)) return defaultValue; if (!config.contains(path)) return defaultValue;
Integer res = super.getInt(path, null); Integer res = super.getInt(path, null);
if ( res == null ) res = config.getInt(path, ConfigUtil.canaryInt); if ( res == null ) res = config.getInt(path, ConfigUtil.canaryInt);
if ( res == ConfigUtil.canaryInt) return defaultValue; if ( res == ConfigUtil.canaryInt) return defaultValue;
return res; return res;
} }
@Override @Override
public List<Integer> getIntList(String path, List<Integer> defaultValue) { public List<Integer> getIntList(String path, List<Integer> defaultValue) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return super.getIntList(path, defaultValue); return super.getIntList(path, defaultValue);
} }
@Override @Override
public List<Double> getDoubleList(String path, List<Double> defaultValue) { public List<Double> getDoubleList(String path, List<Double> defaultValue) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return super.getDoubleList(path, defaultValue); return super.getDoubleList(path, defaultValue);
} }
void addAll(Configuration source, Configuration target){ void addAll(Configuration source, Configuration target){
Map<String, Object> all = source.getValues(true); Map<String, Object> all = source.getValues(true);
for ( String path: all.keySet()){ for ( String path: all.keySet()){
target.set(path, source.get(path)); target.set(path, source.get(path));
} }
} }
void setOptions(Configuration cfg){ void setOptions(Configuration cfg){
ConfigurationOptions opt = cfg.options(); ConfigurationOptions opt = cfg.options();
opt.pathSeparator(this.sep); opt.pathSeparator(this.sep);
//opt.copyDefaults(true); //opt.copyDefaults(true);
} }
@Override @Override
public boolean setPathSeparatorChar(char sep) { public boolean setPathSeparatorChar(char sep) {
this.sep = sep; this.sep = sep;
setOptions(config); setOptions(config);
return true; return true;
} }
} }

View File

@ -1,155 +1,155 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* CONVENTIONS: * CONVENTIONS:
* - Return strings if objects can be made strings. * - Return strings if objects can be made strings.
* - No exceptions, rather leave elements out of lists. * - No exceptions, rather leave elements out of lists.
* - Lists of length 0 and null can not always be distinguished (?->extra safe wrapper ?) * - Lists of length 0 and null can not always be distinguished (?->extra safe wrapper ?)
* - All contents are treated alike, even if given as a string (!): true and 'true', 1 and '1' * - All contents are treated alike, even if given as a string (!): true and 'true', 1 and '1'
* @author mc_dev * @author mc_dev
* *
*/ */
public interface CompatConfig { public interface CompatConfig {
// Boolean // Boolean
/** /**
* Only accepts true and false , 'true' and 'false'. * Only accepts true and false , 'true' and 'false'.
* @param path * @param path
* @param defaultValue * @param defaultValue
* @return * @return
*/ */
public Boolean getBoolean(String path, Boolean defaultValue); public Boolean getBoolean(String path, Boolean defaultValue);
public Boolean getBoolean(String path); public Boolean getBoolean(String path);
// Long // Long
public Long getLong(String path); public Long getLong(String path);
public Long getLong(String path, Long defaultValue); public Long getLong(String path, Long defaultValue);
// Double // Double
public Double getDouble(String path); public Double getDouble(String path);
public Double getDouble(String path, Double defaultValue); public Double getDouble(String path, Double defaultValue);
public List<Double> getDoubleList(String path); public List<Double> getDoubleList(String path);
public List<Double> getDoubleList(String path , List<Double> defaultValue); public List<Double> getDoubleList(String path , List<Double> defaultValue);
// Integer (abbreviation) // Integer (abbreviation)
public Integer getInt(String path); public Integer getInt(String path);
public Integer getInt(String path, Integer defaultValue); public Integer getInt(String path, Integer defaultValue);
public List<Integer> getIntList(String path); public List<Integer> getIntList(String path);
public List<Integer> getIntList(String path, List<Integer> defaultValue); public List<Integer> getIntList(String path, List<Integer> defaultValue);
// Integer (full name) // Integer (full name)
public Integer getInteger(String path); public Integer getInteger(String path);
public Integer getInteger(String path, Integer defaultValue); public Integer getInteger(String path, Integer defaultValue);
public List<Integer> getIntegerList(String path); public List<Integer> getIntegerList(String path);
public List<Integer> getIntegerList(String path, List<Integer> defaultValue); public List<Integer> getIntegerList(String path, List<Integer> defaultValue);
// String // String
public String getString(String path); public String getString(String path);
public String getString(String path, String defaultValue); public String getString(String path, String defaultValue);
public List<String> getStringList(String path); public List<String> getStringList(String path);
public List<String> getStringList(String path, List<String> defaultValue); public List<String> getStringList(String path, List<String> defaultValue);
// Generic methods: // Generic methods:
public Object get(String path); public Object get(String path);
public Object get(String path, Object defaultValue); public Object get(String path, Object defaultValue);
public Object getProperty(String path); public Object getProperty(String path);
public Object getProperty(String path, Object defaultValue); public Object getProperty(String path, Object defaultValue);
public void set(String path, Object value); public void set(String path, Object value);
public void setProperty(String path, Object value); public void setProperty(String path, Object value);
/** /**
* Remove a path (would also remove sub sections, unless for path naming problems). * Remove a path (would also remove sub sections, unless for path naming problems).
* @param path * @param path
*/ */
public void remove(String path); public void remove(String path);
/** /**
* Works same as remove(path): removes properties and sections alike. * Works same as remove(path): removes properties and sections alike.
* @param path * @param path
*/ */
public void removeProperty(String path); public void removeProperty(String path);
// Contains/has // Contains/has
public boolean hasEntry(String path); public boolean hasEntry(String path);
public boolean contains(String path); public boolean contains(String path);
// Keys (Object): [possibly deprecated] // Keys (Object): [possibly deprecated]
/** /**
* @deprecated Seems not to be supported anymore by new configuration, use getStringKeys(String path); * @deprecated Seems not to be supported anymore by new configuration, use getStringKeys(String path);
* @param path * @param path
* @return * @return
*/ */
public List<Object> getKeys(String path); public List<Object> getKeys(String path);
/** /**
* @deprecated Seems not to be supported anymore by new configuration, use getStringKeys(); * @deprecated Seems not to be supported anymore by new configuration, use getStringKeys();
* @return * @return
*/ */
public List<Object> getKeys(); public List<Object> getKeys();
// Keys (String): // Keys (String):
/** /**
* *
* @return never null * @return never null
*/ */
public List<String> getStringKeys(); public List<String> getStringKeys();
public List<String> getStringKeys(String path); public List<String> getStringKeys(String path);
/** /**
* Get all keys from the section (deep or shallow). * Get all keys from the section (deep or shallow).
* @param path * @param path
* @param deep * @param deep
* @return Never null. * @return Never null.
*/ */
public Set<String> getStringKeys(String path, boolean deep); public Set<String> getStringKeys(String path, boolean deep);
/** /**
* convenience method. * convenience method.
* @param path * @param path
* @return never null * @return never null
* *
*/ */
public Set<String> getStringKeysDeep(String path); public Set<String> getStringKeysDeep(String path);
// Values: // Values:
/** /**
* Equivalent to new config: values(true) * Equivalent to new config: values(true)
* @return * @return
*/ */
public Map<String, Object> getValuesDeep(); public Map<String, Object> getValuesDeep();
// Technical / IO: // Technical / IO:
/** /**
* False if not supported. * False if not supported.
* @param sep * @param sep
* @return * @return
*/ */
public boolean setPathSeparatorChar(char sep); public boolean setPathSeparatorChar(char sep);
public void load(); public void load();
public boolean save(); public boolean save();
/** /**
* Clear all contents. * Clear all contents.
*/ */
public void clear(); public void clear();
/** /**
* Return a YAML-String representation of the contents, null if not supported. * Return a YAML-String representation of the contents, null if not supported.
* @return null if not supported. * @return null if not supported.
*/ */
public String getYAMLString(); public String getYAMLString();
/** /**
* Add all entries from the YAML-String representation to the configuration, forget or clear all previous entries. * Add all entries from the YAML-String representation to the configuration, forget or clear all previous entries.
* @return * @return
*/ */
public boolean fromYamlString(String input); public boolean fromYamlString(String input);
} }

View File

@ -1,56 +1,56 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.io.File; import java.io.File;
public class CompatConfigFactory { public class CompatConfigFactory {
public static final String version = "0.8.1"; public static final String version = "0.8.1";
/** /**
* Attempt to get a working file configuration.<br> * Attempt to get a working file configuration.<br>
* This is not fit for fast processing.<br> * This is not fit for fast processing.<br>
* Use getDBConfig to use this with a database.<br> * Use getDBConfig to use this with a database.<br>
* @param file May be null (then memory is used). * @param file May be null (then memory is used).
* @return null if fails. * @return null if fails.
*/ */
public static final CompatConfig getConfig(File file){ public static final CompatConfig getConfig(File file){
CompatConfig out = null; CompatConfig out = null;
// TODO: add more (latest API) // TODO: add more (latest API)
// try{ // try{
// return new OldConfig(file); // return new OldConfig(file);
// } catch (Throwable t){ // } catch (Throwable t){
// } // }
try{ try{
return new NewConfig(file); return new NewConfig(file);
} catch (Throwable t){ } catch (Throwable t){
} }
return out; return out;
} }
// public static final CompatConfig getOldConfig(File file){ // public static final CompatConfig getOldConfig(File file){
// return new OldConfig(file); // return new OldConfig(file);
// } // }
public static final CompatConfig getNewConfig(File file){ public static final CompatConfig getNewConfig(File file){
return new NewConfig(file); return new NewConfig(file);
} }
// /** // /**
// * Get a ebeans based database config (!). // * Get a ebeans based database config (!).
// * @param file // * @param file
// * @return // * @return
// */ // */
// public static final CompatConfig getDBConfig(EbeanServer server, String dbKey){ // public static final CompatConfig getDBConfig(EbeanServer server, String dbKey){
// try{ // try{
// return new SnakeDBConfig(server, dbKey); // return new SnakeDBConfig(server, dbKey);
// } catch (Throwable t){ // } catch (Throwable t){
// //
// } // }
// return new DBConfig(server, dbKey); // return new DBConfig(server, dbKey);
// } // }
// TODO: add setup helpers (!) // TODO: add setup helpers (!)
// TODO: add conversion helpers (!) // TODO: add conversion helpers (!)
} }

View File

@ -1,196 +1,224 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class ConfigUtil { import fr.neatmonster.nocheatplus.checks.CheckType;
public static final int canaryInt = Integer.MIN_VALUE +7; public class ConfigUtil {
public static final long canaryLong = Long.MIN_VALUE + 7L;
public static final double canaryDouble = -Double.MAX_VALUE + 1.2593; public static final int canaryInt = Integer.MIN_VALUE +7;
public static final long canaryLong = Long.MIN_VALUE + 7L;
public static String stringPath( String path){ public static final double canaryDouble = -Double.MAX_VALUE + 1.2593;
return stringPath(path, '.');
} public static String stringPath( String path){
return stringPath(path, '.');
public static String stringPath( String path , char sep ){ }
String useSep = (sep=='.')?"\\.":""+sep;
String[] split = path.split(useSep); public static String stringPath( String path , char sep ){
StringBuilder builder = new StringBuilder(); String useSep = (sep=='.')?"\\.":""+sep;
builder.append(stringPart(split[0])); String[] split = path.split(useSep);
for (int i = 1; i<split.length; i++){ StringBuilder builder = new StringBuilder();
builder.append(sep+stringPart(split[i])); builder.append(stringPart(split[0]));
} for (int i = 1; i<split.length; i++){
return builder.toString(); builder.append(sep+stringPart(split[i]));
} }
return builder.toString();
/** }
* Aimed at numbers in paths.
* @param cfg /**
* @param path * Aimed at numbers in paths.
* @return * @param cfg
*/ * @param path
public static String bestPath(CompatConfig cfg, String path){ * @return
return bestPath(cfg, path, '.'); */
} public static String bestPath(CompatConfig cfg, String path){
return bestPath(cfg, path, '.');
}
/**
* Aimed at numbers in paths.
* @param cfg /**
* @param path * Aimed at numbers in paths.
* @param sep * @param cfg
* @return * @param path
*/ * @param sep
public static String bestPath(CompatConfig cfg, String path, char sep){ * @return
String useSep = (sep=='.')?"\\.":""+sep; */
String[] split = path.split(useSep); public static String bestPath(CompatConfig cfg, String path, char sep){
String res; String useSep = (sep=='.')?"\\.":""+sep;
if (cfg.hasEntry(split[0]) )res = split[0]; String[] split = path.split(useSep);
else{ String res;
res = stringPart(split[0]); if (cfg.hasEntry(split[0]) )res = split[0];
if ( !cfg.hasEntry(res)) return path; else{
} res = stringPart(split[0]);
for (int i = 1; i<split.length; i++){ if ( !cfg.hasEntry(res)) return path;
if (cfg.hasEntry(res+sep+split[i]) ) res += sep+split[i]; }
else{ for (int i = 1; i<split.length; i++){
res += sep+stringPart(split[i]); if (cfg.hasEntry(res+sep+split[i]) ) res += sep+split[i];
if ( !cfg.hasEntry(res)) return path; else{
} res += sep+stringPart(split[i]);
} if ( !cfg.hasEntry(res)) return path;
return res; }
} }
return res;
public static String stringPart(String input){ }
try{
Double.parseDouble(input); public static String stringPart(String input){
return "'"+input+"'"; try{
} catch (NumberFormatException e){ Double.parseDouble(input);
} return "'"+input+"'";
try{ } catch (NumberFormatException e){
Long.parseLong(input); }
return "'"+input+"'"; try{
} catch (NumberFormatException e){ Long.parseLong(input);
} return "'"+input+"'";
try{ } catch (NumberFormatException e){
Integer.parseInt(input); }
return "'"+input+"'"; try{
} catch (NumberFormatException e){ Integer.parseInt(input);
} return "'"+input+"'";
return input; } catch (NumberFormatException e){
} }
return input;
public static boolean forceDefaults(CompatConfig defaults, CompatConfig config){ }
Map<String ,Object> all = defaults.getValuesDeep();
boolean changed = false; public static boolean forceDefaults(CompatConfig defaults, CompatConfig config){
for ( String path : all.keySet()){ Map<String ,Object> all = defaults.getValuesDeep();
if ( !config.hasEntry(path)){ boolean changed = false;
config.setProperty(path, defaults.getProperty(path, null)); for ( String path : all.keySet()){
changed = true; if ( !config.hasEntry(path)){
} config.setProperty(path, defaults.getProperty(path, null));
} changed = true;
return changed; }
} }
return changed;
/** }
* Add StringList entries to a set.
* @param cfg /**
* @param path * Add StringList entries to a set.
* @param set * @param cfg
* @param clear If to clear the set. * @param path
* @param trim * @param set
* @param lowerCase * @param clear If to clear the set.
*/ * @param trim
public static void readStringSetFromList(CompatConfig cfg, String path, Set<String> set, boolean clear, boolean trim, boolean lowerCase){ * @param lowerCase
if (clear) set.clear(); */
List<String> tempList = cfg.getStringList(path , null); public static void readStringSetFromList(CompatConfig cfg, String path, Set<String> set, boolean clear, boolean trim, boolean lowerCase){
if (tempList != null){ if (clear) set.clear();
for (String entry : tempList){ List<String> tempList = cfg.getStringList(path , null);
if (trim) entry = entry.trim(); if (tempList != null){
if (lowerCase) entry = entry.toLowerCase(); for (String entry : tempList){
set.add(entry); if (trim) entry = entry.trim();
} if (lowerCase) entry = entry.toLowerCase();
} set.add(entry);
} }
}
/** }
* Return an ArrayList.
* @param input /**
* @return * Add StringList entries to a set.
*/ * @param cfg
public static final <T> List<T> asList(final T[] input){ * @param path
final List<T> out = new ArrayList<T>(input.length); * @param set
for (int i = 0; i < input.length; i++){ * @param clear If to clear the set.
out.add(input[i]); * @param trim
} * @param upperCase
return out; */
} public static void readCheckTypeSetFromList(CompatConfig cfg, String path, Set<CheckType> set, boolean clear, boolean trim, boolean upperCase){
if (clear) set.clear();
/** List<String> tempList = cfg.getStringList(path , null);
* Return an ArrayList. if (tempList != null){
* @param input for (String entry : tempList) {
* @return if (trim) entry = entry.trim();
*/ if (upperCase) entry = entry.toUpperCase();
public static final List<Integer> asList(final int[] input){ try {
final List<Integer> out = new ArrayList<Integer>(input.length); final CheckType checkType = CheckType.valueOf(entry);
for (int i = 0; i < input.length; i++){ set.add(checkType);
out.add(input[i]); } catch (Exception e) {
} System.out.println("[CompatNoCheatPlus] Unknown check " + entry + ". Skipping.");
return out; }
} }
}
/** }
* Return an ArrayList.
* @param input /**
* @return * Return an ArrayList.
*/ * @param input
public static final List<Long> asList(final long[] input){ * @return
final List<Long> out = new ArrayList<Long>(input.length); */
for (int i = 0; i < input.length; i++){ public static final <T> List<T> asList(final T[] input){
out.add(input[i]); final List<T> out = new ArrayList<T>(input.length);
} for (int i = 0; i < input.length; i++){
return out; out.add(input[i]);
} }
return out;
/** }
* Return an ArrayList.
* @param input /**
* @return * Return an ArrayList.
*/ * @param input
public static final List<Double> asList(final double[] input){ * @return
final List<Double> out = new ArrayList<Double>(input.length); */
for (int i = 0; i < input.length; i++){ public static final List<Integer> asList(final int[] input){
out.add(input[i]); final List<Integer> out = new ArrayList<Integer>(input.length);
} for (int i = 0; i < input.length; i++){
return out; out.add(input[i]);
} }
return out;
/** }
* Return an ArrayList.
* @param input /**
* @return * Return an ArrayList.
*/ * @param input
public static final List<Float> asList(final float[] input){ * @return
final List<Float> out = new ArrayList<Float>(input.length); */
for (int i = 0; i < input.length; i++){ public static final List<Long> asList(final long[] input){
out.add(input[i]); final List<Long> out = new ArrayList<Long>(input.length);
} for (int i = 0; i < input.length; i++){
return out; out.add(input[i]);
} }
return out;
/** }
* Return an ArrayList.
* @param input /**
* @return * Return an ArrayList.
*/ * @param input
public static final List<Boolean> asList(final boolean[] input){ * @return
final List<Boolean> out = new ArrayList<Boolean>(input.length); */
for (int i = 0; i < input.length; i++){ public static final List<Double> asList(final double[] input){
out.add(input[i]); final List<Double> out = new ArrayList<Double>(input.length);
} for (int i = 0; i < input.length; i++){
return out; out.add(input[i]);
} }
return out;
} }
/**
* Return an ArrayList.
* @param input
* @return
*/
public static final List<Float> asList(final float[] input){
final List<Float> out = new ArrayList<Float>(input.length);
for (int i = 0; i < input.length; i++){
out.add(input[i]);
}
return out;
}
/**
* Return an ArrayList.
* @param input
* @return
*/
public static final List<Boolean> asList(final boolean[] input){
final List<Boolean> out = new ArrayList<Boolean>(input.length);
for (int i = 0; i < input.length; i++){
out.add(input[i]);
}
return out;
}
}

View File

@ -1,78 +1,78 @@
package me.asofold.bpl.cncp.config.compatlayer; package me.asofold.bpl.cncp.config.compatlayer;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.MemoryConfiguration; import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
public class NewConfig extends AbstractNewConfig{ public class NewConfig extends AbstractNewConfig{
public NewConfig(File file) { public NewConfig(File file) {
super(file); super(file);
} }
@Override @Override
public void load(){ public void load(){
config = new MemoryConfiguration(); config = new MemoryConfiguration();
setOptions(config); setOptions(config);
FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
setOptions(cfg); setOptions(cfg);
addAll(cfg, config); addAll(cfg, config);
} }
@Override @Override
public boolean save(){ public boolean save(){
YamlConfiguration cfg = new YamlConfiguration(); YamlConfiguration cfg = new YamlConfiguration();
setOptions(cfg); setOptions(cfg);
addAll(config, cfg); addAll(config, cfg);
try{ try{
cfg.save(file); cfg.save(file);
return true; return true;
} catch (Throwable t){ } catch (Throwable t){
return false; return false;
} }
} }
@Override @Override
public Map<String, Object> getValuesDeep() { public Map<String, Object> getValuesDeep() {
return config.getValues(true); return config.getValues(true);
} }
@Override @Override
public void clear() { public void clear() {
setFile(file); setFile(file);
} }
@Override @Override
public String getYAMLString() { public String getYAMLString() {
final YamlConfiguration temp = new YamlConfiguration(); final YamlConfiguration temp = new YamlConfiguration();
addAll(config, temp); addAll(config, temp);
return temp.saveToString(); return temp.saveToString();
} }
@Override @Override
public boolean fromYamlString(String input) { public boolean fromYamlString(String input) {
final YamlConfiguration temp = new YamlConfiguration(); final YamlConfiguration temp = new YamlConfiguration();
try { try {
clear(); clear();
temp.loadFromString(input); temp.loadFromString(input);
addAll(temp, config); addAll(temp, config);
return true; return true;
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
} }
} }

View File

@ -1,56 +1,56 @@
package me.asofold.bpl.cncp.hooks; package me.asofold.bpl.cncp.hooks;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory; import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook; import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
/** /**
* Reads an enabled flag from the config (true by default). The prefix given will be something like "hooks.", so add your hook name to the path like:<br> * Reads an enabled flag from the config (true by default). The prefix given will be something like "hooks.", so add your hook name to the path like:<br>
* prefix + "myhook.extra-config" * prefix + "myhook.extra-config"
* @author mc_dev * @author mc_dev
* *
*/ */
public class AbstractConfigurableHook extends AbstractHook implements public class AbstractConfigurableHook extends AbstractHook implements
ConfigurableHook { ConfigurableHook {
protected final String configPrefix; protected final String configPrefix;
protected final String hookName, hookVersion; protected final String hookName, hookVersion;
protected boolean enabled = true; protected boolean enabled = true;
public AbstractConfigurableHook(String hookName, String hookVersion, String configPrefix){ public AbstractConfigurableHook(String hookName, String hookVersion, String configPrefix){
this.configPrefix = configPrefix; this.configPrefix = configPrefix;
this.hookName = hookName; this.hookName = hookName;
this.hookVersion = hookVersion; this.hookVersion = hookVersion;
} }
@Override @Override
public String getHookName() { public String getHookName() {
return hookName; return hookName;
} }
@Override @Override
public String getHookVersion() { public String getHookVersion() {
return hookVersion; return hookVersion;
} }
@Override @Override
public void applyConfig(CompatConfig cfg, String prefix) { public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true); enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
} }
@Override @Override
public boolean updateConfig(CompatConfig cfg, String prefix) { public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null); CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true); defaults.set(prefix + configPrefix + "enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg); return ConfigUtil.forceDefaults(defaults, cfg);
} }
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
} }

View File

@ -1,46 +1,46 @@
package me.asofold.bpl.cncp.hooks; package me.asofold.bpl.cncp.hooks;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import fr.neatmonster.nocheatplus.hooks.NCPHook;
/** /**
* Extend this to make sure that future changes will not break your hooks.<br> * Extend this to make sure that future changes will not break your hooks.<br>
* Probable changes:<br> * Probable changes:<br>
* - Adding onReload method. * - Adding onReload method.
* @author mc_dev * @author mc_dev
* *
*/ */
public abstract class AbstractHook implements Hook{ public abstract class AbstractHook implements Hook{
@Override @Override
public CheckType[] getCheckTypes() { public CheckType[] getCheckTypes() {
// Handle all CheckEvents (improbable). // Handle all CheckEvents (improbable).
return null; return null;
} }
@Override @Override
public Listener[] getListeners() { public Listener[] getListeners() {
// No listeners to register with cncp. // No listeners to register with cncp.
return null; return null;
} }
@Override @Override
public NCPHook getNCPHook() { public NCPHook getNCPHook() {
// No hooks to add to NCP. // No hooks to add to NCP.
return null; return null;
} }
/** /**
* Throw a runtime exception if the plugin is not present. * Throw a runtime exception if the plugin is not present.
* @param pluginName * @param pluginName
*/ */
protected void assertPluginPresent(String pluginName){ protected void assertPluginPresent(String pluginName){
if (Bukkit.getPluginManager().getPlugin(pluginName) == null) throw new RuntimeException("Assertion, " + getHookName() + ": Plugin " + pluginName + " is not present."); if (Bukkit.getPluginManager().getPlugin(pluginName) == null) throw new RuntimeException("Assertion, " + getHookName() + ": Plugin " + pluginName + " is not present.");
} }
} }

View File

@ -0,0 +1,142 @@
package me.asofold.bpl.cncp.hooks.CMI;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import com.Zrips.CMI.CMI;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
import me.asofold.bpl.cncp.hooks.generic.ExemptionManager;
import me.asofold.bpl.cncp.utils.TickTask2;
public class HookCMI extends AbstractHook implements Listener, ConfigurableHook {
protected Object ncpHook = null;
protected boolean enabled = true;
protected String configPrefix = "cmi.";
protected final ExemptionManager exMan = new ExemptionManager();
protected final CheckType[] exemptBreakMany = new CheckType[]{
CheckType.BLOCKBREAK, CheckType.COMBINED_IMPROBABLE,
};
protected final CheckType[] exemptPlaceMany = new CheckType[]{
CheckType.BLOCKPLACE, CheckType.COMBINED_IMPROBABLE,
};
public HookCMI(){
assertPluginPresent("CMI");
}
@Override
public String getHookName() {
return "CMI(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{
this
};
}
@Override
public NCPHook getNCPHook() {
if (ncpHook == null){
ncpHook = new NCPHook() {
@Override
public String getHookName() {
return "CMI(cncp)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public boolean onCheckFailure(CheckType checkType, Player player, IViolationInfo info) {
return false;
}
};
}
return (NCPHook) ncpHook;
}
@EventHandler(priority=EventPriority.MONITOR)
//@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onMirrorBreakMonitor(final BlockBreakEvent event){
removeExemption(event.getPlayer(), exemptBreakMany);
}
@EventHandler(priority=EventPriority.LOWEST)
//@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onMirrorBreakLowest(final BlockBreakEvent event){
if (CMI.getInstance().getMirrorManager().isMirroring(event.getPlayer())) {
addExemption(event.getPlayer(), exemptBreakMany);
}
}
@EventHandler(priority=EventPriority.MONITOR)
//@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagLateFeature, afterTag = CompatNoCheatPlus.afterTagLateFeature)
public final void onMirrorPlaceMonitor(final BlockPlaceEvent event){
removeExemption(event.getPlayer(), exemptPlaceMany);
}
@EventHandler(priority=EventPriority.LOWEST)
//@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onMirrorPlaceLowest(final BlockPlaceEvent event){
if (CMI.getInstance().getMirrorManager().isMirroring(event.getPlayer())) {
addExemption(event.getPlayer(), exemptPlaceMany);
}
}
public void addExemption(final Player player, final CheckType[] types){
for (final CheckType type : types){
exMan.addExemption(player, type);
TickTask2.addUnexemptions(player, types);
}
}
public void removeExemption(final Player player, final CheckType[] types){
for (final CheckType type : types){
exMan.removeExemption(player, type);
}
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
}

View File

@ -0,0 +1,82 @@
package me.asofold.bpl.cncp.hooks.GravityTubes;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.permissions.Permissible;
import com.benzoft.gravitytubes.GTPerm;
import com.benzoft.gravitytubes.GravityTube;
import com.benzoft.gravitytubes.files.ConfigFile;
import com.benzoft.gravitytubes.files.GravityTubesFile;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.players.IPlayerData;
import fr.neatmonster.nocheatplus.utilities.location.TrigUtil;
import me.asofold.bpl.cncp.hooks.GravityTubes.HookGravityTubes.HookFacade;
public class HookFacadeImpl implements HookFacade, NCPHook {
public HookFacadeImpl(){}
@Override
public String getHookName() {
return "GravityTubes(cncp)";
}
@Override
public String getHookVersion() {
return "1.1";
}
@Override
public final boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
//if (checkType == CheckType.MOVING_CREATIVEFLY && !ConfigFile.getInstance().isSneakToFall() )
if (player.getGameMode() == GameMode.SPECTATOR) return false;
if ((checkType == CheckType.MOVING_CREATIVEFLY || checkType == CheckType.MOVING_SURVIVALFLY) && GTPerm.USE.checkPermission((Permissible)player)) {
final GravityTube tube = GravityTubesFile.getInstance().getTubes().stream().filter(gravityTube -> isInTube(gravityTube, player, true, true)).findFirst().orElse(null);
if (tube != null) {
return true;
}
}
return false;
}
private boolean isInTube(GravityTube tube, Player p, boolean longerH, boolean extendxz) {
final int tubePower = tube.getPower();
int power = tubePower > 171 ? 4 : tubePower > 80 ? 3 : tubePower > 25 ? 2 : 1;
final Location pLoc = p.getLocation();
final Location tLoc = tube.getSourceLocation();
final boolean b1 = p.getWorld().equals(tLoc.getWorld()) && pLoc.getY() >= tLoc.getBlockY() && pLoc.getY() <= tLoc.getBlockY() + tube.getHeight() + (longerH ? power : 0);
if (!extendxz)
return b1 && pLoc.getBlockX() == tLoc.getBlockX() && pLoc.getBlockZ() == tLoc.getBlockZ();
return b1 && isTubeNearby(pLoc.getBlockX(), pLoc.getBlockZ(), tLoc.getBlockX(), tLoc.getBlockZ());
}
private boolean isTubeNearby(int x1, int z1, int x2, int z2) {
return TrigUtil.distance(x1,z1,x2,z2) < 1.5;
}
@Override
public void onMoveLowest(PlayerMoveEvent event) {
final Player p = event.getPlayer();
if (p.getGameMode() == GameMode.SPECTATOR) return;
final double hDist = TrigUtil.xzDistance(event.getFrom(), event.getTo());
final double vDist = event.getFrom().getY() - event.getTo().getY();
if (GTPerm.USE.checkPermission((Permissible)p)
&& vDist > 0 && hDist < 0.35
&& ConfigFile.getInstance().isDisableFallDamage() && p.isSneaking()) {
final GravityTube tube = GravityTubesFile.getInstance().getTubes().stream().filter(gravityTube -> isInTube(gravityTube, p, false, false)).findFirst().orElse(null);
final IPlayerData pData = DataManager.getPlayerData(p);
if (tube != null && pData != null) {
final MovingData mData = pData.getGenericInstance(MovingData.class);
mData.clearNoFallData();
}
}
}
}

View File

@ -0,0 +1,77 @@
package me.asofold.bpl.cncp.hooks.GravityTubes;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
public class HookGravityTubes extends AbstractHook implements Listener, ConfigurableHook {
public static interface HookFacade{
public void onMoveLowest(PlayerMoveEvent event);
}
protected HookFacade ncpHook = null;
protected boolean enabled = true;
protected String configPrefix = "gravitytubes.";
public HookGravityTubes(){
assertPluginPresent("GravityTubes");
}
@Override
public String getHookName() {
return "GravityTubes(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{
this
};
}
@Override
public NCPHook getNCPHook() {
if (ncpHook == null){
ncpHook = new HookFacadeImpl();
}
return (NCPHook) ncpHook;
}
@EventHandler(priority=EventPriority.LOWEST)
//@RegisterMethodWithOrder(tag = CompatNoCheatPlus.tagEarlyFeature, beforeTag = CompatNoCheatPlus.beforeTagEarlyFeature)
public final void onMoveLowest(final PlayerMoveEvent event){
ncpHook.onMoveLowest(event);
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
}

View File

@ -1,48 +1,48 @@
package me.asofold.bpl.cncp.hooks; package me.asofold.bpl.cncp.hooks;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import fr.neatmonster.nocheatplus.hooks.NCPHook;
/** /**
* Interface for hooking into another plugin.<br> * Interface for hooking into another plugin.<br>
* NOTE: You also have to implement the methods described in NCPHook.<br> * NOTE: You also have to implement the methods described in NCPHook.<br>
* If you do not use listeners or don't want the prevent-hooks configuration feature * If you do not use listeners or don't want the prevent-hooks configuration feature
* to take effect, you can also register directly with NCPHookManager. * to take effect, you can also register directly with NCPHookManager.
* *
* @author mc_dev * @author mc_dev
* *
*/ */
public interface Hook{ public interface Hook{
/** /**
* *
* @return * @return
*/ */
public String getHookName(); public String getHookName();
/** /**
* *
* @return * @return
*/ */
public String getHookVersion(); public String getHookVersion();
/** /**
* The check types to register for, see NCPHookManager for reference, you can use ALL, * The check types to register for, see NCPHookManager for reference, you can use ALL,
*/ */
public CheckType[] getCheckTypes(); public CheckType[] getCheckTypes();
/** /**
* Get listener instances to be registered with cncp. * Get listener instances to be registered with cncp.
* @return null if unused. * @return null if unused.
*/ */
public Listener[] getListeners(); public Listener[] getListeners();
/** /**
* Get the hook to be registered with NCP, the registration will take place after NoCheatPlus has been enabled. * Get the hook to be registered with NCP, the registration will take place after NoCheatPlus has been enabled.
* @return Should always return the same hook or null if not used. * @return Should always return the same hook or null if not used.
*/ */
public NCPHook getNCPHook(); public NCPHook getNCPHook();
} }

View File

@ -1,79 +1,79 @@
package me.asofold.bpl.cncp.hooks.citizens2; package me.asofold.bpl.cncp.hooks.citizens2;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory; import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook; import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook; import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo; import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import fr.neatmonster.nocheatplus.hooks.NCPHook;
public class HookCitizens2 extends AbstractHook implements ConfigurableHook{ public class HookCitizens2 extends AbstractHook implements ConfigurableHook{
protected Object ncpHook = null; protected Object ncpHook = null;
protected boolean enabled = true; protected boolean enabled = true;
protected String configPrefix = "citizens2."; protected String configPrefix = "citizens2.";
public HookCitizens2(){ public HookCitizens2(){
assertPluginPresent("Citizens"); assertPluginPresent("Citizens");
CitizensAPI.getNPCRegistry(); // to let it fail for old versions. CitizensAPI.getNPCRegistry(); // to let it fail for old versions.
} }
@Override @Override
public String getHookName() { public String getHookName() {
return "Citizens2(default)"; return "Citizens2(default)";
} }
@Override @Override
public String getHookVersion() { public String getHookVersion() {
return "2.2"; return "2.2";
} }
@Override @Override
public NCPHook getNCPHook() { public NCPHook getNCPHook() {
if (ncpHook == null){ if (ncpHook == null){
ncpHook = new NCPHook() { ncpHook = new NCPHook() {
@Override @Override
public final boolean onCheckFailure(final CheckType checkType, final Player player, IViolationInfo info) { public final boolean onCheckFailure(final CheckType checkType, final Player player, IViolationInfo info) {
return CitizensAPI.getNPCRegistry().isNPC(player); return CitizensAPI.getNPCRegistry().isNPC(player);
} }
@Override @Override
public final String getHookVersion() { public final String getHookVersion() {
return "2.0"; return "2.0";
} }
@Override @Override
public final String getHookName() { public final String getHookName() {
return "Citizens2(cncp)"; return "Citizens2(cncp)";
} }
}; };
} }
return (NCPHook) ncpHook; return (NCPHook) ncpHook;
} }
@Override @Override
public void applyConfig(CompatConfig cfg, String prefix) { public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true); enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
} }
@Override @Override
public boolean updateConfig(CompatConfig cfg, String prefix) { public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null); CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true); defaults.set(prefix + configPrefix + "enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg); return ConfigUtil.forceDefaults(defaults, cfg);
} }
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
} }

View File

@ -1,85 +1,85 @@
package me.asofold.bpl.cncp.hooks.generic; package me.asofold.bpl.cncp.hooks.generic;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory; import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook; import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
/** /**
* Exempting players by class names for some class. * Exempting players by class names for some class.
* @author mc_dev * @author mc_dev
* *
*/ */
public abstract class ClassExemptionHook extends AbstractHook implements ConfigurableHook{ public abstract class ClassExemptionHook extends AbstractHook implements ConfigurableHook{
protected final ExemptionManager man = new ExemptionManager(); protected final ExemptionManager man = new ExemptionManager();
protected final List<String> defaultClasses = new LinkedList<String>(); protected final List<String> defaultClasses = new LinkedList<String>();
protected final LinkedHashSet<String> classes = new LinkedHashSet<String>(); protected final LinkedHashSet<String> classes = new LinkedHashSet<String>();
protected boolean defaultEnabled = true; protected boolean defaultEnabled = true;
protected boolean enabled = true; protected boolean enabled = true;
protected final String configPrefix; protected final String configPrefix;
public ClassExemptionHook(String configPrefix){ public ClassExemptionHook(String configPrefix){
this.configPrefix = configPrefix; this.configPrefix = configPrefix;
} }
public void setClasses(final Collection<String> classes){ public void setClasses(final Collection<String> classes){
this.classes.clear(); this.classes.clear();
this.classes.addAll(classes); this.classes.addAll(classes);
} }
/** /**
* Check if a player is to be exempted and exempt for the check type. * Check if a player is to be exempted and exempt for the check type.
* @param player * @param player
* @param clazz * @param clazz
* @param checkType * @param checkType
* @return If exempted. * @return If exempted.
*/ */
public boolean checkExempt(final Player player, final Class<?> clazz, final CheckType checkType){ public boolean checkExempt(final Player player, final Class<?> clazz, final CheckType checkType){
if (!classes.contains(clazz.getSimpleName())) return false; if (!classes.contains(clazz.getSimpleName())) return false;
return man.addExemption(player, checkType); return man.addExemption(player, checkType);
} }
/** /**
* *
* @param player * @param player
* @param checkType * @param checkType
* @return If the player is still exempted. * @return If the player is still exempted.
*/ */
public boolean checkUnexempt(final Player player, final Class<?> clazz, final CheckType checkType){ public boolean checkUnexempt(final Player player, final Class<?> clazz, final CheckType checkType){
if (!classes.contains(clazz.getSimpleName())) return false; if (!classes.contains(clazz.getSimpleName())) return false;
return man.removeExemption(player, checkType); return man.removeExemption(player, checkType);
} }
@Override @Override
public void applyConfig(CompatConfig cfg, String prefix) { public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", defaultEnabled); enabled = cfg.getBoolean(prefix + configPrefix + "enabled", defaultEnabled);
ConfigUtil.readStringSetFromList(cfg, prefix + configPrefix + "exempt-names", classes, true, true, false); ConfigUtil.readStringSetFromList(cfg, prefix + configPrefix + "exempt-names", classes, true, true, false);
} }
@Override @Override
public boolean updateConfig(CompatConfig cfg, String prefix) { public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null); CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", defaultEnabled); defaults.set(prefix + configPrefix + "enabled", defaultEnabled);
defaults.set(prefix + configPrefix + "exempt-names", defaultClasses); defaults.set(prefix + configPrefix + "exempt-names", defaultClasses);
return ConfigUtil.forceDefaults(defaults, cfg); return ConfigUtil.forceDefaults(defaults, cfg);
} }
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
} }

View File

@ -1,32 +1,32 @@
package me.asofold.bpl.cncp.hooks.generic; package me.asofold.bpl.cncp.hooks.generic;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
/** /**
* A hook that is using configuration. * A hook that is using configuration.
* @author mc_dev * @author mc_dev
* *
*/ */
public interface ConfigurableHook { public interface ConfigurableHook {
/** /**
* Adjust internals to the given configuration. * Adjust internals to the given configuration.
* @param cfg * @param cfg
* @param prefix * @param prefix
*/ */
public void applyConfig(CompatConfig cfg, String prefix); public void applyConfig(CompatConfig cfg, String prefix);
/** /**
* Update the given configuration with defaults where / if necessary (no blunt overwrite, it is a users config). * Update the given configuration with defaults where / if necessary (no blunt overwrite, it is a users config).
* @param cfg * @param cfg
* @param prefix * @param prefix
* @return If the configuration was changed. * @return If the configuration was changed.
*/ */
public boolean updateConfig(CompatConfig cfg, String prefix); public boolean updateConfig(CompatConfig cfg, String prefix);
/** /**
* If the hook is enabled by configuration or not. * If the hook is enabled by configuration or not.
* @return * @return
*/ */
public boolean isEnabled(); public boolean isEnabled();
} }

View File

@ -1,224 +1,224 @@
package me.asofold.bpl.cncp.hooks.generic; package me.asofold.bpl.cncp.hooks.generic;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
/** /**
* Auxiliary methods and data structure to handle simple sort of exemption. <br> * Auxiliary methods and data structure to handle simple sort of exemption. <br>
* NOTE: Not thread safe. * NOTE: Not thread safe.
* *
* @deprecated: Buggy / outdated concept - should not rely on nested stuff, use TickTask2 to unexempt timely if in doubt. * @deprecated: Buggy / outdated concept - should not rely on nested stuff, use TickTask2 to unexempt timely if in doubt.
* *
* @author mc_dev * @author mc_dev
* *
*/ */
public class ExemptionManager{ public class ExemptionManager{
public static enum Status{ public static enum Status{
EXEMPTED, EXEMPTED,
NOT_EXEMPTED, NOT_EXEMPTED,
NEEDS_EXEMPTION, NEEDS_EXEMPTION,
NEEDS_UNEXEMPTION, NEEDS_UNEXEMPTION,
} }
public static class ExemptionInfo{ public static class ExemptionInfo{
public static class CheckEntry{ public static class CheckEntry{
public int skip = 0; public int skip = 0;
public int exempt = 0; public int exempt = 0;
} }
/** Counts per type, for players being exempted already, to prevent unexempting. */ /** Counts per type, for players being exempted already, to prevent unexempting. */
public final Map<CheckType, CheckEntry> entries = new HashMap<CheckType, CheckEntry>(); public final Map<CheckType, CheckEntry> entries = new HashMap<CheckType, CheckEntry>();
/** /**
* If empty, it can get removed. * If empty, it can get removed.
* @return * @return
*/ */
public boolean isEmpty(){ public boolean isEmpty(){
return entries.isEmpty(); return entries.isEmpty();
} }
/** /**
* *
* @param type * @param type
* @param isExempted If the player is currently exempted, to be filled in with NCPHookManager.isExempted(player, type) * @param isExempted If the player is currently exempted, to be filled in with NCPHookManager.isExempted(player, type)
* @return EXEMPTED or NEEDS_EXEMPTION. The latter means the player has to be exempted. * @return EXEMPTED or NEEDS_EXEMPTION. The latter means the player has to be exempted.
*/ */
public Status increase(final CheckType type, final boolean isExempted){ public Status increase(final CheckType type, final boolean isExempted){
final CheckEntry entry = entries.get(type); final CheckEntry entry = entries.get(type);
if (entry == null){ if (entry == null){
final CheckEntry newEntry = new CheckEntry(); final CheckEntry newEntry = new CheckEntry();
entries.put(type, newEntry); entries.put(type, newEntry);
if (isExempted){ if (isExempted){
newEntry.skip = 1; newEntry.skip = 1;
return Status.EXEMPTED; return Status.EXEMPTED;
} }
else{ else{
newEntry.exempt = 1; newEntry.exempt = 1;
return Status.NEEDS_EXEMPTION; return Status.NEEDS_EXEMPTION;
} }
} }
if (entry.skip > 0){ if (entry.skip > 0){
if (isExempted){ if (isExempted){
entry.skip ++; entry.skip ++;
return Status.EXEMPTED; return Status.EXEMPTED;
} }
else{ else{
entry.exempt = entry.skip + 1; entry.exempt = entry.skip + 1;
entry.skip = 0; entry.skip = 0;
return Status.NEEDS_EXEMPTION; return Status.NEEDS_EXEMPTION;
} }
} }
else{ else{
// entry.exempt > 0 // entry.exempt > 0
entry.exempt ++; entry.exempt ++;
return isExempted ? Status.EXEMPTED : Status.NEEDS_EXEMPTION; return isExempted ? Status.EXEMPTED : Status.NEEDS_EXEMPTION;
} }
} }
/** /**
* *
* @param type * @param type
* @param isExempted If the player is currently exempted, to be filled in with NCPHookManager.isExempted(player, type) * @param isExempted If the player is currently exempted, to be filled in with NCPHookManager.isExempted(player, type)
* @return Status, if NEEDS_EXEMPTION the player has to be exempted. If NEEDS_UNEXEMPTION, the player has to be unexempted. * @return Status, if NEEDS_EXEMPTION the player has to be exempted. If NEEDS_UNEXEMPTION, the player has to be unexempted.
*/ */
public Status decrease(final CheckType type, final boolean isExempted){ public Status decrease(final CheckType type, final boolean isExempted){
final CheckEntry info = entries.get(type); final CheckEntry info = entries.get(type);
if (info == null) return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED; if (info == null) return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED;
if (info.skip > 0){ if (info.skip > 0){
info.skip --; info.skip --;
if (info.skip == 0){ if (info.skip == 0){
entries.remove(type); entries.remove(type);
return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED; return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED;
} }
else if (isExempted) return Status.EXEMPTED; else if (isExempted) return Status.EXEMPTED;
else{ else{
info.exempt = info.skip; info.exempt = info.skip;
info.skip = 0; info.skip = 0;
return Status.NEEDS_EXEMPTION; return Status.NEEDS_EXEMPTION;
} }
} }
else{ else{
info.exempt --; info.exempt --;
if (info.exempt == 0){ if (info.exempt == 0){
entries.remove(type); entries.remove(type);
return isExempted ? Status.NEEDS_UNEXEMPTION : Status.NOT_EXEMPTED; return isExempted ? Status.NEEDS_UNEXEMPTION : Status.NOT_EXEMPTED;
} }
else{ else{
return isExempted ? Status.EXEMPTED : Status.NEEDS_EXEMPTION; return isExempted ? Status.EXEMPTED : Status.NEEDS_EXEMPTION;
} }
} }
} }
} }
/** Exact player name -> ExemptionInfo */ /** Exact player name -> ExemptionInfo */
protected final Map<String, ExemptionInfo> exemptions; protected final Map<String, ExemptionInfo> exemptions;
public ExemptionManager(){ public ExemptionManager(){
this(30, 0.75f); this(30, 0.75f);
} }
/** /**
* *
* @param initialCapacity For the exemption HashMap. * @param initialCapacity For the exemption HashMap.
* @param loadFactor For the exemption HashMap. * @param loadFactor For the exemption HashMap.
*/ */
public ExemptionManager(int initialCapacity, float loadFactor) { public ExemptionManager(int initialCapacity, float loadFactor) {
exemptions = new HashMap<String, ExemptionManager.ExemptionInfo>(initialCapacity, loadFactor); exemptions = new HashMap<String, ExemptionManager.ExemptionInfo>(initialCapacity, loadFactor);
} }
/** /**
* Add exemption count and exempt, if necessary. * Add exemption count and exempt, if necessary.
* @param player * @param player
* @param type * @param type
* @return If the player was exempted already. * @return If the player was exempted already.
*/ */
public boolean addExemption(final Player player, final CheckType type){ public boolean addExemption(final Player player, final CheckType type){
final Status status = addExemption(player.getName(), type, NCPExemptionManager.isExempted(player, type)); final Status status = addExemption(player.getName(), type, NCPExemptionManager.isExempted(player, type));
if (status == Status.NEEDS_EXEMPTION) NCPExemptionManager.exemptPermanently(player, type); if (status == Status.NEEDS_EXEMPTION) NCPExemptionManager.exemptPermanently(player, type);
// System.out.println("add: " + type.toString() + " -> " + status); // System.out.println("add: " + type.toString() + " -> " + status);
return status == Status.EXEMPTED; return status == Status.EXEMPTED;
} }
/** /**
* Increment exemption count. * Increment exemption count.
* @param name * @param name
* @return If exemption is needed (NEEDS_EXEMPTION). * @return If exemption is needed (NEEDS_EXEMPTION).
*/ */
public Status addExemption(final String name, final CheckType type, boolean isExempted){ public Status addExemption(final String name, final CheckType type, boolean isExempted){
final ExemptionInfo info = exemptions.get(name); final ExemptionInfo info = exemptions.get(name);
final Status status; final Status status;
if (info == null){ if (info == null){
final ExemptionInfo newInfo = new ExemptionInfo(); final ExemptionInfo newInfo = new ExemptionInfo();
status = newInfo.increase(type, isExempted); status = newInfo.increase(type, isExempted);
exemptions.put(name, newInfo); exemptions.put(name, newInfo);
} }
else{ else{
status = info.increase(type, isExempted); status = info.increase(type, isExempted);
} }
return status; return status;
} }
/** /**
* Decrement exemption count, exempt or unexempt if necessary. * Decrement exemption count, exempt or unexempt if necessary.
* @param player * @param player
* @param type * @param type
* @return If the player is still exempted. * @return If the player is still exempted.
*/ */
public boolean removeExemption(final Player player, final CheckType type){ public boolean removeExemption(final Player player, final CheckType type){
final Status status = removeExemption(player.getName(), type, NCPExemptionManager.isExempted(player, type)); final Status status = removeExemption(player.getName(), type, NCPExemptionManager.isExempted(player, type));
if (status == Status.NEEDS_EXEMPTION) NCPExemptionManager.exemptPermanently(player, type); if (status == Status.NEEDS_EXEMPTION) NCPExemptionManager.exemptPermanently(player, type);
else if (status == Status.NEEDS_UNEXEMPTION) NCPExemptionManager.unexempt(player, type); else if (status == Status.NEEDS_UNEXEMPTION) NCPExemptionManager.unexempt(player, type);
// System.out.println("remove: " + type.toString() + " -> " + status); // System.out.println("remove: " + type.toString() + " -> " + status);
return status == Status.EXEMPTED || status == Status.NEEDS_EXEMPTION; return status == Status.EXEMPTED || status == Status.NEEDS_EXEMPTION;
} }
/** /**
* Decrement exemption count. * Decrement exemption count.
* @param name * @param name
* @return Status, NEEDS_EXEMPTION and NEEDS_UNEXEMPTION make it necessary to call NCP API. * @return Status, NEEDS_EXEMPTION and NEEDS_UNEXEMPTION make it necessary to call NCP API.
*/ */
public Status removeExemption(final String name, final CheckType type, boolean isExempted){ public Status removeExemption(final String name, final CheckType type, boolean isExempted){
final ExemptionInfo info = exemptions.get(name); final ExemptionInfo info = exemptions.get(name);
if (info == null) return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED; if (info == null) return isExempted ? Status.EXEMPTED : Status.NOT_EXEMPTED;
final Status status = info.decrease(type, isExempted); final Status status = info.decrease(type, isExempted);
if (info.isEmpty()) exemptions.remove(name); if (info.isEmpty()) exemptions.remove(name);
return status; return status;
} }
/** /**
* Check if the player should be exempted right now according to stored info. * Check if the player should be exempted right now according to stored info.
* @param name * @param name
* @param type * @param type
* @return * @return
*/ */
public boolean shouldBeExempted(final String name, final CheckType type){ public boolean shouldBeExempted(final String name, final CheckType type){
final ExemptionInfo info = exemptions.get(name); final ExemptionInfo info = exemptions.get(name);
if (info == null) return false; if (info == null) return false;
return !info.isEmpty(); return !info.isEmpty();
} }
/** /**
* Hides the API access from listeners potentially. * Hides the API access from listeners potentially.
* @param player * @param player
* @param checkType * @param checkType
*/ */
public void exempt(final Player player, final CheckType checkType){ public void exempt(final Player player, final CheckType checkType){
NCPExemptionManager.exemptPermanently(player, checkType); NCPExemptionManager.exemptPermanently(player, checkType);
} }
/** /**
* Hides the API access from listeners potentially. * Hides the API access from listeners potentially.
* @param player * @param player
* @param checkType * @param checkType
*/ */
public void unexempt(final Player player, final CheckType checkType){ public void unexempt(final Player player, final CheckType checkType){
NCPExemptionManager.unexempt(player, checkType); NCPExemptionManager.unexempt(player, checkType);
} }
} }

View File

@ -0,0 +1,64 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
/**
* Wrap block break events to exempt players from checks by comparison of event class names.
* @author mc_dev
*
*/
public class HookBlockBreak extends ClassExemptionHook implements Listener {
public HookBlockBreak() {
super("block-break.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MachinaCraft
"ArtificialBlockBreakEvent",
// mcMMO
"FakeBlockBreakEvent",
// MagicSpells
"MagicSpellsBlockBreakEvent"
}));
}
@Override
public String getHookName() {
return "BlockBreak(default)";
}
@Override
public String getHookVersion() {
return "1.1";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
final void onBlockBreakLowest(final BlockBreakEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKBREAK);
}
@EventHandler(priority = EventPriority.MONITOR)
final void onBlockBreakMonitor(final BlockBreakEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKBREAK);
}
}

View File

@ -0,0 +1,62 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
/**
* Wrap block place events to exempt players from checks by comparison of event class names.
* @author mc_dev
*
*/
public class HookBlockPlace extends ClassExemptionHook implements Listener{
public HookBlockPlace() {
super("block-place.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MachinaCraft
"ArtificialBlockPlaceEvent",
// MagicSpells
"MagicSpellsBlockPlaceEvent"
}));
}
@Override
public String getHookName() {
return "BlockPlace(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
final void onBlockPlaceLowest(final BlockPlaceEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKPLACE);
}
@EventHandler(priority = EventPriority.MONITOR)
final void onBlockPlaceMonitor(final BlockPlaceEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKPLACE);
}
}

View File

@ -0,0 +1,66 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
public class HookEntityDamageByEntity extends ClassExemptionHook implements
Listener {
public HookEntityDamageByEntity() {
super("entity-damage-by-entity.");
defaultClasses.addAll(Arrays.asList(new String[] {
// CrackShot
"WeaponDamageEntityEvent",
// MagicSpells
"MagicSpellsEntityDamageByEntityEvent" }));
}
@Override
public String getHookName() {
return "EntityDamageByEntity(default)";
}
@Override
public String getHookVersion() {
return "0.0";
}
@Override
public Listener[] getListeners() {
return new Listener[] { this };
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty())
enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
final void onDamageLowest(final EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
if (damager instanceof Player) {
checkExempt((Player) damager, event.getClass(), CheckType.FIGHT);
}
}
@EventHandler(priority = EventPriority.MONITOR)
final void onDamageMonitor(final EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
if (damager instanceof Player) {
checkUnexempt((Player) damager, event.getClass(), CheckType.FIGHT);
}
}
}

View File

@ -0,0 +1,171 @@
package me.asofold.bpl.cncp.hooks.generic;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.utilities.TickTask;
public class HookInstaBreak extends AbstractHook implements ConfigurableHook, Listener {
public static interface InstaExemption{
public void addExemptNext(CheckType[] types);
public Set<CheckType> getExemptNext();
}
public static class StackEntry{
public final CheckType[] checkTypes;
public final int tick;
public final Player player;
public boolean used = false;
public StackEntry(final Player player , final CheckType[] checkTypes){
this.player = player;
this.checkTypes = checkTypes;
tick = TickTask.getTick();
}
public boolean isOutdated(final int tick){
return tick != this.tick;
}
}
protected static InstaExemption runtime = null;
public static void addExemptNext(final CheckType[] types){
runtime.addExemptNext(types);
}
protected final ExemptionManager exMan = new ExemptionManager();
protected boolean enabled = true;
protected final List<StackEntry> stack = new LinkedList<StackEntry>();
@Override
public String getHookName() {
return "InstaBreak(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + "insta-break.enabled", true);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + "insta-break.enabled", true);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public CheckType[] getCheckTypes() {
return null;
}
@Override
public Listener[] getListeners() {
runtime = new InstaExemption() {
protected final Set<CheckType> types = new HashSet<CheckType>();
@Override
public final void addExemptNext(final CheckType[] types) {
for (int i = 0; i < types.length; i++){
this.types.add(types[i]);
}
}
@Override
public Set<CheckType> getExemptNext() {
return types;
}
};
return new Listener[]{this};
}
protected CheckType[] fetchTypes(){
final Set<CheckType> types = runtime.getExemptNext();
final CheckType[] a = new CheckType[types.size()];
if (!types.isEmpty()) types.toArray(a);
types.clear();
return a;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
public void onBlockDamage(final BlockDamageEvent event){
checkStack();
if (!event.isCancelled() && event.getInstaBreak()){
stack.add(new StackEntry(event.getPlayer(), fetchTypes()));
}
else{
runtime.getExemptNext().clear();
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
public void onBlockBreakLowest(final BlockBreakEvent event){
checkStack();
if (!stack.isEmpty()){
final Player player = event.getPlayer();
final StackEntry entry = stack.get(stack.size() - 1);
if (player.equals(entry.player)) addExemption(entry);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
public void onBlockBreakMONITOR(final BlockBreakEvent event){
if (!stack.isEmpty()){
final Player player = event.getPlayer();
final StackEntry entry = stack.get(stack.size() - 1);
if (player.equals(entry.player)) removeExemption(stack.remove(stack.size() - 1));
}
}
public void addExemption(final StackEntry entry){
entry.used = true;
for (int i = 0; i < entry.checkTypes.length; i++){
exMan.addExemption(entry.player, entry.checkTypes[i]);
}
}
public void removeExemption(final StackEntry entry){
if (!entry.used) return;
for (int i = 0; i < entry.checkTypes.length; i++){
exMan.removeExemption(entry.player, entry.checkTypes[i]);
}
}
public void checkStack(){
if (stack.isEmpty()) return;
Iterator<StackEntry> it = stack.iterator();
final int tick = TickTask.getTick();
while (it.hasNext()){
final StackEntry entry = it.next();
if (entry.isOutdated(tick)) it.remove();
if (entry.used) removeExemption(entry);
}
}
}

View File

@ -1,121 +1,121 @@
package me.asofold.bpl.cncp.hooks.generic; package me.asofold.bpl.cncp.hooks.generic;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Set; import java.util.Set;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory; import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook; import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo; import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import fr.neatmonster.nocheatplus.hooks.NCPHook;
public final class HookPlayerClass extends AbstractHook implements ConfigurableHook { public final class HookPlayerClass extends AbstractHook implements ConfigurableHook {
private static final boolean defaultEnabled = false; private static final boolean defaultEnabled = false;
protected final Set<String> classNames = new HashSet<String>(); protected final Set<String> classNames = new HashSet<String>();
protected boolean exemptAll = true; protected boolean exemptAll = true;
protected boolean checkSuperClass = true; protected boolean checkSuperClass = true;
protected Object ncpHook = null; protected Object ncpHook = null;
protected boolean enabled = defaultEnabled; protected boolean enabled = defaultEnabled;
/** /**
* Normal class name. * Normal class name.
*/ */
protected Set<String> playerClassNames = new HashSet<String>(); protected Set<String> playerClassNames = new HashSet<String>();
public HookPlayerClass(){ public HookPlayerClass(){
// TODO: Might need cleanup. // TODO: Might need cleanup.
this.playerClassNames.addAll(Arrays.asList(new String[]{"CraftPlayer", "SpoutCraftPlayer", "SpoutPlayer", "SpoutClientPlayer", "SpoutPlayerSnapshot"})); this.playerClassNames.addAll(Arrays.asList(new String[]{"CraftPlayer", "SpoutCraftPlayer", "SpoutPlayer", "SpoutClientPlayer", "SpoutPlayerSnapshot"}));
} }
@Override @Override
public final String getHookName() { public final String getHookName() {
return "PlayerClass(default)"; return "PlayerClass(default)";
} }
@Override @Override
public final String getHookVersion() { public final String getHookVersion() {
return "2.3"; return "2.3";
} }
@Override @Override
public NCPHook getNCPHook() { public NCPHook getNCPHook() {
if (ncpHook == null){ if (ncpHook == null){
ncpHook = new NCPHook() { ncpHook = new NCPHook() {
@Override @Override
public final boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) { public final boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
if (exemptAll && !playerClassNames.contains(player.getClass().getSimpleName())) return true; if (exemptAll && !playerClassNames.contains(player.getClass().getSimpleName())) return true;
else { else {
if (classNames.isEmpty()) return false; if (classNames.isEmpty()) return false;
final Class<?> clazz = player.getClass(); final Class<?> clazz = player.getClass();
final String name = clazz.getSimpleName(); final String name = clazz.getSimpleName();
if (classNames.contains(name)) return true; if (classNames.contains(name)) return true;
else if (checkSuperClass){ else if (checkSuperClass){
while (true){ while (true){
final Class<?> superClass = clazz.getSuperclass(); final Class<?> superClass = clazz.getSuperclass();
if (superClass == null) return false; if (superClass == null) return false;
else{ else{
final String superName = superClass.getSimpleName(); final String superName = superClass.getSimpleName();
if (superName.equals("Object")) return false; if (superName.equals("Object")) return false;
else if (classNames.contains(superName)){ else if (classNames.contains(superName)){
return true; return true;
} }
} }
} }
} }
} }
return false; // ECLIPSE return false; // ECLIPSE
} }
@Override @Override
public String getHookVersion() { public String getHookVersion() {
return "3.1"; return "3.1";
} }
@Override @Override
public String getHookName() { public String getHookName() {
return "PlayerClass(cncp)"; return "PlayerClass(cncp)";
} }
}; };
} }
return (NCPHook) ncpHook; return (NCPHook) ncpHook;
} }
@Override @Override
public void applyConfig(CompatConfig cfg, String prefix) { public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + "player-class.enabled", defaultEnabled); enabled = cfg.getBoolean(prefix + "player-class.enabled", defaultEnabled);
ConfigUtil.readStringSetFromList(cfg, prefix + "player-class.exempt-names", classNames, true, true, false); ConfigUtil.readStringSetFromList(cfg, prefix + "player-class.exempt-names", classNames, true, true, false);
exemptAll = cfg.getBoolean(prefix + "player-class.exempt-all", true); exemptAll = cfg.getBoolean(prefix + "player-class.exempt-all", true);
ConfigUtil.readStringSetFromList(cfg, prefix + "player-class.class-names", playerClassNames, true, true, false); ConfigUtil.readStringSetFromList(cfg, prefix + "player-class.class-names", playerClassNames, true, true, false);
checkSuperClass = cfg.getBoolean(prefix + "player-class.super-class", true); checkSuperClass = cfg.getBoolean(prefix + "player-class.super-class", true);
} }
@Override @Override
public boolean updateConfig(CompatConfig cfg, String prefix) { public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null); CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + "player-class.enabled", defaultEnabled); defaults.set(prefix + "player-class.enabled", defaultEnabled);
defaults.set(prefix + "player-class.exempt-names", new LinkedList<String>()); defaults.set(prefix + "player-class.exempt-names", new LinkedList<String>());
defaults.set(prefix + "player-class.exempt-all", true); defaults.set(prefix + "player-class.exempt-all", true);
defaults.set(prefix + "player-class.class-names", new LinkedList<String>(playerClassNames)); defaults.set(prefix + "player-class.class-names", new LinkedList<String>(playerClassNames));
defaults.set(prefix + "player-class.super-class", true); defaults.set(prefix + "player-class.super-class", true);
return ConfigUtil.forceDefaults(defaults, cfg); return ConfigUtil.forceDefaults(defaults, cfg);
} }
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
} }

View File

@ -0,0 +1,58 @@
package me.asofold.bpl.cncp.hooks.generic;
import fr.neatmonster.nocheatplus.checks.CheckType;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.Arrays;
/**
* Wrap player interact events to exempt players from checks by comparison of event class names.
* Uses mc_dev's format for exemption based upon class names.
*
*/
public class HookPlayerInteract extends ClassExemptionHook implements Listener{
public HookPlayerInteract() {
super("player-interact.");
defaultClasses.addAll(Arrays.asList(new String[]{
// MagicSpells
"MagicSpellsPlayerInteractEvent"
}));
}
@Override
public String getHookName() {
return "Interact(default)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public Listener[] getListeners() {
return new Listener[]{this};
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix);
if (classes.isEmpty()) enabled = false;
}
@EventHandler(priority = EventPriority.LOWEST)
final void onPlayerInteractLowest(final PlayerInteractEvent event){
checkExempt(event.getPlayer(), event.getClass(), CheckType.BLOCKINTERACT);
}
@EventHandler(priority = EventPriority.MONITOR)
final void onPlayerInteractMonitor(final PlayerInteractEvent event){
checkUnexempt(event.getPlayer(), event.getClass(), CheckType.BLOCKINTERACT);
}
}

View File

@ -0,0 +1,106 @@
package me.asofold.bpl.cncp.hooks.generic;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
public class HookSetSpeed extends AbstractHook implements Listener, ConfigurableHook{
private static final float defaultFlySpeed = 0.1f;
private static final float defaultWalkSpeed = 0.2f;
protected float flySpeed = defaultFlySpeed;
protected float walkSpeed = defaultWalkSpeed;
protected boolean enabled = false;
// private String allowFlightPerm = "cncp.allow-flight";
public HookSetSpeed() throws SecurityException, NoSuchMethodException{
Player.class.getDeclaredMethod("setFlySpeed", float.class);
}
public void init(){
for (final Player player : Bukkit.getOnlinePlayers()){
setSpeed(player);
}
}
@Override
public String getHookName() {
return "SetSpeed(default)";
}
@Override
public String getHookVersion() {
return "2.2";
}
@Override
public CheckType[] getCheckTypes() {
return new CheckType[0];
}
@Override
public Listener[] getListeners() {
try{
// Initialize here, at the end of enable.
init();
}
catch (Throwable t){}
return new Listener[]{this} ;
}
public final void setSpeed(final Player player){
// if (allowFlightPerm.equals("") || player.hasPermission(allowFlightPerm)) player.setAllowFlight(true);
player.setWalkSpeed(walkSpeed);
player.setFlySpeed(flySpeed);
}
@EventHandler(priority=EventPriority.LOWEST)
public final void onPlayerJoin(final PlayerJoinEvent event){
setSpeed(event.getPlayer());
}
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + "set-speed.enabled", false);
flySpeed = cfg.getDouble(prefix + "set-speed.fly-speed", (double) defaultFlySpeed).floatValue();
walkSpeed = cfg.getDouble(prefix + "set-speed.walk-speed", (double) defaultWalkSpeed).floatValue();
// allowFlightPerm = cfg.getString(prefix + "set-speed.allow-flight-permission", ref.allowFlightPerm);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + "set-speed.enabled", false);
defaults.set(prefix + "set-speed.fly-speed", defaultFlySpeed);
defaults.set(prefix + "set-speed.walk-speed", defaultWalkSpeed);
// cfg.set(prefix + "set-speed.allow-flight-permission", ref.allowFlightPerm);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
// public String getAllowFlightPerm() {
// return allowFlightPerm;
// }
// public void setAllowFlightPerm(String allowFlightPerm) {
// this.allowFlightPerm = allowFlightPerm;
// }
}

View File

@ -1,121 +1,121 @@
package me.asofold.bpl.cncp.hooks.magicspells; package me.asofold.bpl.cncp.hooks.magicspells;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig; import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory; import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil; import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractConfigurableHook; import me.asofold.bpl.cncp.hooks.AbstractConfigurableHook;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
public class HookMagicSpells extends AbstractConfigurableHook implements Listener{ public class HookMagicSpells extends AbstractConfigurableHook implements Listener{
protected final Map<String, CheckType[]> spellMap = new HashMap<String, CheckType[]>(); protected final Map<String, CheckType[]> spellMap = new HashMap<String, CheckType[]>();
public HookMagicSpells(){ public HookMagicSpells(){
super("MagicSpells(default)", "1.0", "magicspells."); super("MagicSpells(default)", "1.0", "magicspells.");
assertPluginPresent("MagicSpells"); assertPluginPresent("MagicSpells");
} }
@Override @Override
public Listener[] getListeners() { public Listener[] getListeners() {
return new Listener[]{ return new Listener[]{
this this
}; };
} }
// @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) // @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
// public void onSpellCast(final SpellCastEvent event){ // public void onSpellCast(final SpellCastEvent event){
// if (event.getSpellCastState() != SpellCastState.NORMAL) return; // if (event.getSpellCastState() != SpellCastState.NORMAL) return;
// exempt(event.getCaster(), event.getSpell()); // exempt(event.getCaster(), event.getSpell());
// } // }
// //
// @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) // @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
// public void onSpellCasted(final SpellCastedEvent event){ // public void onSpellCasted(final SpellCastedEvent event){
// unexempt(event.getCaster(), event.getSpell()); // unexempt(event.getCaster(), event.getSpell());
// } // }
// //
// private void exempt(final Player player, final Spell spell) { // private void exempt(final Player player, final Spell spell) {
// final CheckType[] types = getCheckTypes(spell); // final CheckType[] types = getCheckTypes(spell);
// if (types == null) return; // if (types == null) return;
// for (final CheckType type : types){ // for (final CheckType type : types){
// NCPExemptionManager.exemptPermanently(player, type); // NCPExemptionManager.exemptPermanently(player, type);
// // Safety fall-back: // // Safety fall-back:
// // TODO: Might interfere with "slow" effects of spells ? [might add config for this.] // // TODO: Might interfere with "slow" effects of spells ? [might add config for this.]
// TickTask2.addUnexemptions(player, types); // TickTask2.addUnexemptions(player, types);
// } // }
// } // }
// //
// private void unexempt(final Player player, final Spell spell) { // private void unexempt(final Player player, final Spell spell) {
// final CheckType[] types = getCheckTypes(spell); // final CheckType[] types = getCheckTypes(spell);
// if (types == null) return; // if (types == null) return;
// for (final CheckType type : types){ // for (final CheckType type : types){
// NCPExemptionManager.unexempt(player, type); // NCPExemptionManager.unexempt(player, type);
// } // }
// } // }
// //
// protected CheckType[] getCheckTypes(final Spell spell){ // protected CheckType[] getCheckTypes(final Spell spell){
// return spellMap.get(spell.getName()); // return spellMap.get(spell.getName());
// } // }
/* (non-Javadoc) /* (non-Javadoc)
* @see me.asofold.bpl.cncp.hooks.AbstractConfigurableHook#applyConfig(me.asofold.bpl.cncp.config.compatlayer.CompatConfig, java.lang.String) * @see me.asofold.bpl.cncp.hooks.AbstractConfigurableHook#applyConfig(me.asofold.bpl.cncp.config.compatlayer.CompatConfig, java.lang.String)
*/ */
@Override @Override
public void applyConfig(CompatConfig cfg, String prefix) { public void applyConfig(CompatConfig cfg, String prefix) {
super.applyConfig(cfg, prefix); super.applyConfig(cfg, prefix);
prefix += this.configPrefix; prefix += this.configPrefix;
List<String> keys = cfg.getStringKeys(prefix + "exempt-spells"); List<String> keys = cfg.getStringKeys(prefix + "exempt-spells");
for (String key : keys){ for (String key : keys){
String fullKey = ConfigUtil.bestPath(cfg, prefix + "exempt-spells." + key); String fullKey = ConfigUtil.bestPath(cfg, prefix + "exempt-spells." + key);
String types = cfg.getString(fullKey); String types = cfg.getString(fullKey);
if (types == null) continue; if (types == null) continue;
String[] split = types.split(","); String[] split = types.split(",");
Set<CheckType> checkTypes = new HashSet<CheckType>(); Set<CheckType> checkTypes = new HashSet<CheckType>();
for (String input : split){ for (String input : split){
input = input.trim().toUpperCase().replace('-', '_').replace(' ', '_').replace('.', '_'); input = input.trim().toUpperCase().replace('-', '_').replace(' ', '_').replace('.', '_');
CheckType type = null; CheckType type = null;
try{ try{
type = CheckType.valueOf(input); type = CheckType.valueOf(input);
} }
catch(Throwable t){ catch(Throwable t){
} }
if (type == null){ if (type == null){
Bukkit.getLogger().warning("[cncp] HookMagicSpells: Bad check type at " + fullKey + ": " + input); Bukkit.getLogger().warning("[CompatNoCheatPlus] HookMagicSpells: Bad check type at " + fullKey + ": " + input);
} }
else checkTypes.add(type); else checkTypes.add(type);
} }
if (checkTypes.isEmpty()){ if (checkTypes.isEmpty()){
Bukkit.getLogger().warning("[cncp] HookMagicSpells: No CheckType entries at: " + fullKey); Bukkit.getLogger().warning("[CompatNoCheatPlus] HookMagicSpells: No CheckType entries at: " + fullKey);
} }
else{ else{
CheckType[] a = new CheckType[checkTypes.size()]; CheckType[] a = new CheckType[checkTypes.size()];
checkTypes.toArray(a); checkTypes.toArray(a);
spellMap.put(key, a); spellMap.put(key, a);
} }
} }
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see me.asofold.bpl.cncp.hooks.AbstractConfigurableHook#updateConfig(me.asofold.bpl.cncp.config.compatlayer.CompatConfig, java.lang.String) * @see me.asofold.bpl.cncp.hooks.AbstractConfigurableHook#updateConfig(me.asofold.bpl.cncp.config.compatlayer.CompatConfig, java.lang.String)
*/ */
@Override @Override
public boolean updateConfig(CompatConfig cfg, String prefix) { public boolean updateConfig(CompatConfig cfg, String prefix) {
super.updateConfig(cfg, prefix); super.updateConfig(cfg, prefix);
CompatConfig defaults = CompatConfigFactory.getConfig(null); CompatConfig defaults = CompatConfigFactory.getConfig(null);
prefix += this.configPrefix; prefix += this.configPrefix;
// TODO: Write default section. // TODO: Write default section.
defaults.set(prefix + "NOTE", "MagicSpells support is experimental, only instant spells can be added here."); defaults.set(prefix + "NOTE", "MagicSpells support is experimental, only instant spells can be added here.");
defaults.set(prefix + "exempt-spells.NameOfExampleSpell", "COMBINED_MUNCHHAUSEN, UNKNOWN, BLOCKPLACE_NOSWING"); defaults.set(prefix + "exempt-spells.NameOfExampleSpell", "COMBINED_MUNCHHAUSEN, UNKNOWN, BLOCKPLACE_NOSWING");
return ConfigUtil.forceDefaults(defaults, cfg); return ConfigUtil.forceDefaults(defaults, cfg);
} }
} }

View File

@ -1,256 +1,253 @@
package me.asofold.bpl.cncp.hooks.mcmmo; package me.asofold.bpl.cncp.hooks.mcmmo;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo; import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.hooks.NCPHook; import fr.neatmonster.nocheatplus.compat.Bridge1_9;
import fr.neatmonster.nocheatplus.players.DataManager; import fr.neatmonster.nocheatplus.compat.Folia;
import fr.neatmonster.nocheatplus.utilities.map.BlockProperties; import fr.neatmonster.nocheatplus.hooks.NCPHook;
import fr.neatmonster.nocheatplus.utilities.map.BlockProperties.ToolProps; import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.utilities.map.BlockProperties.ToolType; import fr.neatmonster.nocheatplus.utilities.map.BlockProperties;
import me.asofold.bpl.cncp.CompatNoCheatPlus; import fr.neatmonster.nocheatplus.utilities.map.BlockProperties.ToolProps;
import me.asofold.bpl.cncp.hooks.generic.ExemptionManager; import fr.neatmonster.nocheatplus.utilities.map.BlockProperties.ToolType;
import me.asofold.bpl.cncp.hooks.generic.HookInstaBreak; import me.asofold.bpl.cncp.CompatNoCheatPlus;
import me.asofold.bpl.cncp.hooks.mcmmo.HookmcMMO.HookFacade; import me.asofold.bpl.cncp.hooks.generic.ExemptionManager;
import me.asofold.bpl.cncp.utils.ActionFrequency; import me.asofold.bpl.cncp.hooks.generic.HookInstaBreak;
import me.asofold.bpl.cncp.utils.TickTask2; import me.asofold.bpl.cncp.hooks.mcmmo.HookmcMMO.HookFacade;
import me.asofold.bpl.cncp.utils.ActionFrequency;
@SuppressWarnings("deprecation") import me.asofold.bpl.cncp.utils.TickTask2;
public class HookFacadeImpl implements HookFacade, NCPHook {
@SuppressWarnings("deprecation")
public class HookFacadeImpl implements HookFacade, NCPHook {
protected final ExemptionManager exMan = new ExemptionManager();
/** Normal click per block skills. */ protected final ExemptionManager exMan = new ExemptionManager();
protected final CheckType[] exemptBreakNormal = new CheckType[]{
CheckType.BLOCKBREAK_FASTBREAK, CheckType.BLOCKBREAK_FREQUENCY, /** Normal click per block skills. */
CheckType.BLOCKBREAK_NOSWING, protected final CheckType[] exemptBreakNormal = new CheckType[]{
CheckType.BLOCKBREAK_WRONGBLOCK, // Not optimal but ok. CheckType.BLOCKBREAK_FASTBREAK, CheckType.BLOCKBREAK_FREQUENCY,
}; CheckType.BLOCKBREAK_NOSWING,
CheckType.BLOCKBREAK_WRONGBLOCK, // Not optimal but ok.
};
protected final CheckType[] exemptBreakMany = new CheckType[]{
CheckType.BLOCKBREAK, CheckType.COMBINED_IMPROBABLE,
}; protected final CheckType[] exemptBreakMany = new CheckType[]{
CheckType.BLOCKBREAK, CheckType.COMBINED_IMPROBABLE,
/** Fighting damage of effects such as bleeding or area (potentially). */ };
protected final CheckType[] exemptFightEffect = new CheckType[]{
CheckType.FIGHT_SPEED, CheckType.FIGHT_DIRECTION, /** Fighting damage of effects such as bleeding or area (potentially). */
CheckType.FIGHT_ANGLE, CheckType.FIGHT_NOSWING, protected final CheckType[] exemptFightEffect = new CheckType[]{
CheckType.FIGHT_REACH, CheckType.COMBINED_IMPROBABLE, CheckType.FIGHT_SPEED, CheckType.FIGHT_DIRECTION,
}; CheckType.FIGHT_ANGLE, CheckType.FIGHT_NOSWING,
CheckType.FIGHT_REACH, CheckType.COMBINED_IMPROBABLE,
// Presets for after failure exemption. };
protected final Map<CheckType, Integer> cancelChecksBlockBreak = new HashMap<CheckType, Integer>();
// protected final Map<CheckType, Integer> cancelChecksBlockDamage = new HashMap<CheckType, Integer>(); // Presets for after failure exemption.
// protected final Map<CheckType, Integer> cancelChecksDamage = new HashMap<CheckType, Integer>(); protected final Map<CheckType, Integer> cancelChecksBlockBreak = new HashMap<CheckType, Integer>();
// protected final Map<CheckType, Integer> cancelChecksBlockDamage = new HashMap<CheckType, Integer>();
protected boolean useInstaBreakHook; // protected final Map<CheckType, Integer> cancelChecksDamage = new HashMap<CheckType, Integer>();
protected int clicksPerSecond;
protected String cancel = null; protected boolean useInstaBreakHook;
protected long cancelTicks = 0; protected int clicksPerSecond;
protected String cancel = null;
protected final Map<CheckType, Integer> cancelChecks = new HashMap<CheckType, Integer>(); protected long cancelTicks = 0;
/** protected final Map<CheckType, Integer> cancelChecks = new HashMap<CheckType, Integer>();
* Last block breaking time
*/ /**
protected final Map<String, ActionFrequency> lastBreak = new HashMap<String, ActionFrequency>(50); * Last block breaking time
*/
/** Counter for nested events to cancel break counting. */ protected final Map<String, ActionFrequency> lastBreak = new HashMap<String, ActionFrequency>(50);
protected int breakCancel = 0;
/** Counter for nested events to cancel break counting. */
protected int lastBreakAddCount = 0; protected int breakCancel = 0;
protected long lastBreakCleanup = 0;
protected int lastBreakAddCount = 0;
public HookFacadeImpl(boolean useInstaBreakHook, int clicksPerSecond){ protected long lastBreakCleanup = 0;
this.useInstaBreakHook = useInstaBreakHook;
this.clicksPerSecond = clicksPerSecond; public HookFacadeImpl(boolean useInstaBreakHook, int clicksPerSecond){
cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_NOSWING, 1); this.useInstaBreakHook = useInstaBreakHook;
cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_FASTBREAK, 1); this.clicksPerSecond = clicksPerSecond;
// cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_NOSWING, 1);
// cancelChecksBlockDamage.put(CheckType.BLOCKBREAK_FASTBREAK, 1); cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_FASTBREAK, 1);
// //
// cancelChecksDamage.put(CheckType.FIGHT_ANGLE, 1); // cancelChecksBlockDamage.put(CheckType.BLOCKBREAK_FASTBREAK, 1);
// cancelChecksDamage.put(CheckType.FIGHT_SPEED, 1); //
} // cancelChecksDamage.put(CheckType.FIGHT_ANGLE, 1);
// cancelChecksDamage.put(CheckType.FIGHT_SPEED, 1);
@Override }
public String getHookName() {
return "mcMMO(cncp)"; @Override
} public String getHookName() {
return "mcMMO(cncp)";
@Override }
public String getHookVersion() {
return "2.3"; @Override
} public String getHookVersion() {
return "2.3";
@Override }
public final boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
// System.out.println(player.getName() + " -> " + checkType + "---------------------------"); @Override
// Somewhat generic canceling mechanism (within the same tick). public final boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
// Might later fail, if block break event gets scheduled after block damage having set insta break, instead of letting them follow directly. // System.out.println(player.getName() + " -> " + checkType + "---------------------------");
if (cancel == null){ // Somewhat generic canceling mechanism (within the same tick).
return false; // Might later fail, if block break event gets scheduled after block damage having set insta break, instead of letting them follow directly.
} if (cancel == null){
return false;
final String name = player.getName(); }
if (cancel.equals(name)){
final String name = player.getName();
if (player.getTicksLived() != cancelTicks){ if (cancel.equals(name)){
cancel = null;
} if (player.getTicksLived() != cancelTicks){
else{ cancel = null;
final Integer n = cancelChecks.get(checkType); }
if (n == null){ else{
return false; final Integer n = cancelChecks.get(checkType);
} if (n == null){
else if (n > 0){ return false;
if (n == 1) cancelChecks.remove(checkType); }
else cancelChecks.put(checkType, n - 1); else if (n > 0){
} if (n == 1) cancelChecks.remove(checkType);
return true; else cancelChecks.put(checkType, n - 1);
} }
} return true;
return false; }
} }
return false;
private final void setPlayer(final Player player, Map<CheckType, Integer> cancelChecks){ }
cancel = player.getName();
cancelTicks = player.getTicksLived(); private final void setPlayer(final Player player, Map<CheckType, Integer> cancelChecks){
this.cancelChecks.clear(); cancel = player.getName();
this.cancelChecks.putAll(cancelChecks); cancelTicks = player.getTicksLived();
} this.cancelChecks.clear();
this.cancelChecks.putAll(cancelChecks);
public ToolProps getToolProps(final ItemStack stack){ }
if (stack == null) return BlockProperties.noTool;
else return BlockProperties.getToolProps(stack); public ToolProps getToolProps(final ItemStack stack){
} if (stack == null) return BlockProperties.noTool;
else return BlockProperties.getToolProps(stack);
public void addExemption(final Player player, final CheckType[] types){ }
for (final CheckType type : types){
exMan.addExemption(player, type); public void addExemption(final Player player, final CheckType[] types){
TickTask2.addUnexemptions(player, types); for (final CheckType type : types){
} exMan.addExemption(player, type);
} TickTask2.addUnexemptions(player, types);
}
public void removeExemption(final Player player, final CheckType[] types){ }
for (final CheckType type : types){
exMan.removeExemption(player, type); public void removeExemption(final Player player, final CheckType[] types){
} for (final CheckType type : types){
} exMan.removeExemption(player, type);
}
@Override }
public final void damageLowest(final Player player) {
// System.out.println("damage lowest"); @Override
// setPlayer(player, cancelChecksDamage); public final void damageLowest(final Player player) {
addExemption(player, exemptFightEffect); // System.out.println("damage lowest");
} // setPlayer(player, cancelChecksDamage);
addExemption(player, exemptFightEffect);
@Override }
public final void blockDamageLowest(final Player player) {
// System.out.println("block damage lowest"); @Override
// setPlayer(player, cancelChecksBlockDamage); public final void blockDamageLowest(final Player player) {
if (getToolProps(player.getItemInHand()).toolType == ToolType.AXE) addExemption(player, exemptBreakMany); // System.out.println("block damage lowest");
else addExemption(player, exemptBreakNormal); // setPlayer(player, cancelChecksBlockDamage);
} if (getToolProps(Bridge1_9.getItemInMainHand(player)).toolType == ToolType.AXE) addExemption(player, exemptBreakMany);
else addExemption(player, exemptBreakNormal);
@Override }
public final boolean blockBreakLowest(final Player player) {
// System.out.println("block break lowest"); @Override
final boolean isAxe = getToolProps(player.getItemInHand()).toolType == ToolType.AXE; public final boolean blockBreakLowest(final Player player) {
if (breakCancel > 0){ // System.out.println("block break lowest");
breakCancel ++; final boolean isAxe = getToolProps(Bridge1_9.getItemInMainHand(player)).toolType == ToolType.AXE;
return true; if (breakCancel > 0){
} breakCancel ++;
final String name = player.getName(); return true;
ActionFrequency freq = lastBreak.get(name); }
final long now = System.currentTimeMillis(); final String name = player.getName();
if (freq == null){ ActionFrequency freq = lastBreak.get(name);
freq = new ActionFrequency(3, 333); final long now = System.currentTimeMillis();
freq.add(now, 1f); if (freq == null){
lastBreak.put(name, freq); freq = new ActionFrequency(3, 333);
lastBreakAddCount ++; freq.add(now, 1f);
if (lastBreakAddCount > 100){ lastBreak.put(name, freq);
lastBreakAddCount = 0; lastBreakAddCount ++;
cleanupLastBreaks(); if (lastBreakAddCount > 100){
} lastBreakAddCount = 0;
} cleanupLastBreaks();
else if (!isAxe){ }
freq.add(now, 1f); }
if (freq.score(1f) > (float) clicksPerSecond){ else if (!isAxe){
breakCancel ++; freq.add(now, 1f);
return true; if (freq.score(1f) > (float) clicksPerSecond){
} breakCancel ++;
} return true;
}
addExemption(player, exemptBreakNormal); }
if (useInstaBreakHook){
HookInstaBreak.addExemptNext(exemptBreakNormal); addExemption(player, exemptBreakNormal);
TickTask2.addUnexemptions(player, exemptBreakNormal); if (useInstaBreakHook){
} HookInstaBreak.addExemptNext(exemptBreakNormal);
else if (!isAxe){ TickTask2.addUnexemptions(player, exemptBreakNormal);
setPlayer(player, cancelChecksBlockBreak); }
Bukkit.getScheduler().scheduleSyncDelayedTask(CompatNoCheatPlus.getInstance(), new Runnable() { else if (!isAxe){
@Override setPlayer(player, cancelChecksBlockBreak);
public void run() { Folia.runSyncTask(CompatNoCheatPlus.getInstance(), (arg) -> DataManager.removeData(player.getName(), CheckType.BLOCKBREAK_FASTBREAK));
DataManager.removeData(player.getName(), CheckType.BLOCKBREAK_FASTBREAK); }
} return false;
}); }
}
return false; protected void cleanupLastBreaks() {
} final long ts = System.currentTimeMillis();
if (ts - lastBreakCleanup < 30000 && ts > lastBreakCleanup) return;
protected void cleanupLastBreaks() { lastBreakCleanup = ts;
final long ts = System.currentTimeMillis(); final List<String> rem = new LinkedList<String>();
if (ts - lastBreakCleanup < 30000 && ts > lastBreakCleanup) return; if (ts >= lastBreakCleanup){
lastBreakCleanup = ts; for (final Entry<String, ActionFrequency> entry : lastBreak.entrySet()){
final List<String> rem = new LinkedList<String>(); if (entry.getValue().score(1f) == 0f) rem.add(entry.getKey());
if (ts >= lastBreakCleanup){ }
for (final Entry<String, ActionFrequency> entry : lastBreak.entrySet()){ }
if (entry.getValue().score(1f) == 0f) rem.add(entry.getKey()); else{
} rem.addAll(lastBreak.keySet());
} }
else{ for (final String key :rem){
rem.addAll(lastBreak.keySet()); lastBreak.remove(key);
} }
for (final String key :rem){ }
lastBreak.remove(key);
} @Override
} public void damageMonitor(Player player) {
// System.out.println("damage monitor");
@Override removeExemption(player, exemptFightEffect);
public void damageMonitor(Player player) { }
// System.out.println("damage monitor");
removeExemption(player, exemptFightEffect); @Override
} public void blockDamageMonitor(Player player) {
// System.out.println("block damage monitor");
@Override if (getToolProps(player.getItemInHand()).toolType == ToolType.AXE) addExemption(player, exemptBreakMany);
public void blockDamageMonitor(Player player) { else removeExemption(player, exemptBreakNormal);
// System.out.println("block damage monitor"); }
if (getToolProps(player.getItemInHand()).toolType == ToolType.AXE) addExemption(player, exemptBreakMany);
else removeExemption(player, exemptBreakNormal); @Override
} public void blockBreakMontitor(Player player) {
if (breakCancel > 0){
@Override breakCancel --;
public void blockBreakMontitor(Player player) { return;
if (breakCancel > 0){ }
breakCancel --; // System.out.println("block break monitor");
return; removeExemption(player, exemptBreakNormal);
} }
// System.out.println("block break monitor");
removeExemption(player, exemptBreakNormal);
}
}
}

View File

@ -0,0 +1,178 @@
package me.asofold.bpl.cncp.hooks.mcmmo;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.CompatConfigFactory;
import me.asofold.bpl.cncp.config.compatlayer.ConfigUtil;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.generic.ConfigurableHook;
import me.asofold.bpl.cncp.utils.PluginGetter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
public final class HookmcMMO extends AbstractHook implements Listener, ConfigurableHook {
/**
* To let the listener access this.
* @author mc_dev
*
*/
public static interface HookFacade{
public void damageLowest(Player player);
public void damageMonitor(Player player);
public void blockDamageLowest(Player player);
public void blockDamageMonitor(Player player);
/**
* If to cancel the event.
* @param player
* @return
*/
public boolean blockBreakLowest(Player player);
public void blockBreakMontitor(Player player);
}
protected HookFacade ncpHook = null;
protected boolean enabled = true;
protected String configPrefix = "mcmmo.";
protected boolean useInstaBreakHook = true;
public HookmcMMO(){
assertPluginPresent("mcMMO");
}
protected final PluginGetter<mcMMO> fetch = new PluginGetter<mcMMO>("mcMMO");
protected int blocksPerSecond = 30;
@Override
public String getHookName() {
return "mcMMO(default)";
}
@Override
public String getHookVersion() {
return "2.1";
}
@Override
public CheckType[] getCheckTypes() {
return new CheckType[]{
CheckType.BLOCKBREAK_FASTBREAK, CheckType.BLOCKBREAK_NOSWING, // old ones
// CheckType.BLOCKBREAK_DIRECTION, CheckType.BLOCKBREAK_FREQUENCY,
// CheckType.BLOCKBREAK_WRONGBLOCK, CheckType.BLOCKBREAK_REACH,
//
// CheckType.FIGHT_ANGLE, CheckType.FIGHT_SPEED, // old ones
//
// CheckType.FIGHT_DIRECTION, CheckType.FIGHT_NOSWING,
// CheckType.FIGHT_REACH,
};
}
@Override
public Listener[] getListeners() {
fetch.fetchPlugin();
return new Listener[]{this, fetch};
}
@Override
public NCPHook getNCPHook() {
if (ncpHook == null){
ncpHook = new HookFacadeImpl(useInstaBreakHook, blocksPerSecond);
}
return (NCPHook) ncpHook;
}
///////////////////////////
// Damage (fight)
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
final void onDamageLowest(final FakeEntityDamageByEntityEvent event){
final Entity entity = event.getDamager();
if (entity instanceof Player)
ncpHook.damageLowest((Player) entity);
}
@EventHandler(priority=EventPriority.MONITOR)
final void onDamageMonitor(final FakeEntityDamageByEntityEvent event){
final Entity entity = event.getDamager();
if (entity instanceof Player)
ncpHook.damageMonitor((Player) entity);
}
///////////////////////////
// Block damage
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
final void onBlockDamageLowest(final FakeBlockDamageEvent event){
ncpHook.blockDamageLowest(event.getPlayer());
}
@EventHandler(priority=EventPriority.LOWEST)
final void onBlockDamageMonitor(final FakeBlockDamageEvent event){
ncpHook.blockDamageMonitor(event.getPlayer());
}
///////////////////////////
// Block break
//////////////////////////
@EventHandler(priority=EventPriority.LOWEST)
final void onBlockBreakLowest(final FakeBlockBreakEvent event){
if (ncpHook.blockBreakLowest(event.getPlayer())){
event.setCancelled(true);
// System.out.println("Cancelled for frequency.");
}
}
@EventHandler(priority=EventPriority.MONITOR)
final void onBlockBreakLMonitor(final FakeBlockBreakEvent event){
ncpHook.blockBreakMontitor(event.getPlayer());
}
/////////////////////////////////
// Config
/////////////////////////////////
@Override
public void applyConfig(CompatConfig cfg, String prefix) {
enabled = cfg.getBoolean(prefix + configPrefix + "enabled", true);
useInstaBreakHook = cfg.getBoolean(prefix + configPrefix + "use-insta-break-hook", true);
blocksPerSecond = cfg.getInt(prefix + configPrefix + "clickspersecond", 20);
}
@Override
public boolean updateConfig(CompatConfig cfg, String prefix) {
CompatConfig defaults = CompatConfigFactory.getConfig(null);
defaults.set(prefix + configPrefix + "enabled", true);
defaults.set(prefix + configPrefix + "use-insta-break-hook", true);
defaults.set(prefix + configPrefix + "clickspersecond", 20);
return ConfigUtil.forceDefaults(defaults, cfg);
}
@Override
public boolean isEnabled() {
return enabled;
}
}

View File

@ -1,290 +1,290 @@
package me.asofold.bpl.cncp.utils; package me.asofold.bpl.cncp.utils;
/** /**
* Keep track of frequency of some action, * Keep track of frequency of some action,
* put weights into buckets, which represent intervals of time. <br> * put weights into buckets, which represent intervals of time. <br>
* (Taken from NoCheatPlus.) * (Taken from NoCheatPlus.)
* @author mc_dev * @author mc_dev
* *
*/ */
public class ActionFrequency { public class ActionFrequency {
/** Reference time for the transition from the first to the second bucket. */ /** Reference time for the transition from the first to the second bucket. */
private long time = 0; private long time = 0;
/** /**
* Time of last update (add). Should be the "time of the last event" for the * Time of last update (add). Should be the "time of the last event" for the
* usual case. * usual case.
*/ */
private long lastUpdate = 0; private long lastUpdate = 0;
private final boolean noAutoReset; private final boolean noAutoReset;
/** /**
* Buckets to fill weights in, each represents an interval of durBucket duration, * Buckets to fill weights in, each represents an interval of durBucket duration,
* index 0 is the latest, highest index is the oldest. * index 0 is the latest, highest index is the oldest.
* Weights will get filled into the next buckets with time passed. * Weights will get filled into the next buckets with time passed.
*/ */
private final float[] buckets; private final float[] buckets;
/** Duration in milliseconds that one bucket covers. */ /** Duration in milliseconds that one bucket covers. */
private final long durBucket; private final long durBucket;
/** /**
* This constructor will set noAutoReset to false, optimized for short term * This constructor will set noAutoReset to false, optimized for short term
* accounting. * accounting.
* *
* @param nBuckets * @param nBuckets
* @param durBucket * @param durBucket
*/ */
public ActionFrequency(final int nBuckets, final long durBucket) { public ActionFrequency(final int nBuckets, final long durBucket) {
this(nBuckets, durBucket, false); this(nBuckets, durBucket, false);
} }
/** /**
* *
* @param nBuckets * @param nBuckets
* @param durBucket * @param durBucket
* @param noAutoReset * @param noAutoReset
* Set to true, to prevent auto-resetting with * Set to true, to prevent auto-resetting with
* "time ran backwards". Setting this to true is recommended if * "time ran backwards". Setting this to true is recommended if
* larger time frames are monitored, to prevent data loss. * larger time frames are monitored, to prevent data loss.
*/ */
public ActionFrequency(final int nBuckets, final long durBucket, final boolean noAutoReset) { public ActionFrequency(final int nBuckets, final long durBucket, final boolean noAutoReset) {
this.buckets = new float[nBuckets]; this.buckets = new float[nBuckets];
this.durBucket = durBucket; this.durBucket = durBucket;
this.noAutoReset = noAutoReset; this.noAutoReset = noAutoReset;
} }
/** /**
* Update and add (updates reference and update time). * Update and add (updates reference and update time).
* @param now * @param now
* @param amount * @param amount
*/ */
public final void add(final long now, final float amount) { public final void add(final long now, final float amount) {
update(now); update(now);
buckets[0] += amount; buckets[0] += amount;
} }
/** /**
* Unchecked addition of amount to the first bucket. * Unchecked addition of amount to the first bucket.
* @param amount * @param amount
*/ */
public final void add(final float amount) { public final void add(final float amount) {
buckets[0] += amount; buckets[0] += amount;
} }
/** /**
* Update without adding, also updates reference and update time. Detects * Update without adding, also updates reference and update time. Detects
* time running backwards. * time running backwards.
* *
* @param now * @param now
*/ */
public final void update(final long now) { public final void update(final long now) {
final long diff = now - time; final long diff = now - time;
if (now < lastUpdate) { if (now < lastUpdate) {
// Time ran backwards. // Time ran backwards.
if (noAutoReset) { if (noAutoReset) {
// Only update time and lastUpdate. // Only update time and lastUpdate.
time = lastUpdate = now; time = lastUpdate = now;
} else { } else {
// Clear all. // Clear all.
clear(now); clear(now);
return; return;
} }
} }
else if (diff >= durBucket * buckets.length) { else if (diff >= durBucket * buckets.length) {
// Clear (beyond range). // Clear (beyond range).
clear(now); clear(now);
return; return;
} }
else if (diff < durBucket) { else if (diff < durBucket) {
// No changes (first bucket). // No changes (first bucket).
} }
else { else {
final int shift = (int) ((float) diff / (float) durBucket); final int shift = (int) ((float) diff / (float) durBucket);
// Update buckets. // Update buckets.
for (int i = 0; i < buckets.length - shift; i++) { for (int i = 0; i < buckets.length - shift; i++) {
buckets[buckets.length - (i + 1)] = buckets[buckets.length - (i + 1 + shift)]; buckets[buckets.length - (i + 1)] = buckets[buckets.length - (i + 1 + shift)];
} }
for (int i = 0; i < shift; i++) { for (int i = 0; i < shift; i++) {
buckets[i] = 0; buckets[i] = 0;
} }
// Set time according to bucket duration (!). // Set time according to bucket duration (!).
time += durBucket * shift; time += durBucket * shift;
} }
// Ensure lastUpdate is set. // Ensure lastUpdate is set.
lastUpdate = now; lastUpdate = now;
} }
/** /**
* Clear all counts, reset reference and update time. * Clear all counts, reset reference and update time.
* @param now * @param now
*/ */
public final void clear(final long now) { public final void clear(final long now) {
for (int i = 0; i < buckets.length; i++) { for (int i = 0; i < buckets.length; i++) {
buckets[i] = 0f; buckets[i] = 0f;
} }
time = lastUpdate = now; time = lastUpdate = now;
} }
/** /**
* @deprecated Use instead: score(float). * @deprecated Use instead: score(float).
* @param factor * @param factor
* @return * @return
*/ */
public final float getScore(final float factor) { public final float getScore(final float factor) {
return score(factor); return score(factor);
} }
/** /**
* @deprecated Use instead: score(float). * @deprecated Use instead: score(float).
* @param factor * @param factor
* @return * @return
*/ */
public final float getScore(final int bucket) { public final float getScore(final int bucket) {
return bucketScore(bucket); return bucketScore(bucket);
} }
/** /**
* Get a weighted sum score, weight for bucket i: w(i) = factor^i. * Get a weighted sum score, weight for bucket i: w(i) = factor^i.
* @param factor * @param factor
* @return * @return
*/ */
public final float score(final float factor) { public final float score(final float factor) {
return sliceScore(0, buckets.length, factor); return sliceScore(0, buckets.length, factor);
} }
/** /**
* Get score of a certain bucket. At own risk. * Get score of a certain bucket. At own risk.
* @param bucket * @param bucket
* @return * @return
*/ */
public final float bucketScore(final int bucket) { public final float bucketScore(final int bucket) {
return buckets[bucket]; return buckets[bucket];
} }
/** /**
* Get score of first end buckets, with factor. * Get score of first end buckets, with factor.
* @param end Number of buckets including start. The end is not included. * @param end Number of buckets including start. The end is not included.
* @param factor * @param factor
* @return * @return
*/ */
public final float leadingScore(final int end, float factor) { public final float leadingScore(final int end, float factor) {
return sliceScore(0, end, factor); return sliceScore(0, end, factor);
} }
/** /**
* Get score from start on, with factor. * Get score from start on, with factor.
* @param start This is included. * @param start This is included.
* @param factor * @param factor
* @return * @return
*/ */
public final float trailingScore(final int start, float factor) { public final float trailingScore(final int start, float factor) {
return sliceScore(start, buckets.length, factor); return sliceScore(start, buckets.length, factor);
} }
/** /**
* Get score from start on, until before end, with factor. * Get score from start on, until before end, with factor.
* @param start This is included. * @param start This is included.
* @param end This is not included. * @param end This is not included.
* @param factor * @param factor
* @return * @return
*/ */
public final float sliceScore(final int start, final int end, float factor) { public final float sliceScore(final int start, final int end, float factor) {
float score = buckets[start]; float score = buckets[start];
float cf = factor; float cf = factor;
for (int i = start + 1; i < end; i++) { for (int i = start + 1; i < end; i++) {
score += buckets[i] * cf; score += buckets[i] * cf;
cf *= factor; cf *= factor;
} }
return score; return score;
} }
/** /**
* Set the value for a buckt. * Set the value for a buckt.
* @param n * @param n
* @param value * @param value
*/ */
public final void setBucket(final int n, final float value) { public final void setBucket(final int n, final float value) {
buckets[n] = value; buckets[n] = value;
} }
/** /**
* Set the reference time and last update time. * Set the reference time and last update time.
* @param time * @param time
*/ */
public final void setTime(final long time) { public final void setTime(final long time) {
this.time = time; this.time = time;
this.lastUpdate = time; this.lastUpdate = time;
} }
/** /**
* Get the reference time for the transition from the first to the second bucket. * Get the reference time for the transition from the first to the second bucket.
* @return * @return
*/ */
public final long lastAccess() { // TODO: Should rename this. public final long lastAccess() { // TODO: Should rename this.
return time; return time;
} }
/** /**
* Get the last time when update was called (adding). * Get the last time when update was called (adding).
* @return * @return
*/ */
public final long lastUpdate() { public final long lastUpdate() {
return lastUpdate; return lastUpdate;
} }
/** /**
* Get the number of buckets. * Get the number of buckets.
* @return * @return
*/ */
public final int numberOfBuckets() { public final int numberOfBuckets() {
return buckets.length; return buckets.length;
} }
/** /**
* Get the duration of a bucket in milliseconds. * Get the duration of a bucket in milliseconds.
* @return * @return
*/ */
public final long bucketDuration() { public final long bucketDuration() {
return durBucket; return durBucket;
} }
/** /**
* Serialize to a String line. * Serialize to a String line.
* @return * @return
*/ */
public final String toLine() { public final String toLine() {
// TODO: Backwards-compatible lastUpdate ? // TODO: Backwards-compatible lastUpdate ?
final StringBuilder buffer = new StringBuilder(50); final StringBuilder buffer = new StringBuilder(50);
buffer.append(buckets.length + ","+durBucket+","+time); buffer.append(buckets.length + ","+durBucket+","+time);
for (int i = 0; i < buckets.length; i++) { for (int i = 0; i < buckets.length; i++) {
buffer.append("," + buckets[i]); buffer.append("," + buckets[i]);
} }
return buffer.toString(); return buffer.toString();
} }
/** /**
* Deserialize from a string. * Deserialize from a string.
* @param line * @param line
* @return * @return
*/ */
public static ActionFrequency fromLine(final String line) { public static ActionFrequency fromLine(final String line) {
// TODO: Backwards-compatible lastUpdate ? // TODO: Backwards-compatible lastUpdate ?
String[] split = line.split(","); String[] split = line.split(",");
if (split.length < 3) throw new RuntimeException("Bad argument length."); // TODO if (split.length < 3) throw new RuntimeException("Bad argument length."); // TODO
final int n = Integer.parseInt(split[0]); final int n = Integer.parseInt(split[0]);
final long durBucket = Long.parseLong(split[1]); final long durBucket = Long.parseLong(split[1]);
final long time = Long.parseLong(split[2]); final long time = Long.parseLong(split[2]);
final float[] buckets = new float[split.length -3]; final float[] buckets = new float[split.length -3];
if (split.length - 3 != buckets.length) throw new RuntimeException("Bad argument length."); // TODO if (split.length - 3 != buckets.length) throw new RuntimeException("Bad argument length."); // TODO
for (int i = 3; i < split.length; i ++) { for (int i = 3; i < split.length; i ++) {
buckets[i - 3] = Float.parseFloat(split[i]); buckets[i - 3] = Float.parseFloat(split[i]);
} }
ActionFrequency freq = new ActionFrequency(n, durBucket); ActionFrequency freq = new ActionFrequency(n, durBucket);
freq.setTime(time); freq.setTime(time);
for (int i = 0; i < buckets.length; i ++) { for (int i = 0; i < buckets.length; i ++) {
freq.setBucket(i, buckets[i]); freq.setBucket(i, buckets[i]);
} }
return freq; return freq;
} }
} }

View File

@ -1,50 +1,50 @@
package me.asofold.bpl.cncp.utils; package me.asofold.bpl.cncp.utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
/** /**
* Simple plugin fetching. * Simple plugin fetching.
* @author mc_dev * @author mc_dev
* *
* @param <T> * @param <T>
*/ */
public final class PluginGetter<T extends Plugin> implements Listener{ public final class PluginGetter<T extends Plugin> implements Listener{
private T plugin = null; private T plugin = null;
private final String pluginName; private final String pluginName;
public PluginGetter(final String pluginName){ public PluginGetter(final String pluginName){
this.pluginName = pluginName; this.pluginName = pluginName;
fetchPlugin(); fetchPlugin();
} }
/** /**
* Fetch from Bukkit and set , might set to null, though. * Fetch from Bukkit and set , might set to null, though.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final void fetchPlugin() { public final void fetchPlugin() {
final Plugin ref = Bukkit.getPluginManager().getPlugin(pluginName); final Plugin ref = Bukkit.getPluginManager().getPlugin(pluginName);
plugin = (T) ref; plugin = (T) ref;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final void onPluginEnable(final PluginEnableEvent event){ final void onPluginEnable(final PluginEnableEvent event){
final Plugin ref = event.getPlugin(); final Plugin ref = event.getPlugin();
if (plugin.getName().equals(pluginName)) plugin = (T) ref; if (plugin.getName().equals(pluginName)) plugin = (T) ref;
} }
/** /**
* For convenience with chaining: getX = new PluginGetter<X>("X").registerEvents(this); * For convenience with chaining: getX = new PluginGetter<X>("X").registerEvents(this);
* @param other * @param other
* @return * @return
*/ */
public final PluginGetter<T> registerEvents(final Plugin other){ public final PluginGetter<T> registerEvents(final Plugin other){
Bukkit.getPluginManager().registerEvents(this, plugin); Bukkit.getPluginManager().registerEvents(this, plugin);
return this; return this;
} }
public final T getPlugin(){ public final T getPlugin(){
return plugin; return plugin;
} }
} }

View File

@ -1,48 +1,48 @@
package me.asofold.bpl.cncp.utils; package me.asofold.bpl.cncp.utils;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
public class TickTask2 implements Runnable { public class TickTask2 implements Runnable {
protected static Map<CheckType, Set<Player>> exemptions = new LinkedHashMap<CheckType, Set<Player>>(40); protected static Map<CheckType, Set<Player>> exemptions = new LinkedHashMap<CheckType, Set<Player>>(40);
/** /**
* Quick fix, meant for sync access (!). * Quick fix, meant for sync access (!).
* @param player * @param player
* @param checkTypes * @param checkTypes
*/ */
public static void addUnexemptions(final Player player, final CheckType[] checkTypes){ public static void addUnexemptions(final Player player, final CheckType[] checkTypes){
for (int i = 0; i < checkTypes.length; i ++){ for (int i = 0; i < checkTypes.length; i ++){
final CheckType type = checkTypes[i]; final CheckType type = checkTypes[i];
Set<Player> set = exemptions.get(type); Set<Player> set = exemptions.get(type);
if (set == null){ if (set == null){
set = new HashSet<Player>(); set = new HashSet<Player>();
exemptions.put(type, set); exemptions.put(type, set);
} }
set.add(player); set.add(player);
} }
} }
@Override @Override
public void run() { public void run() {
for (final Entry<CheckType, Set<Player>> entry : exemptions.entrySet()){ for (final Entry<CheckType, Set<Player>> entry : exemptions.entrySet()){
final Set<Player> set = entry.getValue(); final Set<Player> set = entry.getValue();
final CheckType type = entry.getKey(); final CheckType type = entry.getKey();
for (final Player player : set){ for (final Player player : set){
NCPExemptionManager.unexempt(player, type); NCPExemptionManager.unexempt(player, type);
} }
} }
exemptions.clear(); exemptions.clear();
} }
} }

View File

@ -1,16 +1,16 @@
package me.asofold.bpl.cncp.utils; package me.asofold.bpl.cncp.utils;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
public class Utils { public class Utils {
public static final String toString(final Throwable t) { public static final String toString(final Throwable t) {
final Writer buf = new StringWriter(500); final Writer buf = new StringWriter(500);
final PrintWriter writer = new PrintWriter(buf); final PrintWriter writer = new PrintWriter(buf);
t.printStackTrace(writer); t.printStackTrace(writer);
return buf.toString(); return buf.toString();
} }
} }