More features for BrewModifyEvent

This commit is contained in:
Sn0wStorm 2019-10-12 16:07:02 +02:00
parent d355966d42
commit b761e83882
10 changed files with 116 additions and 34 deletions

View File

@ -1,6 +1,7 @@
package com.dre.brewery;
import com.dre.brewery.api.events.brew.BrewModifyEvent;
import com.dre.brewery.api.events.brew.BrewBeginModifyEvent;
import com.dre.brewery.api.events.brew.BrewModifiedEvent;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
@ -121,7 +122,7 @@ public class BIngredients {
cookedName = P.p.languageReader.get("Brew_Undefined");
Brew.PotionColor.CYAN.colorBrew(potionMeta, potion, true);
}
BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, BrewModifyEvent.Type.FILL);
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, potionMeta, BrewBeginModifyEvent.Type.FILL);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) {
return null;
@ -137,6 +138,8 @@ public class BIngredients {
//potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true);
brew.touch();
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, potionMeta, BrewModifiedEvent.Type.FILL);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
brew.save(potionMeta);
potion.setItemMeta(potionMeta);

View File

@ -15,6 +15,7 @@ import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
@ -144,12 +145,12 @@ public class BPlayer {
}
// Drink a brew and apply effects, etc.
public static boolean drink(Brew brew, Player player) {
public static boolean drink(Brew brew, ItemMeta meta, Player player) {
BPlayer bPlayer = get(player);
if (bPlayer == null) {
bPlayer = addPlayer(player);
}
BrewDrinkEvent drinkEvent = new BrewDrinkEvent(brew, player, bPlayer);
BrewDrinkEvent drinkEvent = new BrewDrinkEvent(brew, meta, player, bPlayer);
P.p.getServer().getPluginManager().callEvent(drinkEvent);
if (drinkEvent.isCancelled()) {
if (bPlayer.drunkeness <= 0) {

View File

@ -1,5 +1,7 @@
package com.dre.brewery;
import com.dre.brewery.api.events.brew.BrewBeginModifyEvent;
import com.dre.brewery.api.events.brew.BrewModifiedEvent;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
@ -255,6 +257,12 @@ public class BRecipe {
Brew brew = createBrew(quality);
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, potionMeta, BrewBeginModifyEvent.Type.CREATE);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) {
return null;
}
Brew.PotionColor.fromString(getColor()).colorBrew(potionMeta, potion, false);
potionMeta.setDisplayName(P.p.color("&f" + getName(quality)));
//if (!P.use1_14) {
@ -269,6 +277,8 @@ public class BRecipe {
lore.convertLore(false);
lore.addOrReplaceEffects(effects, quality);
lore.write();
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, potionMeta, BrewModifiedEvent.Type.CREATE);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
brew.touch();
brew.save(potionMeta);

View File

@ -1,6 +1,7 @@
package com.dre.brewery;
import com.dre.brewery.api.events.brew.BrewModifyEvent;
import com.dre.brewery.api.events.brew.BrewBeginModifyEvent;
import com.dre.brewery.api.events.brew.BrewModifiedEvent;
import com.dre.brewery.lore.*;
import org.bukkit.Color;
import org.bukkit.Material;
@ -22,7 +23,9 @@ import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
public class Brew {
@ -464,8 +467,7 @@ public class Brew {
// distill custom potion in given slot
public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) {
if (immutable) return;
//List<Consumer<Brew>> fcts = new ArrayList<>();
BrewModifyEvent modifyEvent = new BrewModifyEvent(this, BrewModifyEvent.Type.DISTILL);
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(this, potionMeta, BrewBeginModifyEvent.Type.DISTILL);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) return;
@ -497,6 +499,8 @@ public class Brew {
lore.updateDistillLore(colorInBrewer);
lore.write();
touch();
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(this, potionMeta, BrewModifiedEvent.Type.DISTILL);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
save(potionMeta);
slotItem.setItemMeta(potionMeta);
@ -522,11 +526,11 @@ public class Brew {
public void age(ItemStack item, float time, byte woodType) {
if (immutable) return;
BrewModifyEvent modifyEvent = new BrewModifyEvent(this, BrewModifyEvent.Type.AGE);
PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(this, potionMeta, BrewBeginModifyEvent.Type.AGE);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) return;
PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
BrewLore lore = new BrewLore(this, potionMeta);
ageTime += time;
@ -569,6 +573,8 @@ public class Brew {
}
lore.write();
touch();
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(this, potionMeta, BrewModifiedEvent.Type.AGE);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
save(potionMeta);
item.setItemMeta(potionMeta);
}

View File

@ -3,19 +3,19 @@ package com.dre.brewery.api.events.brew;
import com.dre.brewery.Brew;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.ItemMeta;
/*
* A Brew is created or modified
* A Brew is starting to be created or modified
* Usually happens on Filling from cauldron, distilling and aging.
*/
public class BrewModifyEvent extends BrewEvent implements Cancellable {
public class BrewBeginModifyEvent extends BrewEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Type type;
private boolean cancelled;
//private List<Consumer<Brew>> fcts;
public BrewModifyEvent(Brew brew, Type type) {
super(brew);
public BrewBeginModifyEvent(Brew brew, ItemMeta meta, Type type) {
super(brew, meta);
this.type = type;
}
@ -23,10 +23,6 @@ public class BrewModifyEvent extends BrewEvent implements Cancellable {
return type;
}
/*public void addModification(Consumer<Brew> predicate) {
fcts.add(predicate);
}*/
@Override
public boolean isCancelled() {
return cancelled;
@ -47,7 +43,7 @@ public class BrewModifyEvent extends BrewEvent implements Cancellable {
}
public enum Type {
//CREATE, // A new Brew is created with arbitrary ways, like the create command
CREATE, // A new Brew is created with arbitrary ways, like the create command
FILL, // Filled from a Cauldron into a new Brew
DISTILL, // Distilled in the Brewing stand
AGE, // Aged in a Barrel

View File

@ -1,14 +1,11 @@
package com.dre.brewery.api.events.brew;
import com.dre.brewery.BEffect;
import com.dre.brewery.BPlayer;
import com.dre.brewery.Brew;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.potion.PotionEffect;
import java.util.List;
import org.bukkit.inventory.meta.ItemMeta;
/*
* A Player Drinks a Brew
@ -23,8 +20,8 @@ public class BrewDrinkEvent extends BrewEvent implements Cancellable {
private int quality;
private boolean cancelled;
public BrewDrinkEvent(Brew brew, Player player, BPlayer bPlayer) {
super(brew);
public BrewDrinkEvent(Brew brew, ItemMeta meta, Player player, BPlayer bPlayer) {
super(brew, meta);
this.player = player;
this.bPlayer = bPlayer;
alc = brew.calcAlcohol();

View File

@ -2,16 +2,22 @@ package com.dre.brewery.api.events.brew;
import com.dre.brewery.Brew;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class BrewEvent extends Event {
protected final Brew brew;
protected final ItemMeta meta;
public BrewEvent(Brew brew) {
public BrewEvent(Brew brew, ItemMeta meta) {
this.brew = brew;
this.meta = meta;
}
public Brew getBrew() {
return brew;
}
public ItemMeta getItemMeta() {
return meta;
}
}

View File

@ -0,0 +1,50 @@
package com.dre.brewery.api.events.brew;
import com.dre.brewery.Brew;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
/*
* A Brew has been created or modified
* Usually happens on Filling from cauldron, distilling and aging.
* Final Modifications to the Brew or the PotionMeta can be done now
*/
public class BrewModifiedEvent extends BrewEvent {
private static final HandlerList handlers = new HandlerList();
private final Type type;
public BrewModifiedEvent(Brew brew, ItemMeta meta, Type type) {
super(brew, meta);
this.type = type;
}
public Type getType() {
return type;
}
public BrewLore getLore() {
return new BrewLore(getBrew(), (PotionMeta) getItemMeta());
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public enum Type {
CREATE, // A new Brew is created with arbitrary ways, like the create command
FILL, // Filled from a Cauldron into a new Brew
DISTILL, // Distilled in the Brewing stand
AGE, // Aged in a Barrel
UNLABEL, // Unlabeling Brew with command
STATIC, // Making Brew static with command
UNKNOWN // Unknown modification, unused
}
}

View File

@ -1,13 +1,15 @@
package com.dre.brewery.listeners;
import com.dre.brewery.*;
import com.dre.brewery.api.events.brew.BrewModifyEvent;
import com.dre.brewery.api.events.brew.BrewBeginModifyEvent;
import com.dre.brewery.api.events.brew.BrewModifiedEvent;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.Locale;
@ -478,7 +480,8 @@ public class CommandListener implements CommandExecutor {
if (hand != null) {
Brew brew = Brew.get(hand);
if (brew != null) {
BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, BrewModifyEvent.Type.STATIC);
ItemMeta meta = hand.getItemMeta();
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, meta, BrewBeginModifyEvent.Type.STATIC);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) {
return;
@ -495,7 +498,10 @@ public class CommandListener implements CommandExecutor {
p.msg(sender, p.languageReader.get("CMD_Static"));
}
brew.touch();
brew.save(hand);
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, meta, BrewModifiedEvent.Type.STATIC);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
brew.save(meta);
hand.setItemMeta(meta);
return;
}
}
@ -515,14 +521,18 @@ public class CommandListener implements CommandExecutor {
if (hand != null) {
Brew brew = Brew.get(hand);
if (brew != null) {
BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, BrewModifyEvent.Type.UNLABEL);
ItemMeta meta = hand.getItemMeta();
BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, meta, BrewBeginModifyEvent.Type.UNLABEL);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) {
return;
}
brew.unLabel(hand);
brew.touch();
brew.save(hand);
BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, meta, BrewModifiedEvent.Type.UNLABEL);
P.p.getServer().getPluginManager().callEvent(modifiedEvent);
brew.save(meta);
hand.setItemMeta(meta);
p.msg(sender, p.languageReader.get("CMD_UnLabel"));
return;
}
@ -602,7 +612,10 @@ public class CommandListener implements CommandExecutor {
}
}
if (recipe != null) {
player.getInventory().addItem(recipe.create(quality));
ItemStack item = recipe.create(quality);
if (item != null) {
player.getInventory().addItem(item);
}
} else {
p.msg(sender, p.languageReader.get("Error_NoBrewName", name));
}

View File

@ -256,7 +256,7 @@ public class PlayerListener implements Listener {
if (item.getType() == Material.POTION) {
Brew brew = Brew.get(item);
if (brew != null) {
if (!BPlayer.drink(brew, player)) {
if (!BPlayer.drink(brew, item.getItemMeta(), player)) {
event.setCancelled(true);
return;
}