How can I cancel the block place?

Discussion in 'Plugin Development' started by DeudlyYT, Sep 9, 2015.

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

    DeudlyYT

    I have a problem, I want to disable the carpets and I need to cancel when you place a carpet. This is my code but doesnt work.
    Code:
     @EventHandler
        public void onItemPlace(BlockPlaceEvent e){
           Player player = e.getPlayer();
           if(e.getBlockPlaced().getType().equals(Material.CARPET)) {
               player.sendMessage(ChatColor.RED + "You cant use this item");
               e.setCancelled(true);
          
           }
        
    The code doesnt work in the server. Whats wrong?
     
  2. Did you register the events and implemented Listener?
     
  3. Offline

    DeudlyYT

    Code:
    package com.gmail.deudlymandame;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("Al usar este plugin aceptas los terminos y condiciones de uso de DeudlyYT");
            getLogger().info("En caso de no aceptarlos, por favor no use este plugin");
        
    
    
        }
       
        @Override
        public void onDisable(){
           
        }
       
        @EventHandler
        public void onItemPlace(BlockPlaceEvent e){
           Player player = e.getPlayer();
           if(e.getBlockPlaced().getType().equals(Material.CARPET)) {
               player.sendMessage(ChatColor.RED + "You cant use this item");
               e.setCancelled(true);
          
           }
       
    
       
    }}
    
    this is all my code
     
  4. Does your plugin load? Do you get the "You cant use this item" message?
     
  5. Offline

    RoboticPlayer

    Just to get into a better habit, use == instead of .equals()
    Also, don't use com.gmail.deudlymandame for your package, because
    A) You don't own the domain gmail.com
    B) It is probably not unique to that plugin
    Instead, if you own the domain of a website, use that backwards, then .(plugin name). For example, if I owned the domain to minecraft.net (which I don't), and I was creating a NoRain plugin, the package would be net.minecraft.norain
    If you don't own a website domain, use me.(your name).(plugin name), so in the case above, I would do me.roboticplayer.norain (remember, it's all lowercase)!

    Also, Java Naming Conventions.
    A) Words within your class names should be capitalized properly (main should be Main)
    B) Don't use "Main" for your class name, your class name should be what that class is doing, or in the case of the main class, you would normally use the name of the plugin.
     
  6. Offline

    DoggyCode™

    No, you should use "equals()" when comparing objects (like ItemStacks).

    And I think instead of using "equals" get the block placed' state and check if it is instanceof Carpet

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  7. When you start the server, do you see the enable message from your plugin ?
     
    Last edited by a moderator: Sep 28, 2015
  8. Offline

    teej107

    it doesn't matter when comparing enums.
     
    DoggyCode™ likes this.
  9. Offline

    mythbusterma

    This is actually okay. Most standards accept the reverse of email addresses that belong to you (I'm assuming this one does).
     
  10. Offline

    DeudlyYT

    I fixed the plugin reading in forums. And I saw that you can name the package with your email
     
  11. Offline

    Lolmewn

    Objects, yes. However, we're using Enums here. With regard to enums, == is considered better since it's null-safe. Performing any .equals operation may result in a NullPointerException, whereas == will just return false.
     
    DoggyCode™ likes this.
  12. Offline

    DoggyCode™

    Informative. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page