Skip to content

Commit

Permalink
improv. fix error and convert to private key
Browse files Browse the repository at this point in the history
  • Loading branch information
nivcoo committed Nov 29, 2021
1 parent 81dfc3c commit 375f0db
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 67 deletions.
17 changes: 9 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>fr.nivcoo</groupId>
<artifactId>Pointz</artifactId>
<version>2.5.1</version>
<version>2.6.0</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
Expand All @@ -19,6 +19,10 @@

<repositories>

<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>vault-repo</id>
<url>https://nexus.hc.to/content/repositories/pub_releases</url>
Expand All @@ -27,19 +31,16 @@
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>private</id>
<url>https://edensky.fr/artifactory/private/</url>
</repository>


</repositories>

<dependencies>
<!-- Paper-API -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.13.2</version>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/fr/nivcoo/pointz/Pointz.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ public void onEnable() {
boolean pluginWebIsEnabled = false;

try {
websiteAPI = new WebsiteAPI(config.getString("api.public_key"), config.getString("api.website_url"));
websiteAPI = new WebsiteAPI(config.getString("api.private_key"), config.getString("api.website_url"));
goodKey = true;
} catch (Exception e) {
e.printStackTrace();
Bukkit.getLogger()
.severe("[Pointz] The public_key isn't valid ! Please copy it on the website in config section");
.severe("[Pointz] The private_key isn't valid ! Please copy it on the website in config section");
}
if (websiteAPI != null) {
HashMap<String, String> check;
Expand Down Expand Up @@ -126,7 +125,8 @@ public void onDisable() {
if (inventoryManager != null)
inventoryManager.closeAllInventories();

cacheManager.stopScheduler();
if (cacheManager != null)
cacheManager.stopScheduler();
}

public void registerCommands() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/fr/nivcoo/pointz/inventory/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package fr.nivcoo.pointz.inventory;

import org.bukkit.Bukkit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package fr.nivcoo.pointz.inventory;

import fr.nivcoo.pointz.Pointz;
Expand Down Expand Up @@ -32,7 +29,7 @@ public void init() {
for (Inventory inv : inventories.values()) {
int tick = 0;
Object currentTick = inv.get(Inventory.TICK);
if (currentTick != null && currentTick instanceof Integer)
if (currentTick instanceof Integer)
tick = Integer.parseInt(currentTick.toString());
inv.put(Inventory.TICK, tick + 1);
inv.getInventoryProvider().update(inv);
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/fr/nivcoo/pointz/inventory/ItemBuilder.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package fr.nivcoo.pointz.inventory;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import fr.nivcoo.pointz.utils.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;

public class ItemBuilder {
private Material m;
Expand Down Expand Up @@ -63,21 +56,6 @@ public ItemStack build() {
if (im != null) {
im.setDisplayName(name);
im.setLore(lores);
if (m == (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM")) && texture != null && !"".equalsIgnoreCase(texture.trim())) {
SkullMeta headMeta = (SkullMeta) im;
GameProfile profile = new GameProfile(UUID.randomUUID(), null);

profile.getProperties().put("textures", new Property("textures", texture));

try {
Field profileField = headMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(headMeta, profile);
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException
| IllegalAccessException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Error while setting head texture", ex);
}
}
is.setItemMeta(im);
}

Expand Down
31 changes: 15 additions & 16 deletions src/main/java/fr/nivcoo/pointz/utils/WebsiteAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,44 @@
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.KeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;
import java.util.stream.Collectors;

public class WebsiteAPI {

private KeyFactory keyFactory;
private PublicKey publicKey;
private PrivateKey privateKey;
private final String USER__AGENT = "Mozilla/5.0";
private String url;

public WebsiteAPI(String publicKeyString, String url) throws Exception {
public WebsiteAPI(String privateKeyString, String url) throws Exception {
this.keyFactory = KeyFactory.getInstance("RSA");
this.url = url + "/pointz/api";
String stringAfter = publicKeyString.replaceAll("\\n", "").replaceAll("-----BEGIN PUBLIC KEY-----", "")
.replaceAll("-----END PUBLIC KEY-----", "").trim();
String stringAfter = privateKeyString.replaceAll("\\n", "").replaceAll("-----BEGIN PRIVATE KEY-----", "")
.replaceAll("-----END PRIVATE KEY-----", "").trim();

byte[] decoded = Base64.getMimeDecoder().decode(stringAfter);

KeySpec keySpec = new X509EncodedKeySpec(decoded);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded);

publicKey = keyFactory.generatePublic(keySpec);
privateKey = keyFactory.generatePrivate(keySpec);

}

public String encryptToBase64(String plainText) {
String encoded = null;
try {
final Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, publicKey);
rsa.init(Cipher.ENCRYPT_MODE, privateKey);
rsa.update(plainText.getBytes());
final byte[] result = rsa.doFinal();

encoded = Base64.getUrlEncoder().withoutPadding().encodeToString(result);

} catch (Exception e) {
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #6");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #6");
}
return encoded;

Expand Down Expand Up @@ -143,7 +142,7 @@ public List<PlayersInformations> getPlayersInfos(List<Player> players) {
results.addAll(Arrays.asList(playersObject));

} catch (Exception e) {
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #5");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #5");

}

Expand All @@ -160,7 +159,7 @@ public void setMoneyPlayer(Player player, double getCibleMoneyAfter) {
sendPost(url, params);

} catch (Exception e) {
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #4");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #4");

}

Expand Down Expand Up @@ -199,7 +198,7 @@ public MWConfig initMWConfig() {
result = new MWConfig(String.valueOf(jobj.get("name_shop")), String.valueOf(jobj.get("name_gui")));

} catch (Exception e) {
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #3");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #3");

}

Expand Down Expand Up @@ -230,7 +229,7 @@ public List<ItemsConverter> initItemsConverter() {

} catch (Exception e) {
e.printStackTrace();
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #2");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #2");

}

Expand Down Expand Up @@ -271,7 +270,7 @@ public List<ItemsShop> initItemsShop() {

} catch (Exception e) {
e.printStackTrace();
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with public key #1");
Bukkit.getLogger().severe("[Pointz] You must Install the website plugin and link it with private key #1");

}

Expand Down
16 changes: 7 additions & 9 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ hooks:
api:
website_url: "https://edensky.fr"
#paste here your public key, get it on website (this is an example)
public_key: "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3vcaidbuH0Atm50iMkSh
TzaYrKEa3zi9TR7yX3bjL4fCo/rKnRc/HbVeH1AUhOT/TYsqBb/ZTyx8/i+XAZDb
0XnlnorTFSjC8tz7tVepv8+HfIw2jUIvUfMmgnhn1YwIXnJpgR/TJnAF04F5v/oD
UpIj2Dg6VP+MfBElaP4ddluHjZGzrIw+84gta1NuV1If2xgqQ0KxL8fxtnW3H9Qt
gu9BIH1Du8bpNBhURvtvUh3bf/3eHeilkfgI4KDTwTk8cnIIifPrafMAg/8/Apja
lDkFxztRYD7TTxFqLluhDFKR7sU8szDcivdB9NvXl4CoGXr5e/zaJ5IoeaiZBUTB
gQIDAQAB
-----END PUBLIC KEY-----"
private_key: "-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDSxqe/XX3ROrvR
BrH7rR1WlfOjvzqY+3OKiVvr3WJRWID4lygSbDxz7X18pEIw+OyyEK3A1UijhvGL
.....
LzQvPzJ2nHBRRwjrXRo7cqEGBumELPdaPvNtTf5mCjqDTMnheCHoWd56+wvL85ii
OpxYOcxxb3hUv0KYII2b2g==
-----END PRIVATE KEY-----"
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Pointz
version: 2.5.1
version: 2.6.0
author: nivcoo
main: fr.nivcoo.pointz.Pointz
softdepend: [ MVdWPlaceholderAPI, PlaceholderAPI ]
Expand Down

0 comments on commit 375f0db

Please sign in to comment.