mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-30 22:44:08 +01:00
WIP improved surrounding challenges.
This commit is contained in:
parent
22389824a7
commit
763a8de8e4
@ -30,6 +30,7 @@ challenges:
|
|||||||
error:
|
error:
|
||||||
island-level: "&cYour island must be level [level] to complete this challenge!"
|
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!"
|
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-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-enough-items: "&cYou do not have enough [items] to complete this challenge!"
|
||||||
not-on-island: "&cYou must be on your island to do that!"
|
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.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
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.ChallengeLevels;
|
||||||
import bskyblock.addon.challenges.database.object.Challenges;
|
import bskyblock.addon.challenges.database.object.Challenges;
|
||||||
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
|
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
|
||||||
@ -393,16 +394,18 @@ public class ChallengesManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createSurroundingChallenge(String string, Map<Material, Integer> map) {
|
public boolean createSurroundingChallenge(SurroundChallengeBuilder inProgress) {
|
||||||
if (map.isEmpty()) {
|
if (inProgress.getReqBlocks().isEmpty() && inProgress.getReqEntities().isEmpty()) {
|
||||||
return;
|
inProgress.getOwner().sendMessage("challenges.error.no-items-clicked");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
Challenges newChallenge = new Challenges();
|
Challenges newChallenge = new Challenges();
|
||||||
newChallenge.setChallengeType(ChallengeType.SURROUNDING);
|
newChallenge.setChallengeType(ChallengeType.SURROUNDING);
|
||||||
newChallenge.setFriendlyName(string);
|
newChallenge.setFriendlyName(inProgress.getName());
|
||||||
newChallenge.setDeployed(true);
|
newChallenge.setDeployed(true);
|
||||||
newChallenge.setRequiredBlocks(map);
|
newChallenge.setRequiredBlocks(inProgress.getReqBlocks());
|
||||||
newChallenge.setUniqueId(string);
|
newChallenge.setRequiredEntities(inProgress.getReqEntities());
|
||||||
|
newChallenge.setUniqueId(inProgress.getName());
|
||||||
newChallenge.setIcon(new ItemStack(Material.ARMOR_STAND));
|
newChallenge.setIcon(new ItemStack(Material.ARMOR_STAND));
|
||||||
newChallenge.setFreeChallenge(true);
|
newChallenge.setFreeChallenge(true);
|
||||||
newChallenge.setLevel(FREE);
|
newChallenge.setLevel(FREE);
|
||||||
@ -412,10 +415,9 @@ public class ChallengesManager {
|
|||||||
chHandler.saveObject(newChallenge);
|
chHandler.saveObject(newChallenge);
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
||||||
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
||||||
// TODO Auto-generated catch block
|
addon.getLogger().severe("Could not save challenge!");
|
||||||
e.printStackTrace();
|
return false;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,17 @@ package bskyblock.addon.challenges.commands.admin;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
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.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
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.PlayerInteractEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
@ -23,8 +25,7 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
|||||||
|
|
||||||
|
|
||||||
private ChallengesAddon addon;
|
private ChallengesAddon addon;
|
||||||
private Map<UUID, Map<Material, Integer>> blocks = new HashMap<>();
|
HashMap<UUID,SurroundChallengeBuilder> inProgress = new HashMap<>();
|
||||||
private Map<UUID, String> name = new HashMap<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Admin command to make surrounding challenges
|
* 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
|
// 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");
|
user.sendRawMessage("Hit things to add them to the list of things required. Right click when done");
|
||||||
blocks.computeIfAbsent(user.getUniqueId(), k -> new HashMap<>());
|
inProgress.put(user.getUniqueId(), new SurroundChallengeBuilder(addon).owner(user).name(args.get(0)));
|
||||||
name.put(user.getUniqueId(), args.get(0));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onBlockBreak(BlockBreakEvent e) {
|
public void onBlockBreak(BlockBreakEvent e) {
|
||||||
if (blocks.containsKey(e.getPlayer().getUniqueId())) {
|
e.setCancelled(inProgress.containsKey(e.getPlayer().getUniqueId()) ? true : false);
|
||||||
e.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onPlayerQuit(PlayerQuitEvent e) {
|
public void onPlayerQuit(PlayerQuitEvent e) {
|
||||||
blocks.remove(e.getPlayer().getUniqueId());
|
inProgress.remove(e.getPlayer().getUniqueId());
|
||||||
name.remove(e.getPlayer().getUniqueId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onPlayerInteract(PlayerInteractEvent e) {
|
public boolean onPlayerInteract(PlayerInteractEvent e) {
|
||||||
if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
|
if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
|
||||||
addon.getLogger().info("DEBUG: left click");
|
addon.getLogger().info("DEBUG: left click");
|
||||||
if (blocks.containsKey(e.getPlayer().getUniqueId())) {
|
if (inProgress.containsKey(e.getPlayer().getUniqueId())) {
|
||||||
// Prevent damage
|
// Prevent damage
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
Map<Material, Integer> blockMap = blocks.get(e.getPlayer().getUniqueId());
|
inProgress.get(e.getPlayer().getUniqueId()).addBlock(e.getClickedBlock().getType());
|
||||||
blockMap.computeIfPresent(e.getClickedBlock().getType(), (state, amount) -> amount++);
|
|
||||||
blockMap.putIfAbsent(e.getClickedBlock().getType(), 1);
|
|
||||||
blocks.put(e.getPlayer().getUniqueId(), blockMap);
|
|
||||||
User.getInstance(e.getPlayer()).sendRawMessage("You added one " + 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)) {
|
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
|
||||||
addon.getLogger().info("DEBUG: right click");
|
addon.getLogger().info("DEBUG: right click");
|
||||||
if (blocks.containsKey(e.getPlayer().getUniqueId())) {
|
return finished(e, 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 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;
|
private int searchRadius = 10;
|
||||||
/**
|
/**
|
||||||
* Inventory slot where this challenge should be placed. 0 to 49.
|
* 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
|
* Take the required items from the player
|
||||||
*/
|
*/
|
||||||
|
@ -105,7 +105,11 @@ public class ChallengesPanels {
|
|||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
addon.getLogger().info("requested slot" + challenge.getSlot());
|
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.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
@ -169,7 +168,7 @@ public class TryToComplete {
|
|||||||
for (int z = -searchRadius; z <= searchRadius; z++) {
|
for (int z = -searchRadius; z <= searchRadius; z++) {
|
||||||
Material mat = user.getWorld().getBlockAt(user.getLocation().add(new Vector(x,y,z))).getType();
|
Material mat = user.getWorld().getBlockAt(user.getLocation().add(new Vector(x,y,z))).getType();
|
||||||
// Remove one
|
// Remove one
|
||||||
blocks.computeIfPresent(mat, (b, amount) -> amount-1);
|
blocks.computeIfPresent(mat, (b, amount) -> amount - 1);
|
||||||
// Remove any that have an amount of 0
|
// Remove any that have an amount of 0
|
||||||
blocks.entrySet().removeIf(en -> en.getValue() <= 0);
|
blocks.entrySet().removeIf(en -> en.getValue() <= 0);
|
||||||
}
|
}
|
||||||
@ -190,7 +189,7 @@ public class TryToComplete {
|
|||||||
Map<EntityType, Integer> entities = new HashMap<>(map);
|
Map<EntityType, Integer> entities = new HashMap<>(map);
|
||||||
user.getPlayer().getNearbyEntities(searchRadius, searchRadius, searchRadius).forEach(entity -> {
|
user.getPlayer().getNearbyEntities(searchRadius, searchRadius, searchRadius).forEach(entity -> {
|
||||||
// Look through all the nearby Entities, filtering by type
|
// 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);
|
entities.entrySet().removeIf(e -> e.getValue() == 0);
|
||||||
});
|
});
|
||||||
if (entities.isEmpty()) {
|
if (entities.isEmpty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user