Search in sources :

Example 1 with Bed

use of org.bukkit.block.data.type.Bed in project Prism-Bukkit by prism.

the class BlockAction method handleApply.

/**
 * The BlockState object is modified by this method.
 *
 * @param block            Block
 * @param originalBlock    BlockState
 * @param parameters       QueryParameters
 * @param cancelIfBadPlace cancelIfBadPlace
 * @return ChangeResult.
 */
@NotNull
private ChangeResult handleApply(final Block block, final BlockState originalBlock, final PrismParameters parameters, final boolean cancelIfBadPlace) {
    BlockState state = block.getState();
    // So this will ensure the head of the bed is broken when removing the bed during rollback.
    if (MaterialTag.BEDS.isTagged(state.getType())) {
        state = Utilities.getSiblingForDoubleLengthBlock(state).getState();
    }
    switch(getMaterial()) {
        case LILY_PAD:
            // If restoring a lily pad, check that block below is water.
            // Be sure it's set to stationary water so the lily pad will stay
            final Block below = block.getRelative(BlockFace.DOWN);
            if (below.getType().equals(WATER) || below.getType().equals(AIR)) {
                below.setType(WATER);
            } else {
                // Prism.debug("Lilypad skipped because no water exists below.");
                return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
            }
            break;
        case NETHER_PORTAL:
            // Only way to restore a nether portal is to set it on fire.
            final Block obsidian = Utilities.getFirstBlockOfMaterialBelow(OBSIDIAN, block.getLocation());
            if (obsidian != null) {
                final Block above = obsidian.getRelative(BlockFace.UP);
                if (!(above.getType() == NETHER_PORTAL)) {
                    above.setType(FIRE);
                    return new ChangeResultImpl(ChangeResultType.APPLIED, null);
                }
            }
            break;
        case JUKEBOX:
            setBlockData(Bukkit.createBlockData(JUKEBOX));
            break;
        default:
            break;
    }
    state.setType(getMaterial());
    state.setBlockData(getBlockData());
    state.update(true);
    BlockState newState = block.getState();
    BlockActionData blockActionData = getActionData();
    if (blockActionData != null) {
        if ((getMaterial() == PLAYER_HEAD || getMaterial() == PLAYER_WALL_HEAD) && blockActionData instanceof SkullActionData) {
            return handleSkulls(block, blockActionData, originalBlock);
        }
        if (Tag.BANNERS.isTagged(getMaterial()) && blockActionData instanceof BannerActionData) {
            return handleBanners(block, blockActionData, originalBlock);
        }
        if (getMaterial() == SPAWNER && blockActionData instanceof SpawnerActionData) {
            final SpawnerActionData s = (SpawnerActionData) blockActionData;
            // Set spawner data
            ((CreatureSpawner) newState).setDelay(s.getDelay());
            ((CreatureSpawner) newState).setSpawnedType(s.getEntityType());
        }
        if (getMaterial() == COMMAND_BLOCK && blockActionData instanceof CommandActionData) {
            final CommandActionData c = (CommandActionData) blockActionData;
            ((CommandBlock) newState).setCommand(c.command);
        }
        if (newState instanceof Nameable && blockActionData.customName != null && !blockActionData.customName.equals("")) {
            ((Nameable) newState).setCustomName(blockActionData.customName);
        }
        if (parameters.getProcessType() == PrismProcessType.ROLLBACK && Tag.SIGNS.isTagged(getMaterial()) && blockActionData instanceof SignActionData) {
            final SignActionData s = (SignActionData) blockActionData;
            // https://snowy-evening.com/botsko/prism/455/
            if (newState instanceof Sign) {
                if (s.lines != null) {
                    for (int i = 0; i < s.lines.length; ++i) {
                        ((Sign) newState).setLine(i, s.lines[i]);
                    }
                }
            }
        }
    } else {
        Prism.debug("BlockAction Data was null with " + parameters.toString());
    }
    // -----------------------------
    // Sibling logic marker
    // If the material is a crop that needs soil, we must restore the soil
    // This may need to go before setting the block, but I prefer the BlockUtil logic to use materials.
    BlockState sibling = null;
    if (Utilities.materialRequiresSoil(getMaterial())) {
        sibling = block.getRelative(BlockFace.DOWN).getState();
        if (cancelIfBadPlace && !MaterialTag.SOIL_CANDIDATES.isTagged(sibling.getType())) {
            Prism.debug(parameters.getProcessType().name() + " skipped due to lack of soil for " + getMaterial().name());
            return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
        }
        sibling.setType(FARMLAND);
    }
    // Chest sides can be broken independently, ignore them
    if (newState.getType() != CHEST && newState.getType() != TRAPPED_CHEST) {
        final Block s = Utilities.getSiblingForDoubleLengthBlock(state);
        if (s != null) {
            sibling = s.getState();
            if (cancelIfBadPlace && !Utilities.isAcceptableForBlockPlace(sibling.getType())) {
                Prism.debug(parameters.getProcessType().name() + " skipped due to lack of wrong sibling type for " + getMaterial().name());
                return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
            }
            sibling.setType(block.getType());
            BlockData siblingData = getBlockData().clone();
            if (siblingData instanceof Bed) {
                // We always log the foot
                ((Bed) siblingData).setPart(Part.HEAD);
            } else if (siblingData instanceof Bisected) {
                // We always log the bottom
                ((Bisected) siblingData).setHalf(Half.TOP);
            }
            sibling.setBlockData(siblingData);
        }
    }
    boolean physics = !parameters.hasFlag(Flag.NO_PHYS);
    newState.update(true, physics);
    if (sibling != null) {
        sibling.update(true, physics);
    }
    return new ChangeResultImpl(ChangeResultType.APPLIED, new BlockStateChangeImpl(originalBlock, state));
}
Also used : Bed(org.bukkit.block.data.type.Bed) CommandBlock(org.bukkit.block.CommandBlock) CreatureSpawner(org.bukkit.block.CreatureSpawner) Nameable(org.bukkit.Nameable) BlockState(org.bukkit.block.BlockState) BlockStateChangeImpl(me.botsko.prism.events.BlockStateChangeImpl) Block(org.bukkit.block.Block) CommandBlock(org.bukkit.block.CommandBlock) Sign(org.bukkit.block.Sign) ChangeResultImpl(me.botsko.prism.appliers.ChangeResultImpl) BlockData(org.bukkit.block.data.BlockData) Bisected(org.bukkit.block.data.Bisected) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Bed

use of org.bukkit.block.data.type.Bed in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method sitting.

@Command(aliases = { "npc" }, usage = "sit (--location x,y,z,world) (--anchor anchor_name) (-c)", desc = "Makes the NPC sit.", flags = "c", modifiers = { "sit" }, min = 1, max = 3, permission = "denizen.npc.sit")
@Requirements(selected = true, ownership = true)
public void sitting(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (npc.hasTrait(SneakingTrait.class)) {
        npc.getOrAddTrait(SneakingTrait.class).stand();
        npc.removeTrait(SneakingTrait.class);
    }
    if (npc.hasTrait(SleepingTrait.class)) {
        npc.getOrAddTrait(SleepingTrait.class).wakeUp();
        npc.removeTrait(SleepingTrait.class);
    }
    SittingTrait trait = npc.getOrAddTrait(SittingTrait.class);
    if (args.hasValueFlag("location")) {
        LocationTag location = LocationTag.valueOf(args.getFlag("location"), CoreUtilities.basicContext);
        if (location == null) {
            Messaging.sendError(sender, "Usage: /npc sit --location x,y,z,world");
            return;
        }
        trait.sit(location);
        return;
    } else if (args.hasValueFlag("anchor")) {
        if (npc.hasTrait(Anchors.class)) {
            Anchors anchors = npc.getOrAddTrait(Anchors.class);
            if (anchors.getAnchor(args.getFlag("anchor")) != null) {
                trait.sit(anchors.getAnchor(args.getFlag("anchor")).getLocation());
                Messaging.send(sender, npc.getName() + " is now sitting.");
                return;
            }
        }
        Messaging.sendError(sender, "The NPC does not have the specified anchor!");
        return;
    }
    Location targetLocation;
    if (args.hasFlag('c')) {
        targetLocation = args.getSenderTargetBlockLocation().clone().add(0.5, 0, 0.5);
        targetLocation.setYaw(npc.getStoredLocation().getYaw());
    } else {
        targetLocation = npc.getStoredLocation().clone();
        targetLocation.add(0, -0.2, 0);
    }
    if (trait.isSitting()) {
        Messaging.send(sender, npc.getName() + " is already sitting, use '/npc stand' to stand the NPC back up.");
        return;
    }
    Block block = targetLocation.getBlock();
    BlockData data = block.getBlockData();
    if (data instanceof Stairs || data instanceof Bed || (data instanceof Slab && ((Slab) data).getType() == Slab.Type.BOTTOM)) {
        targetLocation.setY(targetLocation.getBlockY() + 0.3);
    } else if (data instanceof Campfire) {
        targetLocation.setY(targetLocation.getBlockY() + 0.2);
    } else if (block.getType().name().endsWith("CARPET")) {
        targetLocation.setY(targetLocation.getBlockY());
    } else if (block.getType().isSolid()) {
        targetLocation.setY(targetLocation.getBlockY() + 0.8);
    }
    trait.sit(targetLocation);
    Messaging.send(sender, npc.getName() + " is now sitting.");
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Anchors(net.citizensnpcs.trait.Anchors) Bed(org.bukkit.block.data.type.Bed) Campfire(org.bukkit.block.data.type.Campfire) Stairs(org.bukkit.block.data.type.Stairs) Block(org.bukkit.block.Block) BlockData(org.bukkit.block.data.BlockData) Slab(org.bukkit.block.data.type.Slab) Location(org.bukkit.Location) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Aggregations

Block (org.bukkit.block.Block)2 BlockData (org.bukkit.block.data.BlockData)2 Bed (org.bukkit.block.data.type.Bed)2 LocationTag (com.denizenscript.denizen.objects.LocationTag)1 ChangeResultImpl (me.botsko.prism.appliers.ChangeResultImpl)1 BlockStateChangeImpl (me.botsko.prism.events.BlockStateChangeImpl)1 Command (net.citizensnpcs.api.command.Command)1 Requirements (net.citizensnpcs.api.command.Requirements)1 Anchors (net.citizensnpcs.trait.Anchors)1 Location (org.bukkit.Location)1 Nameable (org.bukkit.Nameable)1 BlockState (org.bukkit.block.BlockState)1 CommandBlock (org.bukkit.block.CommandBlock)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1 Sign (org.bukkit.block.Sign)1 Bisected (org.bukkit.block.data.Bisected)1 Campfire (org.bukkit.block.data.type.Campfire)1 Slab (org.bukkit.block.data.type.Slab)1 Stairs (org.bukkit.block.data.type.Stairs)1 NotNull (org.jetbrains.annotations.NotNull)1