Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
Prevent ingot crafting from the same metal blocks (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB committed Oct 16, 2020
1 parent c8c5b6f commit 6cbec9f
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapelessRecipe;

import com.hm.achievement.category.MultipleAchievements;
import com.hm.achievement.db.CacheManager;
Expand Down Expand Up @@ -46,7 +48,8 @@ public CraftsListener(@Named("main") CommentedYamlConfiguration mainConfig, int
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCraftItem(CraftItemEvent event) {
if (!(event.getWhoClicked() instanceof Player) || event.getAction() == InventoryAction.NOTHING
|| event.getClick() == ClickType.NUMBER_KEY && event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD) {
|| event.getClick() == ClickType.NUMBER_KEY && event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD
|| isCraftingIngotFromBlock(event.getRecipe())) {
return;
}

Expand Down Expand Up @@ -81,4 +84,28 @@ public void onCraftItem(CraftItemEvent event) {

updateStatisticAndAwardAchievementsIfAvailable(player, foundAchievements, eventAmount);
}

/**
* Metal blocks can be used for repeated crafts of ingots (e.g. iron block -> 9 iron ingots -> iron block -> ...).
* Detect and prevent this.
*
* @param recipe
* @return true if the player is trying to craft ingots from a block of the same metal
*/
private boolean isCraftingIngotFromBlock(Recipe recipe) {
if (recipe instanceof ShapelessRecipe) {
ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe;
List<ItemStack> ingredientList = shapelessRecipe.getIngredientList();
if (ingredientList.size() == 1) {
Material resultMaterial = shapelessRecipe.getResult().getType();
Material ingredientMaterial = ingredientList.get(0).getType();
if (resultMaterial == Material.GOLD_INGOT && ingredientMaterial == Material.GOLD_BLOCK
|| resultMaterial == Material.NETHERITE_INGOT && ingredientMaterial == Material.NETHERITE_BLOCK
|| resultMaterial == Material.IRON_INGOT && ingredientMaterial == Material.IRON_BLOCK) {
return true;
}
}
}
return false;
}
}

0 comments on commit 6cbec9f

Please sign in to comment.