Multiple Actions?

Discussion in 'Plugin Development' started by spiroulis, Jan 21, 2014.

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

    spiroulis

    Hey so im making a rpg plugin and i've made a spell which enables if the player right clocks on a block but i want him to enable it if for example he right clicks on a block and then left click on a block. Heres my code, maybe someone has a nice idea :)
    Code:java
    1. @EventHandler
    2. public void RageStrike(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(isWarrior.containsKey(player)){
    5. if(player.getItemInHand().getType() == Material.DIAMOND_SWORD || player.getItemInHand().getType() == Material.IRON_SWORD || player.getItemInHand().getType() == Material.STONE_SWORD || player.getItemInHand().getType() == Material.WOOD_SWORD) {
    6. if(player.getLevel() <= 9){ //lvl 1
    7. if(RageStrike1.containsKey(player)){
    8. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    10. long last = LastUsed.containsKey(player.getName()) ? LastUsed.get(player.getName()) : 0;
    11. long ctm = System.currentTimeMillis();
    12. long timeleft = RageStrikeCooldown + last - ctm / 1000;
    13. if (ctm - last < RageStrikeCooldown) {
    14. event.getPlayer().sendMessage(ChatColor.DARK_RED + "You still have " + timeleft + " seconds cooldown for Rage Strike.");
    15. event.setCancelled(true);
    16. } else {
    17. LastUsed.put(player.getName(), now);
    18. Location Location = player.getLocation();
    19. player.playEffect(Location, Effect.SMOKE, 10);
    20. for (Entity entity : player.getNearbyEntities(3, 3, 3))
    21. {
    22. if (entity instanceof LivingEntity)
    23. {
    24. ((LivingEntity) entity).damage(4, player);
    25. }
    26. }
    27. }
    28. }
     
  2. Offline

    Chinwe

    Lines 8 and 9: you're checking if it's Action.RIGHT_CLICK_BLOCK, then immediately after if it's Action.LEFT_CLICK_BLOCK - they can't both be true :oops:
     
  3. Offline

    spiroulis

    Chinwe so how will i be able to make a countdown to check if the player has left clicked in that amount of time? (im a nooby developer :$ ) i tried this but it doesnt work:
    Code:java
    1. @EventHandler
    2. public void RageStrike(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(isWarrior.containsKey(player)){
    5. if(player.getItemInHand().getType() == Material.DIAMOND_SWORD || player.getItemInHand().getType() == Material.IRON_SWORD || player.getItemInHand().getType() == Material.STONE_SWORD || player.getItemInHand().getType() == Material.WOOD_SWORD) {
    6. if(player.getLevel() <= 9){ //lvl 1
    7. if(RageStrike1.containsKey(player)){
    8. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. long l = lastUsage.containsKey(player.getName()) ? lastUsage.get(player.getName()) : 0;
    10. long n = System.currentTimeMillis();
    11. if(n - l < ActionTime){
    12. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    13. long last = LastUsed.containsKey(player.getName()) ? LastUsed.get(player.getName()) : 0;
    14. long ctm = System.currentTimeMillis();
    15. long timeleft = RageStrikeCooldown + last - ctm / 1000;
    16. if (ctm - last < RageStrikeCooldown) {
    17. event.getPlayer().sendMessage(ChatColor.DARK_RED + "You still have " + timeleft + " seconds cooldown for Rage Strike.");
    18. event.setCancelled(true);
    19. } else {
    20. LastUsed.put(player.getName(), now);
    21. Location Location = player.getLocation();
    22. player.playEffect(Location, Effect.SMOKE, 10);
    23. for (Entity entity : player.getNearbyEntities(3, 3, 3))
    24. {
    25. if (entity instanceof LivingEntity)
    26. {
    27. ((LivingEntity) entity).damage(4, player);
    28. }
    29. }
    30. }
    31. }
     
  4. spiroulis You want the player to right click on a block and then left click, so the spell happens?
    I would recommend you to create a boolean and then set the it to true when right clicking a block and if left clicking a block check if the boolean is true.
    Somewhere before the Event Handler do
    Code:java
    1. boolean blockEnabled = false;


    and then change the code to something like:
    Code:java
    1. @EventHandler
    2. public void RageStrike(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(isWarrior.containsKey(player)){
    5. if(player.getItemInHand().getType() == Material.DIAMOND_SWORD || player.getItemInHand().getType() == Material.IRON_SWORD || player.getItemInHand().getType() == Material.STONE_SWORD || player.getItemInHand().getType() == Material.WOOD_SWORD) {
    6. if(player.getLevel() <= 9){ //lvl 1
    7. if(RageStrike1.containsKey(player)){
    8. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. blockEnabled = true;
    10. }
    11. if(event.getAction() == Action.LEFT_CLICK_BLOCK && blockEnabled == true){
    12. long last = LastUsed.containsKey(player.getName()) ? LastUsed.get(player.getName()) : 0;
    13. long ctm = System.currentTimeMillis();
    14. long timeleft = RageStrikeCooldown + last - ctm / 1000;
    15. if (ctm - last < RageStrikeCooldown) {
    16. event.getPlayer().sendMessage(ChatColor.DARK_RED + "You still have " + timeleft + " seconds cooldown for Rage Strike.");
    17. event.setCancelled(true);
    18. } else {
    19. LastUsed.put(player.getName(), now);
    20. Location Location = player.getLocation();
    21. player.playEffect(Location, Effect.SMOKE, 10);
    22. for (Entity entity : player.getNearbyEntities(3, 3, 3)) {
    23. if (entity instanceof LivingEntity){
    24. ((LivingEntity) entity).damage(4, player);
    25. }
    26. }
    27. }
    28. blockEnabled = false;
    29. }
    30. }
    31. }
    32. }
    33. }
    34. }


    This one should hopefully work, if not, just tahg me and I will try to help! ;)
     
    spiroulis likes this.
  5. Offline

    spiroulis

    Lionhard Thanks for the help it works fine now but i still have one question. What if i wanted to make the player so when he right clicks if he doesnt left click in a specific amount of time the spell would be cancelled?
     
  6. spiroulis Hmm, I have no experience with this, I just got an Idea how you could get this to work. In the main class,
    in the onEnable() Method do following:
    Code:java
    1. // This will run the MyMethod() each amount of ticks you have specified.
    2. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. myMethod();
    5. }
    6. }, 0, 1200); // Replace 1200 with your time. (In Ticks, 20 ticks = 1 second.)
    7. //This will run the myMethod() each amount of ticks you have specified.


    If the third parameter is 0, when the server starts it runs the method without looking at the 'timer'. After that, the method will run each 1200 ticks. If you want it to start 1200 ticks after the plugin enables, replace 0 with 1200.
    Hope you understood that, I'm not that good at explaining things. :D

    and then create the MyMethod() somewhere in the code like:
    Code:java
    1. public void myMethod(){
    2. if(blockEnabled){
    3. blockEnabled = false;
    4. }
    5. }


    This should work, although if you right click a block 1 tick before the method will run, it will be set again to false so you would have to right click again and then left click.
     
  7. Offline

    spiroulis

    Lionhard Thank you so much, i changed the code a bit and it works as i want now (even though there are some tiny bugs) here is what i did just if you are interested to see xd
    Code:java
    1. public void RageStrike(PlayerInteractEvent event){
    2. final Player player = event.getPlayer();
    3. if(isWarrior.containsKey(player)){
    4. if(player.getItemInHand().getType() == Material.DIAMOND_SWORD || player.getItemInHand().getType() == Material.IRON_SWORD || player.getItemInHand().getType() == Material.STONE_SWORD || player.getItemInHand().getType() == Material.WOOD_SWORD) {
    5. if(player.getLevel() <= 9){ //lvl 1
    6. if(RageStrike1.containsKey(player)){
    7. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    8. blockEnabled = true;
    9. RightClicked = true;
    10. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    11. public void run() {
    12. if(RightClicked == true){
    13. player.sendMessage("test");
    14. RightClicked = false;
    15. }
    16. }
    17. }, 100L, 100L); //tick time 20 = 1 second
    18. player.playSound(player.getLocation(), Sound.CLICK, 1, 1);
    19. }
    20. if(event.getAction() == Action.LEFT_CLICK_BLOCK && blockEnabled == true && RightClicked == true){
    21. RightClicked = false;
    22.  


    and then the rest of the code
     
  8. spiroulis Glad that I helped you. Although, why do you have 2 variables blockEnabled and RightClicked? Isn't 1 enough?
     
Thread Status:
Not open for further replies.

Share This Page