Search in sources :

Example 51 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class FreezeAction method perform.

@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
    Block block = context.getTargetBlock();
    Material material = Material.SNOW;
    if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) {
        if (!freezeWater) {
            return SpellResult.NO_TARGET;
        }
        material = iceMaterial;
    } else if (block.getType() == Material.LAVA) {
        if (!freezeLava) {
            return SpellResult.NO_TARGET;
        }
        material = Material.COBBLESTONE;
    } else if (block.getType() == Material.STATIONARY_LAVA) {
        if (!freezeLava) {
            return SpellResult.NO_TARGET;
        }
        material = Material.OBSIDIAN;
    } else if (block.getType() == Material.FIRE) {
        if (!freezeFire) {
            return SpellResult.NO_TARGET;
        }
        material = Material.AIR;
    } else if (block.getType() == Material.SNOW) {
        material = Material.SNOW;
    } else if (context.isTransparent(block.getType())) {
        return SpellResult.NO_TARGET;
    } else {
        block = block.getRelative(BlockFace.UP);
        // This is kind of ugly, maybe clean it up somehow?
        if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) {
            if (!freezeWater) {
                return SpellResult.NO_TARGET;
            }
            material = iceMaterial;
        } else if (block.getType() == Material.LAVA) {
            if (!freezeLava) {
                return SpellResult.NO_TARGET;
            }
            material = Material.COBBLESTONE;
        } else if (block.getType() == Material.STATIONARY_LAVA) {
            if (!freezeLava) {
                return SpellResult.NO_TARGET;
            }
            material = Material.OBSIDIAN;
        } else if (block.getType() == Material.FIRE) {
            if (!freezeFire) {
                return SpellResult.NO_TARGET;
            }
            material = Material.AIR;
        }
    }
    if (!context.isDestructible(block)) {
        return SpellResult.NO_TARGET;
    }
    context.registerForUndo(block);
    MaterialAndData applyMaterial = new MaterialAndData(material);
    if (block.getType() == Material.SNOW && material == Material.SNOW) {
        short data = block.getData();
        if (data < 7) {
            data++;
        }
        applyMaterial.setData(data);
    }
    applyMaterial.modify(block);
    return SpellResult.CAST;
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) Material(org.bukkit.Material)

Example 52 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class MaterialAndData method isDifferent.

@Override
@SuppressWarnings("deprecation")
public boolean isDifferent(Block block) {
    Material blockMaterial = block.getType();
    byte blockData = block.getData();
    if ((material != null && blockMaterial != material) || (data != null && blockData != data)) {
        return true;
    }
    // Special cases
    if (material == Material.WALL_BANNER || material == Material.STANDING_BANNER) {
        // Can't compare patterns for now
        return true;
    }
    BlockState blockState = block.getState();
    if (blockState instanceof Sign) {
        // Not digging into sign text
        return true;
    } else if (blockState instanceof CommandBlock && extraData != null && extraData instanceof BlockCommand) {
        CommandBlock command = (CommandBlock) blockState;
        if (!command.getCommand().equals(((BlockCommand) extraData).command)) {
            return true;
        }
    } else if (blockState instanceof InventoryHolder) {
        // Just copy it over.... not going to compare inventories :P
        return true;
    }
    return false;
}
Also used : BlockState(org.bukkit.block.BlockState) Material(org.bukkit.Material) Sign(org.bukkit.block.Sign) CommandBlock(org.bukkit.block.CommandBlock) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 53 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class MaterialAndData method updateFromBlock.

@SuppressWarnings("deprecation")
public void updateFromBlock(Block block, @Nullable MaterialSet restrictedMaterials) {
    if (block == null) {
        isValid = false;
        return;
    }
    if (!block.getChunk().isLoaded()) {
        block.getChunk().load(true);
        return;
    }
    if (restrictedMaterials != null && restrictedMaterials.testBlock(block)) {
        isValid = false;
        return;
    }
    // Look for special block states
    extraData = null;
    Material blockMaterial = block.getType();
    material = blockMaterial;
    data = (short) block.getData();
    try {
        BlockState blockState = block.getState();
        if (material == Material.FLOWER_POT || blockState instanceof InventoryHolder || blockState instanceof Sign) {
            extraData = new BlockTileEntity(NMSUtils.getTileEntityData(block.getLocation()));
        } else if (blockState instanceof CommandBlock) {
            // This seems to occasionally throw exceptions...
            CommandBlock command = (CommandBlock) blockState;
            extraData = new BlockCommand(command.getCommand(), command.getName());
        } else if (blockState instanceof Skull) {
            Skull skull = (Skull) blockState;
            data = (short) skull.getSkullType().ordinal();
            extraData = new BlockSkull(InventoryUtils.getSkullProfile(skull), skull.getSkullType(), skull.getRotation());
        } else if (blockState instanceof CreatureSpawner) {
            CreatureSpawner spawner = (CreatureSpawner) blockState;
            extraData = new BlockMobSpawner(spawner.getCreatureTypeName());
        } else if (blockMaterial == Material.STANDING_BANNER || blockMaterial == Material.WALL_BANNER) {
            if (blockState != null && blockState instanceof Banner) {
                Banner banner = (Banner) blockState;
                DyeColor color = banner.getBaseColor();
                extraData = new BlockBanner(banner.getPatterns(), color);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    isValid = true;
}
Also used : Banner(org.bukkit.block.Banner) Material(org.bukkit.Material) CommandBlock(org.bukkit.block.CommandBlock) DyeColor(org.bukkit.DyeColor) CreatureSpawner(org.bukkit.block.CreatureSpawner) BlockState(org.bukkit.block.BlockState) Skull(org.bukkit.block.Skull) Sign(org.bukkit.block.Sign) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 54 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class MaterialAndData method update.

public void update(String materialKey) {
    if (materialKey == null || materialKey.length() == 0) {
        isValid = false;
        return;
    }
    String[] pieces = splitMaterialKey(materialKey);
    Short data = 0;
    Material material = null;
    BlockExtraData extraData = null;
    try {
        if (pieces.length > 0) {
            if (!pieces[0].equals("*")) {
                // Legacy material id loading
                try {
                    Integer id = Integer.parseInt(pieces[0]);
                    material = DeprecatedUtils.getMaterial(id);
                } catch (Exception ex) {
                    material = Material.getMaterial(pieces[0].toUpperCase());
                }
            }
        }
    } catch (Exception ex) {
        material = null;
    }
    try {
        if (pieces.length > 1) {
            // Some special-cases
            if (pieces[1].equals("*")) {
                data = null;
            } else if (material == Material.MOB_SPAWNER) {
                extraData = new BlockMobSpawner(pieces[1]);
            } else if (material == Material.SKULL_ITEM) {
                if (pieces.length > 2) {
                    data = 3;
                    String dataString = pieces[1];
                    for (int i = 2; i < pieces.length; i++) {
                        dataString += ":" + pieces[i];
                    }
                    ItemStack item = InventoryUtils.getURLSkull(dataString);
                    extraData = new BlockSkull(InventoryUtils.getSkullProfile(item.getItemMeta()), SkullType.PLAYER);
                } else {
                    try {
                        data = Short.parseShort(pieces[1]);
                    } catch (Exception ex) {
                        data = 3;
                        extraData = new BlockSkull(pieces[1]);
                    }
                }
            } else if (material == Material.STANDING_BANNER || material == Material.WALL_BANNER || material == Material.BANNER) {
                DyeColor color = null;
                try {
                    short colorIndex = Short.parseShort(pieces[1]);
                    data = colorIndex;
                    color = DyeColor.values()[colorIndex];
                } catch (Exception ex) {
                    color = null;
                }
                if (color != null) {
                    extraData = new BlockBanner(color);
                }
            } else if (material == Material.LEATHER_BOOTS || material == Material.LEATHER_CHESTPLATE || material == Material.LEATHER_HELMET || material == Material.LEATHER_LEGGINGS) {
                StringUtils.split(pieces[1], ',');
                for (String piece : pieces) {
                    if (piece.startsWith("#")) {
                        try {
                            Color color = Color.fromRGB(Integer.parseInt(piece.substring(1), 16));
                            extraData = new LeatherArmorData(color);
                        } catch (Exception ex) {
                            extraData = null;
                        }
                    } else {
                        try {
                            data = Short.parseShort(pieces[1]);
                        } catch (Exception ex) {
                            data = 0;
                        }
                    }
                }
            } else {
                try {
                    data = Short.parseShort(pieces[1]);
                } catch (Exception ex) {
                    data = 0;
                }
            }
        }
    } catch (Exception ex) {
        material = null;
    }
    if (material == null) {
        this.setMaterial(null, null);
        isValid = false;
    } else {
        setMaterial(material, data);
    }
    if (isValid) {
        this.extraData = extraData;
    }
}
Also used : Color(org.bukkit.Color) DyeColor(org.bukkit.DyeColor) Material(org.bukkit.Material) DyeColor(org.bukkit.DyeColor) ItemStack(org.bukkit.inventory.ItemStack)

Example 55 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class Schematic method load.

@SuppressWarnings("deprecation")
public void load(short width, short height, short length, short[] blockTypes, byte[] data, Collection<Object> tileEntityData, Collection<Object> entityData, Vector origin, Vector offset) {
    size = new Vector(width, height, length);
    center = new Vector(Math.floor(size.getBlockX() / 2), 0, Math.floor(size.getBlockZ() / 2));
    blocks = new MaterialAndData[width][height][length];
    entities = new ArrayList<>();
    // Load entities
    for (Object entity : entityData) {
        String type = NMSUtils.getMetaString(entity, "id");
        Vector position = NMSUtils.getPosition(entity, "Pos");
        if (position == null)
            continue;
        position = position.subtract(origin).subtract(center);
        if (type == null)
            continue;
        // Only doing paintings and item frames for now.
        if (type.equals("Painting")) {
            String motive = NMSUtils.getMetaString(entity, "Motive");
            motive = motive.toLowerCase();
            Art art = Art.ALBAN;
            for (Art test : Art.values()) {
                if (test.name().toLowerCase().replace("_", "").equals(motive)) {
                    art = test;
                    break;
                }
            }
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            EntityData painting = com.elmakers.mine.bukkit.entity.EntityData.loadPainting(position, art, getFacing(facing));
            entities.add(painting);
        } else if (type.equals("ItemFrame")) {
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            byte rotation = NMSUtils.getMetaByte(entity, "ItemRotation");
            Rotation rot = Rotation.NONE;
            if (rotation < Rotation.values().length) {
                rot = Rotation.values()[rotation];
            }
            ItemStack item = NMSUtils.getItem(NMSUtils.getNode(entity, "Item"));
            EntityData itemFrame = com.elmakers.mine.bukkit.entity.EntityData.loadItemFrame(position, item, getFacing(facing), rot);
            entities.add(itemFrame);
        }
    }
    // Map tile entity data
    Map<BlockVector, Object> tileEntityMap = new HashMap<>();
    for (Object tileEntity : tileEntityData) {
        try {
            Integer x = NMSUtils.getMetaInt(tileEntity, "x");
            Integer y = NMSUtils.getMetaInt(tileEntity, "y");
            Integer z = NMSUtils.getMetaInt(tileEntity, "z");
            if (x == null || y == null || z == null)
                continue;
            BlockVector location = new BlockVector(x, y, z);
            tileEntityMap.put(location, tileEntity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    for (int y = 0; y < height; y++) {
        for (int z = 0; z < length; z++) {
            for (int x = 0; x < width; x++) {
                int index = x + (y * length + z) * width;
                Material material = null;
                try {
                    material = Material.getMaterial(blockTypes[index]);
                } catch (Exception ex) {
                    material = null;
                    ex.printStackTrace();
                }
                if (material != null) {
                    MaterialAndData block = new com.elmakers.mine.bukkit.block.MaterialAndData(material, data[index]);
                    // Check for tile entity data
                    BlockVector blockLocation = new BlockVector(x, y, z);
                    Object tileEntity = tileEntityMap.get(blockLocation);
                    if (tileEntity != null) {
                        try {
                            if (material == Material.COMMAND) {
                                String customName = NMSUtils.getMetaString(tileEntity, "CustomName");
                                if (!customName.isEmpty()) {
                                    block.setCustomName(customName);
                                }
                                block.setCommandLine(NMSUtils.getMetaString(tileEntity, "Command"));
                            } else {
                                block.setRawData(tileEntity);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    blocks[x][y][z] = block;
                }
            }
        }
    }
    loaded = true;
}
Also used : Art(org.bukkit.Art) HashMap(java.util.HashMap) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) Material(org.bukkit.Material) Rotation(org.bukkit.Rotation) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) BlockVector(org.bukkit.util.BlockVector) ItemStack(org.bukkit.inventory.ItemStack) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Aggregations

Material (org.bukkit.Material)427 ItemStack (org.bukkit.inventory.ItemStack)99 Block (org.bukkit.block.Block)87 EventHandler (org.bukkit.event.EventHandler)51 ArrayList (java.util.ArrayList)46 Player (org.bukkit.entity.Player)44 Location (org.bukkit.Location)42 Vector (org.bukkit.util.Vector)27 EntityType (org.bukkit.entity.EntityType)26 GlowBlock (net.glowstone.block.GlowBlock)24 Test (org.junit.Test)24 BlockFace (org.bukkit.block.BlockFace)23 Entity (org.bukkit.entity.Entity)22 MaterialData (org.bukkit.material.MaterialData)22 World (org.bukkit.World)20 HashMap (java.util.HashMap)19 IOException (java.io.IOException)17 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)16 File (java.io.File)15 HashSet (java.util.HashSet)13