Skip to content

Commit

Permalink
Readicionado remoção de chaves inválidas
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock committed Feb 11, 2024
1 parent 27b2faa commit c5821b8
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 73 deletions.
2 changes: 1 addition & 1 deletion paper/run/server.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Minecraft server properties
#Sun Feb 11 12:09:18 GMT-03:00 2024
#Sun Feb 11 16:58:39 GMT-03:00 2024
enable-jmx-monitoring=false
rcon.port=25575
level-seed=
Expand Down
34 changes: 18 additions & 16 deletions paper/src/main/java/com/badbones69/crazycrates/api/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class FileManager {
@NotNull
private final CrazyCrates plugin = CrazyCrates.get();

private final HashMap<Files, File> files = new HashMap<>();
private final Map<Files, File> files = new HashMap<>();
private final List<String> homeFolders = new ArrayList<>();
private final List<CustomFile> customFiles = new ArrayList<>();
private final HashMap<String, String> jarHomeFolders = new HashMap<>();
private final HashMap<String, String> autoGenerateFiles = new HashMap<>();
private final HashMap<Files, FileConfiguration> configurations = new HashMap<>();
private final Map<String, String> jarHomeFolders = new HashMap<>();
private final Map<String, String> autoGenerateFiles = new HashMap<>();
private final Map<Files, FileConfiguration> configurations = new HashMap<>();

/**
* Sets up the plugin and loads all necessary files.
Expand Down Expand Up @@ -91,8 +91,7 @@ public FileManager setup() {
} else {
homeFile.mkdir();

if (this.plugin.isLogging())
this.plugin.getLogger().info("The folder " + homeFolder + "/ was not found so it was created.");
this.plugin.debug(() -> "The folder " + homeFolder + "/ was not found so it was created.", Level.INFO);

for (Map.Entry<String, String> entry : this.autoGenerateFiles.entrySet()) {
String fileName = entry.getKey();
Expand All @@ -108,7 +107,7 @@ public FileManager setup() {
this.customFiles.add(new CustomFile(fileName, homeFolder));
}

if (this.plugin.isLogging()) this.plugin.getLogger().info("Created new default file: " + homeFolder + "/" + fileName + ".");
this.plugin.debug(() -> "Created new default file: " + homeFolder + "/" + fileName + ".", Level.INFO);
} catch (Exception exception) {
this.plugin.getLogger().log(Level.SEVERE, "Failed to create new default file: " + homeFolder + "/" + fileName + "!", exception);
}
Expand Down Expand Up @@ -146,7 +145,7 @@ public FileManager unregisterCustomFilesFolder(String homeFolder) {
/**
* Register a file that needs to be generated when it's home folder doesn't exist. Make sure to have a "/" in front of the home folder's name.
*
* @param fileName the name of the file you want to auto-generate when the folder doesn't exist.
* @param fileName the name of the file you want to auto-generate when the folder doesn't exist.
* @param homeFolder the folder that has custom files in it.
*/
public FileManager registerDefaultGenerateFiles(String fileName, String homeFolder) {
Expand All @@ -157,8 +156,8 @@ public FileManager registerDefaultGenerateFiles(String fileName, String homeFold
/**
* Register a file that needs to be generated when it's home folder doesn't exist. Make sure to have a "/" in front of the home folder's name.
*
* @param fileName the name of the file you want to auto-generate when the folder doesn't exist.
* @param homeFolder the folder that has custom files in it.
* @param fileName the name of the file you want to auto-generate when the folder doesn't exist.
* @param homeFolder the folder that has custom files in it.
* @param jarHomeFolder the folder that the file is found in the jar.
*/
public FileManager registerDefaultGenerateFiles(String fileName, String homeFolder, String jarHomeFolder) {
Expand Down Expand Up @@ -216,7 +215,7 @@ public void removeFile(String name) {
/**
* Add a file by name.
*
* @param name name to use.
* @param name name to use.
* @param folder folder to add to.
*/
public void addFile(String name, String folder) {
Expand Down Expand Up @@ -364,7 +363,7 @@ public enum Files {
/**
* The files that the server will try and load.
*
* @param fileName the file name that will be in the plugin's folder.
* @param fileName the file name that will be in the plugin's folder.
* @param fileLocation the location the file in the plugin's folder.
*/
Files(String fileName, String fileLocation) {
Expand All @@ -374,9 +373,9 @@ public enum Files {
/**
* The files that the server will try and load.
*
* @param fileName the file name that will be in the plugin's folder.
* @param fileName the file name that will be in the plugin's folder.
* @param fileLocation the location of the file will be in the plugin's folder.
* @param fileJar the location of the file in the jar.
* @param fileJar the location of the file in the jar.
*/
Files(String fileName, String fileLocation, String fileJar) {
this.fileName = fileName;
Expand Down Expand Up @@ -431,7 +430,9 @@ public void saveFile() {
* Overrides the loaded state file and loads the file systems file.
*/
public void reloadFile() {
if (this.getFileName().endsWith(".yml")) this.fileManager.reloadFile(this);
if (this.getFileName().endsWith(".yml")) {
this.fileManager.reloadFile(this);
}
}
}

Expand All @@ -448,7 +449,7 @@ public static class CustomFile {
/**
* A custom file that is being made.
*
* @param name name of the file.
* @param name name of the file.
* @param homeFolder the home folder of the file.
*/
public CustomFile(String name, String homeFolder) {
Expand Down Expand Up @@ -510,6 +511,7 @@ public FileConfiguration getFile() {

/**
* Check if the file actually exists in the file system.
*
* @return true if it does and false if it doesn't.
*/
public boolean exists() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.badbones69.crazycrates.api.enums.Messages;
import com.badbones69.crazycrates.api.utils.ItemUtils;
import com.badbones69.crazycrates.api.utils.MiscUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -45,12 +46,12 @@ public int getVirtualKeys(UUID uuid, String crateName) {
@Override
public void addVirtualKeys(int amount, UUID uuid, String crateName) {
if (isPlayerNull(uuid)) {
if (this.plugin.isLogging()) this.plugin.getLogger().warning("Player with the uuid: " + uuid + " is null.");
this.plugin.debug(() -> "Player with the uuid: " + uuid + " is null.", Level.WARNING);
return;
}

if (isCrateInvalid(crateName)) {
if (this.plugin.isLogging()) this.plugin.getLogger().warning("Crate " + crateName + " doesn't exist.");
this.plugin.debug(() -> "Crate " + crateName + " doesn't exist.", Level.WARNING);
return;
}

Expand All @@ -60,7 +61,9 @@ public void addVirtualKeys(int amount, UUID uuid, String crateName) {

int keys = getVirtualKeys(uuid, crate.getName());

if (!this.data.contains("Players." + uuid + ".Name")) this.data.set("Players." + uuid + ".Name", player.getName());
if (!this.data.contains("Players." + uuid + ".Name")) {
this.data.set("Players." + uuid + ".Name", player.getName());
}

this.data.set("Players." + uuid + "." + crate.getName(), (Math.max((keys + amount), 0)));

Expand All @@ -70,12 +73,12 @@ public void addVirtualKeys(int amount, UUID uuid, String crateName) {
@Override
public void setKeys(int amount, UUID uuid, String crateName) {
if (isPlayerNull(uuid)) {
if (this.plugin.isLogging()) this.plugin.getLogger().warning("Player with the uuid: " + uuid + " is null.");
this.plugin.debug(() -> "Player with the uuid: " + uuid + " is null.", Level.WARNING);
return;
}

if (isCrateInvalid(crateName)) {
if (this.plugin.isLogging()) this.plugin.getLogger().warning("Crate " + crateName + " doesn't exist.");
this.plugin.debug(() -> "Crate " + crateName + " doesn't exist.", Level.WARNING);
return;
}

Expand Down Expand Up @@ -154,7 +157,7 @@ public int getPhysicalKeys(UUID uuid, String crateName) {
}

Player player = getUser(uuid);

int keys = 0;

Crate crate = this.plugin.getCrateManager().getCrateFromName(crateName);
Expand Down Expand Up @@ -207,13 +210,15 @@ public boolean takeKeys(int amount, UUID uuid, String crateName, KeyType keyType
if ((takeAmount - keyAmount) >= 0) {
MiscUtils.removeMultipleItemStacks(player.getInventory(), item);

if (crate.getCrateType() == CrateType.cosmic) addOpenedCrate(player.getUniqueId(), amount, crate.getName());
if (crate.getCrateType() == CrateType.cosmic)
addOpenedCrate(player.getUniqueId(), amount, crate.getName());

takeAmount -= keyAmount;
} else {
item.setAmount(keyAmount - takeAmount);

if (crate.getCrateType() == CrateType.cosmic) addOpenedCrate(player.getUniqueId(), amount, crate.getName());
if (crate.getCrateType() == CrateType.cosmic)
addOpenedCrate(player.getUniqueId(), amount, crate.getName());

takeAmount = 0;
}
Expand All @@ -234,11 +239,13 @@ public boolean takeKeys(int amount, UUID uuid, String crateName, KeyType keyType
player.getEquipment().setItemInOffHand(new ItemStack(Material.AIR, 1));
takeAmount -= keyAmount;

if (crate.getCrateType() == CrateType.cosmic) addOpenedCrate(player.getUniqueId(), amount, crate.getName());
if (crate.getCrateType() == CrateType.cosmic)
addOpenedCrate(player.getUniqueId(), amount, crate.getName());
} else {
item.setAmount(keyAmount - takeAmount);

if (crate.getCrateType() == CrateType.cosmic) addOpenedCrate(player.getUniqueId(), amount, crate.getName());
if (crate.getCrateType() == CrateType.cosmic)
addOpenedCrate(player.getUniqueId(), amount, crate.getName());

takeAmount = 0;
}
Expand All @@ -265,7 +272,8 @@ public boolean takeKeys(int amount, UUID uuid, String crateName, KeyType keyType
this.data.set("Players." + uuid + "." + crate.getName(), newAmount);
}

if (crate.getCrateType() == CrateType.cosmic) addOpenedCrate(player.getUniqueId(), amount, crate.getName());
if (crate.getCrateType() == CrateType.cosmic)
addOpenedCrate(player.getUniqueId(), amount, crate.getName());

Files.DATA.saveFile();
return true;
Expand Down Expand Up @@ -320,15 +328,13 @@ public boolean hasPhysicalKey(UUID uuid, String crateName, boolean checkHand) {
/**
* Check if a key is from a specific Crate.
*
* @param item The key item you are checking.
* @param item The key item you are checking.
* @param crate The crate you are checking.
* @return true if it belongs to that crate and false if it does not.
*/
private boolean isKeyFromCrate(ItemStack item, Crate crate) {
if (crate.getCrateType() != CrateType.menu) {
if (item != null && item.getType() != Material.AIR) {
return ItemUtils.isSimilar(item, crate);
}
if (crate.getCrateType() != CrateType.menu && (item != null && item.getType() != Material.AIR)) {
return ItemUtils.isSimilar(item, crate);
}

return false;
Expand All @@ -345,15 +351,17 @@ public boolean addOfflineKeys(UUID uuid, String crateName, int keys, KeyType key

try {
if (keyType == KeyType.physical_key) {
if (this.data.contains("Offline-Players." + uuid + ".Physical." + crate.getName())) keys += this.data.getInt("Offline-Players." + uuid + ".Physical." + crate.getName());
if (this.data.contains("Offline-Players." + uuid + ".Physical." + crate.getName()))
keys += this.data.getInt("Offline-Players." + uuid + ".Physical." + crate.getName());

this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), keys);
Files.DATA.saveFile();

return true;
}

if (this.data.contains("Offline-Players." + uuid + "." + crate.getName())) keys += this.data.getInt("Offline-Players." + uuid + "." + crate.getName());
if (this.data.contains("Offline-Players." + uuid + "." + crate.getName()))
keys += this.data.getInt("Offline-Players." + uuid + "." + crate.getName());

this.data.set("Offline-Players." + uuid + "." + crate.getName(), keys);
Files.DATA.saveFile();
Expand Down Expand Up @@ -386,7 +394,8 @@ public boolean takeOfflineKeys(UUID uuid, String crateName, int keys, KeyType ke
this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), this.data.getInt("Offline-Players." + uuid + ".Physical." + crate.getName()) - keys);

// Remove the data if 0 keys remain after if checks.
if (this.data.getInt("Offline-Players." + uuid + ".Physical." + crate.getName()) <= 0) this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), null);
if (this.data.getInt("Offline-Players." + uuid + ".Physical." + crate.getName()) <= 0)
this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), null);

Files.DATA.saveFile();

Expand Down Expand Up @@ -492,7 +501,7 @@ public void loadOfflinePlayersKeys(Player player, List<Crate> crates) {
while (keysGiven < amount) {
// If the inventory is full, drop the remaining keys then stop.
if (MiscUtils.isInventoryFull(player)) {
player.getWorld().dropItemNaturally(player.getLocation(), crate.getKey(amount-keysGiven));
player.getWorld().dropItemNaturally(player.getLocation(), crate.getKey(amount - keysGiven));
break;
}

Expand All @@ -503,7 +512,8 @@ public void loadOfflinePlayersKeys(Player player, List<Crate> crates) {
player.getInventory().addItem(crate.getKey(keysGiven));

// If keys given is greater or equal than, remove data.
if (keysGiven >= amount) this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), null);
if (keysGiven >= amount)
this.data.set("Offline-Players." + uuid + ".Physical." + crate.getName(), null);
}
}

Expand Down Expand Up @@ -533,7 +543,7 @@ public int getCrateOpened(UUID uuid, String crateName) {
this.plugin.getLogger().warning("Crate " + crateName + " doesn't exist.");
return 0;
}

return this.data.getInt("Players." + uuid + ".tracking." + crateName, 0);
}

Expand Down Expand Up @@ -561,7 +571,7 @@ public void addOpenedCrate(UUID uuid, int amount, String crateName) {
return;
}

this.data.set("Players." + uuid + ".tracking.total-crates", this.data.getInt("Players." + uuid + ".tracking.total-crates", 0)+amount);
this.data.set("Players." + uuid + ".tracking.total-crates", this.data.getInt("Players." + uuid + ".tracking.total-crates", 0) + amount);
this.data.set("Players." + uuid + ".tracking." + crate.getName(), amount);

Files.DATA.saveFile();
Expand Down Expand Up @@ -598,7 +608,7 @@ public void addOpenedCrate(UUID uuid, String crateName) {

Files.DATA.saveFile();
}

private boolean isCrateInvalid(String crateName) {
return crateName.isBlank() || this.plugin.getCrateManager().getCrateFromName(crateName) == null;
}
Expand Down
Loading

0 comments on commit c5821b8

Please sign in to comment.