From 763a8de8e4f6473973eb363d2b46b9337d992774 Mon Sep 17 00:00:00 2001 From: Tastybento Date: Sat, 24 Feb 2018 21:29:09 -0800 Subject: [PATCH] WIP improved surrounding challenges. --- locales/en-US.yml | 1 + .../addon/challenges/ChallengesManager.java | 40 ++++----- .../commands/admin/CreateSurrounding.java | 84 ++++++++++++------- .../admin/SurroundChallengeBuilder.java | 83 ++++++++++++++++++ .../database/object/Challenges.java | 3 +- .../challenges/panel/ChallengesPanels.java | 6 +- .../addon/challenges/panel/TryToComplete.java | 5 +- 7 files changed, 168 insertions(+), 54 deletions(-) create mode 100644 src/bskyblock/addon/challenges/commands/admin/SurroundChallengeBuilder.java diff --git a/locales/en-US.yml b/locales/en-US.yml index ab008fe..d5cd34e 100755 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -30,6 +30,7 @@ challenges: error: island-level: "&cYour island must be level [level] to complete this challenge!" items-not-there: "&cAll required items must be close to you on your island!" + no-items-clicked: "&cYou did not click on anything. Cancelling." not-close-enough: "&cYou must be standing within [number] blocks of all required items." not-enough-items: "&cYou do not have enough [items] to complete this challenge!" not-on-island: "&cYou must be on your island to do that!" diff --git a/src/bskyblock/addon/challenges/ChallengesManager.java b/src/bskyblock/addon/challenges/ChallengesManager.java index feabb98..6f46d1e 100644 --- a/src/bskyblock/addon/challenges/ChallengesManager.java +++ b/src/bskyblock/addon/challenges/ChallengesManager.java @@ -18,6 +18,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import bskyblock.addon.challenges.commands.admin.SurroundChallengeBuilder; import bskyblock.addon.challenges.database.object.ChallengeLevels; import bskyblock.addon.challenges.database.object.Challenges; import bskyblock.addon.challenges.database.object.Challenges.ChallengeType; @@ -37,8 +38,8 @@ public class ChallengesManager { private AbstractDatabaseHandler chHandler; private AbstractDatabaseHandler lvHandler; - - + + private ChallengesPanels challengesPanels; @SuppressWarnings("unchecked") @@ -260,7 +261,7 @@ public class ChallengesManager { } return result; } - + /** * Get the status on every level * @param user @@ -276,13 +277,13 @@ public class ChallengesManager { } return result; } - + public class LevelStatus { private final ChallengeLevels level; private final ChallengeLevels previousLevel; private final int numberOfChallengesStillToDo; private final boolean complete; - + public LevelStatus(ChallengeLevels level, ChallengeLevels previousLevel, int numberOfChallengesStillToDo, boolean complete) { super(); this.level = level; @@ -315,7 +316,7 @@ public class ChallengesManager { return complete; } - + } /** @@ -354,7 +355,7 @@ public class ChallengesManager { }); } }); - + // Save the challenge try { chHandler.saveObject(newChallenge); @@ -365,7 +366,7 @@ public class ChallengesManager { user.sendRawMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage()); return; } - + user.sendRawMessage("Success"); } @@ -390,19 +391,21 @@ public class ChallengesManager { */ public void setChallengeComplete(User user, String uniqueId) { // TODO Auto-generated method stub - + } - public void createSurroundingChallenge(String string, Map map) { - if (map.isEmpty()) { - return; + public boolean createSurroundingChallenge(SurroundChallengeBuilder inProgress) { + if (inProgress.getReqBlocks().isEmpty() && inProgress.getReqEntities().isEmpty()) { + inProgress.getOwner().sendMessage("challenges.error.no-items-clicked"); + return false; } Challenges newChallenge = new Challenges(); newChallenge.setChallengeType(ChallengeType.SURROUNDING); - newChallenge.setFriendlyName(string); + newChallenge.setFriendlyName(inProgress.getName()); newChallenge.setDeployed(true); - newChallenge.setRequiredBlocks(map); - newChallenge.setUniqueId(string); + newChallenge.setRequiredBlocks(inProgress.getReqBlocks()); + newChallenge.setRequiredEntities(inProgress.getReqEntities()); + newChallenge.setUniqueId(inProgress.getName()); newChallenge.setIcon(new ItemStack(Material.ARMOR_STAND)); newChallenge.setFreeChallenge(true); newChallenge.setLevel(FREE); @@ -412,10 +415,9 @@ public class ChallengesManager { chHandler.saveObject(newChallenge); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; + addon.getLogger().severe("Could not save challenge!"); + return false; } + return true; } - } diff --git a/src/bskyblock/addon/challenges/commands/admin/CreateSurrounding.java b/src/bskyblock/addon/challenges/commands/admin/CreateSurrounding.java index 98c5015..34b6092 100644 --- a/src/bskyblock/addon/challenges/commands/admin/CreateSurrounding.java +++ b/src/bskyblock/addon/challenges/commands/admin/CreateSurrounding.java @@ -2,15 +2,17 @@ package bskyblock.addon.challenges.commands.admin; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.UUID; -import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -23,8 +25,7 @@ public class CreateSurrounding extends CompositeCommand implements Listener { private ChallengesAddon addon; - private Map> blocks = new HashMap<>(); - private Map name = new HashMap<>(); + HashMap inProgress = new HashMap<>(); /** * Admin command to make surrounding challenges @@ -52,48 +53,71 @@ public class CreateSurrounding extends CompositeCommand implements Listener { } // Tell user to hit objects to add to the surrounding object requirements user.sendRawMessage("Hit things to add them to the list of things required. Right click when done"); - blocks.computeIfAbsent(user.getUniqueId(), k -> new HashMap<>()); - name.put(user.getUniqueId(), args.get(0)); + inProgress.put(user.getUniqueId(), new SurroundChallengeBuilder(addon).owner(user).name(args.get(0))); return true; } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent e) { - if (blocks.containsKey(e.getPlayer().getUniqueId())) { - e.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerQuit(PlayerQuitEvent e) { - blocks.remove(e.getPlayer().getUniqueId()); - name.remove(e.getPlayer().getUniqueId()); + e.setCancelled(inProgress.containsKey(e.getPlayer().getUniqueId()) ? true : false); } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerInteract(PlayerInteractEvent e) { + public void onPlayerQuit(PlayerQuitEvent e) { + inProgress.remove(e.getPlayer().getUniqueId()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public boolean onPlayerInteract(PlayerInteractEvent e) { if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) { addon.getLogger().info("DEBUG: left click"); - if (blocks.containsKey(e.getPlayer().getUniqueId())) { + if (inProgress.containsKey(e.getPlayer().getUniqueId())) { // Prevent damage e.setCancelled(true); - Map blockMap = blocks.get(e.getPlayer().getUniqueId()); - blockMap.computeIfPresent(e.getClickedBlock().getType(), (state, amount) -> amount++); - blockMap.putIfAbsent(e.getClickedBlock().getType(), 1); - blocks.put(e.getPlayer().getUniqueId(), blockMap); + inProgress.get(e.getPlayer().getUniqueId()).addBlock(e.getClickedBlock().getType()); User.getInstance(e.getPlayer()).sendRawMessage("You added one " + e.getClickedBlock().getType()); - return; - }; + return true; + } } if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { addon.getLogger().info("DEBUG: right click"); - if (blocks.containsKey(e.getPlayer().getUniqueId())) { - e.setCancelled(true); - User.getInstance(e.getPlayer()).sendRawMessage("Finished!"); - addon.getChallengesManager().createSurroundingChallenge(name.get(e.getPlayer().getUniqueId()), blocks.get(e.getPlayer().getUniqueId())); - blocks.remove(e.getPlayer().getUniqueId()); - name.remove(e.getPlayer().getUniqueId()); - } + return finished(e, e.getPlayer().getUniqueId()); } + return false; } + + private boolean finished(Cancellable e, UUID uuid) { + if (inProgress.containsKey(uuid)) { + e.setCancelled(true); + boolean status = inProgress.get(uuid).build(); + inProgress.remove(uuid); + return status; + } + return false; + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public boolean onPlayerInteract(PlayerInteractAtEntityEvent e) { + addon.getLogger().info("DEBUG: right click entity"); + return finished(e, e.getPlayer().getUniqueId()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public boolean onLeft(EntityDamageByEntityEvent e) { + addon.getLogger().info("DEBUG: left click entity"); + if (!(e.getDamager() instanceof Player)) { + return false; + } + Player player = (Player)e.getDamager(); + if (inProgress.containsKey(player.getUniqueId())) { + // Prevent damage + e.setCancelled(true); + inProgress.get(player.getUniqueId()).addEntity(e.getEntityType()); + User.getInstance(player).sendRawMessage("You added one " + e.getEntityType()); + return true; + } + return false; + } + + } diff --git a/src/bskyblock/addon/challenges/commands/admin/SurroundChallengeBuilder.java b/src/bskyblock/addon/challenges/commands/admin/SurroundChallengeBuilder.java new file mode 100644 index 0000000..0b85d08 --- /dev/null +++ b/src/bskyblock/addon/challenges/commands/admin/SurroundChallengeBuilder.java @@ -0,0 +1,83 @@ +package bskyblock.addon.challenges.commands.admin; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.entity.EntityType; + +import bskyblock.addon.challenges.ChallengesAddon; +import us.tastybento.bskyblock.api.commands.User; + +/** + * Enables the state of a Surrounding Challenge to be stored as it is built + * @author tastybento + * + */ +public class SurroundChallengeBuilder { + private ChallengesAddon addon; + private String name; + private User owner; + private Map reqBlocks = new HashMap<>(); + private Map reqEntities = new HashMap<>(); + + public SurroundChallengeBuilder(ChallengesAddon addon) { + this.addon = addon; + } + + SurroundChallengeBuilder name(String name) { + this.name = name; + return this; + } + + SurroundChallengeBuilder owner(User user) { + this.owner = user; + return this; + } + + SurroundChallengeBuilder addBlock(Material mat) { + reqBlocks.computeIfPresent(mat, (material, amount) -> amount + 1); + reqBlocks.putIfAbsent(mat, 1); + return this; + } + + SurroundChallengeBuilder addEntity(EntityType ent) { + reqEntities.computeIfPresent(ent, (type, amount) -> amount + 1); + reqEntities.putIfAbsent(ent, 1); + return this; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @return the owner + */ + public User getOwner() { + return owner; + } + + /** + * @return the reqBlocks + */ + public Map getReqBlocks() { + return reqBlocks; + } + + /** + * @return the reqEntities + */ + public Map getReqEntities() { + return reqEntities; + } + + public boolean build() { + return addon.getChallengesManager().createSurroundingChallenge(this); + + } + +} \ No newline at end of file diff --git a/src/bskyblock/addon/challenges/database/object/Challenges.java b/src/bskyblock/addon/challenges/database/object/Challenges.java index 0366e12..3f081c6 100644 --- a/src/bskyblock/addon/challenges/database/object/Challenges.java +++ b/src/bskyblock/addon/challenges/database/object/Challenges.java @@ -150,8 +150,9 @@ public class Challenges implements DataObject { private int searchRadius = 10; /** * Inventory slot where this challenge should be placed. 0 to 49. + * A negative value means "do not care" */ - private int slot; + private int slot = -1; /** * Take the required items from the player */ diff --git a/src/bskyblock/addon/challenges/panel/ChallengesPanels.java b/src/bskyblock/addon/challenges/panel/ChallengesPanels.java index dbd8548..d671f94 100644 --- a/src/bskyblock/addon/challenges/panel/ChallengesPanels.java +++ b/src/bskyblock/addon/challenges/panel/ChallengesPanels.java @@ -105,7 +105,11 @@ public class ChallengesPanels { }) .build(); addon.getLogger().info("requested slot" + challenge.getSlot()); - panelBuilder.addItem(challenge.getSlot(),item); + if (challenge.getSlot() >= 0) { + panelBuilder.addItem(challenge.getSlot(),item); + } else { + panelBuilder.addItem(item); + } } diff --git a/src/bskyblock/addon/challenges/panel/TryToComplete.java b/src/bskyblock/addon/challenges/panel/TryToComplete.java index 74c8e35..053080e 100644 --- a/src/bskyblock/addon/challenges/panel/TryToComplete.java +++ b/src/bskyblock/addon/challenges/panel/TryToComplete.java @@ -10,7 +10,6 @@ import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.block.Block; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; @@ -169,7 +168,7 @@ public class TryToComplete { for (int z = -searchRadius; z <= searchRadius; z++) { Material mat = user.getWorld().getBlockAt(user.getLocation().add(new Vector(x,y,z))).getType(); // Remove one - blocks.computeIfPresent(mat, (b, amount) -> amount-1); + blocks.computeIfPresent(mat, (b, amount) -> amount - 1); // Remove any that have an amount of 0 blocks.entrySet().removeIf(en -> en.getValue() <= 0); } @@ -190,7 +189,7 @@ public class TryToComplete { Map entities = new HashMap<>(map); user.getPlayer().getNearbyEntities(searchRadius, searchRadius, searchRadius).forEach(entity -> { // Look through all the nearby Entities, filtering by type - entities.computeIfPresent(entity.getType(), (reqEntity, amount) -> amount--); + entities.computeIfPresent(entity.getType(), (reqEntity, amount) -> amount - 1); entities.entrySet().removeIf(e -> e.getValue() == 0); }); if (entities.isEmpty()) {