Skip to content

Commit

Permalink
Fixed MerchantRecipe again
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Apr 25, 2021
1 parent 13c8187 commit 0772695
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.willfp.eco.core.display.Display;
import com.willfp.eco.proxy.proxies.VillagerTradeProxy;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMerchantRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
Expand All @@ -12,29 +11,37 @@

public final class VillagerTrade implements VillagerTradeProxy {
@Override
public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) {
try {
// Bukkit MerchantRecipe result
Field fResult = MerchantRecipe.class.getDeclaredField("result");
fResult.setAccessible(true);
ItemStack result = merchantRecipe.getResult().clone();
Display.display(result);
fResult.set(merchantRecipe, result);
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) {
CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe;

CraftMerchantRecipe newRecipe = new CraftMerchantRecipe(
Display.display(recipe.getResult().clone()),
recipe.getUses(),
recipe.getMaxUses(),
recipe.hasExperienceReward(),
recipe.getVillagerExperience(),
recipe.getPriceMultiplier()
);

// Get NMS MerchantRecipe from CraftMerchantRecipe
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
fHandle.setAccessible(true);
net.minecraft.server.v1_16_R1.MerchantRecipe handle = (net.minecraft.server.v1_16_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR
for (ItemStack ingredient : recipe.getIngredients()) {
newRecipe.addIngredient(Display.display(ingredient.clone()));
}

Field fSelling = net.minecraft.server.v1_16_R1.MerchantRecipe.class.getDeclaredField("sellingItem");
fSelling.setAccessible(true);
getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice());

ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone();
Display.display(selling);
return newRecipe;
}

fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
@NotNull
private net.minecraft.server.v1_16_R1.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) {
try {
Field handle = CraftMerchantRecipe.class.getDeclaredField("handle");
handle.setAccessible(true);
return (net.minecraft.server.v1_16_R1.MerchantRecipe) handle.get(recipe);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}

throw new IllegalArgumentException("Not CMR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.willfp.eco.core.display.Display;
import com.willfp.eco.proxy.proxies.VillagerTradeProxy;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftMerchantRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
Expand All @@ -12,29 +11,37 @@

public final class VillagerTrade implements VillagerTradeProxy {
@Override
public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) {
try {
// Bukkit MerchantRecipe result
Field fResult = MerchantRecipe.class.getDeclaredField("result");
fResult.setAccessible(true);
ItemStack result = merchantRecipe.getResult().clone();
Display.display(result);
fResult.set(merchantRecipe, result);
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) {
CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe;

CraftMerchantRecipe newRecipe = new CraftMerchantRecipe(
Display.display(recipe.getResult().clone()),
recipe.getUses(),
recipe.getMaxUses(),
recipe.hasExperienceReward(),
recipe.getVillagerExperience(),
recipe.getPriceMultiplier()
);

// Get NMS MerchantRecipe from CraftMerchantRecipe
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
fHandle.setAccessible(true);
net.minecraft.server.v1_16_R2.MerchantRecipe handle = (net.minecraft.server.v1_16_R2.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR
for (ItemStack ingredient : recipe.getIngredients()) {
newRecipe.addIngredient(Display.display(ingredient.clone()));
}

Field fSelling = net.minecraft.server.v1_16_R2.MerchantRecipe.class.getDeclaredField("sellingItem");
fSelling.setAccessible(true);
getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice());

ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone();
Display.display(selling);
return newRecipe;
}

fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
@NotNull
private net.minecraft.server.v1_16_R2.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) {
try {
Field handle = CraftMerchantRecipe.class.getDeclaredField("handle");
handle.setAccessible(true);
return (net.minecraft.server.v1_16_R2.MerchantRecipe) handle.get(recipe);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}

throw new IllegalArgumentException("Not CMR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.willfp.eco.core.display.Display;
import com.willfp.eco.proxy.proxies.VillagerTradeProxy;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMerchantRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
Expand All @@ -12,29 +11,37 @@

public final class VillagerTrade implements VillagerTradeProxy {
@Override
public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) {
try {
// Bukkit MerchantRecipe result
Field fResult = MerchantRecipe.class.getDeclaredField("result");
fResult.setAccessible(true);
ItemStack result = merchantRecipe.getResult().clone();
Display.display(result);
fResult.set(merchantRecipe, result);
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) {
CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe;

CraftMerchantRecipe newRecipe = new CraftMerchantRecipe(
Display.display(recipe.getResult().clone()),
recipe.getUses(),
recipe.getMaxUses(),
recipe.hasExperienceReward(),
recipe.getVillagerExperience(),
recipe.getPriceMultiplier()
);

// Get NMS MerchantRecipe from CraftMerchantRecipe
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
fHandle.setAccessible(true);
net.minecraft.server.v1_16_R3.MerchantRecipe handle = (net.minecraft.server.v1_16_R3.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR
for (ItemStack ingredient : recipe.getIngredients()) {
newRecipe.addIngredient(Display.display(ingredient.clone()));
}

Field fSelling = net.minecraft.server.v1_16_R3.MerchantRecipe.class.getDeclaredField("sellingItem");
fSelling.setAccessible(true);
getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice());

ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone();
Display.display(selling);
return newRecipe;
}

fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
@NotNull
private net.minecraft.server.v1_16_R3.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) {
try {
Field handle = CraftMerchantRecipe.class.getDeclaredField("handle");
handle.setAccessible(true);
return (net.minecraft.server.v1_16_R3.MerchantRecipe) handle.get(recipe);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}

throw new IllegalArgumentException("Not CMR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

public interface VillagerTradeProxy extends AbstractProxy {
/**
* Display a MerchantRecipe without creating a new one.
* Display a MerchantRecipe.
*
* @param recipe The recipe.
* @return The new recipe.
*/
void displayTrade(@NotNull MerchantRecipe recipe);
MerchantRecipe displayTrade(@NotNull MerchantRecipe recipe);
}

0 comments on commit 0772695

Please sign in to comment.