Skip to content

Commit 0efc966

Browse files
committed
feat(bedwars): WIP quick buy
1 parent 1c862c5 commit 0efc966

18 files changed

Lines changed: 137 additions & 55 deletions

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/gui/GUIItemShop.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public ItemStack.Builder getItem(HypixelPlayer p) {
333333
}
334334

335335
if (shopItem == null) {
336-
return ItemStack.builder(Material.AIR);
336+
return FILLER_ITEM;
337337
}
338338

339339
if (shopItem instanceof UpgradeableShopItem upgradeableShopItem) {

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/ShopItem.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
@Getter
1414
public abstract class ShopItem {
1515

16+
private final String id;
1617
private final String name;
1718
private final String description;
1819
private final Function<BedwarsGameType, Integer> price;
1920
private final int amount;
2021
private final Currency currency;
2122
private final ItemStack display;
2223

23-
public ShopItem(String name, String description, int price, int amount, Currency currency, Material display) {
24+
public ShopItem(String id, String name, String description, int price, int amount, Currency currency, Material display) {
25+
this.id = id;
2426
this.name = name;
2527
this.description = description;
2628
this.price = (_) -> price;
@@ -29,7 +31,8 @@ public ShopItem(String name, String description, int price, int amount, Currency
2931
this.display = ItemStack.of(display);
3032
}
3133

32-
public ShopItem(String name, String description, Function<BedwarsGameType, Integer> price, int amount, Currency currency, Material display) {
34+
public ShopItem(String id, String name, String description, Function<BedwarsGameType, Integer> price, int amount, Currency currency, Material display) {
35+
this.id = id;
3336
this.name = name;
3437
this.description = description;
3538
this.price = price;
@@ -41,7 +44,7 @@ public ShopItem(String name, String description, Function<BedwarsGameType, Integ
4144
/**
4245
* Called when a player purchases an item. This method should be used to give items to the player or apply effects.
4346
*
44-
* @param player the player who purchaseed the item
47+
* @param player the player who purchased the item
4548
*/
4649
public abstract void onPurchase(BedWarsPlayer player);
4750

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/ShopManager.java

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import net.minestom.server.potion.PotionType;
77
import net.swofty.type.bedwarsgame.shop.impl.*;
88
import net.swofty.type.bedwarsgame.user.BedWarsPlayer;
9+
import net.swofty.type.generic.data.datapoints.DatapointStringList;
10+
import net.swofty.type.generic.data.handlers.BedWarsDataHandler;
911

1012
import java.util.ArrayList;
1113
import java.util.HashMap;
@@ -19,32 +21,32 @@ public class ShopManager {
1921

2022
private final ShopItem WOOL = new Wool();
2123
private final ShopItem HARDENED_CLAY = new HardenedClay();
22-
private final ShopItem GLASS = new BasicItem("Blast-Proof Glass", "Immune to explosions.", 12, 4, Currency.IRON, Material.GLASS);
23-
private final ShopItem ENDSTONE = new BasicItem("Endstone", "Solid block to defend your bed. w", 24, 12, Currency.IRON, Material.END_STONE);
24-
private final ShopItem OBSIDIAN = new BasicItem("Obsidian", "Great for defending.", 4, 4, Currency.EMERALD, Material.OBSIDIAN);
25-
private final ShopItem LADDER = new BasicItem("Ladder", "Useful to save cats stuck in trees.", 8, 8, Currency.IRON, Material.LADDER);
26-
private final ShopItem PLANKS = new BasicItem("Wood", "Good block to defend your bed.\nStrong against pickaxes.", 4, 16, Currency.GOLD, Material.OAK_PLANKS);
27-
private final ShopItem STONE_SWORD = new ReplaceAdderItem("Stone Sword", "", 10, Currency.IRON, Material.STONE_SWORD);
28-
private final ShopItem IRON_SWORD = new ReplaceAdderItem("Iron Sword", "", 7, Currency.GOLD, Material.IRON_SWORD);
29-
private final ShopItem DIAMOND_SWORD = new ReplaceAdderItem("Diamond Sword", "", (t) -> t.isDoublesSolo() ? 3 : 4, Currency.EMERALD, Material.DIAMOND_SWORD);
30-
private final ShopItem ENDER_PEARL = new ReplaceAdderItem("Ender Pearl", "The quickest way to invade enemy\nbases.", 2, Currency.EMERALD, Material.ENDER_PEARL);
31-
private final ShopItem TNT = new BasicItem("TNT", "Instantly ignites, appropriate to\nexpode things!", 4, 1, Currency.GOLD, Material.TNT);
32-
private final ShopItem WATER_BUCKET = new BasicItem("Water Bucket", "Great to slow down approaching\nenemies. Can also protect against\nTNT.", 6, 1, Currency.GOLD, Material.WATER_BUCKET);
24+
private final ShopItem GLASS = new BasicItem("glass", "Blast-Proof Glass", "Immune to explosions.", 12, 4, Currency.IRON, Material.GLASS);
25+
private final ShopItem ENDSTONE = new BasicItem("endstone", "Endstone", "Solid block to defend your bed. w", 24, 12, Currency.IRON, Material.END_STONE);
26+
private final ShopItem OBSIDIAN = new BasicItem("obsidian", "Obsidian", "Great for defending.", 4, 4, Currency.EMERALD, Material.OBSIDIAN);
27+
private final ShopItem LADDER = new BasicItem("ladder", "Ladder", "Useful to save cats stuck in trees.", 8, 8, Currency.IRON, Material.LADDER);
28+
private final ShopItem PLANKS = new BasicItem("wood", "Wood", "Good block to defend your bed.\nStrong against pickaxes.", 4, 16, Currency.GOLD, Material.OAK_PLANKS);
29+
private final ShopItem STONE_SWORD = new ReplaceAdderItem("stone_sword", "Stone Sword", "", 10, Currency.IRON, Material.STONE_SWORD);
30+
private final ShopItem IRON_SWORD = new ReplaceAdderItem("iron_sword", "Iron Sword", "", 7, Currency.GOLD, Material.IRON_SWORD);
31+
private final ShopItem DIAMOND_SWORD = new ReplaceAdderItem("diamond_sword", "Diamond Sword", "", (t) -> t.isDoublesSolo() ? 3 : 4, Currency.EMERALD, Material.DIAMOND_SWORD);
32+
private final ShopItem ENDER_PEARL = new ReplaceAdderItem("ender_pearl", "Ender Pearl", "The quickest way to invade enemy\nbases.", 2, Currency.EMERALD, Material.ENDER_PEARL);
33+
private final ShopItem TNT = new BasicItem("tnt", "TNT", "Instantly ignites, appropriate to\nexpode things!", 4, 1, Currency.GOLD, Material.TNT);
34+
private final ShopItem WATER_BUCKET = new BasicItem("water_bucket", "Water Bucket", "Great to slow down approaching\nenemies. Can also protect against\nTNT.", 6, 1, Currency.GOLD, Material.WATER_BUCKET);
3335
private final ShopItem BRIDGE_EGG = new BridgeEggShopItem();
34-
private final ShopItem ARROW = new BasicItem("Arrows", "", 3, 16, Currency.GOLD, Material.ARROW);
35-
private final ShopItem BOW = new BowShopItem("Bow", "", 6, Currency.IRON, EnchantmentList.EMPTY.with(Enchantment.POWER, 1));
36+
private final ShopItem ARROW = new BasicItem("arrow", "Arrows", "", 3, 16, Currency.GOLD, Material.ARROW);
37+
private final ShopItem BOW = new BowShopItem("bow_1", "Bow", "", 6, Currency.IRON, EnchantmentList.EMPTY.with(Enchantment.POWER, 1));
3638
private final ShopItem PICKAXE = new PickaxeShopItem();
3739
private final ShopItem AXE = new AxeShopItem();
3840
private final ShopItem FIREBALL = new FireballShopItem();
3941
private final ShopItem POPUP_TOWER = new PopupTowerItem();
4042
private final ShopItem GOLDEN_APPLE = new GappleShopItem();
41-
private final ShopItem INVISIBILITY_POTION = new PotionShopItem("Invisibility Potion (30 seconds)", "§9Complete invisibility (0:30).", 2, 1, Currency.EMERALD, PotionType.INVISIBILITY);
42-
private final ShopItem SPEED_POTION = new PotionShopItem("Speed II Potion (45 seconds)", "§9Speed II (0:45).", 2, 1, Currency.EMERALD, PotionType.SWIFTNESS);
43-
private final ShopItem JUMP_POTION = new PotionShopItem("Jump V Potion (45 seconds)", "§9Jump Boost V (0:45).", 2, 1, Currency.EMERALD, PotionType.LEAPING);
44-
private final ShopItem CHAINMAIL_ARMOR = new ArmorShopItem("Permanent Chainmail Armor", "", 24, Currency.IRON, Material.CHAINMAIL_BOOTS, Material.CHAINMAIL_LEGGINGS, 1);
45-
private final ShopItem IRON_ARMOR = new ArmorShopItem("Permanent Iron Armor", "", 12, Currency.GOLD, Material.IRON_BOOTS, Material.IRON_LEGGINGS, 2);
46-
private final ShopItem DIAMOND_ARMOR = new ArmorShopItem("Permanent Diamond Armor", "", 6, Currency.EMERALD, Material.DIAMOND_BOOTS, Material.DIAMOND_LEGGINGS, 3);
47-
private final ShopItem SHEARS = new BasicItem("Shears", "Great to get rid of wool. You will\nalways spawn with these shears.", 20, 1, Currency.IRON, Material.SHEARS);
43+
private final ShopItem INVISIBILITY_POTION = new PotionShopItem("invisibility_potion", "Invisibility Potion (30 seconds)", "§9Complete invisibility (0:30).", 2, 1, Currency.EMERALD, PotionType.INVISIBILITY);
44+
private final ShopItem SPEED_POTION = new PotionShopItem("speed_potion", "Speed II Potion (45 seconds)", "§9Speed II (0:45).", 2, 1, Currency.EMERALD, PotionType.SWIFTNESS);
45+
private final ShopItem JUMP_POTION = new PotionShopItem("jump_potion", "Jump V Potion (45 seconds)", "§9Jump Boost V (0:45).", 2, 1, Currency.EMERALD, PotionType.LEAPING);
46+
private final ShopItem CHAINMAIL_ARMOR = new ArmorShopItem("chainmail_armor", "Permanent Chainmail Armor", "", 24, Currency.IRON, Material.CHAINMAIL_BOOTS, Material.CHAINMAIL_LEGGINGS, 1);
47+
private final ShopItem IRON_ARMOR = new ArmorShopItem("iron_armor", "Permanent Iron Armor", "", 12, Currency.GOLD, Material.IRON_BOOTS, Material.IRON_LEGGINGS, 2);
48+
private final ShopItem DIAMOND_ARMOR = new ArmorShopItem("diamond_armor", "Permanent Diamond Armor", "", 6, Currency.EMERALD, Material.DIAMOND_BOOTS, Material.DIAMOND_LEGGINGS, 3);
49+
private final ShopItem SHEARS = new BasicItem("shears", "Shears", "Great to get rid of wool. You will\nalways spawn with these shears.", 20, 1, Currency.IRON, Material.SHEARS);
4850

4951

5052
public ShopManager() {
@@ -96,9 +98,18 @@ public ShopItem getShopItem(int categoryId, int itemIndex) {
9698
return null;
9799
}
98100

99-
// TODO: actual fetching
100-
public Map<Integer, ShopItem> getQuickBuy(BedWarsPlayer player) {
101-
Map<Integer, ShopItem> quickBuy = new HashMap<>();
101+
public ShopItem getShopItemById(String itemId) {
102+
for (List<ShopItem> items : categorizedShopItems.values()) {
103+
for (ShopItem item : items) {
104+
if (item.getId().equals(itemId)) {
105+
return item;
106+
}
107+
}
108+
}
109+
return null;
110+
}
111+
112+
private void populateDefaultQuickBuy(Map<Integer, ShopItem> quickBuy) {
102113
quickBuy.put(0, WOOL);
103114
quickBuy.put(1, STONE_SWORD);
104115
quickBuy.put(2, CHAINMAIL_ARMOR);
@@ -118,8 +129,25 @@ public Map<Integer, ShopItem> getQuickBuy(BedWarsPlayer player) {
118129
quickBuy.put(16, GLASS);
119130
quickBuy.put(17, ENDSTONE);
120131
quickBuy.put(18, AXE);
121-
quickBuy.put(19, WOOL);
122-
quickBuy.put(20, WOOL);
132+
}
133+
134+
public Map<Integer, ShopItem> getQuickBuy(BedWarsPlayer player) {
135+
BedWarsDataHandler dataHandler = BedWarsDataHandler.getUser(player);
136+
List<String> customQuickBuy = dataHandler.get(BedWarsDataHandler.Data.QUICK_BUY, DatapointStringList.class).getValue();
137+
Map<Integer, ShopItem> quickBuy = new HashMap<>();
138+
139+
if (customQuickBuy.isEmpty()) {
140+
populateDefaultQuickBuy(quickBuy);
141+
} else {
142+
for (int i = 0; i < customQuickBuy.size() && i <= 20; i++) {
143+
String itemId = customQuickBuy.get(i);
144+
ShopItem item = getShopItemById(itemId);
145+
if (item != null) {
146+
quickBuy.put(i, item);
147+
}
148+
}
149+
}
150+
123151
return quickBuy;
124152
}
125153

@@ -128,4 +156,56 @@ public ShopItem getQuickShopItem(BedWarsPlayer player, int itemIndex) {
128156
return quickBuy.get(itemIndex);
129157
}
130158

159+
public void setQuickBuyItem(BedWarsPlayer player, int slot, ShopItem item) {
160+
if (slot < 0 || slot > 20) {
161+
throw new IllegalArgumentException("Quick buy slot must be between 0 and 20");
162+
}
163+
if (item == null) {
164+
throw new IllegalArgumentException("Item cannot be null");
165+
}
166+
167+
BedWarsDataHandler dataHandler = BedWarsDataHandler.getUser(player);
168+
DatapointStringList quickBuyData = dataHandler.get(BedWarsDataHandler.Data.QUICK_BUY, DatapointStringList.class);
169+
List<String> quickBuyList = new ArrayList<>(quickBuyData.getValue());
170+
171+
// Ensure the list is large enough
172+
while (quickBuyList.size() <= slot) {
173+
quickBuyList.add("");
174+
}
175+
176+
quickBuyList.set(slot, item.getId());
177+
quickBuyData.setValue(quickBuyList);
178+
}
179+
180+
public void removeQuickBuyItem(BedWarsPlayer player, int slot) {
181+
if (slot < 0 || slot > 20) {
182+
throw new IllegalArgumentException("Quick buy slot must be between 0 and 20");
183+
}
184+
185+
BedWarsDataHandler dataHandler = BedWarsDataHandler.getUser(player);
186+
DatapointStringList quickBuyData = dataHandler.get(BedWarsDataHandler.Data.QUICK_BUY, DatapointStringList.class);
187+
List<String> quickBuyList = new ArrayList<>(quickBuyData.getValue());
188+
189+
if (slot < quickBuyList.size()) {
190+
quickBuyList.set(slot, "");
191+
quickBuyData.setValue(quickBuyList);
192+
}
193+
}
194+
195+
public void resetQuickBuyToDefault(BedWarsPlayer player) {
196+
BedWarsDataHandler dataHandler = BedWarsDataHandler.getUser(player);
197+
DatapointStringList quickBuyData = dataHandler.get(BedWarsDataHandler.Data.QUICK_BUY, DatapointStringList.class);
198+
199+
List<String> defaultItemIds = new ArrayList<>();
200+
Map<Integer, ShopItem> defaultLayout = new HashMap<>();
201+
populateDefaultQuickBuy(defaultLayout);
202+
203+
for (int i = 0; i <= 20; i++) {
204+
ShopItem item = defaultLayout.get(i);
205+
defaultItemIds.add(item != null ? item.getId() : "");
206+
}
207+
208+
quickBuyData.setValue(defaultItemIds);
209+
}
210+
131211
}

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/UpgradeableShopItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public abstract class UpgradeableShopItem extends ShopItem {
1717
private final List<UpgradeableItemTier> tiers;
1818
private final Tag<Integer> upgradeTag;
1919

20-
public UpgradeableShopItem(String name, String description, List<UpgradeableItemTier> tiers, Tag<Integer> upgradeTag) {
20+
public UpgradeableShopItem(String id, String name, String description, List<UpgradeableItemTier> tiers, Tag<Integer> upgradeTag) {
2121
// The initial price and other details are taken from the first tier.
22-
super(name, description, tiers.getFirst().price(), 1, tiers.getFirst().currency(), tiers.getFirst().material());
22+
super(id, name, description, tiers.getFirst().price(), 1, tiers.getFirst().currency(), tiers.getFirst().material());
2323
this.tiers = tiers;
2424
this.upgradeTag = upgradeTag;
2525
}

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/ArmorShopItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class ArmorShopItem extends ShopItem {
1616
private final Material leggings;
1717
private final int armorLevel;
1818

19-
public ArmorShopItem(String name, String description, int price, Currency currency, Material boots, Material leggings, int armorLevel) {
20-
super(name, description, price, 1, currency, boots);
19+
public ArmorShopItem(String id, String name, String description, int price, Currency currency, Material boots, Material leggings, int armorLevel) {
20+
super(id, name, description, price, 1, currency, boots);
2121
this.boots = boots;
2222
this.leggings = leggings;
2323
this.armorLevel = armorLevel;

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/AxeShopItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AxeShopItem extends UpgradeableShopItem {
1313
public static final Tag<Integer> AXE_UPGRADE_TAG = Tag.Integer("axe_upgrade");
1414

1515
public AxeShopItem() {
16-
super("Upgradeable Axe", "This is an upgradable item.\nIt will lose 1 tier upn death!\n\nYou will permanently respawn with at\nleast the lowest tier.",
16+
super("axe", "Upgradeable Axe", "This is an upgradable item.\nIt will lose 1 tier upn death!\n\nYou will permanently respawn with at\nleast the lowest tier.",
1717
List.of(
1818
new UpgradeableItemTier("Wooden Axe (Efficiency I)", _ -> 10, Currency.IRON, Material.WOODEN_AXE),
1919
new UpgradeableItemTier("Stone Axe (Efficiency I)", _ ->10, Currency.IRON, Material.STONE_AXE),

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/BasicItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
public class BasicItem extends ShopItem {
99

10-
public BasicItem(String name, String description, int cost, int amount, Currency currency, Material material) {
11-
super(name, description, cost, amount, currency, material);
10+
public BasicItem(String id, String name, String description, int cost, int amount, Currency currency, Material material) {
11+
super(id, name, description, cost, amount, currency, material);
1212
}
1313

1414
@Override

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/BowShopItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class BowShopItem extends ShopItem {
1212

1313
final EnchantmentList enchantmentList;
1414

15-
public BowShopItem(String name, String description, int price, Currency currency, EnchantmentList enchantmentList) {
16-
super(name, description, price, 1, currency, Material.BOW);
15+
public BowShopItem(String id, String name, String description, int price, Currency currency, EnchantmentList enchantmentList) {
16+
super(id, name, description, price, 1, currency, Material.BOW);
1717
this.enchantmentList = enchantmentList;
1818
}
1919

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/BridgeEggShopItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class BridgeEggShopItem extends ShopItem {
1010

1111
public BridgeEggShopItem() {
12-
super("Bridge Egg", "This egg creates a bridge in its trail\nafter being thrown.", 1, 1, Currency.EMERALD, Material.EGG);
12+
super("bridge_egg", "Bridge Egg", "This egg creates a bridge in its trail\nafter being thrown.", 1, 1, Currency.EMERALD, Material.EGG);
1313
}
1414

1515
@Override

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/shop/impl/FireballShopItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class FireballShopItem extends ShopItem {
1010

1111
public FireballShopItem() {
12-
super("Fireball", "Right-click to launch! Great to knock\nback enemies walking on thin bridges.", 40, 1, Currency.IRON, Material.FIRE_CHARGE);
12+
super("fireball", "Fireball", "Right-click to launch! Great to knock\nback enemies walking on thin bridges.", 40, 1, Currency.IRON, Material.FIRE_CHARGE);
1313
}
1414

1515
@Override

0 commit comments

Comments
 (0)