Solved Give Player Amplified Speed Effect While Standing Near a Beacon

Discussion in 'Spigot Plugin Development' started by TechBug2012, Jun 6, 2017.

  1. I'm currently working on a city server with a few other people. The mall in this city is quite big and very annoying to get through, so I am developing a simple plugin that toggles a highly amplified speed effect on/off to be used only in the mall:
    Code (Java):

    PotionEffect superspeed = new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 10);
    This is normally a very simple plugin to make. However, the mall has several beacons around its entrance, which gives several potion effects, including speed. Before I remembered this, I checked whether or not the player has the superspeed effect simply like so:
    Code (Java):

    if (p.hasPotionEffect(PotionEffectType.SPEED)) {
        p.removePotionEffect(PotionEffectType.SPEED);
        p.sendMessage(ChatColor.GREEN + "You are no longer going super speedy!");
    } else {
        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 10));
        p.sendMessage(ChatColor.GREEN + "You are now going super speedy!");
    }
    However, this doesn't work if a player is standing near a beacon, because if they try to enable it, the plugin detects that they have a speed effect and "disables" it. They have to move away from the beacon in order to toggle superspeed on, which is very inconvenient. It also doesn't disable properly.

    To further the inconvenience, I made the plugin with the WorldGuard API so that if the player steps out of the mall region, the superspeed effect is automatically disabled, but if they exit near a beacon, it will send them the message saying that the effect was disabled even if they don't have it on.

    I've come up with this as a solution:
    Code (Java):

    if (cmd.getName().equalsIgnoreCase("superspeed")) {
                if (!(getRegionName(p, p.getLocation()).equals("mall"))) {
                    p.sendMessage(ChatColor.RED + "The superspeed command can only be used in the mall!");
                    return false;
                } else {
                    if (p.hasPotionEffect(PotionEffectType.SPEED)) {
                        for (PotionEffect e : p.getActivePotionEffects()) {
                            if (e.getAmplifier() == superspeed.getAmplifier()) {
                                p.removePotionEffect(PotionEffectType.SPEED);
                                p.sendMessage(ChatColor.GREEN + "You are no longer going super speedy!");
                                break;
                            }
                            else {
                                p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 10));
                                p.sendMessage(ChatColor.GREEN + "You are now going super speedy!");
                                break;
                            }
                        }
                    } else {
                        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 10));
                        p.sendMessage(ChatColor.GREEN + "You are now going super speedy!");
                    }
                }
            }
    It's almost there. Players can now properly disable near a beacon, but they cannot enable. When they try to enable, the plugin tells them the effect has been enabled, but the effect does not enable. I can't figure out why. Can anyone on here help me with this?

    Thanks in advance!
     
  2. Just do it with setWalkSpeed and ask him how much walkspeed he have (normal is 0.2F) and than set it to a higher speed like 0.5 and back again
     
    • Like Like x 1
  3. Integer.MAX_VALUE will not work properly AFAIK, try 50 or something (it bugs if over a certain number)
     
  4. I hadn't thought of using setWalkSpeed actually... I'll try that. Do you know how to get the walk speed though? What would I set it to in order to keep the same speed as a speed potion amplified by 10?
    I am using Integer.MAX_INT for the duration of the potion, not the speed, if that's what you were thinking. In any case, it seems to work fine.
     
  5. Oh, havent worked with potion effects in a while so got the order of arguments mixed up