@@ -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 }
0 commit comments