Problem with stopping interaction with storage minecarts and hopper minecarts

Discussion in 'Plugin Development' started by Nerdfuryz, Mar 15, 2013.

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

    Nerdfuryz

    Code:
    package me.Dylan.nohopper;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class nohopper extends JavaPlugin
    implements Listener{
        public static Logger logger = Logger.getLogger("Minecraft");
        public static Material[] blacklist = {Material.STORAGE_MINECART, Material.MINECART, Material.HOPPER_MINECART};
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            nohopper.logger.info(pdfFile.getName() + "Has been Enabled! (Nerdfuryz)");
            logger.info("[NoHopper] Second Plugin made by NerdFuryz");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
           
          }
              @EventHandler
              public void onPlayerInteractEvent(PlayerInteractEvent e) {
                if ((e.getPlayer().getGameMode().equals(GameMode.CREATIVE))){
                    Block block = e.getClickedBlock();
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if(block.getTypeId() == 154){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                        }
                        if(block.getTypeId() == 146){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                    }
                        if(block.getTypeId() == 158){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                    }
                        if(block.getTypeId() == 408){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                    }
                        if(block.getTypeId() == 342){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                    }
                        if(block.getTypeId() == 54){
                    e.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to interact with this block in creative!");
                    e.setCancelled(true);
                    }
                }
                }
              }
    }
    No errors or anything but I need to know why this isn't stopping interaction with the hopper minecarts and storage minecarts. Is there like a special thing
     
  2. Offline

    Cronos79

    I had a plugin blocking people from putting things in them also.. after updating to 1.5 it no longer blocks..
    Mine was to make creative not be able to place diamond blocks into chects.. minecrat chests etc .. This will kill the econ on my server :(
     
  3. Offline

    chasechocolate

    I believe minecarts are entities, so you may have to use PlayerInteractEntityEvent to do that check.
     
  4. Offline

    thebiologist13

    Hello!

    I think your problem is you are using a PlayerInteractEvent and not a PlayerInteractEntityEvent for the minecarts. Since minecarts are a type of entity and PlayerInteractEvent is for blocks, the event will not fire. If you make a listener for a PlayerInteractEntityEvent, check that the entity that was clicked was an instance of MinecartHopper or StorageMinecart, and finally cancel the event if so, that should work.
    Code:java
    1. @EventHandler
    2. public void onMinecartClick(PlayerInteractEntityEvent event) {
    3. Entity entity = event.getRightClicked();
    4. if(entity instanceof MinecartHopper) {
    5. event.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to interact with hopper minecarts!");
    6. event.setCancelled(true);
    7. }
    8. if(entity instanceof StorageMinecart) {
    9. event.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to interact with storage minecarts!");
    10. event.setCancelled(true);
    11. }
    12. }

    I hope this helps!
    ~thebiologist13
     
Thread Status:
Not open for further replies.

Share This Page