[TUT] How to Restrict Items/Block usage to levels!

Discussion in 'Resources' started by Kodfod, Jul 14, 2012.

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

    Kodfod

    Hello, I'd thought I would make my first tutorial on how to restrict item usage and block placing to a players level. Please read before posting. Enjoy!

    What is The Point of Restricting Things to Levels?

    This could be used for Role-Playing among other things. Leveling up to get better weapons/Tools has always been a part of RPG's.

    So How DO You Do it?

    It is quite simple really. Let's look at it piece by piece and explain what is happening. We will put it all together at the end.


    First Off, Lets get started with a basic plugin base.
    Code:JAVA
    1.  
    2. package me.kodfod.levelTools;
    3.  
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin {
    7.  
    8. Logger log = Logger.getLogger("Minecraft");
    9.  
    10. @Override
    11. public void onDisable() {
    12. //Prints message to Console saying Disabled
    13. log.info(this.getName() + " Disabled.");
    14. }
    15.  
    16. @Override
    17. public void onEnable() {
    18. //Prints message to Console saying Enabled
    19. log.info(this.getName() + " Enabled.");
    20. }
    21.  
    22.  


    Now That we have the base, we need to get the listener implemented:

    Code:JAVA
    1.  
    2. import org.bukkit.event.Listener;
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. //Implements Listener Allows us to use one class to get the Event.
    6. public class Main extends JavaPlugin implements Listener {
    7.  
    8. @Override
    9. public void onDisable() {
    10. //Prints message to Console saying Disabled
    11. log.info(this.getName() + " Disabled.");
    12. }
    13.  
    14. @Override
    15. public void onEnable() {
    16. //Prints message to Console saying Enabled
    17. log.info(this.getName() + " Enabled.");
    18. //This Next Line Registers The Event In This Class
    19. this.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22.  


    Now That We have The Event register we need to make it:

    Code:JAVA
    1.  
    2. //Below your onEnable()
    3. @EventHandler
    4. //Import the EventHandler && BlockBreakEvent
    5. public void onToolUse(BlockBreakEvent e) {
    6. // e is the short cut of saying BlockBreakEvent
    7. }
    8.  
    9.  


    Now time to check what they broke the block with:

    Code:JAVA
    1.  
    2. // e = the event // Checking if the item in hand is equal to Diamond pickaxe.
    3. if (e.getPlayer().getItemInHand().getTypeId() == Material.DIAMOND_PICKAXE
    4. .getId()) {
    5. }
    6.  


    Time to get the level set to allowed to use it

    Code:JAVA
    1.  
    2. if (e.getPlayer().getItemInHand().getTypeId() == Material.DIAMOND_PICKAXE
    3. .getId()) {
    4.  
    5. // we do - 1 because we are doing Greater Than the level. So if you put in 5 we are checking for any level above 4.
    6. if (e.getPlayer().getLevel() > (getConfig().getInt(
    7. "Diamond.Pickaxe.Use") - 1)) {
    8. return;
    9. }
    10.  
    11.  


    That is if the level is greater than the conifg option. If it's not:


    Code:JAVA
    1.  
    2. else {
    3. e.setCancelled(true);
    4. e.getPlayer().sendMessage(
    5. ChatColor.AQUA + "You Need To Be Level "
    6. + ChatColor.GOLD
    7. + getConfig().getInt("Diamond.Pickaxe.Use")
    8. + ChatColor.AQUA
    9. + " To use the Diamond PickAxe.");
    10. // this cancels the break, ans sends the player a message.
    11. }
    12. }
    13.  
    14.  


    Now For the Config.yml, you need to create it in the same directory as your plugin.yml.
    It Should look Something like this:

    Code:
    Diamond:
      Pickaxe:
        Use: 5
    Now, we need to do a few more final touches:

    Code:JAVA
    1.  
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.BlockBreakEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. //imported all the chat colors and other things,
    9.  
    10. public class Main extends JavaPlugin implements Listener {
    11.  
    12. @Override
    13. public void onDisable() {
    14. //Prints message to Console saying Disabled
    15. log.info(this.getName() + " Disabled.");
    16. this.saveConfig();
    17.  
    18. }
    19.  
    20. @Override
    21. public void onEnable() {
    22. //copies the config and saves it if it's not there
    23. this.getConfig().options().copyDefaults(true);
    24. this.saveConfig();
    25. this.getServer().getPluginManager().registerEvents(this, this);
    26. //Prints message to Console saying Enabled
    27. log.info(this.getName() + " Enabled.");
    28. }
    29.  



    Now Everything put together:

    Code:JAVA
    1.  
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.BlockBreakEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin implements Listener {
    10.  
    11. @Override
    12. public void onDisable() {
    13. //Prints message to Console saying Disabled
    14. log.info(this.getName() + " Disabled.");
    15. this.saveConfig();
    16.  
    17. }
    18.  
    19.  
    20. @Override
    21. public void onEnable() {
    22. //copies the config and saves it if it's not there
    23. this.getConfig().options().copyDefaults(true);
    24. this.saveConfig();
    25. this.getServer().getPluginManager().registerEvents(this, this);
    26. //Prints message to Console saying Enabled
    27. log/info(this.getName() + " Enabled.");
    28. }
    29.  
    30.  
    31. @EventHandler
    32. public void onToolUse(BlockBreakEvent e) {
    33. if (e.getPlayer().getItemInHand().getTypeId() == Material.DIAMOND_PICKAXE
    34. .getId()) {
    35. if (e.getPlayer().getLevel() > (getConfig().getInt(
    36. "Diamond.Pickaxe.Use") - 1)) {
    37. return;
    38. } else {
    39. e.setCancelled(true);
    40. e.getPlayer().sendMessage(
    41. ChatColor.AQUA + "You Need To Be Level "
    42. + ChatColor.GOLD
    43. + getConfig().getInt("Diamond.Pickaxe.Use")
    44. + ChatColor.AQUA
    45. + " To use the Diamond PickAxe.");
    46. }
    47. }
    48. }
    49.  


    Blocks:

    Now For Blocks. The Concept is the same, except for you are going to look for block placed and destroyed.

    Code:JAVA
    1.  
    2. public void onBlockPlace(BlockPlaceEvent e) {
    3. if (e.getBlock().getTypeId() == Block.STONE.id) {
    4. if (e.getPlayer().getLevel() > (getConfig().getInt(
    5. "Block.place." + e.getBlock().getTypeId) - 1)) {
    6. return;
    7. } else {
    8. e.setCancelled(true);
    9. e.getPlayer().sendMessage(
    10. ChatColor.AQUA + "You Need To Be Level "
    11. + ChatColor.GOLD
    12. + getConfig().getInt("Block.Place." + e.getBlock().getTypeId)
    13. + ChatColor.AQUA
    14. + " To Place this block.");
    15. }
    16. }
    17.  



    Everything else is the same.


    That's it! If You Have any Questions Feel Free to ask!
     
  2. its beeter if you use the loggers instead of System.out
     
  3. Offline

    Kodfod

    meh i usally do, but oh well i'll change it
     
  4. Offline

    shmkane

    Since you're using a config, wont this affect the whole server, not just one player?
     
  5. Offline

    Kodfod

    You can add a bypass by adding a permission node and a check inside the events:
    if player does not have restrict.bypass permission: block
    else return

    But by default it blocks it for everyone.
     
Thread Status:
Not open for further replies.

Share This Page