Solved RedStone disable ERROR.

Discussion in 'Plugin Development' started by Hoppys, Dec 12, 2015.

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

    Hoppys

    Hey so i'm currently working on my plugin and i have some slight issues, i can disable blocks but cannot disable Items such as Redstone, Torches etc. i'm wondering if it's with the BlockPlaceEvent or it's running on a different method, this maybe a rookie question but i am very new to MC Java development.

    Code:
    @EventHandler
    public void RedStone(BlockPlaceEvent e) {
        Block block = e.getBlock();
        Player player = e.getPlayer();
       
    if (block.getType() == Material.REDSTONE_COMPARATOR) {
        block.setType(Material.AIR);
        player.damage(2, player);
        player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "RedStone", "do NOT place this item.");
        if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
    }
    }
    }
           
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Hoppys It should be BlockPlaceEvent likeyou have already, did you register the event?
     
  3. Offline

    mcdorli

    You mean you're new to java development? You should learn more java. Start here.

    1.: Your spacing is terrible
    2.: Enderman can place blocks too, you need to check if the entity is an instance of the player.

    The code you have currently,
    checks if the player places a redstone comparator [​IMG]
    if he did, then set it back to air (You should cancel the event instead), damage him, and send him a message

    I don't see, where you cancel other types of redstone.
     
  4. Offline

    Hoppys

    Ah I see what you mean, and yea I'm not new to Java just new to mc plugins, and the spacing I do currently helps me out, IMO it looks cleaner. But yea once I'm home I'll try adding in the method. Thanks :)

    alright so i've done the same code to the blocks and it worked for them, but whenever i set it for the other materials like repeaters and such, it doesn't seem to work, this is the full Listener code.

    Code:
    package me.hoppys.tnt;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.block.BlockRedstoneEvent;
    
    import net.minecraft.server.v1_8_R3.BlockRedstoneOre;
    import net.minecraft.server.v1_8_R3.BlockRepeater;
    
    public class TNTListener implements Listener{
       
        public TNTListener(TNT plugin) {
       
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
    @EventHandler
    public void onTNTPlace(BlockPlaceEvent e){
        Block block = e.getBlockPlaced();
        Player player = e.getPlayer();
       
       
        if(block.getType() == Material.TNT) {
            block.setType(Material.AIR);
            player.damage(2, player);
            player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "TNT", "Do NOT Place this item down.");
            if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
                }
        }
    
    @EventHandler
    public void BlockRedStone(BlockPlaceEvent e) {
        Block block = e.getBlock();
        Player player = e.getPlayer();
       
    if (block.getType() == Material.REDSTONE_ORE) {
        block.setType(Material.AIR);
        player.damage(2, player);
        player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "RedStone", "do NOT place this item.");
        if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
    }
        }
    
    @EventHandler
    public void RedStone(BlockPlaceEvent e) {
        Block block = e.getBlock();
        Player player = e.getPlayer();
       
    if (block.getType() == Material.REDSTONE_COMPARATOR) {
        block.setType(Material.AIR);
        player.damage(2, player);
        player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "RedStone", "do NOT place this item.");
        if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
    }
    }
    }
    
    
    Problem is solved, i've used this code instead,

    Code:
    package me.hoppys.tnt;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import net.minecraft.server.v1_8_R3.BlockRedstoneOre;
    import net.minecraft.server.v1_8_R3.BlockRepeater;
    
    public class TNTListener implements Listener{
       
        public TNTListener(TNT plugin) {
       
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
    @EventHandler
    public void onTNTPlace(BlockPlaceEvent e){
        Block block = e.getBlockPlaced();
        Player player = e.getPlayer();
       
       
        if(block.getType() == Material.TNT) {
            block.setType(Material.AIR);
            player.damage(2, player);
            player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "TNT", "Do NOT Place this item down.");
            if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
                }
        }
    
    @EventHandler
    public void BlockRedStone(BlockPlaceEvent e) {
        Block block = e.getBlock();
        Player player = e.getPlayer();
       
    if (block.getType() == Material.REDSTONE_ORE) {
        block.setType(Material.AIR);
        player.damage(2, player);
        player.sendTitle(ChatColor.RED.toString() + ChatColor.BOLD.toString() + "RedStone", "do NOT place this item.");
        if (!e.isCancelled()) e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.NOTE_PLING, 1, 1);
    }
        }
    
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event){
     if(event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK){ return; }
     final Player player = event.getPlayer();
     if(player.getItemInHand().getType().equals(Material.REDSTONE_COMPARATOR)){
      event.setCancelled(true);
     }
    }
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 12, 2015
Thread Status:
Not open for further replies.

Share This Page