Permalink
Browse files

Change give/take events and item condition to accept multiple items

Make items that don't fit into the inventory drop on the ground instead
of deleting them.
Close #21 and fix #54

Signed-off-by: Co0sh <coosheck@gmail.com>
  • Loading branch information...
Co0sh committed Feb 17, 2015
1 parent 677da12 commit 4c14dd630b55f2b561353471da4a7a98fb802d72
@@ -478,7 +478,7 @@ private void handleConditions(CommandSender sender, String[] args) {
String playerID = PlayerConverter.getID(args[1]);
sender.sendMessage(getMessage("player_condition").replaceAll("%condition%",
ConfigHandler.getString("conditions." + args[2])).replaceAll("%outcome%",
BetonQuest.condition(playerID, args[1]) + ""));
BetonQuest.condition(playerID, args[2]) + ""));
}
/**
@@ -17,6 +17,9 @@
*/
package pl.betoncraft.betonquest.conditions;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.inventory.ItemStack;
import pl.betoncraft.betonquest.api.Condition;
@@ -32,8 +35,7 @@
*/
public class ItemCondition extends Condition {
private QuestItem questItem;
private int amount = 1;
private List<Item> questItems = new ArrayList<>();
/**
* Constructor method
@@ -43,33 +45,63 @@
*/
public ItemCondition(String playerID, String instructions) {
super(playerID, instructions);
String[] parts = instructions.split(" ");
for (String part : parts) {
if (part.contains("item:")) {
questItem = new QuestItem(part.substring(5));
}
if (part.contains("amount:")) {
amount = Integer.valueOf(part.substring(7));
String items = instructions.split(" ")[1];
for (String item : items.split(",")) {
String name = item.split(":")[0];
int amount = 1;
if (item.split(":").length > 1 && item.split(":")[1].matches("\\d+")) {
amount = Integer.parseInt(item.split(":")[1]);
}
QuestItem questItem = new QuestItem(name);
questItems.add(new Item(questItem, amount));
}
}
@Override
public boolean isMet() {
ItemStack[] items = PlayerConverter.getPlayer(playerID).getInventory().getContents();
for (ItemStack item : items) {
if (item == null) {
continue;
}
if (!Utils.isItemEqual(item, questItem)) {
continue;
}
amount = amount - item.getAmount();
if (amount <= 0) {
return true;
int counter = 0;
for (Item questItem : questItems) {
ItemStack[] items = PlayerConverter.getPlayer(playerID).getInventory().getContents();
for (ItemStack item : items) {
if (item == null) {
continue;
}
if (!questItem.isItemEqual(item)) {
continue;
}
questItem.setAmount(questItem.getAmount() - item.getAmount());
if (questItem.getAmount() <= 0) {
counter++;
break;
}
}
}
if (counter == questItems.size()) {
return true;
}
return false;
}
private class Item {
private QuestItem questItem;
private int amount = 1;
public Item(QuestItem questItem, int amount) {
this.questItem = questItem;
this.amount = amount;
}
public boolean isItemEqual(ItemStack item) {
return Utils.isItemEqual(item, questItem);
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
}
@@ -65,7 +65,7 @@
* Destination version. At the end of the updating process this will be the
* current version
*/
private final String destination = "v4";
private final String destination = "v5";
public ConfigUpdater() {
String version = BetonQuest.getInstance().getConfig().getString("version", null);
@@ -162,6 +162,82 @@ private void update() {
update();
}
@SuppressWarnings("unused")
private void update_from_v4() {
Debug.info("Starting update from v4 to v5");
try {
// update all give/take events and item condition to match new parser
ConfigAccessor eventsAccessor = ConfigHandler.getConfigs().get("events");
FileConfiguration eventsConfig = eventsAccessor.getConfig();
Debug.info("Updating events!");
// check every event in configuration
for (String key : eventsConfig.getKeys(false)) {
Debug.info(" Processing " + key);
String instruction = eventsConfig.getString(key);
// if the event is of type "give" or "take" then proceed
if (instruction.startsWith("give ") || instruction.startsWith("take ")) {
String[] parts = instruction.split(" ");
Debug.info(" Found " + parts[0] + " event");
// get item's amount
int amount = 1;
for (String part : parts) {
if (part.startsWith("amount:")) {
amount = Integer.parseInt(part.substring(7));
Debug.info(" Amount is set to " + amount);
}
}
// generate new instruction
String newInstruction = parts[0] + " " + parts[1] + ((amount != 1) ? ":" + amount : "");
Debug.info(" Saving instruction '" + newInstruction + "'");
// save it
eventsConfig.set(key, newInstruction);
}
}
// when all events are converted, save the file
eventsAccessor.saveConfig();
// update all item conditions
ConfigAccessor conditionsAccessor = ConfigHandler.getConfigs().get("conditions");
FileConfiguration conditionsConfig = conditionsAccessor.getConfig();
Debug.info("Updatng conditions!");
// check every condition in configuration
for (String key : conditionsConfig.getKeys(false)) {
Debug.info(" Processing " + key);
String instruction = conditionsConfig.getString(key);
// if the condition is of type "item" then proceed
if (instruction.startsWith("item ")) {
String[] parts = instruction.split(" ");
Debug.info(" Found item condition");
// get item name and amount
String name = null;
int amount = 1;
for (String part : parts) {
if (part.startsWith("item:")) {
name = part.substring(5);
Debug.info(" Name is " + name);
} else if (part.startsWith("amount:")) {
amount = Integer.parseInt(part.substring(7));
Debug.info(" Amount is " + amount);
}
}
// generate new instruction
String newInstruction = "item " + name + ((amount != 1) ? ":" + amount : "");
Debug.info(" Saving instruction '" + newInstruction + "'");
// save it
conditionsConfig.set(key, newInstruction);
}
}
// when all conditions are converted, save the file
conditionsAccessor.saveConfig();
Debug.broadcast("Converted give/take events and item conditions to new format!");
} catch (Exception e) {
e.printStackTrace();
displayError();
}
config.set("version", "v5");
instance.saveConfig();
Debug.info("Conversion to v5 finished!");
}
@SuppressWarnings("unused")
private void update_from_v3() {
Debug.info("Starting update from v3 to v4");
@@ -431,12 +507,7 @@ private void update_from_v2() {
// the version wouldn't get changed and updater would fall into
// an infinite loop of endless exceptiorns
e.printStackTrace();
Debug.error("There was an error during updating process! (you don't say?) Please "
+ "downgrade to the previous working version of the plugin and restore your "
+ "configuration from the backup. Don't forget to send this error to the developer"
+ ", so he can fix it! Sorry for inconvenience, here's the link:"
+ " <https://github.com/Co0sh/BetonQuest/issues> and a cookie: "
+ "<http://i.imgur.com/iR4UMH5.png>");
displayError();
}
// set v3 version
config.set("version", "v3");
@@ -949,4 +1020,13 @@ private void addChangelog() {
}
}
private void displayError() {
Debug.error("There was an error during updating process! (you don't say?) Please "
+ "downgrade to the previous working version of the plugin and restore your "
+ "configuration from the backup. Don't forget to send this error to the developer"
+ ", so he can fix it! Sorry for inconvenience, here's the link:"
+ " <https://github.com/Co0sh/BetonQuest/issues> and a cookie: "
+ "<http://i.imgur.com/iR4UMH5.png>");
}
}
@@ -17,8 +17,11 @@
*/
package pl.betoncraft.betonquest.events;
import java.util.HashMap;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
@@ -50,67 +53,72 @@ public GiveEvent(String playerID, String instructions) {
super(playerID, instructions);
String[] parts = instructions.split(" ");
questItem = new QuestItem(parts[1]);
for (String part : parts) {
if (part.contains("amount:")) {
amount = Integer.valueOf(part.substring(7));
}
}
while (amount > 0) {
int stackSize;
if (amount > 64) {
stackSize = 64;
} else {
stackSize = amount;
}
byte data;
if (questItem.getData() < 0) {
data = 0;
} else {
data = (byte) questItem.getData();
String[] items = parts[1].split(",");
for (String rawItem : items) {
String itemName = rawItem.split(":")[0];
if (rawItem.split(":").length > 1) {
amount = Integer.parseInt(rawItem.split(":")[1]);
}
ItemStack item = new ItemStack(Material.matchMaterial(questItem.getMaterial()),
stackSize, data);
ItemMeta meta = item.getItemMeta();
if (questItem.getName() != null) {
meta.setDisplayName(questItem.getName());
}
meta.setLore(questItem.getLore());
for (String enchant : questItem.getEnchants().keySet()) {
meta.addEnchant(Enchantment.getByName(enchant), questItem.getEnchants()
.get(enchant), true);
}
if (Material.matchMaterial(questItem.getMaterial()).equals(Material.WRITTEN_BOOK)) {
BookMeta bookMeta = (BookMeta) meta;
if (questItem.getAuthor() != null) {
bookMeta.setAuthor(questItem.getAuthor());
questItem = new QuestItem(itemName);
while (amount > 0) {
int stackSize;
if (amount > 64) {
stackSize = 64;
} else {
bookMeta.setAuthor(ConfigHandler.getString("messages."
+ ConfigHandler.getString("config.language") + ".unknown_author"));
}
if (questItem.getText() != null) {
bookMeta.setPages(Utils.pagesFromString(questItem.getText(), false));
stackSize = amount;
}
if (questItem.getTitle() != null) {
bookMeta.setTitle(questItem.getTitle());
byte data;
if (questItem.getData() < 0) {
data = 0;
} else {
bookMeta.setTitle(ConfigHandler.getString("messages."
+ ConfigHandler.getString("config.language") + ".unknown_title"));
data = (byte) questItem.getData();
}
item.setItemMeta(bookMeta);
}
if (Material.matchMaterial(questItem.getMaterial()).equals(Material.POTION)) {
PotionMeta potionMeta = (PotionMeta) meta;
for (PotionEffect effect : questItem.getEffects()) {
potionMeta.addCustomEffect(effect, true);
ItemStack item = new ItemStack(Material.matchMaterial(questItem.getMaterial()),
stackSize, data);
ItemMeta meta = item.getItemMeta();
if (questItem.getName() != null) {
meta.setDisplayName(questItem.getName());
}
meta.setLore(questItem.getLore());
for (String enchant : questItem.getEnchants().keySet()) {
meta.addEnchant(Enchantment.getByName(enchant), questItem.getEnchants()
.get(enchant), true);
}
if (Material.matchMaterial(questItem.getMaterial()).equals(Material.WRITTEN_BOOK)) {
BookMeta bookMeta = (BookMeta) meta;
if (questItem.getAuthor() != null) {
bookMeta.setAuthor(questItem.getAuthor());
} else {
bookMeta.setAuthor(ConfigHandler.getString("messages."
+ ConfigHandler.getString("config.language") + ".unknown_author"));
}
if (questItem.getText() != null) {
bookMeta.setPages(Utils.pagesFromString(questItem.getText(), false));
}
if (questItem.getTitle() != null) {
bookMeta.setTitle(questItem.getTitle());
} else {
bookMeta.setTitle(ConfigHandler.getString("messages."
+ ConfigHandler.getString("config.language") + ".unknown_title"));
}
item.setItemMeta(bookMeta);
}
if (Material.matchMaterial(questItem.getMaterial()).equals(Material.POTION)) {
PotionMeta potionMeta = (PotionMeta) meta;
for (PotionEffect effect : questItem.getEffects()) {
potionMeta.addCustomEffect(effect, true);
}
item.setItemMeta(potionMeta);
}
item.setItemMeta(potionMeta);
item.setItemMeta(meta);
Player player = PlayerConverter.getPlayer(playerID);
HashMap<Integer, ItemStack> left = player
.getInventory().addItem(item);
for (Integer leftNumber : left.keySet()) {
player.getWorld().dropItem(player.getLocation(), left.get(leftNumber));
}
amount = amount - stackSize;
}
item.setItemMeta(meta);
PlayerConverter.getPlayer(playerID).getInventory().addItem(item);
amount = amount - stackSize;
}
}
}
Oops, something went wrong.

0 comments on commit 4c14dd6

Please sign in to comment.