77import io .github .thebusybiscuit .slimefun4 .implementation .Slimefun ;
88import io .github .thebusybiscuit .slimefun4 .implementation .items .SimpleSlimefunItem ;
99import io .github .thebusybiscuit .slimefun4 .libraries .dough .data .persistent .PersistentDataAPI ;
10+ import io .github .thebusybiscuit .slimefun4 .libraries .dough .items .ItemUtils ;
1011import io .github .thebusybiscuit .slimefun4 .libraries .dough .protection .Interaction ;
1112import me .voper .slimeframe .SlimeFrame ;
1213import me .voper .slimeframe .core .datatypes .MerchantRecipeListDataType ;
2122import org .bukkit .Sound ;
2223import org .bukkit .enchantments .Enchantment ;
2324import org .bukkit .entity .AbstractVillager ;
25+ import org .bukkit .entity .Player ;
2426import org .bukkit .inventory .ItemFlag ;
2527import org .bukkit .inventory .ItemStack ;
2628import org .bukkit .inventory .MerchantRecipe ;
2729import org .bukkit .inventory .meta .ItemMeta ;
2830
2931import javax .annotation .Nonnull ;
3032import java .util .ArrayList ;
33+ import java .util .HashMap ;
3134import java .util .List ;
3235import java .util .concurrent .ThreadLocalRandom ;
3336import java .util .function .Predicate ;
@@ -54,7 +57,9 @@ public MerchantSoulContract(ItemGroup itemGroup, SlimefunItemStack item, RecipeT
5457 @ Override
5558 public EntityInteractHandler getItemHandler () {
5659 return (e , item , offhand ) -> {
57- if (e .isCancelled () || !Slimefun .getProtectionManager ().hasPermission (e .getPlayer (), e .getRightClicked ().getLocation (), Interaction .INTERACT_ENTITY ) || !Slimefun .getProtectionManager ().hasPermission (e .getPlayer (), e .getRightClicked ().getLocation (), Interaction .ATTACK_ENTITY )) {
60+ Player p = e .getPlayer ();
61+
62+ if (e .isCancelled () || !Slimefun .getProtectionManager ().hasPermission (p , e .getRightClicked ().getLocation (), Interaction .INTERACT_ENTITY ) || !Slimefun .getProtectionManager ().hasPermission (p , e .getRightClicked ().getLocation (), Interaction .ATTACK_ENTITY )) {
5863 // They don't have permission to use it in this area
5964 return ;
6065 }
@@ -68,14 +73,14 @@ public EntityInteractHandler getItemHandler() {
6873
6974 if (PersistentDataAPI .has (meta , Keys .MERCHANT_RECIPE , new MerchantRecipeListDataType ())) {
7075 e .setCancelled (true );
71- e . getPlayer () .sendMessage ("Contract already sealed" );
76+ p .sendMessage ("Contract already sealed" );
7277 return ;
7378 }
7479
7580 final List <MerchantRecipe > recipes = new ArrayList <>(merchant .getRecipes ());
7681 if (recipes .isEmpty ()) {
7782 e .setCancelled (true );
78- e . getPlayer () .sendMessage ("This merchant does not have any trade available for you" );
83+ p .sendMessage ("This merchant does not have any trade available for you" );
7984 return ;
8085 }
8186
@@ -123,9 +128,16 @@ public EntityInteractHandler getItemHandler() {
123128 );
124129 });
125130
131+ ItemStack contract = new ItemStack (item );
132+ contract .setAmount (1 );
126133 meta .setLore (lore );
127- item .setItemMeta (meta );
134+ contract .setItemMeta (meta );
128135
136+ ItemUtils .consumeItem (item , false );
137+ HashMap <Integer , ItemStack > result = p .getInventory ().addItem (contract );
138+ if (!result .isEmpty ()) {
139+ p .getWorld ().dropItemNaturally (p .getLocation (), contract );
140+ }
129141
130142 double offset = ThreadLocalRandom .current ().nextDouble (0.5 );
131143
@@ -135,7 +147,7 @@ public EntityInteractHandler getItemHandler() {
135147
136148 e .setCancelled (true );
137149 merchant .setHealth (0 );
138- ChatUtils .sendMessage (e . getPlayer () , "Soul Contract successfully sealed" );
150+ ChatUtils .sendMessage (p , "Soul Contract successfully sealed" );
139151 }
140152 };
141153 }
0 commit comments