mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-03 22:26:16 +00:00
anvil repair system
This commit is contained in:
@@ -3,7 +3,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 3001
|
||||
data:
|
||||
display-name: "<!i><i18n:item.chinese_lantern>"
|
||||
item-name: "<!i><i18n:item.chinese_lantern>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/chinese_lantern"
|
||||
|
||||
@@ -3,7 +3,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 3000
|
||||
data:
|
||||
display-name: "<!i><i18n:item.fairy_flower>"
|
||||
item-name: "<!i><i18n:item.fairy_flower>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/fairy_flower"
|
||||
|
||||
@@ -3,7 +3,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 2000
|
||||
data:
|
||||
display-name: "<!i><i18n:item.bench>"
|
||||
item-name: "<!i><i18n:item.bench>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/bench"
|
||||
@@ -49,7 +49,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 2001
|
||||
data:
|
||||
display-name: "<!i><i18n:item.table_lamp>"
|
||||
item-name: "<!i><i18n:item.table_lamp>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/table_lamp"
|
||||
@@ -93,7 +93,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 2002
|
||||
data:
|
||||
display-name: "<!i><i18n:item.wooden_chair>"
|
||||
item-name: "<!i><i18n:item.wooden_chair>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/wooden_chair"
|
||||
|
||||
@@ -6,7 +6,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_rod>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_rod>"
|
||||
tooltip-style: minecraft:topaz
|
||||
model:
|
||||
template: models:fishing_rod_2d
|
||||
@@ -22,7 +22,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_bow>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_bow>"
|
||||
tooltip-style: minecraft:topaz
|
||||
model:
|
||||
template: models:bow_2d
|
||||
@@ -42,7 +42,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_crossbow>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_crossbow>"
|
||||
tooltip-style: minecraft:topaz
|
||||
model:
|
||||
template: models:crossbow_2d
|
||||
@@ -66,7 +66,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_pickaxe>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_pickaxe>"
|
||||
tooltip-style: minecraft:topaz
|
||||
components:
|
||||
minecraft:max_damage: 64
|
||||
@@ -84,7 +84,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_axe>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_axe>"
|
||||
tooltip-style: minecraft:topaz
|
||||
components:
|
||||
minecraft:max_damage: 64
|
||||
@@ -102,7 +102,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_hoe>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_hoe>"
|
||||
tooltip-style: minecraft:topaz
|
||||
components:
|
||||
minecraft:max_damage: 64
|
||||
@@ -120,7 +120,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_shovel>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_shovel>"
|
||||
tooltip-style: minecraft:topaz
|
||||
components:
|
||||
minecraft:max_damage: 64
|
||||
@@ -138,7 +138,7 @@ items:
|
||||
tags:
|
||||
- "topaz_tools"
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_sword>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_sword>"
|
||||
tooltip-style: minecraft:topaz
|
||||
components:
|
||||
minecraft:max_damage: 64
|
||||
@@ -175,7 +175,7 @@ templates:
|
||||
material: "chainmail_{part}"
|
||||
custom-model-data: 1000
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz_{part}>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz_{part}>"
|
||||
tooltip-style: minecraft:topaz
|
||||
settings:
|
||||
tags:
|
||||
|
||||
@@ -3,7 +3,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 1010
|
||||
data:
|
||||
display-name: "<!i><i18n:item.topaz_ore>"
|
||||
item-name: "<!i><i18n:item.topaz_ore>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/topaz_ore"
|
||||
@@ -16,7 +16,7 @@ items:
|
||||
material: paper
|
||||
custom-model-data: 1011
|
||||
data:
|
||||
display-name: "<!i><i18n:item.deepslate_topaz_ore>"
|
||||
item-name: "<!i><i18n:item.deepslate_topaz_ore>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/deepslate_topaz_ore"
|
||||
@@ -34,7 +34,7 @@ items:
|
||||
- "#topaz_tools"
|
||||
percent: 0.25
|
||||
data:
|
||||
display-name: "<!i><#FF8C00><i18n:item.topaz>"
|
||||
item-name: "<!i><#FF8C00><i18n:item.topaz>"
|
||||
model:
|
||||
template: models:generated
|
||||
arguments:
|
||||
|
||||
@@ -9,7 +9,7 @@ items:
|
||||
- "minecraft:logs"
|
||||
- "minecraft:logs_that_burn"
|
||||
data:
|
||||
display-name: "<!i><i18n:item.palm_log>"
|
||||
item-name: "<!i><i18n:item.palm_log>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/palm_log"
|
||||
@@ -55,7 +55,7 @@ items:
|
||||
- "minecraft:logs"
|
||||
- "minecraft:logs_that_burn"
|
||||
data:
|
||||
display-name: "<!i><i18n:item.stripped_palm_log>"
|
||||
item-name: "<!i><i18n:item.stripped_palm_log>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/stripped_palm_log"
|
||||
@@ -98,7 +98,7 @@ items:
|
||||
- "minecraft:logs"
|
||||
- "minecraft:logs_that_burn"
|
||||
data:
|
||||
display-name: "<!i><i18n:item.palm_wood>"
|
||||
item-name: "<!i><i18n:item.palm_wood>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/palm_wood"
|
||||
@@ -144,7 +144,7 @@ items:
|
||||
- "minecraft:logs"
|
||||
- "minecraft:logs_that_burn"
|
||||
data:
|
||||
display-name: "<!i><i18n:item.stripped_palm_wood>"
|
||||
item-name: "<!i><i18n:item.stripped_palm_wood>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/stripped_palm_wood"
|
||||
@@ -186,7 +186,7 @@ items:
|
||||
- "minecraft:planks"
|
||||
- "minecraft:wooden_tool_materials"
|
||||
data:
|
||||
display-name: "<!i><i18n:item.palm_planks>"
|
||||
item-name: "<!i><i18n:item.palm_planks>"
|
||||
model:
|
||||
type: "minecraft:model"
|
||||
path: "minecraft:item/custom/palm_planks"
|
||||
@@ -218,7 +218,7 @@ items:
|
||||
settings:
|
||||
fuel-time: 100
|
||||
data:
|
||||
display-name: "<!i><i18n:item.palm_sapling>"
|
||||
item-name: "<!i><i18n:item.palm_sapling>"
|
||||
model:
|
||||
template: models:generated
|
||||
arguments:
|
||||
@@ -271,7 +271,7 @@ items:
|
||||
material: oak_leaves
|
||||
custom-model-data: 1000
|
||||
data:
|
||||
display-name: "<!i><i18n:item.palm_leaves>"
|
||||
item-name: "<!i><i18n:item.palm_leaves>"
|
||||
components:
|
||||
minecraft:block_state:
|
||||
distance: "1"
|
||||
|
||||
@@ -65,7 +65,7 @@ templates:
|
||||
material: arrow
|
||||
custom-model-data: "{model_data}"
|
||||
data:
|
||||
display-name: "{name}"
|
||||
item-name: "{name}"
|
||||
lore: "{lore}"
|
||||
model:
|
||||
template: models:generated
|
||||
@@ -117,28 +117,28 @@ items:
|
||||
material: arrow
|
||||
custom-model-data: 1000
|
||||
data:
|
||||
display-name: "<!i><#FAFAD2><i18n:internal.next_recipe>"
|
||||
item-name: "<!i><#FAFAD2><i18n:internal.next_recipe>"
|
||||
lore:
|
||||
- "<!i><#F5F5F5><arg:current_page>/<arg:max_page>"
|
||||
internal:next_recipe_1:
|
||||
material: arrow
|
||||
custom-model-data: 1001
|
||||
data:
|
||||
display-name: "<!i><#808080><i18n:internal.next_recipe>"
|
||||
item-name: "<!i><#808080><i18n:internal.next_recipe>"
|
||||
lore:
|
||||
- "<!i><#696969><arg:current_page>/<arg:max_page>"
|
||||
internal:previous_recipe_0:
|
||||
material: arrow
|
||||
custom-model-data: 1002
|
||||
data:
|
||||
display-name: "<!i><#FAFAD2><i18n:internal.previous_recipe>"
|
||||
item-name: "<!i><#FAFAD2><i18n:internal.previous_recipe>"
|
||||
lore:
|
||||
- "<!i><#F5F5F5><arg:current_page>/<arg:max_page>"
|
||||
internal:previous_recipe_1:
|
||||
material: arrow
|
||||
custom-model-data: 1003
|
||||
data:
|
||||
display-name: "<!i><#808080><i18n:internal.previous_recipe>"
|
||||
item-name: "<!i><#808080><i18n:internal.previous_recipe>"
|
||||
lore:
|
||||
- "<!i><#696969><arg:current_page>/<arg:max_page>"
|
||||
internal:get_item:
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package net.momirealms.craftengine.bukkit.util;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
||||
import org.bukkit.inventory.AnvilInventory;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class LegacyInventoryUtils {
|
||||
|
||||
@@ -14,4 +17,17 @@ public class LegacyInventoryUtils {
|
||||
anvilInventory.setRepairCost(repairCost);
|
||||
anvilInventory.setRepairCostAmount(amount);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getRenameText(AnvilInventory anvilInventory) {
|
||||
return anvilInventory.getRenameText();
|
||||
}
|
||||
|
||||
public static int getMaxRepairCost(AnvilInventory anvilInventory) {
|
||||
return anvilInventory.getMaximumRepairCost();
|
||||
}
|
||||
|
||||
public static InventoryView getView(PrepareAnvilEvent event) {
|
||||
return event.getView();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class ComponentItemFactory extends BukkitItemFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void displayName(ItemWrapper<ItemStack> item, String json) {
|
||||
protected void customName(ItemWrapper<ItemStack> item, String json) {
|
||||
if (json == null) {
|
||||
item.removeComponent(ComponentKeys.CUSTOM_NAME);
|
||||
} else {
|
||||
@@ -71,7 +71,7 @@ public class ComponentItemFactory extends BukkitItemFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<String> displayName(ItemWrapper<ItemStack> item) {
|
||||
protected Optional<String> customName(ItemWrapper<ItemStack> item) {
|
||||
if (!item.hasComponent(ComponentKeys.CUSTOM_NAME)) return Optional.empty();
|
||||
return Optional.ofNullable(
|
||||
(String) ComponentType.encodeJava(
|
||||
@@ -272,4 +272,19 @@ public class ComponentItemFactory extends BukkitItemFactory {
|
||||
item.setComponent(ComponentKeys.MAX_STACK_SIZE, maxStackSize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void repairCost(ItemWrapper<ItemStack> item, Integer data) {
|
||||
if (data == null) {
|
||||
item.removeComponent(ComponentKeys.REPAIR_COST);
|
||||
} else {
|
||||
item.setComponent(ComponentKeys.REPAIR_COST, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<Integer> repairCost(ItemWrapper<ItemStack> item) {
|
||||
if (!item.hasComponent(ComponentKeys.REPAIR_COST)) return Optional.empty();
|
||||
return Optional.ofNullable((Integer) ComponentType.encodeJava(ComponentKeys.REPAIR_COST, item.getComponent(ComponentKeys.REPAIR_COST)).orElse(null));
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class UniversalItemFactory extends BukkitItemFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void displayName(ItemWrapper<ItemStack> item, String json) {
|
||||
protected void customName(ItemWrapper<ItemStack> item, String json) {
|
||||
if (json != null) {
|
||||
item.set(json, "display", "Name");
|
||||
} else {
|
||||
@@ -32,19 +32,19 @@ public class UniversalItemFactory extends BukkitItemFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<String> displayName(ItemWrapper<ItemStack> item) {
|
||||
protected Optional<String> customName(ItemWrapper<ItemStack> item) {
|
||||
if (!item.hasTag("display", "Name")) return Optional.empty();
|
||||
return Optional.of(item.get("display", "Name"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void itemName(ItemWrapper<ItemStack> item, String json) {
|
||||
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
|
||||
customName(item, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<String> itemName(ItemWrapper<ItemStack> item) {
|
||||
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
|
||||
return customName(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -218,4 +218,15 @@ public class UniversalItemFactory extends BukkitItemFactory {
|
||||
@Override
|
||||
protected void maxStackSize(ItemWrapper<ItemStack> item, Integer maxStackSize) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void repairCost(ItemWrapper<ItemStack> item, Integer data) {
|
||||
item.set(data, "RepairCost");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<Integer> repairCost(ItemWrapper<ItemStack> item) {
|
||||
if (!item.hasTag("RepairCost")) return Optional.empty();
|
||||
return Optional.of(item.get("RepairCost"));
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package net.momirealms.craftengine.bukkit.item.recipe;
|
||||
|
||||
import com.destroystokyo.paper.event.inventory.PrepareResultEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.plugin.injector.BukkitInjector;
|
||||
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
|
||||
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.ItemUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.LegacyInventoryUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||
@@ -15,10 +18,10 @@ import net.momirealms.craftengine.core.item.recipe.Recipe;
|
||||
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
|
||||
import net.momirealms.craftengine.core.item.recipe.input.CraftingInput;
|
||||
import net.momirealms.craftengine.core.item.recipe.input.SingleItemInput;
|
||||
import net.momirealms.craftengine.core.pack.LoadingSequence;
|
||||
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;
|
||||
@@ -459,21 +462,19 @@ public class RecipeEventListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
// one of them is vanilla item
|
||||
if (!firstCustom || !secondCustom) {
|
||||
// block "vanilla + custom" recipes
|
||||
if (wrappedFirst.vanillaId().equals(wrappedSecond.vanillaId())) {
|
||||
event.setResult(null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// both of them are custom items
|
||||
// if the second is an enchanted book, then apply it
|
||||
if (wrappedSecond.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// one of them is vanilla item
|
||||
if (!firstCustom || !secondCustom) {
|
||||
// block "vanilla + custom" recipes
|
||||
event.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
// not the same item
|
||||
if (!wrappedFirst.customId().equals(wrappedSecond.customId())) {
|
||||
event.setResult(null);
|
||||
@@ -511,10 +512,12 @@ public class RecipeEventListener implements Listener {
|
||||
}
|
||||
|
||||
Item<ItemStack> wrappedFirst = BukkitItemManager.instance().wrap(first);
|
||||
int damage = wrappedFirst.damage().orElse(0);
|
||||
|
||||
int maxDamage = wrappedFirst.maxDamage().orElse(0);
|
||||
// not a repairable item
|
||||
if (maxDamage == 0 || damage == 0) return;
|
||||
if (maxDamage == 0) return;
|
||||
|
||||
int damage = wrappedFirst.damage().orElse(0);
|
||||
|
||||
Key firstId = wrappedFirst.id();
|
||||
Optional<CustomItem<ItemStack>> optionalCustomTool = wrappedFirst.getCustomItem();
|
||||
@@ -548,20 +551,85 @@ public class RecipeEventListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean hasResult = true;
|
||||
|
||||
int realDurabilityPerItem = (int) (repairItem.amount() + repairItem.percent() * maxDamage);
|
||||
int consumeMaxAmount = damage / realDurabilityPerItem + 1;
|
||||
int consumeMaxAmount = damage == 0 ? 0 : damage / realDurabilityPerItem + 1;
|
||||
int actualConsumedAmount = Math.min(consumeMaxAmount, wrappedSecond.count());
|
||||
int actualRepairAmount = actualConsumedAmount * realDurabilityPerItem;
|
||||
int damageAfter = Math.max(damage - actualRepairAmount, 0);
|
||||
wrappedFirst.damage(damageAfter);
|
||||
event.setResult(wrappedFirst.loadCopy());
|
||||
|
||||
String renameText;
|
||||
int maxRepairCost;
|
||||
if (VersionHelper.isVersionNewerThan1_21_2()) {
|
||||
AnvilView anvilView = event.getView();
|
||||
renameText = anvilView.getRenameText();
|
||||
maxRepairCost = anvilView.getMaximumRepairCost();
|
||||
} else {
|
||||
renameText = LegacyInventoryUtils.getRenameText(inventory);
|
||||
maxRepairCost = LegacyInventoryUtils.getMaxRepairCost(inventory);
|
||||
}
|
||||
|
||||
int repairCost = actualConsumedAmount;
|
||||
int repairPenalty = wrappedFirst.repairCost().orElse(0) + wrappedSecond.repairCost().orElse(0);
|
||||
|
||||
if (renameText != null && !renameText.isBlank()) {
|
||||
try {
|
||||
if (!renameText.equals(Reflections.method$Component$getString.invoke(ComponentUtils.jsonToMinecraft(wrappedFirst.hoverName().orElse(AdventureHelper.EMPTY_COMPONENT))))) {
|
||||
wrappedFirst.customName(AdventureHelper.componentToJson(Component.text(renameText)));
|
||||
repairCost += 1;
|
||||
} else if (repairCost == 0) {
|
||||
hasResult = false;
|
||||
System.out.println("1");
|
||||
}
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get hover name", e);
|
||||
}
|
||||
} else if (VersionHelper.isVersionNewerThan1_20_5() && wrappedFirst.hasComponent(ComponentKeys.CUSTOM_NAME)) {
|
||||
repairCost += 1;
|
||||
wrappedFirst.customName(null);
|
||||
} else if (!VersionHelper.isVersionNewerThan1_20_5() && wrappedFirst.hasTag("display", "Name")) {
|
||||
repairCost += 1;
|
||||
wrappedFirst.customName(null);
|
||||
}
|
||||
|
||||
int finalCost = repairCost + repairPenalty;
|
||||
|
||||
if (VersionHelper.isVersionNewerThan1_21()) {
|
||||
AnvilView anvilView = event.getView();
|
||||
anvilView.setRepairCost(10);
|
||||
anvilView.setRepairCost(finalCost);
|
||||
anvilView.setRepairItemCountCost(actualConsumedAmount);
|
||||
} else {
|
||||
LegacyInventoryUtils.setRepairCost(inventory, 10, actualRepairAmount);
|
||||
LegacyInventoryUtils.setRepairCost(inventory, finalCost, actualRepairAmount);
|
||||
}
|
||||
|
||||
Player player;
|
||||
try {
|
||||
player = (Player) Reflections.method$InventoryView$getPlayer.invoke(VersionHelper.isVersionNewerThan1_21() ? event.getView() : LegacyInventoryUtils.getView(event));
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get inventory viewer", e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (finalCost >= maxRepairCost && !plugin.adapt(player).canInstabuild()) {
|
||||
hasResult = false;
|
||||
}
|
||||
|
||||
if (hasResult) {
|
||||
int afterPenalty = wrappedFirst.repairCost().orElse(0);
|
||||
int anotherPenalty = wrappedSecond.repairCost().orElse(0);
|
||||
if (afterPenalty < anotherPenalty) {
|
||||
afterPenalty = anotherPenalty;
|
||||
}
|
||||
afterPenalty = calculateIncreasedRepairCost(afterPenalty);
|
||||
wrappedFirst.repairCost(afterPenalty);
|
||||
event.setResult(wrappedFirst.loadCopy());
|
||||
}
|
||||
}
|
||||
|
||||
public static int calculateIncreasedRepairCost(int cost) {
|
||||
return (int) Math.min((long) cost * 2L + 1L, 2147483647L);
|
||||
}
|
||||
|
||||
// only handle repair items for the moment
|
||||
|
||||
@@ -10,6 +10,10 @@ public class ComponentUtils {
|
||||
|
||||
public static Object adventureToMinecraft(Component component) {
|
||||
String json = AdventureHelper.componentToJson(component);
|
||||
return jsonToMinecraft(json);
|
||||
}
|
||||
|
||||
public static Object jsonToMinecraft(String json) {
|
||||
if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
try {
|
||||
return Reflections.method$Component$Serializer$fromJson.invoke(null, json, Reflections.instance$MinecraftRegistry);
|
||||
|
||||
@@ -72,6 +72,12 @@ public class Reflections {
|
||||
)
|
||||
);
|
||||
|
||||
public static final Method method$Component$getString = requireNonNull(
|
||||
ReflectionUtils.getMethod(
|
||||
clazz$Component, String.class, new String[]{"getString", "a"}
|
||||
)
|
||||
);
|
||||
|
||||
public static final Class<?> clazz$RandomSource = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("util.RandomSource")
|
||||
|
||||
@@ -26,6 +26,17 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
|
||||
return this.factory.damage(this.item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> repairCost(Integer data) {
|
||||
this.factory.repairCost(this.item, data);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Integer> repairCost() {
|
||||
return this.factory.repairCost(this.item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> maxDamage(Integer data) {
|
||||
this.factory.maxDamage(this.item, data);
|
||||
@@ -96,8 +107,8 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> displayName() {
|
||||
return this.factory.displayName(this.item);
|
||||
public Optional<String> customName() {
|
||||
return this.factory.customName(this.item);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,8 +139,8 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> displayName(String displayName) {
|
||||
this.factory.displayName(this.item, displayName);
|
||||
public Item<I> customName(String displayName) {
|
||||
this.factory.customName(this.item, displayName);
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -168,7 +168,11 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
registerDataFunction((obj) -> {
|
||||
String name = obj.toString();
|
||||
return new DisplayNameModifier<>(name);
|
||||
}, "name", "display-name", "custom-name");
|
||||
}, "custom-name");
|
||||
registerDataFunction((obj) -> {
|
||||
String name = obj.toString();
|
||||
return new ItemNameModifier<>(name);
|
||||
}, "item-name", "display-name");
|
||||
registerDataFunction((obj) -> {
|
||||
List<String> name = MiscUtils.getAsStringList(obj);
|
||||
return new LoreModifier<>(name);
|
||||
@@ -197,12 +201,6 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
String pattern = data.get("pattern").toString().toLowerCase(Locale.ENGLISH);
|
||||
return new TrimModifier<>(material, pattern);
|
||||
}, "trim");
|
||||
if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
registerDataFunction((obj) -> {
|
||||
String name = obj.toString();
|
||||
return new ItemNameModifier<>(name);
|
||||
}, "item-name");
|
||||
}
|
||||
if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
registerDataFunction((obj) -> {
|
||||
Map<String, Object> data = MiscUtils.castToMap(obj, false);
|
||||
|
||||
@@ -19,4 +19,5 @@ public class ComponentKeys {
|
||||
public static final String TOOLTIP_STYLE = Key.key("minecraft", "tooltip_style").asString();
|
||||
public static final String JUKEBOX_PLAYABLE = Key.key("minecraft", "jukebox_playable").asString();
|
||||
public static final String TRIM = Key.key("minecraft", "trim").asString();
|
||||
public static final String REPAIR_COST = Key.key("minecraft", "repair_cost").asString();
|
||||
}
|
||||
|
||||
@@ -41,13 +41,21 @@ public interface Item<I> {
|
||||
|
||||
Optional<Integer> damage();
|
||||
|
||||
Item<I> repairCost(Integer data);
|
||||
|
||||
Optional<Integer> repairCost();
|
||||
|
||||
Item<I> maxDamage(Integer data);
|
||||
|
||||
Optional<Integer> maxDamage();
|
||||
|
||||
Item<I> displayName(String displayName);
|
||||
Item<I> customName(String displayName);
|
||||
|
||||
Optional<String> displayName();
|
||||
Optional<String> customName();
|
||||
|
||||
default Optional<String> hoverName() {
|
||||
return customName().or(this::itemName);
|
||||
}
|
||||
|
||||
Item<I> itemName(String itemName);
|
||||
|
||||
|
||||
@@ -49,9 +49,9 @@ public abstract class ItemFactory<P extends Plugin, W extends ItemWrapper<I>, I>
|
||||
|
||||
protected abstract Optional<Integer> customModelData(ItemWrapper<I> item);
|
||||
|
||||
protected abstract void displayName(ItemWrapper<I> item, String json);
|
||||
protected abstract void customName(ItemWrapper<I> item, String json);
|
||||
|
||||
protected abstract Optional<String> displayName(ItemWrapper<I> item);
|
||||
protected abstract Optional<String> customName(ItemWrapper<I> item);
|
||||
|
||||
protected abstract void itemName(ItemWrapper<I> item, String json);
|
||||
|
||||
@@ -104,4 +104,8 @@ public abstract class ItemFactory<P extends Plugin, W extends ItemWrapper<I>, I>
|
||||
protected abstract boolean is(ItemWrapper<I> item, Key itemTag);
|
||||
|
||||
protected abstract boolean isBlockItem(ItemWrapper<I> item);
|
||||
|
||||
protected abstract void repairCost(ItemWrapper<I> item, Integer data);
|
||||
|
||||
protected abstract Optional<Integer> repairCost(ItemWrapper<I> item);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ public class DisplayNameModifier<I> implements ItemModifier<I> {
|
||||
|
||||
@Override
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.displayName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(this.argument, context.tagResolvers())));
|
||||
item.customName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(this.argument, context.tagResolvers())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager {
|
||||
this.plugin.logger().warn("Can't not find item " + it.icon() + " for category icon");
|
||||
return null;
|
||||
}
|
||||
item.displayName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(it.displayName(), ItemBuildContext.EMPTY.tagResolvers())));
|
||||
item.customName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(it.displayName(), ItemBuildContext.EMPTY.tagResolvers())));
|
||||
item.lore(it.displayLore().stream().map(lore -> AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(lore, ItemBuildContext.EMPTY.tagResolvers()))).toList());
|
||||
item.load();
|
||||
return new ItemWithAction(item, (element, click) -> {
|
||||
@@ -217,7 +217,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager {
|
||||
this.plugin.logger().warn("Can't not find item " + subCategory.icon() + " for category icon");
|
||||
return null;
|
||||
}
|
||||
item.displayName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(subCategory.displayName(), ItemBuildContext.EMPTY.tagResolvers())));
|
||||
item.customName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(subCategory.displayName(), ItemBuildContext.EMPTY.tagResolvers())));
|
||||
item.lore(subCategory.displayLore().stream().map(lore -> AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(lore, ItemBuildContext.EMPTY.tagResolvers()))).toList());
|
||||
item.load();
|
||||
return new ItemWithAction(item, (element, click) -> {
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* Helper class for handling Adventure components and related functionalities.
|
||||
*/
|
||||
public class AdventureHelper {
|
||||
|
||||
public static final String EMPTY_COMPONENT = componentToJson(Component.empty());
|
||||
private final MiniMessage miniMessage;
|
||||
private final MiniMessage miniMessageStrict;
|
||||
private final GsonComponentSerializer gsonComponentSerializer;
|
||||
|
||||
Reference in New Issue
Block a user