use of org.cubeengine.libcube.service.task.Task in project modules-extra by CubeEngine.
the class FlyListener method playerInteract.
@Listener
public void playerInteract(final InteractBlockEvent.Secondary event, @First Player player) {
if (!player.getItemInHand(HandTypes.MAIN_HAND).map(i -> i.getType().equals(ItemTypes.FEATHER)).orElse(false)) {
return;
}
if (!player.hasPermission(FLY_FEATHER.getId())) {
i18n.send(player, NEGATIVE, "You dont have permission to use this!");
// Disable when player is flying
player.offer(Keys.CAN_FLY, false);
player.offer(Keys.IS_FLYING, false);
return;
}
// I Believe I Can Fly ...
player.offer(Keys.CAN_FLY, !player.get(Keys.CAN_FLY).get());
if (player.get(Keys.CAN_FLY).get()) {
final ItemStack feather = ItemStack.of(ItemTypes.FEATHER, -1);
player.getInventory().query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(feather)).poll(1);
player.setVelocity(player.getVelocity().add(0, 1, 0));
// make sure the player stays flying
player.setLocation(player.getLocation().add(0, 0.05, 0));
player.offer(Keys.IS_FLYING, true);
i18n.send(player, POSITIVE, "You can now fly!");
Task flymore = new Task(Fly.class, tm) {
public // 2 feather/min
void run() {
if (!player.get(Keys.IS_FLYING).get()) {
player.offer(Keys.CAN_FLY, false);
this.cancelTask();
return;
}
if (player.getInventory().contains(ItemTypes.FEATHER)) {
player.getInventory().query(feather).poll(1);
} else {
player.offer(Keys.CAN_FLY, false);
this.cancelTask();
}
}
};
flymore.scheduleAsyncRepeatingTask(1000 * 30, 1000 * 30);
Task oldTask = this.tasks.put(player, flymore);
if (oldTask != null) {
oldTask.cancelTask();
}
} else {
// or not
player.offer(Keys.FALL_DISTANCE, 0f);
i18n.send(player, NEUTRAL, "You cannot fly anymore!");
}
}