Created
December 28, 2011 01:42
-
-
Save macalinao/1525755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.crimsonrpg.core.citizens; | |
import com.crimsonrpg.api.citizen.MessageLevel; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.entity.Player; | |
import org.getspout.spoutapi.player.SpoutPlayer; | |
import com.crimsonrpg.api.citizen.Citizen; | |
import com.crimsonrpg.flaggables.api.GenericFlaggable; | |
/** | |
* Represents a citizen of the world. | |
* This is a wrapper for a Player. | |
*/ | |
public class SimpleCitizen extends GenericFlaggable implements Citizen { | |
private Player player = null; | |
public SimpleCitizen(String name) { | |
super(name); | |
} | |
public String getName() { | |
return player.getName(); | |
} | |
public Location getLocation() { | |
return player.getLocation(); | |
} | |
public boolean hasPermission(String permission) { | |
return player.hasPermission(permission); | |
} | |
public void sendMessage(String string) { | |
player.sendMessage(string); | |
} | |
public void sendMessage(String message, MessageLevel level) { | |
player.sendMessage(level.getColor().toString() + message); | |
} | |
public Player getBukkitEntity() { | |
return player; | |
} | |
public SpoutPlayer getSpoutEntity() { | |
if (player instanceof SpoutPlayer) { | |
return (SpoutPlayer) player; | |
} else { | |
throw new IllegalArgumentException("Spout isn't enabled at the moment!"); | |
} | |
} | |
protected void setBukkitEntity(Player player) { | |
if (this.player == null) { | |
this.player = player; | |
} | |
} | |
//DEPRECATED | |
public void sendError(String string) { | |
player.sendMessage(ChatColor.DARK_RED + string); | |
} | |
public void sendInfo(String string) { | |
player.sendMessage(ChatColor.YELLOW + string); | |
} | |
public void sendPlotMessage(String string) { | |
player.sendMessage(ChatColor.BLUE + string); | |
} | |
public void sendPrivateMessage(String string) { | |
player.sendMessage(ChatColor.LIGHT_PURPLE + string); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.crimsonrpg.core.citizens; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.bukkit.configuration.ConfigurationSection; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.entity.Player; | |
import org.getspout.spoutapi.player.SpoutPlayer; | |
import com.crimsonrpg.api.citizen.Citizen; | |
import com.crimsonrpg.api.citizen.CitizenManager; | |
import com.crimsonrpg.flaggables.api.Flag; | |
import com.crimsonrpg.flaggables.api.Flaggables; | |
/** | |
* Represents a simple citizen manager. | |
*/ | |
public class SimpleCitizenManager implements CitizenManager { | |
private Map<Player, Citizen> citizenList = new HashMap<Player, Citizen>(); | |
/** | |
* Loads a Citizen to the world. | |
* | |
* @param player | |
* @return | |
*/ | |
private Citizen loadCitizen(Player player) { | |
//Create the citizen object | |
SimpleCitizen citizen = new SimpleCitizen(player.getName()); | |
citizen.setBukkitEntity(player); | |
citizenList.put(player, citizen); | |
File citizenFolder = new File("./plugins/CrimsonCitizens/citizens/" + player.getName() + File.separator); | |
citizenFolder.mkdirs(); | |
File attributeFile = new File(citizenFolder.getPath() + File.separator + "attributes.yml"); | |
//Check if the citizen is new; if so, wait for a save | |
if (!attributeFile.exists()) { | |
return citizen; | |
} | |
ConfigurationSection flagsSection = YamlConfiguration.loadConfiguration(attributeFile); | |
List<Flag> flags = Flaggables.getFlagManager().makeFlagList(flagsSection); | |
for (Flag flag : flags) citizen.setFlag(flag); | |
return citizen; | |
} | |
public List<Citizen> getCitizenList() { | |
return new ArrayList(citizenList.values()); | |
} | |
public Citizen getCitizen(Player player) { | |
Citizen citizen = citizenList.get(player); | |
return (citizen == null) ? loadCitizen(player) : citizen; | |
} | |
public void unloadCitizen(Player player) { | |
citizenList.remove(player); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment