Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some providers to interfaces. #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 8 additions & 125 deletions src/main/java/net/canarymod/user/OperatorsProvider.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,18 @@
package net.canarymod.user;

import net.canarymod.ToolBox;
import net.canarymod.api.PlayerReference;
import net.canarymod.backbone.BackboneOperators;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static net.canarymod.Canary.log;

/**
* Access to the backbone for operators
*
* @author Jason (darkdiplomat)
*/
public class OperatorsProvider {
private BackboneOperators backboneOps;
private List<String> ops;

public OperatorsProvider() {
backboneOps = new BackboneOperators();
ops = backboneOps.loadOps();
readOpsCfg();
}
public interface OperatorsProvider {

/**
* Reload the ops from database
*/
public void reload() {
ops = backboneOps.loadOps();
readOpsCfg();
}

/**
* Reads the config/ops.cfg file if it exists and updates the database
* with the names found in it.
*/
private void readOpsCfg() {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("config/ops.cfg")));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
continue;
}
if (!isOpped(line)) {
addPlayer(line);
}
}
}
catch (FileNotFoundException e) {
log.info("Could not find config/ops.cfg. Creating one for you...");
File f = new File("config/ops.cfg");
try {
if (f.createNewFile()) {
PrintWriter pwriter = new PrintWriter(new FileWriter(f));
pwriter.println("# Note: This file is not guaranteed to be synchronous with the actual ops list in database.");
pwriter.println("# However, you may use it to quickly add new operators as you please.");
pwriter.println("# Any duplicate entries will be taken care of so don't worry.");
pwriter.println("# Lines starting with # are comments ;)");
pwriter.println("# Add one name to each line.");
pwriter.close();
log.info("You can now add ops to config/ops.cfg (one per line!). We left you a note.");
}
}
catch (IOException e1) {
log.error("Failed to write config/ops.cfg! (Probably no write-access!)", e);
}
}
catch (IOException e) {
log.error("Failed to read from config/ops.cfg!", e);
}
}
void reload();

/**
* Check if a given Player name or UUID is opped.
Expand All @@ -87,89 +22,37 @@ private void readOpsCfg() {
*
* @return true if player is opped, false otherwise
*/
public boolean isOpped(String nameOrUUID) {
// Did UUID get passed?
if (ToolBox.isUUID(nameOrUUID)) {
return ops.contains(nameOrUUID);
}
else {
return ops.contains(ToolBox.usernameToUUID(nameOrUUID));
}
}
boolean isOpped(String nameOrUUID);

public boolean isOpped(PlayerReference playerReference) {
if (playerReference != null) {
// Lets update to UUID if we can get a UUID
if (ops.contains(playerReference.getName())) {
if (playerReference.getUUIDString() != null) {
removePlayer(playerReference.getName());
addPlayer(playerReference.getUUIDString());
}
return true;
}
// UUID test it is
return ops.contains(playerReference.getUUIDString());
}
return false;
}
boolean isOpped(PlayerReference playerReference);

/**
* Adds a new operators entry
*
* @param entry
* the player uuid/name you want to add
*/
public void addPlayer(String entry) {
if (!ops.contains(entry)) {
if (!ToolBox.isUUID(entry)) {
String uuid = ToolBox.usernameToUUID(entry);
if (!ops.contains(uuid)) {
ops.add(uuid);
backboneOps.addOpEntry(uuid);
}
}
else {
ops.add(entry);
backboneOps.addOpEntry(entry);
}
}
}
void addPlayer(String entry);

/**
* Removes the given player from the ops list
*
* @param entry
* the player uuid/name you want to remove
*/
public void removePlayer(String entry) {
if (ops.contains(entry)) {
ops.remove(entry);
backboneOps.removeOpEntry(entry);
}
else if (!ToolBox.isUUID(entry)) {
String uuid = ToolBox.usernameToUUID(entry);
if (ops.contains(uuid)) {
ops.remove(uuid);
backboneOps.removeOpEntry(uuid);
}
}
}
void removePlayer(String entry);

/**
* gets the current size of the ops list
*
* @return the size
*/
public int getSize() {
return ops.size();
}
int getSize();

/**
* Gets an array of all Operators
*
* @return
*/
public String[] getOps() {
return ops.toArray(new String[getSize()]);
}
String[] getOps();
}
56 changes: 7 additions & 49 deletions src/main/java/net/canarymod/user/WhitelistProvider.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package net.canarymod.user;

import net.canarymod.ToolBox;
import net.canarymod.backbone.BackboneWhitelist;

import java.util.List;

/**
* Access to the backbone for whitelist
*
* @author Chris (damagefilter)
*/
public class WhitelistProvider {
private BackboneWhitelist backboneWhitelist;
private List<String> whitelist;

public WhitelistProvider() {
backboneWhitelist = new BackboneWhitelist();
whitelist = backboneWhitelist.loadWhitelist();
}
public interface WhitelistProvider {

/**
* Reload the whitelist from database
*/
public void reload() {
whitelist = backboneWhitelist.loadWhitelist();
}
void reload();

/**
* Check if a given player is whitelisted.
Expand All @@ -34,58 +20,30 @@ public void reload() {
*
* @return
*/
public boolean isWhitelisted(String subject) {
String uuid = subject;
if (!ToolBox.isUUID(uuid)) {
uuid = ToolBox.usernameToUUID(uuid);
}
return whitelist.contains(uuid);
}
boolean isWhitelisted(String subject);

/**
* Adds a new whitelist entry
*
* @param subject
* player name or uuid
*/
public void addPlayer(String subject) {
String uuid = subject;
if (!ToolBox.isUUID(uuid)) {
uuid = ToolBox.usernameToUUID(uuid);
}
if (!whitelist.contains(uuid)) {
whitelist.add(uuid);
backboneWhitelist.addWhitelistEntry(uuid);
}
}
void addPlayer(String subject);

/**
* Removes the given player from the whitelist
*
* @param subject
* player name or uuid
*/
public void removePlayer(String subject) {
String uuid = subject;
if (!ToolBox.isUUID(uuid)) {
uuid = ToolBox.usernameToUUID(uuid);
}
if (whitelist.contains(uuid)) {
whitelist.remove(uuid);
backboneWhitelist.removeWhitelistEntry(uuid);
}
}
void removePlayer(String subject);

/**
* gets the current size of the whitelist
*
* @return
*/
public int getSize() {
return whitelist.size();
}
int getSize();

public String[] getWhitelisted() {
return whitelist.toArray(new String[getSize()]);
}
String[] getWhitelisted();
}