Search in sources :

Example 11 with Craft

use of net.countercraft.movecraft.craft.Craft 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);
}
Also used : WallSign(org.bukkit.block.data.type.WallSign) Player(org.bukkit.entity.Player) CruiseOnPilotCraft(net.countercraft.movecraft.craft.CruiseOnPilotCraft) CruiseOnPilotSubCraft(net.countercraft.movecraft.craft.CruiseOnPilotSubCraft) Craft(net.countercraft.movecraft.craft.Craft) SubCraft(net.countercraft.movecraft.craft.SubCraft) CruiseOnPilotCraft(net.countercraft.movecraft.craft.CruiseOnPilotCraft) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) World(org.bukkit.World) CraftPilotEvent(net.countercraft.movecraft.events.CraftPilotEvent) CruiseOnPilotSubCraft(net.countercraft.movecraft.craft.CruiseOnPilotSubCraft) PlayerCraftImpl(net.countercraft.movecraft.craft.PlayerCraftImpl) BlockState(org.bukkit.block.BlockState) WallSign(org.bukkit.block.data.type.WallSign) Sign(org.bukkit.block.Sign) CruiseOnPilotSubCraft(net.countercraft.movecraft.craft.CruiseOnPilotSubCraft) SubCraft(net.countercraft.movecraft.craft.SubCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) CraftType(net.countercraft.movecraft.craft.type.CraftType) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) Pair(net.countercraft.movecraft.util.Pair) EventHandler(org.bukkit.event.EventHandler)

Example 12 with Craft

use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.

the class DetectionTask method get.

@Override
public Effect get() {
    frontier();
    if (!illegal.isEmpty())
        return null;
    var result = COMPLETION_VALIDATORS.stream().reduce(DetectionPredicate::and).orElse((a, b, c, d) -> Result.fail()).validate(materials, type, movecraftWorld, player);
    result = result.isSucess() ? VISITED_VALIDATORS.stream().reduce(DetectionPredicate::and).orElse((a, b, c, d) -> Result.fail()).validate(visitedMaterials, type, movecraftWorld, player) : result;
    if (!result.isSucess()) {
        String message = result.getMessage();
        return () -> audience.sendMessage(Component.text(message));
    }
    var hitbox = new BitmapHitBox(legal);
    var parents = findParents(hitbox);
    var supplied = supplier.apply(type, world, player, parents);
    result = supplied.getLeft();
    Craft craft = supplied.getRight();
    if (type.getBoolProperty(CraftType.MUST_BE_SUBCRAFT) && !(craft instanceof SubCraft)) {
        result = Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Must Be Subcraft"));
    }
    if (!result.isSucess()) {
        String message = result.getMessage();
        return () -> audience.sendMessage(Component.text(message));
    }
    craft.setAudience(audience);
    craft.setHitBox(hitbox);
    craft.setFluidLocations(new BitmapHitBox(fluid));
    craft.setOrigBlockCount(craft.getHitBox().size());
    final CraftDetectEvent event = new CraftDetectEvent(craft, startLocation);
    WorldManager.INSTANCE.executeMain(() -> Bukkit.getPluginManager().callEvent(event));
    if (event.isCancelled())
        return () -> craft.getAudience().sendMessage(Component.text(event.getFailMessage()));
    return ((Effect) () -> {
        // Notify player and console
        craft.getAudience().sendMessage(Component.text(String.format("%s Size: %s", I18nSupport.getInternationalisedString("Detection - Successfully piloted craft"), craft.getHitBox().size())));
        Movecraft.getInstance().getLogger().info(String.format(I18nSupport.getInternationalisedString("Detection - Success - Log Output"), player == null ? "null" : player.getName(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size(), craft.getHitBox().getMinX(), craft.getHitBox().getMinZ()));
    }).andThen(// TODO: Remove
    water(craft)).andThen(// Fire off pilot event
    () -> Bukkit.getServer().getPluginManager().callEvent(new CraftPilotEvent(craft, CraftPilotEvent.Reason.PLAYER))).andThen(// Apply post detection effect
    postDetection.apply(craft)).andThen(// Add craft to CraftManager
    () -> CraftManager.getInstance().add(craft));
}
Also used : AllowedBlockValidator(net.countercraft.movecraft.processing.tasks.detection.validators.AllowedBlockValidator) Arrays(java.util.Arrays) CraftDetectEvent(net.countercraft.movecraft.events.CraftDetectEvent) MovecraftWorld(net.countercraft.movecraft.processing.MovecraftWorld) Player(org.bukkit.entity.Player) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) FlyBlockValidator(net.countercraft.movecraft.processing.tasks.detection.validators.FlyBlockValidator) CraftManager(net.countercraft.movecraft.craft.CraftManager) Material(org.bukkit.Material) Effect(net.countercraft.movecraft.processing.effects.Effect) Bukkit(org.bukkit.Bukkit) Result(net.countercraft.movecraft.processing.functions.Result) CraftType(net.countercraft.movecraft.craft.type.CraftType) PilotSignValidator(net.countercraft.movecraft.processing.tasks.detection.validators.PilotSignValidator) Craft(net.countercraft.movecraft.craft.Craft) BitmapHitBox(net.countercraft.movecraft.util.hitboxes.BitmapHitBox) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) AtomicLocationSet(net.countercraft.movecraft.util.AtomicLocationSet) Movecraft(net.countercraft.movecraft.Movecraft) I18nSupport(net.countercraft.movecraft.localisation.I18nSupport) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CraftSupplier(net.countercraft.movecraft.processing.functions.CraftSupplier) WaterContactValidator(net.countercraft.movecraft.processing.tasks.detection.validators.WaterContactValidator) Queue(java.util.Queue) DetectionPredicate(net.countercraft.movecraft.processing.functions.DetectionPredicate) NotNull(org.jetbrains.annotations.NotNull) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) SubCraft(net.countercraft.movecraft.craft.SubCraft) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) LongAdder(java.util.concurrent.atomic.LongAdder) CollectionUtils(net.countercraft.movecraft.util.CollectionUtils) SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) Deque(java.util.Deque) Function(java.util.function.Function) Supplier(java.util.function.Supplier) CraftPilotEvent(net.countercraft.movecraft.events.CraftPilotEvent) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) NameSignValidator(net.countercraft.movecraft.processing.tasks.detection.validators.NameSignValidator) Component(net.kyori.adventure.text.Component) WorldManager(net.countercraft.movecraft.processing.WorldManager) HitBox(net.countercraft.movecraft.util.hitboxes.HitBox) Functions(com.google.common.base.Functions) ForkJoinTask(java.util.concurrent.ForkJoinTask) ForbiddenSignStringValidator(net.countercraft.movecraft.processing.tasks.detection.validators.ForbiddenSignStringValidator) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) ForbiddenBlockValidator(net.countercraft.movecraft.processing.tasks.detection.validators.ForbiddenBlockValidator) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Tags(net.countercraft.movecraft.util.Tags) Audience(net.kyori.adventure.audience.Audience) ForkJoinPool(java.util.concurrent.ForkJoinPool) SizeValidator(net.countercraft.movecraft.processing.tasks.detection.validators.SizeValidator) Comparator(java.util.Comparator) Collections(java.util.Collections) Craft(net.countercraft.movecraft.craft.Craft) SubCraft(net.countercraft.movecraft.craft.SubCraft) CraftDetectEvent(net.countercraft.movecraft.events.CraftDetectEvent) SubCraft(net.countercraft.movecraft.craft.SubCraft) DetectionPredicate(net.countercraft.movecraft.processing.functions.DetectionPredicate) BitmapHitBox(net.countercraft.movecraft.util.hitboxes.BitmapHitBox) CraftPilotEvent(net.countercraft.movecraft.events.CraftPilotEvent)

Example 13 with Craft

use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.

the class DetectionTask method findParents.

@NotNull
private Set<Craft> findParents(@NotNull HitBox hitBox) {
    SolidHitBox solidHitBox = hitBox.boundingHitBox();
    Set<Craft> nearby = new HashSet<>();
    for (Craft c : CraftManager.getInstance()) {
        // Add the craft to nearby if their bounding boxes intersect
        SolidHitBox otherSolidBox = c.getHitBox().boundingHitBox();
        if (solidHitBox.intersects(otherSolidBox))
            nearby.add(c);
    }
    Set<Craft> parents = new HashSet<>();
    for (var loc : hitBox) {
        if (nearby.size() == 0)
            break;
        for (Craft c : nearby) {
            if (c.getHitBox().contains(loc)) {
                parents.add(c);
            }
        }
        // Clear out crafts from nearby as they get added to parents
        nearby.removeAll(parents);
    }
    return parents;
}
Also used : SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) Craft(net.countercraft.movecraft.craft.Craft) SubCraft(net.countercraft.movecraft.craft.SubCraft) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Craft

use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.

the class ReleaseCommand method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
    if (!command.getName().equalsIgnoreCase("release"))
        return false;
    if (!commandSender.hasPermission("movecraft.commands") && !commandSender.hasPermission("movecraft.commands.release")) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
        return true;
    }
    if (args.length == 0) {
        if (!(commandSender instanceof Player)) {
            commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player- Error - You do not have a craft to release!"));
            return true;
        }
        Player player = (Player) commandSender;
        final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(player.getName());
        if (pCraft == null) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player- Error - You do not have a craft to release!"));
            return true;
        }
        CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.PLAYER, false);
        return true;
    }
    if (!commandSender.hasPermission("movecraft.commands.release.others")) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - No Force Release"));
        return true;
    }
    if (args[0].equalsIgnoreCase("-p")) {
        for (Player p : Bukkit.getOnlinePlayers()) {
            String name = p.getName();
            final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(name);
            if (pCraft != null)
                CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false);
        }
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Player Crafts"));
        return true;
    }
    if (args[0].equalsIgnoreCase("-a")) {
        final List<Craft> craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts());
        for (Craft craft : craftsToRelease) {
            CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false);
        }
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Crafts"));
        return true;
    }
    if (args[0].equalsIgnoreCase("-n")) {
        final List<Craft> craftsToRelease = new ArrayList<>(CraftManager.getInstance().getCrafts());
        for (Craft craft : craftsToRelease) {
            if (!(craft instanceof PilotedCraft))
                CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.FORCE, false);
        }
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Released All Null Crafts"));
        return true;
    }
    Player target = Bukkit.getPlayer(args[0]);
    if (target == null) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player - Not Found"));
        return true;
    }
    final Craft pCraft = CraftManager.getInstance().getCraftByPlayerName(args[0]);
    if (pCraft != null) {
        CraftManager.getInstance().release(pCraft, CraftReleaseEvent.Reason.FORCE, false);
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Release - Successful Force Release"));
        return true;
    }
    commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Player - Not Piloting"));
    return true;
}
Also used : Player(org.bukkit.entity.Player) Craft(net.countercraft.movecraft.craft.Craft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) ArrayList(java.util.ArrayList) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft)

Example 15 with Craft

use of net.countercraft.movecraft.craft.Craft in project Movecraft by APDevTeam.

the class RotateCommand method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
    if (!command.getName().equalsIgnoreCase("rotate")) {
        return false;
    }
    if (!(commandSender instanceof Player)) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Must Be Player"));
        return true;
    }
    Player player = (Player) commandSender;
    if (args.length < 1) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Specify Direction"));
        return true;
    }
    if (args[0].equalsIgnoreCase("left")) {
        if (!player.hasPermission("movecraft.commands") && !player.hasPermission("movecraft.commands.rotateleft")) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
            return true;
        }
        final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName());
        if (craft == null) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft"));
            return true;
        }
        if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
            return true;
        }
        CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.ANTICLOCKWISE, craft.getHitBox().getMidPoint());
        return true;
    }
    if (args[0].equalsIgnoreCase("right")) {
        if (!player.hasPermission("movecraft.commands") && !player.hasPermission("movecraft.commands.rotateright")) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
            return true;
        }
        final Craft craft = CraftManager.getInstance().getCraftByPlayerName(player.getName());
        if (craft == null) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft"));
            return true;
        }
        if (!player.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".rotate")) {
            player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
            return true;
        }
        CraftManager.getInstance().getCraftByPlayerName(player.getName()).rotate(MovecraftRotation.CLOCKWISE, craft.getHitBox().getMidPoint());
        return true;
    }
    player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Rotation - Invalid Direction"));
    return true;
}
Also used : Player(org.bukkit.entity.Player) Craft(net.countercraft.movecraft.craft.Craft)

Aggregations

Craft (net.countercraft.movecraft.craft.Craft)42 EventHandler (org.bukkit.event.EventHandler)22 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)19 Player (org.bukkit.entity.Player)14 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)13 Sign (org.bukkit.block.Sign)12 PilotedCraft (net.countercraft.movecraft.craft.PilotedCraft)11 BlockState (org.bukkit.block.BlockState)11 PlayerCraft (net.countercraft.movecraft.craft.PlayerCraft)9 World (org.bukkit.World)9 Location (org.bukkit.Location)6 Block (org.bukkit.block.Block)6 SubCraft (net.countercraft.movecraft.craft.SubCraft)4 CraftType (net.countercraft.movecraft.craft.type.CraftType)4 HitBox (net.countercraft.movecraft.util.hitboxes.HitBox)4 Material (org.bukkit.Material)4 NotNull (org.jetbrains.annotations.NotNull)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3