[BLIND] Reset block break timing with changing the tool.

This commit is contained in:
asofold 2014-02-03 00:25:10 +01:00
parent 165af2ccaf
commit 51172bdc79
2 changed files with 67 additions and 10 deletions

View File

@ -3,7 +3,10 @@ package fr.neatmonster.nocheatplus.checks.blockbreak;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import fr.neatmonster.nocheatplus.checks.access.ACheckData;
import fr.neatmonster.nocheatplus.checks.access.CheckDataFactory;
@ -84,10 +87,12 @@ public class BlockBreakData extends ACheckData {
public final ActionFrequency wrongBlockVL;
// Shared data.
public int clickedX;
public int clickedX = Integer.MAX_VALUE;
public int clickedY;
public int clickedZ;
public int clickedTick;
/** Tool that the block was clicked with, null for the case of air. */
public Material clickedTool = null;
// TODO: use tick here too ?
public long wasInstaBreak;
@ -118,5 +123,44 @@ public class BlockBreakData extends ACheckData {
frequencyBuckets = new ActionFrequency(cc.frequencyBuckets, cc.frequencyBucketDur);
wrongBlockVL = new ActionFrequency(6, 20000);
}
/**
* Meant to record the first click/damage on a block (not subsequent clicking), forces internals update.
* @param block
* @param tick
* @param now
* @param mat
*/
public void setClickedBlock(Block block, int tick, long now, Material tool) {
fastBreakfirstDamage = now;
// Also set last clicked blocks position.
clickedX = block.getX();
clickedY = block.getY();
clickedZ = block.getZ();
clickedTick = tick;
clickedTool = tool == Material.AIR ? null : tool;
}
/**
* Reset clicked block (as if not clicked anything before).
*/
public void resetClickedBlock() {
clickedX = Integer.MAX_VALUE;
clickedTick = 0;
fastBreakfirstDamage = 0;
clickedTool = null;
}
public boolean toolChanged(ItemStack stack) {
return toolChanged(stack == null ? null: stack.getType());
}
public boolean toolChanged(Material mat) {
if (mat == Material.AIR) {
return clickedTool != null;
} else {
return clickedTool != mat;
}
}
}

View File

@ -1,6 +1,7 @@
package fr.neatmonster.nocheatplus.checks.blockbreak;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
@ -11,6 +12,8 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.ItemStack;
import fr.neatmonster.nocheatplus.checks.CheckListener;
import fr.neatmonster.nocheatplus.checks.CheckType;
@ -245,19 +248,29 @@ public class BlockBreakListener extends CheckListener {
final int tick = TickTask.getTick();
// Skip if already set to the same block without breaking within one tick difference.
if (tick < data.clickedTick);
else if (data.fastBreakBreakTime < data.fastBreakfirstDamage && data.clickedX == block.getX() && data.clickedZ == block.getZ() && data.clickedY == block.getY()){
final ItemStack stack = player.getItemInHand();
final Material tool = stack == null ? null: stack.getType();
if (data.toolChanged(tool)) {
// Update.
} else if (tick < data.clickedTick) {
// Update.
} else if (data.fastBreakBreakTime < data.fastBreakfirstDamage && data.clickedX == block.getX() && data.clickedZ == block.getZ() && data.clickedY == block.getY()){
if (tick - data.clickedTick <= 1 ) return;
}
// (Always set, the interact event only fires once: the first time.)
// Only record first damage:
data.fastBreakfirstDamage = now;
// Also set last clicked blocks position.
data.clickedX = block.getX();
data.clickedY = block.getY();
data.clickedZ = block.getZ();
data.clickedTick = tick;
data.setClickedBlock(block, tick, now, tool);
}
@EventHandler(ignoreCancelled = false, priority = EventPriority.MONITOR)
public void onItemHeld(final PlayerItemHeldEvent event) {
// Reset clicked block.
// TODO: Not for 1.5.2 and before?
final Player player = event.getPlayer();
final BlockBreakData data = BlockBreakData.getData(player);
if (data.toolChanged(player.getInventory().getItem(event.getNewSlot()))) {
data.resetClickedBlock();
}
}
}