diff --git a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileData.java b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileData.java index db130b1..1021edb 100644 --- a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileData.java +++ b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileData.java @@ -17,8 +17,9 @@ public final class ProjectileData { private Material material; public boolean isBouncyBlock(Block block) { + if (settings.contains(ProjectileSetting.ALL_BLOCKS)) return true; if (!blocks.isEmpty() && block != null) return blocks.contains(block.getType()); - return true; + return false; } public void addBlock(Material material) { diff --git a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileDefaults.java b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileDefaults.java index e31fc75..e0a277b 100644 --- a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileDefaults.java +++ b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileDefaults.java @@ -12,6 +12,7 @@ public static ProjectileData defaultData() { defaultData.addSetting(ProjectileSetting.DROPS); defaultData.addSetting(ProjectileSetting.REMOVES); defaultData.addSetting(ProjectileSetting.RETURNS); + defaultData.addSetting(ProjectileSetting.ALL_BLOCKS); defaultData.addBlock(Material.GRASS_BLOCK); defaultData.addBlock(Material.SAND); defaultData.addBlock(Material.STONE); @@ -22,6 +23,7 @@ public static ProjectileData defaultData() { public static ProjectileData snowBallData() { ProjectileData snowballData = new ProjectileData(); snowballData.setMaterial(Material.SNOWBALL); + snowballData.addSetting(ProjectileSetting.ALL_BLOCKS); snowballData.addSetting(ProjectileSetting.REMOVES); snowballData.addSetting(ProjectileSetting.RETURNS); snowballData.addSetting(ProjectileSetting.DROPS); @@ -37,6 +39,7 @@ public static ProjectileData snowBallData() { public static ProjectileData turtleEggData() { ProjectileData turtleEggData = new ProjectileData(); turtleEggData.setMaterial(Material.TURTLE_EGG); + turtleEggData.addSetting(ProjectileSetting.ALL_BLOCKS); turtleEggData.addSetting(ProjectileSetting.REMOVES); turtleEggData.addSetting(ProjectileSetting.DROPS); turtleEggData.setPhysics(ProjectilePhysics.LAUNCH_VELOCITY, 1.0D); @@ -51,6 +54,7 @@ public static ProjectileData turtleEggData() { public static ProjectileData hotPotatoData() { ProjectileData hotPotatoData = new ProjectileData(); hotPotatoData.setMaterial(Material.BAKED_POTATO); + hotPotatoData.addSetting(ProjectileSetting.ALL_BLOCKS); hotPotatoData.addSetting(ProjectileSetting.REMOVES); hotPotatoData.addSetting(ProjectileSetting.RETURNS); hotPotatoData.addSetting(ProjectileSetting.DROPS); @@ -66,6 +70,7 @@ public static ProjectileData hotPotatoData() { public static ProjectileData newtonsAppleData() { ProjectileData newtonsAppleData = new ProjectileData(); newtonsAppleData.setMaterial(Material.APPLE); + newtonsAppleData.addSetting(ProjectileSetting.ALL_BLOCKS); newtonsAppleData.addSetting(ProjectileSetting.REMOVES); newtonsAppleData.addSetting(ProjectileSetting.RETURNS); newtonsAppleData.addSetting(ProjectileSetting.DROPS); @@ -81,6 +86,7 @@ public static ProjectileData newtonsAppleData() { public static ProjectileData groovyJukeBoxData() { ProjectileData groovyJukeBoxData = new ProjectileData(); groovyJukeBoxData.setMaterial(Material.JUKEBOX); + groovyJukeBoxData.addSetting(ProjectileSetting.ALL_BLOCKS); groovyJukeBoxData.addSetting(ProjectileSetting.REMOVES); groovyJukeBoxData.addSetting(ProjectileSetting.RETURNS); groovyJukeBoxData.addSetting(ProjectileSetting.DROPS); diff --git a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileSetting.java b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileSetting.java index eb7eb04..6042de5 100644 --- a/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileSetting.java +++ b/src/main/java/com/huskydreaming/bouncyball/data/projectiles/ProjectileSetting.java @@ -5,6 +5,7 @@ public enum ProjectileSetting { DROPS("Item drops when projectile stops moving"), GLOWS("Makes the projectile glow"), ITEM_NAME("Name above item when dropped"), + ALL_BLOCKS("Bounces on all blocks"), REMOVES("Projectile is removed from inventory"); private final String description; diff --git a/src/main/java/com/huskydreaming/bouncyball/enumerations/Menu.java b/src/main/java/com/huskydreaming/bouncyball/enumerations/Menu.java index 9864abb..db4dc4a 100644 --- a/src/main/java/com/huskydreaming/bouncyball/enumerations/Menu.java +++ b/src/main/java/com/huskydreaming/bouncyball/enumerations/Menu.java @@ -21,20 +21,24 @@ public enum Menu implements Parseable { EDIT_AMOUNT_LORE(List.of("", "&7Left-Click to increase", "&7Right-Click to decrease")), EDIT_BLOCK_TITLE("&eEdit Blocks"), EDIT_BLOCK_LORE(List.of("&7Click to edit allowed blocks.")), + EDIT_BLOCK_DISABLED_TITLE("&cEdit Blocks"), + EDIT_BLOCK_DISABLED_LORE(List.of("&7Can already bounce on all blocks", "&7Available to change in settings")), EDIT_COLOR_TITLE("<0><1>"), EDIT_COLOR_LORE(List.of("&7Click to select color")), EDIT_DELETE_TITLE("&cDelete"), EDIT_DELETE_LORE(List.of("&7Click to delete bouncy ball")), EDIT_MATERIAL_TITLE("&eEdit Materials"), EDIT_MATERIAL_LORE(List.of("&7Click to edit material.")), - EDIT_PHYSICS_TITLE("&b<0>"), - EDIT_PHYSICS_LORE(List.of( + EDIT_PHYSIC_TITLE("&b<0>"), + EDIT_PHYSIC_LORE(List.of( "&f<0>", "", "&7Amount: &f<1>", "", "&7Left-Click to increment", "&7Right-Click to decrement")), + EDIT_PHYSICS_TITLE("&eEdit Physics"), + EDIT_PHYSICS_LORE(List.of("Click to edit material.")), EDIT_PARTICLE_TITLE("&eEdit Particles"), EDIT_PARTICLE_LORE(List.of("&7Click to edit particles.")), EDIT_SET_BLOCK_TITLE("&e<0>"), @@ -43,6 +47,8 @@ public enum Menu implements Parseable { EDIT_SET_BLOCK_LORE(List.of("&7Click to set block as bounce-able.")), EDIT_SET_MATERIAL_LORE(List.of("&7Click to set material.")), EDIT_SET_PARTICLE_LORE(List.of("&7Click to set particle.")), + EDIT_SETTINGS_TITLE("&eEdit Settings"), + EDIT_SETTINGS_LORE(List.of("&7Click to edit settings.")), EDIT_CURRENT_BLOCK_TITLE("&b<0>"), EDIT_CURRENT_MATERIAL_TITLE("&b<0>"), EDIT_CURRENT_PARTICLE_TITLE("&b<0>"), diff --git a/src/main/java/com/huskydreaming/bouncyball/handlers/implementations/InventoryHandlerImpl.java b/src/main/java/com/huskydreaming/bouncyball/handlers/implementations/InventoryHandlerImpl.java index 5bcd73c..c106d31 100644 --- a/src/main/java/com/huskydreaming/bouncyball/handlers/implementations/InventoryHandlerImpl.java +++ b/src/main/java/com/huskydreaming/bouncyball/handlers/implementations/InventoryHandlerImpl.java @@ -2,6 +2,8 @@ import com.huskydreaming.bouncyball.data.particles.ParticleColor; import com.huskydreaming.bouncyball.data.projectiles.ProjectileData; +import com.huskydreaming.bouncyball.data.projectiles.ProjectilePhysics; +import com.huskydreaming.bouncyball.data.projectiles.ProjectileSetting; import com.huskydreaming.bouncyball.handlers.interfaces.InventoryHandler; import com.huskydreaming.bouncyball.inventories.*; import com.huskydreaming.bouncyball.repositories.interfaces.ProjectileRepository; @@ -51,7 +53,7 @@ public SmartInventory getEditInventory(HuskyPlugin plugin, String key) { return SmartInventory.builder() .manager(inventoryManager) .id("editInventory") - .size(5, 9) + .size(3, 9) .provider(mainInventory) .title("Editing: " + name) .build(); @@ -110,8 +112,40 @@ public SmartInventory getParticleInventory(HuskyPlugin plugin, String key) { .manager(inventoryManager) .id("particleInventory") .size(Math.min(rows + 2, 5), 9) - .provider(particleInventory) .title("Particles: " + name) + .provider(particleInventory) + .build(); + } + + @Override + public SmartInventory getSettingsInventory(HuskyPlugin plugin, String key) { + ProjectileSetting[] settings = ProjectileSetting.values(); + int rows = (int) Math.ceil((double) settings.length / 9); + + SettingsInventory settingsInventory = new SettingsInventory(plugin, key, rows, settings); + String name = Util.capitalize(key.replace("_", " ")); + return SmartInventory.builder() + .manager(inventoryManager) + .id("settingsInventory") + .title("Settings: " + name) + .size(Math.min(rows + 2, 5), 9) + .provider(settingsInventory) + .build(); + } + + @Override + public SmartInventory getPhysicsInventory(HuskyPlugin plugin, String key) { + ProjectilePhysics[] physics = ProjectilePhysics.values(); + int rows = (int) Math.ceil((double) physics.length / 9); + + PhysicsInventory physicsInventory = new PhysicsInventory(plugin, key, rows, physics); + String name = Util.capitalize(key.replace("_", " ")); + return SmartInventory.builder() + .manager(inventoryManager) + .id("physicsInventory") + .title("Physics: " + name) + .size(Math.min(rows + 2, 5), 9) + .provider(physicsInventory) .build(); } @@ -124,9 +158,9 @@ public SmartInventory getColorInventory(HuskyPlugin plugin, String key) { return SmartInventory.builder() .manager(inventoryManager) .id("colorInventory") + .title("Select Color") .size(Math.min(rows + 2, 5), 9) .provider(colorInventory) - .title("Particle Color") .build(); } diff --git a/src/main/java/com/huskydreaming/bouncyball/handlers/interfaces/InventoryHandler.java b/src/main/java/com/huskydreaming/bouncyball/handlers/interfaces/InventoryHandler.java index 7e81b93..3369572 100644 --- a/src/main/java/com/huskydreaming/bouncyball/handlers/interfaces/InventoryHandler.java +++ b/src/main/java/com/huskydreaming/bouncyball/handlers/interfaces/InventoryHandler.java @@ -16,5 +16,9 @@ public interface InventoryHandler extends Handler { SmartInventory getParticleInventory(HuskyPlugin plugin, String key); + SmartInventory getSettingsInventory(HuskyPlugin plugin, String key); + + SmartInventory getPhysicsInventory(HuskyPlugin plugin, String key); + SmartInventory getColorInventory(HuskyPlugin plugin, String key); } diff --git a/src/main/java/com/huskydreaming/bouncyball/inventories/EditInventory.java b/src/main/java/com/huskydreaming/bouncyball/inventories/EditInventory.java index 5b8f581..57306d4 100644 --- a/src/main/java/com/huskydreaming/bouncyball/inventories/EditInventory.java +++ b/src/main/java/com/huskydreaming/bouncyball/inventories/EditInventory.java @@ -1,14 +1,12 @@ package com.huskydreaming.bouncyball.inventories; import com.huskydreaming.bouncyball.data.projectiles.ProjectileData; -import com.huskydreaming.bouncyball.data.projectiles.ProjectilePhysics; import com.huskydreaming.bouncyball.data.projectiles.ProjectileSetting; import com.huskydreaming.bouncyball.handlers.interfaces.InventoryHandler; import com.huskydreaming.bouncyball.enumerations.Menu; import com.huskydreaming.bouncyball.repositories.interfaces.ProjectileRepository; import com.huskydreaming.huskycore.HuskyPlugin; import com.huskydreaming.huskycore.inventories.InventoryItem; -import com.huskydreaming.huskycore.storage.parseables.DefaultMenu; import com.huskydreaming.huskycore.utilities.builders.ItemBuilder; import fr.minuskube.inv.ClickableItem; import fr.minuskube.inv.content.InventoryContents; @@ -17,8 +15,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.text.DecimalFormat; -import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -43,20 +39,12 @@ public void init(Player player, InventoryContents contents) { contents.set(0, 0, InventoryItem.back(player, inventoryHandler.getBouncyBallsInventory(plugin))); - contents.set(1, 2, editBlocks()); - contents.set(1, 3, editMaterials()); - contents.set(1, 4, editParticles()); - contents.set(1, 5, deleteItem()); - - int index = 2; - for (ProjectilePhysics projectilePhysics : ProjectilePhysics.values()) { - contents.set(2, index++, physicsItem(projectilePhysics, contents)); - } - - index = 2; - for (ProjectileSetting projectileSetting : ProjectileSetting.values()) { - contents.set(3, index++, activeItem(projectileSetting, contents)); - } + contents.set(1, 1, editBlocks()); + contents.set(1, 2, editMaterials()); + contents.set(1, 3, editParticles()); + contents.set(1, 4, editSettings()); + contents.set(1, 5, editPhysics()); + contents.set(1, 7, deleteItem()); } @Override @@ -65,14 +53,23 @@ public void update(Player player, InventoryContents contents) { } private ClickableItem editBlocks() { + ProjectileData projectileData = projectileRepository.getProjectileData(key); + Set settings = projectileData.getSettings(); + + boolean allBlocks = settings.contains(ProjectileSetting.ALL_BLOCKS); + + String title = allBlocks ? Menu.EDIT_BLOCK_DISABLED_TITLE.parse() : Menu.EDIT_BLOCK_TITLE.parse(); + List lore = allBlocks ? Menu.EDIT_BLOCK_DISABLED_LORE.parseList() : Menu.EDIT_BLOCK_LORE.parseList(); + Material material = allBlocks ? Material.BEDROCK : Material.GRASS_BLOCK; + ItemStack itemStack = ItemBuilder.create() - .setDisplayName(Menu.EDIT_BLOCK_TITLE.parse()) - .setLore(Menu.EDIT_BLOCK_LORE.parseList()) - .setMaterial(Material.GRASS_BLOCK) + .setDisplayName(title) + .setLore(lore) + .setMaterial(material) .build(); return ClickableItem.of(itemStack, e -> { - if (e.getWhoClicked() instanceof Player player) { + if (e.getWhoClicked() instanceof Player player && !allBlocks) { inventoryHandler.getBlockInventory(player.getWorld(), plugin, key).open(player); } }); @@ -92,109 +89,65 @@ private ClickableItem editMaterials() { }); } - private ClickableItem deleteItem() { + private ClickableItem editSettings() { ItemStack itemStack = ItemBuilder.create() - .setDisplayName(Menu.EDIT_DELETE_TITLE.parse()) - .setLore(Menu.EDIT_DELETE_LORE.parseList()) - .setMaterial(Material.TNT_MINECART) + .setDisplayName(Menu.EDIT_SETTINGS_TITLE.parse()) + .setLore(Menu.EDIT_SETTINGS_LORE.parseList()) + .setMaterial(Material.COMPARATOR) .build(); return ClickableItem.of(itemStack, e -> { if (e.getWhoClicked() instanceof Player player) { - projectileRepository.removeProjectileData(key); - - if (projectileRepository.getProjectileDataMap().isEmpty()) { - player.closeInventory(); - } else { - inventoryHandler.getBouncyBallsInventory(plugin).open(player); - } - + inventoryHandler.getSettingsInventory(plugin, key).open(player); } }); } - private ClickableItem editParticles() { + private ClickableItem editPhysics() { ItemStack itemStack = ItemBuilder.create() - .setDisplayName(Menu.EDIT_PARTICLE_TITLE.parse()) - .setLore(Menu.EDIT_PARTICLE_LORE.parseList()) - .setMaterial(Material.NETHER_STAR) + .setDisplayName(Menu.EDIT_PHYSICS_TITLE.parse()) + .setLore(Menu.EDIT_SETTINGS_LORE.parseList()) + .setMaterial(Material.FEATHER) .build(); return ClickableItem.of(itemStack, e -> { if (e.getWhoClicked() instanceof Player player) { - inventoryHandler.getParticleInventory(plugin, key).open(player); + inventoryHandler.getPhysicsInventory(plugin, key).open(player); } }); } - private ClickableItem activeItem(ProjectileSetting projectileSetting, InventoryContents contents) { - String materialEnabled = DefaultMenu.ENABLE_MATERIAL.parse(); - String materialDisabled = DefaultMenu.DISABLED_MATERIAL.parse(); - - String displayNameEnabled = DefaultMenu.ENABLE_TITLE.parameterize(projectileSetting.name()); - String displayNameDisabled = DefaultMenu.DISABLED_TITLE.parameterize(projectileSetting.name()); - - ProjectileData projectileData = projectileRepository.getProjectileData(key); - Set settings = projectileData.getSettings(); - - boolean enabled = settings.contains(projectileSetting); - - String displayName = enabled ? displayNameEnabled : displayNameDisabled; - Material material = Material.valueOf(enabled ? materialEnabled : materialDisabled); - String description = enabled ? DefaultMenu.DESCRIPTION_ENABLE.parse() : DefaultMenu.DESCRIPTION_DISABLE.parse(); - - List strings = new ArrayList<>(); - strings.add(DefaultMenu.DESCRIPTION_DEFAULT.parameterize(projectileSetting.getDescription())); - strings.add(""); - strings.add(description); - + private ClickableItem deleteItem() { ItemStack itemStack = ItemBuilder.create() - .setDisplayName(displayName) - .setLore(strings) - .setMaterial(material) + .setDisplayName(Menu.EDIT_DELETE_TITLE.parse()) + .setLore(Menu.EDIT_DELETE_LORE.parseList()) + .setMaterial(Material.TNT_MINECART) .build(); return ClickableItem.of(itemStack, e -> { if (e.getWhoClicked() instanceof Player player) { - if (settings.contains(projectileSetting)) { - projectileData.removeSetting(projectileSetting); + projectileRepository.removeProjectileData(key); + + if (projectileRepository.getProjectileDataMap().isEmpty()) { + player.closeInventory(); } else { - projectileData.addSetting(projectileSetting); + inventoryHandler.getBouncyBallsInventory(plugin).open(player); } - contents.inventory().open(player); + } }); } - private ClickableItem physicsItem(ProjectilePhysics projectilePhysics, InventoryContents contents) { - ProjectileData projectileData = projectileRepository.getProjectileData(key); - double amount = projectileData.getPhysics(projectilePhysics); - + private ClickableItem editParticles() { ItemStack itemStack = ItemBuilder.create() - .setDisplayName(Menu.EDIT_PHYSICS_TITLE.parameterize(projectilePhysics.name())) - .setLore(Menu.EDIT_PHYSICS_LORE.parameterizeList(projectilePhysics.getDescription(), amount)) - .setMaterial(projectilePhysics.getMaterial()) + .setDisplayName(Menu.EDIT_PARTICLE_TITLE.parse()) + .setLore(Menu.EDIT_PARTICLE_LORE.parseList()) + .setMaterial(Material.NETHER_STAR) .build(); return ClickableItem.of(itemStack, e -> { if (e.getWhoClicked() instanceof Player player) { - double increment = projectilePhysics.getIncrement(); - DecimalFormat df = new DecimalFormat("0.00"); - if (e.isLeftClick()) { - projectileData.setPhysics(projectilePhysics, Double.parseDouble(df.format(amount + increment))); - contents.inventory().open(player); - return; - } - if (e.isRightClick()) { - if (amount <= increment) { - // This is a safeguard in case the amount is offset - projectileData.setPhysics(projectilePhysics, increment); - contents.inventory().open(player); - return; - } - projectileData.setPhysics(projectilePhysics, Double.parseDouble(df.format(amount - increment))); - contents.inventory().open(player); - } + inventoryHandler.getParticleInventory(plugin, key).open(player); } }); } diff --git a/src/main/java/com/huskydreaming/bouncyball/inventories/PhysicsInventory.java b/src/main/java/com/huskydreaming/bouncyball/inventories/PhysicsInventory.java new file mode 100644 index 0000000..6508a59 --- /dev/null +++ b/src/main/java/com/huskydreaming/bouncyball/inventories/PhysicsInventory.java @@ -0,0 +1,81 @@ +package com.huskydreaming.bouncyball.inventories; + +import com.huskydreaming.bouncyball.data.projectiles.ProjectileData; +import com.huskydreaming.bouncyball.data.projectiles.ProjectilePhysics; +import com.huskydreaming.bouncyball.enumerations.Menu; +import com.huskydreaming.bouncyball.handlers.interfaces.InventoryHandler; +import com.huskydreaming.bouncyball.repositories.interfaces.ProjectileRepository; +import com.huskydreaming.huskycore.HuskyPlugin; +import com.huskydreaming.huskycore.inventories.InventoryItem; +import com.huskydreaming.huskycore.inventories.providers.InventoryPageProvider; +import com.huskydreaming.huskycore.utilities.builders.ItemBuilder; +import fr.minuskube.inv.content.InventoryContents; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; + +import java.text.DecimalFormat; + +public class PhysicsInventory extends InventoryPageProvider { + + private final InventoryHandler inventoryHandler; + private final ProjectileRepository projectileRepository; + + private final HuskyPlugin plugin; + private final String key; + + public PhysicsInventory(HuskyPlugin plugin, String key, int rows, ProjectilePhysics[] physics) { + super("Physics", rows, physics); + this.plugin = plugin; + this.key = key; + + inventoryHandler = plugin.provide(InventoryHandler.class); + projectileRepository = plugin.provide(ProjectileRepository.class); + } + + @Override + public void init(Player player, InventoryContents contents) { + super.init(player, contents); + + contents.set(0, 0, InventoryItem.back(player, inventoryHandler.getEditInventory(plugin, key))); + } + + @Override + public ItemStack construct(Player player, int i, ProjectilePhysics physics) { + ProjectileData projectileData = projectileRepository.getProjectileData(key); + double amount = projectileData.getPhysics(physics); + + return ItemBuilder.create() + .setDisplayName(Menu.EDIT_PHYSIC_TITLE.parameterize(physics.name())) + .setLore(Menu.EDIT_PHYSIC_LORE.parameterizeList(physics.getDescription(), amount)) + .setMaterial(physics.getMaterial()) + .build(); + } + + @Override + public void run(InventoryClickEvent event, ProjectilePhysics physics, InventoryContents contents) { + if (event.getWhoClicked() instanceof Player player) { + ProjectileData projectileData = projectileRepository.getProjectileData(key); + double increment = physics.getIncrement(); + double amount = projectileData.getPhysics(physics); + DecimalFormat df = new DecimalFormat("0.00"); + + if (event.isLeftClick()) { + projectileData.setPhysics(physics, Double.parseDouble(df.format(amount + increment))); + contents.inventory().open(player); + return; + } + + if (event.isRightClick()) { + if (amount <= increment) { + // This is a safeguard in case the amount is offset + projectileData.setPhysics(physics, increment); + contents.inventory().open(player); + return; + } + projectileData.setPhysics(physics, Double.parseDouble(df.format(amount - increment))); + contents.inventory().open(player); + } + } + } +} diff --git a/src/main/java/com/huskydreaming/bouncyball/inventories/SettingsInventory.java b/src/main/java/com/huskydreaming/bouncyball/inventories/SettingsInventory.java new file mode 100644 index 0000000..950d017 --- /dev/null +++ b/src/main/java/com/huskydreaming/bouncyball/inventories/SettingsInventory.java @@ -0,0 +1,90 @@ +package com.huskydreaming.bouncyball.inventories; + +import com.huskydreaming.bouncyball.data.projectiles.ProjectileData; +import com.huskydreaming.bouncyball.data.projectiles.ProjectileSetting; +import com.huskydreaming.bouncyball.handlers.interfaces.InventoryHandler; +import com.huskydreaming.bouncyball.repositories.interfaces.ProjectileRepository; +import com.huskydreaming.huskycore.HuskyPlugin; +import com.huskydreaming.huskycore.inventories.InventoryItem; +import com.huskydreaming.huskycore.inventories.providers.InventoryPageProvider; +import com.huskydreaming.huskycore.storage.parseables.DefaultMenu; +import com.huskydreaming.huskycore.utilities.builders.ItemBuilder; +import fr.minuskube.inv.content.InventoryContents; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class SettingsInventory extends InventoryPageProvider { + + private final InventoryHandler inventoryHandler; + private final ProjectileRepository projectileRepository; + + private final HuskyPlugin plugin; + private final String key; + + + public SettingsInventory(HuskyPlugin plugin, String key, int rows, ProjectileSetting[] settings) { + super("Settings", rows, settings); + this.plugin = plugin; + this.key = key; + + inventoryHandler = plugin.provide(InventoryHandler.class); + projectileRepository = plugin.provide(ProjectileRepository.class); + } + + @Override + public void init(Player player, InventoryContents contents) { + super.init(player, contents); + + contents.set(0, 0, InventoryItem.back(player, inventoryHandler.getEditInventory(plugin, key))); + } + + @Override + public ItemStack construct(Player player, int i, ProjectileSetting setting) { + String materialEnabled = DefaultMenu.ENABLE_MATERIAL.parse(); + String materialDisabled = DefaultMenu.DISABLED_MATERIAL.parse(); + + String displayNameEnabled = DefaultMenu.ENABLE_TITLE.parameterize(setting.name()); + String displayNameDisabled = DefaultMenu.DISABLED_TITLE.parameterize(setting.name()); + + ProjectileData projectileData = projectileRepository.getProjectileData(key); + Set settings = projectileData.getSettings(); + + boolean enabled = settings.contains(setting); + + String displayName = enabled ? displayNameEnabled : displayNameDisabled; + Material material = Material.valueOf(enabled ? materialEnabled : materialDisabled); + String description = enabled ? DefaultMenu.DESCRIPTION_ENABLE.parse() : DefaultMenu.DESCRIPTION_DISABLE.parse(); + + List strings = new ArrayList<>(); + strings.add(DefaultMenu.DESCRIPTION_DEFAULT.parameterize(setting.getDescription())); + strings.add(""); + strings.add(description); + + return ItemBuilder.create() + .setDisplayName(displayName) + .setLore(strings) + .setMaterial(material) + .build(); + } + + @Override + public void run(InventoryClickEvent event, ProjectileSetting setting, InventoryContents contents) { + if (event.getWhoClicked() instanceof Player player) { + ProjectileData projectileData = projectileRepository.getProjectileData(key); + Set settings = projectileData.getSettings(); + + if (settings.contains(setting)) { + projectileData.removeSetting(setting); + } else { + projectileData.addSetting(setting); + } + contents.inventory().open(player); + } + } +} \ No newline at end of file