Skip to content

Commit a274158

Browse files
committed
Add configurable container PDC key and clear-tag rules
1 parent ee63853 commit a274158

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

src/main/java/me/crafter/mc/lockettepro/Config.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public class Config {
2222
private static Set<String> privatestrings = new HashSet<>();
2323
private static Set<String> additionalstrings = new HashSet<>();
2424
private static Set<String> everyonestrings = new HashSet<>();
25+
private static Set<String> pdcclearstrings = new HashSet<>();
2526
private static Set<String> timerstrings = new HashSet<>();
27+
private static String lockedcontainerpdckey = "lockettepro:locked_container";
2628
private static String defaultprivatestring = "[Private]";
2729
private static String defaultadditionalstring = "[More Users]";
2830
private static byte enablequickprotect = (byte) 1;
@@ -71,10 +73,16 @@ public static void reload() {
7173
List<String> privatestringlist = config.getStringList("private-signs");
7274
List<String> additionalstringlist = config.getStringList("additional-signs");
7375
List<String> everyonestringlist = config.getStringList("everyone-signs");
76+
List<String> pdcclearstringlist = config.getStringList("pdc-clear-sign-tags");
7477
List<String> protectionexemptstringlist = config.getStringList("protection-exempt");
7578
privatestrings = new HashSet<>(privatestringlist);
7679
additionalstrings = new HashSet<>(additionalstringlist);
7780
everyonestrings = new HashSet<>(everyonestringlist);
81+
pdcclearstrings = new HashSet<>(pdcclearstringlist);
82+
lockedcontainerpdckey = config.getString("locked-container-pdc-key", "lockettepro:locked_container");
83+
if (lockedcontainerpdckey == null || lockedcontainerpdckey.isBlank()) {
84+
lockedcontainerpdckey = "lockettepro:locked_container";
85+
}
7886
protectionexempt = new HashSet<>(protectionexemptstringlist);
7987
defaultprivatestring = privatestringlist.get(0);
8088
defaultadditionalstring = additionalstringlist.get(0);
@@ -153,6 +161,9 @@ public static void initDefaultConfig() {
153161
config.addDefault("additional-signs", additional_signs);
154162
String[] everyone_signs = {"[Everyone]", "[everyone]"};
155163
config.addDefault("everyone-signs", everyone_signs);
164+
String[] pdc_clear_sign_tags = {"[hopper]"};
165+
config.addDefault("pdc-clear-sign-tags", pdc_clear_sign_tags);
166+
config.addDefault("locked-container-pdc-key", "lockettepro:locked_container");
156167
String[] timer_signs = {"[Timer:@]", "[timer:@]"};
157168
config.addDefault("timer-signs", timer_signs);
158169
String[] lockables = {"CHEST","TRAPPED_CHEST","FURNACE","BURNING_FURNACE","HOPPER","BREWING_STAND","DIAMOND_BLOCK",
@@ -243,6 +254,14 @@ public static boolean isEveryoneSignString(String message) {
243254
return everyonestrings.contains(message);
244255
}
245256

257+
public static boolean isPdcClearSignString(String message) {
258+
return pdcclearstrings.contains(message);
259+
}
260+
261+
public static String getLockedContainerPdcKeyString() {
262+
return lockedcontainerpdckey;
263+
}
264+
246265
public static boolean isTimerSignString(String message) {
247266
for (String timerstring : timerstrings) {
248267
String[] splitted = timerstring.split("@", 2);

src/main/java/me/crafter/mc/lockettepro/LocketteProAPI.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,59 @@ public static boolean isEveryoneOnSign(Block block) { // Requires (isLockSign or
219219
return false;
220220
}
221221

222+
public static boolean shouldClearContainerPdcTag(Block block) {
223+
if (block == null) return false;
224+
if (block.getBlockData() instanceof Door) {
225+
Block[] doors = getDoors(block);
226+
if (doors == null) return false;
227+
for (BlockFace doorface : newsfaces) {
228+
Block relative0 = doors[0].getRelative(doorface), relative1 = doors[1].getRelative(doorface);
229+
if (relative0.getType() == doors[0].getType() && relative1.getType() == doors[1].getType()) {
230+
if (hasPdcClearTagSingleBlock(relative1.getRelative(BlockFace.UP), doorface.getOppositeFace()))
231+
return true;
232+
if (hasPdcClearTagSingleBlock(relative1, doorface.getOppositeFace())) return true;
233+
if (hasPdcClearTagSingleBlock(relative0, doorface.getOppositeFace())) return true;
234+
if (hasPdcClearTagSingleBlock(relative0.getRelative(BlockFace.DOWN), doorface.getOppositeFace()))
235+
return true;
236+
}
237+
}
238+
if (hasPdcClearTagSingleBlock(doors[1].getRelative(BlockFace.UP), null)) return true;
239+
if (hasPdcClearTagSingleBlock(doors[1], null)) return true;
240+
if (hasPdcClearTagSingleBlock(doors[0], null)) return true;
241+
if (hasPdcClearTagSingleBlock(doors[0].getRelative(BlockFace.DOWN), null)) return true;
242+
} else if (block.getBlockData() instanceof Chest) {
243+
BlockFace chestface = getRelativeChestFace(block);
244+
if (chestface != null) {
245+
Block relativechest = block.getRelative(chestface);
246+
if (hasPdcClearTagSingleBlock(relativechest, chestface.getOppositeFace())) return true;
247+
}
248+
}
249+
return hasPdcClearTagSingleBlock(block, null);
250+
}
251+
252+
private static boolean hasPdcClearTagSingleBlock(Block block, BlockFace exempt) {
253+
for (BlockFace blockface : newsfaces) {
254+
if (blockface == exempt) continue;
255+
Block relativeblock = block.getRelative(blockface);
256+
if (isLockSignOrAdditionalSign(relativeblock) && getFacing(relativeblock) == blockface) {
257+
if (hasPdcClearTagOnSign(relativeblock)) {
258+
return true;
259+
}
260+
}
261+
}
262+
return false;
263+
}
264+
265+
private static boolean hasPdcClearTagOnSign(Block block) {
266+
String[] lines = ((Sign) block.getState()).getSide(Side.FRONT).getLines();
267+
for (int i = 1; i < 4; i++) {
268+
if (Config.isPdcClearSignString(lines[i])) {
269+
return true;
270+
}
271+
}
272+
return false;
273+
}
274+
222275
public static boolean isOwnerOfSign(Block block, Player player) { // Requires isSign
223276
Block protectedblock = getAttachedBlock(block);
224277
// Normal situation, that block is just locked by an adjacent sign

src/main/java/me/crafter/mc/lockettepro/Utils.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Utils {
4242

4343
public static final String usernamepattern = "^[a-zA-Z0-9_]*$";
4444
public static final String LOCKED_CONTAINER_PDC_KEY_STRING = "lockettepro:locked_container";
45-
private static final String LOCKED_CONTAINER_PDC_KEY_PATH = "locked_container";
45+
private static final String LOCKED_CONTAINER_PDC_KEY_DEFAULT_PATH = "locked_container";
4646
private static final LockedContainerPdcAccess LOCKED_CONTAINER_PDC_ACCESS = LockedContainerPdcAccess.create();
4747

4848
private static final LoadingCache<UUID, Block> selectedsign = CacheBuilder.newBuilder()
@@ -214,7 +214,9 @@ private static void setLockedContainerPdc(Block block, boolean locked) {
214214
public static void refreshLockedContainerPdcTag(Block block) {
215215
if (block == null) return;
216216

217-
boolean locked = LocketteProAPI.isLocked(block) && !LocketteProAPI.isOpenToEveryone(block);
217+
boolean locked = LocketteProAPI.isLocked(block)
218+
&& !LocketteProAPI.isOpenToEveryone(block)
219+
&& !LocketteProAPI.shouldClearContainerPdcTag(block);
218220
setLockedContainerPdc(block, locked);
219221
syncConnectedContainerPdc(block, locked);
220222
}
@@ -247,6 +249,8 @@ public static void refreshLockedContainerPdcTagsInLoadedChunks() {
247249
}
248250

249251
private static final class LockedContainerPdcAccess {
252+
private final Method namespacedKeyFromStringMethod;
253+
private final Method namespacedKeyFromStringWithPluginMethod;
250254
private final Constructor<?> namespacedKeyConstructor;
251255
private final Object byteType;
252256
private final Method tileStateGetPdcMethod;
@@ -255,13 +259,17 @@ private static final class LockedContainerPdcAccess {
255259
private final Method pdcRemoveMethod;
256260

257261
private LockedContainerPdcAccess(
262+
Method namespacedKeyFromStringMethod,
263+
Method namespacedKeyFromStringWithPluginMethod,
258264
Constructor<?> namespacedKeyConstructor,
259265
Object byteType,
260266
Method tileStateGetPdcMethod,
261267
Method pdcHasMethod,
262268
Method pdcSetMethod,
263269
Method pdcRemoveMethod
264270
) {
271+
this.namespacedKeyFromStringMethod = namespacedKeyFromStringMethod;
272+
this.namespacedKeyFromStringWithPluginMethod = namespacedKeyFromStringWithPluginMethod;
265273
this.namespacedKeyConstructor = namespacedKeyConstructor;
266274
this.byteType = byteType;
267275
this.tileStateGetPdcMethod = tileStateGetPdcMethod;
@@ -278,13 +286,25 @@ static LockedContainerPdcAccess create() {
278286
Class<?> persistentDataTypeClass = Class.forName("org.bukkit.persistence.PersistentDataType");
279287

280288
Constructor<?> namespacedKeyConstructor = namespacedKeyClass.getConstructor(Plugin.class, String.class);
289+
Method namespacedKeyFromStringMethod = null;
290+
Method namespacedKeyFromStringWithPluginMethod = null;
291+
try {
292+
namespacedKeyFromStringMethod = namespacedKeyClass.getMethod("fromString", String.class);
293+
} catch (NoSuchMethodException ignored) {
294+
}
295+
try {
296+
namespacedKeyFromStringWithPluginMethod = namespacedKeyClass.getMethod("fromString", String.class, Plugin.class);
297+
} catch (NoSuchMethodException ignored) {
298+
}
281299
Method tileStateGetPdcMethod = tileStateClass.getMethod("getPersistentDataContainer");
282300
Method pdcHasMethod = persistentDataContainerClass.getMethod("has", namespacedKeyClass, persistentDataTypeClass);
283301
Method pdcSetMethod = persistentDataContainerClass.getMethod("set", namespacedKeyClass, persistentDataTypeClass, Object.class);
284302
Method pdcRemoveMethod = persistentDataContainerClass.getMethod("remove", namespacedKeyClass);
285303
Field byteTypeField = persistentDataTypeClass.getField("BYTE");
286304

287305
return new LockedContainerPdcAccess(
306+
namespacedKeyFromStringMethod,
307+
namespacedKeyFromStringWithPluginMethod,
288308
namespacedKeyConstructor,
289309
byteTypeField.get(null),
290310
tileStateGetPdcMethod,
@@ -293,7 +313,7 @@ static LockedContainerPdcAccess create() {
293313
pdcRemoveMethod
294314
);
295315
} catch (ReflectiveOperationException ignored) {
296-
return new LockedContainerPdcAccess(null, null, null, null, null, null);
316+
return new LockedContainerPdcAccess(null, null, null, null, null, null, null, null);
297317
}
298318
}
299319

@@ -308,8 +328,38 @@ boolean isSupported() {
308328

309329
Object createKey() {
310330
if (!isSupported()) return null;
331+
String keyString = Config.getLockedContainerPdcKeyString();
332+
if (keyString == null || keyString.isBlank()) {
333+
keyString = LOCKED_CONTAINER_PDC_KEY_STRING;
334+
}
335+
336+
if (this.namespacedKeyFromStringWithPluginMethod != null) {
337+
try {
338+
Object key = this.namespacedKeyFromStringWithPluginMethod.invoke(null, keyString, LockettePro.getPlugin());
339+
if (key != null) return key;
340+
} catch (ReflectiveOperationException ignored) {
341+
}
342+
}
343+
344+
if (this.namespacedKeyFromStringMethod != null) {
345+
try {
346+
Object key = this.namespacedKeyFromStringMethod.invoke(null, keyString);
347+
if (key != null) return key;
348+
} catch (ReflectiveOperationException ignored) {
349+
}
350+
}
351+
352+
String keyPath = keyString;
353+
int separator = keyPath.indexOf(':');
354+
if (separator >= 0) {
355+
keyPath = keyPath.substring(separator + 1);
356+
}
357+
if (keyPath.isEmpty()) {
358+
keyPath = LOCKED_CONTAINER_PDC_KEY_DEFAULT_PATH;
359+
}
360+
311361
try {
312-
return this.namespacedKeyConstructor.newInstance(LockettePro.getPlugin(), LOCKED_CONTAINER_PDC_KEY_PATH);
362+
return this.namespacedKeyConstructor.newInstance(LockettePro.getPlugin(), keyPath);
313363
} catch (ReflectiveOperationException ignored) {
314364
return null;
315365
}

src/main/resources/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ additional-signs:
2727
everyone-signs:
2828
- '[Everyone]'
2929
- '[everyone]'
30+
pdc-clear-sign-tags:
31+
- '[hopper]'
32+
locked-container-pdc-key: 'lockettepro:locked_container'
3033
timer-signs:
3134
- '[Timer:@]'
3235
- '[timer:@]'
@@ -51,4 +54,4 @@ lockables:
5154
- IRON_DOOR
5255
- LECTERN
5356
protection-exempt:
54-
- nothing
57+
- nothing

0 commit comments

Comments
 (0)