Search in sources :

Example 1 with Pair

use of net.countercraft.movecraft.util.Pair in project Movecraft by APDevTeam.

the class PilotCommand method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
    if (!command.getName().equalsIgnoreCase("pilot"))
        return false;
    if (!(commandSender instanceof Player)) {
        commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Must Be Player"));
        return true;
    }
    Player player = (Player) commandSender;
    if (!player.hasPermission("movecraft.commands") || !player.hasPermission("movecraft.commands.pilot")) {
        player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
        return true;
    }
    if (args.length < 1) {
        player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - No Craft Type"));
        return true;
    }
    if (!player.hasPermission("movecraft." + args[0] + ".pilot")) {
        player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Insufficient Permissions"));
        return true;
    }
    CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(args[0]);
    if (craftType == null) {
        player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Pilot - Invalid Craft Type"));
        return true;
    }
    final World world = player.getWorld();
    MovecraftLocation startPoint = MathUtils.bukkit2MovecraftLoc(player.getLocation());
    CraftManager.getInstance().detect(startPoint, craftType, (type, w, p, parents) -> {
        // Note: This only passes in a non-null player.
        assert p != null;
        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 -> () -> {
        // Release old craft if it exists
        Craft oldCraft = CraftManager.getInstance().getCraftByPlayer(player);
        if (oldCraft != null)
            CraftManager.getInstance().release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false);
    });
    return true;
}
Also used : Player(org.bukkit.entity.Player) PlayerCraftImpl(net.countercraft.movecraft.craft.PlayerCraftImpl) CruiseOnPilotCraft(net.countercraft.movecraft.craft.CruiseOnPilotCraft) CruiseOnPilotSubCraft(net.countercraft.movecraft.craft.CruiseOnPilotSubCraft) Craft(net.countercraft.movecraft.craft.Craft) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) CraftType(net.countercraft.movecraft.craft.type.CraftType) Pair(net.countercraft.movecraft.util.Pair)

Example 2 with Pair

use of net.countercraft.movecraft.util.Pair in project Movecraft by APDevTeam.

the class SubcraftRotateSign method onSignClick.

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
    MovecraftRotation rotation;
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
        rotation = MovecraftRotation.CLOCKWISE;
    else if (event.getAction() == Action.LEFT_CLICK_BLOCK)
        rotation = MovecraftRotation.ANTICLOCKWISE;
    else
        return;
    BlockState state = event.getClickedBlock().getState();
    if (!(state instanceof Sign))
        return;
    Sign sign = (Sign) state;
    if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER))
        return;
    Location loc = event.getClickedBlock().getLocation();
    MovecraftLocation startPoint = new MovecraftLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
    if (rotating.contains(startPoint)) {
        event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Rotation - Already Rotating"));
        event.setCancelled(true);
        return;
    }
    // rotate subcraft
    String craftTypeStr = ChatColor.stripColor(sign.getLine(1));
    CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(craftTypeStr);
    if (craftType == null)
        return;
    if (ChatColor.stripColor(sign.getLine(2)).equals("") && ChatColor.stripColor(sign.getLine(3)).equals("")) {
        sign.setLine(2, "_\\ /_");
        sign.setLine(3, "/ \\");
        sign.update(false, false);
    }
    if (!event.getPlayer().hasPermission("movecraft." + craftTypeStr + ".pilot") || !event.getPlayer().hasPermission("movecraft." + craftTypeStr + ".rotate")) {
        event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
        return;
    }
    Craft playerCraft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
    if (playerCraft != null) {
        if (!playerCraft.isNotProcessing()) {
            event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Detection - Parent Craft is busy"));
            return;
        }
        // prevent the parent craft from moving or updating until the subcraft is done
        playerCraft.setProcessing(true);
        new BukkitRunnable() {

            @Override
            public void run() {
                playerCraft.setProcessing(false);
            }
        }.runTaskLater(Movecraft.getInstance(), (10));
    }
    rotating.add(startPoint);
    Player player = event.getPlayer();
    World world = event.getClickedBlock().getWorld();
    CraftManager.getInstance().detect(startPoint, craftType, (type, w, p, parents) -> {
        if (parents.size() > 1)
            return new Pair<>(Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Failed - Already commanding a craft")), null);
        if (parents.size() < 1)
            return new Pair<>(Result.succeed(), new SubcraftRotateCraft(type, w, p));
        Craft parent = parents.iterator().next();
        return new Pair<>(Result.succeed(), new SubCraftImpl(type, w, parent));
    }, world, player, Movecraft.getAdventure().player(player), craft -> () -> {
        Bukkit.getServer().getPluginManager().callEvent(new CraftPilotEvent(craft, CraftPilotEvent.Reason.SUB_CRAFT));
        if (craft instanceof SubCraft) {
            // Subtract craft from the parent
            Craft parent = ((SubCraft) craft).getParent();
            var newHitbox = parent.getHitBox().difference(craft.getHitBox());
            ;
            parent.setHitBox(newHitbox);
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                craft.rotate(rotation, startPoint, true);
                if (craft instanceof SubCraft) {
                    Craft parent = ((SubCraft) craft).getParent();
                    var newHitbox = parent.getHitBox().union(craft.getHitBox());
                    parent.setHitBox(newHitbox);
                }
                CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false);
            }
        }.runTaskLater(Movecraft.getInstance(), 3);
    });
    event.setCancelled(true);
    new BukkitRunnable() {

        @Override
        public void run() {
            rotating.remove(startPoint);
        }
    }.runTaskLater(Movecraft.getInstance(), 4);
}
Also used : Player(org.bukkit.entity.Player) SubcraftRotateCraft(net.countercraft.movecraft.craft.SubcraftRotateCraft) Craft(net.countercraft.movecraft.craft.Craft) SubCraft(net.countercraft.movecraft.craft.SubCraft) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) World(org.bukkit.World) SubcraftRotateCraft(net.countercraft.movecraft.craft.SubcraftRotateCraft) CraftPilotEvent(net.countercraft.movecraft.events.CraftPilotEvent) MovecraftRotation(net.countercraft.movecraft.MovecraftRotation) BlockState(org.bukkit.block.BlockState) Sign(org.bukkit.block.Sign) SubCraftImpl(net.countercraft.movecraft.craft.SubCraftImpl) 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 3 with Pair

use of net.countercraft.movecraft.util.Pair 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)

Aggregations

MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)3 Craft (net.countercraft.movecraft.craft.Craft)3 CraftType (net.countercraft.movecraft.craft.type.CraftType)3 Pair (net.countercraft.movecraft.util.Pair)3 World (org.bukkit.World)3 Player (org.bukkit.entity.Player)3 CruiseOnPilotCraft (net.countercraft.movecraft.craft.CruiseOnPilotCraft)2 CruiseOnPilotSubCraft (net.countercraft.movecraft.craft.CruiseOnPilotSubCraft)2 PlayerCraftImpl (net.countercraft.movecraft.craft.PlayerCraftImpl)2 SubCraft (net.countercraft.movecraft.craft.SubCraft)2 CraftPilotEvent (net.countercraft.movecraft.events.CraftPilotEvent)2 Location (org.bukkit.Location)2 BlockState (org.bukkit.block.BlockState)2 Sign (org.bukkit.block.Sign)2 EventHandler (org.bukkit.event.EventHandler)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 MovecraftRotation (net.countercraft.movecraft.MovecraftRotation)1 SubCraftImpl (net.countercraft.movecraft.craft.SubCraftImpl)1 SubcraftRotateCraft (net.countercraft.movecraft.craft.SubcraftRotateCraft)1 WallSign (org.bukkit.block.data.type.WallSign)1