Skip to content

Commit 22fce88

Browse files
committed
Working on adding more options what you can set
1 parent 3e2af1c commit 22fce88

File tree

2 files changed

+129
-20
lines changed

2 files changed

+129
-20
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/compound/CompoundTag.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ public final class CompoundTag {
4141
Validate.checkNotNull(compoundSession, "The compound session could not be loaded.");
4242
}
4343

44-
/**
45-
* This handle is the NBTTagCompound object.
46-
*
47-
* @return returns the NBTTagCompound object.
48-
*/
49-
@Nonnull
50-
Object getHandle() {
51-
return compoundSession.getHandle();
52-
}
53-
5444
/**
5545
* Checks whether this {@link CompoundTag} contains the given key.
5646
*

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/compound/LegacyNBT.java

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public boolean hasTag() {
197197
* Both root and nested compounds are considered valid targets.
198198
*
199199
* @param name the custom key of the nested compound. To target the root compound,
200-
* use an empty string or {@link #hasTag()}.
200+
* use an empty string or {@link #hasTag()}.
201201
* @return {@code true} if the specified (or root) compound exists
202202
*/
203203
public boolean hasTag(@Nonnull final String name) {
@@ -387,7 +387,10 @@ private CompoundTag getCompound(final String name, final boolean usingName) {
387387
logger.log(Level.FINE, () -> "Empty string passed to getCompound(name). Use getCompound() for root instead.");
388388

389389
try {
390-
if (!hasTag()) return null;
390+
if (!hasTag()) {
391+
this.compoundState = CompoundState.NULL;
392+
return null;
393+
}
391394
Object root = GET_TAG.invoke(nmsItemCopy);
392395
Object nested = null;
393396
if (!name.isEmpty()) {
@@ -421,16 +424,27 @@ public static class CompoundSession {
421424
private static final MethodHandle remove;
422425
private static final MethodHandle setString;
423426
private static final MethodHandle getString;
427+
private static final MethodHandle setInt;
428+
private static final MethodHandle getInt;
429+
private static final MethodHandle getShort;
430+
private static final MethodHandle setShort;
431+
private static final MethodHandle setByte;
432+
private static final MethodHandle getByte;
424433
private static final MethodHandle setBoolean;
425434
private static final MethodHandle getBoolean;
426-
427435
private final Object handle;
428436

429437
static {
430438
MethodHandle hasTagKey = null;
431439
MethodHandle removeM = null;
432440
MethodHandle setStringM = null;
433441
MethodHandle getStringM = null;
442+
MethodHandle setIntM = null;
443+
MethodHandle getIntM = null;
444+
MethodHandle getShortM = null;
445+
MethodHandle setShortM = null;
446+
MethodHandle setByteM = null;
447+
MethodHandle getByteM = null;
434448
MethodHandle setBooleanM = null;
435449
MethodHandle getBooleanM = null;
436450
try {
@@ -444,14 +458,33 @@ public static class CompoundSession {
444458
System.out.println("ReturnType " + method.getReturnType());
445459
});
446460

447-
448461
hasTagKey = lookup.findVirtual(nbtTag, "hasKey",
449462
MethodType.methodType(boolean.class, String.class));
450463
removeM = lookup.findVirtual(nbtTag, "remove",
451464
MethodType.methodType(void.class, String.class));
465+
466+
setIntM = lookup.findVirtual(nbtTag, "setInt",
467+
MethodType.methodType(void.class, String.class, int.class));
468+
469+
getIntM = lookup.findVirtual(nbtTag, "getInt",
470+
MethodType.methodType(int.class, String.class));
471+
472+
setShortM = lookup.findVirtual(nbtTag, "setShort",
473+
MethodType.methodType(void.class, String.class, short.class));
474+
475+
getShortM = lookup.findVirtual(nbtTag, "getShort",
476+
MethodType.methodType(short.class, String.class));
477+
478+
setByteM = lookup.findVirtual(nbtTag, "setByte",
479+
MethodType.methodType(void.class, String.class, byte.class));
480+
481+
getByteM = lookup.findVirtual(nbtTag, "getByte",
482+
MethodType.methodType(byte.class, String.class));
483+
452484
setStringM = lookup.findVirtual(nbtTag, "setString",
453485
MethodType.methodType(void.class, String.class, String.class));
454486
getStringM = lookup.findVirtual(nbtTag, "getString",
487+
455488
MethodType.methodType(String.class, String.class));
456489
setBooleanM = lookup.findVirtual(nbtTag, "setBoolean",
457490
MethodType.methodType(void.class, String.class, boolean.class));
@@ -465,6 +498,13 @@ public static class CompoundSession {
465498
hasKey = hasTagKey;
466499
setString = setStringM;
467500
getString = getStringM;
501+
setInt = setIntM;
502+
getInt = getIntM;
503+
getShort = getShortM;
504+
setShort = setShortM;
505+
setByte = setByteM;
506+
getByte = getByteM;
507+
468508
setBoolean = setBooleanM;
469509
getBoolean = getBooleanM;
470510

@@ -527,6 +567,72 @@ public void remove(@Nonnull final String key) {
527567
}
528568
}
529569

570+
/**
571+
* Sets a String value in the underlying NBTTagCompound.
572+
*
573+
* @param key the key to set
574+
* @param value the String value to assign
575+
*/
576+
public void setString(@Nonnull final String key, final String value) {
577+
if (setString == null) return;
578+
579+
try {
580+
setString.invoke(handle, key, value);
581+
} catch (Throwable e) {
582+
logger.logError(e, () -> "Failed to set string value from reflection");
583+
}
584+
}
585+
586+
/**
587+
* Gets a string value from the underlying NBTTagCompound.
588+
*
589+
* @param key the key of the string value
590+
* @return the stored string value, or empty string if unavailable
591+
*/
592+
public String getString(@Nonnull final String key) {
593+
if (getString == null) return "";
594+
595+
try {
596+
return (String) getString.invoke(handle, key);
597+
} catch (Throwable e) {
598+
logger.logError(e, () -> "Failed to retrieve string value from reflection");
599+
}
600+
return "";
601+
}
602+
603+
/**
604+
* Sets a byte value in the underlying NBTTagCompound.
605+
*
606+
* @param key the key to set
607+
* @param value the byte value to assign
608+
*/
609+
public void setByte(@Nonnull final String key, final byte value) {
610+
if (setByte == null) return;
611+
612+
try {
613+
setByte.invoke(handle, key, value);
614+
} catch (Throwable e) {
615+
logger.logError(e, () -> "Failed to set byte value from reflection");
616+
}
617+
}
618+
619+
/**
620+
* Gets a byte value from the underlying NBTTagCompound.
621+
*
622+
* @param key the key of the byte value
623+
* @return the stored byte value, or {@code -1} if unavailable
624+
*/
625+
public byte getByte(@Nonnull final String key) {
626+
if (getByte == null) return -1;
627+
628+
try {
629+
return (byte) getByte.invoke(handle, key);
630+
} catch (Throwable e) {
631+
logger.logError(e, () -> "Failed to retrieve byte value from reflection");
632+
}
633+
return -1;
634+
}
635+
530636
/**
531637
* Sets a boolean value in the underlying NBTTagCompound.
532638
*
@@ -586,21 +692,34 @@ private static String getPackageVersion() {
586692
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
587693
}
588694

695+
/**
696+
* Represents the current state of a compound during retrieval or creation.
697+
*
698+
* <p>There are four possible states:</p>
699+
* <ul>
700+
* <li><strong>NOT_CREATED</strong> – The compound has not been created yet.</li>
701+
* <li><strong>CREATED</strong> – The compound was successfully retrieved or created.
702+
* This may refer to either the root compound or a nested compound.</li>
703+
* <li><strong>NULL</strong> – The root or nested compound does not exist.</li>
704+
* <li><strong>ERROR</strong> – An error occurred while attempting to retrieve or create the compound.</li>
705+
* </ul>
706+
*/
589707
public enum CompoundState {
590-
ERROR("Failed to initialize compound"),
708+
NOT_CREATED("Compound has not been created yet and may be null"),
591709
CREATED("Compound created successfully"),
592-
NULL("Compound is null"),
593-
NOT_CREATED("Compound is not set yet, can be null");
710+
ERROR("Failed to initialize compound"),
711+
NULL("Compound does not exist");
712+
594713
private final String message;
595714

596-
CompoundState(String message) {
715+
CompoundState(@Nonnull final String message) {
597716
this.message = message;
598717
}
599718

600719
/**
601-
* The message that you could send to a logger.
720+
* Returns a descriptive message suitable for logging.
602721
*
603-
* @return the message for the specific state.
722+
* @return the descriptive message for this state.
604723
*/
605724
public String getMessage() {
606725
return message;

0 commit comments

Comments
 (0)