use of net.countercraft.movecraft.craft.CruiseOnPilotCraft in project Movecraft by APDevTeam.
the class CraftSign method onSignClick.
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null)
return;
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign))
return;
Sign sign = (Sign) state;
CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0)));
if (craftType == null)
return;
// Valid sign prompt for ship command.
Player player = event.getPlayer();
if (!player.hasPermission("movecraft." + ChatColor.stripColor(sign.getLine(0)) + ".pilot")) {
player.sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return;
}
Location loc = event.getClickedBlock().getLocation();
MovecraftLocation startPoint = new MovecraftLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
if (piloting.contains(startPoint)) {
event.setCancelled(true);
return;
}
// Attempt to run detection
World world = event.getClickedBlock().getWorld();
CraftManager.getInstance().detect(startPoint, craftType, (type, w, p, parents) -> {
// Note: This only passes in a non-null player.
assert p != null;
if (type.getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
if (parents.size() > 1)
return new Pair<>(Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Failed - Already commanding a craft")), null);
if (parents.size() == 1) {
Craft parent = parents.iterator().next();
return new Pair<>(Result.succeed(), new CruiseOnPilotSubCraft(type, world, p, parent));
}
return new Pair<>(Result.succeed(), new CruiseOnPilotCraft(type, world, p));
} else {
if (parents.size() > 0)
return new Pair<>(Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Failed - Already commanding a craft")), null);
return new Pair<>(Result.succeed(), new PlayerCraftImpl(type, w, p));
}
}, world, player, Movecraft.getAdventure().player(player), craft -> () -> {
Bukkit.getServer().getPluginManager().callEvent(new CraftPilotEvent(craft, CraftPilotEvent.Reason.PLAYER));
if (craft instanceof SubCraft) {
// Subtract craft from the parent
Craft parent = ((SubCraft) craft).getParent();
var newHitbox = parent.getHitBox().difference(craft.getHitBox());
;
parent.setHitBox(newHitbox);
parent.setOrigBlockCount(parent.getOrigBlockCount() - craft.getHitBox().size());
}
if (craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
// Setup cruise direction
if (sign.getBlockData() instanceof WallSign)
craft.setCruiseDirection(CruiseDirection.fromBlockFace(((WallSign) sign.getBlockData()).getFacing()));
else
craft.setCruiseDirection(CruiseDirection.NONE);
// Start craft cruising
craft.setLastCruiseUpdate(System.currentTimeMillis());
craft.setCruising(true);
// Stop craft cruising and sink it in 15 seconds
new BukkitRunnable() {
@Override
public void run() {
craft.setCruising(false);
CraftManager.getInstance().sink(craft);
}
}.runTaskLater(Movecraft.getInstance(), (20 * 15));
} else {
// Release old craft if it exists
Craft oldCraft = CraftManager.getInstance().getCraftByPlayer(player);
if (oldCraft != null)
CraftManager.getInstance().release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false);
}
});
event.setCancelled(true);
new BukkitRunnable() {
@Override
public void run() {
piloting.remove(startPoint);
}
}.runTaskLater(Movecraft.getInstance(), 4);
}
Aggregations