org.bukkit.event.inventory.PrepareItemCraftEvent Java Examples

The following examples show how to use org.bukkit.event.inventory.PrepareItemCraftEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WoolMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler
public void handleWoolCrafting(PrepareItemCraftEvent event) {
  ItemStack result = event.getRecipe().getResult();
  InventoryHolder holder = event.getInventory().getHolder();

  if (holder instanceof Player) {
    MatchPlayer playerHolder = this.match.getPlayer((Player) holder);

    if (playerHolder != null && result != null && result.getType() == Material.WOOL) {
      for (MonumentWool wool : this.wools.values()) {
        if (wool.getDefinition().isObjectiveWool(result)) {
          if (!wool.getDefinition().isCraftable()) {
            playerHolder.sendWarning(
                TranslatableComponent.of("wool.craftingDisabled", wool.getComponentName()));
            event.getInventory().setResult(null);
          }
        }
      }
    }
  }
}
 
Example #2
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private PrepareItemCraftEvent mockPreCraftingEvent(ItemStack item) {
    Player player = server.addPlayer();

    CraftingInventory inv = Mockito.mock(CraftingInventory.class);
    MutableObject result = new MutableObject(new ItemStack(Material.EMERALD));

    Mockito.doAnswer(invocation -> {
        ItemStack argument = invocation.getArgument(0);
        result.setValue(argument);
        return null;
    }).when(inv).setResult(Mockito.any());

    Mockito.when(inv.getResult()).thenAnswer(invocation -> result.getValue());
    Mockito.when(inv.getContents()).thenReturn(new ItemStack[] { null, null, item, null, null, null, null, null, null });

    InventoryView view = player.openInventory(inv);
    PrepareItemCraftEvent event = new PrepareItemCraftEvent(inv, view, false);

    listener.onPrepareCraft(event);
    return event;
}
 
Example #3
Source File: RegionInteractListener.java    From NovaGuilds with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onPrepareItemCraft(PrepareItemCraftEvent event) {
	if(event.getViewers().isEmpty()) {
		return;
	}

	NovaPlayer nPlayer = PlayerManager.getPlayer(event.getViewers().get(0));

	if(event.getRecipe() == null
			|| event.getRecipe().getResult() == null
			|| event.getRecipe().getResult().getType() != Material.SHIELD
			|| !nPlayer.hasGuild()
			|| nPlayer.getGuild().getBannerMeta().numberOfPatterns() == 0) {
		return;
	}

	for(ItemStack ingredient : event.getInventory().getContents()) {
		if(ingredient != null
				&& ingredient.getType() == Material.SHIELD
				&& ingredient.hasItemMeta()) {
			return;
		}
	}

	event.getInventory().setResult(BannerUtils.applyMeta(event.getRecipe().getResult(), nPlayer.getGuild().getBannerMeta()));
}
 
Example #4
Source File: WoolMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler
public void handleWoolCrafting(PrepareItemCraftEvent event) {
    ItemStack result = event.getRecipe().getResult();
    InventoryHolder holder = event.getInventory().getHolder();

    if (holder instanceof Player) {
        MatchPlayer playerHolder = this.match.getPlayer((Player) holder);

        if (playerHolder != null && result != null && result.getType() == Material.WOOL) {
            for(MonumentWool wool : this.wools) {
                if(wool.getDefinition().isObjectiveWool(result)) {
                    if(!wool.getDefinition().isCraftable()) {
                        playerHolder.sendMessage(ChatColor.RED + PGMTranslations.t("match.wool.craftDisabled", playerHolder, BukkitUtils.woolMessage(wool.getDyeColor())));
                        event.getInventory().setResult(null);
                    }
                }
            }
        }
    }
}
 
Example #5
Source File: GoldenHeadsModule.java    From UHC with MIT License 6 votes vote down vote up
@EventHandler
public void on(PrepareItemCraftEvent event) {
    if (event.getRecipe().getResult().getType() != Material.GOLDEN_APPLE) return;

    final ItemStack centre = event.getInventory().getMatrix()[CRAFTING_INVENTORY_CENTRE_SLOT_ID];

    if (centre == null || centre.getType() != Material.SKULL_ITEM) return;

    if (!isEnabled()) {
        event.getInventory().setResult(new ItemStack(Material.AIR));
        return;
    }

    final SkullMeta meta = (SkullMeta) centre.getItemMeta();
    final ItemStack goldenHeadItem = getGoldenHeadItem(meta.hasOwner() ? meta.getOwner() : "Manually Crafted");
    event.getInventory().setResult(goldenHeadItem);
}
 
Example #6
Source File: PlayerListener.java    From Transport-Pipes with MIT License 6 votes vote down vote up
@EventHandler
public void onCraft(PrepareItemCraftEvent e) {
    if (e.getInventory().getViewers().isEmpty() || e.getInventory().getResult() == null) {
        return;
    }
    Player p = (Player) e.getInventory().getViewers().get(0);
    for (BaseDuctType bdt : ductRegister.baseDuctTypes()) {
        for (Object dt : bdt.ductTypes()) {
            if (e.getRecipe().getResult().isSimilar(bdt.getItemManager().getItem((DuctType) dt))) {
                if (!((DuctType) dt).hasPlayerCraftingPermission(p)) {
                    e.getInventory().setResult(null);
                    return;
                }
            }
        }
    }
    if (itemService.isWrench(e.getRecipe().getResult())) {
        if (!p.hasPermission("transportpipes.craft.wrench")) {
            e.getInventory().setResult(null);
        }
    }
}
 
Example #7
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onCraftBlacklisted(PrepareItemCraftEvent event){
		
	if(event.getRecipe() == null)return;
	String key = ItemUtils.getRecipeKey(event.getRecipe(), false);
	String key2 = ItemUtils.getRecipeKey(event.getRecipe(), true);
	if(ProRecipes.getPlugin().blacklisted.contains(key) || ProRecipes.getPlugin().blacklisted.contains(key2)){
		event.getInventory().setItem(0, null);
		event.getInventory().setResult(new ItemStack(Material.AIR));
		return;
	}

}
 
Example #8
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPreCraftEventWithVanillaItem() {
    VanillaItem item = TestUtilities.mockVanillaItem(plugin, Material.GOLD_INGOT, true);
    item.register(plugin);

    PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem());
    Assertions.assertNotNull(event.getInventory().getResult());
}
 
Example #9
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPreCraftEventWithSlimefunItem() {
    SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "MOCK_DIAMOND2", new CustomItem(Material.DIAMOND, "&cMock Diamond"));
    item.register(plugin);

    PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem());
    Assertions.assertNull(event.getInventory().getResult());
}
 
Example #10
Source File: VanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onPrepareCraft(PrepareItemCraftEvent e) {
    if (e.getInventory().getResult() != null) {
        for (ItemStack item : e.getInventory().getContents()) {
            SlimefunItem sfItem = SlimefunItem.getByItem(item);

            if (sfItem != null && !sfItem.isUseableInWorkbench()) {
                e.getInventory().setResult(null);
                break;
            }
        }
    }
}
 
Example #11
Source File: GlisteringMelonRecipeModule.java    From UHC with MIT License 5 votes vote down vote up
@EventHandler
public void on(PrepareItemCraftEvent event) {
    final Recipe recipe = event.getRecipe();

    if (recipe.getResult().getType() != Material.SPECKLED_MELON) return;

    if (RecipeUtil.hasRecipeGotMaterial(recipe, isEnabled() ? Material.GOLD_NUGGET : Material.GOLD_BLOCK)) {
        event.getInventory().setResult(new ItemStack(Material.AIR));
    }
}
 
Example #12
Source File: GoldenCarrotRecipeModule.java    From UHC with MIT License 5 votes vote down vote up
@EventHandler
public void on(PrepareItemCraftEvent event) {
    final Recipe recipe = event.getRecipe();

    if (recipe.getResult().getType() != Material.GOLDEN_CARROT) return;

    if (RecipeUtil.hasRecipeGotMaterial(recipe, isEnabled() ? Material.GOLD_NUGGET : Material.GOLD_INGOT)) {
        event.getInventory().setResult(new ItemStack(Material.AIR));
    }
}
 
Example #13
Source File: PlayerListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onCraftItem(PrepareItemCraftEvent e) {
    if (e.getView().getPlayer() instanceof Player) {
        Player p = (Player) e.getView().getPlayer();

        ItemStack result = e.getInventory().getResult();

        if (RedProtect.get().getUtil().denyPotion(result, p)) {
            e.getInventory().setResult(new ItemStack(Material.AIR));
        }
    }
}
 
Example #14
Source File: ItemCraftListener.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler
public void onItemCraft(PrepareItemCraftEvent event) {
    try {
        final CraftingInventory inventory = event.getInventory();
        if (inventory.getResult() == null) return;

        for (ItemStack itemStack : inventory.getContents()) {
            if (Utils.getCrystals(itemStack) == 0) continue;
            inventory.setResult(null);
            return;
        }
    } catch (Exception e) {
        IridiumSkyblock.getInstance().sendErrorMessage(e);
    }
}
 
Example #15
Source File: ItemModifyMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onPrepareItemCraft(PrepareItemCraftEvent event) {
    ItemStack stack = event.getInventory().getResult();
    if(applyRules(stack)) {
        event.getInventory().setResult(stack);
    }
}
 
Example #16
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public static ItemStack callPreCraftEvent(InventoryCrafting matrix, ItemStack result, InventoryView lastCraftView, boolean isRepair) {
    CraftInventoryCrafting inventory = new CraftInventoryCrafting(matrix, matrix.resultInventory);
    inventory.setResult(CraftItemStack.asCraftMirror(result));

    PrepareItemCraftEvent event = new PrepareItemCraftEvent(inventory, lastCraftView, isRepair);
    Bukkit.getPluginManager().callEvent(event);

    org.bukkit.inventory.ItemStack bitem = event.getInventory().getResult();

    return CraftItemStack.asNMSCopy(bitem);
}
 
Example #17
Source File: CraftItemListener.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPrepare(PrepareItemCraftEvent event) {
	if (MineTinker.getPlugin().getConfig().getBoolean("ModifiersCanBeUsedForCrafting")) return;
	CraftingInventory inv = event.getInventory();
	for (ItemStack is : inv.getMatrix()) {
		if (is == null) continue;
		if (modManager.isModifierItem(is)) {
			inv.setResult(null);
			break;
		}
	}
}
 
Example #18
Source File: ItemModifyMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onPrepareItemCraft(PrepareItemCraftEvent event) {
  ItemStack stack = event.getInventory().getResult();
  if (applyRules(stack)) {
    event.getInventory().setResult(stack);
  }
}
 
Example #19
Source File: CraftingMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void removeDisabledRecipe(PrepareItemCraftEvent event) {
  CraftingInventory crafting = event.getInventory();
  ItemStack result = crafting.getResult();
  if (result == null) {
    return;
  }

  for (SingleMaterialMatcher disabled : disabledRecipes) {
    if (disabled.matches(result)) {
      crafting.setResult(null);
      break;
    }
  }
}
 
Example #20
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
public void callPrepare(CraftingInventory inv, InventoryView v){
	PrepareItemCraftEvent event = new PrepareItemCraftEvent(inv, v, false);
	ProRecipes.getPlugin().getServer().getPluginManager().callEvent(event);
}
 
Example #21
Source File: ConvertToolListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onCraft(PrepareItemCraftEvent event) {
	CraftingInventory inv = event.getInventory();

	boolean canConvert = false;
	World world = null;

	HumanEntity humanEntity = null;

	for (HumanEntity human : inv.getViewers()) {
		if (human.hasPermission("minetinker.tool.create")) {
			canConvert = true;
			world = human.getWorld();
			humanEntity = human;
			break;
		}
	}

	if (!canConvert) {
		return;
	}

	if (Lists.WORLDS.contains(world.getName())) {
		return;
	}

	int recipeItems = 0;
	ItemStack lastItem = null;

	for (ItemStack item : inv.getMatrix()) {
		if (item != null && item.getType() != Material.AIR) {
			recipeItems += 1;
			lastItem = item;
		}
	}

	if (recipeItems == 1) {
		if (modManager.isArmorViable(lastItem) || modManager.isToolViable(lastItem) || modManager.isWandViable(lastItem)) {
			inv.setResult(new ItemStack(Material.AIR, 1));
			return;
		}

		if (ToolType.isMaterialCompatible(lastItem.getType())) {
			inv.setResult(lastItem);
			modManager.convertItemStack(event.getInventory().getResult(), humanEntity);
			inv.getResult().setAmount(1);
		}
	}
}
 
Example #22
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testPreCraftEventWithoutSlimefunItems() {
    PrepareItemCraftEvent event = mockPreCraftingEvent(new ItemStack(Material.DIAMOND));
    Assertions.assertNotNull(event.getInventory().getResult());
}
 
Example #23
Source File: ItemMods.java    From CardinalPGM with MIT License 4 votes vote down vote up
@EventHandler
public void onPlayerCraft(PrepareItemCraftEvent event) {
    applyRules(event.getInventory().getResult());
}