Skip to content

Instantly share code, notes, and snippets.

@WASasquatch
Last active April 15, 2017 03:29
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/908d984492a61d9a6046f4e8d6a9d5dc to your computer and use it in GitHub Desktop.
Save WASasquatch/908d984492a61d9a6046f4e8d6a9d5dc to your computer and use it in GitHub Desktop.
PotionEffectsManager2 - Manage Players and Potion Effects by Sets
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.entity.Player;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.potion.PotionEffect;
/*************************
*
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.
*
**************************/
public class PotionEffectsManager {
private Map<String, Collection<Player>> playerSets = new HashMap<String, Collection<Player>>();
private Map<String, Collection<PotionEffect>> potionSets = new HashMap<String, Collection<PotionEffect>>();
public boolean addAllEffects(String set, Collection<PotionEffect> effects) {
if ( ! ( playerSets.containsKey(set) ) ) {
createPotionSet(set);
}
Collection<PotionEffect> potionEffects = potionSets.get(set);
potionEffects.addAll(effects);
potionSets.put(set, potionEffects);
if ( potionSets.get(set).containsAll(effects) ) {
return true;
}
return false;
}
public boolean addEffect(String set, PotionEffect effect) {
if ( ! ( potionSets.containsKey(set) ) ) {
createPotionSet(set);
}
Collection<PotionEffect> potionEffects = potionSets.get(set);
return potionEffects.add(effect);
}
public boolean addAllPlayers(String set, Collection<Player> players) {
if ( ! ( playerSets.containsKey(set) ) ) {
createPlayerSet(set);
}
Collection<Player> playerSet = playerSets.get(set);
playerSet.addAll(players);
playerSets.put(set, playerSet);
if ( playerSets.get(set).containsAll(players) ) {
return true;
}
return false;
}
public boolean addPlayer(String set, Player player) {
if ( ! ( playerSets.containsKey(set) ) ) {
createPlayerSet(set);
}
return playerSets.get(set).add(player);
}
public boolean addPlayersInRadius(String set, Location center, World world, int radius, boolean ellipsis) {
Collection<Player> ep = Collections.emptySet();
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(set, ep);
return true;
}
return false;
}
public Collection<Player> applySetToPlayers(String potionSet, String playerSet) {
Collection<Player> rogues = Collections.emptySet();
for ( Player player: playerSets.get(playerSet) ) {
if ( ! ( player.addPotionEffects(potionSets.get(potionSet)) ) ) {
rogues.add(player);
}
}
if ( rogues.size() > 0 ) {
// Something has gone wrong with applying effects to these "rogues"
return rogues;
}
return Collections.emptySet();
}
public Collection<Player> applySetToAllPlayers(String potionSet) {
Collection<Player> rogues = Collections.emptySet();
for ( Collection<Player> players : playerSets.values() ) {
for ( Player player : players ) {
if ( ! ( player.addPotionEffects(potionSets.get(potionSet)) ) ) {
rogues.add(player);
}
}
}
if ( rogues.size() > 0 ) {
// Something has gone wrong with applying effects to these "rogues"
return rogues;
}
return null;
}
public Collection<Player> applyAllSetsToPlayers(String set) {
Collection<Player> rogues = Collections.emptySet();
Collection<PotionEffect> potionEffects = new ArrayList<PotionEffect>();
for ( Collection<PotionEffect> effects : potionSets.values() ) {
potionEffects.addAll(effects);
}
for ( Player player : playerSets.get(set) ) {
if ( ! ( player.addPotionEffects(potionEffects) ) ) {
rogues.add(player);
}
}
if ( rogues.size() > 0 ) {
// Something has gone wrong with applying effects to these "rogues"
return rogues;
}
return Collections.emptySet();
}
public Collection<Player> applyAllSetsToAllPlayers() {
Collection<Player> rogues = Collections.emptySet();
Collection<PotionEffect> potionEffects = new ArrayList<PotionEffect>();
for ( Collection<PotionEffect> effects : potionSets.values() ) {
potionEffects.addAll(effects);
}
for ( Collection<Player> players : playerSets.values() ) {
for ( Player player : players ) {
if ( ! ( player.addPotionEffects(potionSets.get(potionEffects)) ) ) {
rogues.add(player);
}
}
}
if ( rogues.size() > 0 ) {
// Something has gone wrong with applying effects to these "rogues"
return rogues;
}
return Collections.emptySet();
}
public boolean applyEffectsToPlayer(String set, Player player) {
return player.addPotionEffects(potionSets.get(set));
}
// Create or reset a player set
public void createPlayerSet(String set) {
playerSets.put(set, Collections.emptySet());
}
// Create or reset a potion set
public void createPotionSet(String set) {
potionSets.put(set, Collections.emptySet());
}
public Collection<PotionEffect> getPotionSet(String set) {
return potionSets.get(set);
}
public Collection<Player> getPlayerSet(String set) {
return playerSets.get(set);
}
public boolean removeAllEffects(String set, Collection<PotionEffect> effects) {
return potionSets.get(set).removeAll(effects);
}
public boolean removeEffect(String set, PotionEffect effect) {
return potionSets.get(set).remove(effect);
}
public boolean removeAllPlayers(String set, Collection<Player> players) {
return playerSets.get(set).removeAll(players);
}
public boolean removePlayer(String set, Player player) {
return playerSets.get(set).remove(player);
}
public void reset() {
resetPotionSets();
resetPlayerSets();
}
public void resetPotionSets() {
potionSets = new HashMap<String, Collection<PotionEffect>>();
}
public void resetPlayerSets() {
playerSets = new HashMap<String, Collection<Player>>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment