Solved Problems with shields

Discussion in 'Plugin Development' started by ZodiacTheories, Jun 4, 2014.

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

    ZodiacTheories

    Hi, so I am coding a plugin and one of the abilities is that when you block with a wood sword you are in shield mode until you block again. For some reason this code doesn't work:

    Code:java
    1. package me.thesamster8.heroesskills.Skills1;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class Shield implements Listener {
    14.  
    15.  
    16.  
    17. private boolean shielded = false;
    18.  
    19. @EventHandler
    20. public void onBlock(PlayerInteractEvent e) {
    21. Player p = e.getPlayer();
    22. if(p.getItemInHand()==new ItemStack(Material.WOOD_SWORD)){
    23. if(e.getAction()==Action.RIGHT_CLICK_AIR || e.getAction()==Action.RIGHT_CLICK_BLOCK) {
    24. shielded = true;
    25. p.sendMessage(ChatColor.GREEN + "Shielded Mode: On");
    26. }
    27. else if(shielded) {
    28. shielded = false;
    29. p.sendMessage("Shielded Mode: Off");
    30. }
    31. }
    32. }
    33.  
    34. @EventHandler
    35. public void onDamage(EntityDamageByEntityEvent e) {
    36. Player p = (Player) e.getEntity();
    37. Player d = (Player) e.getDamager();
    38. if(shielded) {
    39. e.setCancelled(true);
    40. }
    41. }
    42. }


    And I have registered my events in my Main class
     
  2. ZodiacTheories
    All of it doesn't work or what exactly happens?
    I'd add this between 23 and 24:
    Code:java
    1. if(!shielded)
     
  3. Offline

    theguynextdoor

    First off, you need to rethink your use of that boolean, especially considering it is a global instance variable because if your code did work then if I were to right click and activate the shield, then it would be active for everyone and also anyone who right clicks with their sword would also deactivate the shield. To fix this problem you would need to look into the use of a List.

    Next thing I would say is that when checking the item in their hand, the way I would do it is I would first check they have something in their hand (hint: It's null if they aren't holding anything). After that I would then get the type of the item in their hand and then do a simple enum check with the Material for wooden sword. Then you would do your stuff with the list.

    With your damage event, you are casting the entities to players without even checking that it was a player that either did the damage, or received it. You should probably add those checks before casting. After that you can do you boolean check with your List which you would have implemented by then and cancel the event accordingly.

    If your code does not appear to be running at all, then add debug messages. One for each block of code. I.e one at the start of the event to see if it is being called, one in the first if statement, one in the second if statement and if you really feel like it, then one in the else statement. From this you can tell what code is being run or not.
     
  4. Offline

    ZodiacTheories

    RAFA_GATO_FOFO

    Updated code as you suggested.

    Nothing happens when I right click a wood sword. This is the only error that appears in the console:
    Code:
    04.06 21:56:33 [Server] WARN No compatible nms block class found.
    Thanks

    theguynextdoor

    Woah, thanks for that detailed response :D

    For the first bit, I was just testing this ability to see if it would work in general, I am planning to add a list later. Second bit: Ill try that. Third: Ill try that. Fouth: Was going to do that :p

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

    fireblast709

    It's a WorldEdit warning, basically informing you that the plugin is out of date.
     
  6. Offline

    ZodiacTheories

    fireblast709

    oh lol ty

    fireblast709 theguynextdoor RAFA_GATO_FOFO

    Thanks guys - especially theguynextdoor!

    For anybody wondering, here is the solution:

    Code:java
    1. package me.thesamster8.heroesskills.Skills1;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12.  
    13. public class Shield implements Listener {
    14.  
    15.  
    16.  
    17. private boolean shielded = false;
    18.  
    19. @EventHandler
    20. public void onBlock(PlayerInteractEvent e) {
    21. Player p = e.getPlayer();
    22. if(p.getItemInHand()==null) return;
    23. else {
    24. if(p.getItemInHand().getType()==Material.WOOD_SWORD) {
    25. if(e.getAction()==Action.RIGHT_CLICK_AIR || e.getAction()==Action.RIGHT_CLICK_BLOCK) {
    26. if(!shielded) {
    27. shielded = true;
    28. p.sendMessage(ChatColor.GREEN + "Shielded Mode: On");
    29. }
    30. else if(shielded) {
    31. shielded = false;
    32. p.sendMessage(ChatColor.RED + "Shielded Mode: Off");
    33. }
    34. }
    35. }
    36. }
    37. }
    38.  
    39. @EventHandler
    40. public void onDamage(EntityDamageByEntityEvent e) {
    41. Entity p = e.getEntity();
    42. Entity d = e.getDamager();
    43. if(p instanceof Player) {
    44. p = (Player) e.getEntity();
    45. if(shielded) {
    46. e.setCancelled(true);
    47. }
    48. }
    49. }
    50. }


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

Share This Page