Compare commits

..

13 Commits

Author SHA1 Message Date
Auxilor
92f8787eb9 Updated to 6.26.3 2022-03-01 13:03:56 +00:00
Auxilor
5403f7a9ed Fixed multiple option display 2022-03-01 11:29:51 +00:00
Auxilor
2924d6f560 All options are now shown to players in crafting 2022-03-01 11:22:31 +00:00
Auxilor
36c0708c17 (Narrator voice) It wasn't 2022-03-01 10:56:54 +00:00
Auxilor
b9fe54e883 This isn't going to be the last commit, is it 2022-03-01 10:49:09 +00:00
Auxilor
5cc2f23547 Removed debug messages 2022-03-01 10:40:07 +00:00
Auxilor
30514ba780 Suffering 2022-03-01 10:39:24 +00:00
Auxilor
02e6c5e8f3 More working around how broken spigot is 2022-03-01 10:35:49 +00:00
Auxilor
fac97329aa Working around how broken spigot is 2022-03-01 10:31:19 +00:00
Auxilor
cdf5cc3abe Suppressed warnings 2022-03-01 10:25:43 +00:00
Auxilor
4cb630d201 Fixed crafting, deprecated now-unneeded API 2022-03-01 10:24:38 +00:00
Auxilor
79d6277d94 Updated to 6.26.2 2022-03-01 08:33:24 +00:00
Auxilor
1a90fa6707 Fixed ComplexInEco 2022-03-01 08:33:14 +00:00
12 changed files with 195 additions and 243 deletions

View File

@@ -57,6 +57,23 @@ public class GroupedTestableItems implements TestableItem {
throw new IllegalStateException("Empty group of children!");
}
/**
* Get matching child for an ItemStack.
*
* @param itemStack The ItemStack.
* @return The matching child, or null if the item matches nothing.
*/
@Nullable
public TestableItem getMatchingChild(@NotNull final ItemStack itemStack) {
for (TestableItem child : children) {
if (child.matches(itemStack)) {
return child;
}
}
return null;
}
/**
* Get the children.
*

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.core.recipe.parts;
import com.willfp.eco.core.items.Items;
import com.willfp.eco.core.items.TestableItem;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
@@ -35,7 +36,13 @@ public class MaterialTestableItem implements TestableItem {
*/
@Override
public boolean matches(@Nullable final ItemStack itemStack) {
return itemStack != null && itemStack.getType() == material;
boolean simpleMatches = itemStack != null && itemStack.getType() == material;
if (!simpleMatches) {
return false;
}
return !Items.isCustomItem(itemStack);
}
@Override

View File

@@ -7,7 +7,9 @@ import com.willfp.eco.core.Prerequisite;
import com.willfp.eco.core.items.TestableItem;
import com.willfp.eco.core.recipe.Recipes;
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
import com.willfp.eco.core.recipe.parts.GroupedTestableItems;
import com.willfp.eco.core.recipe.parts.TestableStack;
import com.willfp.eco.util.ListUtils;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
@@ -66,9 +68,10 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
@Override
public boolean test(@NotNull final ItemStack[] matrix) {
List<ItemStack> dynamicMatrix = Arrays.asList(matrix);
boolean matches = true;
for (int i = 0; i < 9; i++) {
if (!parts.get(i).matches(matrix[i])) {
if (!parts.get(i).matches(ListUtils.getOrNull(dynamicMatrix, i))) {
matches = false;
}
}
@@ -102,23 +105,38 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
}
char character = String.valueOf(i).toCharArray()[0];
ItemStack item = parts.get(i).getItem();
if (parts.get(i) instanceof TestableStack) {
ItemMeta meta = item.getItemMeta();
assert meta != null;
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
assert lore != null;
lore.add("");
String add = Eco.getHandler().getEcoPlugin().getLangYml().getFormattedString("multiple-in-craft");
add = add.replace("%amount%", String.valueOf(item.getAmount()));
lore.add(add);
meta.setLore(lore);
item.setItemMeta(meta);
List<TestableItem> items = new ArrayList<>();
if (parts.get(i) instanceof GroupedTestableItems group) {
items.addAll(group.getChildren());
} else {
items.add(parts.get(i));
}
displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(item));
List<ItemStack> displayedItems = new ArrayList<>();
for (TestableItem testableItem : items) {
if (testableItem instanceof TestableStack) {
ItemStack item = testableItem.getItem().clone();
ItemMeta meta = item.getItemMeta();
assert meta != null;
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
assert lore != null;
lore.add("");
String add = Eco.getHandler().getEcoPlugin().getLangYml().getFormattedString("multiple-in-craft");
add = add.replace("%amount%", String.valueOf(item.getAmount()));
lore.add(add);
meta.setLore(lore);
item.setItemMeta(meta);
displayedItems.add(item);
} else {
displayedItems.add(testableItem.getItem());
}
}
displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(displayedItems));
}
if (Prerequisite.HAS_1_18.isMet() && !Prerequisite.HAS_PAPER.isMet()) {

View File

@@ -1,6 +1,5 @@
package com.willfp.eco.util;
import org.apache.commons.lang.Validate;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
@@ -9,28 +8,16 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
/**
* Utilities / API methods for blocks.
*/
public final class BlockUtils {
/**
* If the meta set function has been set.
*/
private static boolean initialized = false;
/**
* The block break function.
*/
private static BiConsumer<Player, Block> blockBreakConsumer = null;
private static Set<Block> getNearbyBlocks(@NotNull final Block start,
@NotNull final List<Material> allowedMaterials,
@NotNull final Set<Block> blocks,
@@ -75,12 +62,11 @@ public final class BlockUtils {
*
* @param player The player to break the block as.
* @param block The block to break.
* @deprecated Added into spigot API in 1.17.1
*/
@Deprecated(since = "6.26.2", forRemoval = true)
public static void breakBlock(@NotNull final Player player,
@NotNull final Block block) {
Validate.isTrue(initialized, "Must be initialized!");
Validate.notNull(blockBreakConsumer, "Must be initialized!");
Location location = block.getLocation();
World world = location.getWorld();
assert world != null;
@@ -89,7 +75,7 @@ public final class BlockUtils {
return;
}
blockBreakConsumer.accept(player, block);
player.breakBlock(block);
}
/**
@@ -107,20 +93,6 @@ public final class BlockUtils {
);
}
/**
* Initialize the block break function.
*
* @param function The function.
*/
@ApiStatus.Internal
public static void initialize(@NotNull final BiConsumer<Player, Block> function) {
Validate.isTrue(!initialized, "Already initialized!");
blockBreakConsumer = function;
initialized = true;
}
private BlockUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -1,14 +0,0 @@
package com.willfp.eco.internal.spigot.proxy.v1_17_R1
import com.willfp.eco.internal.spigot.proxy.BlockBreakProxy
import org.bukkit.block.Block
import org.bukkit.entity.Player
class BlockBreak : BlockBreakProxy {
override fun breakBlock(
player: Player,
block: Block
) {
player.breakBlock(block)
}
}

View File

@@ -1,14 +0,0 @@
package com.willfp.eco.internal.spigot.proxy.v1_18_R1
import com.willfp.eco.internal.spigot.proxy.BlockBreakProxy
import org.bukkit.block.Block
import org.bukkit.entity.Player
class BlockBreak : BlockBreakProxy {
override fun breakBlock(
player: Player,
block: Block
) {
player.breakBlock(block)
}
}

View File

@@ -106,15 +106,13 @@ import com.willfp.eco.internal.spigot.integrations.mcmmo.McmmoIntegrationImpl
import com.willfp.eco.internal.spigot.integrations.multiverseinventories.MultiverseInventoriesIntegration
import com.willfp.eco.internal.spigot.integrations.shop.ShopShopGuiPlus
import com.willfp.eco.internal.spigot.math.evaluateExpression
import com.willfp.eco.internal.spigot.proxy.BlockBreakProxy
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.StackedRecipeListener
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
import com.willfp.eco.util.SkullUtils
@@ -158,7 +156,6 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
Entities.registerArgParser(EntityArgParserEquipment())
ShapedRecipeListener.registerListener(ComplexInComplex())
ShapedRecipeListener.registerListener(ComplexInEco())
ShapedRecipeListener.registerListener(ComplexInVanilla())
SegmentParserGroup().register()
@@ -170,9 +167,6 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
{ meta -> skullProxy.getSkullTexture(meta) }
)
val blockBreakProxy = getProxy(BlockBreakProxy::class.java)
BlockUtils.initialize { player, block -> blockBreakProxy.breakBlock(player, block) }
val tpsProxy = getProxy(TPSProxy::class.java)
ServerUtils.initialize { tpsProxy.getTPS() }
@@ -322,6 +316,7 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
ArmorListener(),
EntityDeathByEntityListeners(this),
ShapedRecipeListener(),
StackedRecipeListener(this),
GUIListener(this),
ArrowDataListener(this),
ArmorChangeEventListeners(this),

View File

@@ -2,95 +2,14 @@ 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.getOrNull(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)) {

View File

@@ -0,0 +1,131 @@
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.EmptyTestableItem
import com.willfp.eco.core.recipe.parts.GroupedTestableItems
import com.willfp.eco.core.recipe.parts.TestableStack
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.CraftingInventory
import kotlin.math.max
import kotlin.math.min
class StackedRecipeListener(
private val plugin: EcoPlugin
) : Listener {
/*
If you think you can fix this code, you're wrong.
Or, pray to whatever god you have that you can figure it out.
Best of luck, you're going to need it.
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
fun handleStacks(event: InventoryClickEvent) {
val inventory = event.clickedInventory as? CraftingInventory ?: return
if (event.slot != 0) {
return
}
// Just in case
if (EmptyTestableItem().matches(inventory.getItem(event.slot))) {
return
}
val matrix = inventory.matrix
val recipe = Recipes.getMatch(matrix) ?: return
var isStackedRecipe = false
var maxCraftable = Int.MAX_VALUE
// Start by calculating the maximum number of items to craft
for (i in 0..8) {
val item = inventory.matrix.getOrNull(i) ?: continue
val part = recipe.parts[i].let {
if (it is GroupedTestableItems) {
it.getMatchingChild(item)
} else it
} ?: continue
if (part is TestableStack) {
isStackedRecipe = true
}
maxCraftable = min(maxCraftable, Math.floorDiv(item.amount, part.item.amount))
}
if (!isStackedRecipe) {
return
}
// Don't allow crafting above the max stack size of the output
maxCraftable = min(maxCraftable, Math.floorDiv(recipe.output.maxStackSize, recipe.output.amount))
// Run this first before the deduction or shift-clicking breaks
val existingResult = inventory.result
// Deduct the correct number of items from the inventory
for (i in 0..8) {
val item = inventory.matrix.getOrNull(i) ?: continue
val part = recipe.parts[i].let {
if (it is GroupedTestableItems) {
it.getMatchingChild(item)
} else it
} ?: continue
val amount = max(
if (event.isShiftClick) {
item.amount - (part.item.amount * maxCraftable)
} else {
item.amount - part.item.amount
}, 0
)
// Anti-Underflow
if (amount == 0) {
item.type = Material.AIR
}
item.amount = amount
/*
Everything below this point is unreadable garbage
If you want to modify the behaviour of stacked recipes, then
change the code above. The code after this just sets the items
in the inventory, despite spigot trying to stop me.
*/
// Do it twice because spigot hates me
// Everything has to be cloned because the inventory changes the item
inventory.matrix[i] = item.clone() // Use un-cloned version first
// This isn't even funny anymore
runTwice {
val newItem = item.clone()
// Just use every method possible to set the item
inventory.matrix[i] = newItem
inventory.setItem(i + 1, newItem)
// Just to be safe, modify the instance (safe check) Using ?. causes a warning.
@Suppress("SENSELESS_COMPARISON") // I hate compiler warnings
if (inventory.matrix[i] != null) {
inventory.matrix[i].amount = amount
}
}
}
// Multiply the result by the amount to craft if shift-clicking
existingResult ?: return
// Modify the item and then set it
if (event.isShiftClick) {
existingResult.amount *= maxCraftable
}
inventory.result = existingResult
}
private fun runTwice(block: () -> Unit) {
block()
plugin.scheduler.run(block)
}
}

View File

@@ -1,68 +0,0 @@
package com.willfp.eco.internal.spigot.recipes.listeners
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.GroupedTestableItems
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
import org.bukkit.inventory.ItemStack
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]
if (part.isCustomWhenShouldNotBe(itemStack)) {
event.deny()
}
}
}
}
private fun TestableItem.isCustomWhenShouldNotBe(itemStack: ItemStack): Boolean {
when (this) {
is MaterialTestableItem -> {
if (Items.isCustomItem(itemStack)) {
return true
}
}
is ModifiedTestableItem -> {
if (this.handle is MaterialTestableItem) {
if (Items.isCustomItem(itemStack)) {
return true
}
}
}
is TestableStack -> {
if (this.handle is MaterialTestableItem) {
if (Items.isCustomItem(itemStack)) {
return true
}
}
}
is GroupedTestableItems -> {
// This will fail if and only if there is a complex item grouped with a simple item of the same type
if (this.children.any { it.isCustomWhenShouldNotBe(itemStack) && it.matches(itemStack) }) {
return true
}
}
}
return false
}

View File

@@ -1,11 +0,0 @@
package com.willfp.eco.internal.spigot.proxy
import org.bukkit.block.Block
import org.bukkit.entity.Player
interface BlockBreakProxy {
fun breakBlock(
player: Player,
block: Block
)
}

View File

@@ -1,3 +1,3 @@
version = 6.26.1
version = 6.26.3
plugin-name = eco
kotlin.code.style = official