Search in sources :

Example 1 with PlayerCraft

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

the class AsyncManager method processCruise.

private void processCruise() {
    for (Craft craft : CraftManager.getInstance()) {
        if (craft == null || !craft.isNotProcessing() || !craft.getCruising())
            continue;
        long ticksElapsed = (System.currentTimeMillis() - craft.getLastCruiseUpdate()) / 50;
        World w = craft.getWorld();
        // time pass more slowly there
        if (craft.getType().getBoolProperty(CraftType.HALF_SPEED_UNDERWATER) && craft.getHitBox().getMinY() < w.getSeaLevel())
            ticksElapsed >>= 1;
        // check direct controls to modify movement
        boolean bankLeft = false;
        boolean bankRight = false;
        boolean dive = false;
        if (craft instanceof PlayerCraft && ((PlayerCraft) craft).getPilotLocked()) {
            Player pilot = ((PlayerCraft) craft).getPilot();
            if (pilot.isSneaking())
                dive = true;
            if (pilot.getInventory().getHeldItemSlot() == 3)
                bankLeft = true;
            if (pilot.getInventory().getHeldItemSlot() == 5)
                bankRight = true;
        }
        int tickCoolDown;
        if (cooldownCache.containsKey(craft)) {
            tickCoolDown = cooldownCache.get(craft);
        } else {
            tickCoolDown = craft.getTickCooldown();
            cooldownCache.put(craft, tickCoolDown);
        }
        // Account for banking and diving in speed calculations by changing the tickCoolDown
        int cruiseSkipBlocks = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w);
        if (craft.getCruiseDirection() != CruiseDirection.UP && craft.getCruiseDirection() != CruiseDirection.DOWN) {
            if (bankLeft || bankRight) {
                if (!dive) {
                    tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + Math.pow(cruiseSkipBlocks >> 1, 2)) / (1 + cruiseSkipBlocks));
                } else {
                    tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + Math.pow(cruiseSkipBlocks >> 1, 2) + 1) / (1 + cruiseSkipBlocks));
                }
            } else if (dive) {
                tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + 1) / (1 + cruiseSkipBlocks));
            }
        }
        if (Math.abs(ticksElapsed) < tickCoolDown)
            continue;
        cooldownCache.remove(craft);
        int dx = 0;
        int dz = 0;
        int dy = 0;
        int vertCruiseSkipBlocks = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_VERT_CRUISE_SKIP_BLOCKS, craft.getWorld());
        // ascend
        if (craft.getCruiseDirection() == CruiseDirection.UP)
            dy = 1 + vertCruiseSkipBlocks;
        // descend
        if (craft.getCruiseDirection() == CruiseDirection.DOWN) {
            dy = -1 - vertCruiseSkipBlocks;
            if (craft.getHitBox().getMinY() <= w.getSeaLevel())
                dy = -1;
        } else if (dive) {
            dy = -((cruiseSkipBlocks + 1) >> 1);
            if (craft.getHitBox().getMinY() <= w.getSeaLevel())
                dy = -1;
        }
        // ship faces west
        if (craft.getCruiseDirection() == CruiseDirection.WEST) {
            dx = -1 - cruiseSkipBlocks;
            if (bankRight)
                dz = (-1 - cruiseSkipBlocks) >> 1;
            if (bankLeft)
                dz = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces east
        if (craft.getCruiseDirection() == CruiseDirection.EAST) {
            dx = 1 + cruiseSkipBlocks;
            if (bankLeft)
                dz = (-1 - cruiseSkipBlocks) >> 1;
            if (bankRight)
                dz = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces north
        if (craft.getCruiseDirection() == CruiseDirection.SOUTH) {
            dz = 1 + cruiseSkipBlocks;
            if (bankRight)
                dx = (-1 - cruiseSkipBlocks) >> 1;
            if (bankLeft)
                dx = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces south
        if (craft.getCruiseDirection() == CruiseDirection.NORTH) {
            dz = -1 - cruiseSkipBlocks;
            if (bankLeft)
                dx = (-1 - cruiseSkipBlocks) >> 1;
            if (bankRight)
                dx = (1 + cruiseSkipBlocks) >> 1;
        }
        if (craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
            dy = craft.getType().getIntProperty(CraftType.CRUISE_ON_PILOT_VERT_MOVE);
        }
        if (craft.getType().getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_CRUISE_SKIP_BLOCKS)) {
            final int gearshift = craft.getCurrentGear();
            dx *= gearshift;
            dy *= gearshift;
            dz *= gearshift;
        }
        craft.translate(dx, dy, dz);
        craft.setLastTranslation(new MovecraftLocation(dx, dy, dz));
        craft.setLastCruiseUpdate(System.currentTimeMillis());
    }
}
Also used : Player(org.bukkit.entity.Player) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) Craft(net.countercraft.movecraft.craft.Craft) World(org.bukkit.World) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 2 with PlayerCraft

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

Example 3 with PlayerCraft

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

the class RemoteSign method onSignClick.

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.LEFT_CLICK_BLOCK) {
        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 foundCraft = null;
    for (PlayerCraft tcraft : CraftManager.getInstance().getPlayerCraftsInWorld(event.getClickedBlock().getWorld())) {
        if (MathUtils.locationInHitBox(tcraft.getHitBox(), event.getClickedBlock().getLocation())) {
            // don't use a craft with a null player. This is
            // mostly to avoid trying to use subcrafts
            foundCraft = tcraft;
            break;
        }
    }
    if (foundCraft == null) {
        event.getPlayer().sendMessage(ERROR_PREFIX + I18nSupport.getInternationalisedString("Remote Sign - Must be a part of a piloted craft"));
        return;
    }
    if (!foundCraft.getType().getBoolProperty(CraftType.ALLOW_REMOTE_SIGN)) {
        event.getPlayer().sendMessage(ERROR_PREFIX + I18nSupport.getInternationalisedString("Remote Sign - Not allowed on this craft"));
        return;
    }
    String targetText = ChatColor.stripColor(sign.getLine(1));
    if (targetText.equalsIgnoreCase(HEADER)) {
        event.getPlayer().sendMessage(ERROR_PREFIX + I18nSupport.getInternationalisedString("Remote Sign - Cannot remote another Remote Sign"));
        return;
    }
    if (targetText.equalsIgnoreCase("")) {
        event.getPlayer().sendMessage("Remote Sign - Cannot be blank");
        return;
    }
    LinkedList<MovecraftLocation> foundLocations = new LinkedList<MovecraftLocation>();
    boolean firstError = true;
    for (MovecraftLocation tloc : foundCraft.getHitBox()) {
        BlockState tstate = event.getClickedBlock().getWorld().getBlockAt(tloc.getX(), tloc.getY(), tloc.getZ()).getState();
        if (!(tstate instanceof Sign)) {
            continue;
        }
        Sign ts = (Sign) tstate;
        if (isEqualSign(ts, targetText)) {
            if (isForbidden(ts)) {
                if (firstError) {
                    event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Remote Sign - Forbidden string found"));
                    firstError = false;
                }
                event.getPlayer().sendMessage(" - ".concat(tloc.toString()).concat(" : ").concat(ts.getLine(0)));
            } else {
                foundLocations.add(tloc);
            }
        }
    }
    if (!firstError) {
        return;
    } else if (foundLocations.isEmpty()) {
        event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Remote Sign - Could not find target sign"));
        return;
    }
    if (Settings.MaxRemoteSigns > -1) {
        int foundLocCount = foundLocations.size();
        if (foundLocCount > Settings.MaxRemoteSigns) {
            event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Remote Sign - Exceeding maximum allowed"), foundLocCount, Settings.MaxRemoteSigns));
            return;
        }
    }
    for (MovecraftLocation foundLoc : foundLocations) {
        Block newBlock = event.getClickedBlock().getWorld().getBlockAt(foundLoc.getX(), foundLoc.getY(), foundLoc.getZ());
        PlayerInteractEvent newEvent = new PlayerInteractEvent(event.getPlayer(), event.getAction(), event.getItem(), newBlock, event.getBlockFace());
        // TODO: DON'T DO THIS
        Bukkit.getServer().getPluginManager().callEvent(newEvent);
    }
    event.setCancelled(true);
}
Also used : BlockState(org.bukkit.block.BlockState) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) Craft(net.countercraft.movecraft.craft.Craft) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) LinkedList(java.util.LinkedList) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PlayerCraft (net.countercraft.movecraft.craft.PlayerCraft)3 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)2 Craft (net.countercraft.movecraft.craft.Craft)2 BlockState (org.bukkit.block.BlockState)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 LinkedList (java.util.LinkedList)1 PilotedCraft (net.countercraft.movecraft.craft.PilotedCraft)1 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)1 CraftType (net.countercraft.movecraft.craft.type.CraftType)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1 Sign (org.bukkit.block.Sign)1 Switch (org.bukkit.block.data.type.Switch)1 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)1