Created
February 21, 2018 18:29
-
-
Save Exerosis/1a23de01faebb605d75f67cd0b38a67d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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