Bumped to v2.1 BETA
This commit is contained in:
parent
5accb3cd0d
commit
d5c8f8597a
7 changed files with 238 additions and 143 deletions
|
@ -12,6 +12,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -58,6 +59,7 @@ public class LGUtil {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isAan(lampBlock)) {
|
||||
final Block neighbor = getNeighbor(lampBlock);
|
||||
if (neighbor != null) {
|
||||
|
@ -97,8 +99,9 @@ public class LGUtil {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isAan(lampBlock)) {
|
||||
lampBlock.setType(Objects.requireNonNull(Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0])));
|
||||
lampBlock.setType(Objects.requireNonNull(Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1])));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -250,15 +253,18 @@ public class LGUtil {
|
|||
}
|
||||
|
||||
private static boolean isAan(Block lamp) {
|
||||
return ((nieuweVersie) && (Objects.requireNonNull(getAsString(getBlockData(lamp))).contains("lit=true"))) || ((!nieuweVersie) && (lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1])));
|
||||
if (nieuweVersie) {
|
||||
return getAsString(getBlockData(lamp)).contains("lit=true");
|
||||
} else {
|
||||
return lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isLamp(Block lamp) {
|
||||
if (nieuweVersie) {
|
||||
return lamp.getType() == XMaterial.REDSTONE_LAMP.parseMaterial();
|
||||
} else {
|
||||
return lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1])
|
||||
|| lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0]);
|
||||
return Arrays.asList(XMaterial.REDSTONE_LAMP.getLegacy()).contains(lamp.getType().name());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,13 @@ package nl.sbdeveloper.themeparkplus.util;
|
|||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
@ -14,28 +20,29 @@ import java.net.URL;
|
|||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* License class for SBDevelopment
|
||||
*
|
||||
* v1.3 - Changed in 03-03-2020
|
||||
* v1.6 - Changed on 06-08-2020
|
||||
*
|
||||
* @author Stijn [SBDeveloper]
|
||||
* @since 23-12-2019
|
||||
*/
|
||||
public class License {
|
||||
public class License implements Listener {
|
||||
/*
|
||||
This file is part of ActionFoto.
|
||||
This file is part of ThemeParkRidecountAddon.
|
||||
Copyright (c) 2018-2020 SBDevelopment - All Rights Reserved
|
||||
Unauthorized copying of this file, via any medium is strictly prohibited
|
||||
Proprietary and confidential
|
||||
Written by Stijn Bannink <stijnbannink23@gmail.com>, January 2020
|
||||
*/
|
||||
|
||||
private Plugin plugin;
|
||||
private String license;
|
||||
private String prefix;
|
||||
private JavaPlugin plugin; // The plugin instance
|
||||
private String license; // The license code
|
||||
private String prefix; // The correct prefix for this plugin
|
||||
@Nullable private String invalidReason; // The reason the license is invalid, if null it's not invalid!
|
||||
@Nullable private static Boolean valid; // If true, it's valid, if false, it's not valid, if null it's not checked!
|
||||
|
||||
/**
|
||||
* Construct a new license
|
||||
|
@ -43,176 +50,158 @@ public class License {
|
|||
* @param prefix The prefix, like TPP or AF
|
||||
* @param license The license from the config
|
||||
*/
|
||||
public License(Plugin plugin, String prefix, String license) {
|
||||
public License(JavaPlugin plugin, String prefix, String license) {
|
||||
this.prefix = prefix;
|
||||
this.plugin = plugin;
|
||||
this.license = license;
|
||||
|
||||
startTimer();
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
|
||||
validateLicense();
|
||||
}
|
||||
|
||||
private void startTimer() {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
|
||||
if (!validateLicense()) {
|
||||
Bukkit.getLogger().severe("[" + prefix + "] License is incorrect!");
|
||||
}
|
||||
}, 0, 20 * 60 * 60);
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
if (this.invalidReason == null) return;
|
||||
|
||||
Player p = e.getPlayer();
|
||||
if (p.isOp() || p.hasPermission("sbd.licensemessages")) {
|
||||
Bukkit.getScheduler().runTaskLater(this.plugin, () -> p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + this.plugin.getName() + ChatColor.GOLD + "] " + ChatColor.RED + "The license is incorrect! Reason: " + ChatColor.GOLD + this.invalidReason), 3 * 20L /* 3 sec */);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a license
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
private boolean validateLicense() {
|
||||
private void validateLicense() {
|
||||
//STEP 1: Check prefix
|
||||
if (!this.license.split("-")[0].contains(this.prefix)) {
|
||||
disable("You used the wrong license for this product.");
|
||||
return;
|
||||
}
|
||||
|
||||
//STEP 2: Send license request
|
||||
String url = "https://sbdplugins.nl/wp-json/lmfwc/v2/licenses/" + this.license;
|
||||
|
||||
@Nullable JsonObject res;
|
||||
@Nullable JsonObject response;
|
||||
try {
|
||||
res = sendGETRequestJSON(url);
|
||||
response = sendGETRequestJSON(url);
|
||||
} catch (IOException e) {
|
||||
disable("GET_request_error");
|
||||
return false;
|
||||
disable("Couldn't send the request.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (res == null) {
|
||||
disable("GET_request_error_2");
|
||||
return false;
|
||||
if (response == null) {
|
||||
disable("Couldn't send the request.");
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject dat = res.get("data").getAsJsonObject();
|
||||
JsonObject dataObject = response.get("data").getAsJsonObject();
|
||||
|
||||
int stat = dat.get("status").getAsInt();
|
||||
if (stat == 404) {
|
||||
disable("status_404_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dat.get("licenseKey").isJsonNull()) {
|
||||
disable("license_null_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!dat.get("licenseKey").getAsString().split("-")[0].contains(prefix)) {
|
||||
disable("prefix_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
switch(dat.get("status").getAsString()) {
|
||||
//STEP 3: Check status
|
||||
switch(dataObject.get("status").getAsString()) {
|
||||
case "2":
|
||||
//activate?
|
||||
//Delivered -> Try to activate (double check timesActivated)
|
||||
break;
|
||||
case "3":
|
||||
//it's good
|
||||
//Activated!
|
||||
break;
|
||||
default:
|
||||
disable("status_error");
|
||||
return false;
|
||||
disable("Your license has a wrong status.");
|
||||
return;
|
||||
}
|
||||
|
||||
//Not activated? Activate it!
|
||||
if (dat.get("timesActivated").isJsonNull() || dat.get("timesActivated").getAsString().equalsIgnoreCase("0")) {
|
||||
return activate();
|
||||
//STEP 4: Check times activated, and if not activated, activate.
|
||||
if (dataObject.get("timesActivated").isJsonNull() || dataObject.get("timesActivated").getAsString().equalsIgnoreCase("0")) {
|
||||
activate();
|
||||
return;
|
||||
}
|
||||
|
||||
if (dat.get("expiresAt").isJsonNull()) {
|
||||
disable("null_error");
|
||||
return false;
|
||||
//STEP 5: Check expire date
|
||||
if (dataObject.get("expiresAt").isJsonNull()) {
|
||||
disable("Your license has no expire date.");
|
||||
return;
|
||||
}
|
||||
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Date date;
|
||||
try {
|
||||
date = format.parse(dat.get("expiresAt").getAsString());
|
||||
date = format.parse(dataObject.get("expiresAt").getAsString());
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
disable("null_error");
|
||||
return false;
|
||||
disable("Your license has a wrong expire date.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(Objects.requireNonNull(date).after(new Date()))) {
|
||||
disable("expired_error");
|
||||
return false;
|
||||
if (date == null) {
|
||||
disable("Your license has a wrong expire date.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dat.get("ipcheck").getAsBoolean()) {
|
||||
disable("ip_error");
|
||||
return false;
|
||||
if (!(date.after(new Date()))) {
|
||||
disable("Your license has expired.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (dat.get("port").isJsonNull()) {
|
||||
disable("null_error");
|
||||
return false;
|
||||
//STEP 6: Check IP and port.
|
||||
if (!dataObject.get("ipcheck").getAsBoolean()) {
|
||||
disable("Your license has been used with another IP. Update it in our Discord.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int por = dat.get("port").getAsInt();
|
||||
if (por != Bukkit.getServer().getPort()) {
|
||||
disable("port_error");
|
||||
return false;
|
||||
}
|
||||
} catch(NumberFormatException e) {
|
||||
disable("null_error");
|
||||
return false;
|
||||
if (dataObject.get("port").isJsonNull()) {
|
||||
disable("Your license has no port.");
|
||||
return;
|
||||
}
|
||||
|
||||
return true;
|
||||
String por = dataObject.get("port").getAsString();
|
||||
if (!checkPortValue(Bukkit.getServer().getPort(), por)) {
|
||||
disable("Your license has been used with another Port. Update it in our Discord.");
|
||||
return;
|
||||
}
|
||||
|
||||
valid = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate the license (private)
|
||||
* Activate the license
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
private boolean activate() {
|
||||
private void activate() {
|
||||
//STEP 1: Send license activate request
|
||||
String url = "https://sbdplugins.nl/wp-json/lmfwc/v2/licenses/activate/" + this.license + "?port=" + Bukkit.getServer().getPort();
|
||||
|
||||
@Nullable JsonObject res;
|
||||
@Nullable JsonObject response;
|
||||
try {
|
||||
res = sendGETRequestJSON(url);
|
||||
response = sendGETRequestJSON(url);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disable("GET_request_error");
|
||||
return false;
|
||||
disable("Couldn't send the activate request.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (res == null) {
|
||||
disable("GET_request_error_2");
|
||||
return false;
|
||||
if (response == null) {
|
||||
disable("Couldn't send the activate request.");
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject dat = res.get("data").getAsJsonObject();
|
||||
JsonObject dataObject = response.get("data").getAsJsonObject();
|
||||
|
||||
int stat = dat.get("status").getAsInt();
|
||||
if (stat == 404) {
|
||||
disable("status_404_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dat.get("licenseKey").isJsonNull()) {
|
||||
disable("license_null_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!dat.get("licenseKey").getAsString().split("-")[0].contains(prefix)) {
|
||||
disable("prefix_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
switch(dat.get("status").getAsString()) {
|
||||
//STEP 2: Check status
|
||||
switch(dataObject.get("status").getAsString()) {
|
||||
case "2":
|
||||
//activate?
|
||||
//Delivered -> STILL NOT ACTIVATED?! -> Double check
|
||||
break;
|
||||
case "3":
|
||||
//it's good
|
||||
//Activated!
|
||||
break;
|
||||
default:
|
||||
disable("status_error");
|
||||
return false;
|
||||
disable("Your license has a wrong status.");
|
||||
return;
|
||||
}
|
||||
|
||||
//Still not activated? Something is wrong...
|
||||
return !dat.get("timesActivated").isJsonNull() && !dat.get("timesActivated").getAsString().equalsIgnoreCase("0");
|
||||
//STEP 3: Check times activated, and if still not activated, disable.
|
||||
if (dataObject.get("timesActivated").isJsonNull() || dataObject.get("timesActivated").getAsString().equalsIgnoreCase("0")) {
|
||||
disable("Couldn't activate the license.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -221,10 +210,15 @@ public class License {
|
|||
* @param reason The disabling reason
|
||||
*/
|
||||
private void disable(String reason) {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
Bukkit.getLogger().severe("[" + plugin.getName() + "] " + "Stopping plugin because licensing system check failed.");
|
||||
Bukkit.getLogger().severe("[" + plugin.getName() + "] " + "Reason: " + reason);
|
||||
Bukkit.getPluginManager().disablePlugin(plugin);
|
||||
this.invalidReason = reason;
|
||||
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> {
|
||||
valid = false;
|
||||
|
||||
Bukkit.getLogger().severe("[" + this.plugin.getName() + "] Stopping plugin because licensing system check failed.");
|
||||
Bukkit.getLogger().severe("[" + this.plugin.getName() + "] Reason: " + reason);
|
||||
Bukkit.getLogger().severe("[" + this.plugin.getName() + "] Contact the developer if you believe something is wrong on their side.");
|
||||
Bukkit.getPluginManager().disablePlugin(this.plugin);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -261,4 +255,51 @@ public class License {
|
|||
return parser.parse(response.toString()).getAsJsonObject();
|
||||
}
|
||||
|
||||
private boolean checkPortValue(int input, @NotNull String dataValue) {
|
||||
//STEP 1: Check wildcard
|
||||
if (dataValue.equals("*")) return true;
|
||||
|
||||
//STEP 2: Check if equals
|
||||
try {
|
||||
int dataVal = Integer.parseInt(dataValue);
|
||||
|
||||
return input == dataVal;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
//STEP 3: Check if range
|
||||
if (dataValue.contains("-")) {
|
||||
String[] dataSplit = dataValue.split("-");
|
||||
|
||||
//STEP 3.1: Check if min or max is wildcard
|
||||
if (dataSplit[0].equals("*") && !dataSplit[1].equals("*")) {
|
||||
int max = Integer.parseInt(dataSplit[1]);
|
||||
return input <= max;
|
||||
} else if (dataSplit[1].equals("*") && !dataSplit[0].equals("*")) {
|
||||
int min = Integer.parseInt(dataSplit[0]);
|
||||
return min <= input;
|
||||
} else {
|
||||
try {
|
||||
int min = Integer.parseInt(dataSplit[0]);
|
||||
int max = Integer.parseInt(dataSplit[1]);
|
||||
|
||||
return (min <= input) && (input <= max);
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Else, invalid value
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the license is valid
|
||||
*
|
||||
* @return true -> VALID, false -> INVALID, null -> UNCHECKED
|
||||
*/
|
||||
@Nullable
|
||||
public static Boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
}
|
Reference in a new issue