Skip to content

Commit

Permalink
Implemented Biome Shop (#719)
Browse files Browse the repository at this point in the history
* Implemented Biome Shop

- registered /is biomes command
- added menu option
- added config file
- added plugin messages for biomes & co.
- basically a carbon copy of IridiumTeams shop, localized for IridiumSkyblock

* Changes

* Remove DI

* Early Return Pattern

* Cleanup

* More Cleanup

* Added TabComplete

* Inventory Changes

* Fixed Issues with island permissions

* Changed configs

---------

Co-authored-by: Peaches_MLG <[email protected]>
  • Loading branch information
sh0inx and PeachesMLG authored Sep 23, 2023
1 parent 3a90628 commit e0e0d4c
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class IridiumSkyblock extends IridiumTeams<Island, User> {
private Missions missions;
private Schematics schematics;
private Shop shop;
private Biomes biomes;
private Settings settings;

private IslandPlaceholderBuilder teamsPlaceholderBuilder;
Expand All @@ -61,6 +62,7 @@ public class IridiumSkyblock extends IridiumTeams<Island, User> {
private MissionManager<Island, User> missionManager;
private SchematicManager schematicManager;
private ShopManager<Island, User> shopManager;
private BiomeManager biomeManager;

private Economy economy;

Expand Down Expand Up @@ -97,6 +99,7 @@ public void onEnable() {
this.databaseManager = new DatabaseManager();
this.missionManager = new MissionManager<>(this);
this.shopManager = new ShopManager<>(this);
this.biomeManager = new BiomeManager();
try {
databaseManager.init();
} catch (SQLException exception) {
Expand Down Expand Up @@ -156,6 +159,7 @@ public void loadConfigs() {
this.missions = getPersist().load(Missions.class);
this.schematics = getPersist().load(Schematics.class);
this.shop = getPersist().load(Shop.class);
this.biomes = getPersist().load(Biomes.class);
this.settings = getPersist().load(Settings.class);
super.loadConfigs();

Expand Down Expand Up @@ -189,6 +193,7 @@ public void saveConfigs() {
getPersist().save(missions);
getPersist().save(schematics);
getPersist().save(shop);
getPersist().save(biomes);
getPersist().save(settings);
saveSchematics();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.iridium.iridiumskyblock.commands;

import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.database.Island;
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.BiomeCategoryGUI;
import com.iridium.iridiumskyblock.gui.BiomeOverviewGUI;
import com.iridium.iridiumteams.IridiumTeams;
import com.iridium.iridiumteams.commands.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class BiomeCommand extends Command<Island, User> {

public BiomeCommand() {
super(Collections.singletonList("biomes"), "Refresh your Island Biome", "%prefix% &7/is biomes <biome>", "", 10);
}

@Override
public boolean execute(User user, Island island, String[] args, IridiumTeams<Island, User> iridiumTeams) {
Player player = user.getPlayer();
if (args.length == 0) {
player.openInventory(new BiomeOverviewGUI(player.getOpenInventory().getTopInventory()).getInventory());
return false;
} else {
Optional<String> categoryName = getCategoryName(String.join(" ", args));

if (!categoryName.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noBiomeCategory
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
));
return false;
}

player.openInventory(new BiomeCategoryGUI(categoryName.get(), player.getOpenInventory().getTopInventory()).getInventory());
return false;
}
}

private Optional<String> getCategoryName(String name) {
for (String category : IridiumSkyblock.getInstance().getBiomes().categories.keySet()) {
if (category.equalsIgnoreCase(name)) {
return Optional.of(category);
}
}
return Optional.empty();
}

@Override
public List<String> onTabComplete(CommandSender commandSender, String[] args, IridiumTeams<Island, User> iridiumTeams) {
return new ArrayList<>(IridiumSkyblock.getInstance().getBiomes().categories.keySet());
}
}
155 changes: 155 additions & 0 deletions src/main/java/com/iridium/iridiumskyblock/configs/Biomes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.iridium.iridiumskyblock.configs;

import com.google.common.collect.ImmutableMap;
import com.iridium.iridiumcore.Item;
import com.iridium.iridiumcore.dependencies.fasterxml.annotation.JsonIgnore;
import com.iridium.iridiumcore.dependencies.xseries.XBiome;
import com.iridium.iridiumcore.dependencies.xseries.XMaterial;
import com.iridium.iridiumcore.dependencies.xseries.XSound;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.*;

public class Biomes {

public Map<String, BiomeCategory> categories = ImmutableMap.<String, BiomeCategory>builder()
.put("Overworld", new BiomeCategory(new Item(XMaterial.GRASS_BLOCK, 11, 1, "&9&lOverworld", Collections.emptyList()), 27))
.put("Nether", new BiomeCategory(new Item(XMaterial.CRIMSON_NYLIUM, 13, 1, "&9&lNether", Collections.emptyList()), 27))
.put("End", new BiomeCategory(new Item(XMaterial.END_STONE, 15, 1, "&9&lEnd", Collections.emptyList()), 27))
.build();
public Map<String, List<BiomeItem>> items = ImmutableMap.<String, List<BiomeItem>>builder()
.put("Overworld", Arrays.asList(
new BiomeItem(
"&9&lPlains",
XMaterial.GRASS_BLOCK,
XBiome.PLAINS,
1,
11,
new Cost(100, new HashMap<>())
),
new BiomeItem(
"&9&lSnowy Plains",
XMaterial.SNOW_BLOCK,
XBiome.SNOWY_PLAINS,
1,
13,
new Cost(50, new HashMap<>())
),
new BiomeItem(
"&9&lSavanna",
XMaterial.TALL_GRASS,
XBiome.SAVANNA,
1,
15,
new Cost(100, new HashMap<>())
)
))
.put("Nether", Arrays.asList(
new BiomeItem(
"&9&lNether Wastes",
XMaterial.NETHERRACK,
XBiome.NETHER_WASTES,
1,
11,
new Cost(50, new HashMap<>())
),
new BiomeItem(
"&9&lCrimson Forest",
XMaterial.CRIMSON_NYLIUM,
XBiome.CRIMSON_FOREST,
1,
13,
new Cost(1000, new HashMap<>())
),
new BiomeItem(
"&9&lWarped Forest",
XMaterial.WARPED_NYLIUM,
XBiome.WARPED_FOREST,
1,
15,
new Cost(100, new HashMap<>())
)
)
)
.put("End", Arrays.asList(
new BiomeItem(
"&9&lTHE END",
XMaterial.END_STONE,
XBiome.THE_END,
1,
11,
new Cost(100, new HashMap<>())
),
new BiomeItem(
"&9&lEnd Highlands",
XMaterial.PURPUR_BLOCK,
XBiome.END_HIGHLANDS,
1,
13,
new Cost(150, new HashMap<>())
),
new BiomeItem(
"&9&lEnd Barrens",
XMaterial.ENDER_EYE,
XBiome.END_BARRENS,
1,
15,
new Cost(150, new HashMap<>())
)
)
)
.build();
public String buyPriceLore = "&aBuy Price: $%vault_cost%";
public String notPurchasableLore = "&cThis item cannot be purchased!";
public boolean abbreviatePrices = true;
public XSound failSound = XSound.BLOCK_ANVIL_LAND;
public XSound successSound = XSound.ENTITY_PLAYER_LEVELUP;
public List<String> biomeItemLore = Arrays.asList(
"&9&l[!] " + "&9Left-Click to Purchase"
);

@NoArgsConstructor
@AllArgsConstructor
public static class BiomeCategory {
public Item item;
public int inventorySize;
}

@NoArgsConstructor
@AllArgsConstructor
public static class BiomeItem {
public String name;
public XMaterial type;
public XBiome biome;
public List<String> lore;
public int defaultAmount;
public int slot;
public int page;
public Cost buyCost;

public BiomeItem(String name, XMaterial type, XBiome biome, int defaultAmount, int slot, Cost buyCost) {
this.name = name;
this.type = type;
this.biome = biome;
this.lore = Collections.emptyList();
this.defaultAmount = defaultAmount;
this.slot = slot;
this.page = 1;
this.buyCost = buyCost;
}
}

@AllArgsConstructor
@NoArgsConstructor
public static class Cost {
public double money;
public Map<String, Double> bankItems;

@JsonIgnore
public boolean canPurchase() {
return money > 0 || bankItems.values().stream().anyMatch(value -> value > 0);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iridium.iridiumskyblock.configs;

import com.iridium.iridiumskyblock.commands.BiomeCommand;
import com.iridium.iridiumskyblock.commands.BorderCommand;
import com.iridium.iridiumskyblock.commands.RegenCommand;
import com.iridium.iridiumskyblock.commands.VisitCommand;
Expand All @@ -14,4 +15,5 @@ public Commands() {
public VisitCommand visitCommand = new VisitCommand();
public BorderCommand borderCommand = new BorderCommand();
public RegenCommand regenCommand = new RegenCommand();
public BiomeCommand biomeCommand = new BiomeCommand();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class Inventories extends com.iridium.iridiumteams.configs.Inventories {
)));

public NoItemGUI islandSchematicGUI = new NoItemGUI(27, "&7Select a Schematic", background2);
public NoItemGUI biomeOverviewGUI = new NoItemGUI(27, "Biomes", background2);
public NoItemGUI biomeCategoryGUI = new NoItemGUI(54, "Biomes - %biome_category_name%", background1);

public Inventories() {
super("Island", "&9");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public Messages() {
public String noSafeLocation = "%prefix% &7Could not find a safe location to teleport to.";
public String cannotHurtPlayers = "%prefix% &7You cannot hurt players on your Island.";
public String creatingIsland = "%prefix% &7Creating Island, please wait...";
public String noBiomeCategory = "%prefix% &7No biome category with that name.";
public String noBiome = "%prefix% &7No biome with that name.";
public String changedBiome = "%prefix% &7%player% successfully changed your Island biome to %biome%.";
}
107 changes: 107 additions & 0 deletions src/main/java/com/iridium/iridiumskyblock/gui/BiomeCategoryGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.iridium.iridiumskyblock.gui;

import com.iridium.iridiumcore.gui.BackGUI;
import com.iridium.iridiumcore.utils.Placeholder;
import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.configs.Biomes;
import com.iridium.iridiumteams.configs.inventories.NoItemGUI;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class BiomeCategoryGUI extends BackGUI {

@Getter
private final String categoryName;
private final Biomes.BiomeCategory biomeCategory;

public BiomeCategoryGUI(String categoryName, Inventory previousInventory) {
super(IridiumSkyblock.getInstance().getInventories().biomeCategoryGUI.background, previousInventory, IridiumSkyblock.getInstance().getInventories().backButton);
this.categoryName = categoryName;
this.biomeCategory = IridiumSkyblock.getInstance().getBiomes().categories.get(categoryName);
}

@NotNull
@Override
public Inventory getInventory() {
NoItemGUI biomeCategoryGUI = IridiumSkyblock.getInstance().getInventories().biomeCategoryGUI;
Inventory inventory = Bukkit.createInventory(this, biomeCategory.inventorySize, StringUtils.color(biomeCategoryGUI.title.replace("%biome_category_name%", categoryName)));
addContent(inventory);
return inventory;
}

@Override
public void addContent(Inventory inventory) {
super.addContent(inventory);

if (!IridiumSkyblock.getInstance().getBiomes().items.containsKey(categoryName)) {
IridiumSkyblock.getInstance().getLogger().warning("Biome Category " + categoryName + " Is not configured with any items!");
return;
}
for (Biomes.BiomeItem biomeItem : IridiumSkyblock.getInstance().getBiomes().items.get(categoryName)) {
ItemStack itemStack = biomeItem.type.parseItem();
ItemMeta itemMeta = itemStack.getItemMeta();

itemStack.setAmount(biomeItem.defaultAmount);
itemMeta.setDisplayName(StringUtils.color(biomeItem.name));
itemMeta.setLore(getBiomeLore(biomeItem));

itemStack.setItemMeta(itemMeta);
inventory.setItem(biomeItem.slot, itemStack);
}
}

private List<Placeholder> getBiomeLorePlaceholders(Biomes.BiomeItem item) {
List<Placeholder> placeholders = new ArrayList<>(Arrays.asList(
new Placeholder("amount", IridiumSkyblock.getInstance().getBiomeManager().formatPrice(item.defaultAmount)),
new Placeholder("vault_cost", IridiumSkyblock.getInstance().getBiomeManager().formatPrice(item.buyCost.money))
));
for (Map.Entry<String, Double> bankItem : item.buyCost.bankItems.entrySet()) {
placeholders.add(new Placeholder(bankItem.getKey() + "_cost", IridiumSkyblock.getInstance().getBiomeManager().formatPrice(bankItem.getValue())));
}
return placeholders;
}

private List<String> getBiomeLore(Biomes.BiomeItem item) {
List<String> lore = item.lore == null ? new ArrayList<>() : new ArrayList<>(StringUtils.color(item.lore));
List<Placeholder> placeholders = getBiomeLorePlaceholders(item);

if (item.buyCost.canPurchase()) {
lore.add(IridiumSkyblock.getInstance().getBiomes().buyPriceLore);
} else {
lore.add(IridiumSkyblock.getInstance().getBiomes().notPurchasableLore);
}

lore.addAll(IridiumSkyblock.getInstance().getBiomes().biomeItemLore);

return StringUtils.color(StringUtils.processMultiplePlaceholders(lore, placeholders));
}

@Override
public void onInventoryClick(InventoryClickEvent event) {
super.onInventoryClick(event);
Optional<Biomes.BiomeItem> biomeItem = IridiumSkyblock.getInstance().getBiomes().items.get(categoryName).stream()
.filter(item -> item.slot == event.getSlot())
.findAny();

if (!biomeItem.isPresent()) {
return;
}

Player player = (Player) event.getWhoClicked();
if (event.isLeftClick() && biomeItem.get().buyCost.canPurchase()) {
IridiumSkyblock.getInstance().getBiomeManager().buy(player, biomeItem.get());
player.closeInventory();
} else {
IridiumSkyblock.getInstance().getBiomes().failSound.play(player);
}
}
}
Loading

0 comments on commit e0e0d4c

Please sign in to comment.