[Help] Parkour Plugin

Discussion in 'Plugin Development' started by JollyGiant16, Jan 28, 2013.

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

    JollyGiant16

    Hello, Bukkit Community

    I'm trying to make a plugin when you finish the course you click a sign that says [Parkour] but I have an issue it's not working.

    The code my plugin so far... My account name is Anonymous350

    Code:
    package me.anonymous350.Parkour;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Parkour extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Parkour plugin;
        public final HashMap<Location, String> signs = new HashMap<Location, String>();
     
     
     
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            if(event.getLine(0).equalsIgnoreCase("Parkour")) {
                signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
                event.getPlayer().sendMessage(ChatColor.GREEN + "Exp Parkour Sign Created!");
             
            }
        }
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            if (signs.containsKey(event.getBlock().getLocation())
                    && !signs.containsValue(event.getPlayer().getName()) || !player.isOp()) {
                event.setCancelled(true);
            } else {
                signs.remove(event.getBlock().getLocation());
            }
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
         
            if(signs.containsKey(event.getClickedBlock().getLocation())) {
                player.teleport(player.getWorld().getSpawnLocation());
                player.setExp(player.getExp() + 500);
                Bukkit.broadcastMessage(ChatColor.DARK_RED + "[Parkour]" + ChatColor.GOLD + player.getName() + " Has finish the Parkour course!");}
             
            }
         
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                Player player = (Player) sender;
          if(commandLabel.equalsIgnoreCase("Parkour"));
          player.sendMessage(ChatColor.DARK_RED + "[Parkour]" + ChatColor.GOLD + " Plugin developed by Anonymous350");{
              }
          return false;
      }
    }
     
  2. Offline

    Remi1115

    Maybe you need to register the event(s).
    (I'm not sure because I normally put my events in different classes).

    You can put this in OnEnable and see if it works.
    Code:Java
    1.  
    2. getServer().getPluginManager().registerEvents(this, this);
    3.  


    Edit: Sorry that I cannot test it myself with your code, it's early in the morning here and I haven't got much time.
     
  3. Offline

    stuntguy3000

    Whats the issue? Any errors?
     
  4. Offline

    JollyGiant16

    So by adding that it will work?

    When I type [Parkour] on the sign it dosn't work in the code it says Parkour but I fixed it after but still won't work.

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

    ZeusAllMighty11

    You said Parkour, not [Parkour]
     
  6. Offline

    H3LLO_M4N

    Dont you need to implement Listener?
     
  7. Offline

    Remi1115

    You need to make an onEnable method and add that into it. That might work.

    Also, if you want to check if the events (like PlayerInteractEvent, or any piece of code) actually get called/executed, you could send a message to the player when he/she does something.
    Like so at the top of the 'onPlayerInteract' method:
    Code:Java
    1.  
    2. event.getPlayer().sendMessage("You interacted with something");
    3.  


    Not if it's in the main class I think.
     
  8. Offline

    evilmidget38

    Remi1115 You do indeed need to implement Listener.
     
  9. Offline

    H3LLO_M4N

    OP go and implement Listener and see if it works.
     
  10. Offline

    RealDope

    Code:JAVA
    1.  
    2. public class Parkour extends JavaPlugin implements Listener {
    3.  
    4. public void onEnable() {
    5. Bukkit.getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8. // Rest of code
    9. }
    10.  
     
    H3LLO_M4N likes this.
  11. Offline

    JollyGiant16

    I have 2 errors with the plugin.

    When I reload the server the sign dosn't work.
    When I try adding if(event.getPlayer().isOp())
    the player who is not op can still create the signs.

    Bump! Please help me!

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

    xXSilentYoshiXx

    JollyGiant16

    Code:java
    1. public void onBlockInteract(PlayerInteractEvent e) {
    2. Player p = e.getPlayer();
    3. String pName = p.getName();
    4.  
    5. if (signs.containsKey("[Winner]")) {
    6. p.teleport(p.getWorld().getSpawnLocation());
    7. p.setExp(500 + p.getExp());
    8. Bukkit.broadcastMessage(ChatColor.AQUA + "[Parkour] " + ChatColor.DARK_RED + pName + ChatColor.GREEN + " has finished the <parkour name> parkour!");
    9. }
    10. }
    11.  
    12.  


    :) Hopefully this works. I'm pretty sure it does.
     
  13. Offline

    JollyGiant16

    I got that working.
    I got these issues: I want only Operators to create the sign but adding player.isOp is not working and when I reload the server the sign doesn't work.
     
  14. Offline

    Jack1312

    If you want the signs to work after reloading the server, your going to need to implement a method of storing your data into a file or an SQL database. So I would suggest you use PatPeter's SQLite library:

    http://dev.bukkit.org/server-mods/sqlibrary/
     
  15. Offline

    JollyGiant16

    What about adding the permission so only ops can build it.
     
  16. Offline

    Jack1312

    Just do a simple check for if the player is op?

    if (event.getPlayer().isOp()) { //put all your stuff here }
     
  17. Offline

    JollyGiant16

    I did but won't work...
     
  18. Offline

    Jack1312

    Post Code Again
     
  19. Offline

    JollyGiant16

    Jack1312
    Code:
    package me.anonymous350.Parkour;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Parkour extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Parkour plugin;
        public final HashMap<Location, String> signs = new HashMap<Location, String>();
     
     
     
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            if(event.getLine(0).equalsIgnoreCase("Parkour")) {
                signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
                event.getPlayer().sendMessage(ChatColor.GREEN + "Exp Parkour Sign Created!");
             
            }
        }
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            if (signs.containsKey(event.getBlock().getLocation())
                    && !signs.containsValue(event.getPlayer().getName()) || !player.isOp()) {
                event.setCancelled(true);
            } else {
                signs.remove(event.getBlock().getLocation());
            }
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
         
            if(signs.containsKey(event.getClickedBlock().getLocation())) {
                player.teleport(player.getWorld().getSpawnLocation());
                player.setExp(player.getExp() + 500);
                Bukkit.broadcastMessage(ChatColor.DARK_RED + "[Parkour]" + ChatColor.GOLD + player.getName() + " Has finish the Parkour course!");}
             
            }
         
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                Player player = (Player) sender;
          if(commandLabel.equalsIgnoreCase("Parkour"));
          player.sendMessage(ChatColor.DARK_RED + "[Parkour]" + ChatColor.GOLD + " Plugin developed by Anonymous350");{
              }
          return false;
      }
    }
     
  20. Offline

    Jack1312

    [EDIT]

    Can I confirm your not using any permissions plugins?

    Just change it from:

    if(event.getLine(0).equalsIgnoreCase("Parkour")) {

    to

    if (event.getPlayer().isOp() && event.getLine(0).equalsIgnoreCase("[Parkour]")) {
     
  21. Offline

    JollyGiant16

    Already fixed it thanks!
    Oh and how could I do the sign reload thing inside of a config.yml?
     
  22. Offline

    Jack1312

    My suggestion is you setup SQLite.

    http://dev.bukkit.org/server-mods/sqlibrary/

    Just download the source and follow these instructions, as on the page:

    How to Install the Library via Source

    1. Download the source here.
    2. Navigate to your Eclipse workspace.
    3. Create a new folder called "Libraries" or "APIs" (whichever one you prefer, you cannot change this later).
    4. Drag and drop "lib" from the zip file into "Libraries".
    5. Navigate to <YourProjectName>\src and create the folder "lib", then create the folder "PatPeter" in it.
    6. Open your Eclipse project.
    7. Drag "lib" from inside of your "Libraries" folder over "src" in Eclipse.
    8. Select "Link to files and folders" and "Create link locations relative to: WORKSPACE_LOC".
    9. Add "import lib.PatPeter.SQLibrary.*;" to the top of your Java file and the Javadoc should be sufficient past that.

    Although I just found a page for storing data in a YAML file on the wiki:

    http://wiki.bukkit.org/Configuration_API_Reference

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

    JollyGiant16

    I was acctually looking at that but I wanted my plugin to be "One" thing. Like through in the data inside the config and have it so when a /reload happens the plugin reloads and still works.
     
  24. Offline

    Jack1312

    There is no way to keep your data through a reload.
    You need to add a method in the onEnable() void to load your data from the configuration file
    and a method to save it during onDisable(). There isn't much else you can do.
     
  25. Offline

    JollyGiant16

    Jack1312
    Code:
    package me.anonymous350.Parkour;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Parkour extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Parkour plugin;
        public final HashMap<Location, String> signs = new HashMap<Location, String>();
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
           
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
            Bukkit.getPluginManager().registerEvents(this, this);
       
        }
       
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
        if(event.getPlayer().isOp()) {
          if(event.getLine(0).equalsIgnoreCase("[Parkour]"))
                event.setLine(0, ChatColor.DARK_RED + "[Parkour]");
                event.setLine(1, "Click this Sign");
                event.setLine(2, "and win 30 exp!");
                signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
                event.getPlayer().sendMessage(ChatColor.GREEN + "Parkour sign created!"); }
               
            }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            if (signs.containsKey(event.getBlock().getLocation())
                    && !signs.containsValue(event.getPlayer().getName()) || !player.isOp()) {
                event.setCancelled(true);
            } else {
                signs.remove(event.getBlock().getLocation());
            }
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
           
            if(signs.containsKey(event.getClickedBlock().getLocation())) {
                player.teleport(player.getWorld().getSpawnLocation());
                player.giveExpLevels(30);
                Bukkit.broadcastMessage(ChatColor.DARK_RED + "[Parkour] " + ChatColor.GOLD + player.getName() + " Has finish the " + ChatColor.GREEN + " Parkour " + ChatColor.GOLD + " course!");}
               
            }
           
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                Player player = (Player) sender;
          if(commandLabel.equalsIgnoreCase("Parkour"));
          player.sendMessage(ChatColor.DARK_RED + "[Parkour]" + ChatColor.GOLD + " Plugin developed by" + ChatColor.GREEN + " Anonymous350");{
            }
         
        return false;
        }
    }
    Fixed. Just still has that reload thing...
     
  26. Offline

    Remi1115

    Oh okay, sorry then.
     
Thread Status:
Not open for further replies.

Share This Page