diff --git a/src/main/java/bentobox/addon/challenges/FreshSqueezedChallenges.java b/src/main/java/bentobox/addon/challenges/FreshSqueezedChallenges.java index 9c2fa1f..2481c02 100644 --- a/src/main/java/bentobox/addon/challenges/FreshSqueezedChallenges.java +++ b/src/main/java/bentobox/addon/challenges/FreshSqueezedChallenges.java @@ -122,8 +122,8 @@ public class FreshSqueezedChallenges { newChallenge.setTakeItems(details.getBoolean("takeItems",true)); newChallenge.setRewardText(details.getString("rewardText", "")); newChallenge.setRewardCommands(details.getStringList("rewardcommands")); - newChallenge.setMoneyReward(details.getInt("moneyReward",0)); - newChallenge.setExpReward(details.getInt("expReward")); + newChallenge.setRewardMoney(details.getInt("moneyReward",0)); + newChallenge.setRewardExp(details.getInt("expReward")); newChallenge.setRepeatable(details.getBoolean("repeatable")); newChallenge.setRepeatRewardText(details.getString("repeatRewardText","")); newChallenge.setRepeatMoneyReward(details.getInt("repearMoneyReward")); @@ -141,7 +141,7 @@ public class FreshSqueezedChallenges { } else if (newChallenge.getChallengeType().equals(Challenges.ChallengeType.ISLAND)) { parseEntities(newChallenge, reqItems); } - newChallenge.setItemReward(parseItems(details.getString("itemReward"))); + newChallenge.setRewardItems(parseItems(details.getString("itemReward"))); newChallenge.setRepeatItemReward(parseItems(details.getString("repeatItemReward"))); // Save if (addon.getChallengesManager().storeChallenge(newChallenge, overwrite, user, false)) { diff --git a/src/main/java/bentobox/addon/challenges/database/object/Challenges.java b/src/main/java/bentobox/addon/challenges/database/object/Challenges.java index b106f1e..77277fe 100644 --- a/src/main/java/bentobox/addon/challenges/database/object/Challenges.java +++ b/src/main/java/bentobox/addon/challenges/database/object/Challenges.java @@ -94,13 +94,13 @@ public class Challenges implements DataObject { // Rewards @ConfigComment("List of items the player will receive first time. ItemStack List.") - private List itemReward = new ArrayList<>(); + private List rewardItems = new ArrayList<>(); @ConfigComment("If this is blank, the reward text will be auto-generated, otherwise this will be used.") private String rewardText = ""; @ConfigComment("Experience point reward") - private int expReward; + private int rewardExp; @ConfigComment("Money reward") - private int moneyReward; + private int rewardMoney; @ConfigComment("Commands to run when the player completes the challenge for the first time. String List") private List rewardCommands = new ArrayList<>(); @@ -173,15 +173,15 @@ public class Challenges implements DataObject { /** * @return the expReward */ - public int getExpReward() { - return expReward; + public int getRewardExp() { + return rewardExp; } /** * @param expReward the expReward to set */ - public void setExpReward(int expReward) { - this.expReward = expReward; + public void setRewardExp(int expReward) { + this.rewardExp = expReward; } /** @@ -246,15 +246,15 @@ public class Challenges implements DataObject { /** * @return the moneyReward */ - public int getMoneyReward() { - return moneyReward; + public int getRewardMoney() { + return rewardMoney; } /** * @param moneyReward the moneyReward to set */ - public void setMoneyReward(int moneyReward) { - this.moneyReward = moneyReward; + public void setRewardMoney(int moneyReward) { + this.rewardMoney = moneyReward; } /** @@ -470,15 +470,15 @@ public class Challenges implements DataObject { /** * @return the itemReward */ - public List getItemReward() { - return itemReward; + public List getRewardItems() { + return rewardItems; } /** * @param itemReward the itemReward to set */ - public void setItemReward(List itemReward) { - this.itemReward = itemReward; + public void setRewardItems(List itemReward) { + this.rewardItems = itemReward; } /** diff --git a/src/main/java/bentobox/addon/challenges/panel/AdminGUI.java b/src/main/java/bentobox/addon/challenges/panel/AdminGUI.java index 57c2192..2380957 100644 --- a/src/main/java/bentobox/addon/challenges/panel/AdminGUI.java +++ b/src/main/java/bentobox/addon/challenges/panel/AdminGUI.java @@ -2,15 +2,18 @@ package bentobox.addon.challenges.panel; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import bentobox.addon.challenges.ChallengesAddon; import bentobox.addon.challenges.database.object.Challenges; +import world.bentobox.bentobox.api.panels.Panel; +import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; -public class AdminGUI { +public class AdminGUI implements ClickHandler { private ChallengesAddon addon; private User player; @@ -37,11 +40,21 @@ public class AdminGUI { this.world = world; this.permPrefix = permPrefix; this.label = label; - + new PanelBuilder().size(27).user(player).name(player.getTranslation("challenges.admin.gui-title")) .item(new PanelItemBuilder().icon(challenge.getIcon()).name("Icon").build()) .item(9, new PanelItemBuilder().icon(new ItemStack(Material.WHITE_BANNER)).name("Description").description(challenge.getDescription()).build()) - .item(18, new PanelItemBuilder().icon(new ItemStack(Material.GREEN_STAINED_GLASS_PANE)).name("Active").build()).build(); + .item(18, new PanelItemBuilder().icon(new ItemStack(Material.GREEN_STAINED_GLASS_PANE)).name("Active").build()) + .item(27, new PanelItemBuilder().icon(new ItemStack(Material.BOOK)).name("Edit required items").clickHandler(this).build()) + .build(); + } + + @Override + public boolean onClick(Panel panel, User user, ClickType clickType, int slot) { + if (slot == 27) { + new RequiredPanel(challenge, user, panel); + } + return true; } diff --git a/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels.java b/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels.java index a8d249a..1e80a10 100644 --- a/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels.java +++ b/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels.java @@ -196,9 +196,9 @@ public class ChallengesPanels { String rewardText = ""; if (!complete) { // First time - moneyReward = challenge.getMoneyReward(); + moneyReward = challenge.getRewardMoney(); rewardText = challenge.getRewardText(); - expReward = challenge.getExpReward(); + expReward = challenge.getRewardExp(); if (!rewardText.isEmpty()) { result.addAll(splitTrans(user, "challenges.first-time-rewards")); } diff --git a/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels2.java b/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels2.java index 6c16743..f5b5cf7 100644 --- a/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels2.java +++ b/src/main/java/bentobox/addon/challenges/panel/ChallengesPanels2.java @@ -232,9 +232,9 @@ public class ChallengesPanels2 { String rewardText = ""; if (admin || !complete) { // First time - moneyReward = challenge.getMoneyReward(); + moneyReward = challenge.getRewardMoney(); rewardText = challenge.getRewardText(); - expReward = challenge.getExpReward(); + expReward = challenge.getRewardExp(); if (!rewardText.isEmpty()) { result.addAll(splitTrans(user, "challenges.first-time-rewards")); } diff --git a/src/main/java/bentobox/addon/challenges/panel/RequiredPanel.java b/src/main/java/bentobox/addon/challenges/panel/RequiredPanel.java new file mode 100644 index 0000000..2db50a0 --- /dev/null +++ b/src/main/java/bentobox/addon/challenges/panel/RequiredPanel.java @@ -0,0 +1,166 @@ +package bentobox.addon.challenges.panel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.EntityType; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.ItemStack; + +import bentobox.addon.challenges.database.object.Challenges; +import world.bentobox.bentobox.api.panels.Panel; +import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler; +import world.bentobox.bentobox.api.panels.PanelListener; +import world.bentobox.bentobox.api.panels.builders.PanelBuilder; +import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.Util; + +/** + * Handles the requirements for a challenge + * Items, blocks, entities + * @author tastybento + * + */ +public class RequiredPanel implements ClickHandler, PanelListener { + private static final int CONTROL_NUMBER = 4; + private Challenges challenge; + private User user; + private Panel panel; + private Panel referringPanel; + + /** + * @param challenge + * @param user + */ + public RequiredPanel(Challenges challenge, User user, Panel referringPanel) { + this.challenge = challenge; + this.user = user; + this.panel = openPanel(); + this.referringPanel = referringPanel; + } + + private Panel openPanel() { + PanelBuilder pb = new PanelBuilder().listener(this).name("Required Items").size(49); + // Add the name and description icon + pb.item(new PanelItemBuilder().icon(Material.BOOK).name(challenge.getFriendlyName()).description(challenge.getDescription()).clickHandler(this).build()); + // Add take all button + pb.item(new PanelItemBuilder().icon(Material.BOOK).name("Take Items").description(challenge.isTakeItems() ? "Yes" : "No").clickHandler(this).build()); + // Add save button + pb.item(new PanelItemBuilder().icon(Material.BOOK).name("Save").clickHandler(this).build()); + // Add cancel button + pb.item(new PanelItemBuilder().icon(Material.BOOK).name("Cancel").clickHandler(this).build()); + + switch (challenge.getChallengeType()) { + case INVENTORY: + // Show the required items in the inventory + challenge.getRequiredItems().stream().map(i -> new PanelItemBuilder().icon(i).clickHandler(this).build()).forEach(pb::item); + return pb.user(user).build(); + case ISLAND: + // Create the blocks required + challenge.getRequiredBlocks().entrySet().stream().map(en -> new ItemStack(en.getKey(), en.getValue())).map(i -> new PanelItemBuilder().icon(i).clickHandler(this).build()).forEach(pb::item); + // Create the entities required + challenge.getRequiredEntities().entrySet().stream().map(this::toSpawnEgg).map(i -> new PanelItemBuilder() + .icon(i) + .name(Util.prettifyText(i.getType().toString())) + .description("Entity") + .clickHandler(this) + .build()).forEach(pb::item); + return pb.user(user).build(); + case LEVEL: + + break; + default: + break; + + } + return panel; + } + + private ItemStack toSpawnEgg(Entry en) { + Material mat = Material.getMaterial(en.getKey().name() + "_SPAWN_EGG"); + if (mat != null) { + return new ItemStack(mat, en.getValue()); + } else { + return new ItemStack(Material.COW_SPAWN_EGG); + } + } + + + @Override + public boolean onClick(Panel panel, User user, ClickType clickType, int slot) { + Bukkit.getLogger().info("DEBUG: slot = " + slot); + return slot < CONTROL_NUMBER; + } + + @Override + public void setup() { + // nothing to do + + } + + @Override + public void onInventoryClose(InventoryCloseEvent event) { + + } + + + @Override + public void onInventoryClick(User user, InventoryClickEvent event) { + // Allow drag and drop + event.setCancelled(event.getRawSlot() < CONTROL_NUMBER); + + Bukkit.getLogger().info("DEBUG: inv slot = " + event.getSlot()); + Bukkit.getLogger().info("DEBUG: inv slot type = " + event.getSlotType()); + if (event.getSlot() == 1) { + // Take items + challenge.setTakeItems(!challenge.isTakeItems()); + // Update item + event.getInventory().setItem(event.getSlot(), new PanelItemBuilder().icon(Material.BOOK).name("Take Items").description(challenge.isTakeItems() ? "Yes" : "No").build().getItem()); + return; + } + if (event.getSlot() == 3) { + // Cancel + referringPanel.open(user); + return; + // Return to previous panel + } + // Save + if (event.getSlot() != 2) { + return; + } + // Save changes + switch (challenge.getChallengeType()) { + case ICON: + break; + case INVENTORY: + List reqItems = new ArrayList<>(); + // Skip first item + for (int i = CONTROL_NUMBER; i < event.getInventory().getSize(); i++) { + if (event.getInventory().getItem(i) != null) { + reqItems.add(event.getInventory().getItem(i)); + } + } + challenge.setRequiredItems(reqItems); + user.sendMessage("challenges.admin.saved"); + // TODO: save challenges + event.getInventory().setItem(event.getSlot(), new PanelItemBuilder().icon(Material.BOOK).name("Save").description("Saved").build().getItem()); + break; + case ISLAND: + break; + case LEVEL: + break; + default: + break; + + } + + } + + +} diff --git a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java index ea37b36..fc6c82e 100644 --- a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java +++ b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java @@ -62,13 +62,13 @@ public class TryToComplete { } if (!result.repeat) { // Give rewards - for (ItemStack reward : challenge.getItemReward()) { + for (ItemStack reward : challenge.getRewardItems()) { user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v)); } // Give money - challenge.getMoneyReward(); + challenge.getRewardMoney(); // Give exp - user.getPlayer().giveExp(challenge.getExpReward()); + user.getPlayer().giveExp(challenge.getRewardExp()); // Run commands runCommands(challenge.getRewardCommands()); user.sendMessage("challenges.you-completed", "[challenge]", challenge.getFriendlyName());