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

[Api] Give & Pick Slimefun Item api #4252

Open
wants to merge 3 commits into
base: master
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 @@ -17,6 +17,7 @@
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand All @@ -39,6 +40,7 @@
import io.github.thebusybiscuit.slimefun4.core.attributes.NotConfigurable;
import io.github.thebusybiscuit.slimefun4.core.attributes.Placeable;
import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.GiveCommand;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.handlers.GlobalItemHandler;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
Expand Down Expand Up @@ -222,6 +224,34 @@ protected SlimefunItem(ItemGroup itemGroup, ItemStack item, String id, RecipeTyp
return itemStackTemplate;
}

/**
* This returns the {@link ItemStack} result when a {@link Player}
* is given this item via the {@link GiveCommand}.
*
* @param player
* The {@link Player} who will receive the item
*
* @return The {@link ItemStack} that is given to the {@link Player}
* */
public @Nonnull ItemStack getGiveItemResult(@Nonnull Player player) {
return getItem();
}

/**
* This returns the {@link ItemStack} result when a {@link Player}
* in creative mode middle-clicks this {@link SlimefunItem}'s {@link Block}.
*
* @param player
* The {@link Player} who middle-clicked this {@link SlimefunItem}
* @param block
* The {@link Block} middle-clicked
*
* @return The {@link ItemStack} that is picked when middle-clicking this {@link SlimefunItem}
*/
public @Nonnull ItemStack getPickBlockResult(@Nonnull Player player, @Nonnull Block block) {
return getItem();
}

/**
* This returns the {@link ItemGroup} of our {@link SlimefunItem}, every {@link SlimefunItem}
* is associated with exactly one {@link ItemGroup}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

class GiveCommand extends SubCommand {
public class GiveCommand extends SubCommand {

private static final String PLACEHOLDER_PLAYER = "%player%";
private static final String PLACEHOLDER_ITEM = "%item%";
Expand Down Expand Up @@ -65,7 +65,7 @@ private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, Strin

if (amount > 0) {
Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount)));
Map<Integer, ItemStack> excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount));
Map<Integer, ItemStack> excess = p.getInventory().addItem(new CustomItemStack(sfItem.getGiveItemResult(p), amount));
if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) {
for (ItemStack is : excess.values()) {
p.getWorld().dropItem(p.getLocation(), is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
Expand Down Expand Up @@ -41,8 +42,10 @@ public void onInventoryCreativeEvent(InventoryCreativeEvent e) {
* ClickType is not MIDDLE but CREATIVE (because this ClickType covers
* multiple cases, we have to filter out more later on)
*/
if (e.getClick() == ClickType.CREATIVE && e.getSlotType() == SlotType.QUICKBAR) {
HumanEntity player = e.getWhoClicked();
if (e.getClick() == ClickType.CREATIVE
&& e.getSlotType() == SlotType.QUICKBAR
&& e.getWhoClicked() instanceof Player player
) {
// get the block the player is looking at for later
Block b = player.getTargetBlockExact(5);

Expand Down Expand Up @@ -74,7 +77,7 @@ public void onInventoryCreativeEvent(InventoryCreativeEvent e) {
}

// Give the item, doing it like this will not alter any other cases.
e.setCursor(sfItem.getItem().clone());
e.setCursor(sfItem.getPickBlockResult(player, b).clone());
}
}

Expand Down