mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2025-01-08 07:27:39 +01:00
Merged the crafting station events
This commit is contained in:
parent
812de85edc
commit
8d9631d8f4
@ -10,7 +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.event.crafting.PlayerUseCraftingStationEvent;
|
||||
import net.Indyuce.mmoitems.api.item.plugin.ConfigItem;
|
||||
import net.Indyuce.mmoitems.api.player.PlayerData;
|
||||
import net.Indyuce.mmoitems.api.util.message.Message;
|
||||
@ -20,8 +20,8 @@ public class CraftingRecipe extends Recipe {
|
||||
private final ConfigMMOItem output;
|
||||
|
||||
/*
|
||||
* there can't be any crafting time for upgrading recipes since there is no way
|
||||
* to save an MMOItem in the config file TODO save as ItemStack
|
||||
* there can't be any crafting time for upgrading recipes since there is no
|
||||
* way to save an MMOItem in the config file TODO save as ItemStack
|
||||
*/
|
||||
private final double craftingTime;
|
||||
|
||||
@ -55,18 +55,19 @@ public class CraftingRecipe extends Recipe {
|
||||
* directly add the ingredients to the player inventory
|
||||
*/
|
||||
if (isInstant()) {
|
||||
CraftingStationCraftEvent event = new CraftingStationCraftEvent(data, station, this, true);
|
||||
PlayerUseCraftingStationEvent event = new PlayerUseCraftingStationEvent(data, station, recipe);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if(event.isCancelled()) return;
|
||||
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
if (hasOption(RecipeOption.OUTPUT_ITEM))
|
||||
new SmartGive(data.getPlayer()).give(getOutput().generate());
|
||||
recipe.getRecipe().getTriggers().forEach(trigger -> trigger.whenCrafting(data));
|
||||
if (!hasOption(RecipeOption.SILENT_CRAFT))
|
||||
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
|
||||
/*
|
||||
* if recipe not instant, add item to crafting queue, either way RELOAD
|
||||
* inventory data and reopen inventory!
|
||||
* if recipe not instant, add item to crafting queue, either way
|
||||
* RELOAD inventory data and reopen inventory!
|
||||
*/
|
||||
} else
|
||||
data.getCrafting().getQueue(station).add(this);
|
||||
|
@ -1,44 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package net.Indyuce.mmoitems.api.event.crafting;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import net.Indyuce.mmoitems.api.crafting.CraftingStation;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.CraftingRecipe;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.Recipe;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.RecipeInfo;
|
||||
import net.Indyuce.mmoitems.api.event.PlayerDataEvent;
|
||||
import net.Indyuce.mmoitems.api.player.PlayerData;
|
||||
|
||||
public class PlayerUseCraftingStationEvent extends PlayerDataEvent {
|
||||
private final Recipe recipe;
|
||||
private final RecipeInfo recipeInfo;
|
||||
private final CraftingStation station;
|
||||
private final StationAction action;
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
/**
|
||||
* Called when a player directly interacts with a recipe in the crafting
|
||||
* station GUI. The recipe is either instant and the item is given
|
||||
* instaneously, or the item is sent in the crafting queue
|
||||
*
|
||||
* @param playerData
|
||||
* The player interacting with the crafting station
|
||||
* @param station
|
||||
* The crafting station being used
|
||||
* @param recipeInfo
|
||||
* The recipe being used to craft the item
|
||||
*/
|
||||
public PlayerUseCraftingStationEvent(PlayerData playerData, CraftingStation station, RecipeInfo recipeInfo) {
|
||||
super(playerData);
|
||||
|
||||
this.recipeInfo = recipeInfo;
|
||||
this.recipe = recipeInfo.getRecipe();
|
||||
this.station = station;
|
||||
this.action = StationAction.INTERACT_WITH_RECIPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player claims an item from the crafting queue.
|
||||
*
|
||||
* @param playerData
|
||||
* The player interacting with the crafting station
|
||||
* @param station
|
||||
* The crafting station being used
|
||||
* @param recipeInfo
|
||||
* The recipe being used to craft the item
|
||||
*/
|
||||
public PlayerUseCraftingStationEvent(PlayerData playerData, CraftingStation station, Recipe recipe) {
|
||||
super(playerData);
|
||||
|
||||
this.recipeInfo = null;
|
||||
this.recipe = recipe;
|
||||
this.station = station;
|
||||
this.action = StationAction.CRAFTING_QUEUE;
|
||||
}
|
||||
|
||||
public CraftingStation getStation() {
|
||||
return station;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The corresponding recipe info IF AND ONLY IF the player is
|
||||
* interacting with a recipe. This method cannot be used when a
|
||||
* player claims an item from the crafting queue.
|
||||
*/
|
||||
public RecipeInfo getRecipeInfo() {
|
||||
Validate.notNull(recipeInfo, "No recipe info is provided when a player claims an item in the crafting queue");
|
||||
return recipeInfo;
|
||||
}
|
||||
|
||||
public Recipe getRecipe() {
|
||||
return recipe;
|
||||
}
|
||||
|
||||
public StationAction getInteraction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public boolean isInstant() {
|
||||
return recipe instanceof CraftingRecipe && ((CraftingRecipe) recipe).isInstant();
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public enum StationAction {
|
||||
|
||||
/**
|
||||
* Called when a player places an item in the crafting queue when the
|
||||
* recipe is not instantaneous.
|
||||
*/
|
||||
INTERACT_WITH_RECIPE,
|
||||
|
||||
/**
|
||||
* Called when a player claims the item either in the crafting queue or
|
||||
* because the recipe is instantaneous
|
||||
*/
|
||||
CRAFTING_QUEUE;
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
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.crafting.recipe.RecipeInfo;
|
||||
import net.Indyuce.mmoitems.api.event.PlayerDataEvent;
|
||||
import net.Indyuce.mmoitems.api.player.PlayerData;
|
||||
|
||||
public class PlayerUseRecipeEvent extends PlayerDataEvent {
|
||||
private final RecipeInfo info;
|
||||
private final CraftingStation station;
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
public PlayerUseRecipeEvent(PlayerData playerData, CraftingStation station, RecipeInfo info) {
|
||||
super(playerData);
|
||||
|
||||
this.info = info;
|
||||
this.station = station;
|
||||
}
|
||||
|
||||
public CraftingStation getStation() {
|
||||
return station;
|
||||
}
|
||||
|
||||
public RecipeInfo getRecipeInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public Recipe getRecipe() {
|
||||
return info.getRecipe();
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ package net.Indyuce.mmoitems.gui;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.Recipe;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Sound;
|
||||
@ -21,9 +20,9 @@ import net.Indyuce.mmoitems.api.crafting.CraftingStatus.CraftingQueue.CraftingIn
|
||||
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.Recipe;
|
||||
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.event.crafting.PlayerUseCraftingStationEvent;
|
||||
import net.Indyuce.mmoitems.api.item.plugin.ConfigItem;
|
||||
import net.Indyuce.mmoitems.api.player.PlayerData;
|
||||
import net.Indyuce.mmoitems.api.util.message.Message;
|
||||
@ -184,9 +183,9 @@ public class CraftingStationView extends PluginInventory {
|
||||
|
||||
if (craft.isReady()) {
|
||||
CraftingRecipe recipe = craft.getRecipe();
|
||||
CraftingStationCraftEvent cscevent = new CraftingStationCraftEvent(data, station, recipe, true);
|
||||
Bukkit.getPluginManager().callEvent(cscevent);
|
||||
if(!cscevent.isCancelled()) {
|
||||
PlayerUseCraftingStationEvent called = new PlayerUseCraftingStationEvent(data, station, recipe);
|
||||
Bukkit.getPluginManager().callEvent(called);
|
||||
if (!called.isCancelled()) {
|
||||
recipe.getTriggers().forEach(trigger -> trigger.whenCrafting(data));
|
||||
ItemStack craftedItem = recipe.getOutput().generate();
|
||||
CustomSoundListener.stationCrafting(craftedItem, data.getPlayer());
|
||||
@ -195,8 +194,7 @@ public class CraftingStationView extends PluginInventory {
|
||||
if (recipe.hasOption(Recipe.RecipeOption.OUTPUT_ITEM))
|
||||
new SmartGive(data.getPlayer()).give(craftedItem);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
|
||||
for (Ingredient ingredient : craft.getRecipe().getIngredients())
|
||||
new SmartGive(data.getPlayer()).give(ingredient.generateItemStack());
|
||||
@ -225,7 +223,7 @@ public class CraftingStationView extends PluginInventory {
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerUseRecipeEvent called = new PlayerUseRecipeEvent(data, station, recipe);
|
||||
PlayerUseCraftingStationEvent called = new PlayerUseCraftingStationEvent(data, station, recipe);
|
||||
Bukkit.getPluginManager().callEvent(called);
|
||||
if (called.isCancelled())
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user