Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/more info in auto enchant event #952

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoDisenchanter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.block.Block;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -18,6 +20,7 @@ public class AutoDisenchantEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private final ItemStack item;
private Block block;
private boolean cancelled;

public AutoDisenchantEvent(@Nonnull ItemStack item) {
Expand All @@ -26,6 +29,13 @@ public AutoDisenchantEvent(@Nonnull ItemStack item) {
this.item = item;
}

public AutoDisenchantEvent(@Nonnull ItemStack item, @Nullable Block block) {
super(true);

this.item = item;
this.block = block;
}

/**
* This returns the {@link ItemStack} that is being disenchanted.
*
Expand All @@ -36,6 +46,15 @@ public ItemStack getItem() {
return item;
}

/**
* This returns the {@link Block} that is disenchanting items
*
* @return The {@link Block} that is disenchanting items
*/
@Nullable public Block getBlock() {
return block;
}

@Override
public boolean isCancelled() {
return cancelled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoEnchanter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.block.Block;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -20,6 +22,7 @@ public class AutoEnchantEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private final ItemStack item;
private Block block;
private boolean cancelled;

public AutoEnchantEvent(@Nonnull ItemStack item) {
Expand All @@ -28,6 +31,13 @@ public AutoEnchantEvent(@Nonnull ItemStack item) {
this.item = item;
}

public AutoEnchantEvent(@Nonnull ItemStack item, @Nullable Block block) {
super(true);

this.item = item;
this.block = block;
}

/**
* This returns the {@link ItemStack} that is being enchanted.
*
Expand All @@ -38,6 +48,15 @@ public ItemStack getItem() {
return item;
}

/**
* This returns the {@link Block} that is enchanting items
*
* @return The {@link Block} that is enchanting items
*/
@Nullable public Block getBlock() {
return block;
}

@Override
public boolean isCancelled() {
return cancelled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) {
}

// Call an event so other Plugins can modify it.
AutoDisenchantEvent event = new AutoDisenchantEvent(item);
AutoDisenchantEvent event = new AutoDisenchantEvent(item, menu.getBlock());
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled()) {
if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) {
menu.replaceExistingItem(slot, null);
menu.pushItem(item, getOutputSlots());
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ public ItemStack getProgressBar() {
@Override
protected MachineRecipe findNextRecipe(BlockMenu menu) {
for (int slot : getInputSlots()) {
ItemStack item = menu.getItemInSlot(slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]);
// Other item
int otherSlot = slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0];
ItemStack item = menu.getItemInSlot(otherSlot);

// Check if the item is enchantable
if (!isEnchantable(item)) {
continue;
}

// Call an event so other Plugins can modify it.
AutoEnchantEvent event = new AutoEnchantEvent(item);
AutoEnchantEvent event = new AutoEnchantEvent(item, menu.getBlock());
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled()) {
if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) {
menu.replaceExistingItem(otherSlot, null);
menu.pushItem(item, getOutputSlots());
}
return null;
}

Expand Down
Loading