Skip to content

Commit 8ad7c8a

Browse files
committed
fix(items): convert only one contract at a time
1 parent 3a9e198 commit 8ad7c8a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/main/java/me/voper/slimeframe/implementation/items/tools/MerchantSoulContract.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
88
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
99
import io.github.thebusybiscuit.slimefun4.libraries.dough.data.persistent.PersistentDataAPI;
10+
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils;
1011
import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction;
1112
import me.voper.slimeframe.SlimeFrame;
1213
import me.voper.slimeframe.core.datatypes.MerchantRecipeListDataType;
@@ -21,13 +22,15 @@
2122
import org.bukkit.Sound;
2223
import org.bukkit.enchantments.Enchantment;
2324
import org.bukkit.entity.AbstractVillager;
25+
import org.bukkit.entity.Player;
2426
import org.bukkit.inventory.ItemFlag;
2527
import org.bukkit.inventory.ItemStack;
2628
import org.bukkit.inventory.MerchantRecipe;
2729
import org.bukkit.inventory.meta.ItemMeta;
2830

2931
import javax.annotation.Nonnull;
3032
import java.util.ArrayList;
33+
import java.util.HashMap;
3134
import java.util.List;
3235
import java.util.concurrent.ThreadLocalRandom;
3336
import 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

Comments
 (0)