Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created February 21, 2018 18:29
Show Gist options
  • Save Exerosis/1a23de01faebb605d75f67cd0b38a67d to your computer and use it in GitHub Desktop.
Save Exerosis/1a23de01faebb605d75f67cd0b38a67d to your computer and use it in GitHub Desktop.
public class SmoothBlockInterrupt {
private final Map<Player, BukkitTask> tasks = new HashMap<>();
public SmoothBlockInterrupt(Plugin plugin, BiConsumer<Block, Player> interruptions) {
getProtocolManager().addPacketListener(new PacketAdapter(plugin, BLOCK_DIG) {
@Override
public void onPacketReceiving(PacketEvent event) {
Player player = event.getPlayer();
Block block = event.getPacket()
.getBlockPositionModifier().read(0)
.toLocation(player.getWorld()).getBlock();
switch (event.getPacket().getPlayerDigTypes().getValues().get(0)) {
case START_DESTROY_BLOCK: {
tasks.compute(player, ($, task) -> {
if (task != null)
task.cancel();
return getScheduler().runTaskLater(plugin, () -> {
interruptions.accept(block, player);
tasks.remove(player);
}, getBreakTime(player, block));
});
break;
}
case ABORT_DESTROY_BLOCK:
case STOP_DESTROY_BLOCK: {
BukkitTask remove = tasks.remove(player);
if (remove != null)
remove.cancel();
}
}
}
});
}
protected long getBreakTime(Player player, Block block) {
return 100; //TODO fill this in or a find a way to pull from nms.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment