/br -> /brew, added Persistence

This commit is contained in:
Sn0wStorm 2014-06-03 23:10:47 +02:00
parent a4b9b08f82
commit 28dcfbe5d2
8 changed files with 140 additions and 66 deletions

View File

@ -6,7 +6,7 @@ softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention]
commands:
brewery:
description: Command for Administration
aliases: [brew,br]
aliases: [brew]
permissions:
# -- Groups --
# User
@ -39,6 +39,7 @@ permissions:
brewery.cmd.player: true
brewery.cmd.copy: true
brewery.cmd.delete: true
brewery.cmd.persist: true
brewery.cmd.reload: true
# *
brewery.*:

View File

@ -77,39 +77,34 @@ public class BPlayer {
return org.bukkit.Bukkit.getPlayerExact(name);
}
// returns true if drinking was successful
public static boolean drink(int uid, Player player) {
Brew brew = Brew.get(uid);
if (brew != null) {
int brewAlc = brew.calcAlcohol();
if (brewAlc == 0) {
//no alcohol so we dont need to add a BPlayer
addBrewEffects(brew, player);
return true;
}
BPlayer bPlayer = get(player.getName());
if (bPlayer == null) {
bPlayer = new BPlayer();
players.put(player.getName(), bPlayer);
}
bPlayer.drunkeness += brewAlc;
if (brew.getQuality() > 0) {
bPlayer.quality += brew.getQuality() * brewAlc;
} else {
bPlayer.quality += brewAlc;
}
if (bPlayer.drunkeness <= 100) {
addBrewEffects(brew, player);
addQualityEffects(brew.getQuality(), brewAlc, player);
} else {
bPlayer.drinkCap(player);
}
return true;
// Drink a brew and apply effects, etc.
public static void drink(Brew brew, Player player) {
int brewAlc = brew.calcAlcohol();
if (brewAlc == 0) {
//no alcohol so we dont need to add a BPlayer
addBrewEffects(brew, player);
return;
}
BPlayer bPlayer = get(player.getName());
if (bPlayer == null) {
bPlayer = new BPlayer();
players.put(player.getName(), bPlayer);
}
bPlayer.drunkeness += brewAlc;
if (brew.getQuality() > 0) {
bPlayer.quality += brew.getQuality() * brewAlc;
} else {
bPlayer.quality += brewAlc;
}
if (bPlayer.drunkeness <= 100) {
addBrewEffects(brew, player);
addQualityEffects(brew.getQuality(), brewAlc, player);
} else {
bPlayer.drinkCap(player);
}
return false;
}
// Player has drunken too much

View File

@ -27,6 +27,7 @@ public class Brew {
private float wood;
private BRecipe currentRecipe;
private boolean unlabeled;
private boolean persistent;
public Brew(int uid, BIngredients ingredients) {
this.ingredients = ingredients;
@ -42,7 +43,7 @@ public class Brew {
}
// loading from file
public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, Boolean unlabeled) {
public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent) {
potions.put(uid, this);
this.ingredients = ingredients;
this.quality = quality;
@ -50,6 +51,7 @@ public class Brew {
this.ageTime = ageTime;
this.wood = wood;
this.unlabeled = unlabeled;
this.persistent = persistent;
setRecipeFromString(recipe);
}
@ -102,11 +104,6 @@ public class Brew {
return 0;
}
// remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!)
public static void remove(ItemStack item) {
potions.remove(getUID(item));
}
// generate an UID
public static int generateUID() {
int uid = -2;
@ -168,6 +165,13 @@ public class Brew {
return brew;
}
// remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!)
public void remove(ItemStack item) {
if (!persistent) {
potions.remove(getUID(item));
}
}
// calculate alcohol from recipe
public int calcAlcohol() {
if (quality == 0) {
@ -259,6 +263,20 @@ public class Brew {
unlabeled = true;
}
public boolean isPersistent() {
return persistent;
}
// Make a potion persistent to not delete it when drinking it
public void makePersistent() {
persistent = true;
}
// Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it
public void removePersistence() {
persistent = false;
}
// Distilling section ---------------
// distill all custom potions in the brewer
@ -572,6 +590,9 @@ public class Brew {
if (brew.unlabeled) {
idConfig.set("unlabeled", true);
}
if (brew.persistent) {
idConfig.set("persist", true);
}
// save the ingredients
idConfig.set("ingId", brew.ingredients.save(config.getParent()));
}

View File

@ -82,11 +82,15 @@ public class LanguageReader {
defaults.put("CMD_Info_NotDrunk", "&v1 is not drunk");
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_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_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_CopyNotPersistent", "&eThese copies of this Brew will not be persistent!");
/* Error */
defaults.put("Error_UnknownCommand", "Unknown Command");
defaults.put("Error_ShowHelp", "Use &6/br 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_ItemNotPotion", "&cThe item in your hand could not be identified as a potion!");
defaults.put("Error_Recipeload", "&cNot all recipes could be restored: More information in the server log!");
@ -102,20 +106,21 @@ public class LanguageReader {
defaults.put("Perms_NoCauldronFill", "&cYou don't have permissions to fill bottles from this cauldron!");
/* Help */
defaults.put("Help_Help", "&6/br help <Page> &9Shows a specific help-page");
defaults.put("Help_Player", "&6/br <Player> <%Drunkeness> <Quality>&9 Sets Drunkeness (and Quality) of a Player");
defaults.put("Help_Info", "&6/br info&9 Displays your current Drunkeness and Quality");
defaults.put("Help_UnLabel", "&6/br unlabel &9Removes the detailled label of a potion");
defaults.put("Help_Copy", "&6/br copy <Quanitiy>&9 Copies the potion in your hand");
defaults.put("Help_Delete", "&6/br delete &9Deletes the potion in your hand");
defaults.put("Help_InfoOther", "&6/br info <Player>&9 Displays the current Drunkeness and Quality of <Player>");
defaults.put("Help_Wakeup", "&6/br wakeup list <Page>&9 Lists all wakeup points");
defaults.put("Help_WakeupList", "&6/br wakeup list <Page> <World>&9 Lists all wakeup points of <world>");
defaults.put("Help_WakeupCheck", "&6/br wakeup check &9Teleports to all wakeup points");
defaults.put("Help_WakeupCheckSpecific", "&6/br wakeup check <id> &9Teleports to the wakeup point with <id>");
defaults.put("Help_WakeupAdd", "&6/br wakeup add &9Adds a wakeup point at your current position");
defaults.put("Help_WakeupRemove", "&6/br wakeup remove <id> &9Removes the wakeup point with <id>");
defaults.put("Help_Reload", "&6/br reload &9Reload config");
defaults.put("Help_Help", "&6/brew help <Page> &9Shows a specific help-page");
defaults.put("Help_Player", "&6/brew <Player> <%Drunkeness> <Quality>&9 Sets Drunkeness (and Quality) of a Player");
defaults.put("Help_Info", "&6/brew info&9 Displays your current Drunkeness and Quality");
defaults.put("Help_UnLabel", "&6/brew unlabel &9Removes the detailled label of a potion");
defaults.put("Help_Copy", "&6/brew copy <Quanitiy>&9 Copies the potion in your hand");
defaults.put("Help_Delete", "&6/brew delete &9Deletes the potion in your hand");
defaults.put("Help_InfoOther", "&6/brew info <Player>&9 Displays the current Drunkeness and Quality of <Player>");
defaults.put("Help_Wakeup", "&6/brew wakeup list <Page>&9 Lists all wakeup points");
defaults.put("Help_WakeupList", "&6/brew wakeup list <Page> <World>&9 Lists all wakeup points of <world>");
defaults.put("Help_WakeupCheck", "&6/brew wakeup check &9Teleports to all wakeup points");
defaults.put("Help_WakeupCheckSpecific", "&6/brew wakeup check <id> &9Teleports to the wakeup point with <id>");
defaults.put("Help_WakeupAdd", "&6/brew wakeup add &9Adds a wakeup point at your current position");
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_Persist", "&6/brew persist &9Make Brew persistent -> copyable by any plugin and technique");
/* Etc. */
defaults.put("Etc_Usage", "Usage:");

View File

@ -307,9 +307,10 @@ public class P extends JavaPlugin {
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
float wood = (float) section.getDouble(uid + ".wood", -1.0);
String recipe = section.getString(uid + ".recipe", null);
Boolean unlabeled = section.getBoolean(uid + ".unlabeled", false);
boolean unlabeled = section.getBoolean(uid + ".unlabeled", false);
boolean persistent = section.getBoolean(uid + ".persist", false);
new Brew(parseInt(uid), ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled);
new Brew(parseInt(uid), ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, persistent);
}
}

View File

@ -86,6 +86,14 @@ public class CommandListener implements CommandExecutor {
p.msg(sender, p.languageReader.get("Error_NoPermissions"));
}
} else if (cmd.equalsIgnoreCase("persist") || cmd.equalsIgnoreCase("persistent")) {
if (sender.hasPermission("brewery.cmd.persist")) {
cmdPersist(sender);
} else {
p.msg(sender, p.languageReader.get("Error_NoPermissions"));
}
} else if (cmd.equalsIgnoreCase("unlabel")) {
if (sender.hasPermission("brewery.cmd.unlabel")) {
@ -180,6 +188,10 @@ public class CommandListener implements CommandExecutor {
cmds.add(p.languageReader.get("Help_Reload"));
}
if (sender.hasPermission("brewery.cmd.persist")) {
cmds.add(p.languageReader.get("Help_Persist"));
}
return cmds;
}
@ -326,6 +338,9 @@ public class CommandListener implements CommandExecutor {
}
count--;
}
if (brew.isPersistent()) {
p.msg(sender, p.languageReader.get("CMD_CopyNotPersistent"));
}
return;
}
}
@ -344,9 +359,39 @@ public class CommandListener implements CommandExecutor {
Player player = (Player) sender;
ItemStack hand = player.getItemInHand();
if (hand != null) {
if (Brew.get(hand) != null) {
Brew.remove(hand);
player.setItemInHand(new ItemStack(0));
Brew brew = Brew.get(hand);
if (brew != null) {
if (brew.isPersistent()) {
p.msg(sender, p.languageReader.get("CMD_PersistRemove"));
} else {
brew.remove(hand);
player.setItemInHand(new ItemStack(0));
}
return;
}
}
p.msg(sender, p.languageReader.get("Error_ItemNotPotion"));
} else {
p.msg(sender, p.languageReader.get("Error_PlayerCommand"));
}
}
public void cmdPersist(CommandSender sender) {
if (sender instanceof Player) {
Player player = (Player) sender;
ItemStack hand = player.getItemInHand();
if (hand != null) {
Brew brew = Brew.get(hand);
if (brew != null) {
if (brew.isPersistent()) {
brew.removePersistence();
p.msg(sender, p.languageReader.get("CMD_UnPersist"));
} else {
brew.makePersistent();
p.msg(sender, p.languageReader.get("CMD_Persistent"));
}
return;
}
}

View File

@ -26,7 +26,10 @@ public class EntityListener implements Listener {
public void onItemDespawn(ItemDespawnEvent event) {
ItemStack item = event.getEntity().getItemStack();
if (item.getTypeId() == 373) {
Brew.remove(item);
Brew brew = Brew.get(item);
if (brew != null) {
brew.remove(item);
}
}
}
@ -37,7 +40,10 @@ public class EntityListener implements Listener {
if (entity instanceof Item) {
ItemStack item = ((Item) entity).getItemStack();
if (item.getTypeId() == 373) {
Brew.remove(item);
Brew brew = Brew.get(item);
if (brew != null) {
brew.remove(item);
}
}
}
}

View File

@ -155,11 +155,11 @@ public class PlayerListener implements Listener {
ItemStack item = event.getItem();
if (item != null) {
if (item.getType() == Material.POTION) {
if (item.hasItemMeta()) {
if (BPlayer.drink(Brew.getUID(item), player)) {
if (player.getGameMode() != org.bukkit.GameMode.CREATIVE) {
Brew.remove(item);
}
Brew brew = Brew.get(item);
if (brew != null) {
BPlayer.drink(brew, player);
if (player.getGameMode() != org.bukkit.GameMode.CREATIVE) {
brew.remove(item);
}
}
} else if (BPlayer.drainItems.containsKey(item.getType())) {