Search in sources :

Example 1 with TeleportationEffect

use of net.countercraft.movecraft.processing.tasks.translation.effects.TeleportationEffect in project Movecraft by APDevTeam.

the class TranslationTask method get.

@Override
public Effect get() {
    var preTranslationResult = preTranslationValidators.stream().reduce(MonadicPredicate::and).orElseThrow().validate(craft);
    if (!preTranslationResult.isSucess()) {
        return () -> craft.getAudience().sendMessage(Component.text(preTranslationResult.getMessage()));
    }
    var preTranslateEvent = WorldManager.INSTANCE.executeMain(() -> {
        var event = new CraftPreTranslateEvent(craft, translation.getX(), translation.getY(), translation.getZ(), craft.getWorld());
        Bukkit.getServer().getPluginManager().callEvent(event);
        return event;
    });
    if (preTranslateEvent.isCancelled()) {
        return () -> craft.getAudience().sendMessage(Component.text(preTranslateEvent.getFailMessage()));
    }
    translation = new MovecraftLocation(preTranslateEvent.getDx(), preTranslateEvent.getDy(), preTranslateEvent.getDz());
    destinationWorld = CachedMovecraftWorld.of(preTranslateEvent.getWorld());
    // TODO: Portal movement
    // TODO: Gravity
    var destinationLocations = new SetHitBox();
    var collisions = new SetHitBox();
    var phaseLocations = new SetHitBox();
    var harvestLocations = new SetHitBox();
    var fuelSources = new ArrayList<FurnaceInventory>();
    for (var originLocation : craft.getHitBox()) {
        var originMaterial = craft.getMovecraftWorld().getMaterial(originLocation);
        // Remove air from hitboxes
        if (originMaterial.isAir())
            continue;
        if (Tags.FURNACES.contains(originMaterial)) {
            var state = craft.getMovecraftWorld().getState(originLocation);
            if (state instanceof FurnaceInventory)
                fuelSources.add((FurnaceInventory) state);
        }
        var destination = originLocation.add(translation);
        destinationLocations.add(destination);
        // previous locations cannot collide
        if (craft.getMovecraftWorld().equals(destinationWorld) && craft.getHitBox().contains(destination)) {
            continue;
        }
        var destinationMaterial = destinationWorld.getMaterial(destination);
        if (craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS).contains(destinationMaterial)) {
            phaseLocations.add(destination);
            continue;
        }
        if (craft.getType().getMaterialSetProperty(CraftType.HARVEST_BLOCKS).contains(destinationMaterial) && craft.getType().getMaterialSetProperty(CraftType.HARVESTER_BLADE_BLOCKS).contains(originMaterial)) {
            harvestLocations.add(destination);
            continue;
        }
        collisions.add(destination);
    }
    double fuelBurnRate = (double) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_FUEL_BURN_RATE, preTranslateEvent.getWorld());
    Effect fuelBurnEffect;
    if (craft.getBurningFuel() >= fuelBurnRate) {
        // call event
        final FuelBurnEvent event = new FuelBurnEvent(craft, craft.getBurningFuel(), fuelBurnRate);
        submitEvent(event);
        fuelBurnEffect = () -> craft.setBurningFuel(event.getBurningFuel() - event.getFuelBurnRate());
    } else {
        var fuelSource = findFuelHolders(craft.getType(), fuelSources);
        if (fuelSource == null) {
            return () -> craft.getAudience().sendMessage(I18nSupport.getInternationalisedComponent("Translation - Failed Craft out of fuel"));
        }
        callFuelEvent(craft, findFuelStack(craft.getType(), fuelSource));
        // TODO: Take Fuel
        fuelBurnEffect = () -> Bukkit.getLogger().info("This is where we'd take ur fuel, if we had some");
    }
    var translationResult = translationValidators.stream().reduce(TetradicPredicate::and).orElseThrow().validate(translation, destinationWorld, destinationLocations, craft.getType());
    if (!translationResult.isSucess()) {
        return () -> craft.getAudience().sendMessage(Component.text(translationResult.getMessage()));
    }
    // Direct float comparison due to check for statically initialized value
    callCollisionEvent(craft, collisions, preTranslateEvent.getWorld());
    if (craft.getType().getFloatProperty(CraftType.COLLISION_EXPLOSION) <= 0F && !collisions.isEmpty()) {
        // TODO: collision highlights
        return () -> craft.getAudience().sendMessage(Component.text(String.format(I18nSupport.getInternationalisedString("Translation - Failed Craft is obstructed") + " @ %d,%d,%d,%s", 0, 0, 0, "not_implemented")));
    }
    Effect fluidBoxEffect = fluidBox(craft, translation);
    var translateEvent = callTranslateEvent(craft, destinationLocations, preTranslateEvent.getWorld());
    // TODO: Sinking?
    // TODO: Collision explosion
    // TODO: phase blocks
    Effect movementEffect = moveCraft();
    // TODO: un-phase blocks
    Effect teleportEffect = new TeleportationEffect(craft, translation, translateEvent.getWorld());
    return fuelBurnEffect.andThen(fluidBoxEffect).andThen(movementEffect).andThen(teleportEffect);
}
Also used : FuelBurnEvent(net.countercraft.movecraft.events.FuelBurnEvent) TeleportationEffect(net.countercraft.movecraft.processing.tasks.translation.effects.TeleportationEffect) FurnaceInventory(org.bukkit.inventory.FurnaceInventory) CraftPreTranslateEvent(net.countercraft.movecraft.events.CraftPreTranslateEvent) MonadicPredicate(net.countercraft.movecraft.processing.functions.MonadicPredicate) ArrayList(java.util.ArrayList) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) TeleportationEffect(net.countercraft.movecraft.processing.tasks.translation.effects.TeleportationEffect) Effect(net.countercraft.movecraft.processing.effects.Effect) TetradicPredicate(net.countercraft.movecraft.processing.functions.TetradicPredicate) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Aggregations

ArrayList (java.util.ArrayList)1 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)1 CraftPreTranslateEvent (net.countercraft.movecraft.events.CraftPreTranslateEvent)1 FuelBurnEvent (net.countercraft.movecraft.events.FuelBurnEvent)1 Effect (net.countercraft.movecraft.processing.effects.Effect)1 MonadicPredicate (net.countercraft.movecraft.processing.functions.MonadicPredicate)1 TetradicPredicate (net.countercraft.movecraft.processing.functions.TetradicPredicate)1 TeleportationEffect (net.countercraft.movecraft.processing.tasks.translation.effects.TeleportationEffect)1 SetHitBox (net.countercraft.movecraft.util.hitboxes.SetHitBox)1 FurnaceInventory (org.bukkit.inventory.FurnaceInventory)1