Skip to content

Commit c4997d2

Browse files
committed
Now support wildcard usage on item ids for weapons/armor
1 parent a095229 commit c4997d2

5 files changed

Lines changed: 80 additions & 29 deletions

File tree

src/main/java/com/azuredoom/classescore/data/EquipmentRules.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Collections;
44
import java.util.Set;
55

6+
import com.azuredoom.classescore.util.ItemPatternMatcher;
7+
68
/**
79
* Defines rules for allowed weapons and armor for a specific class.
810
*
@@ -33,10 +35,7 @@ public EquipmentRules(Set<String> allowedWeapons, Set<String> allowedArmor) {
3335
* @return true if the weapon is allowed, or if the set of allowed weapons is empty; false otherwise.
3436
*/
3537
public boolean isWeaponAllowed(String weaponId) {
36-
if (allowedWeapons.isEmpty()) {
37-
return true;
38-
}
39-
return allowedWeapons.contains(weaponId);
38+
return ItemPatternMatcher.matches(allowedWeapons, weaponId);
4039
}
4140

4241
/**
@@ -47,9 +46,6 @@ public boolean isWeaponAllowed(String weaponId) {
4746
* @return true if the armor is allowed, or if the set of allowed armor is empty; false otherwise.
4847
*/
4948
public boolean isArmorAllowed(String armorId) {
50-
if (allowedArmor.isEmpty()) {
51-
return true;
52-
}
53-
return allowedArmor.contains(armorId);
49+
return ItemPatternMatcher.matches(allowedArmor, armorId);
5450
}
5551
}

src/main/java/com/azuredoom/classescore/gameplay/services/items/PlayerRestrictionCache.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,9 @@ public class PlayerRestrictionCache {
2121
* @param classDef the class definition containing the class ID and equipment restriction rules
2222
*/
2323
public void setClass(UUID playerId, ClassDefinition classDef) {
24-
Set<String> allowedWeapons = classDef.equipmentRules().allowedWeapons();
25-
if (allowedWeapons == null) {
26-
allowedWeapons = Collections.emptySet();
27-
}
28-
Set<String> allowedArmor = classDef.equipmentRules().allowedArmor();
29-
if (allowedArmor == null) {
30-
allowedArmor = Collections.emptySet();
31-
}
32-
3324
states.put(
3425
playerId,
35-
new PlayerRestrictionState(classDef.id(), Set.copyOf(allowedWeapons), Set.copyOf(allowedArmor))
26+
new PlayerRestrictionState(classDef.id(), classDef.equipmentRules())
3627
);
3728
}
3829

@@ -61,8 +52,7 @@ public boolean canUseWeapon(UUID playerId, String itemId) {
6152
return true;
6253
}
6354

64-
var allowedWeapons = state.allowedWeapons();
65-
return allowedWeapons.isEmpty() || allowedWeapons.contains(itemId);
55+
return state.equipmentRules().isWeaponAllowed(itemId);
6656
}
6757

6858
/**
@@ -80,8 +70,7 @@ public boolean canUseArmor(UUID playerId, String itemId) {
8070
return true;
8171
}
8272

83-
var allowedArmor = state.allowedArmor();
84-
return allowedArmor.isEmpty() || allowedArmor.contains(itemId);
73+
return state.equipmentRules().isArmorAllowed(itemId);
8574
}
8675

8776
/**
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.azuredoom.classescore.gameplay.services.items;
22

3-
import java.util.Set;
3+
import com.azuredoom.classescore.data.EquipmentRules;
44

55
/**
66
* Represents the state of restrictions applied to a player based on their class. This record is used to manage the
77
* allowable weapons and armor items that a player can use, according to their designated class.
88
*
99
* @param classId The unique identifier of the player's class.
10-
* @param allowedWeapons A set of item IDs representing the weapons allowed for the player's class.
11-
* @param allowedArmor A set of item IDs representing the armor allowed for the player's class.
10+
* @param equipmentRules The equipment rules associated with the player's class.
1211
*/
1312
public record PlayerRestrictionState(
1413
String classId,
15-
Set<String> allowedWeapons,
16-
Set<String> allowedArmor
14+
EquipmentRules equipmentRules
1715
) {}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.azuredoom.classescore.util;
2+
3+
import java.util.Set;
4+
import java.util.regex.Pattern;
5+
6+
public final class ItemPatternMatcher {
7+
8+
private ItemPatternMatcher() {}
9+
10+
public static boolean matches(Set<String> allowedPatterns, String itemId) {
11+
if (allowedPatterns == null || allowedPatterns.isEmpty()) {
12+
return true;
13+
}
14+
15+
if (itemId == null || itemId.isBlank()) {
16+
return false;
17+
}
18+
19+
for (String pattern : allowedPatterns) {
20+
if (matchesPattern(pattern, itemId)) {
21+
return true;
22+
}
23+
}
24+
25+
return false;
26+
}
27+
28+
private static boolean matchesPattern(String pattern, String itemId) {
29+
if (pattern == null || pattern.isBlank()) {
30+
return false;
31+
}
32+
33+
if (!pattern.contains("*")) {
34+
return pattern.equals(itemId);
35+
}
36+
37+
String regex = toRegex(pattern);
38+
return Pattern.matches(regex, itemId);
39+
}
40+
41+
private static String toRegex(String glob) {
42+
StringBuilder regex = new StringBuilder();
43+
regex.append("^");
44+
45+
for (char c : glob.toCharArray()) {
46+
switch (c) {
47+
case '*' -> regex.append(".*");
48+
case '.' -> regex.append("\\.");
49+
case '(' -> regex.append("\\(");
50+
case ')' -> regex.append("\\)");
51+
case '[' -> regex.append("\\[");
52+
case ']' -> regex.append("\\]");
53+
case '{' -> regex.append("\\{");
54+
case '}' -> regex.append("\\}");
55+
case '+' -> regex.append("\\+");
56+
case '?' -> regex.append("\\?");
57+
case '^' -> regex.append("\\^");
58+
case '$' -> regex.append("\\$");
59+
case '|' -> regex.append("\\|");
60+
case '\\' -> regex.append("\\\\");
61+
default -> regex.append(c);
62+
}
63+
}
64+
65+
regex.append("$");
66+
return regex.toString();
67+
}
68+
}

src/main/resources/classes/blademaster.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
}
5151
],
5252
"equipmentRules": {
53-
"allowedWeapons": ["Weapon_Sword_Adamantite", "Weapon_Sword_Crude"],
54-
"allowedArmor": ["Armor_Adamantite_Chest", "Armor_Adamantite_Legs", "Armor_Adamantite_Head", "Armor_Adamantite_Hands"]
53+
"allowedWeapons": ["Weapon_Sword_*"],
54+
"allowedArmor": ["Armor_Adamantite_*"]
5555
}
5656
}

0 commit comments

Comments
 (0)