completed basics for testing blockshop

This commit is contained in:
kai ohara 2025-02-13 19:43:20 -05:00
parent 5422c9cbe3
commit 4cb1e717fb
30 changed files with 100 additions and 92 deletions

View File

@ -1,34 +1,16 @@
# config.yml
shop: shop:
categories: categories:
- name: "Building Blocks" stone_blocks:
items: items:
- material: "STONE" stone:
price: 1 material: STONE
display_name: "Stone"
- material: "WOOD"
price: 2
display_name: "Wood"
- material: "BRICK"
price: 3
display_name: "Brick"
- name: "Decorative Blocks"
items:
- material: "LANTERN"
price: 5
display_name: "Lantern"
- material: "PAINTING"
price: 10 price: 10
display_name: "Painting" display_name: "Stone Block"
- name: "Tools" wood_blocks:
items: items:
- material: "WOODEN_SWORD" oak_planks:
price: 10 material: OAK_PLANKS
display_name: "Wooden Sword" price: 15
- material: "IRON_SWORD" display_name: "Oak Planks"
price: 20
display_name: "Iron Sword"
pagination: pagination:
max_items_per_page: 45 # Max items to display per page max_items_per_page: 45 # Max items to display per page

View File

@ -2,6 +2,7 @@ package CoswayUtil;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.*; import org.bukkit.event.*;
@ -17,7 +18,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
public class BlockShop implements Listener { public class BlockShop implements Listener {
private JavaPlugin plugin; private static JavaPlugin plugin;
private final Economy economy; private final Economy economy;
private static FileConfiguration config; private static FileConfiguration config;
@ -26,6 +27,7 @@ public class BlockShop implements Listener {
public BlockShop(JavaPlugin plugin, Economy economy) { public BlockShop(JavaPlugin plugin, Economy economy) {
this.plugin = plugin; this.plugin = plugin;
this.economy = economy; this.economy = economy;
config = plugin.getConfig(); // Fix: Initialize config properly
} }
// Method to create the special book (or other item) to open the shop // Method to create the special book (or other item) to open the shop
@ -39,18 +41,25 @@ public class BlockShop implements Listener {
return item; return item;
} }
public void ShopGUI(JavaPlugin plugin) {
this.plugin = plugin;
config = plugin.getConfig();
}
public static void openShop(Player player, int page) { public static void openShop(Player player, int page) {
Inventory shopInventory = Bukkit.createInventory(null, 54, "Shop"); reloadShopConfig();
Inventory shopInventory = Bukkit.createInventory(null, 54, ChatColor.GREEN+"Block Shop");
// Get the categories from the config // Get the categories from the config
List<String> categories = config.getStringList("shop.categories"); if (config == null) {
player.sendMessage(ChatColor.RED + "Shop configuration is missing.");
return;
}
ConfigurationSection categoriesSection = config.getConfigurationSection("shop.categories");
ConfigurationSection Conf = config.getConfigurationSection("shop");
if (categoriesSection == null) {
player.sendMessage(ChatColor.RED + "No categories found in the shop.");
player.sendMessage(ChatColor.AQUA + "" + Conf);
return;
}
Set<String> categories = categoriesSection.getKeys(false);
int maxItemsPerPage = config.getInt("pagination.max_items_per_page"); int maxItemsPerPage = config.getInt("pagination.max_items_per_page");
int startIndex = (page - 1) * maxItemsPerPage; int startIndex = (page - 1) * maxItemsPerPage;
int endIndex = startIndex + maxItemsPerPage; int endIndex = startIndex + maxItemsPerPage;
@ -62,8 +71,18 @@ public class BlockShop implements Listener {
// Calculate the correct number of items for pagination // Calculate the correct number of items for pagination
for (int i = startIndex; i < endIndex && i < items.size(); i++) { for (int i = startIndex; i < endIndex && i < items.size(); i++) {
ItemStack item = items.get(i); ItemStack item = items.get(i);
// Get the item key from config based on index
String itemKey = new ArrayList<>(config.getConfigurationSection("shop.categories." + categoryName + ".items").getKeys(false)).get(i);
// Retrieve the quantity from the config
int quantity = config.getInt("shop.categories." + categoryName + ".items." + itemKey + ".quantity", 64); // Default 64 if missing
// Set the correct stack size
item.setAmount(quantity);
shopInventory.addItem(item); shopInventory.addItem(item);
} }
} }
// Show inventory to the player // Show inventory to the player
@ -71,27 +90,32 @@ public class BlockShop implements Listener {
} }
private static List<ItemStack> loadItemsFromCategory(String category) { private static List<ItemStack> loadItemsFromCategory(String category) {
List<ItemStack> items = new ArrayList<>(); List<ItemStack> items = new ArrayList<>(); // Correct variable name
// Load items for the category from the config // Load items for the category from the config
List<String> itemList = config.getStringList("shop.categories." + category + ".items"); ConfigurationSection itemSection = config.getConfigurationSection("shop.categories." + category + ".items");
if (itemSection == null) {
return items;
}
for (String item : itemList) { for (String itemKey : itemSection.getKeys(false)) {
String materialName = config.getString("shop.categories." + category + ".items." + item + ".material"); String materialName = config.getString("shop.categories." + category + ".items." + itemKey + ".material");
Material material = Material.getMaterial(materialName.toUpperCase()); int price = config.getInt("shop.categories." + category + ".items." + itemKey + ".price");
int price = config.getInt("shop.categories." + category + ".items." + item + ".price"); String displayName = config.getString("shop.categories." + category + ".items." + itemKey + ".display_name");
String displayName = config.getString("shop.categories." + category + ".items." + item + ".display_name");
if (material != null) { if (materialName != null) {
ItemStack itemStack = new ItemStack(material); Material material = Material.matchMaterial(materialName.toUpperCase());
ItemMeta meta = itemStack.getItemMeta(); if (material != null) {
if (meta != null) { ItemStack itemStack = new ItemStack(material);
meta.setDisplayName(displayName); ItemMeta meta = itemStack.getItemMeta();
List<String> lore = new ArrayList<>(); if (meta != null) {
lore.add(ChatColor.GOLD + "Price: " + price + " coins"); meta.setDisplayName(ChatColor.GREEN + (displayName != null ? displayName : "Unnamed Item"));
meta.setLore(lore); List<String> lore = new ArrayList<>();
itemStack.setItemMeta(meta); lore.add(ChatColor.GOLD + "Price: $" + price);
items.add(itemStack); meta.setLore(lore);
itemStack.setItemMeta(meta);
items.add(itemStack);
}
} }
} }
} }
@ -99,6 +123,11 @@ public class BlockShop implements Listener {
return items; return items;
} }
public static void reloadShopConfig() {
plugin.reloadConfig(); // Reloads config from disk
config = plugin.getConfig(); // Reassign the updated config
}
// Helper method to add items to the shop // Helper method to add items to the shop
private void addItemToShop(Inventory shop, Material material, String displayName, double price) { private void addItemToShop(Inventory shop, Material material, String displayName, double price) {
ItemStack item = new ItemStack(material); ItemStack item = new ItemStack(material);
@ -129,7 +158,7 @@ public class BlockShop implements Listener {
Player player = (Player) event.getWhoClicked(); Player player = (Player) event.getWhoClicked();
InventoryView clickedInventory = event.getWhoClicked().getOpenInventory(); InventoryView clickedInventory = event.getWhoClicked().getOpenInventory();
if (clickedInventory.getTitle().equals(ChatColor.DARK_GREEN + "Block Shop")) { if (clickedInventory.getTitle().equals(ChatColor.GREEN + "Block Shop")) {
event.setCancelled(true); // Prevent item from being moved event.setCancelled(true); // Prevent item from being moved
ItemStack clickedItem = event.getCurrentItem(); ItemStack clickedItem = event.getCurrentItem();
@ -137,10 +166,20 @@ public class BlockShop implements Listener {
double price = getPrice(clickedItem); double price = getPrice(clickedItem);
if (economy.has(player, price)) { if (economy.has(player, price)) {
economy.withdrawPlayer(player, price); economy.withdrawPlayer(player, price);
player.getInventory().addItem(clickedItem); // Create a new ItemStack with the default meta
player.sendMessage(ChatColor.GREEN + "You bought " + clickedItem.getType() + " for " + price + " currency."); ItemStack sold = new ItemStack(clickedItem.getType(), clickedItem.getAmount());
// Reset item meta (default name, no lore)
ItemMeta defaultMeta = sold.getItemMeta();
if (defaultMeta != null) {
defaultMeta.setDisplayName(null); // Reset to default name
defaultMeta.setLore(null); // Remove lore
sold.setItemMeta(defaultMeta);
}
player.getInventory().addItem(sold);
player.sendMessage(ChatColor.GREEN + "You bought " + sold.getType() + " for $" + price);
} else { } else {
player.sendMessage(ChatColor.RED + "You don't have enough currency to buy this."); player.sendMessage(ChatColor.RED + "You don't have enough money to buy this.");
} }
} }
} }
@ -148,16 +187,21 @@ public class BlockShop implements Listener {
// Helper method to retrieve the price of an item (can be customized to use a config file for prices) // Helper method to retrieve the price of an item (can be customized to use a config file for prices)
private double getPrice(ItemStack item) { private double getPrice(ItemStack item) {
// For simplicity, we assign prices based on material name, you can use a config file for more flexibility for (String category : config.getConfigurationSection("shop.categories").getKeys(false)) {
switch (item.getType()) { ConfigurationSection itemsSection = config.getConfigurationSection("shop.categories." + category + ".items");
case STONE: return 10; if (itemsSection != null) {
case DIRT: return 5; for (String itemKey : itemsSection.getKeys(false)) {
case OAK_PLANKS: return 15; String materialName = config.getString("shop.categories." + category + ".items." + itemKey + ".material");
case BRICKS: return 20; if (materialName != null && item.getType() == Material.matchMaterial(materialName.toUpperCase())) {
default: return 0; return config.getDouble("shop.categories." + category + ".items." + itemKey + ".price", 0);
}
}
}
} }
return 0; // Default to 0 if no price is found
} }
// Register the block shop commands and events // Register the block shop commands and events
public void register() { public void register() {
plugin.getServer().getPluginManager().registerEvents(this, plugin); plugin.getServer().getPluginManager().registerEvents(this, plugin);

View File

@ -1,34 +1,16 @@
# config.yml
shop: shop:
categories: categories:
- name: "Building Blocks" stone_blocks:
items: items:
- material: "STONE" stone:
price: 1 material: STONE
display_name: "Stone"
- material: "WOOD"
price: 2
display_name: "Wood"
- material: "BRICK"
price: 3
display_name: "Brick"
- name: "Decorative Blocks"
items:
- material: "LANTERN"
price: 5
display_name: "Lantern"
- material: "PAINTING"
price: 10 price: 10
display_name: "Painting" display_name: "Stone Block"
- name: "Tools" wood_blocks:
items: items:
- material: "WOODEN_SWORD" oak_planks:
price: 10 material: OAK_PLANKS
display_name: "Wooden Sword" price: 15
- material: "IRON_SWORD" display_name: "Oak Planks"
price: 20
display_name: "Iron Sword"
pagination: pagination:
max_items_per_page: 45 # Max items to display per page max_items_per_page: 45 # Max items to display per page