Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 4205444

Browse files
committed
add acl
1 parent d5da154 commit 4205444

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cat.nyaa.nyaautils;
2+
3+
import cat.nyaa.utils.FileConfigure;
4+
import cat.nyaa.utils.ISerializable;
5+
import org.bukkit.ChatColor;
6+
import org.bukkit.configuration.ConfigurationSection;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
9+
import java.util.*;
10+
11+
public class Acl extends FileConfigure {
12+
13+
public static class list implements ISerializable {
14+
@Serializable
15+
boolean enchant = true;
16+
@Serializable
17+
boolean repair = true;
18+
19+
public list normalize() {
20+
return this;
21+
}
22+
}
23+
24+
private static final Map<String, list> aclMap = new HashMap<>();
25+
private final NyaaUtils plugin;
26+
public Acl(NyaaUtils plugin) {
27+
this.plugin = plugin;
28+
}
29+
30+
@Override
31+
protected String getFileName() {
32+
return "acl.yml";
33+
}
34+
35+
@Override
36+
protected JavaPlugin getPlugin() {
37+
return plugin;
38+
}
39+
40+
@Override
41+
public void deserialize(ConfigurationSection config) {
42+
aclMap.clear();
43+
for (String key : config.getKeys(false)) {
44+
if (!config.isConfigurationSection(key)) continue;
45+
list list = new list();
46+
list.deserialize(config.getConfigurationSection(key));
47+
aclMap.put(key, list.normalize());
48+
}
49+
}
50+
51+
@Override
52+
public void serialize(ConfigurationSection config) {
53+
Set<String> tmp = new HashSet<>(config.getKeys(false));
54+
for (String key : tmp) { // clear section
55+
config.set(key, null);
56+
}
57+
58+
for (Map.Entry<String, list> pair : aclMap.entrySet()) {
59+
ConfigurationSection section = config.createSection(pair.getKey());
60+
pair.getValue().normalize().serialize(section);
61+
}
62+
}
63+
64+
public boolean canEnchant(List<String> lore) {
65+
for (String s : lore) {
66+
if (aclMap.containsKey(ChatColor.stripColor(s))) {
67+
if (aclMap.get(ChatColor.stripColor(s)).enchant != plugin.cfg.acl_default_enchant) {
68+
return aclMap.get(ChatColor.stripColor(s)).enchant;
69+
}
70+
}
71+
}
72+
return plugin.cfg.acl_default_enchant;
73+
74+
}
75+
76+
public boolean canRepair(List<String> lore) {
77+
for (String s : lore) {
78+
if (aclMap.containsKey(ChatColor.stripColor(s))) {
79+
if (aclMap.get(ChatColor.stripColor(s)).repair != plugin.cfg.acl_default_repair) {
80+
return aclMap.get(ChatColor.stripColor(s)).repair;
81+
}
82+
}
83+
}
84+
return plugin.cfg.acl_default_repair;
85+
}
86+
}

src/main/java/cat/nyaa/nyaautils/CommandHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public void commandEnchant(CommandSender sender, Arguments args) {
8686
return;
8787
}
8888

89+
if (main.hasItemMeta() && main.getItemMeta().hasLore()) {
90+
if (!plugin.cfg.acl.canEnchant(main.getItemMeta().getLore())) {
91+
sender.sendMessage(I18n._("user.enchant.invalid_item"));
92+
return;
93+
}
94+
}
95+
8996
String enchStr = args.next().toUpperCase();
9097
Enchantment ench = Enchantment.getByName(enchStr);
9198
if (ench == null) {

src/main/java/cat/nyaa/nyaautils/Configuration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,25 @@ public class Configuration implements ISerializable {
8686
public int mailCooldown = 20;
8787
@Serializable(name = "mail.timeoutTicks")
8888
public int mailTimeout = 200;
89+
@Serializable
90+
public boolean acl_default_enchant = true;
91+
@Serializable
92+
public boolean acl_default_repair = true;
8993

9094
public List<BasicItemMatcher> enchantSrc = new ArrayList<>();
9195
public HashMap<Enchantment, Integer> enchantMaxLevel = new HashMap<>();
9296

9397
public final MailboxLocations mailbox;
9498
public final RepairConfig repair;
99+
public final Acl acl;
95100

96101
private final NyaaUtils plugin;
97102

98103
public Configuration(NyaaUtils plugin) {
99104
this.plugin = plugin;
100105
this.mailbox = new MailboxLocations(plugin);
101106
this.repair = new RepairConfig(plugin);
107+
this.acl = new Acl(plugin);
102108
for (Enchantment e : Enchantment.values()) {
103109
if (e == null) {
104110
plugin.getLogger().warning("Bad enchantment: null");
@@ -149,6 +155,7 @@ public void deserialize(ConfigurationSection config) {
149155

150156
if (plugin.isServerEnabled()) mailbox.load();
151157
repair.load();
158+
acl.load();
152159
}
153160

154161
@Override
@@ -170,6 +177,7 @@ public void serialize(ConfigurationSection config) {
170177

171178
if (plugin.isServerEnabled()) mailbox.save();
172179
repair.save();
180+
acl.save();
173181
}
174182

175183
public enum LootProtectMode {

src/main/java/cat/nyaa/nyaautils/repair/RepairCommands.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void addRepairItem(CommandSender sender, Arguments args) {
4949
@SubCommand(value = "info", permission = "nu.repair")
5050
public void repairInfo(CommandSender sender, Arguments args) {
5151
ItemStack item = getItemInHand(sender);
52-
RepairInstance info = new RepairInstance(item, plugin.cfg.repair);
52+
RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin);
5353
new Message(I18n._("user.repair.info_1")).append(item).send(asPlayer(sender));
5454
msg(sender, "user.repair.info_2", item.getType().name());
5555
if (info.stat != REPAIRABLE) {
@@ -89,7 +89,7 @@ private void increaseReapirCount(ItemStack item, int x) {
8989
public void repairHand(CommandSender sender, Arguments args) {
9090
ItemStack item = getItemInHand(sender);
9191
ItemStack material = getItemInOffHand(sender);
92-
RepairInstance info = new RepairInstance(item, plugin.cfg.repair);
92+
RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin);
9393
if (info.stat != REPAIRABLE) {
9494
msg(sender, "user.repair.info_3", I18n._("user.repair.unrepairable." + info.stat.name()));
9595
return;
@@ -126,7 +126,7 @@ public void repairHand(CommandSender sender, Arguments args) {
126126
public void repairFull(CommandSender sender, Arguments args) {
127127
ItemStack item = getItemInHand(sender);
128128
ItemStack material = getItemInOffHand(sender);
129-
RepairInstance info = new RepairInstance(item, plugin.cfg.repair);
129+
RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin);
130130
if (info.stat != REPAIRABLE) {
131131
msg(sender, "user.repair.info_3", I18n._("user.repair.unrepairable." + info.stat.name()));
132132
return;

src/main/java/cat/nyaa/nyaautils/repair/RepairInstance.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cat.nyaa.nyaautils.repair;
22

3+
import cat.nyaa.nyaautils.NyaaUtils;
34
import org.bukkit.Material;
45
import org.bukkit.inventory.ItemStack;
56
import org.bukkit.inventory.meta.Repairable;
@@ -20,11 +21,17 @@ public enum RepairStat{
2021
public int durRecovered;
2122
public int repairLimit;
2223

23-
public RepairInstance(ItemStack item, RepairConfig config) {
24+
public RepairInstance(ItemStack item, RepairConfig config, NyaaUtils plugin) {
2425
if (item == null || item.getType() == Material.AIR) return;
2526
RepairConfig.RepairConfigItem cfg = config.getRepairConfig(item.getType());
2627
if (cfg == null) return;
2728
if (!(item.getItemMeta() instanceof Repairable)) return;
29+
if (item.hasItemMeta() && item.getItemMeta().hasLore()) {
30+
if (!plugin.cfg.acl.canRepair(item.getItemMeta().getLore())) {
31+
stat = RepairStat.UNREPAIRABLE;
32+
return;
33+
}
34+
}
2835
stat = RepairStat.REPAIRABLE;
2936
if (item.getItemMeta().spigot().isUnbreakable()){
3037
stat = RepairStat.UNREPAIRABLE_UNBREAKABLE;

0 commit comments

Comments
 (0)