Skip to content

Commit

Permalink
fix(crafter): prevent using sf item in crafter (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Aug 24, 2024
1 parent 6f16a50 commit fdb765c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.CraftingTableListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.GrindstoneListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.SmithingTableListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.VanillaCrafterListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.BeeListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.EntityInteractionListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.FireworksListener;
Expand Down Expand Up @@ -708,6 +709,7 @@ private void registerListeners() {
new BeeWingsListener(this, (BeeWings) SlimefunItems.BEE_WINGS.getItem());
new PiglinListener(this);
new SmithingTableListener(this);
new VanillaCrafterListener(this);
new JoinListener(this);

// Item-specific Listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ default boolean isUnallowed(@Nullable ItemStack item) {
default boolean isUnallowed(@Nullable SlimefunItem item) {
return item != null && !(item instanceof VanillaItem) && !item.isDisabled();
}

default boolean isCraftingUnallowed(@Nullable ItemStack item) {
if (item == null) {
return false;
}

SlimefunItem sfItem = SlimefunItem.getByItem(item);
return isCraftingUnallowed(sfItem);
}

default boolean isCraftingUnallowed(@Nullable SlimefunItem item) {
return item != null && !item.isUseableInWorkbench();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting;

import city.norain.slimefun4.compatibillty.VersionedEvent;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import javax.annotation.Nonnull;
import org.bukkit.block.Crafter;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;

public class VanillaCrafterListener implements SlimefunCraftingListener {
public VanillaCrafterListener(@Nonnull Slimefun plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler(ignoreCancelled = true)
public void onCrafter(InventoryClickEvent e) {
Inventory clickedInventory = e.getClickedInventory();
Inventory topInventory = VersionedEvent.getTopInventory(e);

if (clickedInventory != null
&& topInventory.getType() == InventoryType.CRAFTER
&& topInventory.getHolder() instanceof Crafter
&& e.getWhoClicked() instanceof Player player) {

if (e.getAction() == InventoryAction.HOTBAR_SWAP) {
e.setCancelled(true);
return;
}

if (clickedInventory.getType() == InventoryType.CRAFTER) {
e.setCancelled(isCraftingUnallowed(SlimefunItem.getByItem(e.getCursor())));
} else {
e.setCancelled(isCraftingUnallowed(SlimefunItem.getByItem(e.getCurrentItem())));
}

if (e.getResult() == Event.Result.DENY) {
Slimefun.getLocalization().sendMessage((Player) e.getWhoClicked(), "crafter.not-working", true);
}
}
}

@EventHandler
public void hopperOnCrafter(InventoryMoveItemEvent e) {
if (e.getDestination().getType() == InventoryType.CRAFTER && isCraftingUnallowed(e.getItem())) {
e.setCancelled(true);
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/languages/en/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ workbench:
cauldron:
no-discoloring: '&4You cannot discolor Slimefun Armor'

crafter:
not-working: '&4You cannot use a Slimefun item in crafter!'

gps:
deathpoint: '&4Deathpoint &7%date%'
status-online: 'ONLINE'
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/languages/zh-CN/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ workbench:
not-enhanced: '&4你不能在普通的工作台上使用 Slimefun 物品'
cauldron:
no-discoloring: '&4你不能用炼药锅洗去 Slimefun 物品的颜色'
crafter:
not-working: '&4你不能在合成机上用 Slimefun 物品合成'
gps:
deathpoint: '&4死亡点 &7%date%'
status-online: '在线'
Expand Down

0 comments on commit fdb765c

Please sign in to comment.