Skip to content

Commit

Permalink
[BLIND] Reset block break timing with changing the tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Feb 2, 2014
1 parent 165af2c commit 51172bd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
Expand Up @@ -3,7 +3,10 @@
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;
Expand Down Expand Up @@ -84,10 +87,12 @@ public static void clear(){
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;
Expand Down Expand Up @@ -118,5 +123,44 @@ public BlockBreakData(final BlockBreakConfig cc) {
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;
}
}

}
@@ -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;
Expand All @@ -11,6 +12,8 @@
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;
Expand Down Expand Up @@ -245,19 +248,29 @@ private void checkBlockDamage(final Player player, final Block block, final Canc

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();
}
}

}

0 comments on commit 51172bd

Please sign in to comment.