Solved Making Enchanting table not able to open

Discussion in 'Plugin Development' started by trusebruse, Nov 10, 2012.

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

    trusebruse

    I have a problem with a plugin i am trying to make. I want to make that you can't open enchantment tables. I only get one error but i don't have a clue how to fix it.
    Error:
    Code:
    Syntax error on token ")", invalid Expression
    Code:
    Code:
    package me.trusebruse.NoEnchantmentTable;
     
        import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
        public final class NoEnchantmentTable extends JavaPlugin {
            public final Logger logger = Logger.getLogger("Minecraft");
            public static NoEnchantmentTable plugin;
     
            @Override
            public void onEnable(){
                this.logger.info("NoEnchantmentTable is enabled.");
                // TODO Insert logic to be performed when the plugin is enabled
            }
            @Override
            public void onDisable() {
                this.logger.info("NoEnchantmentTable is disabled.");
                // TODO Insert logic to be performed when the plugin is disabled
                     
            }
            public boolean onPlayerInteractBlock (PlayerInteractEvent evt)
            {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                    Block.getTypeId() == Material.ENCHANTMENT_TABLE);
                {
                event.setCancelled(true);
                {
                return false;
                }
                }
            }
        }
        }
    Anyone know what the wrong is and does this code work for disabling enchantment tables being opened?

    EDIT:
    The problem is at:
    Code:
     Block.getTypeId() == Material.ENCHANTMENT_TABLE);
     
  2. Offline

    Njol

    Are you using an IDE? And do you actually know what you're doing? Because that's a simple syntax error that even a beginner should be able to resolve on his own.
     
    ZeusAllMighty11 likes this.
  3. Offline

    trusebruse

    well i know what i am doing, i asked my brother for help and i dont know why it dont works.
    I am quite new at Java but i have some experience.
     
  4. Offline

    Switchbladed

    Try this?
    Code:
            @EventHandler
            public void onPlayerInteractBlock(PlayerInteractEvent event){
          Block block = event.getClickedBlock();
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
      if(block.getType() == Material.ENCHANTMENT_TABLE){
      event.setCancelled(true);
      }
                }
            }
    
    event.setCancelled(true); cancels the event, so it should cancel the opening of the enchanting table.
     
  5. Offline

    trusebruse

    No i dont work. I get more errors in the code if i change it to:
    Code:
    if(block.getType() == Material.ENCHANTMENT_TABLE)
     
  6. Offline

    Karl Marx

    Code:
    public final class NoEnchantmentTable extends JavaPlugin implements Listener{
        //trimmed for space...
     
        @Override
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public boolean onPlayerInteractBlock (PlayerInteractEvent evt)
        {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK
                && Block.getTypeId() == Material.ENCHANTMENT_TABLE)
            {
                event.setCancelled(true);
                return false;
            }
        }
    }
    

    I think you had a few extra }s in there, too. You really ought to take some time to figure out what parenthesis and brackets are used for :p
     
  7. Offline

    trusebruse

    Well, i get errors when i remove them. But i can't get it to work...
     
  8. Offline

    Switchbladed

    Did you use my whole code? Or just change the single line?
    I edited more than just one line I think.
     
  9. Offline

    Quintinity

    Here is my attempt at fixing your code:

    Code:
    package me.trusebruse.NoEnchantmentTable;
    import java.util.logging.Logger;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.*;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class NoEnchantmentTable extends JavaPlugin implements Listener
    {
            public final Logger logger = Logger.getLogger("Minecraft");
            public static NoEnchantmentTable plugin;
     
            @Override
            public void onEnable()
            {
                this.logger.info("NoEnchantmentTable is enabled.");
                // TODO Insert logic to be performed when the plugin is enabled
            }
     
            @Override
            public void onDisable()
            {
                this.logger.info("NoEnchantmentTable is disabled.");
                // TODO Insert logic to be performed when the plugin is disabled
             
            }
     
            @EventHandler
            public void onPlayerInteractBlock (PlayerInteractEvent event)
            {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType()  == Material.ENCHANTMENT_TABLE) {
                      event.setCancelled(true);
                }
            }
    }
    
     
  10. Offline

    Lolmewn

    Just remove the ) before the ;
     
  11. Offline

    Quintinity

    That won't fix it. His event handler doesn't have the @EventHandler annotation, his class does not implement Listener, and in the event arguments, the event object is called evt, but in the body, he calls it event.
     
  12. Offline

    Lolmewn

    Does fix the 'error' though. Sure, his plugin won't work, but that's something completely different.
     
  13. Offline

    Quintinity

    Sure, but it will create even more errors. This
    Code:
    Block.getTypeId() == Material.ENCHANTMENT_TABLE;
    is not valid code.
     
  14. Offline

    trusebruse

    Thanks so very much! But it did't work with public void, i had to change it to public boolean.
    And thanks everyone else! :D
    One more thing, if i want a message to show up when some tries to open a enchantment table, how can i get this to work?
    Code:
    event.getPlayer().sendMessage(ChatColor.AQUA + "You can not open Enchantment tables. " + ChatColor.BOLD + event.getClickedBlock().getType().toString().toLowerCase().replace("_", ""));
     
  15. Offline

    Lolmewn

    Oh yeah you're right :)
    Still sleepy over here :D
     
  16. Offline

    trusebruse

    I got everything to work but now i have problems with brackets :/
    My code:
    Code:
    package me.trusebruse.NoEnchantmentTable;
     
        import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
       
        public final class NoEnchantmentTable extends JavaPlugin
            {
            public final Logger logger = Logger.getLogger("Minecraft");
            public static NoEnchantmentTable plugin;
       
            @Override
            public void onEnable()
            {
                this.logger.info("NoEnchantmentTable is enabled.");
                // TODO Insert logic to be performed when the plugin is enabled
            }
            @Override
            public void onDisable()
            {
                this.logger.info("NoEnchantmentTable is disabled.");
           
                // TODO Insert logic to be performed when the plugin is disabled
            }           
            @EventHandler
            public boolean onPlayerInteractBlock (PlayerInteractEvent event)
            {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.ENCHANTMENT_TABLE);
                event.setCancelled(true);
                return false;
                (event.getPlayer()).sendMessage(ChatColor.AQUA + "You can not open Enchantment tables. " + ChatColor.BOLD + event.getClickedBlock().getType().toString().toLowerCase().replace("_", ""));
            }
            {
            }
           
     
           
           
          
    Last bracket got this error: Syntax error, insert "}" to complete ClassBody

    God dammit!
    I can't get this to work, please help me!

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

    Quintinity

    Code:
    package me.trusebruse.NoEnchantmentTable;
     
        import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
        public final class NoEnchantmentTable extends JavaPlugin
            {
            public final Logger logger = Logger.getLogger("Minecraft");
            public static NoEnchantmentTable plugin;
     
            @Override
            public void onEnable()
            {
                this.logger.info("NoEnchantmentTable is enabled.");
                // TODO Insert logic to be performed when the plugin is enabled
            }
            @Override
            public void onDisable()
            {
                this.logger.info("NoEnchantmentTable is disabled.");
         
                // TODO Insert logic to be performed when the plugin is disabled
            }         
            @EventHandler
            public boolean onPlayerInteractBlock (PlayerInteractEvent event)
            {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.ENCHANTMENT_TABLE) {
                event.setCancelled(true);
                (event.getPlayer()).sendMessage(ChatColor.AQUA + "You can not open Enchantment tables. " + ChatColor.BOLD + event.getClickedBlock().getType().toString().toLowerCase().replace("_", ""));
                return false;
            }
            }
    }
         
     
         
         
          
     
  18. Offline

    trusebruse

    It dont work! :/
    I dont know what to do.
     
  19. Offline

    ZeusAllMighty11

    People on this thread have offered more than enough help for a beginner to figure it out.

    Take some time, see what changes they made. Also, go to step 0-2 of learning Java... (Look up YT tuts, they're usually the same order in the beginning)
     
    ferrybig and TwistedMexi like this.
  20. Offline

    trusebruse

    Dude, all help i have got does not work. It is not my fault. Try it by your self if you want to see if it works.
     
  21. Offline

    ZeusAllMighty11

    Help does not mean copy and paste.

    They gave you HELP -
    Help = the necessary stuff you need for your code to function. It does not mean you can just copy and paste and it will all work as you wished.
     
    ferrybig and fireblast709 like this.
  22. Offline

    the_merciless

    To be fair, he is obviously very new to java, and people arguing between themselves has probably confused him a little. Why do people always have to help with cryptic advice forcing them to try and work it out and wasting everyones time and effort.

    This is because you have a closing bracket ) without an opening bracket (
    Remove the closing bracket to fix this error.

    If you are listening to events, (PlayerInteractEvent) Your class must implement the listener, you do that by changing your class name to this:


    public final class NoEnchantmentTable extends JavaPlugin implements listener {


    Then you must specify when you wish to use the eventhandler, to do this you must add @Eventhandler like this

    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent event){

    Also an event is not a boolean, you must use public void like above.

    You have a lot of syntax errors so i would suggest viewing some tutorials on java. This guy helped me alot:

    http://thenewboston.org/list.php?cat=31
     
    trusebruse likes this.
  23. Offline

    TwistedMexi

    Even after removing the ")" his entire line is incorrect. You can't set .getTypeId() , let alone set it to a material, but he can use .setType(Material.ENCHANTMENT_TABLE);
     
  24. Offline

    the_merciless

    Yes, i overlooked that. The whole point in my post was to try and get people to be a little more descriptive and helpful. There is no use just telling them what to do, they will never understand it.

    event.getType will return a material
    event.getTypeId will return that materials id.

    so you would use them like this:

    if (event.getType().equals(Material.ENCHANTMENT_TABLE)){
    //do this
    }

    and

    if (event.getTypeId().equals(54)){
    // do this
    }
     
    TwistedMexi likes this.
  25. Offline

    trusebruse

    Thanks for supporting and helping me! But i get this error: The method getType() is undefined for the type PlayerInteractEvent. i dont know what it means and i dont what that code would do :/
     
  26. Offline

    the_merciless

    You have 1 too many closing brackets }

    take a look at you opening brackets and work out where the closing bracket is,

    if (player.hasPermission("random.permission")){
    if (player.blah()){
    //do this
    }
    }
     
  27. Offline

    trusebruse

    that code does not work either, "The method setType(Material) is undefined for the type PlayerInteractEvent shows up".
     
  28. Offline

    the_merciless

    Sorry you will need to get the block 1st (event.getBlock().getType().equals........)

    Stop copying everything everyone types, look at their code and see what they done different. That guy is using SET Type. You do not want to set it, that is trying to change the block to that type. You just want to 'get' as you are only checking what it is.

    getBlock = is getting the block involved
    getType = is getting what type of block it is
    .equals(Material.TNT) = is checking if the type is tnt

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

    trusebruse

    But there is something wrong with "getBlock"
    Edit:
    "getBlock" does not work at all
     
  30. Offline

    the_merciless

Thread Status:
Not open for further replies.

Share This Page