Search in sources :

Example 16 with Craft

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

the class ScuttleSign method onSignClick.

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
        return;
    if (event.getClickedBlock() == null)
        return;
    BlockState state = event.getClickedBlock().getState();
    if (!(state instanceof Sign)) {
        return;
    }
    Sign sign = (Sign) state;
    if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER)) {
        return;
    }
    Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
    if (craft == null) {
        if (!event.getPlayer().hasPermission("movecraft.commands.scuttle.others")) {
            event.getPlayer().sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft"));
            return;
        }
        craft = MathUtils.fastNearestCraftToLoc(CraftManager.getInstance().getCrafts(), event.getClickedBlock().getLocation());
        if (craft == null)
            return;
    }
    scuttle(craft, event.getPlayer());
}
Also used : BlockState(org.bukkit.block.BlockState) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) Craft(net.countercraft.movecraft.craft.Craft) Sign(org.bukkit.block.Sign) EventHandler(org.bukkit.event.EventHandler)

Example 17 with Craft

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

the class SpeedSign method onSignTranslate.

@EventHandler
public final void onSignTranslate(SignTranslateEvent event) {
    Craft craft = event.getCraft();
    if (!ChatColor.stripColor(event.getLine(0)).equalsIgnoreCase("Speed:")) {
        return;
    }
    event.setLine(1, String.format("%.2f", craft.getSpeed()) + "m/s");
    event.setLine(2, String.format("%.2f", craft.getMeanCruiseTime() * 1000) + "ms");
    event.setLine(3, craft.getTickCooldown() + "T");
}
Also used : Craft(net.countercraft.movecraft.craft.Craft) EventHandler(org.bukkit.event.EventHandler)

Example 18 with Craft

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

the class StatusSign method onSignTranslate.

@EventHandler
public final void onSignTranslate(SignTranslateEvent event) {
    Craft craft = event.getCraft();
    if (!ChatColor.stripColor(event.getLine(0)).equalsIgnoreCase("Status:")) {
        return;
    }
    int fuel = 0;
    int totalBlocks = 0;
    Counter<Material> foundBlocks = new Counter<>();
    var v = craft.getType().getObjectProperty(CraftType.FUEL_TYPES);
    if (!(v instanceof Map<?, ?>))
        throw new IllegalStateException("FUEL_TYPES must be of type Map");
    var fuelTypes = (Map<?, ?>) v;
    for (var e : fuelTypes.entrySet()) {
        if (!(e.getKey() instanceof Material))
            throw new IllegalStateException("Keys in FUEL_TYPES must be of type Material");
        if (!(e.getValue() instanceof Double))
            throw new IllegalStateException("Values in FUEL_TYPES must be of type Double");
    }
    for (MovecraftLocation ml : craft.getHitBox()) {
        Material material = craft.getWorld().getBlockAt(ml.getX(), ml.getY(), ml.getZ()).getType();
        foundBlocks.add(material);
        if (Tags.FURNACES.contains(material)) {
            InventoryHolder inventoryHolder = (InventoryHolder) craft.getWorld().getBlockAt(ml.getX(), ml.getY(), ml.getZ()).getState();
            for (ItemStack iStack : inventoryHolder.getInventory()) {
                if (iStack == null || !fuelTypes.containsKey(iStack.getType()))
                    continue;
                fuel += iStack.getAmount() * (double) fuelTypes.get(iStack.getType());
            }
        }
        if (!material.isAir() && material != Material.FIRE) {
            totalBlocks++;
        }
    }
    int signLine = 1;
    int signColumn = 0;
    /*  TODO: Implement new system for flyblocks on status signs
        for(EnumSet<Material> alFlyBlockID : craft.getType().getFlyBlocks().keySet()) {
            Material flyBlockID= alFlyBlockID.get(0);
            double minimum=craft.getType().getFlyBlocks().get(alFlyBlockID).get(0);
            if(foundBlocks.get(flyBlockID) != 0 && minimum>0) { // if it has a minimum, it should be considered for sinking consideration
                int amount=foundBlocks.get((flyBlockID));
                double percentPresent= (amount*100D/totalBlocks);
                String signText="";
                if(percentPresent>minimum*1.04) {
                    signText+= ChatColor.GREEN;
                } else if(percentPresent>minimum*1.02) {
                    signText+=ChatColor.YELLOW;
                } else {
                    signText+=ChatColor.RED;
                }
                if(flyBlockID == Material.REDSTONE_BLOCK) {
                    signText+="R";
                } else if(flyBlockID == Material.IRON_BLOCK) {
                    signText+="I";
                } else {
                    signText+= flyBlockID.toString().charAt(0);
                }

                signText+=" ";
                signText+=  (int) percentPresent;
                signText+="/";
                signText+= (int) minimum;
                signText+="  ";
                if(signColumn==0) {
                    event.setLine(signLine,signText);
                    signColumn++;
                } else if(signLine<3) {
                    String existingLine = event.getLine(signLine);
                    existingLine += signText;
                    event.setLine(signLine, existingLine);
                    signLine++;
                    signColumn=0;
                }
            }
        }
        if (signLine < 3 && signColumn == 1){
            signLine++;
        }
        */
    String fuelText = "";
    int cruiseSkipBlocks = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, craft.getWorld());
    cruiseSkipBlocks++;
    double fuelBurnRate = (double) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_FUEL_BURN_RATE, craft.getWorld());
    int fuelRange = (int) Math.round((fuel * (1 + cruiseSkipBlocks)) / fuelBurnRate);
    if (fuelRange > 1000) {
        fuelText += ChatColor.GREEN;
    } else if (fuelRange > 100) {
        fuelText += ChatColor.YELLOW;
    } else {
        fuelText += ChatColor.RED;
    }
    fuelText += "Fuel range:";
    fuelText += fuelRange;
    event.setLine(signLine, fuelText);
}
Also used : Craft(net.countercraft.movecraft.craft.Craft) Material(org.bukkit.Material) Counter(net.countercraft.movecraft.util.Counter) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) InventoryHolder(org.bukkit.inventory.InventoryHolder) EventHandler(org.bukkit.event.EventHandler)

Example 19 with Craft

use of net.countercraft.movecraft.craft.Craft 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 20 with Craft

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

the class CruiseSign method onSignClick.

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
        return;
    }
    BlockState state = event.getClickedBlock().getState();
    if (!(state instanceof Sign)) {
        return;
    }
    Sign sign = (Sign) state;
    if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: OFF")) {
        if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) {
            return;
        }
        Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
        if (!c.getType().getBoolProperty(CraftType.CAN_CRUISE)) {
            return;
        }
        // c.resetSigns(false, true, true);
        sign.setLine(0, "Cruise: ON");
        sign.update(true);
        if (sign.getBlockData() instanceof WallSign) {
            c.setCruiseDirection(CruiseDirection.fromBlockFace(((WallSign) sign.getBlockData()).getFacing()));
        } else {
            c.setCruiseDirection(CruiseDirection.NONE);
        }
        c.setLastCruiseUpdate(System.currentTimeMillis());
        c.setCruising(true);
        c.resetSigns(sign);
        if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) {
            CraftManager.getInstance().addReleaseTask(c);
        }
        return;
    }
    if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: ON") && CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) != null && CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_CRUISE)) {
        Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
        sign.setLine(0, "Cruise: OFF");
        sign.update(true);
        c.setCruising(false);
        c.resetSigns(sign);
    }
}
Also used : WallSign(org.bukkit.block.data.type.WallSign) BlockState(org.bukkit.block.BlockState) Craft(net.countercraft.movecraft.craft.Craft) Sign(org.bukkit.block.Sign) WallSign(org.bukkit.block.data.type.WallSign) EventHandler(org.bukkit.event.EventHandler)

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