Skip to content

Instantly share code, notes, and snippets.

@Rojoss
Created January 23, 2016 06:35
Show Gist options
  • Save Rojoss/d557a4a2eb4c39845893 to your computer and use it in GitHub Desktop.
Save Rojoss/d557a4a2eb4c39845893 to your computer and use it in GitHub Desktop.
/**
* Base Arena class.
* This class is a GameComponent so you can add any of the components.
* @see {@link com.jroossien.gameapi.components}
*/
public abstract class Arena extends GameComponent {
protected UUID uid;
protected String name;
protected String type;
/**
* Use the {@link com.jroossien.gameapi.game.Game#createArena(String)} method to create a new arena.
* @param uid The unique ID for the arena.
* @param type The type of arena which is the name of the {@link com.jroossien.gameapi.game.Game}.
* @param name The name of the arena.
*/
public Arena(UUID uid, String type, String name) {
super(null); //This is the base component and it has no parent.
this.uid = uid;
this.type = type;
this.name = name;
}
/**
* Arenas from config will instantiate using this constructor.
* @param data The {@link ConfigurationSection} with all the arena data.
*/
public Arena(ConfigurationSection data) {
super(null); //This is the base component and it has no parent.
uid = Parse.UUID(data.getString("uid"));
name = data.getString("name");
type = data.getString("type");
}
/**
* Used by the {@link com.jroossien.gameapi.game.Game} to save all the arena data.
* Do not manually call this method without using {@link com.jroossien.gameapi.game.Game}
* @param cfg The {@link YamlConfiguration} to save the data in.
* @return The YamlConfiguration will be returned to Game to actually save the data and such.
*/
public YamlConfiguration save(YamlConfiguration cfg) {
cfg.set("uid", uid.toString());
cfg.set("name", name);
//TODO: When the name is changed delete the previous config file and create a new one or rename it.
//TODO: Save the actual config file inside Game
return cfg;
}
/**
* Add all the {@link GameComponent}s to the arena.
* This allows you to design the game like how you want it.
* You can add as many components as you need.
* For example if you want to allow people to spectate your game you would add a {@link com.jroossien.gameapi.components.SpectateComponent}
* @see {@link com.jroossien.gameapi.components}
*/
public abstract void addComponents();
/**
* Get the unique ID of the arena.
* @return The unique ID. {@link UUID}
*/
public UUID getUid() {
return uid;
}
/**
* Get the name of the arena.
* Do not use the name to identify/reference an arena!
* Use {@link #getUid()} for that as that's unique to each arena.
* @return The name of the arena. <b>May not be unique!</b>
*/
public String getName() {
return name;
}
/**
* Set the name of the arena.
* @param name The new name for the arena.
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the type of the arena.
* This will be the name of the game for example Spleef.
* @return The arena type / game name.
*/
public String getType() {
return type;
}
}
/**
* A game component.
* The game component can have have many child components and it has one parent component.
* Except for the top level component which is the {@link com.jroossien.gameapi.game.arena.Arena} in this Game API which has no parent component.
*/
//TODO: Add conflicts between components so that you can't add componentX when it has componentY
public abstract class GameComponent {
private GameComponent parent;
private Map<Class<? extends GameComponent>, GameComponent> components = new HashMap<Class<? extends GameComponent>, GameComponent>();
/**
* Instantiate a new game component for the specified parent.
* It will not automatically add the component to provided parent.
* <b>Example usage:</b>
* addComponent(new SubComponent(this));
* @param parent The parent component.
* This may be null for the top level component.
*/
public GameComponent(GameComponent parent) {
this.parent = parent;
}
/**
* Check whether or not this component has a parent component.
* @return True when the component has a parent.
*/
public boolean hasParent() {
return parent != null;
}
/**
* Get the parent component instance.
* @return The parent component.
*/
public GameComponent getParent() {
return parent;
}
/**
* Adds a new component to this component.
* If this component already had a component of the provided type it will return the previous component.
* It won't overwrite the component.
* @param component The component instance to add.
* @return The added component or the previous component if it already had a component of that type.
*/
public GameComponent addComponent(GameComponent component) {
return components.put(component.getClass(), component);
}
/**
* Gets a child component from this component.
* Will return null if this component doesn't have the provided component type.
* @param <T> The type of component to get.
* @return A GameComponent or {@code null} if this component has no component of the specified type.
*/
public <T extends GameComponent> T getComponent() {
return (T)components.get(T);
}
/**
* Check if this component has a child component of the specified type.
* @param component The component type to check for.
* @return True when this component has a child of the specified component type.
*/
public boolean hasComponent(Class<? extends GameComponent> component) {
return components.containsKey(component);
}
}
/**
* Adding this component allows players to spectate the game.
*/
public class SpectateComponent extends GameComponent {
private List<UUID> spectators = new ArrayList<UUID>();
public SpectateComponent(GameComponent parent) {
super(parent);
}
/**
* Get the list with spectating players.
* @return List of spectating players their {@link UUID}s
*/
public List<UUID> getSpectators() {
return spectators;
}
/**
* Checks whether or not the specified players {@link UUID} is in the spectator list.
* @param player The players {@link UUID} to check.
* @return True when the spectator list contains the players {@link UUID}.
*/
public boolean isSpectating(UUID player) {
return spectators.contains(player);
}
/**
* Add the given players {@link UUID} to the spectator list.
* @param player The players {@link UUID} to add.
*/
public void addSpectator(UUID player) {
spectators.add(player);
}
/**
* Remove the given players {@link UUID} from the spectator list.
* @param player The players {@link UUID} to remove.
*/
public void removeSpectator(UUID player) {
spectators.add(player);
}
/**
* Clear the spectator list.
*/
public void removeSpectators() {
spectators.clear();
}
}
public class TestArena extends Arena {
public TestArena(UUID uid, String type, String name) {
super(uid, type, name);
}
@Override
public void addComponents() {
//Add a bunch of components here
addComponent(new SpectateComponent(this));
}
public TestArena(ConfigurationSection data) {
super(data);
//Load arena specific config data.
}
@Override
public YamlConfiguration save(YamlConfiguration cfg) {
//Save arena specific config data.
return super.save(cfg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment