Solved Toggle Particle Visibilty Command

Discussion in 'Plugin Development' started by Jaaakee224, Feb 13, 2014.

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

    Jaaakee224

    So I have a simple blood plugin, which on every hit, shows the player bleeding with redstone block particles. I want it so if players do the command "/realblood toggle" it will disable the visibility to only the player, and if the player runs the command again, it will enable.

    Here is my code so far..
    Code:java
    1. package me.Jaaakee224.RealBlood;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.Effect;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityDamageEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class RealBlood extends JavaPlugin implements Listener {
    17. public static Map<Player,Boolean> states = new HashMap<Player, Boolean>();
    18. @Override
    19. public void onDisable() {
    20.  
    21. }
    22.  
    23. @Override
    24. public void onEnable() {
    25. getServer().getPluginManager().registerEvents(this, this);
    26. }
    27.  
    28. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    29. if(command.getName().equalsIgnoreCase("realblood")) {
    30. if(args.length == 0) { return false; }
    31. if(args[0].equalsIgnoreCase("toggle")) {
    32.  
    33.  
    34. }
    35.  
    36. }
    37. return true;
    38. }
    39.  
    40. @SuppressWarnings("deprecation")
    41. @EventHandler
    42. public void EntityBlood(EntityDamageEvent e){
    43. for(Entity entity : e.getEntity().getNearbyEntities(32,32,32)) {
    44. if(entity instanceof Player) {
    45. Player target = (Player) entity;
    46. if (states.containsKey(target)) {
    47. if(!states.get(target)) { continue; }
    48. }
    49. target.playEffect(e.getEntity().getLocation().add(0, 1, 0), Effect.STEP_SOUND, 152);
    50. }
    51. }
    52. }
    53. }

    Please help me with this.
     
  2. Haha you probably shouldn't be suppressing deprecation warnings, but I guess it doesn't really matter that much.

    Also, you should probably just store player names not the player object and retrieve the player object whenever your need it using then name. There can be problems with storing the actual player object.

    Now to your code, it looks fine so far. Lists (Map) referencing players are really annoying because you need to have code making sure to add players every time they join, every time the onEnable runs, and you need to check that the player is actually inside the list whenever it is referenced. Alternatively you could try just having a list of player names that want blood disabled so you wouldn't have to make sure every player is inside it.
     
  3. Offline

    Jaaakee224

    BorisTheTerrible Can you show me an example of this? I haven't worked with HashMap in a long time, and I need a little refresh.
     
  4. Offline

    Jaaakee224

    bump

    Jaaakee224
    Pseudo logic:

    Command-
    If list contains sender name
    remove from list
    else
    add to list

    Event-
    Check if the player(s) invokved in hte event are in the list
    run code

    xTrollxDudex Thanks :D

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

Share This Page