Updating Plugin CraftItem Stack

Discussion in 'Plugin Development' started by linkrock4, Dec 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    linkrock4

    So as all of you know the CraftItem Stack changed because of fireworks but i cant seem to update it because i dont understand why its not working.

    Code:
    package net.skydevs.src;
     
    import java.util.Random;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.ChunkSnapshot;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.craftbukkit.v1_4_6.block.CraftBlock;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockFormEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.world.ChunkLoadEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Christmas extends JavaPlugin implements Listener {
        Random rand = new Random();
        public String ChristmasMessage;
     
        public void onEnable() { // Server Starts
            getLogger().info("Christmas Plugin has been enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            {
     
                getLogger().info("Checking already loaded chunks for snowy areas...");
     
                for(final World world : getServer().getWorlds())
                {
                    for(final Chunk chunk : world.getLoadedChunks())
                    {
                        checkChunk(chunk);
                    }
                }
            }
        }
     
        public void onDisable() { // Server Stops
            getLogger().info("Christmas Plugin has been disabled!");
     
        }
        //Message
        public boolean onCommand(CommandSender sender, Command cmd, String Label,
                String[] args) {
     
            if (cmd.getName().equalsIgnoreCase("Christmas")) {
                Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.RED
                        + "Christmas" + ChatColor.BLACK + "] " + ChatColor.GREEN
                        + "Happy " + ChatColor.RED
                        + "Christmas! We hope you enjoy the new features we have for you!");
     
            }
     
            return false;
     
        }
        // Presents
        @EventHandler
        @SuppressWarnings("deprecation")
            public void onBlockPlace(BlockPlaceEvent e) {
                if (e.getBlockPlaced().getTypeId() == 19) {
                    int id = rand.nextInt(400);
                    while(Material.getMaterial(id) == null)
                      id = rand.nextInt(400);
                        e.getPlayer().sendMessage(
                                ChatColor.BLACK + "[" + ChatColor.RED + "Christmas"
                                        + ChatColor.BLACK + "] " + ChatColor.GREEN
                                        + "Ho! Ho! Ho! " + ChatColor.RED
                                        + "Merry Christmas! Enjoy your gift!");
     
                        Inventory inv = e.getPlayer().getInventory();
                        ItemStack is;
                        for(int i = 0; i < inv.getSize(); i++) {
                            is = inv.getItem(i);
                            if(is.getTypeId() == 19)
                            {
                                if(is.getAmount() > 1)
                                    is.setAmount(is.getAmount() - 1);
                                else
                                    is = null;
                                inv.setItem(i, is);
                                break;
                            }
                        }
     
                        is = new ItemStack(id, 1);
                        if(id == 137)
                        {
                            CraftItemStack cis = new CraftItemStack(is);
                            cis.getHandle().c(ChatColor.RED + "Present");
                            is = cis;
                        }
                        for(ItemStack item: inv.addItem(is).values())
                            e.getPlayer().getWorld().dropItemNaturally(e.getPlayer().getLocation(), item);
                          e.getPlayer().updateInventory();
                      e.setCancelled(true);
     
            }
            // Weather
     
        }
        // Snow under blocks
        @EventHandler(ignoreCancelled = true)
        public void chunkLoad(final ChunkLoadEvent event)
        {
            checkChunk(event.getChunk());
        }
     
        private void checkChunk(final Chunk chunk)
        {
            final ChunkSnapshot chunkSnap = chunk.getChunkSnapshot(true, false, false);
     
            for(int x = 0; x < 16; x++)
            {
                for(int z = 0; z < 16; z++)
                {
                    final int y = chunkSnap.getHighestBlockYAt(x, z);
     
                    if(chunkSnap.getBlockTypeId(x, y, z) == Material.SNOW.getId())
                        placeSnow(chunk, chunkSnap, x, y, z);
                }
            }
        }
     
        @EventHandler(ignoreCancelled = true)
        public void snowForm(final BlockFormEvent event)
        {
            if(event.getNewState().getType() != Material.SNOW)
                return;
     
            placeSnow(event.getBlock());
        }
     
        private void placeSnow(final Block block)
        {
            final Location loc = block.getLocation();
            final Chunk chunk = block.getChunk();
     
            placeSnow(chunk, chunk.getChunkSnapshot(true, false, false), Math.abs((chunk.getX() * 16) - loc.getBlockX()), loc.getBlockY(), Math.abs((chunk.getZ() * 16) - loc.getBlockZ()));
        }
     
        private void placeSnow(final Chunk chunk, final ChunkSnapshot chunkSnap, final int x, int y, final int z)
        {
            if(y <= 1)
                return;
     
            int type = chunkSnap.getBlockTypeId(x, --y, z);
     
            if(type != Material.LEAVES.getId())
                return;
     
            int lastType = type;
     
            while(true)
            {
                type = chunkSnap.getBlockTypeId(x, --y, z);
     
                switch(Material.getMaterial(type))
                {
                    case AIR:
                    case SNOW:
                        break;
     
                    case LEAVES:
                    {
                        if(lastType == 0)
                            chunk.getBlock(x, y + 1, z).setType(Material.SNOW);
     
                        break;
                    }
     
                    case STONE:
                    case GRASS:
                    case DIRT:
                    case COBBLESTONE:
                    case WOOD:
                    case BEDROCK:
                    case SAND:
                    case GRAVEL:
                    case GOLD_ORE:
                    case IRON_ORE:
                    case COAL_ORE:
                    case LOG:
                    case SPONGE:
                    case GLASS:
                    case LAPIS_ORE:
                    case LAPIS_BLOCK:
                    case DISPENSER:
                    case SANDSTONE:
                    case NOTE_BLOCK:
    //                case PISTON_BASE:
    //                case PISTON_STICKY_BASE:
    //                case PISTON_MOVING_PIECE:
    //                case PISTON_EXTENSION:
                    case WOOL:
                    case GOLD_BLOCK:
                    case IRON_BLOCK:
                    case DOUBLE_STEP:
                    case BRICK:
                    case TNT:
                    case BOOKSHELF:
                    case MOSSY_COBBLESTONE:
                    case OBSIDIAN:
                    case MOB_SPAWNER:
                    case DIAMOND_ORE:
                    case DIAMOND_BLOCK:
                    case WORKBENCH:
                    case FURNACE:
    //                case BURNING_FURNACE:
                    case REDSTONE_ORE:
    //                case ICE:
                    case SNOW_BLOCK:
                    case CLAY:
                    case JUKEBOX:
                    case PUMPKIN:
                    case NETHERRACK:
                    case SOUL_SAND:
                    case GLOWSTONE:
    //                case JACK_O_LANTERN:
                    case SMOOTH_BRICK:
                    case MELON_BLOCK:
                    case NETHER_BRICK:
                    case ENDER_STONE:
                    case REDSTONE_LAMP_OFF:
    //                case REDSTONE_LAMP_ON:
                    case WOOD_DOUBLE_STEP:
                    case EMERALD_BLOCK:
                    case EMERALD_ORE:
                    {
                        if(lastType == 0)
                            chunk.getBlock(x, y + 1, z).setType(Material.SNOW);
     
                        return;
                    }
     
                    default:
                        return;
                }
     
                lastType = type;
            }
            // Color names
     
        }
        // Snowman spawning
     
    }
     
  2. Offline

    Dgauss

    Ok before I posted that I thought they had gotten rid of the Craft in front of the classes, this was misguided. What happened is they moved it one folder deeper. Where it was
    import org.bukkit.craftbukkit.entity.CraftHumanEntity;
    It now needs to be
    import org.bukkit.craftbukkit.v1_4_6.entity.CraftHumanEntity;

    You can also take the original development patch and move all of the filed from the v1_4_6 folder into the craftbukkit folder. (Note:More Code will have to be changed in this case)
     
  3. Offline

    A5H73Y

    I came on here to ask the exact question :L
     
  4. Offline

    linkrock4

    You sir are a genius, but now i seem to have a problem with the:
    * new CraftItemStack();
    * blablabla...getHandle()...blablabla
     
  5. Offline

    fireblast709

    Take a look at the new feature called ItemMeta
     
  6. Offline

    linkrock4

    So basicly gethandle doesnt exsist in craftitemstack anymore, what can it be replaced by?

    EDIT: Fixed ;D
     
  7. Offline

    fireblast709

    the newly added ItemMeta and CraftItemStack.asNMSCopy(original). Also you could use reflection to get the handle (though I do not recommend this method)
     
Thread Status:
Not open for further replies.

Share This Page