Skip to content

Commit 5cfd483

Browse files
committed
Add virtual currency.
1 parent 25d077f commit 5cfd483

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+813
-71
lines changed

assembly/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
<pattern>org.apache.commons.codec</pattern>
5757
<shadedPattern>net.craftingstore.libraries.apache.commons.codec</shadedPattern>
5858
</relocation>
59+
<relocation>
60+
<pattern>org.apache.commons.text</pattern>
61+
<shadedPattern>net.craftingstore.libraries.apache.commons.text</shadedPattern>
62+
</relocation>
5963
<relocation>
6064
<pattern>org.json</pattern>
6165
<shadedPattern>net.craftingstore.libraries.json</shadedPattern>

bukkit/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<url>https://repo.codemc.org/repository/maven-public</url>
5050
</repository>
5151
<!-- PlaceholderRAPI end -->
52+
<repository>
53+
<id>jitpack.io</id>
54+
<url>https://jitpack.io</url>
55+
</repository>
5256
</repositories>
5357

5458
<dependencies>
@@ -80,5 +84,11 @@
8084
<artifactId>XSeries</artifactId>
8185
<version>7.9.1.1</version>
8286
</dependency>
87+
<dependency>
88+
<groupId>com.github.MilkBowl</groupId>
89+
<artifactId>VaultAPI</artifactId>
90+
<version>1.7</version>
91+
<scope>provided</scope>
92+
</dependency>
8393
</dependencies>
8494
</project>

bukkit/src/main/java/net/craftingstore/bukkit/BukkitPluginConfiguration.java

+5
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public boolean isBuyCommandEnabled() {
3939
public int getTimeBetweenCommands() {
4040
return plugin.getConfig().getInt("time-between-commands", 200);
4141
}
42+
43+
@Override
44+
public String getNotEnoughBalanceMessage() {
45+
return plugin.getConfig().getString("not-enough-balance-message", "&4You do not have enough in-game money in your account! You can also buy this package on the website:");
46+
}
4247
}

bukkit/src/main/java/net/craftingstore/bukkit/CraftingStoreBukkit.java

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.craftingstore.bukkit.commands.CraftingStoreCommand;
55
import net.craftingstore.bukkit.config.Config;
66
import net.craftingstore.bukkit.hooks.PlaceholderAPIHook;
7+
import net.craftingstore.bukkit.hooks.VaultHook;
78
import net.craftingstore.bukkit.listeners.InventoryListener;
89
import net.craftingstore.bukkit.listeners.AdminJoinListener;
910
import net.craftingstore.bukkit.listeners.PendingDonationJoinListener;
@@ -18,6 +19,7 @@ public class CraftingStoreBukkit extends JavaPlugin {
1819
private CraftingStore craftingStore;
1920
private Config config;
2021
private String prefix = ChatColor.GRAY + "[" + ChatColor.RED + "CraftingStore" + ChatColor.GRAY + "] " + ChatColor.WHITE;
22+
private VaultHook vaultHook;
2123

2224
@Override
2325
public void onEnable() {
@@ -38,6 +40,16 @@ public void onEnable() {
3840
new PlaceholderAPIHook(craftingStore);
3941
craftingStore.getLogger().info("Hooked with PlaceholderAPI");
4042
}
43+
// Vault hook
44+
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
45+
this.vaultHook = new VaultHook(this);
46+
if (this.vaultHook.register()) {
47+
craftingStore.getLogger().info("Hooked with Vault");
48+
} else {
49+
craftingStore.getLogger().info("There was a problem hooking with Vault");
50+
this.vaultHook = null;
51+
}
52+
}
4153
}
4254

4355
@Override
@@ -62,4 +74,12 @@ public Config getConfigWrapper() {
6274
public String getPrefix() {
6375
return prefix;
6476
}
77+
78+
public VaultHook getVaultHook() {
79+
return vaultHook;
80+
}
81+
82+
public boolean isHookedWithVault() {
83+
return vaultHook != null;
84+
}
6585
}

bukkit/src/main/java/net/craftingstore/bukkit/commands/BuyCommand.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.craftingstore.bukkit.CraftingStoreBukkit;
44
import net.craftingstore.bukkit.inventory.InventoryBuilder;
55
import net.craftingstore.core.exceptions.CraftingStoreApiException;
6+
import net.craftingstore.core.models.api.ApiInventory;
67
import net.craftingstore.core.models.api.inventory.CraftingStoreInventory;
78
import org.bukkit.Bukkit;
89
import org.bukkit.command.Command;
@@ -34,8 +35,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
3435
Bukkit.getScheduler().runTaskAsynchronously(instance, () -> {
3536
try {
3637
InventoryBuilder builder = new InventoryBuilder(this.instance);
37-
CraftingStoreInventory gui = instance.getCraftingStore().getApi().getGUI().get();
38-
Inventory inventory = builder.buildInventory(gui);
38+
ApiInventory gui = instance.getCraftingStore().getApi().getGUI().get();
39+
Inventory inventory = builder.buildInventory(
40+
new CraftingStoreInventory(gui.getTitle(), gui.getContent(), gui.getSize())
41+
);
3942
Bukkit.getScheduler().runTask(instance, () -> {
4043
if (p.isOnline()) {
4144
p.openInventory(inventory);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.craftingstore.bukkit.hooks;
2+
3+
import net.craftingstore.bukkit.CraftingStoreBukkit;
4+
import net.milkbowl.vault.economy.Economy;
5+
import org.bukkit.plugin.RegisteredServiceProvider;
6+
7+
public class VaultHook {
8+
private final CraftingStoreBukkit instance;
9+
private Economy economy;
10+
11+
public VaultHook(CraftingStoreBukkit instance) {
12+
this.instance = instance;
13+
}
14+
15+
public boolean register() {
16+
RegisteredServiceProvider<Economy> rsp = instance.getServer().getServicesManager().getRegistration(Economy.class);
17+
if (rsp == null) {
18+
return false;
19+
}
20+
this.economy = rsp.getProvider();
21+
return true;
22+
}
23+
24+
public Economy getEconomy() {
25+
return economy;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package net.craftingstore.bukkit.inventory;
2+
3+
import net.craftingstore.bukkit.CraftingStoreBukkit;
4+
import net.craftingstore.bukkit.util.ChatColorUtil;
5+
import net.craftingstore.core.exceptions.CraftingStoreApiException;
6+
import net.craftingstore.core.models.api.ApiInventory;
7+
import net.craftingstore.core.models.api.ApiPackageInformation;
8+
import net.craftingstore.core.models.api.inventory.CraftingStoreInventory;
9+
import net.craftingstore.core.models.api.inventory.InventoryItem;
10+
import net.craftingstore.core.models.api.inventory.InventoryItemIcon;
11+
import net.craftingstore.core.models.api.inventory.InventoryItemType;
12+
import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage;
13+
import org.bukkit.entity.Player;
14+
import org.bukkit.inventory.Inventory;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.concurrent.ExecutionException;
19+
20+
public class BuyInventoryBuilder {
21+
22+
private final CraftingStoreBukkit instance;
23+
private final InventoryBuilder inventoryBuilder;
24+
25+
public BuyInventoryBuilder(CraftingStoreBukkit instance, InventoryBuilder inventoryBuilder) {
26+
this.instance = instance;
27+
this.inventoryBuilder = inventoryBuilder;
28+
}
29+
30+
public Inventory build(Player p, InventoryItemBuyablePackage item, CraftingStoreInventoryHolder holder) throws CraftingStoreApiException, ExecutionException, InterruptedException {
31+
ApiInventory buyMenu = this.instance.getCraftingStore().getApi().getGUI().get().getBuyMenu();
32+
CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory(buyMenu.getTitle(), buyMenu.getContent(), buyMenu.getSize());
33+
34+
String ip = p.getAddress().getAddress().getHostAddress();
35+
ApiPackageInformation packageInformation = this.instance.getCraftingStore().getApi().getPackageInformation(
36+
p.getName(),
37+
p.getUniqueId(),
38+
ip,
39+
item.getPackageId()
40+
).get();
41+
if (!packageInformation.isAllowedToBuy()) {
42+
p.sendMessage(ChatColorUtil.translate(packageInformation.getMessage()));
43+
p.closeInventory();
44+
return null;
45+
}
46+
if (packageInformation.getPrice() != item.getPrice()) {
47+
p.sendMessage(this.instance.getPrefix() + "There was a problem with the payment data. Please try again later.");
48+
p.closeInventory();
49+
return null;
50+
}
51+
52+
Map placeholders = new HashMap<>();
53+
placeholders.put("package_name", item.getName());
54+
placeholders.put("package_price_ingame", packageInformation.getPrice());
55+
return this.inventoryBuilder.buildInventory(
56+
craftingStoreInventory,
57+
new BuyMenuInventoryHolder(craftingStoreInventory, holder, item),
58+
placeholders
59+
);
60+
}
61+
62+
public Inventory buildLoadInventory() {
63+
InventoryItemIcon icon = new InventoryItemIcon("LIGHT_BLUE_STAINED_GLASS_PANE", 1, 0, null);
64+
InventoryItem[] content = new InventoryItem[9];
65+
for (int i = 0; i < 9; i++) {
66+
content[i] = new InventoryItem("Loading...", null, InventoryItemType.NULL, icon, i);
67+
}
68+
69+
CraftingStoreInventory craftingStoreInventory = new CraftingStoreInventory(
70+
"Loading...",
71+
content,
72+
9
73+
);
74+
return this.inventoryBuilder.buildInventory(
75+
craftingStoreInventory,
76+
new CraftingStoreInventoryHolder(craftingStoreInventory, null)
77+
);
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.craftingstore.bukkit.inventory;
2+
3+
import net.craftingstore.core.models.api.inventory.CraftingStoreInventory;
4+
import net.craftingstore.core.models.api.inventory.types.InventoryItemBuyablePackage;
5+
6+
public class BuyMenuInventoryHolder extends CraftingStoreInventoryHolder {
7+
8+
private final InventoryItemBuyablePackage itemBuyablePackage;
9+
10+
public BuyMenuInventoryHolder(
11+
CraftingStoreInventory csInventory,
12+
CraftingStoreInventoryHolder parentInventory,
13+
InventoryItemBuyablePackage itemBuyablePackage
14+
) {
15+
super(csInventory, parentInventory);
16+
this.itemBuyablePackage = itemBuyablePackage;
17+
}
18+
19+
public InventoryItemBuyablePackage getItemBuyablePackage() {
20+
return itemBuyablePackage;
21+
}
22+
}

bukkit/src/main/java/net/craftingstore/bukkit/inventory/InventoryBuilder.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
import net.craftingstore.bukkit.util.VersionUtil;
66
import net.craftingstore.core.models.api.inventory.CraftingStoreInventory;
77
import net.craftingstore.core.models.api.inventory.InventoryItem;
8+
import org.apache.commons.text.StringSubstitutor;
89
import org.bukkit.Bukkit;
910
import org.bukkit.inventory.Inventory;
1011
import org.bukkit.inventory.ItemStack;
1112
import org.bukkit.inventory.meta.ItemMeta;
1213

1314
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.Map;
1417
import java.util.stream.Collectors;
1518

1619
public class InventoryBuilder {
@@ -24,24 +27,31 @@ public InventoryBuilder(CraftingStoreBukkit instance) {
2427
}
2528

2629
public Inventory buildInventory(CraftingStoreInventory csInventory) {
27-
return buildInventory(csInventory, null);
30+
return buildInventory(csInventory, new CraftingStoreInventoryHolder(csInventory, null));
2831
}
2932

30-
public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder parent) {
33+
public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder holder) {
34+
return buildInventory(csInventory, holder, new HashMap<>());
35+
}
36+
37+
public Inventory buildInventory(CraftingStoreInventory csInventory, CraftingStoreInventoryHolder holder, Map<String, ?> placeholders) {
3138
String title = csInventory.getTitle();
3239
if (title == null || title.isEmpty()) {
3340
title = "CraftingStore";
3441
}
35-
title = ChatColorUtil.translate(title);
36-
Inventory inventory = Bukkit.createInventory(new CraftingStoreInventoryHolder(csInventory, parent), csInventory.getSize(), title);
42+
43+
StringSubstitutor stringSubstitutor = new StringSubstitutor(placeholders, "{", "}");
44+
title = ChatColorUtil.translate(stringSubstitutor.replace(title));
45+
Inventory inventory = Bukkit.createInventory(holder, csInventory.getSize(), title);
3746

3847
for (InventoryItem inventoryItem : csInventory.getContent()) {
3948
ItemStack itemStack = this.inventoryItemBuilder.getItemStack(inventoryItem.getIcon().getMaterial(), inventoryItem.getIcon().getAmount());
4049
ItemMeta meta = itemStack.getItemMeta();
4150
if (meta != null) {
42-
meta.setDisplayName(ChatColorUtil.translate(inventoryItem.getName()));
51+
meta.setDisplayName(ChatColorUtil.translate(stringSubstitutor.replace(inventoryItem.getName())));
4352
if (inventoryItem.getDescription() != null && inventoryItem.getDescription().length != 0) {
4453
meta.setLore(Arrays.stream(inventoryItem.getDescription())
54+
.map(stringSubstitutor::replace)
4555
.map(ChatColorUtil::translate)
4656
.collect(Collectors.toList())
4757
);

bukkit/src/main/java/net/craftingstore/bukkit/inventory/handlers/BackButtonHandler.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public BackButtonHandler(InventoryBuilder builder) {
1818
@Override
1919
public void handle(Player p, InventoryItemBackButton item, CraftingStoreInventoryHolder holder) {
2020
if (holder.getParentInventory() != null) {
21-
Inventory inventory = builder.buildInventory(holder.getParentInventory().getCsInventory(), holder.getParentInventory().getParentInventory());
21+
CraftingStoreInventoryHolder parentInventory = holder.getParentInventory();
22+
Inventory inventory = builder.buildInventory(
23+
parentInventory.getCsInventory(),
24+
new CraftingStoreInventoryHolder(parentInventory.getCsInventory(), parentInventory.getParentInventory())
25+
);
2226
p.openInventory(inventory);
2327
}
2428
}

0 commit comments

Comments
 (0)