SANITY

Discussion in 'Plugin Development' started by skipperguy12, Sep 15, 2012.

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

    skipperguy12

    Code:
    package com.github.skipperguy12.Sanity;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class Sanity extends JavaPlugin{
       
          public void onDisable() {
                PluginDescriptionFile pdfFile = getDescription();
                System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!" );
            }
     
            public void onEnable() {
     
                PluginDescriptionFile pdfFile = getDescription();
                System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
               
            }
           
            public void onPlayerMoveEvent(PlayerMoveEvent event) {
                Player player = event.getPlayer();
               
                if(player.getLocation().getBlockY() <= 40){
                    player.setLevel(player.getLevel() -1);
                   
                }
               
                if(player.getLevel() == 0) {
                    player.setHealth(player.getHealth() -1/2);
                }
               
               
            }
       
       
    }
    
    Nothing happens... :l

    I also have a SanityPlayerListener.java class.

    These are the contents:

    Code:
    package com.github.skipperguy12.Sanity;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
     
    public class SanityPlayerListener implements Listener {
       
    }
    
    No errors! D:
     
  2. Offline

    izak12345678910

    Wrong thread Plugin Development should be the thread
     
  3. Offline

    skipperguy12

    Whoops
     
  4. Offline

    izak12345678910

    Its ok everyone makes mistakes! Which reminds me of a funny meme I saw the other day :p
    Hannah Montana: Nobodys Perfect song
    Selena Gomez: Who says your not perfect Song
    Kid: Hannah Montana does :(
    (it was funnier on an app i saw with pictures :p)
     
  5. Offline

    pyraetos

    Moved to proper section.
     
  6. Offline

    CRAZYxMUNK3Y

    You never registered the events. Have a look at this post/wiki about Event Handling
     
  7. This should work:
    PHP:
    @EventHandler       
    public void onPlayerMoveEvent(PlayerMoveEvent event) {
                
    Player player event.getPlayer();
             
                if(
    player.getLocation().getBlockY() <= 40){
                    
    player.setLevel(player.getLevel() -1);   
                }
             
                if(
    player.getLevel() == 0) {
                    
    player.setHealth(player.getHealth() -1/2);
                } 
            }
     
  8. Offline

    skipperguy12

    Didn't do anything D:

    I have two files, Sanity, and SanityPlayerListener, I tried putting this in both. Nothing happens...
     
  9. Offline

    CRAZYxMUNK3Y

    Main:
    Code:
    package com.github.skipperguy12.Sanity;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class Sanity extends JavaPlugin{
     
    SanityPlayerListener listener = new SanityPlayerListener(this);
       
          public void onDisable() {
                PluginDescriptionFile pdfFile = getDescription();
                System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!" );
            }
     
            public void onEnable() {
     
                PluginDescriptionFile pdfFile = getDescription();
                System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
                getServer().getPluginManager().registerEvents(listener, this);
               // Register the Event(s)
            }
    }
    
    Listener:
    Code:
    package com.github.skipperguy12.Sanity;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
     
    public class SanityPlayerListener implements Listener {
    @EventHandler
    //Tell bukkit it's an Event
        public void onPlayerMoveEvent(PlayerMoveEvent event) {
        Player player = event.getPlayer();
              
        if(player.getLocation().getBlockY() <= 40){
        player.setLevel(player.getLevel() -1);
                  
            }
              
        if(player.getLevel() == 0) {
                    player.setHealth(player.getHealth() -1/2);
        }
     
      }
    }
    
     
  10. Offline

    skipperguy12

    Error at SanityPlayerListener listener = new SanityPlayerListener(this);

    Also, I added more code to the listener:

    Code:
    package com.github.skipperguy12.Sanity;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class SanityPlayerListener implements Listener {
        //Tell bukkit it's an Event
    @EventHandler
    //Start every player when they join with 100 as their level
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
       
        player.setLevel(100);
       
       
    }
     
    //Every time a player moves, and his/her Y coords are equal to or below 40, start taking off their Level, and if it reaces 0, start making the player take damage
    public void onPlayerMoveEvent(PlayerMoveEvent event) {
        Player player = event.getPlayer();
     
        if(player.getLocation().getBlockY() <= 40){
            player.setLevel(player.getLevel() -1);
            player.sendMessage(ChatColor.AQUA + "[Name]:" + ChatColor.RED + "You are out of bounds!");
        }else{
            //If nessecary, insert code to remove potion effects.
        }
     
        if(player.getLevel() == 0) {
            player.setHealth(player.getHealth() -1/2);
        }
       
        if(player.getLevel() < 25) {
            player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 0, 0));
            player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 0, 0));
            player.setSprinting(false);
        }
    }
    //Stop mobs and players from dropping EXP
    public void onEntityDeath(EntityDeathEvent event) {
    //if (event instanceof PlayerDeathEvent) {
    PlayerDeathEvent e = (PlayerDeathEvent) event;
    e.setDeathMessage(null);
    e.setDroppedExp(0);
    }
    }
     
  11. Offline

    CRAZYxMUNK3Y

    skipperguy12
    public SanityListener listener = new SanityListener(this);
     
  12. Offline

    skipperguy12


    I can tell it obviously didn't work because when I joined, my level was NOT set to 100. And, no messages when I was below 40 on y axis appeared. Nor did any potion efects happen.
     
  13. Offline

    LimitedWard

    In the main class, you need to create listener objects. Otherwise, there would be no reason why the main class would even utilize the code within the listeners.
     
  14. Offline

    skipperguy12

    What? I'm new to this, and trust me, i'm confused...
     
  15. Offline

    LimitedWard

    At the top inside your main class write:
    Code:
    public typeOfListener nameOfListener = new typeOfListener(this);
    or something like that.
     
  16. Offline

    evilmidget38

    skipperguy12 There's a few main things you need to do. I'll outline these in a list below, since I write far too many essays during the weekdays.

    • You need to annotate all eventhandlers with the annotation '@EventHandler'. In simple terms, that just means add '@EventHandler' right before the methods which are to listen for events. E.g.
      Code:
       @EventHandler
      public void onPlayerDeath(PlayerDeathEvent e) {
      //This is where we can do stuff, since we've annotated with @EventHandler
      }  
    • The reason everyone's code example they're giving you isn't working, is because your Listener doesn't have a constructor, but everyone is giving you code as if it did. In your situation, you probably don't need a constructor. And, since you need to register your listener(that was going to be my next bullet), we can hit two birds with one stone.
      Code:
      getServer().getPluginManager().registerEvents(this, new SanityPlayerListener());
      
      What that code does is two things. The first, is that it creates a new listener object. This object is what it'll use to handle the events. The second, is that it registers the listener. This means it goes through all the methods in the Listener, and checks if they have the annotation '@EventHandler'. It checks for what event they listen for, and then calls them at the appropriate times.
     
  17. Offline

    skipperguy12

    This....is the only reply that helped! Thank you so much!


    Now I have added a few more things:
    Fix the level when a player comes back above level 40.
    Add a respawn event.

    Now, here's what still doesn't work:

    Damage when your level is at 0.

    Potion effects, and anti sprint when level is less than 25.

    I also don't like how rapidly the sanity goes down. Using the move event causes an undesired effect. What other event can I use to make the level/sanity go down in a timely manner?

    Thank you, skipperguy12
     
  18. Offline

    evilmidget38

    Instead of using PlayerMoveEvent, you can control exactly how fast it decreases by using a Runnable object and the Bukkit Scheduler. For information regarding this, you should check out the Scheduler Wiki page as well as this thread, as they both should contain some useful information. There's probably also some tutorials floating around somewhere.
     
  19. Offline

    skipperguy12

    I tried to do that, what do I replace myPlugin with? Sanity, SanityPlayerListener, and Bukkit don't work.

    Nvm, I figured out to use "this". But now, I can only place it in my Sanity.java without getting an error, and my actual code is in SanityPlayerListener... What do I do? Transfer my code? Then I can't define player.

    //EDIT:

    Scratch the potions not working, I got them to work!

    Now I just need loops!

    Bump! Please, I really want this! I feel so close to done!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page