Rewrote custom recipe listener in kotlin
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
package com.willfp.eco.internal.spigot.recipes;
|
||||
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent;
|
||||
|
||||
public interface RecipeValidator {
|
||||
|
||||
boolean validate(PrepareItemCraftEvent event);
|
||||
|
||||
boolean validate(CraftItemEvent event);
|
||||
|
||||
}
|
||||
@@ -1,379 +0,0 @@
|
||||
package com.willfp.eco.internal.spigot.recipes;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.items.Items;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import com.willfp.eco.core.recipe.Recipes;
|
||||
import com.willfp.eco.core.recipe.parts.MaterialTestableItem;
|
||||
import com.willfp.eco.core.recipe.parts.ModifiedTestableItem;
|
||||
import com.willfp.eco.core.recipe.parts.TestableStack;
|
||||
import com.willfp.eco.core.recipe.recipes.CraftingRecipe;
|
||||
import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent;
|
||||
import org.bukkit.event.player.PlayerRecipeDiscoverEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShapedRecipeListener extends PluginDependent<EcoPlugin> implements Listener {
|
||||
|
||||
private static final List<RecipeValidator> VALIDATORS = new ArrayList<>();
|
||||
|
||||
public static void registerValidator(RecipeValidator validator) {
|
||||
VALIDATORS.add(validator);
|
||||
}
|
||||
|
||||
public ShapedRecipeListener(@NotNull final EcoPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private void allow(@NotNull final Event event,
|
||||
@NotNull final CraftingRecipe recipe) {
|
||||
if (event instanceof PrepareItemCraftEvent) {
|
||||
((PrepareItemCraftEvent) event).getInventory().setResult(recipe.getOutput());
|
||||
}
|
||||
|
||||
if (event instanceof CraftItemEvent) {
|
||||
((CraftItemEvent) event).getInventory().setResult(recipe.getOutput());
|
||||
}
|
||||
}
|
||||
|
||||
private void deny(@NotNull final Event event) {
|
||||
if (event instanceof PrepareItemCraftEvent) {
|
||||
((PrepareItemCraftEvent) event).getInventory().setResult(new ItemStack(Material.AIR));
|
||||
}
|
||||
|
||||
if (event instanceof CraftItemEvent) {
|
||||
((CraftItemEvent) event).getInventory().setResult(new ItemStack(Material.AIR));
|
||||
((CraftItemEvent) event).setResult(Event.Result.DENY);
|
||||
((CraftItemEvent) event).setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void complexRecipeListener(@NotNull final PrepareItemCraftEvent event) {
|
||||
if (!(event.getRecipe() instanceof ShapedRecipe recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EcoPlugin.getPluginNames().contains(recipe.getKey().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getInventory().getViewers().get(0) instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack[] matrix = event.getInventory().getMatrix();
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
CraftingRecipe matched = Recipes.getMatch(matrix);
|
||||
|
||||
if (matched == null) {
|
||||
deny(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (matched.test(matrix)) {
|
||||
if (matched.getPermission() != null) {
|
||||
if (player.hasPermission(matched.getPermission())) {
|
||||
allow(event, matched);
|
||||
} else {
|
||||
deny(event);
|
||||
}
|
||||
} else {
|
||||
allow(event, matched);
|
||||
}
|
||||
} else {
|
||||
deny(event);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void complexRecipeListener(@NotNull final CraftItemEvent event) {
|
||||
if (!(event.getRecipe() instanceof ShapedRecipe recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EcoPlugin.getPluginNames().contains(recipe.getKey().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getInventory().getViewers().get(0) instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack[] matrix = event.getInventory().getMatrix();
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
CraftingRecipe matched = Recipes.getMatch(matrix);
|
||||
|
||||
if (matched == null) {
|
||||
deny(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (matched.test(matrix)) {
|
||||
if (matched.getPermission() != null) {
|
||||
if (player.hasPermission(matched.getPermission())) {
|
||||
allow(event, matched);
|
||||
} else {
|
||||
deny(event);
|
||||
}
|
||||
} else {
|
||||
allow(event, matched);
|
||||
}
|
||||
} else {
|
||||
deny(event);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void stackedRecipeListener(@NotNull final CraftItemEvent event) {
|
||||
if (!(event.getRecipe() instanceof ShapedRecipe recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EcoPlugin.getPluginNames().contains(recipe.getKey().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getInventory().getViewers().get(0) instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack[] matrix = event.getInventory().getMatrix();
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
CraftingRecipe matched = Recipes.getMatch(matrix);
|
||||
|
||||
if (matched == null) {
|
||||
deny(event);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isStackedRecipe = false;
|
||||
|
||||
int upperBound = 64;
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack inMatrix = event.getInventory().getMatrix()[i];
|
||||
TestableItem inRecipe = matched.getParts().get(i);
|
||||
|
||||
if (inRecipe instanceof TestableStack testableStack) {
|
||||
int max = Math.floorDiv(inMatrix.getAmount(), testableStack.getAmount());
|
||||
if (max < upperBound) {
|
||||
upperBound = max;
|
||||
}
|
||||
isStackedRecipe = true;
|
||||
} else if (inMatrix != null) {
|
||||
int max = inMatrix.getAmount();
|
||||
if (max < upperBound) {
|
||||
upperBound = max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isStackedRecipe) {
|
||||
return;
|
||||
}
|
||||
|
||||
int toGivePerRecipe = event.getRecipe().getResult().getAmount();
|
||||
int maxStackSize = event.getRecipe().getResult().getMaxStackSize();
|
||||
while (toGivePerRecipe * upperBound > maxStackSize) {
|
||||
upperBound--;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack inMatrix = event.getInventory().getMatrix()[i];
|
||||
TestableItem inRecipe = matched.getParts().get(i);
|
||||
|
||||
if (inRecipe instanceof TestableStack testableStack) {
|
||||
if (event.isShiftClick()) {
|
||||
int amount = inMatrix.getAmount() + 1;
|
||||
for (int j = 0; j < upperBound; j++) {
|
||||
amount -= testableStack.getAmount();
|
||||
}
|
||||
inMatrix.setAmount(amount);
|
||||
} else {
|
||||
inMatrix.setAmount(inMatrix.getAmount() - (testableStack.getAmount() - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int finalUpperBound = upperBound;
|
||||
|
||||
if (event.isShiftClick()) {
|
||||
ItemStack result = event.getInventory().getResult();
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.setAmount(result.getAmount() * finalUpperBound);
|
||||
event.getInventory().setResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void preventUsingComplexPartInEcoRecipe(@NotNull final PrepareItemCraftEvent event) {
|
||||
if (!(event.getRecipe() instanceof ShapedRecipe recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
CraftingRecipe craftingRecipe = Recipes.getRecipe(recipe.getKey());
|
||||
|
||||
if (!(craftingRecipe instanceof ShapedCraftingRecipe shapedCraftingRecipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack itemStack = event.getInventory().getMatrix()[i];
|
||||
TestableItem part = shapedCraftingRecipe.getParts().get(i);
|
||||
if (part instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (part instanceof ModifiedTestableItem modified) {
|
||||
if (modified.getHandle() instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (part instanceof TestableStack modified) {
|
||||
if (modified.getHandle() instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void preventUsingComplexPartInEcoRecipe(@NotNull final CraftItemEvent event) {
|
||||
if (!(event.getRecipe() instanceof ShapedRecipe recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
CraftingRecipe craftingRecipe = Recipes.getRecipe(recipe.getKey());
|
||||
|
||||
if (!(craftingRecipe instanceof ShapedCraftingRecipe shapedCraftingRecipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
ItemStack itemStack = event.getInventory().getMatrix()[i];
|
||||
TestableItem part = shapedCraftingRecipe.getParts().get(i);
|
||||
if (part instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (part instanceof ModifiedTestableItem modified) {
|
||||
if (modified.getHandle() instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (part instanceof TestableStack modified) {
|
||||
if (modified.getHandle() instanceof MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void preventUsingComplexPartInVanillaRecipe(@NotNull final PrepareItemCraftEvent event) {
|
||||
if (!(event.getRecipe() instanceof Keyed recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (EcoPlugin.getPluginNames().contains(recipe.getKey().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (ItemStack itemStack : event.getInventory().getMatrix()) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void preventUsingComplexPartInVanillaRecipe(@NotNull final CraftItemEvent event) {
|
||||
if (!(event.getRecipe() instanceof Keyed recipe)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (EcoPlugin.getPluginNames().contains(recipe.getKey().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (VALIDATORS.stream().anyMatch(validator -> validator.validate(event))) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (ItemStack itemStack : event.getInventory().getMatrix()) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void preventLearningDisplayedRecipes(@NotNull final PlayerRecipeDiscoverEvent event) {
|
||||
if (!EcoPlugin.getPluginNames().contains(event.getRecipe().getNamespace())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getRecipe().getKey().contains("_displayed")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,24 @@ import com.willfp.eco.core.integrations.shop.ShopManager
|
||||
import com.willfp.eco.core.items.Items
|
||||
import com.willfp.eco.internal.display.EcoDisplayHandler
|
||||
import com.willfp.eco.internal.drops.DropManager
|
||||
import com.willfp.eco.internal.entities.*
|
||||
import com.willfp.eco.internal.entities.EntityArgParserAdult
|
||||
import com.willfp.eco.internal.entities.EntityArgParserAttackDamage
|
||||
import com.willfp.eco.internal.entities.EntityArgParserAttackSpeed
|
||||
import com.willfp.eco.internal.entities.EntityArgParserBaby
|
||||
import com.willfp.eco.internal.entities.EntityArgParserCharged
|
||||
import com.willfp.eco.internal.entities.EntityArgParserExplosionRadius
|
||||
import com.willfp.eco.internal.entities.EntityArgParserFlySpeed
|
||||
import com.willfp.eco.internal.entities.EntityArgParserFollowRange
|
||||
import com.willfp.eco.internal.entities.EntityArgParserHealth
|
||||
import com.willfp.eco.internal.entities.EntityArgParserJumpStrength
|
||||
import com.willfp.eco.internal.entities.EntityArgParserKnockback
|
||||
import com.willfp.eco.internal.entities.EntityArgParserKnockbackResistance
|
||||
import com.willfp.eco.internal.entities.EntityArgParserName
|
||||
import com.willfp.eco.internal.entities.EntityArgParserNoAI
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSilent
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSize
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSpawnReinforcements
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSpeed
|
||||
import com.willfp.eco.internal.items.ArgParserColor
|
||||
import com.willfp.eco.internal.items.ArgParserCustomModelData
|
||||
import com.willfp.eco.internal.items.ArgParserEnchantment
|
||||
@@ -55,7 +72,19 @@ import com.willfp.eco.internal.spigot.integrations.anticheat.AnticheatMatrix
|
||||
import com.willfp.eco.internal.spigot.integrations.anticheat.AnticheatNCP
|
||||
import com.willfp.eco.internal.spigot.integrations.anticheat.AnticheatSpartan
|
||||
import com.willfp.eco.internal.spigot.integrations.anticheat.AnticheatVulcan
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.*
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefBentoBox
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefCombatLogXV10
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefCombatLogXV11
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefDeluxeCombat
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefFactionsUUID
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefGriefPrevention
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefIridiumSkyblock
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefKingdoms
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefLands
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefRPGHorses
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefSuperiorSkyblock2
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefTowny
|
||||
import com.willfp.eco.internal.spigot.integrations.antigrief.AntigriefWorldGuard
|
||||
import com.willfp.eco.internal.spigot.integrations.customentities.CustomEntitiesMythicMobs
|
||||
import com.willfp.eco.internal.spigot.integrations.customitems.CustomItemsCustomCrafting
|
||||
import com.willfp.eco.internal.spigot.integrations.customitems.CustomItemsExecutableItems
|
||||
@@ -76,6 +105,9 @@ import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.SkullProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.TPSProxy
|
||||
import com.willfp.eco.internal.spigot.recipes.ShapedRecipeListener
|
||||
import com.willfp.eco.internal.spigot.recipes.listeners.ComplexInComplex
|
||||
import com.willfp.eco.internal.spigot.recipes.listeners.ComplexInEco
|
||||
import com.willfp.eco.internal.spigot.recipes.listeners.ComplexInVanilla
|
||||
import com.willfp.eco.util.BlockUtils
|
||||
import com.willfp.eco.util.NumberUtils
|
||||
import com.willfp.eco.util.ServerUtils
|
||||
@@ -121,6 +153,10 @@ abstract class EcoSpigotPlugin : EcoPlugin(
|
||||
Entities.registerArgParser(EntityArgParserExplosionRadius())
|
||||
Entities.registerArgParser(EntityArgParserSilent())
|
||||
|
||||
ShapedRecipeListener.registerListener(ComplexInComplex())
|
||||
ShapedRecipeListener.registerListener(ComplexInEco())
|
||||
ShapedRecipeListener.registerListener(ComplexInVanilla())
|
||||
|
||||
val skullProxy = getProxy(SkullProxy::class.java)
|
||||
SkullUtils.initialize(
|
||||
{ meta, base64 -> skullProxy.setSkullTexture(meta, base64) },
|
||||
@@ -228,7 +264,11 @@ abstract class EcoSpigotPlugin : EcoPlugin(
|
||||
IntegrationLoader("ItemsAdder") { CustomItemsManager.register(CustomItemsItemsAdder()) },
|
||||
IntegrationLoader("HeadDatabase") { CustomItemsManager.register(CustomItemsHeadDatabase(this)) },
|
||||
IntegrationLoader("ExecutableItems") { CustomItemsManager.register(CustomItemsExecutableItems()) },
|
||||
IntegrationLoader("CustomCrafting") { CustomItemsManager.register(CustomItemsCustomCrafting()); ShapedRecipeListener.registerValidator(CustomRecipeCustomCrafting()) },
|
||||
IntegrationLoader("CustomCrafting") {
|
||||
CustomItemsManager.register(CustomItemsCustomCrafting()); ShapedRecipeListener.registerValidator(
|
||||
CustomRecipeCustomCrafting()
|
||||
)
|
||||
},
|
||||
|
||||
// Shop
|
||||
IntegrationLoader("ShopGUIPlus") { ShopManager.register(ShopShopGuiPlus()) },
|
||||
@@ -278,7 +318,7 @@ abstract class EcoSpigotPlugin : EcoPlugin(
|
||||
NaturalExpGainListeners(),
|
||||
ArmorListener(),
|
||||
EntityDeathByEntityListeners(this),
|
||||
ShapedRecipeListener(this),
|
||||
ShapedRecipeListener(),
|
||||
GUIListener(this),
|
||||
ArrowDataListener(this),
|
||||
ArmorChangeEventListeners(this),
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
package com.willfp.eco.internal.spigot.integrations.customrecipes
|
||||
|
||||
import com.willfp.eco.core.integrations.Integration
|
||||
import com.willfp.eco.internal.spigot.recipes.GenericCraftEvent
|
||||
import com.willfp.eco.internal.spigot.recipes.RecipeValidator
|
||||
import me.wolfyscript.customcrafting.CustomCrafting
|
||||
import org.bukkit.event.inventory.CraftItemEvent
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
class CustomRecipeCustomCrafting: RecipeValidator {
|
||||
override fun validate(event: CraftItemEvent): Boolean {
|
||||
if (event.inventory.viewers.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
val player = event.inventory.viewers[0]
|
||||
class CustomRecipeCustomCrafting : RecipeValidator, Integration {
|
||||
override fun validate(event: GenericCraftEvent): Boolean {
|
||||
val player = event.inventory.viewers.getOrNull(0) as? Player ?: return false
|
||||
return CustomCrafting.inst().craftManager.has(player.uniqueId)
|
||||
}
|
||||
|
||||
override fun validate(event: PrepareItemCraftEvent): Boolean {
|
||||
if (event.inventory.viewers.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
val player = event.inventory.viewers[0]
|
||||
return CustomCrafting.inst().craftManager.has(player.uniqueId)
|
||||
override fun getPluginName(): String {
|
||||
return "CustomCrafting"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.willfp.eco.internal.spigot.recipes
|
||||
|
||||
import com.willfp.eco.core.recipe.recipes.CraftingRecipe
|
||||
import org.bukkit.Keyed
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.event.Event
|
||||
import org.bukkit.event.inventory.CraftItemEvent
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent
|
||||
import org.bukkit.inventory.CraftingInventory
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
interface RecipeValidator {
|
||||
fun validate(event: GenericCraftEvent): Boolean
|
||||
}
|
||||
|
||||
interface RecipeListener {
|
||||
fun handle(event: GenericCraftEvent)
|
||||
}
|
||||
|
||||
interface GenericCraftEvent {
|
||||
val inventory: CraftingInventory
|
||||
val recipe: Keyed
|
||||
|
||||
fun allow(recipe: CraftingRecipe)
|
||||
fun deny()
|
||||
}
|
||||
|
||||
class WrappedPrepareItemCraftEvent(
|
||||
private val event: PrepareItemCraftEvent
|
||||
) : GenericCraftEvent {
|
||||
override val inventory: CraftingInventory
|
||||
get() = event.inventory
|
||||
|
||||
override val recipe: Keyed
|
||||
get() = event.recipe as Keyed
|
||||
|
||||
override fun allow(recipe: CraftingRecipe) {
|
||||
this.inventory.result = recipe.output
|
||||
}
|
||||
|
||||
override fun deny() {
|
||||
this.inventory.result = ItemStack(Material.AIR)
|
||||
}
|
||||
}
|
||||
|
||||
class WrappedCraftItemEvent(
|
||||
private val event: CraftItemEvent
|
||||
) : GenericCraftEvent {
|
||||
override val inventory: CraftingInventory
|
||||
get() = event.inventory
|
||||
|
||||
override val recipe: Keyed
|
||||
get() = event.recipe as Keyed
|
||||
|
||||
override fun allow(recipe: CraftingRecipe) {
|
||||
this.inventory.result = recipe.output
|
||||
}
|
||||
|
||||
override fun deny() {
|
||||
this.inventory.result = ItemStack(Material.AIR)
|
||||
event.result = Event.Result.DENY
|
||||
event.isCancelled = true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.willfp.eco.internal.spigot.recipes
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.recipe.Recipes
|
||||
import com.willfp.eco.core.recipe.parts.TestableStack
|
||||
import org.bukkit.Keyed
|
||||
import org.bukkit.event.EventHandler
|
||||
import org.bukkit.event.EventPriority
|
||||
import org.bukkit.event.Listener
|
||||
import org.bukkit.event.inventory.CraftItemEvent
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent
|
||||
import org.bukkit.event.player.PlayerRecipeDiscoverEvent
|
||||
import org.bukkit.inventory.ShapedRecipe
|
||||
|
||||
class ShapedRecipeListener : Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
fun stackedRecipeListener(event: CraftItemEvent) {
|
||||
val recipe = event.recipe as? ShapedRecipe ?: return
|
||||
|
||||
if (!EcoPlugin.getPluginNames().contains(recipe.key.namespace)) {
|
||||
return
|
||||
}
|
||||
|
||||
val matrix = event.inventory.matrix
|
||||
|
||||
val wrapped = WrappedCraftItemEvent(event)
|
||||
|
||||
if (validators.any { it.validate(wrapped) }) {
|
||||
return
|
||||
}
|
||||
|
||||
val matched = Recipes.getMatch(matrix)
|
||||
|
||||
if (matched == null) {
|
||||
wrapped.deny()
|
||||
return
|
||||
}
|
||||
|
||||
var isStackedRecipe = false
|
||||
var upperBound = 64
|
||||
for (i in 0..8) {
|
||||
val inMatrix = event.inventory.matrix[i]
|
||||
val inRecipe = matched.parts[i]
|
||||
|
||||
if (inRecipe is TestableStack) {
|
||||
val max = Math.floorDiv(inMatrix.amount, inRecipe.amount)
|
||||
if (max < upperBound) {
|
||||
upperBound = max
|
||||
}
|
||||
isStackedRecipe = true
|
||||
} else if (inMatrix != null) {
|
||||
val max = inMatrix.amount
|
||||
if (max < upperBound) {
|
||||
upperBound = max
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isStackedRecipe) {
|
||||
return
|
||||
}
|
||||
|
||||
val toGivePerRecipe = event.recipe.result.amount
|
||||
val maxStackSize = event.recipe.result.maxStackSize
|
||||
while (toGivePerRecipe * upperBound > maxStackSize) {
|
||||
upperBound--
|
||||
}
|
||||
|
||||
for (i in 0..8) {
|
||||
val inMatrix = event.inventory.matrix[i]
|
||||
val inRecipe = matched.parts[i]
|
||||
|
||||
if (inRecipe is TestableStack) {
|
||||
if (event.isShiftClick) {
|
||||
var amount = inMatrix.amount + 1
|
||||
for (j in 0..upperBound) {
|
||||
amount -= inRecipe.amount
|
||||
}
|
||||
inMatrix.amount = amount
|
||||
} else {
|
||||
inMatrix.amount = inMatrix.amount - (inRecipe.amount - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (event.isShiftClick) {
|
||||
val result = event.inventory.result ?: return
|
||||
|
||||
result.amount = result.amount * upperBound
|
||||
event.inventory.result = result
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun preventLearningDisplayedRecipes(event: PlayerRecipeDiscoverEvent) {
|
||||
if (!EcoPlugin.getPluginNames().contains(event.recipe.namespace)) {
|
||||
return
|
||||
}
|
||||
if (event.recipe.key.contains("_displayed")) {
|
||||
event.isCancelled = true
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun processListeners(event: PrepareItemCraftEvent) {
|
||||
if (event.recipe !is Keyed) {
|
||||
return
|
||||
}
|
||||
|
||||
for (listener in listeners) {
|
||||
listener.handle(WrappedPrepareItemCraftEvent(event))
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun processListeners(event: CraftItemEvent) {
|
||||
if (event.recipe !is Keyed) {
|
||||
return
|
||||
}
|
||||
|
||||
for (listener in listeners) {
|
||||
listener.handle(WrappedCraftItemEvent(event))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val validators = mutableListOf<RecipeValidator>()
|
||||
private val listeners = mutableListOf<RecipeListener>()
|
||||
|
||||
fun registerValidator(validator: RecipeValidator) {
|
||||
validators.add(validator)
|
||||
}
|
||||
|
||||
fun registerListener(listener: RecipeListener) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.willfp.eco.internal.spigot.recipes.listeners
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.recipe.Recipes
|
||||
import com.willfp.eco.internal.spigot.recipes.GenericCraftEvent
|
||||
import com.willfp.eco.internal.spigot.recipes.RecipeListener
|
||||
import com.willfp.eco.internal.spigot.recipes.ShapedRecipeListener
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
class ComplexInComplex : RecipeListener {
|
||||
override fun handle(event: GenericCraftEvent) {
|
||||
val recipe = event.recipe
|
||||
|
||||
if (!EcoPlugin.getPluginNames().contains(recipe.key.namespace)) {
|
||||
return
|
||||
}
|
||||
|
||||
val player = event.inventory.viewers.getOrNull(0) as? Player ?: return
|
||||
|
||||
val matrix = event.inventory.matrix
|
||||
|
||||
if (ShapedRecipeListener.validators.any { it.validate(event) }) {
|
||||
return
|
||||
}
|
||||
|
||||
val matched = Recipes.getMatch(matrix)
|
||||
|
||||
if (matched == null) {
|
||||
event.deny()
|
||||
return
|
||||
}
|
||||
|
||||
if (matched.test(matrix)) {
|
||||
if (matched.permission != null) {
|
||||
if (player.hasPermission(matched.permission!!)) {
|
||||
event.allow(matched)
|
||||
} else {
|
||||
event.deny()
|
||||
}
|
||||
} else {
|
||||
event.allow(matched)
|
||||
}
|
||||
} else {
|
||||
event.deny()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.willfp.eco.internal.spigot.recipes.listeners
|
||||
|
||||
import com.willfp.eco.core.items.Items
|
||||
import com.willfp.eco.core.recipe.Recipes
|
||||
import com.willfp.eco.core.recipe.parts.MaterialTestableItem
|
||||
import com.willfp.eco.core.recipe.parts.ModifiedTestableItem
|
||||
import com.willfp.eco.core.recipe.parts.TestableStack
|
||||
import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe
|
||||
import com.willfp.eco.internal.spigot.recipes.GenericCraftEvent
|
||||
import com.willfp.eco.internal.spigot.recipes.RecipeListener
|
||||
import com.willfp.eco.internal.spigot.recipes.ShapedRecipeListener
|
||||
|
||||
class ComplexInEco : RecipeListener {
|
||||
override fun handle(event: GenericCraftEvent) {
|
||||
val craftingRecipe = Recipes.getRecipe(event.recipe.key)
|
||||
|
||||
if (craftingRecipe !is ShapedCraftingRecipe) {
|
||||
return
|
||||
}
|
||||
|
||||
if (ShapedRecipeListener.validators.any { it.validate(event) }) {
|
||||
return
|
||||
}
|
||||
|
||||
for (i in 0..8) {
|
||||
val itemStack = event.inventory.matrix[i]
|
||||
val part = craftingRecipe.parts[i]
|
||||
when (part) {
|
||||
is MaterialTestableItem -> {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.deny()
|
||||
}
|
||||
}
|
||||
is ModifiedTestableItem -> {
|
||||
if (part.handle is MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.deny()
|
||||
}
|
||||
}
|
||||
}
|
||||
is TestableStack -> {
|
||||
if (part.handle is MaterialTestableItem) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.deny()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.willfp.eco.internal.spigot.recipes.listeners
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.items.Items
|
||||
import com.willfp.eco.internal.spigot.recipes.GenericCraftEvent
|
||||
import com.willfp.eco.internal.spigot.recipes.RecipeListener
|
||||
import com.willfp.eco.internal.spigot.recipes.ShapedRecipeListener
|
||||
|
||||
class ComplexInVanilla : RecipeListener {
|
||||
override fun handle(event: GenericCraftEvent) {
|
||||
if (EcoPlugin.getPluginNames().contains(event.recipe.key.namespace)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (ShapedRecipeListener.validators.any { it.validate(event) }) {
|
||||
return
|
||||
}
|
||||
|
||||
for (itemStack in event.inventory.matrix) {
|
||||
if (Items.isCustomItem(itemStack)) {
|
||||
event.deny()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user