Nooby casting help.

Discussion in 'Plugin Development' started by ZomBlade_Shadow, Dec 7, 2014.

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

    ZomBlade_Shadow

    So i coded an XP storing plugin


    Code:java
    1. package me.ZomBlade.BloodyXP;
    2.  
    3. import org.bukkit.OfflinePlayer;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.SignChangeEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Main extends JavaPlugin implements Listener {
    11.  
    12. public void onEnable() {
    13. getLogger().info("XPSign Enabled!");
    14. getServer().getPluginManager().registerEvents(this, this);
    15. }
    16.  
    17. @EventHandler
    18. public void OnSignChange(SignChangeEvent e) {
    19. if (e.getLine(0).equalsIgnoreCase("&1[StoreXP]")) {
    20. e.setLine(2, "Depôt d'XP");
    21.  
    22. }
    23. }
    24.  
    25.  
    26.  
    27.  


    I need to know how I would do my casting on the player that does the Sign.
    like Player p = (Player)[SOMETHING]
    cause im going to do
    Code:java
    1. if p.hasPermission("store.xp") {
    2.  
    3. }

    Thanks for helping.
     
  2. Offline

    Rocoty

  3. Offline

    ZomBlade_Shadow

    Thanks.
    That's why I said nooby,I started coding like a week ago.
     
  4. Offline

    mine-care

    ZomBlade_Shadow have you gone over java basics?
    Also you don't need to send the enable message since bukkit does it for you! :D
    Enjoy your coding journey
     
  5. Offline

    Agentleader1

    Ok, so you are new to plugin programming. I assure you, it will be FUN and VIOLENT. If you don't know what I mean by VIOLENT is not able to find solutions and you keep trying and can't get it right. Well, it's just that way! Every plugin creator has been through that.

    ANYWAYS,
    if you are looking for a way to see if they have permission to RIGHT CLICK the sign, it's all different:
    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent pie){
    Player player = pie.getPlayer();
    if(pie.getAction() == Action.RIGHT_CLICK_BLOCK){
          if(pie.getClickedBlock().getState() instanceof Sign){
                Sign sign = (Sign) pie.getClickedBlock().getState();   
                if (sign.getLine(0).equalsIgnoreCase("&1[StoreXP]")) {
                    sign.setLine(2, "Depôt d'XP");
                    sign.setLine(0, sign.getLine(0).replace("&","§"));
                    if(player.hasPermission("xp.store"){
                            //do your work here
                    }
                }
          }
      }
    }
    ^BTW, that code may have bracket flaws, I may have added or had less of the brackets needed

    If you want to see if they have permission to CREATE, try the following:
    Code:
    @EventHandler
    public void onInteract(SignChangeEvent sce){
    Player player = sce.getPlayer();
    Sign sign = (Sign) sce.getBlock().getState(); 
                if (sign.getLine(0).equalsIgnoreCase("&1[StoreXP]")) {
                    sign.setLine(2, "Depôt d'XP");
                    sign.setLine(0, sign.getLine(0).replace("&","§"));
                    if(player.hasPermission("xp.create"){ //watch out, different permission
                            //do your work here
                    }
          }
    }
    This hopefully should work, but I kind of rushed this. Sorry about that. Hope you enjoy the code!!!
     
    Last edited: Dec 9, 2014
  6. Offline

    SuperOriginal

    Agentleader1 Pretty sure this is already solved, and I don't think mass spoonfeeding will help him through the "VIOLENT" journey.
     
    Rocoty likes this.
  7. Offline

    ZomBlade_Shadow

    Thanks mine-care,i figured out it was PlayerInteractEvent e
    i should of done my casting with e.

    AgentLeader1 im actually going through the painful,fun and violent journey xd thx for the information on casting too :)
     
  8. Offline

    Agentleader1

    Man, I wanted the credit! Oh I don't like this, other people always beat me to it! T_T
    That's it, I'm done!! I officially quit programming, see you guys later! :p
     
  9. Offline

    JordyPwner

    Im pretty sure its .replaceAll("&","§")); instead of .replace("&","§"));
     
  10. Offline

    Lolmewn

    Nope. ReplaceAll requires Regex, while replace simply replaces all instances of the first charsequence by the second. It's in the javadocs :3
     
    leon3001 and Skionz like this.
  11. Offline

    JordyPwner

    Hmm never knew that :p
     
  12. Offline

    Agentleader1

    Either way .replace works
     
  13. @Agentleader1 "Right! I'd like to stop my users from using ellipses because they keep doing it!"

    Code:
    event.setMessage(event.getMessage().replaceAll("...", ""));
    "Hey, wait, why are all messages blank now? :("

    In programming, never do something that's "wrong but it works". It'll come back and bite you, it's merely a matter of time.

    Yes, replace() does work, but by that logic we could use replaceAll() too, since it will work, except when it doesn't... make sure you know when to not just dismiss someone by saying "Oh, this way works too", but to actually identify that the alternative has flaws. Because let's face it, you may be the one on the wrong side of the argument :)
     
    leon3001, mythbusterma and teej107 like this.
  14. Offline

    TheOatBaron

    As a side note, Line 19 "e.getLine(0).equalsIgnoreCase("&1[StoreXP]")" You'll need to replace the & with a § if you mean a colored sign
     
  15. Offline

    fireblast709

    Rather use ChatColors.
     
  16. Offline

    TheOatBaron

    Eh, they take a lot of room and makes it hard to look at it.
     
  17. Offline

    mythbusterma

    @TheOatBaron

    They also are a whole lot more readable, and better programming practice, can be auto completed by an IDE, and make it clear exactly which colour you're trying to create.

    But who cares, they're hard to look at apparently.
     
    SuperOriginal and fireblast709 like this.
  18. Offline

    teej107

    Idk about that. I have no idea if §a is RED. Honestly, I don't. I may have gotten that right or not but I always have a hard time remembering the codes. As for RED, GREEN or DARK_RED for example, I don't have to memorize some codes to use that. But apart from that, they made ChatColors for a reason and that reason is a pretty darn good one!
     
  19. Offline

    TheOatBaron

    It's personal preference.
     
  20. Offline

    mythbusterma

    Wasn't aware good coding practises were personal preference, I thought they were industry standards. Thanks for the information!
     
    Skionz and SuperOriginal like this.
  21. Offline

    TheOatBaron

    Please use a more condescending tone, you are really too nice of a person.
     
    Skionz likes this.
  22. Offline

    Agentleader1

    @TheOatBaron @mythbusterma @teej107 @fireblast709 @AdamQpzm
    This is already solved guys, don't give him so many notifications/emails!
     
  23. Offline

    teej107

    @Agentleader1 If he wants to stop this thread (at least from my posting) he can set the thread prefix to solved.
     
  24. @Agentleader1 He only receives those if he gets tagged or quoted, or he's watching the thread (assuming default settings)
     
  25. Offline

    Agentleader1

    Not quite, the notifications get converted to alerts. Which can be seen, and it doesn't need to be changed.
     
  26. @Agentleader1 Huh? What notifications? Someone doesn't know if someone posts in there thread here unless they're tagged/quoted/watching the thread.
     
  27. Offline

    Agentleader1

    EXCEPT, the fact that notifications get converted to Alerts. They won't get through email by default. But when they visit the website, they will get alerts.
     
  28. Offline

    teej107

    @Agentleader1 You are saying like notifications and alerts are different. They seem the same to me. Either way, this thread is getting off topic. If the OP has this solved, then he can mark it as solved with the prefix.
     
    AdamQpzm likes this.
  29. Final reply: Yes, but that's my point - they won't get either notifications or alerts unless they're following the thread, tagged, or are quoted.
     
  30. Offline

    Agentleader1

    FINALER reply: By default, they get alerts without getting tagged.
     
Thread Status:
Not open for further replies.

Share This Page