[BLEEDING/INCOMPLETE] Start sketch for more inventory-open checking.

This commit is contained in:
asofold 2013-09-20 09:37:08 +02:00
parent 41e1e8017b
commit 2417ba9ffe
8 changed files with 152 additions and 37 deletions

View File

@ -97,6 +97,7 @@ public enum CheckType {
INVENTORY_INSTANTBOW(INVENTORY, Permissions.INVENTORY_INSTANTBOW), INVENTORY_INSTANTBOW(INVENTORY, Permissions.INVENTORY_INSTANTBOW),
INVENTORY_INSTANTEAT(INVENTORY, Permissions.INVENTORY_INSTANTEAT), INVENTORY_INSTANTEAT(INVENTORY, Permissions.INVENTORY_INSTANTEAT),
INVENTORY_ITEMS(INVENTORY, Permissions.INVENTORY_ITEMS), INVENTORY_ITEMS(INVENTORY, Permissions.INVENTORY_ITEMS),
INVENTORY_OPEN(INVENTORY, Permissions.INVENTORY_OPEN),
MOVING(MovingConfig.factory, MovingData.factory, Permissions.MOVING), MOVING(MovingConfig.factory, MovingData.factory, Permissions.MOVING),
MOVING_CREATIVEFLY(MOVING, Permissions.MOVING_CREATIVEFLY), MOVING_CREATIVEFLY(MOVING, Permissions.MOVING_CREATIVEFLY),

View File

@ -103,6 +103,10 @@ public class InventoryConfig extends ACheckConfig {
public final boolean itemsCheck; public final boolean itemsCheck;
public final boolean openCheck;
public final boolean openClose;
public final boolean openCancelOther;
/** /**
* Instantiates a new inventory configuration. * Instantiates a new inventory configuration.
* *
@ -138,6 +142,10 @@ public class InventoryConfig extends ACheckConfig {
instantEatActions = data.getOptimizedActionList(ConfPaths.INVENTORY_INSTANTEAT_ACTIONS, Permissions.INVENTORY_INSTANTEAT); instantEatActions = data.getOptimizedActionList(ConfPaths.INVENTORY_INSTANTEAT_ACTIONS, Permissions.INVENTORY_INSTANTEAT);
itemsCheck = data.getBoolean(ConfPaths.INVENTORY_ITEMS_CHECK); itemsCheck = data.getBoolean(ConfPaths.INVENTORY_ITEMS_CHECK);
openCheck = data.getBoolean(ConfPaths.INVENTORY_OPEN_CHECK);
openClose = data.getBoolean(ConfPaths.INVENTORY_OPEN_CLOSE);
openCancelOther = data.getBoolean(ConfPaths.INVENTORY_OPEN_CANCELOTHER);
} }
/* /*
@ -154,6 +162,8 @@ public class InventoryConfig extends ACheckConfig {
return fastClickCheck; return fastClickCheck;
case INVENTORY_ITEMS: case INVENTORY_ITEMS:
return itemsCheck; return itemsCheck;
case INVENTORY_OPEN:
return openCheck;
case INVENTORY_DROP: case INVENTORY_DROP:
return dropCheck; return dropCheck;
case INVENTORY_INSTANTBOW: case INVENTORY_INSTANTBOW:

View File

@ -70,6 +70,8 @@ public class InventoryListener extends CheckListener {
protected final Items items = addCheck(new Items()); protected final Items items = addCheck(new Items());
private final Open open = addCheck(new Open());
public InventoryListener(){ public InventoryListener(){
super(CheckType.INVENTORY); super(CheckType.INVENTORY);
} }
@ -326,29 +328,49 @@ public class InventoryListener extends CheckListener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerChangedWorld(final PlayerChangedWorldEvent event){ public void onPlayerChangedWorld(final PlayerChangedWorldEvent event){
if (InventoryUtil.shouldEnsureCloseInventories()){ open.check(event.getPlayer());
InventoryUtil.closePlayerInventoryRecursively(event.getPlayer());
}
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerPortal(final PlayerPortalEvent event){ public void onPlayerPortal(final PlayerPortalEvent event){
if (InventoryUtil.shouldEnsureCloseInventories()){ // Note: ignore cancelother setting.
InventoryUtil.closePlayerInventoryRecursively(event.getPlayer()); open.check(event.getPlayer());
}
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onEntityPortal(final EntityPortalEnterEvent event){ public void onEntityPortal(final EntityPortalEnterEvent event){
if (InventoryUtil.shouldEnsureCloseInventories()){ final Player player = InventoryUtil.getPlayerPassengerRecursively(event.getEntity());
InventoryUtil.closePlayerInventoryRecursively(event.getEntity()); if (player != null) {
// Note: ignore cancelother setting.
open.check(player);
} }
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerTeleport(final PlayerTeleportEvent event){ public void onPlayerTeleport(final PlayerTeleportEvent event){
if (InventoryUtil.shouldEnsureCloseInventories()){ // Note: ignore cancelother setting.
InventoryUtil.closePlayerInventoryRecursively(event.getPlayer()); open.check(event.getPlayer());
}
} }
// @EventHandler(priority = EventPriority.MONITOR)
// public void onVehicleDestroy(final VehicleDestroyEvent event) {
// final Entity entity = event.getVehicle();
// if (entity instanceof InventoryHolder) { // Fail on 1.4 ?
// checkInventoryHolder((InventoryHolder) entity);
// }
// }
//
// @EventHandler(priority = EventPriority.MONITOR)
// public void onBlockBreak(final BlockBreakEvent event) {
// final Block block = event.getBlock();
// if (block == null) {
// return;
// }
// // TODO: + explosions !? + entity change block + ...
// }
//
// private void checkInventoryHolder(InventoryHolder entity) {
// // TODO Auto-generated method stub
//
// }
} }

View File

@ -0,0 +1,56 @@
package fr.neatmonster.nocheatplus.checks.inventory;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.components.DisableListener;
import fr.neatmonster.nocheatplus.utilities.InventoryUtil;
/**
* Watch over open inventories - check with "combined" static access, put here because it has too much to do with inventories.
* @author mc_dev
*
*/
public class Open extends Check implements DisableListener{
private static Open instance = null;
// TODO: Add specific contexts (allow different settings for fight / blockbreak etc.).
/**
* Static access check, if there is a cancel-action flag the caller should have stored that locally already and use the result to know if to cancel or not.
* @param player
* @return If cancelling some event is opportune (open inventory and cancel flag set).
*/
public static boolean checkClose(Player player) {
return instance.check(player);
}
public Open() {
super(CheckType.INVENTORY_OPEN);
instance = this;
}
@Override
public void onDisable() {
instance = null;
}
/**
* This check contains the isEnabled checking (!). Inventory is closed if set in the config.
* @param player
* @return If cancelling some event is opportune (open inventory and cancel flag set).
*/
public boolean check(final Player player) {
if (!isEnabled(player) || !InventoryUtil.hasInventoryOpen(player)) {
return false;
}
final InventoryConfig cc = InventoryConfig.getConfig(player);
if (cc.openClose) {
player.closeInventory();
}
return cc.openCancelOther;
}
}

View File

@ -546,11 +546,14 @@ public abstract class ConfPaths {
public static final String INVENTORY_INSTANTEAT_CHECK = INVENTORY_INSTANTEAT + "active"; public static final String INVENTORY_INSTANTEAT_CHECK = INVENTORY_INSTANTEAT + "active";
public static final String INVENTORY_INSTANTEAT_ACTIONS = INVENTORY_INSTANTEAT + "actions"; public static final String INVENTORY_INSTANTEAT_ACTIONS = INVENTORY_INSTANTEAT + "actions";
private static final String INVENTORY_ITEMS = INVENTORY + ".items"; private static final String INVENTORY_ITEMS = INVENTORY + "items.";
public static final String INVENTORY_ITEMS_CHECK = INVENTORY_ITEMS + ".active"; public static final String INVENTORY_ITEMS_CHECK = INVENTORY_ITEMS + "active";
@GlobalConfig private static final String INVENTORY_OPEN = INVENTORY + "open.";
public static final String INVENTORY_ENSURECLOSE = INVENTORY + ".ensureclose"; public static final String INVENTORY_OPEN_CHECK = INVENTORY_OPEN + "active";
// TODO: close and cancelother on open-section-level are temporary.
public static final String INVENTORY_OPEN_CLOSE = INVENTORY_OPEN + "close";
public static final String INVENTORY_OPEN_CANCELOTHER = INVENTORY_OPEN + "cancelother";
/* /*
* e e ,e, * e e ,e,
@ -681,4 +684,6 @@ public abstract class ConfPaths {
public static final String MISCELLANEOUS_OPINCONSOLEONLY = "miscellaneous.opinconsoleonly"; public static final String MISCELLANEOUS_OPINCONSOLEONLY = "miscellaneous.opinconsoleonly";
@Moved(newPath = COMPATIBILITY_MANAGELISTENERS) @Moved(newPath = COMPATIBILITY_MANAGELISTENERS)
public static final String MISCELLANEOUS_MANAGELISTENERS = "miscellaneous.managelisteners"; public static final String MISCELLANEOUS_MANAGELISTENERS = "miscellaneous.managelisteners";
@Moved(newPath = INVENTORY_OPEN_CHECK)
public static final String INVENTORY_ENSURECLOSE = "inventory.ensureclose";
} }

View File

@ -405,7 +405,9 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.INVENTORY_ITEMS_CHECK, true); set(ConfPaths.INVENTORY_ITEMS_CHECK, true);
set(ConfPaths.INVENTORY_ENSURECLOSE, true); set(ConfPaths.INVENTORY_OPEN_CHECK, true);
set(ConfPaths.INVENTORY_OPEN_CLOSE, true);
set(ConfPaths.INVENTORY_OPEN_CANCELOTHER, true);
/* /*
* e e ,e, * e e ,e,

View File

@ -174,6 +174,7 @@ public class Permissions {
public static final String INVENTORY_INSTANTBOW = INVENTORY + ".instantbow"; public static final String INVENTORY_INSTANTBOW = INVENTORY + ".instantbow";
public static final String INVENTORY_INSTANTEAT = INVENTORY + ".instanteat"; public static final String INVENTORY_INSTANTEAT = INVENTORY + ".instanteat";
public static final String INVENTORY_ITEMS = INVENTORY + ".items"; public static final String INVENTORY_ITEMS = INVENTORY + ".items";
public static final String INVENTORY_OPEN = INVENTORY + ".open";
/* /*
* e e ,e, * e e ,e,

View File

@ -7,9 +7,6 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigManager;
/** /**
* Auxiliary/convenience methods for inventories. * Auxiliary/convenience methods for inventories.
* @author mc_dev * @author mc_dev
@ -70,22 +67,29 @@ public class InventoryUtil {
} }
/** /**
* Test if global config has the flag set. * Search for players / passengers (broken by name: closes the inventory of first player found including entity and passengers recursively).
* @return * @param entity
*/ */
public static boolean shouldEnsureCloseInventories(){ public static boolean closePlayerInventoryRecursively(Entity entity){
return ConfigManager.getConfigFile().getBoolean(ConfPaths.INVENTORY_ENSURECLOSE, true); // Find a player.
final Player player = getPlayerPassengerRecursively(entity);
if (player != null && closeOpenInventory((Player) entity)) {
return true;
} else {
return false;
}
} }
/** /**
* Search for players / passengers. * Get a player from an entity. This will return the first player found amongst the entity itself and passengers checked recursively.
* @param entity * @param entity
* @return
*/ */
public static void closePlayerInventoryRecursively(Entity entity){ public static Player getPlayerPassengerRecursively(Entity entity) {
// Find a player.
while (entity != null){ while (entity != null){
if (entity instanceof Player){ if (entity instanceof Player){
closeOpenInventory((Player) entity); // Scrap the case of players riding players for the moment.
return (Player) entity;
} }
final Entity passenger = entity.getPassenger(); final Entity passenger = entity.getPassenger();
if (entity.equals(passenger)){ if (entity.equals(passenger)){
@ -96,17 +100,31 @@ public class InventoryUtil {
entity = passenger; entity = passenger;
} }
} }
return null;
} }
/** /**
* Close one players inventory, if open. This ignores InventoryType.CRAFTING. * Close one players inventory, if open. This might ignore InventoryType.CRAFTING (see: hasInventoryOpen).
* @param player * @param player
* @return If closed.
*/ */
public static void closeOpenInventory(final Player player){ public static boolean closeOpenInventory(final Player player){
final InventoryView view = player.getOpenInventory(); if (hasInventoryOpen(player)) {
if (view != null && view.getType() != InventoryType.CRAFTING) {
player.closeInventory(); player.closeInventory();
return true;
} else {
return true;
} }
} }
/**
* Check if the players inventory is open. This might ignore InventoryType.CRAFTING.
* @param player
* @return
*/
public static boolean hasInventoryOpen(final Player player) {
final InventoryView view = player.getOpenInventory();
return view != null && view.getType() != InventoryType.CRAFTING;
}
} }