Search in sources :

Example 6 with CraftType

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

the class CraftManager method loadCraftTypes.

@NotNull
private Set<CraftType> loadCraftTypes() {
    File craftsFile = new File(Movecraft.getInstance().getDataFolder().getAbsolutePath() + "/types");
    if (craftsFile.mkdirs()) {
        Movecraft.getInstance().saveResource("types/Airship.craft", false);
        Movecraft.getInstance().saveResource("types/Airskiff.craft", false);
        Movecraft.getInstance().saveResource("types/BigAirship.craft", false);
        Movecraft.getInstance().saveResource("types/BigSubAirship.craft", false);
        Movecraft.getInstance().saveResource("types/Elevator.craft", false);
        Movecraft.getInstance().saveResource("types/LaunchTorpedo.craft", false);
        Movecraft.getInstance().saveResource("types/Ship.craft", false);
        Movecraft.getInstance().saveResource("types/SubAirship.craft", false);
        Movecraft.getInstance().saveResource("types/Submarine.craft", false);
        Movecraft.getInstance().saveResource("types/Turret.craft", false);
    }
    Set<CraftType> craftTypes = new HashSet<>();
    File[] files = craftsFile.listFiles();
    if (files == null) {
        return craftTypes;
    }
    for (File file : files) {
        if (file.isFile()) {
            if (file.getName().contains(".craft")) {
                try {
                    CraftType type = new CraftType(file);
                    craftTypes.add(type);
                } catch (IllegalArgumentException | CraftType.TypeNotFoundException | ParserException | ScannerException e) {
                    Movecraft.getInstance().getLogger().log(Level.SEVERE, I18nSupport.getInternationalisedString("Startup - failure to load craft type") + " '" + file.getName() + "' " + e.getMessage());
                }
            }
        }
    }
    if (craftTypes.isEmpty()) {
        Movecraft.getInstance().getLogger().log(Level.SEVERE, ERROR_PREFIX + I18nSupport.getInternationalisedString("Startup - No Crafts Found"));
    }
    Movecraft.getInstance().getLogger().log(Level.INFO, String.format(I18nSupport.getInternationalisedString("Startup - Number of craft files loaded"), craftTypes.size()));
    return craftTypes;
}
Also used : ScannerException(org.yaml.snakeyaml.scanner.ScannerException) ParserException(org.yaml.snakeyaml.parser.ParserException) File(java.io.File) CraftType(net.countercraft.movecraft.craft.type.CraftType) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with CraftType

use of net.countercraft.movecraft.craft.type.CraftType 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 8 with CraftType

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

the class PilotCommand method onTabComplete.

@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
    if (strings.length != 1 || !commandSender.hasPermission("movecraft.commands") || !commandSender.hasPermission("movecraft.commands.pilot"))
        return Collections.emptyList();
    List<String> completions = new ArrayList<>();
    for (CraftType type : CraftManager.getInstance().getCraftTypes()) if (commandSender.hasPermission("movecraft." + type.getStringProperty(CraftType.NAME) + ".pilot"))
        completions.add(type.getStringProperty(CraftType.NAME));
    List<String> returnValues = new ArrayList<>();
    for (String completion : completions) if (completion.toLowerCase().startsWith(strings[strings.length - 1].toLowerCase()))
        returnValues.add(completion);
    return returnValues;
}
Also used : ArrayList(java.util.ArrayList) CraftType(net.countercraft.movecraft.craft.type.CraftType)

Example 9 with CraftType

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

the class InteractListener method onPlayerInteract.

// LOWEST so that it runs before the other events
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteract(@NotNull PlayerInteractEvent e) {
    if (e.getAction() == Action.LEFT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR) {
        if (e.getItem() != null && e.getItem().getType() == Settings.PilotTool) {
            // Handle pilot tool left clicks
            e.setCancelled(true);
            Player p = e.getPlayer();
            PlayerCraft craft = CraftManager.getInstance().getCraftByPlayer(p);
            if (craft == null)
                return;
            if (craft.getPilotLocked()) {
                // Allow all players to leave direct control mode
                craft.setPilotLocked(false);
                p.sendMessage(I18nSupport.getInternationalisedString("Direct Control - Leaving"));
            } else if (!p.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".move") || !craft.getType().getBoolProperty(CraftType.CAN_DIRECT_CONTROL)) {
                // Deny players from entering direct control mode
                p.sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
            } else {
                // Enter direct control mode
                craft.setPilotLocked(true);
                craft.setPilotLockedX(p.getLocation().getBlockX() + 0.5);
                craft.setPilotLockedY(p.getLocation().getY());
                craft.setPilotLockedZ(p.getLocation().getBlockZ() + 0.5);
                p.sendMessage(I18nSupport.getInternationalisedString("Direct Control - Entering"));
            }
        } else if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
            // Handle button left clicks
            BlockState state = e.getClickedBlock().getState();
            if (!(state instanceof Switch))
                return;
            Switch data = (Switch) state.getBlockData();
            if (data.isPowered()) {
                // Depower the button
                data.setPowered(false);
                e.getClickedBlock().setBlockData(data);
                e.setCancelled(true);
            }
        }
    } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
        if (e.getItem() == null || e.getItem().getType() != Settings.PilotTool)
            return;
        // Handle pilot tool right clicks
        e.setCancelled(true);
        Player p = e.getPlayer();
        PlayerCraft craft = CraftManager.getInstance().getCraftByPlayer(p);
        if (craft == null)
            return;
        CraftType type = craft.getType();
        int currentGear = craft.getCurrentGear();
        if (p.isSneaking() && !craft.getPilotLocked()) {
            // Handle shift right clicks (when not in direct control mode)
            int gearShifts = type.getIntProperty(CraftType.GEAR_SHIFTS);
            if (gearShifts == 1) {
                p.sendMessage(I18nSupport.getInternationalisedString("Gearshift - Disabled for craft type"));
                return;
            }
            currentGear++;
            if (currentGear > gearShifts)
                currentGear = 1;
            p.sendMessage(I18nSupport.getInternationalisedString("Gearshift - Gear changed") + " " + currentGear + " / " + gearShifts);
            craft.setCurrentGear(currentGear);
            return;
        }
        int tickCooldown = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_TICK_COOLDOWN, craft.getWorld());
        if (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_DIRECT_MOVEMENT) && type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN))
            // Account for gear shifts
            tickCooldown *= currentGear;
        Long lastTime = timeMap.get(p);
        if (lastTime != null) {
            long ticksElapsed = (System.currentTimeMillis() - lastTime) / 50;
            // if the craft should go slower underwater, make time pass more slowly there
            if (craft.getType().getBoolProperty(CraftType.HALF_SPEED_UNDERWATER) && craft.getHitBox().getMinY() < craft.getWorld().getSeaLevel())
                ticksElapsed /= 2;
            if (ticksElapsed < tickCooldown)
                // Not enough time has passed, so don't do anything
                return;
        }
        if (!p.hasPermission("movecraft." + craft.getType().getStringProperty(CraftType.NAME) + ".move")) {
            p.sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
            // Player doesn't have permission to move this craft, so don't do anything
            return;
        }
        if (!MathUtils.locationNearHitBox(craft.getHitBox(), p.getLocation(), 2))
            // Player is not near the craft, so don't do anything
            return;
        if (craft.getPilotLocked()) {
            // Direct control mode allows vertical movements when right-clicking
            // Default to up
            int dy = 1;
            if (p.isSneaking())
                // Down if sneaking
                dy = -1;
            if (craft.getType().getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_DIRECT_MOVEMENT))
                // account for gear shifts
                dy *= currentGear;
            craft.translate(craft.getWorld(), 0, dy, 0);
            timeMap.put(p, System.currentTimeMillis());
            craft.setLastCruiseUpdate(System.currentTimeMillis());
            return;
        }
        double rotation = p.getLocation().getYaw() * Math.PI / 180.0;
        float nx = -(float) Math.sin(rotation);
        float nz = (float) Math.cos(rotation);
        int dx = (Math.abs(nx) >= 0.5 ? 1 : 0) * (int) Math.signum(nx);
        int dz = (Math.abs(nz) > 0.5 ? 1 : 0) * (int) Math.signum(nz);
        float pitch = p.getLocation().getPitch();
        int dy = -(Math.abs(pitch) >= 25 ? 1 : 0) * (int) Math.signum(pitch);
        if (Math.abs(pitch) >= 75) {
            dx = 0;
            dz = 0;
        }
        craft.translate(craft.getWorld(), dx, dy, dz);
        timeMap.put(p, System.currentTimeMillis());
        craft.setLastCruiseUpdate(System.currentTimeMillis());
    }
}
Also used : Player(org.bukkit.entity.Player) BlockState(org.bukkit.block.BlockState) Switch(org.bukkit.block.data.type.Switch) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) CraftType(net.countercraft.movecraft.craft.type.CraftType) EventHandler(org.bukkit.event.EventHandler)

Aggregations

CraftType (net.countercraft.movecraft.craft.type.CraftType)9 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)4 Player (org.bukkit.entity.Player)4 Craft (net.countercraft.movecraft.craft.Craft)3 Pair (net.countercraft.movecraft.util.Pair)3 World (org.bukkit.World)3 BlockState (org.bukkit.block.BlockState)3 EventHandler (org.bukkit.event.EventHandler)3 ArrayList (java.util.ArrayList)2 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 Sign (org.bukkit.block.Sign)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 File (java.io.File)1 HashSet (java.util.HashSet)1 OptionalInt (java.util.OptionalInt)1