mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-28 05:25:21 +01:00
WIP improved surrounding challenges.
This commit is contained in:
parent
22389824a7
commit
763a8de8e4
@ -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!"
|
||||
|
@ -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<Challenges> chHandler;
|
||||
private AbstractDatabaseHandler<ChallengeLevels> 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<Material, Integer> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<UUID, Map<Material, Integer>> blocks = new HashMap<>();
|
||||
private Map<UUID, String> name = new HashMap<>();
|
||||
HashMap<UUID,SurroundChallengeBuilder> 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<Material, Integer> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<Material, Integer> reqBlocks = new HashMap<>();
|
||||
private Map<EntityType, Integer> 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<Material, Integer> getReqBlocks() {
|
||||
return reqBlocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the reqEntities
|
||||
*/
|
||||
public Map<EntityType, Integer> getReqEntities() {
|
||||
return reqEntities;
|
||||
}
|
||||
|
||||
public boolean build() {
|
||||
return addon.getChallengesManager().createSurroundingChallenge(this);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<EntityType, Integer> 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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user