Skip to content

Commit

Permalink
comments for license modules
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jan 15, 2024
1 parent 88cee66 commit cdaa0b7
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package me.hsgamer.hscore.license.common;

/**
* The common license properties for {@link LicenseProperties}
*/
public enum CommonLicenseProperty {
/**
* The identifier of the user
*/
USER("user"),
/**
* The identifier of the resource
*/
RESOURCE("resource"),
/**
* The unique identifier of the license
*/
NONCE("nonce"),
/**
* The type of the license
*/
TYPE("type"),
;
private final String key;
Expand All @@ -12,6 +27,11 @@ public enum CommonLicenseProperty {
this.key = key;
}

/**
* Get the key
*
* @return the key
*/
public String getKey() {
return key;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package me.hsgamer.hscore.license.common;

/**
* The license checker
*/
public interface LicenseChecker {
/**
* Check the license
*
* @return the result
*/
LicenseResult checkLicense();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,43 @@

import java.util.Properties;

/**
* The license properties
*/
public class LicenseProperties extends Properties {
/**
* Create a new instance
*/
public LicenseProperties() {
super();
}

/**
* Create a new instance
*
* @param defaults the default properties
*/
public LicenseProperties(Properties defaults) {
super(defaults);
}

/**
* Get the property
*
* @param key the key
*
* @return the property
*/
public String getProperty(CommonLicenseProperty key) {
return getProperty(key.getKey());
}

/**
* Set the property
*
* @param key the key
* @param value the value
*/
public void setProperty(CommonLicenseProperty key, String value) {
setProperty(key.getKey(), value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package me.hsgamer.hscore.license.common;

/**
* The result from {@link LicenseChecker}
*/
public class LicenseResult {
private final LicenseStatus status;
private final LicenseProperties properties;

/**
* Create a new result
*
* @param status the status
* @param properties the properties
*/
public LicenseResult(LicenseStatus status, LicenseProperties properties) {
this.status = status;
this.properties = properties;
}

/**
* Get the status
*
* @return the status
*/
public LicenseStatus getStatus() {
return status;
}

/**
* Get the properties
*
* @return the properties
*/
public LicenseProperties getProperties() {
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package me.hsgamer.hscore.license.common;

/**
* The status of the license
*/
public enum LicenseStatus {
/**
* The license is valid
*/
VALID,
/**
* The license is invalid
*/
INVALID,
/**
* The checker is offline
*/
OFFLINE,
/**
* Unknown status
*/
UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the base classes for the license checker
*/
package me.hsgamer.hscore.license.common;
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,54 @@
import me.hsgamer.hscore.license.common.LicenseResult;
import me.hsgamer.hscore.license.common.LicenseStatus;

/**
* The license checker for Polymart
*/
// TODO: Use Polymart API to check the license
public class PolymartLicenseChecker implements LicenseChecker {
private final String resource;
private final boolean isPaid;
private final PolymartLicenseFetcher fetcher;

/**
* Create a new license checker
*
* @param resource the resource id
* @param isPaid whether the resource is paid
* @param fetcher the license fetcher
*/
public PolymartLicenseChecker(String resource, boolean isPaid, PolymartLicenseFetcher fetcher) {
this.resource = resource;
this.isPaid = isPaid;
this.fetcher = fetcher;
}

/**
* Create a new license checker with the default fetcher
*
* @param resource the resource id
* @param isPaid whether the resource is paid
*/
public PolymartLicenseChecker(String resource, boolean isPaid) {
this(resource, isPaid, PolymartLicenseFetcher.defaultFetcher());
}

/**
* Check whether the checker can be used
*
* @param identifier the identifier
*
* @return true if it can be used
*/
public static boolean isAvailable(String identifier) {
return "1".equals(identifier);
}

/**
* Check whether the checker can be used
*
* @return true if it can be used
*/
public static boolean isAvailable() {
return isAvailable("%%__POLYMART__%%");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import me.hsgamer.hscore.license.common.CommonLicenseProperty;
import me.hsgamer.hscore.license.common.LicenseProperties;

/**
* The license entry for Polymart
*/
public class PolymartLicenseEntry {
public final String user;
public final String username;
Expand All @@ -26,6 +29,13 @@ public PolymartLicenseEntry(String user, String username, String resource, Strin
this.timestamp = timestamp;
}

/**
* Check if the entry is valid
*
* @param isPaid if the resource is paid
*
* @return true if it is
*/
public boolean isValid(boolean isPaid) {
boolean valid =
user != null && !user.isEmpty() && !user.contains("__USER__")
Expand All @@ -44,6 +54,11 @@ public boolean isValid(boolean isPaid) {
return valid;
}

/**
* Convert to properties
*
* @return the properties
*/
public LicenseProperties toProperties() {
LicenseProperties properties = new LicenseProperties();
properties.put(CommonLicenseProperty.TYPE, "polymart");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package me.hsgamer.hscore.license.polymart;

/**
* Fetch the {@link PolymartLicenseEntry}
*/
public interface PolymartLicenseFetcher {
/**
* The default fetcher.
* The values will be replaced by Polymart when uploading the resource.
*
* @return the default fetcher
*/
static PolymartLicenseFetcher defaultFetcher() {
return () -> {
// Will be replaced by Polymart
Expand All @@ -18,5 +27,10 @@ static PolymartLicenseFetcher defaultFetcher() {
};
}

/**
* Fetch the license entry
*
* @return the license entry
*/
PolymartLicenseEntry fetchLicense();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the implementation of {@link me.hsgamer.hscore.license.common.LicenseChecker} for Polymart
*/
package me.hsgamer.hscore.license.polymart;
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@
import me.hsgamer.hscore.license.common.LicenseResult;
import me.hsgamer.hscore.license.common.LicenseStatus;

/**
* The license checker for SpigotMC
*/
public class SpigotLicenseChecker implements LicenseChecker {
private final String resource;
private final SpigotLicenseFetcher fetcher;

/**
* Create a new license checker
*
* @param resource the resource id
* @param fetcher the fetcher
*/
public SpigotLicenseChecker(String resource, SpigotLicenseFetcher fetcher) {
this.resource = resource;
this.fetcher = fetcher;
}

/**
* Create a new license checker with the default fetcher
*
* @param resource the resource id
*/
public SpigotLicenseChecker(String resource) {
this(resource, SpigotLicenseFetcher.defaultFetcher());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import me.hsgamer.hscore.license.common.CommonLicenseProperty;
import me.hsgamer.hscore.license.common.LicenseProperties;

/**
* The license entry for SpigotMC
*/
public class SpigotLicenseEntry {
public final String user;
public final String resource;
Expand All @@ -14,13 +17,23 @@ public SpigotLicenseEntry(String user, String resource, String nonce) {
this.nonce = nonce;
}

/**
* Check if the entry is valid
*
* @return true if it is
*/
public boolean isValid() {
return
user != null && !user.contains("__USER__")
&& resource != null && !resource.contains("__RESOURCE__")
&& nonce != null && !nonce.contains("__NONCE__");
}

/**
* Convert to properties
*
* @return the properties
*/
public LicenseProperties toProperties() {
LicenseProperties properties = new LicenseProperties();
properties.setProperty(CommonLicenseProperty.TYPE, "spigotmc");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package me.hsgamer.hscore.license.spigotmc;

/**
* Fetch the {@link SpigotLicenseEntry}
*/
public interface SpigotLicenseFetcher {
/**
* The default fetcher.
* The values will be replaced by SpigotMC when uploading the resource.
*
* @return the default fetcher
*/
static SpigotLicenseFetcher defaultFetcher() {
return () -> {
// Will be replaced by SpigotMC
Expand All @@ -12,5 +21,10 @@ static SpigotLicenseFetcher defaultFetcher() {
};
}

/**
* Fetch the license entry
*
* @return the license entry
*/
SpigotLicenseEntry fetchLicense();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the implementation of {@link me.hsgamer.hscore.license.common.LicenseChecker} for SpigotMC
*/
package me.hsgamer.hscore.license.spigotmc;

0 comments on commit cdaa0b7

Please sign in to comment.