Search in sources :

Example 1 with CraftRotateEvent

use of net.countercraft.movecraft.events.CraftRotateEvent in project Movecraft by APDevTeam.

the class RotationTask method execute.

@Override
protected void execute() {
    if (oldHitBox.isEmpty())
        return;
    if (getCraft().getDisabled() && !(craft instanceof SinkingCraft)) {
        failed = true;
        failMessage = I18nSupport.getInternationalisedString("Translation - Failed Craft Is Disabled");
    }
    // check for fuel, burn some from a furnace if needed. Blocks of coal are supported, in addition to coal and charcoal
    if (!checkFuel()) {
        failMessage = I18nSupport.getInternationalisedString("Translation - Failed Craft out of fuel");
        failed = true;
        return;
    }
    // if a subcraft, find the parent craft. If not a subcraft, it is it's own parent
    Set<Craft> craftsInWorld = CraftManager.getInstance().getCraftsInWorld(getCraft().getWorld());
    Craft parentCraft = getCraft();
    for (Craft craft : craftsInWorld) {
        if (craft != getCraft() && !craft.getHitBox().intersection(oldHitBox).isEmpty()) {
            parentCraft = craft;
            break;
        }
    }
    for (MovecraftLocation originalLocation : oldHitBox) {
        MovecraftLocation newLocation = MathUtils.rotateVec(rotation, originalLocation.subtract(originPoint)).add(originPoint);
        newHitBox.add(newLocation);
        Material oldMaterial = originalLocation.toBukkit(w).getBlock().getType();
        // prevent chests collision
        if (Tags.CHESTS.contains(oldMaterial) && !checkChests(oldMaterial, newLocation)) {
            failed = true;
            failMessage = String.format(I18nSupport.getInternationalisedString("Rotation - Craft is obstructed") + " @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
            break;
        }
        if (!withinWorldBorder(craft.getWorld(), newLocation)) {
            failMessage = I18nSupport.getInternationalisedString("Rotation - Failed Craft cannot pass world border") + String.format(" @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
            failed = true;
            return;
        }
        Material newMaterial = newLocation.toBukkit(w).getBlock().getType();
        if (newMaterial.isAir() || (newMaterial == Material.PISTON_HEAD) || craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS).contains(newMaterial))
            continue;
        if (!oldHitBox.contains(newLocation)) {
            failed = true;
            failMessage = String.format(I18nSupport.getInternationalisedString("Rotation - Craft is obstructed") + " @ %d,%d,%d", newLocation.getX(), newLocation.getY(), newLocation.getZ());
            break;
        }
    }
    if (!oldFluidList.isEmpty()) {
        for (MovecraftLocation fluidLoc : oldFluidList) {
            newFluidList.add(MathUtils.rotateVec(rotation, fluidLoc.subtract(originPoint)).add(originPoint));
        }
    }
    if (failed) {
        if (this.isSubCraft && parentCraft != getCraft()) {
            parentCraft.setProcessing(false);
        }
        return;
    }
    // call event
    CraftRotateEvent event = new CraftRotateEvent(craft, rotation, originPoint, oldHitBox, newHitBox);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        failed = true;
        failMessage = event.getFailMessage();
        return;
    }
    if (parentCraft != craft) {
        parentCraft.getFluidLocations().removeAll(oldFluidList);
        parentCraft.getFluidLocations().addAll(newFluidList);
    }
    updates.add(new CraftRotateCommand(getCraft(), originPoint, rotation));
    // rotate entities in the craft
    Location tOP = new Location(getCraft().getWorld(), originPoint.getX(), originPoint.getY(), originPoint.getZ());
    tOP.setX(tOP.getBlockX() + 0.5);
    tOP.setZ(tOP.getBlockZ() + 0.5);
    if (!(craft instanceof SinkingCraft && craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS)) && craft.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) {
        Location midpoint = new Location(craft.getWorld(), (oldHitBox.getMaxX() + oldHitBox.getMinX()) / 2.0, (oldHitBox.getMaxY() + oldHitBox.getMinY()) / 2.0, (oldHitBox.getMaxZ() + oldHitBox.getMinZ()) / 2.0);
        for (Entity entity : craft.getWorld().getNearbyEntities(midpoint, oldHitBox.getXLength() / 2.0 + 1, oldHitBox.getYLength() / 2.0 + 2, oldHitBox.getZLength() / 2.0 + 1)) {
            if (!craft.getType().getBoolProperty(CraftType.ONLY_MOVE_PLAYERS) || ((entity.getType() == EntityType.PLAYER || entity.getType() == EntityType.PRIMED_TNT) && !(craft instanceof SinkingCraft))) {
                // Player is onboard this craft
                Location adjustedPLoc = entity.getLocation().subtract(tOP);
                double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation, adjustedPLoc.getX(), adjustedPLoc.getZ());
                float newYaw = rotation == MovecraftRotation.CLOCKWISE ? 90F : -90F;
                EntityUpdateCommand eUp = new EntityUpdateCommand(entity, rotatedCoords[0] + tOP.getX() - entity.getLocation().getX(), 0, rotatedCoords[1] + tOP.getZ() - entity.getLocation().getZ(), newYaw, 0);
                updates.add(eUp);
            }
        }
    }
    if (getCraft().getCruising()) {
        if (rotation == MovecraftRotation.ANTICLOCKWISE) {
            // ship faces west
            switch(getCraft().getCruiseDirection()) {
                case WEST:
                    getCraft().setCruiseDirection(CruiseDirection.SOUTH);
                    break;
                // ship faces east
                case EAST:
                    getCraft().setCruiseDirection(CruiseDirection.NORTH);
                    break;
                // ship faces north
                case SOUTH:
                    getCraft().setCruiseDirection(CruiseDirection.EAST);
                    break;
                // ship faces south
                case NORTH:
                    getCraft().setCruiseDirection(CruiseDirection.WEST);
                    break;
            }
        } else if (rotation == MovecraftRotation.CLOCKWISE) {
            // ship faces west
            switch(getCraft().getCruiseDirection()) {
                case WEST:
                    getCraft().setCruiseDirection(CruiseDirection.NORTH);
                    break;
                // ship faces east
                case EAST:
                    getCraft().setCruiseDirection(CruiseDirection.SOUTH);
                    break;
                // ship faces north
                case SOUTH:
                    getCraft().setCruiseDirection(CruiseDirection.WEST);
                    break;
                // ship faces south
                case NORTH:
                    getCraft().setCruiseDirection(CruiseDirection.EAST);
                    break;
            }
        }
    }
    // if you rotated a subcraft, update the parent with the new blocks
    if (this.isSubCraft) {
        // also find the furthest extent from center and notify the player of the new direction
        int farthestX = 0;
        int farthestZ = 0;
        for (MovecraftLocation loc : newHitBox) {
            if (Math.abs(loc.getX() - originPoint.getX()) > Math.abs(farthestX))
                farthestX = loc.getX() - originPoint.getX();
            if (Math.abs(loc.getZ() - originPoint.getZ()) > Math.abs(farthestZ))
                farthestZ = loc.getZ() - originPoint.getZ();
        }
        Component faceMessage = I18nSupport.getInternationalisedComponent("Rotation - Farthest Extent Facing").append(Component.text(" "));
        if (Math.abs(farthestX) > Math.abs(farthestZ)) {
            if (farthestX > 0) {
                faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East"));
            } else {
                faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West"));
            }
        } else {
            if (farthestZ > 0) {
                faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South"));
            } else {
                faceMessage = faceMessage.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North"));
            }
        }
        getCraft().getAudience().sendMessage(faceMessage);
        craftsInWorld = CraftManager.getInstance().getCraftsInWorld(getCraft().getWorld());
        for (Craft craft : craftsInWorld) {
            if (!newHitBox.intersection(craft.getHitBox()).isEmpty() && craft != getCraft()) {
                // craft.setHitBox(newHitBox);
                if (Settings.Debug) {
                    Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size()));
                }
                craft.setHitBox(craft.getHitBox().difference(oldHitBox).union(newHitBox));
                if (Settings.Debug) {
                    Bukkit.broadcastMessage(String.format("Hitbox of craft %s intersects hitbox of craft %s", this.craft.getType().getStringProperty(CraftType.NAME), craft.getType().getStringProperty(CraftType.NAME)));
                    Bukkit.broadcastMessage(String.format("Size of %s hitbox: %d, Size of %s hitbox: %d", this.craft.getType().getStringProperty(CraftType.NAME), newHitBox.size(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size()));
                }
                break;
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) CraftRotateEvent(net.countercraft.movecraft.events.CraftRotateEvent) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) Craft(net.countercraft.movecraft.craft.Craft) Material(org.bukkit.Material) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) EntityUpdateCommand(net.countercraft.movecraft.mapUpdater.update.EntityUpdateCommand) Component(net.kyori.adventure.text.Component) CraftRotateCommand(net.countercraft.movecraft.mapUpdater.update.CraftRotateCommand) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Aggregations

MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)1 Craft (net.countercraft.movecraft.craft.Craft)1 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)1 CraftRotateEvent (net.countercraft.movecraft.events.CraftRotateEvent)1 CraftRotateCommand (net.countercraft.movecraft.mapUpdater.update.CraftRotateCommand)1 EntityUpdateCommand (net.countercraft.movecraft.mapUpdater.update.EntityUpdateCommand)1 Component (net.kyori.adventure.text.Component)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 Entity (org.bukkit.entity.Entity)1