Skip to content

Commit

Permalink
* Cleaned up code
Browse files Browse the repository at this point in the history
* Added balance getters for EconomyProvision
* Optimized factory configuration builder.
  • Loading branch information
Hempfest committed Mar 6, 2021
1 parent d52e912 commit d2c118e
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 47 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,5 @@ A spigot development tool that makes certain tasks much easier.
*Ships with shaded, unedited LGPLv3 components:*
- *from **VaultAPI** by [Morgan Humes](https://github.com/MilkBowl/) (aka MilkBowl); sources for these components can be found [here](https://github.com/MilkBowl/VaultAPI/).*
- *from **Enterprise** by [Sanctum](https://github.com/the-h-team/); sources for included components can be found [here](https://github.com/the-h-team/Enterprise).*
- *the whole of **MenuMan** by [ms5984](https://github.com/ms5984/); source code can be found [here](https://github.com/ms5984/MenuMan/).*

*You may inspect the [pom.xml](./pom.xml) for further detail on the shading process.*
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.sanctum.Labyrinth</groupId>
<artifactId>Labyrinth</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<packaging>jar</packaging>

<name>Labyrinth</name>
Expand Down
64 changes: 39 additions & 25 deletions src/main/java/com/github/sanctum/labyrinth/Labyrinth.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import com.github.sanctum.labyrinth.data.container.DataContainer;
import com.github.sanctum.labyrinth.gui.GuiLibrary;
import com.github.sanctum.labyrinth.gui.Menu;
import com.github.sanctum.labyrinth.library.Applicable;
import com.github.sanctum.labyrinth.library.Item;
import com.github.sanctum.labyrinth.library.Items;
import com.github.sanctum.labyrinth.library.SkullItem;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.Synchronous;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -40,57 +43,72 @@ public final class Labyrinth extends JavaPlugin implements Listener {
@Override
public void onEnable() {
instance = this;
Bukkit.getServicesManager().register(EconomyProvision.class, new DefaultProvision(), this, ServicePriority.Normal);
run();
Config main = new Config("Config", "Configuration");
if (main.getConfig().getBoolean("use-click-event")) {
Bukkit.getPluginManager().registerEvents(this, this);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> new VaultHook(this), 5);
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> new AdvancedHook(this), 5);
EconomyProvision provision = new DefaultProvision();
Bukkit.getServicesManager().register(EconomyProvision.class, provision, this, ServicePriority.Normal);
getLogger().info("- Registered factory implementation, " + provision.getImplementation());
run(() -> {
Config main = Config.get("Config", "Configuration");
if (!main.exists()) {
InputStream is = getResource("Config.yml");
Config.copy(is, main.getFile());
}
}).applyAfter(() -> {
Config main = Config.get("Config", "Configuration");
if (main.getConfig().getBoolean("use-click-event")) {
Bukkit.getPluginManager().registerEvents(this, this);
}
run(() -> new VaultHook(this)).applyAfter(() -> {
new AdvancedHook(this);
}).run();
}).wait(2);


boolean success;
getLogger().info("▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");
getLogger().info("Labyrinth (C) 2021, Open-source spigot development tool.");
getLogger().info("▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");
try {
DataContainer.querySaved();
success = true;
} catch (NullPointerException e) {
getLogger().info("- Process failed. No directory found to process.");
getLogger().info("- Store a new instance of data for query to take effect on enable.");
getLogger().info("▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");
success = false;
}
if (success) {
if (DataContainer.get().length == 0) {
success = false;
getLogger().info("- Process failed. No data found to process.");
}
getLogger().info("- Query success! All found meta cached. (" + DataContainer.get().length + ")");
} else {
getLogger().info("- Query failed! (SEE ABOVE FOR INFO)");
}
getLogger().info("▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬");

boolean isNew = Arrays.stream(Material.values()).map(Material::name).collect(Collectors.toList()).contains("PLAYER_HEAD");

for (OfflinePlayer p : Bukkit.getOfflinePlayers()) {

Material type = Material.matchMaterial(isNew ? "PLAYER_HEAD" : "SKULL_ITEM");
final boolean isNew = Arrays.stream(Material.values()).map(Material::name).collect(Collectors.toList()).contains("PLAYER_HEAD");
final Material type = Items.getMaterial(isNew ? "PLAYER_HEAD" : "SKULL_ITEM");
Arrays.stream(Bukkit.getOfflinePlayers()).forEach(p -> {
ItemStack item = new ItemStack(type, 1);

if (!isNew) {
item.setDurability((short) 3);
}

SkullMeta meta = (SkullMeta) item.getItemMeta();
if (!meta.hasOwner()) {
meta.setOwningPlayer(p);
}
assert meta != null;
meta.setOwningPlayer(p);
item.setItemMeta(meta);
new SkullItem(p.getUniqueId().toString(), item);
}
});

}

@Override
public void onDisable() {
guiManager.clear();

SkullItem.getLog().clear();
if (Item.getCache().size() > 0) {
for (Item i : Item.getCache()) {
Item.removeEntry(i);
Expand All @@ -117,15 +135,11 @@ public static GuiLibrary guiManager(Player p) {
}
}

private void run() {
Config main = new Config("Config", "Configuration");
if (!main.exists()) {
InputStream is = getResource("Config.yml");
Config.copy(is, main.getFile());
}
private Synchronous run(Applicable applicable) {
return Schedule.sync(applicable);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.NORMAL)
public void onMenuClick(InventoryClickEvent e) {
InventoryHolder holder = e.getInventory().getHolder();
try {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/github/sanctum/labyrinth/data/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ public String getDirectory() {
}

public static Config get(final String n, final String d) {
for(final Config c: Config.configs) {
if(c.getName().equals(n) && c.getDirectory().equals(d)) {
for (final Config c: configs) {
if(c.getName().equals(n) && (c.getDirectory() != null && c.getDirectory().equals(d))) {
return c;
}
}

return new Config(n, d);
}

public void flush() {
configs.removeIf(c -> c.getName().equals(getName()));
}

public boolean delete() {
Config.configs.removeIf(c -> c.getName().equals(getName()));
configs.removeIf(c -> c.getName().equals(getName()));
return this.getFile().delete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import java.math.BigDecimal;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

public class DefaultProvision extends EconomyProvision {

@Override
public String getImplementation() {
return "Default";
boolean isVault = Bukkit.getPluginManager().isPluginEnabled("Vault");
if (Bukkit.getPluginManager().isPluginEnabled("Enterprise")) {
isVault = false;
} else {
if (!isVault) {
return "Default | No Economy Bridge";
}
}
return "Default (Normal) | Interface: " + (isVault ? "Vault" : "Enterprise");
}

@Override
Expand Down Expand Up @@ -40,4 +49,24 @@ public Optional<Boolean> has(BigDecimal amount, OfflinePlayer p) {
public Optional<Boolean> has(BigDecimal amount, OfflinePlayer p, String world) {
return super.has(amount, p, world);
}

@Override
public Optional<Double> balance(OfflinePlayer p) {
return super.balance(p);
}

@Override
public Optional<Double> balance(OfflinePlayer p, String world) {
return super.balance(p, world);
}

@Override
public Optional<Double> accountBalance(OfflinePlayer p) {
return super.accountBalance(p);
}

@Override
public Optional<Double> accountBalance(OfflinePlayer p, String world) {
return super.accountBalance(p, world);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.sanctum.economy.construct.account.permissive.AccountType;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
Expand All @@ -18,6 +19,20 @@ public static EconomyProvision getInstance() {

public abstract String getImplementation();

public Optional<Double> balance(OfflinePlayer p) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getWallet(p).getBalance().doubleValue());
}
return Optional.of(VaultHook.getEconomy().getBalance(p));
}

public Optional<Double> balance(OfflinePlayer p, String world) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getWallet(p).getBalance(world).doubleValue());
}
return Optional.of(VaultHook.getEconomy().getBalance(p, world));
}

public Optional<Boolean> has(BigDecimal amount, OfflinePlayer p) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getWallet(p).has(amount));
Expand Down Expand Up @@ -60,6 +75,20 @@ public Optional<Boolean> withdraw(BigDecimal amount, OfflinePlayer p, String wor
return Optional.of(VaultHook.getEconomy().withdrawPlayer(p, world, amount.doubleValue()).transactionSuccess());
}

public Optional<Double> accountBalance(OfflinePlayer p) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getAccount(p).getBalance().doubleValue());
}
return Optional.of(VaultHook.getEconomy().bankBalance(p.getUniqueId().toString()).amount);
}

public Optional<Double> accountBalance(OfflinePlayer p, String world) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getAccount(p).getBalance(world).doubleValue());
}
return Optional.of(VaultHook.getEconomy().bankBalance(p.getUniqueId().toString()).amount);
}

public Optional<Boolean> accountHas(BigDecimal amount, OfflinePlayer p) {
if (AdvancedHook.getEconomy() != null) {
return Optional.of(AdvancedHook.getEconomy().getAccount(p, AccountType.BANK_ACCOUNT).has(amount));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.io.Serializable;

/**
* Use this functional interface to form lambdas that execute code on run time for you.
* Use this functional interface to form lambdas/references that execute on run time for you.
* Either passing values or running code, this can come in handy and also extends functionality from the Serializable interface.
*/
@FunctionalInterface
public interface Applicable extends Serializable {
/**
* Execute any information applied within the method.
* Execute any information applied within reference.
*/
void apply();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static HUID fromOldString(String wID) {
if (wID.replace("-", "").length() != 6) {
throw new NullPointerException("[hempCore] - Unable to parse HUID");
}
Labyrinth.getInstance().getLogger().warning("- Mocking full size NEW HUID, it is recommended you append an extra 6 characters to all your meta id's equaling 12 total in length");
Labyrinth.getInstance().getLogger().warning("- Mocking full size NEW HUID, this is not guaranteed to work and could cause problems.");
String add = new RandomID(6).generate();
return new HUID(wID.replace("-", "") + add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class SkullItem {
public SkullItem(String holder, ItemStack head) {
this.holder = holder;
this.head = head;
SkullItem.log.add(this);
if (Head.find(UUID.fromString(holder)) == null) {
SkullItem.log.add(this);
}
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/main/resources/Config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#
#
# MMP""MM""YMM `7MMF' `7MMF'`7MM"""YMM .g8"""bgd .g8""8q. `7MM"""Mq. `7MM"""YMM
# P' MM `7 MM MM MM `7 .dP' `M .dP' `YM. MM `MM. MM `7
# MM MM MM MM d dM' ` dM' `MM MM ,M9 MM d
# MM MMmmmmmmMM MMmmMM MM MM MM MMmmdM9 MMmmMM
# MM MM MM MM Y , MM. MM. ,MP MM YM. MM Y ,
# MM MM MM MM ,M `Mb. ,' `Mb. ,dP' MM `Mb. MM ,M
# .JMML. .JMML. .JMML..JMMmmmmMMM `"bmmmd' `"bmmd"' .JMML. .JMM..JMMmmmmMMM
#
# ___ ________ ________ ___ ___ ________ ___ ________ _________ ___ ___
# |\ \ |\ __ \ |\ __ \ |\ \ / /||\ __ \ |\ \ |\ ___ \ |\___ ___\|\ \|\ \
# \ \ \ \ \ \|\ \\ \ \|\ /_ \ \ \/ / /\ \ \|\ \\ \ \\ \ \\ \ \\|___ \ \_|\ \ \\\ \
# \ \ \ \ \ __ \\ \ __ \ \ \ / / \ \ _ _\\ \ \\ \ \\ \ \ \ \ \ \ \ __ \
# \ \ \____ \ \ \ \ \\ \ \|\ \ \/ / / \ \ \\ \|\ \ \\ \ \\ \ \ \ \ \ \ \ \ \ \
# \ \_______\\ \__\ \__\\ \_______\ __/ / / \ \__\\ _\ \ \__\\ \__\\ \__\ \ \__\ \ \__\ \__\
# \|_______| \|__|\|__| \|_______||\___/ / \|__|\|__| \|__| \|__| \|__| \|__| \|__|\|__|
# \|___|/
# ============================================================================================
# Use the default Labyrinth GUI event listener? (Recommended 'true' by default)
# Use the default Labyrinth GUI event listener? (Recommended 'true' by default for MC versions 1.16 and below.)
use-click-event: true
# ============================================================================================
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: Labyrinth
version: 1.3.2
version: 1.3.3
main: com.github.sanctum.labyrinth.Labyrinth
prefix: Labyrinth
api-version: 1.13
Expand Down

0 comments on commit d2c118e

Please sign in to comment.