Search in sources :

Example 26 with BlockData

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

the class MaterialSwitchable method describes.

public static boolean describes(ObjectTag material) {
    if (!(material instanceof MaterialTag)) {
        return false;
    }
    MaterialTag mat = (MaterialTag) material;
    if (!mat.hasModernData()) {
        return false;
    }
    BlockData data = mat.getModernData();
    return data instanceof Powerable || data instanceof Openable || data instanceof Dispenser || data instanceof DaylightDetector || data instanceof Piston || data instanceof Lightable || data instanceof EndPortalFrame || data instanceof Hopper;
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Lightable(org.bukkit.block.data.Lightable) Powerable(org.bukkit.block.data.Powerable) BlockData(org.bukkit.block.data.BlockData) Openable(org.bukkit.block.data.Openable)

Example 27 with BlockData

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

the class AdjustBlockCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag mechanismName = scriptEntry.getElement("mechanism");
    ObjectTag value = scriptEntry.getObjectTag("mechanism_value");
    ElementTag noPhysics = scriptEntry.getElement("no_physics");
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("locations", locations), mechanismName, noPhysics, value);
    }
    boolean doPhysics = noPhysics == null || !noPhysics.asBoolean();
    for (LocationTag location : locations) {
        Block block = location.getBlock();
        BlockData data = block.getBlockData();
        MaterialTag specialMaterial = new MaterialTag(data);
        Mechanism mechanism = new Mechanism(mechanismName.asString(), value, scriptEntry.getContext());
        specialMaterial.safeAdjust(mechanism);
        if (doPhysics) {
            block.setBlockData(data, false);
            applyPhysicsAt(location);
        } else {
            ModifyBlockCommand.setBlock(block.getLocation(), specialMaterial, false, null);
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Block(org.bukkit.block.Block) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) BlockData(org.bukkit.block.data.BlockData)

Example 28 with BlockData

use of org.bukkit.block.data.BlockData 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)

Example 29 with BlockData

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

the class SpongeSchematicHelper method fromSpongeStream.

// Referenced from WorldEdit source and Sponge schematic format v2 documentation
// Some values are custom and specific to Denizen
public static CuboidBlockSet fromSpongeStream(InputStream is) {
    boolean isPrimary = Bukkit.isPrimaryThread();
    CuboidBlockSet cbs = new CuboidBlockSet();
    try {
        NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(is));
        NamedTag rootTag = nbtStream.readNamedTag();
        nbtStream.close();
        if (!rootTag.getName().equals("Schematic")) {
            throw new Exception("Tag 'Schematic' does not exist or is not first!");
        }
        CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
        Map<String, Tag> schematic = schematicTag.getValue();
        if (schematic.containsKey("DenizenEntities")) {
            String entities = stringifyTag(schematic.get("DenizenEntities"));
            cbs.entities = ListTag.valueOf(entities, CoreUtilities.errorButNoDebugContext);
        }
        short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
        short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
        short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
        int originX = 0;
        int originY = 0;
        int originZ = 0;
        if (schematic.containsKey("DenizenOffset")) {
            // Note: "Offset" contains complete nonsense from WE, so just don't touch it.
            int[] offsetArr = getChildTag(schematic, "DenizenOffset", IntArrayTag.class).getValue();
            originX = offsetArr[0];
            originY = offsetArr[1];
            originZ = offsetArr[2];
        }
        cbs.x_width = width;
        cbs.z_height = length;
        cbs.y_length = height;
        cbs.center_x = originX;
        cbs.center_y = originY;
        cbs.center_z = originZ;
        cbs.blocks = new FullBlockData[width * length * height];
        Map<String, Tag> paletteMap = getChildTag(schematic, "Palette", CompoundTag.class).getValue();
        HashMap<Integer, BlockData> palette = new HashMap<>(256);
        List<Map.Entry<Integer, String>> latePairs = isPrimary ? null : new ArrayList<>();
        for (String key : paletteMap.keySet()) {
            int id = getChildTag(paletteMap, key, IntTag.class).getValue();
            if (isPrimary) {
                palette.put(id, blockDataCache.computeIfAbsent(key, SpongeSchematicHelper::unstableParseMaterial));
            } else {
                BlockData entry = blockDataCache.get(key);
                if (entry != null) {
                    palette.put(id, entry);
                } else {
                    latePairs.add(new AbstractMap.SimpleEntry<>(id, key));
                }
            }
        }
        if (!isPrimary && !latePairs.isEmpty()) {
            BoolHolder bool = new BoolHolder();
            Bukkit.getScheduler().runTask(Denizen.getInstance(), () -> {
                for (Map.Entry<Integer, String> pair : latePairs) {
                    palette.put(pair.getKey(), blockDataCache.computeIfAbsent(pair.getValue(), SpongeSchematicHelper::unstableParseMaterial));
                }
                bool.bool = true;
            });
            for (int i = 0; i < 1000; i++) {
                Thread.sleep(50);
                if (bool.bool) {
                    break;
                }
            }
        }
        Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<>();
        if (schematic.containsKey("BlockEntities")) {
            List<Tag> tileEntities = getChildTag(schematic, "BlockEntities", JNBTListTag.class).getValue();
            for (Tag tag : tileEntities) {
                if (!(tag instanceof CompoundTag)) {
                    continue;
                }
                CompoundTag t = (CompoundTag) tag;
                int[] pos = getChildTag(t.getValue(), "Pos", IntArrayTag.class).getValue();
                int x = pos[0];
                int y = pos[1];
                int z = pos[2];
                BlockVector vec = new BlockVector(x, y, z);
                tileEntitiesMap.put(vec, t.getValue());
            }
        }
        byte[] blocks = getChildTag(schematic, "BlockData", ByteArrayTag.class).getValue();
        int i = 0;
        int index = 0;
        while (i < blocks.length) {
            int value = 0;
            int varintLength = 0;
            while (true) {
                value |= (blocks[i] & 127) << (varintLength++ * 7);
                if (varintLength > 5) {
                    throw new Exception("Schem file blocks tag data corrupted");
                }
                if ((blocks[i] & 128) != 128) {
                    i++;
                    break;
                }
                i++;
            }
            FullBlockData block = new FullBlockData(palette.get(value));
            int y = index / (width * length);
            int z = (index % (width * length)) / width;
            int x = (index % (width * length)) % width;
            int cbsIndex = z + y * cbs.z_height + x * cbs.z_height * cbs.y_length;
            BlockVector pt = new BlockVector(x, y, z);
            if (tileEntitiesMap.containsKey(pt)) {
                block.tileEntityData = NMSHandler.getInstance().createCompoundTag(tileEntitiesMap.get(pt));
            }
            cbs.blocks[cbsIndex] = block;
            index++;
        }
        if (schematic.containsKey("DenizenFlags")) {
            Map<String, Tag> flags = getChildTag(schematic, "DenizenFlags", CompoundTag.class).getValue();
            for (Map.Entry<String, Tag> flagData : flags.entrySet()) {
                int flagIndex = Integer.valueOf(flagData.getKey());
                cbs.blocks[flagIndex].flags = MapTag.valueOf(stringifyTag(flagData.getValue()), CoreUtilities.noDebugContext);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to load Sponge-format schematic file", e);
    }
    return cbs;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GZIPInputStream(java.util.zip.GZIPInputStream) BlockData(org.bukkit.block.data.BlockData) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) BlockVector(org.bukkit.util.BlockVector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 30 with BlockData

use of org.bukkit.block.data.BlockData in project EliteMobs by MagmaGuy.

the class TransitiveBlock method serializeTransitiveBlocks.

public static List<TransitiveBlock> serializeTransitiveBlocks(List<String> deserializedList, String filename) {
    List<TransitiveBlock> transitiveBlocks = new ArrayList<>();
    for (String deserializedString : deserializedList) {
        try {
            String[] elements = deserializedString.split("/");
            String[] vector = elements[0].split(",");
            double x = Double.parseDouble(vector[0]);
            double y = Double.parseDouble(vector[1]);
            double z = Double.parseDouble(vector[2]);
            BlockData blockData = Bukkit.getServer().createBlockData(elements[1]);
            transitiveBlocks.add(new TransitiveBlock(blockData, new Vector(x, y, z)));
        } catch (Exception ex) {
            new WarningMessage("Failed to serialize Transitive Block! Issue with entry " + deserializedString + " in file " + filename);
        }
    }
    return transitiveBlocks;
}
Also used : WarningMessage(com.magmaguy.elitemobs.utils.WarningMessage) ArrayList(java.util.ArrayList) BlockData(org.bukkit.block.data.BlockData) Vector(org.bukkit.util.Vector)

Aggregations

BlockData (org.bukkit.block.data.BlockData)34 Block (org.bukkit.block.Block)10 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)8 Location (org.bukkit.Location)7 MinigamePlayer (au.com.mineauz.minigames.objects.MinigamePlayer)3 ArrayList (java.util.ArrayList)3 Material (org.bukkit.Material)3 BlockState (org.bukkit.block.BlockState)3 Lightable (org.bukkit.block.data.Lightable)3 ItemStack (org.bukkit.inventory.ItemStack)3 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 MaterialState (me.botsko.prism.api.objects.MaterialState)2 BlockStateChangeImpl (me.botsko.prism.events.BlockStateChangeImpl)2 Bisected (org.bukkit.block.data.Bisected)2 Bed (org.bukkit.block.data.type.Bed)2 Player (org.bukkit.entity.Player)2