Skip to content

Commit

Permalink
vanilla durability mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Oct 20, 2024
1 parent 80a3576 commit dc4f2e3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/main/java/think/rpgitems/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public Configuration(RPGItems plugin) {
@Serializable(name = "item.defaults.force_bar", alias = "forceBar")
public boolean forceBar = false;

@Serializable(name = "item.defaults.vanilla_durability")
public boolean defaultVanillaDurability = false;

@Serializable(name = "item.defaults.license")
public String defaultLicense = "All Right Reserved";

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/think/rpgitems/commands/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ public void itemCost(CommandSender sender, Arguments args) {
}

@SubCommand(value = "durability", tabCompleter = "itemCompleter")
@Completion("item:infinite,default,bound,togglebar,barformat")
@Completion("item:infinite,default,bound,togglebar,barformat,vanilla")
public void itemDurability(CommandSender sender, Arguments args) {
if (readOnly(sender)) return;
RPGItem item = getItem(args.nextString(), sender);
Expand Down Expand Up @@ -1364,6 +1364,14 @@ public void itemDurability(CommandSender sender, Arguments args) {
msgs(sender, "message.barformat." + item.getBarFormat().name());
break;
}
case "vanilla": {
item.setDurabilityVanilla(!item.isDurabilityVanilla());
item.rebuild();
ItemManager.refreshItem();
ItemManager.save(item);
msgs(sender, "message.durability.vanilla", I18n.getFormatted(sender, item.isDurabilityVanilla() ? "message.on" : "message.off"));
break;
}
default:
throw new BadCommandException("message.error.invalid_option", arg, "durability", "value,infinite,togglebar,default,bound");
}
Expand Down
46 changes: 32 additions & 14 deletions src/main/java/think/rpgitems/item/RPGItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.MC1_14_R1;
import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.MC1_21_R1;
import static org.bukkit.Material.*;
import static org.bukkit.attribute.AttributeModifier.Operation.ADD_NUMBER;
Expand Down Expand Up @@ -185,6 +186,7 @@ public class RPGItem implements RPGBaseHolder {
@Getter @Setter private int durabilityLowerBound;
@Getter @Setter private int durabilityUpperBound;
@Getter @Setter private BarFormat barFormat = BarFormat.DEFAULT;
@Getter @Setter private boolean durabilityVanilla = plugin.cfg.defaultVanillaDurability;

@Getter @Setter private int blockBreakingCost = 0;
@Getter @Setter private int hittingCost = 0;
Expand Down Expand Up @@ -454,6 +456,7 @@ private void restore(ConfigurationSection s) throws UnknownPowerException, Unkno
if (getDefaultDurability() <= 0) {
setDefaultDurability(getMaxDurability());
}
setDurabilityVanilla(s.getBoolean("durabilityVanilla", plugin.cfg.defaultVanillaDurability));
setDurabilityLowerBound(s.getInt("durabilityLowerBound", 0));
setDurabilityUpperBound(s.getInt("durabilityUpperBound", getItem().getMaxDurability()));
if (s.isBoolean("forceBar")) {
Expand Down Expand Up @@ -724,6 +727,7 @@ public void save(ConfigurationSection s) {
s.set("hitCostByDamage", isHitCostByDamage());
s.set("maxDurability", getMaxDurability());
s.set("defaultDurability", getDefaultDurability());
s.set("durabilityVanilla", isDurabilityVanilla());
s.set("durabilityLowerBound", getDurabilityLowerBound());
s.set("durabilityUpperBound", getDurabilityUpperBound());
s.set("hasDurabilityBar", isHasDurabilityBar());
Expand Down Expand Up @@ -857,19 +861,22 @@ public void updateItem(@Nullable Player player, ItemStack item, boolean loreOnly
if (meta instanceof LeatherArmorMeta) {
((LeatherArmorMeta) meta).setColor(Color.fromRGB(getDataValue()));
}
Damageable damageable = (Damageable) meta;
if (getMaxDurability() > 0) {
int durability = ItemPDC.computeIfAbsent(rpgitemsTagContainer, TAG_DURABILITY, PersistentDataType.INTEGER, this::getDefaultDurability);
if (isCustomItemModel()) {
damageable.setDamage(getDataValue());
} else {
damageable.setDamage((getItem().getMaxDurability() - ((short) ((double) getItem().getMaxDurability() * ((double) durability / (double) getMaxDurability())))));
}
} else {
if (isCustomItemModel()) {
damageable.setDamage(getDataValue());
if (!isDurabilityVanilla() && meta instanceof Damageable) {
// TODO: Damageable 不兼容 1.12 以下版本
Damageable damageable = (Damageable) meta;
if (getMaxDurability() > 0) {
int durability = ItemPDC.computeIfAbsent(rpgitemsTagContainer, TAG_DURABILITY, PersistentDataType.INTEGER, this::getDefaultDurability);
if (isCustomItemModel()) {
damageable.setDamage(getDataValue());
} else {
damageable.setDamage((getItem().getMaxDurability() - ((short) ((double) getItem().getMaxDurability() * ((double) durability / (double) getMaxDurability())))));
}
} else {
damageable.setDamage(getItem().getMaxDurability() != 0 ? 0 : getDataValue());
if (isCustomItemModel()) {
damageable.setDamage(getDataValue());
} else {
damageable.setDamage(getItem().getMaxDurability() != 0 ? 0 : getDataValue());
}
}
}
// Patch for mcMMO buff. See SkillUtils.java#removeAbilityBuff in mcMMO
Expand Down Expand Up @@ -912,7 +919,9 @@ public void updateItem(@Nullable Player player, ItemStack item, boolean loreOnly
meta.setUnbreakable(hasMarker(Unbreakable.class));
meta.removeItemFlags(meta.getItemFlags().toArray(new ItemFlag[0]));

meta.setCustomModelData(itemUpdateEvent.getCustomModelData());
if (MinecraftVersion.isAtLeastVersion(MC1_14_R1)) {
meta.setCustomModelData(itemUpdateEvent.getCustomModelData());
}
for (ItemFlag flag : itemUpdateEvent.getItemFlags()) {
meta.addItemFlags(flag);
}
Expand Down Expand Up @@ -1676,8 +1685,17 @@ public boolean consumeDurability(ItemStack item, int val, boolean checkbound) {

public boolean consumeDurability(@Nullable Player player, ItemStack item, int val, boolean checkbound) {
if (val == 0) return true;
int durability;
ItemMeta itemMeta = item.getItemMeta();
if (durabilityVanilla) {
// TODO: Damageable 不兼容 1.12 以下版本
if (!(itemMeta instanceof Damageable)) return false;
Damageable damageable = (Damageable) itemMeta;
int value = damageable.getDamage() + val;
if (value > item.getType().getMaxDurability()) return false;
damageable.setDamage(value);
return true;
}
int durability;
if (getMaxDurability() != -1) {
ISubItemTagContainer tagContainer = ItemPDC.makeTag(Objects.requireNonNull(itemMeta), TAG_META);
durability = ItemPDC.computeIfAbsent(tagContainer, TAG_DURABILITY, PersistentDataType.INTEGER, this::getDefaultDurability);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/lang/zh_CN.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
message:
line_separator: ' '
on: '开启'
off: '关闭'
error:
invalid_command_arg: '加载技能失败:%s,%s'
legacy_name: '%s 在 Spigot 1.13 有一个新名字:%s'
Expand Down Expand Up @@ -93,6 +95,7 @@ message:
bound: 耐久下界和上界已设置为 %s 与 %s
default: 默认耐久已设置为 %s
toggle: 耐久条已切换
vanilla: 原版耐久模式已切换为 %s
worldguard:
disable: Worldguard支持已禁用
error: 没有发现Worldguard插件
Expand Down

0 comments on commit dc4f2e3

Please sign in to comment.