Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Last active December 23, 2015 23:09
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 PaulBGD/6708279 to your computer and use it in GitHub Desktop.
Save PaulBGD/6708279 to your computer and use it in GitHub Desktop.
PlayerManager Resource 1
package me.ultimate.Arena;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class PlayerManager {
private Arena arena; // This is used to get data from the arena object
private List<PlayerData> players = new ArrayList<PlayerData>();
public PlayerManager(Arena arena) {
this.arena = arena;
}
// We can use this to add players to the arena
public void addPlayer(Player p) {
// Setup the player with his data, and add it to the list
players.add(new PlayerData(p));
// Now, teleport him to where he needs to go (I remove fall distance so
// he doesn't die on teleport
p.setFallDistance(0);
p.teleport(arena.getSpawn());
// Let's say I want to give him a diamond sword when he spawns. This is
// where that would go.
p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
}
// The player has to leave the arena eventually, so we have to have this
// method.
public void removePlayer(Player p) {
// We'll loop through the data to find the player
for (PlayerData data : players) {
if (data.name.equalsIgnoreCase(p.getName())) {
// Match! We got his data.
data.reset();
// Again, we'll remove his fall distance and teleport him
p.setFallDistance(0);
p.teleport(arena.getStop());
}
}
// Well, we didn't find him. We just won't do anything
}
// Let's say you have a command, and you want to check if the player is in
// two arenas. All you have to do is check it here
public boolean playerPlaying(Player p) {
for (PlayerData data : players) {
if (data.name.equalsIgnoreCase(p.getName())) {
// Match! We found him in this arena
return true;
}
}
//Nope, no match. Return false
return false;
}
private class PlayerData {
// Holds the player's items
private ItemStack[] inventory;
private ItemStack[] armor;
// This is used to get the player object later
String name;
// This gets all of the items from the player's inventory, then clears
// it
public PlayerData(Player p) {
name = p.getName();
inventory = p.getInventory().getContents();
armor = p.getInventory().getArmorContents();
p.getInventory().clear();
p.getInventory().setArmorContents(null);
}
// This resets all of the player's things
public void reset() {
Player p = Bukkit.getPlayerExact(name);
p.getInventory().clear();
p.getInventory().setContents(inventory);
p.getInventory().setArmorContents(armor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment