Started api and events

This commit is contained in:
Sn0wStorm
2019-10-11 20:44:43 +02:00
parent 49fb0e3d82
commit 519c4821d9
32 changed files with 1400 additions and 264 deletions
+150
View File
@@ -0,0 +1,150 @@
package com.dre.brewery.api;
import com.dre.brewery.BCauldron;
import com.dre.brewery.BRecipe;
import com.dre.brewery.Barrel;
import com.dre.brewery.Brew;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class BreweryApi {
/*
* Convenience methods to get common objects or do common things
*/
/*
* Remove any data that this Plugin may associate with the given Block
* Currently Cauldrons and Barrels (Cauldron, Wood, Woodstairs, Fence, Sign)
* Does not remove any actual Blocks
* Returns true if anything was removed
*/
public static boolean removeAny(Block block) {
if (removeCauldron(block)) return true;
return removeBarrel(block);
}
/*
* Like removeAny() but removes data as if the given player broke the Block
* Currently only makes a difference for Logging
*/
public static boolean removeAnyByPlayer(Block block, Player player) {
if (removeCauldron(block)) return true;
return removeBarrelByPlayer(block, player);
}
// # # # # # Brew # # # # #
/*
* Get a Brew from an ItemStack
* Reads the Brew data from the saved data on the item
* Checks if item is actually a Brew
* Returns null if item is not a Brew
*/
public static Brew getBrew(ItemStack item) {
return Brew.get(item);
}
/*
* Get a Brew from an ItemMeta
* Reads the Brew data from the saved data in the Meta
* Checks if meta has a Brew saved
* Returns null if meta is not a Brew
*/
public static Brew getBrew(ItemMeta meta) {
return Brew.get(meta);
}
// # # # # # Barrel # # # # #
/*
* Get a Barrel from a Block
* May be any Wood, Fence, Sign that is part of a Barrel
* Returns null if block is not part of a Barrel
*/
public static Barrel getBarrel(Block block) {
return Barrel.get(block);
}
/*
* Get the Inventory of a Block part of a Barrel
* May be any Wood, Fence or Sign that is part of a Barrel
* Returns null if block is not part of a Barrel
*/
public static Inventory getBarrelInventory(Block block) {
Barrel barrel = Barrel.get(block);
if (barrel != null) {
return barrel.getInventory();
}
return null;
}
/*
* Remove any Barrel that this Block may be Part of
* Returns true if a Barrel was removed
* Does not remove any actual Block
*/
public static boolean removeBarrel(Block block) { // TODO add dropItems flag
return removeBarrelByPlayer(block, null);
}
/*
* Remove any Barrel that this Block may be Part of, as if broken by the Player
* Returns true if a Barrel was removed
* Does not remove any actual Block
*/
public static boolean removeBarrelByPlayer(Block block, Player player) {
Barrel barrel = Barrel.get(block);
if (barrel != null) {
barrel.remove(block, player);
return true;
}
return false;
}
// # # # # # Cauldron # # # # #
/*
* Get a BCauldron from a Block
* Returns null if block is not a BCauldron
*/
public static BCauldron getCauldron(Block block) {
return BCauldron.get(block);
}
/*
* Remove any data associated with a Cauldron at that given Block
* Returns true if a Cauldron was removed
* Does not actually remove the Block
*/
public static boolean removeCauldron(Block block) {
return BCauldron.remove(block);
}
// # # # # # Recipe # # # # #
/*
* Get a BRecipe by its name
* The name is the middle one of the three if three are set in the config
* Returns null if recipe with that name does not exist
*/
public static BRecipe getRecipe(String name) {
return BRecipe.get(name);
}
/*
* Add a new recipe
* Not Implemented yet
*/
public static boolean addRecipe(BRecipe recipe) {
throw new NotImplementedException(); // TODO implement
}
}
@@ -0,0 +1,103 @@
package com.dre.brewery.api.events;
import com.dre.brewery.BCauldron;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Cauldron;
import org.bukkit.material.MaterialData;
/*
* Player adding an ingredient to a cauldron
* Always one item added at a time
* If needed use the caudrons add method to manually add more Items
*/
public class IngedientAddEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Block block;
private final BCauldron cauldron;
private ItemStack ingredient;
private boolean cancelled;
private boolean takeItem = true;
public IngedientAddEvent(Player who, Block block, BCauldron bCauldron, ItemStack ingredient) {
super(who);
this.block = block;
cauldron = bCauldron;
this.ingredient = ingredient.clone();
}
public Block getBlock() {
return block;
}
public BCauldron getCauldron() {
return cauldron;
}
// Get the item currently being added to the cauldron by the player
// Can be changed directly or with the setter
// The amount is ignored and always one added
public ItemStack getIngredient() {
return ingredient;
}
// Set the ingredient added to the cauldron to something else
// Will always be accepted, even when not in a recipe or the cooked list
// The amount is ignored and always one added
public void setIngredient(ItemStack ingredient) {
this.ingredient = ingredient;
}
// If the amount of the item in the players hand should be decreased
// Default true
public boolean shouldTakeItem() {
return takeItem;
}
// Set if the amount of the item in the players hand should be decreased
public void setTakeItem(boolean takeItem) {
this.takeItem = takeItem;
}
// Get the MaterialData of the Cauldron
// May be null if the Cauldron does not exist anymore
public Cauldron getCauldronData() {
BlockState state = block.getState();
if (state != null) {
MaterialData data = state.getData();
if (data instanceof Cauldron) {
return ((Cauldron) state);
}
}
return null;
}
// 0 = empty, 1 = something in, 2 = full
public int getFillLevel() {
return BCauldron.getFillLevel(block);
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,54 @@
package com.dre.brewery.api.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.potion.PotionEffect;
import java.util.List;
/*
* Called when the Alcohol in the Player demands its toll
* These effects are applied regularly to the Player depending on his alcohol level
* By default it is just one Confusion effect
* Can be changed or cancelled
*/
public class PlayerAlcEffectEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private List<PotionEffect> effects;
private boolean cancelled;
public PlayerAlcEffectEvent(Player who, List<PotionEffect> effects) {
super(who);
this.effects = effects;
}
public List<PotionEffect> getEffects() {
return effects;
}
public void setEffects(List<PotionEffect> effects) {
this.effects = effects;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,52 @@
package com.dre.brewery.api.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.potion.PotionEffect;
import java.util.List;
/*
* Called when the Effects of a Brew are applied to the player (drinking the Brew)
* These depend on alcohol and quality of the brew
* Can be changed or cancelled
*/
public class PlayerDrinkEffectEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private List<PotionEffect> effects;
private boolean cancelled;
public PlayerDrinkEffectEvent(Player who, List<PotionEffect> effects) {
super(who);
this.effects = effects;
}
public List<PotionEffect> getEffects() {
return effects;
}
public void setEffects(List<PotionEffect> effects) {
this.effects = effects;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,60 @@
package com.dre.brewery.api.events;
import com.dre.brewery.BPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
/*
* The player pukes (throws puke items to the ground)
* Those items can never be picked up and despawn after the time set in the config
* Number of items to drop can be changed with count
*/
public class PlayerPukeEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private int count;
private boolean cancelled;
private BPlayer bPlayer;
public PlayerPukeEvent(Player who, int count) {
super(who);
this.count = count;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public BPlayer getBPlayer() {
if (bPlayer == null) {
bPlayer = BPlayer.get(player);
}
return bPlayer;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,64 @@
package com.dre.brewery.api.events;
import com.dre.brewery.BPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.util.Vector;
/*
* The Players movement is hindered because of drunkeness
* Called each time before pushing the Player with the Vector push 10 times
* The Push Vector can be changed or multiplied
*/
public class PlayerPushEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final BPlayer bPlayer;
private Vector push;
private boolean cancelled;
public PlayerPushEvent(Player who, Vector push, BPlayer bPlayer) {
super(who);
this.push = push;
this.bPlayer = bPlayer;
}
public BPlayer getbPlayer() {
return bPlayer;
}
// The Vector in which direction and magnitude the player is Pushed
// Can be changed directly or through setPush
public Vector getPush() {
return push;
}
// Set the Push vector, can not be null
public void setPush(Vector push) {
if (push == null) {
throw new NullPointerException("Push Vector is null");
}
this.push = push;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,54 @@
package com.dre.brewery.api.events.barrel;
import com.dre.brewery.Barrel;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/*
* A Player opens a Barrel by rightclicking it
* The PlayerInteractEvent on the Barrel may be cancelled. In that case this never gets called
* Can be cancelled to silently deny opening the Barrel
*/
public class BarrelAccessEvent extends BarrelEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final Block clickedBlock;
private boolean isCancelled;
public BarrelAccessEvent(Barrel barrel, Player player, Block clickedBlock) {
super(barrel);
this.player = player;
this.clickedBlock = clickedBlock;
}
// Gets the Block that was actually clicked.
// For access Permissions getSpigot() should be used
public Block getClickedBlock() {
return clickedBlock;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public void setCancelled(boolean cancelled) {
isCancelled = cancelled;
}
public Player getPlayer() {
return player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,44 @@
package com.dre.brewery.api.events.barrel;
import com.dre.brewery.Barrel;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/*
* Called when a Barrel is created by a Player by placing a Sign
* Cancelling this will silently fail the Barrel creation
*/
public class BarrelCreateEvent extends BarrelEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private boolean cancelled;
public BarrelCreateEvent(Barrel barrel, Player player) {
super(barrel);
this.player = player;
}
public Player getPlayer() {
return player;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,71 @@
package com.dre.brewery.api.events.barrel;
import com.dre.brewery.Barrel;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/*
* A Barrel is being destroyed by something, may not be by a Player
* A BarrelRemoveEvent will be called after this, if this is not cancelled
* Use the BarrelRemoveEvent to monitor any and all barrels being removed in a non cancellable way
*/
public class BarrelDestroyEvent extends BarrelEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Block broken;
private final Reason reason;
private final Player player;
private boolean cancelled;
public BarrelDestroyEvent(Barrel barrel, Block broken, Reason reason, Player player) {
super(barrel);
this.broken = broken;
this.player = player;
this.reason = reason;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public Block getBroken() {
return broken;
}
public Reason getReason() {
return reason;
}
public boolean hasPlayer() {
return player != null;
}
// MAY BE NULL if no Player is involved
public Player getPlayerOptional() {
return player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public enum Reason {
PLAYER, // A Player Broke the Barrel
BROKEN, // A Block was broken by something
BURNED, // A Block burned away
EXPLODED, // The Barrel exploded somehow
UNKNOWN // The Barrel was broken somehow else
}
}
@@ -0,0 +1,27 @@
package com.dre.brewery.api.events.barrel;
import com.dre.brewery.Barrel;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.Inventory;
public abstract class BarrelEvent extends Event {
protected final Barrel barrel;
public BarrelEvent(Barrel barrel) {
this.barrel = barrel;
}
public Barrel getBarrel() {
return barrel;
}
public Inventory getInventory() {
return barrel.getInventory();
}
public Block getSpigot() {
return barrel.getSpigot();
}
}
@@ -0,0 +1,34 @@
package com.dre.brewery.api.events.barrel;
import com.dre.brewery.Barrel;
import org.bukkit.event.HandlerList;
/*
* A Barrel is being removed. There may have been a BarrelDestroyEvent before
* If not, Worldedit, other Plugins etc may be the cause for unexpected removal
*/
public class BarrelRemoveEvent extends BarrelEvent {
private static final HandlerList handlers = new HandlerList();
private boolean itemsDrop = true;
public BarrelRemoveEvent(Barrel barrel) {
super(barrel);
}
public boolean shouldItemsDrop() {
return itemsDrop;
}
public void setShouldItemsDrop(boolean itemsDrop) {
this.itemsDrop = itemsDrop;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,79 @@
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;
/*
* A Player Drinks a Brew
* The amount of alcohol and quality that will be added to the player can be get/set here
* If cancelled the drinking will fail silently
*/
public class BrewDrinkEvent extends BrewEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final BPlayer bPlayer;
private int alc;
private int quality;
private boolean cancelled;
public BrewDrinkEvent(Brew brew, Player player, BPlayer bPlayer) {
super(brew);
this.player = player;
this.bPlayer = bPlayer;
alc = brew.calcAlcohol();
quality = brew.getQuality();
}
public Player getPlayer() {
return player;
}
public BPlayer getbPlayer() {
return bPlayer;
}
public int getAddedAlcohol() {
return alc;
}
public void setAddedAlcohol(int alc) {
this.alc = alc;
}
public int getQuality() {
return quality;
}
public void setQuality(int quality) {
if (quality > 10 || quality < 0) {
throw new IllegalArgumentException("Quality must be in range from 0 to 10");
}
this.quality = quality;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,17 @@
package com.dre.brewery.api.events.brew;
import com.dre.brewery.Brew;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public abstract class BrewEvent extends Event {
protected final Brew brew;
public BrewEvent(Brew brew) {
this.brew = brew;
}
public Brew getBrew() {
return brew;
}
}
@@ -0,0 +1,53 @@
package com.dre.brewery.api.events.brew;
import com.dre.brewery.Brew;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/*
* A Brew is created or modified
* Usually happens on Filling from cauldron, distilling and aging.
*/
public class BrewModifyEvent extends BrewEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Type type;
private boolean cancelled;
public BrewModifyEvent(Brew brew, Type type) {
super(brew);
this.type = type;
}
public Type getType() {
return type;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@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
}
}