Search in sources :

Example 1 with SubcraftRotateCraft

use of net.countercraft.movecraft.craft.SubcraftRotateCraft 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)

Aggregations

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