mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
fix renaming
This commit is contained in:
@@ -13,8 +13,11 @@ public class LegacyInventoryUtils {
|
||||
return player.getOpenInventory().getTopInventory();
|
||||
}
|
||||
|
||||
public static void setRepairCost(AnvilInventory anvilInventory, int repairCost, int amount) {
|
||||
public static void setRepairCost(AnvilInventory anvilInventory, int repairCost) {
|
||||
anvilInventory.setRepairCost(repairCost);
|
||||
}
|
||||
|
||||
public static void setRepairCostAmount(AnvilInventory anvilInventory, int amount) {
|
||||
anvilInventory.setRepairCostAmount(amount);
|
||||
}
|
||||
|
||||
@@ -27,6 +30,10 @@ public class LegacyInventoryUtils {
|
||||
return anvilInventory.getMaximumRepairCost();
|
||||
}
|
||||
|
||||
public static int getRepairCost(AnvilInventory anvilInventory) {
|
||||
return anvilInventory.getRepairCost();
|
||||
}
|
||||
|
||||
public static InventoryView getView(PrepareAnvilEvent event) {
|
||||
return event.getView();
|
||||
}
|
||||
|
||||
@@ -21,10 +21,7 @@ import net.momirealms.craftengine.core.item.recipe.input.SingleItemInput;
|
||||
import net.momirealms.craftengine.core.plugin.config.ConfigManager;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.Pair;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import net.momirealms.craftengine.core.util.*;
|
||||
import net.momirealms.craftengine.core.util.context.ContextHolder;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
@@ -561,13 +558,16 @@ public class RecipeEventListener implements Listener {
|
||||
|
||||
String renameText;
|
||||
int maxRepairCost;
|
||||
int previousCost;
|
||||
if (VersionHelper.isVersionNewerThan1_21_2()) {
|
||||
AnvilView anvilView = event.getView();
|
||||
renameText = anvilView.getRenameText();
|
||||
maxRepairCost = anvilView.getMaximumRepairCost();
|
||||
previousCost = anvilView.getRepairCost();
|
||||
} else {
|
||||
renameText = LegacyInventoryUtils.getRenameText(inventory);
|
||||
maxRepairCost = LegacyInventoryUtils.getMaxRepairCost(inventory);
|
||||
previousCost = LegacyInventoryUtils.getRepairCost(inventory);
|
||||
}
|
||||
|
||||
int repairCost = actualConsumedAmount;
|
||||
@@ -580,7 +580,6 @@ public class RecipeEventListener implements Listener {
|
||||
repairCost += 1;
|
||||
} else if (repairCost == 0) {
|
||||
hasResult = false;
|
||||
System.out.println("1");
|
||||
}
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get hover name", e);
|
||||
@@ -595,12 +594,32 @@ public class RecipeEventListener implements Listener {
|
||||
|
||||
int finalCost = repairCost + repairPenalty;
|
||||
|
||||
if (VersionHelper.isVersionNewerThan1_21()) {
|
||||
AnvilView anvilView = event.getView();
|
||||
anvilView.setRepairCost(finalCost <= 1 ? 2 : finalCost - 1);
|
||||
} else {
|
||||
LegacyInventoryUtils.setRepairCost(inventory, finalCost <= 1 ? 2 : finalCost - 1);
|
||||
}
|
||||
|
||||
try {
|
||||
if (VersionHelper.isVersionNewerThan1_21()) {
|
||||
Object anvilMenu = Reflections.field$CraftInventoryView$container.get(event.getView());
|
||||
Reflections.method$AbstractContainerMenu$broadcastChanges.invoke(anvilMenu);
|
||||
} else {
|
||||
Object anvilMenu = Reflections.field$CraftInventoryAnvil$menu.get(inventory);
|
||||
Reflections.method$AbstractContainerMenu$broadcastChanges.invoke(anvilMenu);
|
||||
}
|
||||
} catch (ReflectiveOperationException e) {
|
||||
this.plugin.logger().warn("Failed to broadcast changes", e);
|
||||
}
|
||||
|
||||
if (VersionHelper.isVersionNewerThan1_21()) {
|
||||
AnvilView anvilView = event.getView();
|
||||
anvilView.setRepairCost(finalCost);
|
||||
anvilView.setRepairItemCountCost(actualConsumedAmount);
|
||||
} else {
|
||||
LegacyInventoryUtils.setRepairCost(inventory, finalCost, actualRepairAmount);
|
||||
LegacyInventoryUtils.setRepairCost(inventory, finalCost);
|
||||
LegacyInventoryUtils.setRepairCostAmount(inventory, actualConsumedAmount);
|
||||
}
|
||||
|
||||
Player player;
|
||||
|
||||
@@ -4728,6 +4728,12 @@ public class Reflections {
|
||||
)
|
||||
);
|
||||
|
||||
public static final Method method$AbstractContainerMenu$broadcastChanges = requireNonNull(
|
||||
ReflectionUtils.getMethod(
|
||||
clazz$AbstractContainerMenu, void.class, new String[]{ "broadcastChanges", "d" }
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$CraftContainer = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleCBClass("inventory.CraftContainer")
|
||||
@@ -4892,4 +4898,33 @@ public class Reflections {
|
||||
clazz$CraftComplexRecipe, clazz$CustomRecipe, 0
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$CraftInventoryAnvil = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleCBClass("inventory.CraftInventoryAnvil")
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$AnvilMenu = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("world.inventory.AnvilMenu"),
|
||||
BukkitReflectionUtils.assembleMCClass("world.inventory.ContainerAnvil")
|
||||
)
|
||||
);
|
||||
|
||||
// 1.21+
|
||||
public static final Class<?> clazz$CraftInventoryView =
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleCBClass("inventory.CraftInventoryView")
|
||||
);
|
||||
|
||||
// 1.21+
|
||||
public static final Field field$CraftInventoryView$container = Optional.ofNullable(clazz$CraftInventoryView)
|
||||
.map(it -> ReflectionUtils.getDeclaredField(it, 0)).orElse(null);
|
||||
|
||||
// 1.20-1.20.6
|
||||
public static final Field field$CraftInventoryAnvil$menu =
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$CraftInventoryAnvil, clazz$AnvilMenu, 0
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,7 @@ import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
import net.minecraft.world.level.ScheduledTickAccess;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.BonemealableBlock;
|
||||
import net.minecraft.world.level.block.Fallable;
|
||||
import net.minecraft.world.level.block.*;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.material.FluidState;
|
||||
import net.minecraft.world.phys.shapes.CollisionContext;
|
||||
|
||||
Reference in New Issue
Block a user