"Create" Command

This commit is contained in:
Sn0wStorm 2014-08-25 20:42:40 +02:00
parent 70177e9f9e
commit 620ab93b6c
4 changed files with 125 additions and 3 deletions

View File

@ -37,6 +37,7 @@ permissions:
brewery.cmd.info: true brewery.cmd.info: true
brewery.cmd.infoOther: true brewery.cmd.infoOther: true
brewery.cmd.player: true brewery.cmd.player: true
brewery.cmd.create: true
brewery.cmd.copy: true brewery.cmd.copy: true
brewery.cmd.delete: true brewery.cmd.delete: true
brewery.cmd.persist: true brewery.cmd.persist: true

View File

@ -8,6 +8,8 @@ import java.util.HashMap;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffectType;
public class BRecipe { public class BRecipe {
@ -173,6 +175,38 @@ public class BRecipe {
return recipeItem.getDurability() == -1 || recipeItem.getDurability() == usedItem.getDurability(); return recipeItem.getDurability() == -1 || recipeItem.getDurability() == usedItem.getDurability();
} }
// Create a Potion from this Recipe with best values. Quality can be set, but will reset to 10 if put in a barrel
public ItemStack create(int quality) {
ItemStack potion = new ItemStack(Material.POTION);
PotionMeta potionMeta = (PotionMeta) potion.getItemMeta();
int uid = Brew.generateUID();
ArrayList<ItemStack> list = new ArrayList<ItemStack>(ingredients.size());
for (ItemStack item : ingredients) {
if (item.getDurability() == -1) {
list.add(new ItemStack(item.getType(), item.getAmount()));
} else {
list.add(item.clone());
}
}
BIngredients bIngredients = new BIngredients(list, cookingTime);
Brew brew = new Brew(uid, bIngredients, quality, distillruns, getAge(), wood, getName(5), false, false);
potion.setDurability(Brew.PotionColor.valueOf(getColor()).getColorId(false));
potionMeta.setDisplayName(P.p.color("&f" + getName(quality)));
// This effect stores the UID in its Duration
potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true);
brew.convertLore(potionMeta, false);
Brew.addOrReplaceEffects(potionMeta, effects);
potion.setItemMeta(potionMeta);
return potion;
}
// true if name and ingredients are correct // true if name and ingredients are correct
public boolean isValid() { public boolean isValid() {
return (name != null && ingredients != null && !ingredients.isEmpty()); return (name != null && ingredients != null && !ingredients.isEmpty());
@ -206,6 +240,16 @@ public class BRecipe {
} }
} }
// If one of the quality names equalIgnoreCase given name
public boolean hasName(String name) {
for (String test : this.name) {
if (test.equalsIgnoreCase(name)) {
return true;
}
}
return false;
}
public int getCookingTime() { public int getCookingTime() {
return cookingTime; return cookingTime;
} }

View File

@ -83,8 +83,8 @@ public class LanguageReader {
defaults.put("CMD_Info_Drunk", "&v1 is &6&v2% &fdrunk, with a quality of &6&v3"); defaults.put("CMD_Info_Drunk", "&v1 is &6&v2% &fdrunk, with a quality of &6&v3");
defaults.put("CMD_UnLabel", "&aLabel removed!"); defaults.put("CMD_UnLabel", "&aLabel removed!");
defaults.put("CMD_Persistent", "&aPotion is now Persistent and may be copied like any other item. You can remove the persistence with the same command."); defaults.put("CMD_Persistent", "&aPotion is now Persistent and may be copied like any other item. You can remove the persistence with the same command.");
defaults.put("CMD_PersistRemove", "&cThis Brew is Persistent. Deleting it would render every copy of it not made with '/brew copy' useless. To proceed, remove the persistence before deleting."); defaults.put("CMD_PersistRemove", "&cThis Brew is Persistent. Deleting it would render every copy of it NOT made with '/brew copy' useless. To proceed, remove the persistence before deleting.");
defaults.put("CMD_UnPersist", "&aPersistence Removed. &eEvery Potential copy not made with '/brew copy' could become useless now!"); defaults.put("CMD_UnPersist", "&aPersistence Removed. &eEvery Potential copy NOT made with '/brew copy' could become useless now!");
defaults.put("CMD_Copy_Error", "&6&v1 &cPotions did not fit into your inventory"); defaults.put("CMD_Copy_Error", "&6&v1 &cPotions did not fit into your inventory");
defaults.put("CMD_CopyNotPersistent", "&eThese copies of this Brew will not be persistent!"); defaults.put("CMD_CopyNotPersistent", "&eThese copies of this Brew will not be persistent!");
@ -93,6 +93,7 @@ public class LanguageReader {
defaults.put("Error_ShowHelp", "Use &6/brew help &fto display the help"); defaults.put("Error_ShowHelp", "Use &6/brew help &fto display the help");
defaults.put("Error_PlayerCommand", "&cThis command can only be executed as a player!"); defaults.put("Error_PlayerCommand", "&cThis command can only be executed as a player!");
defaults.put("Error_ItemNotPotion", "&cThe item in your hand could not be identified as a potion!"); defaults.put("Error_ItemNotPotion", "&cThe item in your hand could not be identified as a potion!");
defaults.put("Error_NoBrewName", "&cNo Recipe with Name: '&v1&c' found!");
defaults.put("Error_Recipeload", "&cNot all recipes could be restored: More information in the server log!"); defaults.put("Error_Recipeload", "&cNot all recipes could be restored: More information in the server log!");
defaults.put("Error_ConfigUpdate", "Unknown Brewery config version: v&v1, config was not updated!"); defaults.put("Error_ConfigUpdate", "Unknown Brewery config version: v&v1, config was not updated!");
@ -121,6 +122,7 @@ public class LanguageReader {
defaults.put("Help_WakeupRemove", "&6/brew wakeup remove <id> &9Removes the wakeup point with <id>"); defaults.put("Help_WakeupRemove", "&6/brew wakeup remove <id> &9Removes the wakeup point with <id>");
defaults.put("Help_Reload", "&6/brew reload &9Reload config"); defaults.put("Help_Reload", "&6/brew reload &9Reload config");
defaults.put("Help_Persist", "&6/brew persist &9Make Brew persistent -> copyable by any plugin and technique"); defaults.put("Help_Persist", "&6/brew persist &9Make Brew persistent -> copyable by any plugin and technique");
defaults.put("Help_Create", "&6/brew create <Recipe> <Quality> &9Create a Brew with optional quality (1-10)");
/* Etc. */ /* Etc. */
defaults.put("Etc_Usage", "Usage:"); defaults.put("Etc_Usage", "Usage:");

View File

@ -9,6 +9,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.dre.brewery.BIngredients;
import com.dre.brewery.BRecipe;
import com.dre.brewery.P; import com.dre.brewery.P;
import com.dre.brewery.Wakeup; import com.dre.brewery.Wakeup;
import com.dre.brewery.BPlayer; import com.dre.brewery.BPlayer;
@ -49,7 +51,11 @@ public class CommandListener implements CommandExecutor {
} else if (cmd.equalsIgnoreCase("create")) { } else if (cmd.equalsIgnoreCase("create")) {
//TODO: create command if (sender.hasPermission("brewery.cmd.create")) {
cmdCreate(sender, args);
} else {
p.msg(sender, p.languageReader.get("Error_NoPermissions"));
}
} else if (cmd.equalsIgnoreCase("info")) { } else if (cmd.equalsIgnoreCase("info")) {
@ -193,6 +199,10 @@ public class CommandListener implements CommandExecutor {
cmds.add(p.languageReader.get("Help_Persist")); cmds.add(p.languageReader.get("Help_Persist"));
} }
if (sender.hasPermission("brewery.cmd.create")) {
cmds.add(p.languageReader.get("Help_Create"));
}
return cmds; return cmds;
} }
@ -436,4 +446,69 @@ public class CommandListener implements CommandExecutor {
} }
public void cmdCreate(CommandSender sender, String[] args) {
if (sender instanceof Player) {
if (args.length < 2) {
p.msg(sender, p.languageReader.get("Etc_Usage"));
p.msg(sender, p.languageReader.get("Help_Create"));
return;
}
int quality = 10;
boolean hasQuality = false;
if (args.length > 2) {
quality = p.parseInt(args[args.length - 1]);
if (quality > 0 && quality <= 10) {
hasQuality = true;
} else {
quality = 10;
}
}
int stringLength;
if (hasQuality) {
stringLength = args.length - 2;
} else {
stringLength = args.length - 1;
}
String name;
if (stringLength > 1) {
StringBuilder builder = new StringBuilder(args[1]);
for (int i = 2; i < stringLength + 1; i++) {
builder.append(" ").append(args[i]);
}
name = builder.toString();
} else {
name = args[1];
}
Player player = (Player) sender;
if (player.getInventory().firstEmpty() == -1) {
p.msg(sender, p.languageReader.get("CMD_Copy_Error", "1"));
return;
}
BRecipe recipe = null;
for (BRecipe r : BIngredients.recipes) {
if (r.hasName(name)) {
recipe = r;
break;
}
}
if (recipe != null) {
player.getInventory().addItem(recipe.create(quality));
} else {
p.msg(sender, p.languageReader.get("Error_NoBrewName", name));
}
} else {
p.msg(sender, p.languageReader.get("Error_PlayerCommand"));
}
}
} }