Skip to content

Instantly share code, notes, and snippets.

@WASasquatch
Last active April 15, 2017 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WASasquatch/bda451a3a9c643d2868f99a5b9d8f4b7 to your computer and use it in GitHub Desktop.
Save WASasquatch/bda451a3a9c643d2868f99a5b9d8f4b7 to your computer and use it in GitHub Desktop.
Manage multiple potion effects on multiple players at once
package wa.was.blastradius.blasts;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
/*************************
*
Copyright (c) 2017 Jordan Thompson (WASasquatch)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PotionEffectsManager#generateEffectsFromConfig() renders from the plugin
config. Below is an example of the configurations YAML format:
potion-effects:
Effect_Name:
type: PotionEffectType
duration: TICKS
amplifier: DOUBLE
ambient: BOOLEAN
particles: BOOLEAN
color: Color
For PotionEffectType's see: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html
For Color's see: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Color.html
*
**************************/
public class PotionEffectsManager {
private Collection<Player> effectedPlayers = new ArrayList<Player>();
private Collection<PotionEffect> potionEffects = new ArrayList<PotionEffect>();
private boolean hasParsedConfig = false;
private HashMap<String, Color> colors = new HashMap<String, Color>() {
private static final long serialVersionUID = 323135985062054098L; {
put("AQUA", Color.AQUA);
put("BLACK", Color.BLACK);
put("BLUE", Color.BLUE);
put("FUCHSIA", Color.FUCHSIA);
put("GRAY", Color.GRAY);
put("GREEN", Color.GREEN);
put("LIME", Color.LIME);
put("MAROON", Color.MAROON);
put("NAVY", Color.NAVY);
put("OLIVE", Color.OLIVE);
put("ORANGE", Color.ORANGE);
put("PRUPLE", Color.PURPLE);
put("RED", Color.RED);
put("SILVER", Color.SILVER);
put("TEAL", Color.TEAL);
put("WHIE", Color.WHITE);
put("YELLOW", Color.YELLOW);
}
};
private HashMap<String, PotionEffectType> effectTypes = new HashMap<String, PotionEffectType>() {
private static final long serialVersionUID = 7862567811034165045L; {
for ( PotionEffectType type: PotionEffectType.values() ) {
if (type != null && type.getName() != null ) {
put(type.getName(), type);
}
}
}
};
public boolean addAllEffects(Collection<PotionEffect> effects) {
return potionEffects.addAll(effects);
}
public boolean addEffect(PotionEffect effect) {
return potionEffects.add(effect);
}
public boolean addAllPlayers(Collection<Player> players) {
return effectedPlayers.addAll(players);
}
public boolean addPlayer(Player player) {
return effectedPlayers.add(player);
}
public boolean addPlayersInRadius(Location center, World world, int radius, boolean ellipsis) {
Collection<Player> ep = new ArrayList<Player>();
List<Player> players = world.getPlayers();
if ( ellipsis ) {
radius = ( radius * radius );
}
for ( Player player : players ) {
if ( center.distanceSquared(player.getLocation()) <= radius ) {
ep.add(player);
}
}
if ( ep.size() > 0 ) {
addAllPlayers(ep);
return true;
}
return false;
}
public void applyEffectsToAll() {
for ( Player player: effectedPlayers ) {
player.addPotionEffects(potionEffects);
}
}
public boolean applyEffectsToPlayer(Player player) {
return player.addPotionEffects(potionEffects);
}
public Collection<PotionEffect> getPotionEffects() {
return potionEffects;
}
public Collection<Player> getPlayers() {
return effectedPlayers;
}
public boolean hasParsedConfig() {
return hasParsedConfig;
}
public boolean removeAllEffects(Collection<PotionEffect> effects) {
return potionEffects.removeAll(effects);
}
public boolean removeEffect(PotionEffect effect) {
return potionEffects.remove(effect);
}
public boolean removeAllPlayers(Collection<Player> players) {
return effectedPlayers.removeAll(players);
}
public boolean removePlayer(Player player) {
return effectedPlayers.remove(player);
}
public void reset() {
resetEffects();
resetPlayers();
}
public void resetEffects() {
potionEffects = new ArrayList<PotionEffect>();
}
public void resetPlayers() {
effectedPlayers = new ArrayList<Player>();
}
public void generateEffectsFromConfig(FileConfiguration config) {
try {
for (String effectName: config.getConfigurationSection("potion-effects").getKeys(false)) {
if ( effectTypes.containsKey(config.getString("potion-effects." + effectName + ".type").toUpperCase()) ) {
if ( config.getString("potion-effects." + effectName + ".color") != null &&
colors.containsKey(config.getString("potion-effects." + effectName + ".color")) ) {
potionEffects.add(
new PotionEffect(effectTypes.get(config.getString("potion-effects." + effectName + ".type").toUpperCase()),
config.getInt("potion-effects." + effectName + ".duration"),
config.getInt("potion-effects." + effectName + ".amplifier"),
config.getBoolean("potion-effects." + effectName + ".ambient"),
config.getBoolean("potion-effects." + effectName + ".particles"),
colors.get(config.getString("potion-effects." + effectName + ".color"))));
} else {
potionEffects.add(
new PotionEffect(effectTypes.get(config.getString("potion-effects." + effectName + ".type").toUpperCase()),
config.getInt("potion-effects." + effectName + ".duration"),
config.getInt("potion-effects." + effectName + ".amplifier"),
config.getBoolean("potion-effects." + effectName + ".ambient"),
config.getBoolean("potion-effects." + effectName + ".particles")));
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
hasParsedConfig = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment