Added CraftingStationCraftEvent

This commit is contained in:
ASangarin 2020-08-08 23:44:10 +02:00
parent 38535fe547
commit 812de85edc
3 changed files with 62 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmoitems.api.crafting.recipe;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
@ -9,6 +10,7 @@ import net.Indyuce.mmoitems.api.crafting.ConfigMMOItem;
import net.Indyuce.mmoitems.api.crafting.CraftingStation;
import net.Indyuce.mmoitems.api.crafting.CraftingStatus.CraftingQueue;
import net.Indyuce.mmoitems.api.crafting.IngredientInventory;
import net.Indyuce.mmoitems.api.event.crafting.CraftingStationCraftEvent;
import net.Indyuce.mmoitems.api.item.plugin.ConfigItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.message.Message;
@ -53,6 +55,10 @@ public class CraftingRecipe extends Recipe {
* directly add the ingredients to the player inventory
*/
if (isInstant()) {
CraftingStationCraftEvent event = new CraftingStationCraftEvent(data, station, this, true);
Bukkit.getPluginManager().callEvent(event);
if(event.isCancelled()) return;
if (hasOption(RecipeOption.OUTPUT_ITEM))
new SmartGive(data.getPlayer()).give(getOutput().generate());
recipe.getRecipe().getTriggers().forEach(trigger -> trigger.whenCrafting(data));

View File

@ -0,0 +1,44 @@
package net.Indyuce.mmoitems.api.event.crafting;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmoitems.api.crafting.CraftingStation;
import net.Indyuce.mmoitems.api.crafting.recipe.Recipe;
import net.Indyuce.mmoitems.api.event.PlayerDataEvent;
import net.Indyuce.mmoitems.api.player.PlayerData;
public class CraftingStationCraftEvent extends PlayerDataEvent {
private final Recipe recipe;
private final CraftingStation station;
private final boolean instant;
private static final HandlerList handlers = new HandlerList();
public CraftingStationCraftEvent(PlayerData playerData, CraftingStation station, Recipe recipe, boolean instant) {
super(playerData);
this.recipe = recipe;
this.station = station;
this.instant = instant;
}
public CraftingStation getStation() {
return station;
}
public Recipe getRecipe() {
return recipe;
}
public boolean isInstant() {
return instant;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -22,6 +22,7 @@ import net.Indyuce.mmoitems.api.crafting.IngredientInventory;
import net.Indyuce.mmoitems.api.crafting.ingredient.Ingredient;
import net.Indyuce.mmoitems.api.crafting.recipe.CraftingRecipe;
import net.Indyuce.mmoitems.api.crafting.recipe.RecipeInfo;
import net.Indyuce.mmoitems.api.event.crafting.CraftingStationCraftEvent;
import net.Indyuce.mmoitems.api.event.crafting.PlayerUseRecipeEvent;
import net.Indyuce.mmoitems.api.item.plugin.ConfigItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
@ -183,13 +184,17 @@ public class CraftingStationView extends PluginInventory {
if (craft.isReady()) {
CraftingRecipe recipe = craft.getRecipe();
recipe.getTriggers().forEach(trigger -> trigger.whenCrafting(data));
ItemStack craftedItem = recipe.getOutput().generate();
CustomSoundListener.stationCrafting(craftedItem, data.getPlayer());
if (!recipe.hasOption(Recipe.RecipeOption.SILENT_CRAFT))
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
if (recipe.hasOption(Recipe.RecipeOption.OUTPUT_ITEM))
new SmartGive(data.getPlayer()).give(craftedItem);
CraftingStationCraftEvent cscevent = new CraftingStationCraftEvent(data, station, recipe, true);
Bukkit.getPluginManager().callEvent(cscevent);
if(!cscevent.isCancelled()) {
recipe.getTriggers().forEach(trigger -> trigger.whenCrafting(data));
ItemStack craftedItem = recipe.getOutput().generate();
CustomSoundListener.stationCrafting(craftedItem, data.getPlayer());
if (!recipe.hasOption(Recipe.RecipeOption.SILENT_CRAFT))
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
if (recipe.hasOption(Recipe.RecipeOption.OUTPUT_ITEM))
new SmartGive(data.getPlayer()).give(craftedItem);
}
}
else {
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);