Skip to content

Commit

Permalink
Add methods
Browse files Browse the repository at this point in the history
- SlimefunItem#getGiveItemResult(Player)
- SlimefunItem#getPickBlockResult(HumanEntity, Block)
  • Loading branch information
JustAHuman-xD committed Oct 24, 2024
1 parent 0a7fea8 commit 971150b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
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(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(Player player, 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,7 @@ 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 +74,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

0 comments on commit 971150b

Please sign in to comment.