This commit is contained in:
ASangarin 2020-08-09 00:53:15 +02:00
commit 613a3df6b3
17 changed files with 200 additions and 136 deletions

View File

@ -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,9 +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);
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());
@ -65,8 +66,8 @@ public class CraftingRecipe extends Recipe {
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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -31,13 +31,23 @@ public class GenerationModifier {
this(null, config);
}
/**
* Loads an item gen modifier from a configuration section. If you provide
* the ItemGenManager, you will be able to use the 'parent' option to
* redirect that modifier to a public gen modifier.
*
* @param manager
* Provide the ItemGenManager to use the 'parent' option
* @param config
* The configuration section to load the modifier from
*/
public GenerationModifier(ItemGenManager manager, ConfigurationSection config) {
Validate.notNull(config, "Could not read config");
id = config.getName().toLowerCase().replace("_", "-");
/*
* when providing a non-null itemGenManager, it indicates that public
* modifiers were loaded and that the constructor can them
* modifiers were loaded and that the constructor can use them
*/
if (manager != null && config.contains("parent")) {
String parentFormat = config.get("parent").toString().toLowerCase().replace("_", "-").replace(" ", "_");

View File

@ -46,8 +46,7 @@ public class GenerationTemplate {
if (config.contains("modifiers"))
for (String key : config.getConfigurationSection("modifiers").getKeys(false))
try {
modifiers.add(new GenerationModifier(MMOItems.plugin.getItemGenerator(),
config.getConfigurationSection("modifiers." + key)));
modifiers.add(new GenerationModifier(MMOItems.plugin.getItemGenerator(), config.getConfigurationSection("modifiers." + key)));
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, "An error occured while trying to load modifier '" + key
+ "' from item gen template '" + id + "': " + exception.getMessage());
@ -62,8 +61,8 @@ public class GenerationTemplate {
ItemStat stat = MMOItems.plugin.getStats().get(id);
base.put(stat, stat.whenInitializedGeneration(config.get("base." + key)));
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, "An error occured while trying to load base item data '"
+ key + "' from item gen template '" + id + "': " + exception.getMessage());
MMOItems.plugin.getLogger().log(Level.INFO, "An error occured while trying to load base item data '" + key
+ "' from item gen template '" + id + "': " + exception.getMessage());
}
}
@ -87,6 +86,12 @@ public class GenerationTemplate {
return options.contains(option);
}
/**
* @param player
* The rpg info about the player whom you want to give a random
* item to
* @return A random item builder which scales on the player's level.
*/
public GeneratedItemBuilder newBuilder(RPGPlayer player) {
int itemLevel = MMOItems.plugin.getItemGenerator().rollLevel(player.getLevel());
RolledTier itemTier = MMOItems.plugin.getItemGenerator().rollTier(itemLevel);
@ -100,8 +105,8 @@ public class GenerationTemplate {
public enum TemplateOption {
/*
* when the item is being generated, modifiers are rolled in a random order so
* you never the same modifiers again and again
* when the item is being generated, modifiers are rolled in a random
* order so you never the same modifiers again and again
*/
ROLL_MODIFIER_CHECK_ORDER;
}

View File

@ -11,6 +11,14 @@ public class NameModifier {
private final String format;
private final int priority;
/**
* Loads a prefix/suffix from either a config section or a string
*
* @param type
* Either a prefix or a suffix
* @param object
* The object to load the modifier from
*/
public NameModifier(ModifierType type, Object object) {
Validate.notNull(object, "Object cannot be null");
this.type = type;

View File

@ -2,11 +2,19 @@ package net.Indyuce.mmoitems.api.itemgen;
import net.Indyuce.mmoitems.stat.data.type.StatData;
/**
* RandomStatDatas are basically the bricks of the generation templates. They
* are the first instances called when loading gen templates from config files.
*
* @author cympe
*/
public interface RandomStatData {
/*
* generate a real stat data based on the item builder which contains
* information about the item level.
/**
* @param builder
* The builder of the random item being generated
* @return A random stat data instance which will then be merged onto the
* base item template
*/
public StatData randomize(GeneratedItemBuilder builder);
}

View File

@ -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;

View File

@ -160,7 +160,7 @@ public class CustomSounds extends ItemStat implements ProperStat {
mmoitem.getNBT().getDouble("MMOITEMS_SOUND_" + sound.name() + "_PIT"));
}
if (sounds.total() > 0)
if (sounds.getCustomSounds().size() > 0)
mmoitem.setData(ItemStat.CUSTOM_SOUNDS, sounds);
}
}

View File

@ -11,9 +11,15 @@ import net.Indyuce.mmoitems.stat.data.type.Mergeable;
import net.Indyuce.mmoitems.stat.data.type.StatData;
public class CommandListData implements StatData, Mergeable, RandomStatData {
private final Set<CommandData> commands = new HashSet<>();
private final Set<CommandData> commands;
public CommandListData(Set<CommandData> commands) {
this.commands = commands;
}
public CommandListData(CommandData... commands) {
this(new HashSet<>());
add(commands);
}
@ -34,6 +40,6 @@ public class CommandListData implements StatData, Mergeable, RandomStatData {
@Override
public StatData randomize(GeneratedItemBuilder builder) {
return this;
return new CommandListData(new HashSet<>(commands));
}
}

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmoitems.stat.data;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -77,6 +78,6 @@ public class GemSocketsData implements StatData, Mergeable, RandomStatData {
@Override
public StatData randomize(GeneratedItemBuilder builder) {
return this;
return new GemSocketsData(new ArrayList<>(emptySlots));
}
}

View File

@ -13,37 +13,41 @@ import net.Indyuce.mmoitems.stat.data.type.Mergeable;
import net.Indyuce.mmoitems.stat.data.type.StatData;
public class SoundListData implements StatData, Mergeable, RandomStatData {
private final Map<CustomSound, SoundData> stats = new HashMap<>();
private final Map<CustomSound, SoundData> sounds;
public SoundListData() {
this(new HashMap<>());
}
public SoundListData(Map<CustomSound, SoundData> sounds) {
this.sounds = sounds;
}
public Set<CustomSound> getCustomSounds() {
return stats.keySet();
return sounds.keySet();
}
public Map<CustomSound, SoundData> mapData() {
return stats;
return sounds;
}
public SoundData get(CustomSound sound) {
return stats.get(sound);
return sounds.get(sound);
}
public void set(CustomSound type, String sound, double volume, double pitch) {
this.stats.put(type, new SoundData(sound, volume, pitch));
}
public int total() {
return stats.size();
this.sounds.put(type, new SoundData(sound, volume, pitch));
}
@Override
public void merge(StatData data) {
Validate.isTrue(data instanceof SoundListData, "Cannot merge two different stat data types");
SoundListData cast = (SoundListData) data;
cast.stats.keySet().forEach(key -> stats.put(key, cast.stats.get(key)));
cast.sounds.forEach((sound, soundData) -> sounds.put(sound, soundData));
}
@Override
public StatData randomize(GeneratedItemBuilder builder) {
return this;
return new SoundListData(new HashMap<>(sounds));
}
}

View File

@ -40,7 +40,7 @@ public class StringListData implements StatData, RandomStatData, Mergeable {
@Override
public StatData randomize(GeneratedItemBuilder builder) {
return this;
return new StringListData(new ArrayList<>(list));
}
@Override

View File

@ -2,8 +2,8 @@ package net.Indyuce.mmoitems.stat.data.type;
public interface Mergeable {
/*
* merging two stat data is used when either applying a gem stone to an item
/**
* Merging two stat data is used when either applying a gem stone to an item
* which already has this type of item data, or when generating an item
* randomly so that the item benefits from all modifiers
*/

View File

@ -76,7 +76,7 @@ public class StringStat extends ItemStat {
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.GREEN + value);
} else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + " None");
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None");
lore.add("");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Left click to change this value.");