Fix villager trades not displaying a lore

In order to fix this issue, I had to do the following:
- Bump ProtocolLib version to 4.6.0 (getting the latest version is enough)
- Intercepted the OPEN_WINDOW_MERCHANT and updated the trades' result to include the enchantments. Do note that the items still will only have a lore at the client-side.
This commit is contained in:
NickAc 2020-08-29 10:02:52 +01:00
parent 33c0caabb9
commit 9a3acbe725
2 changed files with 23 additions and 2 deletions

View File

@ -245,7 +245,7 @@
<dependency> <dependency>
<groupId>com.comphenix.protocol</groupId> <groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId> <artifactId>ProtocolLib</artifactId>
<version>4.5.0</version> <version>4.6.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -4,15 +4,18 @@ import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.willfp.ecoenchants.Main; import com.willfp.ecoenchants.Main;
import org.bukkit.inventory.MerchantRecipe;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class DisplayPacketAdapter extends PacketAdapter { public class DisplayPacketAdapter extends PacketAdapter {
private static final List<PacketType> packets = Arrays.asList( private static final List<PacketType> packets = Arrays.asList(
PacketType.Play.Server.WINDOW_ITEMS, PacketType.Play.Server.WINDOW_ITEMS,
PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.SET_SLOT,
PacketType.Play.Client.SET_CREATIVE_SLOT PacketType.Play.Client.SET_CREATIVE_SLOT,
PacketType.Play.Server.OPEN_WINDOW_MERCHANT
); );
public DisplayPacketAdapter() { public DisplayPacketAdapter() {
@ -34,6 +37,24 @@ public class DisplayPacketAdapter extends PacketAdapter {
item = EnchantDisplay.displayEnchantments(item); item = EnchantDisplay.displayEnchantments(item);
return item; return item;
}); });
} else if (PacketType.Play.Server.OPEN_WINDOW_MERCHANT.equals(packetType)) {
List<MerchantRecipe> merchantRecipes = event.getPacket().getMerchantRecipeLists().readSafely(0);
if (merchantRecipes != null) {
List<MerchantRecipe> newList =
merchantRecipes.stream().map(oldRecipe -> {
MerchantRecipe recipe =
new MerchantRecipe(EnchantDisplay.displayEnchantments(oldRecipe.getResult()),
oldRecipe.getUses(),
oldRecipe.getMaxUses(),
oldRecipe.hasExperienceReward(),
oldRecipe.getVillagerExperience(),
oldRecipe.getPriceMultiplier());
recipe.setIngredients(oldRecipe.getIngredients());
return recipe;
}).collect(Collectors.toList());
event.getPacket().getMerchantRecipeLists().writeSafely(0, newList);
}
} }
} }