Search in sources :

Example 16 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class ContactsSign method onCraftDetect.

@EventHandler
public void onCraftDetect(CraftDetectEvent event) {
    World world = event.getCraft().getWorld();
    for (MovecraftLocation location : event.getCraft().getHitBox()) {
        var block = location.toBukkit(world).getBlock();
        if (!Tag.SIGNS.isTagged(block.getType())) {
            continue;
        }
        BlockState state = block.getState();
        if (state instanceof Sign) {
            Sign sign = (Sign) state;
            if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Contacts:")) {
                sign.setLine(1, "");
                sign.setLine(2, "");
                sign.setLine(3, "");
                sign.update();
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) Sign(org.bukkit.block.Sign) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) EventHandler(org.bukkit.event.EventHandler)

Example 17 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation 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 18 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class DetectionTask method water.

@Deprecated
@NotNull
private Effect water(@NotNull Craft craft) {
    final int waterLine = WorldManager.INSTANCE.executeMain(craft::getWaterLine);
    if (craft.getType().getBoolProperty(CraftType.BLOCKED_BY_WATER) || craft.getHitBox().getMinY() > waterLine)
        return () -> {
        };
    var badWorld = WorldManager.INSTANCE.executeMain(craft::getWorld);
    // The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
    final HitBox invertedHitBox = new BitmapHitBox(craft.getHitBox().boundingHitBox()).difference(craft.getHitBox());
    // A set of locations that are confirmed to be "exterior" locations
    final SetHitBox confirmed = new SetHitBox();
    final SetHitBox entireHitbox = new SetHitBox(craft.getHitBox());
    // place phased blocks
    final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
    overlap.retainAll(craft.getHitBox().asSet().stream().map(l -> l.toBukkit(badWorld)).collect(Collectors.toSet()));
    final int minX = craft.getHitBox().getMinX();
    final int maxX = craft.getHitBox().getMaxX();
    final int minY = craft.getHitBox().getMinY();
    final int maxY = overlap.isEmpty() ? craft.getHitBox().getMaxY() : Collections.max(overlap, Comparator.comparingInt(Location::getBlockY)).getBlockY();
    final int minZ = craft.getHitBox().getMinZ();
    final int maxZ = craft.getHitBox().getMaxZ();
    final HitBox[] surfaces = { new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, minY, maxZ)) };
    final SetHitBox validExterior = new SetHitBox();
    for (HitBox hitBox : surfaces) {
        validExterior.addAll(new BitmapHitBox(hitBox).difference(craft.getHitBox()));
    }
    // Check to see which locations in the from set are actually outside of the craft
    // use a modified BFS for multiple origin elements
    SetHitBox visited = new SetHitBox();
    Queue<MovecraftLocation> queue = Lists.newLinkedList(validExterior);
    while (!queue.isEmpty()) {
        MovecraftLocation node = queue.poll();
        if (visited.contains(node))
            continue;
        visited.add(node);
        // If the node is already a valid member of the exterior of the HitBox, continued search is unitary.
        for (MovecraftLocation neighbor : CollectionUtils.neighbors(invertedHitBox, node)) {
            queue.add(neighbor);
        }
    }
    confirmed.addAll(visited);
    entireHitbox.addAll(invertedHitBox.difference(confirmed));
    var waterData = Bukkit.createBlockData(Material.WATER);
    return () -> {
        for (MovecraftLocation location : entireHitbox) {
            if (location.getY() <= waterLine) {
                craft.getPhaseBlocks().put(location.toBukkit(badWorld), waterData);
            }
        }
    };
}
Also used : BitmapHitBox(net.countercraft.movecraft.util.hitboxes.BitmapHitBox) SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) HitBox(net.countercraft.movecraft.util.hitboxes.HitBox) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) Movecraft(net.countercraft.movecraft.Movecraft) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) BitmapHitBox(net.countercraft.movecraft.util.hitboxes.BitmapHitBox) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class BaseCraft method resetSigns.

@Override
public void resetSigns(@NotNull Sign clicked) {
    for (final MovecraftLocation ml : hitBox) {
        final Block b = ml.toBukkit(w).getBlock();
        if (!(b.getState() instanceof Sign)) {
            continue;
        }
        final Sign sign = (Sign) b.getState();
        if (sign.equals(clicked)) {
            continue;
        }
        if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: ON")) {
            sign.setLine(0, "Cruise: OFF");
        } else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: OFF") && ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Cruise: ON") && getFacing(sign) == getFacing(clicked)) {
            sign.setLine(0, "Cruise: ON");
        } else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: ON")) {
            sign.setLine(0, "Ascend: OFF");
        } else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: OFF") && ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Ascend: ON")) {
            sign.setLine(0, "Ascend: ON");
        } else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: ON")) {
            sign.setLine(0, "Descend: OFF");
        } else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: OFF") && ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Descend: ON")) {
            sign.setLine(0, "Descend: ON");
        }
        sign.update();
    }
}
Also used : Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 20 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class StatusSign method onCraftDetect.

@EventHandler
public void onCraftDetect(CraftDetectEvent event) {
    World world = event.getCraft().getWorld();
    for (MovecraftLocation location : event.getCraft().getHitBox()) {
        var block = location.toBukkit(world).getBlock();
        if (!Tag.SIGNS.isTagged(block.getType())) {
            continue;
        }
        BlockState state = block.getState();
        if (state instanceof Sign) {
            Sign sign = (Sign) state;
            if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Status:")) {
                sign.setLine(1, "");
                sign.setLine(2, "");
                sign.setLine(3, "");
                sign.update();
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) Sign(org.bukkit.block.Sign) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) EventHandler(org.bukkit.event.EventHandler)

Aggregations

MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)60 Craft (net.countercraft.movecraft.craft.Craft)18 EventHandler (org.bukkit.event.EventHandler)18 Material (org.bukkit.Material)17 World (org.bukkit.World)15 Sign (org.bukkit.block.Sign)14 BlockState (org.bukkit.block.BlockState)13 Block (org.bukkit.block.Block)12 ArrayList (java.util.ArrayList)11 Location (org.bukkit.Location)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SetHitBox (net.countercraft.movecraft.util.hitboxes.SetHitBox)9 HashSet (java.util.HashSet)8 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)8 NotNull (org.jetbrains.annotations.NotNull)8 MovecraftRotation (net.countercraft.movecraft.MovecraftRotation)6 HitBox (net.countercraft.movecraft.util.hitboxes.HitBox)6 SolidHitBox (net.countercraft.movecraft.util.hitboxes.SolidHitBox)6 LinkedList (java.util.LinkedList)5