mirror of
https://gitlab.com/phoenix-dvpmt/mmocore.git
synced 2024-11-23 00:05:52 +01:00
!Moved compatibility methods to MI
This commit is contained in:
parent
3c046682cc
commit
6d53f009bd
20
pom.xml
20
pom.xml
@ -59,14 +59,14 @@
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sk89q-repo</id>
|
||||
<url>https://maven.enginehub.org/repo/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sk89q-repo</id>
|
||||
<url>https://maven.enginehub.org/repo/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -85,9 +85,9 @@
|
||||
<dependency>
|
||||
<groupId>net.Indyuce</groupId>
|
||||
<artifactId>MMOItems</artifactId>
|
||||
<version>4.7.14</version>
|
||||
<version>5.1.2</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/lib/MMOItems Premium.jar</systemPath>
|
||||
<systemPath>${basedir}/lib/MMOItems.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nisovin</groupId>
|
||||
|
@ -40,7 +40,6 @@ import net.Indyuce.mmocore.comp.holograms.CMIPlugin;
|
||||
import net.Indyuce.mmocore.comp.holograms.HologramSupport;
|
||||
import net.Indyuce.mmocore.comp.holograms.HologramsPlugin;
|
||||
import net.Indyuce.mmocore.comp.holograms.HolographicDisplaysPlugin;
|
||||
import net.Indyuce.mmocore.comp.mmoitems.MMOItemsMMOLoader;
|
||||
import net.Indyuce.mmocore.comp.mythicmobs.MythicMobsDrops;
|
||||
import net.Indyuce.mmocore.comp.mythicmobs.MythicMobsMMOLoader;
|
||||
import net.Indyuce.mmocore.comp.placeholder.DefaultParser;
|
||||
@ -140,9 +139,6 @@ public class MMOCore extends JavaPlugin {
|
||||
/*
|
||||
* register extra objective, drop items...
|
||||
*/
|
||||
if (Bukkit.getPluginManager().getPlugin("MMOItems") != null)
|
||||
loadManager.registerLoader(new MMOItemsMMOLoader());
|
||||
|
||||
if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null)
|
||||
loadManager.registerLoader(new WorldGuardMMOLoader());
|
||||
|
||||
|
@ -1,66 +0,0 @@
|
||||
package net.Indyuce.mmocore.api.experience.source;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import net.Indyuce.mmocore.api.experience.Profession;
|
||||
import net.Indyuce.mmocore.api.experience.source.type.SpecificExperienceSource;
|
||||
import net.Indyuce.mmocore.api.load.MMOLineConfig;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmocore.manager.profession.ExperienceManager;
|
||||
import net.Indyuce.mmoitems.MMOItems;
|
||||
import net.Indyuce.mmoitems.api.CustomBlock;
|
||||
|
||||
public class MineMIBlockExperienceSource extends SpecificExperienceSource<Integer> {
|
||||
public final int id;
|
||||
private final boolean silkTouch;
|
||||
private final boolean playerPlaced;
|
||||
|
||||
public MineMIBlockExperienceSource(Profession profession, MMOLineConfig config) {
|
||||
super(profession, config);
|
||||
|
||||
config.validate("id");
|
||||
id = config.getInt("id", 1);
|
||||
silkTouch = config.getBoolean("silk-touch", true);
|
||||
playerPlaced = config.getBoolean("player-placed", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExperienceManager<MineMIBlockExperienceSource> newManager() {
|
||||
return new ExperienceManager<MineMIBlockExperienceSource>() {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void a(BlockBreakEvent event) {
|
||||
if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL)
|
||||
return;
|
||||
PlayerData data = PlayerData.get(event.getPlayer());
|
||||
|
||||
for (MineMIBlockExperienceSource source : getSources())
|
||||
{
|
||||
if (!MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType()))
|
||||
continue;
|
||||
if (source.silkTouch && hasSilkTouch(event.getPlayer().getInventory().getItemInMainHand()))
|
||||
continue;
|
||||
if ((!source.playerPlaced) && event.getBlock().hasMetadata("player_placed"))
|
||||
continue;
|
||||
|
||||
if (source.matches(data, CustomBlock.getFromData(event.getBlock().getBlockData()).getId()))
|
||||
source.giveExperience(data, event.getBlock().getLocation());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private boolean hasSilkTouch(ItemStack item) {
|
||||
return item != null && item.hasItemMeta() && item.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(PlayerData player, Integer blockId) {
|
||||
return id == blockId && hasRightClass(player);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package net.Indyuce.mmocore.comp.mmoitems;
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import net.Indyuce.mmocore.api.droptable.condition.Condition;
|
||||
import net.Indyuce.mmocore.api.droptable.dropitem.DropItem;
|
||||
import net.Indyuce.mmocore.api.experience.Profession;
|
||||
import net.Indyuce.mmocore.api.experience.source.MineMIBlockExperienceSource;
|
||||
import net.Indyuce.mmocore.api.experience.source.type.ExperienceSource;
|
||||
import net.Indyuce.mmocore.api.load.MMOLineConfig;
|
||||
import net.Indyuce.mmocore.api.load.MMOLoader;
|
||||
import net.Indyuce.mmocore.api.quest.objective.Objective;
|
||||
import net.Indyuce.mmocore.api.quest.trigger.Trigger;
|
||||
|
||||
public class MMOItemsMMOLoader implements MMOLoader {
|
||||
|
||||
@Override
|
||||
public Condition loadCondition(MMOLineConfig config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Trigger loadTrigger(MMOLineConfig config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DropItem loadDropItem(MMOLineConfig config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Objective loadObjective(MMOLineConfig config, ConfigurationSection section) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExperienceSource<?> loadExperienceSource(MMOLineConfig config, Profession profession) {
|
||||
if (config.getKey().equals("minemiblock"))
|
||||
return new MineMIBlockExperienceSource(profession, config);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,351 +0,0 @@
|
||||
package net.Indyuce.mmocore.listener.profession;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BrewingStand;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.BrewerInventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmoitems.MMOItems;
|
||||
import net.Indyuce.mmoitems.api.Type;
|
||||
import net.mmogroup.mmolib.api.item.NBTItem;
|
||||
|
||||
public class Alchemy implements Listener {
|
||||
private Set<String> runnables = new HashSet<>();
|
||||
|
||||
private static Set<BrewingRecipe> recipes = new HashSet<>();
|
||||
|
||||
public Alchemy(ConfigurationSection config) {
|
||||
Alchemy.recipes.clear();
|
||||
for (String key : config.getKeys(false)) {
|
||||
BrewingRecipe recipe = new BrewingRecipe(config.getConfigurationSection(key));
|
||||
if (recipe.isValid())
|
||||
recipes.add(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* force place the item in the brewing stand inventory so it can start the
|
||||
* recipe.
|
||||
*/
|
||||
@EventHandler
|
||||
public void a(InventoryClickEvent event) {
|
||||
if (event.getInventory().getType() != InventoryType.BREWING)
|
||||
return;
|
||||
|
||||
event.setCancelled(true);
|
||||
ItemStack item = event.getCurrentItem();
|
||||
if (item == null || item.getType() == Material.AIR)
|
||||
return;
|
||||
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
BrewingStand stand = (BrewingStand) event.getInventory().getHolder();
|
||||
BrewerInventory inv = (BrewerInventory) event.getInventory();
|
||||
|
||||
// send ingredient back in the player inventory
|
||||
if (event.getRawSlot() == 3) {
|
||||
if (player.getInventory().firstEmpty() == -1)
|
||||
return;
|
||||
|
||||
player.getInventory().addItem(inv.getIngredient());
|
||||
inv.setIngredient(null);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1);
|
||||
checkForBrewing(player, stand);
|
||||
return;
|
||||
}
|
||||
|
||||
// send fuel back in the player inventory
|
||||
if (event.getRawSlot() == 4) {
|
||||
if (player.getInventory().firstEmpty() == -1)
|
||||
return;
|
||||
|
||||
player.getInventory().addItem(inv.getFuel());
|
||||
inv.setFuel(null);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1);
|
||||
checkForBrewing(player, stand);
|
||||
return;
|
||||
}
|
||||
|
||||
// send bottle back in the player inventory
|
||||
if (event.getRawSlot() >= 0 && event.getRawSlot() < 3) {
|
||||
if (player.getInventory().firstEmpty() == -1)
|
||||
return;
|
||||
|
||||
player.getInventory().addItem(inv.getItem(event.getRawSlot()));
|
||||
inv.setItem(event.getRawSlot(), null);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1);
|
||||
checkForBrewing(player, stand);
|
||||
return;
|
||||
}
|
||||
|
||||
BrewingRecipe recipe = getCorrespondingRecipe(NBTItem.get(item));
|
||||
|
||||
// send fuel in the brewing stand
|
||||
if (recipe == null) {
|
||||
if (item.getType() == Material.BLAZE_POWDER && (inv.getFuel() == null || inv.getFuel().getType() == Material.AIR || item.isSimilar(inv.getFuel()))) {
|
||||
int fuel = inv.getFuel() == null ? 0 : inv.getFuel().getAmount();
|
||||
int needed = 64 - fuel;
|
||||
int sent = Math.min(needed, item.getAmount());
|
||||
|
||||
if (sent == 0)
|
||||
return;
|
||||
|
||||
ItemStack fuelItem = item.clone();
|
||||
fuelItem.setAmount(fuel + sent);
|
||||
inv.setFuel(fuelItem);
|
||||
|
||||
if (sent == item.getAmount())
|
||||
event.setCurrentItem(null);
|
||||
else
|
||||
item.setAmount(item.getAmount() - sent);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 2);
|
||||
checkForBrewing(player, stand);
|
||||
return;
|
||||
}
|
||||
|
||||
// send potion in the brewing stand
|
||||
if (item.getType() == Material.POTION && ((PotionMeta) item.getItemMeta()).getCustomEffects().isEmpty()) {
|
||||
int empty = getEmptyBottleSlot(inv);
|
||||
if (empty == -1)
|
||||
return;
|
||||
|
||||
inv.setItem(empty, event.getCurrentItem());
|
||||
event.setCurrentItem(null);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 2);
|
||||
checkForBrewing(player, stand);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// send ingredient in the brewing stand
|
||||
if (inv.getIngredient() == null || inv.getIngredient().getType() == Material.AIR || item.isSimilar(inv.getIngredient())) {
|
||||
int ingredient = inv.getIngredient() == null ? 0 : inv.getIngredient().getAmount();
|
||||
int needed = 64 - ingredient;
|
||||
int sent = Math.min(needed, item.getAmount());
|
||||
|
||||
if (sent == 0)
|
||||
return;
|
||||
|
||||
ItemStack ingredientItem = item.clone();
|
||||
ingredientItem.setAmount(ingredient + sent);
|
||||
inv.setIngredient(ingredientItem);
|
||||
|
||||
if (sent == item.getAmount())
|
||||
event.setCurrentItem(null);
|
||||
else
|
||||
item.setAmount(item.getAmount() - sent);
|
||||
|
||||
player.playSound(player.getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 2);
|
||||
checkForBrewing(player, stand);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForBrewing(Player player, BrewingStand stand) {
|
||||
BrewingRecipe recipe = getCorrespondingRecipe(NBTItem.get(stand.getInventory().getIngredient()));
|
||||
Location loc = stand.getLocation();
|
||||
if (!runnables.contains(loc.getBlockY() + "-" + loc.getBlockY() + "-" + loc.getBlockZ()) && recipe != null && stand.getFuelLevel() > 0 && hasAtLeastOnePotion(stand.getInventory()))
|
||||
new BrewingRunnable(player, stand).start(recipe);
|
||||
}
|
||||
|
||||
private int getEmptyBottleSlot(BrewerInventory inv) {
|
||||
ItemStack item;
|
||||
for (int j = 0; j < 3; j++)
|
||||
if ((item = inv.getItem(j)) == null || item.getType() == Material.AIR)
|
||||
return j;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns if the corresponding brewing inventory has at least ONE potion
|
||||
* and if
|
||||
*/
|
||||
private boolean hasAtLeastOnePotion(BrewerInventory inv) {
|
||||
ItemStack item;
|
||||
for (int j = 0; j < 3; j++)
|
||||
if ((item = inv.getItem(j)) != null)
|
||||
return item.getType() == Material.POTION;
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasNoBottle(BrewerInventory inv) {
|
||||
ItemStack item;
|
||||
for (int j = 0; j < 3; j++)
|
||||
if ((item = inv.getItem(j)) != null && item.getType() == Material.POTION)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private BrewingRecipe getCorrespondingRecipe(NBTItem item) {
|
||||
for (BrewingRecipe recipe : recipes)
|
||||
if (recipe.matchesIngredient(item))
|
||||
return recipe;
|
||||
return null;
|
||||
}
|
||||
|
||||
private ItemStack consume(ItemStack item) {
|
||||
if (item.getAmount() < 2)
|
||||
return null;
|
||||
|
||||
item.setAmount(item.getAmount() - 1);
|
||||
return item;
|
||||
}
|
||||
|
||||
public class BrewingRunnable extends BukkitRunnable {
|
||||
private int time = 0;
|
||||
private Block block;
|
||||
private Location loc;
|
||||
private BrewingRecipe recipe;
|
||||
private String mapPath;
|
||||
private Player player;
|
||||
|
||||
public BrewingRunnable(Player player, BrewingStand stand) {
|
||||
this.block = stand.getBlock();
|
||||
this.player = player;
|
||||
loc = stand.getLocation().add(.5, .5, .5);
|
||||
}
|
||||
|
||||
public void start(BrewingRecipe recipe) {
|
||||
this.recipe = recipe;
|
||||
BrewingStand stand = (BrewingStand) block.getState();
|
||||
stand.setFuelLevel(stand.getFuelLevel() - 1);
|
||||
stand.update();
|
||||
runnables.add(mapPath = loc.getBlockY() + "-" + loc.getBlockY() + "-" + loc.getBlockZ());
|
||||
runTaskTimer(MMOCore.plugin, 1, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
BrewingStand stand = (BrewingStand) block.getState();
|
||||
stand.getWorld().spawnParticle(Particle.SPELL_MOB, loc.clone().add(Math.cos((double) time / 3.) * .4, 0, Math.sin((double) time / 3.) * .4), 0);
|
||||
|
||||
// cancel the recipe if ingredient was changed
|
||||
if (!recipe.matchesIngredient(stand.getInventory().getIngredient()) || hasNoBottle(stand.getInventory())) {
|
||||
runnables.remove(mapPath);
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
if (time++ > recipe.getCookingTime()) {
|
||||
runnables.remove(mapPath);
|
||||
stand.getInventory().setIngredient(consume(stand.getInventory().getIngredient()));
|
||||
|
||||
int count = 0;
|
||||
ItemStack item, result = recipe.getResult();
|
||||
for (int j = 0; j < 3; j++)
|
||||
if ((item = stand.getInventory().getItem(j)) != null && item.getType() != Material.AIR) {
|
||||
count++;
|
||||
stand.getInventory().setItem(j, result);
|
||||
}
|
||||
|
||||
if (MMOCore.plugin.professionManager.has("alchemy"))
|
||||
PlayerData.get(player).getCollectionSkills().giveExperience(MMOCore.plugin.professionManager.get("alchemy"), count * recipe.experience, loc);
|
||||
|
||||
stand.getWorld().playSound(stand.getLocation(), Sound.BLOCK_BREWING_STAND_BREW, 1, 1);
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
stand.setBrewingTime((int) (400. * (1. - (double) time / recipe.getCookingTime())));
|
||||
stand.update();
|
||||
}
|
||||
}
|
||||
|
||||
public class BrewingRecipe {
|
||||
|
||||
// ingredient
|
||||
private Type ingredientType;
|
||||
private String ingredientId;
|
||||
|
||||
private int time, experience;
|
||||
|
||||
// result
|
||||
private Type type;
|
||||
private String id;
|
||||
|
||||
private boolean valid = true;
|
||||
|
||||
public BrewingRecipe(ConfigurationSection section) {
|
||||
try {
|
||||
String[] split = section.getString("ingredient").split("\\.");
|
||||
ingredientType = MMOItems.plugin.getTypes().get(split[0]);
|
||||
ingredientId = split[1];
|
||||
|
||||
split = section.getString("result").split("\\.");
|
||||
type = MMOItems.plugin.getTypes().get(split[0]);
|
||||
id = split[1];
|
||||
|
||||
time = (int) (section.getDouble("cook-time") * 20.);
|
||||
experience = section.getInt("exp");
|
||||
} catch (Exception e) {
|
||||
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not register brewing recipe named " + section.getName());
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
public BrewingRecipe(Type ingredientType, String ingredientId, int time, Type type, String id) {
|
||||
this.ingredientType = ingredientType;
|
||||
this.ingredientId = ingredientId;
|
||||
this.time = time;
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public boolean matchesIngredient(NBTItem nbt) {
|
||||
return nbt.getString("MMOITEMS_ITEM_TYPE").equals(ingredientType.getId()) && nbt.getString("MMOITEMS_ITEM_ID").equals(ingredientId);
|
||||
}
|
||||
|
||||
public boolean matchesIngredient(ItemStack item) {
|
||||
return matchesIngredient(NBTItem.get(item));
|
||||
}
|
||||
|
||||
public int getCookingTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public ItemStack getResult() {
|
||||
return MMOItems.plugin.getItems().getItem(type, id);
|
||||
}
|
||||
}
|
||||
|
||||
// @EventHandler
|
||||
// public void a(ProjectileLaunchEvent event) {
|
||||
// if (!(event.getEntity() instanceof ThrownPotion) ||
|
||||
// !(event.getEntity().getShooter() instanceof Player))
|
||||
// return;
|
||||
//
|
||||
// double c = 1 + random.nextDouble() * 2;
|
||||
//
|
||||
// Vector vec = event.getEntity().getVelocity();
|
||||
// vec.setX(vec.getX() * c);
|
||||
// vec.setZ(vec.getZ() * c);
|
||||
// event.getEntity().setVelocity(vec);
|
||||
//
|
||||
// new PotionParticles((ThrownPotion) event.getEntity()).start();
|
||||
// }
|
||||
}
|
@ -1,134 +0,0 @@
|
||||
package net.Indyuce.mmocore.listener.profession;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.FurnaceSmeltEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmoitems.MMOItems;
|
||||
import net.Indyuce.mmoitems.api.Type;
|
||||
import net.mmogroup.mmolib.MMOLib;
|
||||
import net.mmogroup.mmolib.api.item.NBTItem;
|
||||
|
||||
public class Smelting implements Listener {
|
||||
|
||||
/*
|
||||
* FurnaceSmeltEvent is called when an item has actually been smelted.
|
||||
* FurnaceBurnEvent is called when a fuel piece is used to refill up the
|
||||
* furnace fuel bar.
|
||||
*/
|
||||
|
||||
private static Set<SmeltingRecipe> recipes = new HashSet<>();
|
||||
private static Set<NamespacedKey> vanillaKeys = new HashSet<>();
|
||||
|
||||
public Smelting(ConfigurationSection config) {
|
||||
for (Iterator<Recipe> iterator = Bukkit.recipeIterator(); iterator.hasNext();) {
|
||||
//Recipe recipe = iterator.next();
|
||||
//if (recipe instanceof FurnaceRecipe && vanillaKeys.contains(((FurnaceRecipe) recipe).getKey()))
|
||||
// iterator.remove();
|
||||
}
|
||||
|
||||
Smelting.recipes.clear();
|
||||
Smelting.vanillaKeys.clear();
|
||||
|
||||
for (String key : config.getKeys(false)) {
|
||||
SmeltingRecipe recipe = new SmeltingRecipe(config.getConfigurationSection(key));
|
||||
if (recipe.isValid()) {
|
||||
recipes.add(recipe);
|
||||
NamespacedKey vanillaKey = new NamespacedKey(MMOCore.plugin, "furnace_recipe_" + key.replace("-", "_").toLowerCase());
|
||||
vanillaKeys.add(vanillaKey);
|
||||
Bukkit.addRecipe(MMOLib.plugin.getVersion().getWrapper().getFurnaceRecipe(vanillaKey, new ItemStack(Material.BARRIER), recipe.getIngredientMaterial(), 0, recipe.getCookingTime()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void a(FurnaceSmeltEvent event) {
|
||||
NBTItem ingredient = NBTItem.get(event.getSource());
|
||||
SmeltingRecipe recipe = getCorrespondingRecipe(ingredient);
|
||||
if (recipe == null)
|
||||
event.setCancelled(true);
|
||||
else
|
||||
event.setResult(recipe.getResult());
|
||||
}
|
||||
|
||||
private SmeltingRecipe getCorrespondingRecipe(NBTItem item) {
|
||||
for (SmeltingRecipe recipe : recipes)
|
||||
if (recipe.matchesIngredient(item))
|
||||
return recipe;
|
||||
return null;
|
||||
}
|
||||
|
||||
public class SmeltingRecipe {
|
||||
|
||||
// ingredient & result
|
||||
private Type ingredientType, resultType;
|
||||
private String ingredientId, resultId;
|
||||
private Material ingredientMaterial;
|
||||
|
||||
private int time;
|
||||
|
||||
public SmeltingRecipe(ConfigurationSection section) {
|
||||
try {
|
||||
String[] split = section.getString("ingredient").split("\\.");
|
||||
ingredientMaterial = MMOItems.plugin.getItems().getItem(ingredientType = MMOItems.plugin.getTypes().get(split[0]), ingredientId = split[1]).getType();
|
||||
|
||||
split = section.getString("result").split("\\.");
|
||||
resultType = MMOItems.plugin.getTypes().get(split[0]);
|
||||
resultId = split[1];
|
||||
|
||||
time = (int) (section.getDouble("cook-time") * 20.);
|
||||
} catch (Exception e) {
|
||||
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load furnace recipe named " + section.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public SmeltingRecipe(Type ingredientType, String ingredientId, int time, Type resultType, String resultId) {
|
||||
this.ingredientType = ingredientType;
|
||||
this.ingredientId = ingredientId;
|
||||
this.time = time;
|
||||
this.resultType = resultType;
|
||||
this.resultId = resultId;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return ingredientType != null && ingredientId != null && time > 0 && resultType != null && resultId != null && ingredientMaterial != null;
|
||||
}
|
||||
|
||||
public boolean matchesIngredient(NBTItem nbt) {
|
||||
return nbt.getString("MMOITEMS_ITEM_TYPE").equals(ingredientType.getId()) && nbt.getString("MMOITEMS_ITEM_ID").equals(ingredientId);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public boolean matchesIngredient(ItemStack item) {
|
||||
return matchesIngredient(NBTItem.get(item));
|
||||
}
|
||||
|
||||
public int getCookingTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public ItemStack getResult() {
|
||||
return MMOItems.plugin.getItems().getItem(resultType, resultId);
|
||||
}
|
||||
|
||||
public Material getIngredientMaterial() {
|
||||
return ingredientMaterial;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "{ingredient=" + ingredientType.getId() + "." + ingredientId + ", time" + time + ", result=" + resultType.getId() + "." + resultId + ", valid=" + isValid() + "}";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user