Simple code doesn't work

Discussion in 'Plugin Development' started by lolcoder, May 6, 2011.

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

    lolcoder

    This should break an area of 10x10 blocks, but it does work only sometime. I can't get the drops and sometimes the client crash (without errors on the server).

    Code:
    package lolcoder.tp;
    
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.inventory.ItemStack;
    
    public class tpBlockListener extends BlockListener {
        public static tp plugin;
    
        public tpBlockListener(tp instance) {
            plugin = instance;
        }
        public void onBlockBreak(BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(player.getName().endsWith("arrior")) {
                World w = player.getWorld();
                Block b;
                for(int i = 0; i < 10; i++) {
                    for(int j = 0; j < 10; j++) {
                        b = w.getBlockAt(block.getX() + i, block.getY(), block.getZ() + j);
                        if(b.getX() != block.getX()) {
                            int t = b.getTypeId();
                            b.setTypeId(0);
                            w.dropItem(b.getLocation(), new ItemStack(t));
                        }
                    }
                }
            }
        }

    EDIT:

    And In the main file I have:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            player.sendMessage("AAA");
            if (commandLabel.equalsIgnoreCase("tppos")) {
                Location loc = player.getLocation();
                player.sendMessage("You are located at:" + loc.getX() + "," + loc.getY() + "," + loc.getZ());
                return true;
            }
            return false;
        } 
    But it doesn't do nothing, it just display that the command doesn't exist.


    Sorry for my bad english.
     
  2. Offline

    nisovin

    You're trying to drop air. Dropping air causes client crashes.
     
  3. Offline

    Deathly

    @lolcoder

    You have to register the command in plugin.yml

    Code:
    commands:
      tppos:
        usage: /<command>
        description: 'Show your coordinates'
    
    @nisovin Where is he dropping air? He is saving the blocks material in a new ItemStack, and dropping it at the original location after setting the block there to air.. No Problems :)
     
  4. Offline

    nisovin

    @Deathly But what if it was air in the first place? Then he'll be dropping an item stack of air.
     
  5. Offline

    Deathly

    @nisovin But if it was already air, then the event wouldnt get called.. ok it depends on the priority he registered it as..

    @lolcoder Add an additional check to prevent dropping air :)
     
  6. Offline

    nisovin

    @Deathly He's manually removing and dropping all blocks near the block broken. Any of those blocks could be air, and he's not checking for that. Thus, he's dropping air.
     
  7. Offline

    Deathly

    Ah right.. yeah.. well just add the check :D
     
  8. Offline

    lolcoder

    Now it works, but... when i get the drop, for example sand, the number of sand in my inventory is still 1.
     
  9. Offline

    Acrobot

    @lolcoder
    Because you drop itemstacks that have 0 amount.
     
  10. Offline

    lolcoder

    @Acrobot: Thank you, now it works.
     
Thread Status:
Not open for further replies.

Share This Page