Skip to content

Commit

Permalink
convert last menu to inventory holders
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Mar 12, 2024
1 parent 7c30648 commit 6948ec4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 52 deletions.
Expand Up @@ -46,7 +46,6 @@ public class CrazyEnchantments extends JavaPlugin {
public final PluginManager pluginManager = getServer().getPluginManager();

private FireworkDamageListener fireworkDamageListener;
private ShopListener shopListener;
private ArmorEnchantments armorEnchantments;

private final BossBarController bossBarController = new BossBarController(this);
Expand Down Expand Up @@ -82,7 +81,7 @@ public void onEnable() {
if (config.getBoolean("Settings.Toggle-Metrics")) new Metrics(this, 4494);

this.pluginManager.registerEvents(this.fireworkDamageListener = new FireworkDamageListener(), this);
this.pluginManager.registerEvents(this.shopListener = new ShopListener(), this);
this.pluginManager.registerEvents(new ShopListener(), this);

// Load what we need to properly enable the plugin.
this.starter.getCrazyManager().load();
Expand Down Expand Up @@ -156,10 +155,6 @@ public FireworkDamageListener getFireworkDamageListener() {
return this.fireworkDamageListener;
}

public ShopListener getShopListener() {
return this.shopListener;
}

public PluginManager getPluginManager() {
return this.pluginManager;
}
Expand Down
@@ -0,0 +1,45 @@
package com.badbones69.crazyenchantments.paper.api.builders.types;

import com.badbones69.crazyenchantments.paper.Starter;
import com.badbones69.crazyenchantments.paper.api.builders.InventoryBuilder;
import com.badbones69.crazyenchantments.paper.api.economy.Currency;
import com.badbones69.crazyenchantments.paper.api.economy.CurrencyAPI;
import com.badbones69.crazyenchantments.paper.api.managers.ShopManager;
import com.badbones69.crazyenchantments.paper.api.objects.other.ItemBuilder;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;

public class ShopMenu extends InventoryBuilder {

@NotNull
private final Starter starter = this.plugin.getStarter();

@NotNull
private final CurrencyAPI currencyAPI = this.starter.getCurrencyAPI();

private final ShopManager shopManager = this.starter.getShopManager();

public ShopMenu(Player player, int size, String title) {
super(player, size, title);
}

@Override
public InventoryBuilder build() {
HashMap<String, String> placeholders = new HashMap<>();

for (Currency currency : Currency.values()) {
placeholders.put("%" + currency.getName() + "%", String.valueOf(this.currencyAPI.getCurrency(getPlayer(), currency)));
}

for (Map.Entry<ItemBuilder, Integer> itemBuilders : this.shopManager.getCustomizerItems().entrySet()) {
itemBuilders.getKey().setNamePlaceholders(placeholders).setLorePlaceholders(placeholders);
getInventory().setItem(itemBuilders.getValue(), itemBuilders.getKey().build());
}

this.shopManager.getShopItems().keySet().forEach(itemBuilder -> getInventory().setItem(this.shopManager.getShopItems().get(itemBuilder), itemBuilder.build()));

return this;
}
}
Expand Up @@ -3,21 +3,18 @@
import com.badbones69.crazyenchantments.paper.CrazyEnchantments;
import com.badbones69.crazyenchantments.paper.Starter;
import com.badbones69.crazyenchantments.paper.api.FileManager.Files;
import com.badbones69.crazyenchantments.paper.api.economy.Currency;
import com.badbones69.crazyenchantments.paper.api.economy.CurrencyAPI;
import com.badbones69.crazyenchantments.paper.api.enums.ShopOption;
import com.badbones69.crazyenchantments.paper.api.objects.Category;
import com.badbones69.crazyenchantments.paper.api.objects.LostBook;
import com.badbones69.crazyenchantments.paper.api.objects.other.ItemBuilder;
import com.badbones69.crazyenchantments.paper.api.utils.ColorUtils;
import com.badbones69.crazyenchantments.paper.controllers.settings.EnchantmentBookSettings;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Map;

//todo() redo this
public class ShopManager {
Expand All @@ -28,17 +25,14 @@ public class ShopManager {
@NotNull
private final Starter starter = this.plugin.getStarter();

@NotNull
private final CurrencyAPI currencyAPI = this.starter.getCurrencyAPI();

@NotNull
private final EnchantmentBookSettings enchantmentBookSettings = this.starter.getEnchantmentBookSettings();

private String inventoryName;
private int inventorySize;
private boolean enchantmentTableShop;
private final HashMap<ItemBuilder, Integer> customizerItems = new HashMap<>();
private final HashMap<ItemBuilder, Integer> shopItems = new HashMap<>();
private final Map<ItemBuilder, Integer> customizerItems = new HashMap<>();
private final Map<ItemBuilder, Integer> shopItems = new HashMap<>();

public void load() {
this.customizerItems.clear();
Expand Down Expand Up @@ -89,27 +83,15 @@ public void load() {
}
}
}

public Inventory getShopInventory(Player player) {
HashMap<String, String> placeholders = new HashMap<>();

for (Currency currency : Currency.values()) {
placeholders.put("%" + currency.getName() + "%", String.valueOf(this.currencyAPI.getCurrency(player, currency)));
}

Inventory inventory = this.plugin.getServer().createInventory(null, this.inventorySize, this.inventoryName);

for (Entry<ItemBuilder, Integer> itemBuilders : this.customizerItems.entrySet()) {
itemBuilders.getKey().setNamePlaceholders(placeholders)
.setLorePlaceholders(placeholders);
inventory.setItem(itemBuilders.getValue(), itemBuilders.getKey().build());
}

this.shopItems.keySet().forEach(itemBuilder -> inventory.setItem(this.shopItems.get(itemBuilder), itemBuilder.build()));
public Map<ItemBuilder, Integer> getShopItems() {
return Collections.unmodifiableMap(this.shopItems);
}

return inventory;
public Map<ItemBuilder, Integer> getCustomizerItems() {
return Collections.unmodifiableMap(this.customizerItems);
}

public String getInventoryName() {
return this.inventoryName;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.badbones69.crazyenchantments.paper.api.FileManager.Files;
import com.badbones69.crazyenchantments.paper.api.MigrateManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.MenuManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.ShopMenu;
import com.badbones69.crazyenchantments.paper.api.builders.types.blacksmith.BlackSmithManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.gkitz.KitsManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.tinkerer.TinkererManager;
Expand All @@ -17,6 +18,7 @@
import com.badbones69.crazyenchantments.paper.api.enums.Scrolls;
import com.badbones69.crazyenchantments.paper.api.enums.pdc.DataKeys;
import com.badbones69.crazyenchantments.paper.api.enums.pdc.Enchant;
import com.badbones69.crazyenchantments.paper.api.managers.ShopManager;
import com.badbones69.crazyenchantments.paper.api.objects.CEBook;
import com.badbones69.crazyenchantments.paper.api.objects.CEnchantment;
import com.badbones69.crazyenchantments.paper.api.objects.Category;
Expand All @@ -27,7 +29,6 @@
import com.badbones69.crazyenchantments.paper.controllers.settings.EnchantmentBookSettings;
import com.badbones69.crazyenchantments.paper.controllers.settings.ProtectionCrystalSettings;
import com.badbones69.crazyenchantments.paper.listeners.ScramblerListener;
import com.badbones69.crazyenchantments.paper.listeners.ShopListener;
import com.badbones69.crazyenchantments.paper.support.PluginSupport;
import com.google.gson.Gson;
import net.kyori.adventure.text.Component;
Expand All @@ -45,7 +46,6 @@
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -73,9 +73,6 @@ public class CECommand implements CommandExecutor {
// Listeners
private final ScramblerListener scramblerListener = this.starter.getScramblerListener();

// Economy Management.
private final ShopListener shopListener = this.plugin.getShopListener();

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String commandLabel, String[] args) {
boolean isPlayer = sender instanceof Player;
Expand All @@ -86,7 +83,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
return true;
}

if (hasPermission(sender, "gui")) this.shopListener.openGUI((Player) sender);
if (hasPermission(sender, "gui")) {
Player player = (Player) sender;

ShopManager shopManager = this.starter.getShopManager();

player.openInventory(new ShopMenu(player, shopManager.getInventorySize(), shopManager.getInventoryName()).build().getInventory());
}

return true;
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.badbones69.crazyenchantments.paper.Starter;
import com.badbones69.crazyenchantments.paper.api.CrazyManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.MenuManager;
import com.badbones69.crazyenchantments.paper.api.builders.types.ShopMenu;
import com.badbones69.crazyenchantments.paper.api.economy.CurrencyAPI;
import com.badbones69.crazyenchantments.paper.api.enums.Dust;
import com.badbones69.crazyenchantments.paper.api.enums.Scrolls;
Expand Down Expand Up @@ -67,24 +68,20 @@ public class ShopListener implements Listener {
@NotNull
private final CurrencyAPI currencyAPI = this.starter.getCurrencyAPI();

private final Material enchantmentTable = new ItemBuilder().setMaterial("ENCHANTING_TABLE").getMaterial();

public void openGUI(Player player) {
player.openInventory(this.shopManager.getShopInventory(player));
}

@EventHandler(ignoreCancelled = true)
public void onInvClick(InventoryClickEvent event) {
ItemStack item = event.getCurrentItem();
Inventory inventory = event.getInventory();
Player player = (Player) event.getWhoClicked();

if (!event.getView().getTitle().equals(this.shopManager.getInventoryName())) return;
if (!(inventory.getHolder(false) instanceof ShopMenu holder)) return;

if (item == null) return;

Player player = holder.getPlayer();

event.setCancelled(true);

if (event.getRawSlot() >= inventory.getSize()) return;
if (event.getClickedInventory() != player.getOpenInventory().getTopInventory()) return;

for (Category category : this.enchantmentBookSettings.getCategories()) {
if (category.isInGUI() && item.isSimilar(category.getDisplayItem().build())) {
Expand Down Expand Up @@ -116,7 +113,6 @@ public void onInvClick(InventoryClickEvent event) {
LostBook lostBook = category.getLostBook();

if (lostBook.isInGUI() && item.isSimilar(lostBook.getDisplayItem().build())) {

if (this.methods.isInventoryFull(player)) return;

if (lostBook.getCurrency() != null && player.getGameMode() != GameMode.CREATIVE) {
Expand Down Expand Up @@ -195,9 +191,10 @@ public void onEnchantmentTableClick(PlayerInteractEvent event) {
Player player = event.getPlayer();
Block block = event.getClickedBlock();

if (block != null && event.getAction() == Action.RIGHT_CLICK_BLOCK && block.getType() == enchantmentTable) {
if (block != null && event.getAction() == Action.RIGHT_CLICK_BLOCK && block.getType() == Material.ENCHANTING_TABLE) {
event.setCancelled(true);
openGUI(player);

player.openInventory(new ShopMenu(player, this.shopManager.getInventorySize(), this.shopManager.getInventoryName()).build().getInventory());
}
}
}
Expand Down

0 comments on commit 6948ec4

Please sign in to comment.