sand and timer

Discussion in 'Plugin Development' started by Brexima, Aug 6, 2012.

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

    Brexima

    hey guys,
    im beginner at java and i did the function like /sand after use it when i look anywhere game summon a sand and it will remove after 5 seconds how can i do this, i hope u understand me my english is not perfect :D

    nobody know how to summon item ? =(

    upp.. :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. Offline

    ZeusAllMighty11 Retired Staff

    Is this a request ? Or are you having issues with current code?

    If you're looking for basic code, here it is:

    when a player types command
    if command is "sand"
    if player has permission sand.despawn
    player add sandRemove list

    eventhandler
    method name(BlockVector ?)
    if block at xyz is type sand
    if player is in list sandRemove
    sand set type to air
     
  3. Offline

    Malikk

    You can't 'summon' a block, you can only change a block's type. So, in this case, from air to sand.

    You would do something along these lines, (psuedo code)
    Code:
    CommandSender sender; // from your command
    Player player = (Player)sender;
     
    Block block = player.getTargetBlock(params);
    block.setType(Material.SAND);
    
    If you're wanting to add a delay to something, check out schedulers.
     
  4. Offline

    Brexima

    Code:
    package com.bukkit.Brexima.Basic;
     
    import java.util.HashSet;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Basic extends JavaPlugin {
        public boolean onCommand(Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("sand")){
                CommandSender sender; // from your command
                Player player = (Player)sender;
               
                Block block = player.getTargetBlock(params);
                block.setType(Material.SAND);
                return true;
            }
            return false;
        }
        }
    its the code but there is a redline on params and not working :(
     
  5. Offline

    Malikk

    Well, I said it was psuedo code. I wasn't sure what the parameters for that method was off the top of my head.

    A couple things,
    1) If you tag me or quote me, I'll get an alert and check back. You shouldn't have to message me to take a look, lol.

    2) your onCommand method should have a commandSender arg

    3) getTargetBlock takes a HashSet, which you probably want to be null, and a maxDistance
    http://jd.bukkit.org/doxygen/d4/d7b...Entity.html#a889b8d9fb7d4a9ad042392a1cdd7e284

    4) you need to first check if the sender is a player before you cast

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("sand")){
                if (sender instanceof Player){
     
                    Player player = (Player)sender;
                    int dist = //Pick whatever distance you want
             
                    Block block = player.getTargetBlock(null, dist);
                    block.setType(Material.SAND);
                    return true;
                }else{
                    sender.sendMessage("This command can only be used in-game");
                    return true;
            }
            return false;
        }
    
     
  6. Offline

    Brexima

    Code:
    package com.bukkit.Brexima.Basic;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Basic extends JavaPlugin {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("sand")){
                if (sender instanceof Player){
     
                    Player player = (Player)sender;
                    int dist = 10;
         
                    Block block = player.getTargetBlock(null, dist);
                    block.setType(Material.SAND);
                    return true;
                }
                else{
                    sender.sendMessage("This command can only be used in-game");
                    return true;
            }
        }
        return false; 
        }
    }
    
    Malikk
    its done ty so much, but i have couple questions and they r little hard i think, its okay if u dont help..
    1.its changing other blocks to sand too i want just air will change with sand so it will seem like a sand summoned :D
    2.i want change block with 2x2 sand square not only one sand..
    3.sand will destroy after 10 seconds

    and im waiting help from the other java knowers :p
     
  7. Offline

    Malikk

    Okay, here goes :p

    1) This ones gonna be a little tricky, and I'm not exactly sure how this will work, but try it out. Hopefully the guess work is still accurate enough for you.

    We're going to get the block the same way, but instead of using that block (which will NEVER be air, due to the way getTargetBlock works), we're going to check all the relative blocks from that blocks faces.

    Code:
    Block targetBlock = player.getTargetBlock(null, dist);
    BlockFace[] faces = {BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST};
            
    for (BlockFace face: faces){
        Block b = targetBlock.getRelative(face);
                
        if (b.getType() == Material.AIR){
            targetBlock = b;
            break;
        }
    }
           
    //Now your target block variable is an air block one block away from where the player was looking.
    
    You can change the order in the array to change the order it checks in (if you want to give favor to UP, for example)

    2) For this, you'll be doing something similar. Rather than using faces, you'll want to say

    Code:
    Block targetBlock; //from earlier
    Block b1,b2,b3,b4;
     
    b1 = targetBlock.getRelative(x,y,z);
    //etc.
    
    You'll change the ints passed in to change what block gets picked. So, (1,0,0), (1,0,1), and (0,0,1) are probably what you want, but I'm not sure.

    3) For this, you'll want to use a scheduler.

    http://wiki.bukkit.org/Scheduler_Programming
     
Thread Status:
Not open for further replies.

Share This Page