Search in sources :

Example 31 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class LocationTrieSet method contains.

@Override
public boolean contains(Object o) {
    if (o instanceof MovecraftLocation) {
        MovecraftLocation location = (MovecraftLocation) o;
        long packed = location.pack();
        return contains(packed);
    }
    return false;
}
Also used : MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 32 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class CruiseSign method onCraftDetect.

@EventHandler
public void onCraftDetect(CraftDetectEvent event) {
    World world = event.getCraft().getWorld();
    for (MovecraftLocation location : event.getCraft().getHitBox()) {
        var block = location.toBukkit(world).getBlock();
        if (!Tag.SIGNS.isTagged(block.getType())) {
            continue;
        }
        BlockState state = block.getState();
        if (state instanceof Sign) {
            Sign sign = (Sign) state;
            if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: ON")) {
                sign.setLine(0, "Cruise: OFF");
                sign.update();
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) Sign(org.bukkit.block.Sign) WallSign(org.bukkit.block.data.type.WallSign) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) EventHandler(org.bukkit.event.EventHandler)

Example 33 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class NameSign method onCraftDetect.

@EventHandler
public void onCraftDetect(@NotNull CraftDetectEvent event) {
    Craft c = event.getCraft();
    if (c instanceof PilotedCraft) {
        PilotedCraft pilotedCraft = (PilotedCraft) c;
        if (Settings.RequireNamePerm && !pilotedCraft.getPilot().hasPermission("movecraft.name.place"))
            return;
    }
    World w = c.getWorld();
    for (MovecraftLocation location : c.getHitBox()) {
        var block = location.toBukkit(w).getBlock();
        if (!Tag.SIGNS.isTagged(block.getType())) {
            continue;
        }
        BlockState state = block.getState();
        if (!(state instanceof Sign)) {
            return;
        }
        Sign sign = (Sign) state;
        if (sign.getLine(0).equalsIgnoreCase(HEADER)) {
            String name = Arrays.stream(sign.getLines()).skip(1).filter(f -> f != null && !f.trim().isEmpty()).collect(Collectors.joining(" "));
            c.setName(name);
            return;
        }
    }
}
Also used : Sign(org.bukkit.block.Sign) Arrays(java.util.Arrays) Tag(org.bukkit.Tag) Craft(net.countercraft.movecraft.craft.Craft) BlockState(org.bukkit.block.BlockState) CraftDetectEvent(net.countercraft.movecraft.events.CraftDetectEvent) Collectors(java.util.stream.Collectors) SignChangeEvent(org.bukkit.event.block.SignChangeEvent) Settings(net.countercraft.movecraft.config.Settings) EventHandler(org.bukkit.event.EventHandler) World(org.bukkit.World) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) NotNull(org.jetbrains.annotations.NotNull) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) ChatUtils(net.countercraft.movecraft.util.ChatUtils) Listener(org.bukkit.event.Listener) BlockState(org.bukkit.block.BlockState) Craft(net.countercraft.movecraft.craft.Craft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) Sign(org.bukkit.block.Sign) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) EventHandler(org.bukkit.event.EventHandler)

Example 34 with MovecraftLocation

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

Example 35 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation 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

MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)60 Craft (net.countercraft.movecraft.craft.Craft)18 EventHandler (org.bukkit.event.EventHandler)18 Material (org.bukkit.Material)17 World (org.bukkit.World)15 Sign (org.bukkit.block.Sign)14 BlockState (org.bukkit.block.BlockState)13 Block (org.bukkit.block.Block)12 ArrayList (java.util.ArrayList)11 Location (org.bukkit.Location)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SetHitBox (net.countercraft.movecraft.util.hitboxes.SetHitBox)9 HashSet (java.util.HashSet)8 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)8 NotNull (org.jetbrains.annotations.NotNull)8 MovecraftRotation (net.countercraft.movecraft.MovecraftRotation)6 HitBox (net.countercraft.movecraft.util.hitboxes.HitBox)6 SolidHitBox (net.countercraft.movecraft.util.hitboxes.SolidHitBox)6 LinkedList (java.util.LinkedList)5