-
Notifications
You must be signed in to change notification settings - Fork 1
StringUtils first dive
Hempfest edited this page Aug 30, 2021
·
2 revisions
The preferred method of use for color translation would involve passing a base string value into the static abuser & translating automatically based upon server versioning.
- Static use, codename : "The Static Abuser";
StringUtils.use(String text).translate() // <-- Return's color translated string. Automatically implements HEX support.
For whatever reason you want to use the coloring utility manually (its wrapped internally within StringUtils no need to use this) here is how NOTE: The HEX ColorType will translate MC color codes as-well as HEX for formats ( #RRGGBB, &#RRGGBB, {#RRGGBB}, &x&r&r&g&g&b&b + gradients using <#RRGGBB></#RRGGBB> ).
- Instantiation :
new ColoredString(String text, ColorType type).toString(); // Valid color types : {ColoredString.ColorType.MC, ColoredString.ColorType.HEX}
- Instantiation :
new ColoredString(String text, ColorType.MC_COMPONENT).toComponent(); // Fills and colors a text component body.
new NewComponent().{textHoverable, textRunnable, textSuggestable} // Create an action written TextComponent automatically color translated. (Accepts HEX)
- Pre-1.15 :
new OldComponent().{textHoverable, textRunnable, textSuggestable} // Create an action written TextComponent automatically color translated. (Doesn't accept HEX)
- Universal :
TextLib.getInstance().{textHoverable, textRunnable, textSuggestable} // Create an action written TextComponent automatically color translated. (HEX support automatically applied)
StringUtils.use(String context).containsIgnoreCase(String target) // Check if a string contains the same character set regardless of case sensitivity.
Example
public class TestClass {
/**
* The regex to use to check contents of
*/
private final String REGEX;
public TestClass(String regex) {
this.REGEX = regex;
}
/**
* @return A new test instance picking a random key from a list.
*/
public static TestClass next() {
Random r = new Random();
List<String> TEMP = new LinkedList<>(Arrays.asList("Cakes", "Apples", "Cheese"));
return new TestClass(TEMP.get(r.nextInt(TEMP.size())));
}
/**
* @return Checks if the given random element matches a required target with case in-sensitivity
*/
public boolean isValid() {
return StringUtils.use(REGEX).containsIgnoreCase("ApPlEs");
}
}
Custom ID generation
public abstract class CustomID {
/**
* The maximum length without separation (-) the id can be
*/
private static final int LENGTH = 12;
private final String id;
protected CustomID(String id) {
this.id = id;
}
protected CustomID() {
this.id = StringUtils.use("SADBHJKU234875").generateID(LENGTH);
// Utilize the utility to generate a random id with the given pattern and length.
}
/**
* @return A default implementation of our custom id.
*/
public static CustomID randomID() {
return new DefaultImpl();
}
/**
* Try to convert a target string into an encapsulated custom id
*
* @param test the string to try to convert.
* @return A custom id object or null if the prerequisites don't match
*/
public static CustomID fromString(String test) {
if (test.replace("-", "").length() > LENGTH) {
throw new IllegalArgumentException("The id given does not match the length requirements for our id.");
}
if (!StringUtils.use(test.replace("-", "")).containsIgnoreCase(Pattern.quote("SADBHJKU234875"))) {
throw new IllegalArgumentException("The id given does not match the regex requirements for our id.");
}
return new DefaultImpl(test);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CustomID)) return false;
CustomID customID = (CustomID) o;
return id.equals(customID.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
/**
* @return The id we have generated converted into its string form.
*/
@Override
public String toString() {
if (!id.contains("-")) {
StringBuilder sb = new StringBuilder(id);
sb.insert(4, '-');
sb.insert(9, '-');
return sb.toString().endsWith("-") ? sb.substring(0, sb.length() - 1) : sb.toString();
}
return id;
}
public static final class DefaultImpl extends CustomID {
protected DefaultImpl(String test) {
super(test);
}
protected DefaultImpl() {
super();
}
}
}
MC version prior to 1.12 dont have namespaced key's??? Oh well, labyrinth's got your back. Completely compatible and used with our legacy safe persistent data container edit in Item.Edit
private final PersistentDataContainer container;
public Impl(PersistentDataContainer container) {
this.container = container;
}
@Override
public <T, Z> void set(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type, @NotNull Z value) {
final org.bukkit.NamespacedKey k = new org.bukkit.NamespacedKey(key.getNamespace(), key.getKey());
container.set(k, type, value);
}
@Override
public <T, Z> boolean has(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type) {
final org.bukkit.NamespacedKey k = new org.bukkit.NamespacedKey(key.getNamespace(), key.getKey());
return container.has(k, type);
}
@Override
public <T, Z> @Nullable Z get(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type) {
final org.bukkit.NamespacedKey k = new org.bukkit.NamespacedKey(key.getNamespace(), key.getKey());
return container.get(k, type);
}
-
- AFK Player Utility
- Command Builder
- Command Utility
- Complete GUI Builder
- Complete Head Database/Locator
- Cooldown Abstraction
- Custom Gradient Color Interface
- Custom ID Generation
- Directional Enumeration
- Economy Interface Wrapper
- Entity Creation Tools
- File Management
- Item Recipe Builder
- Item Modification Builder
- Legacy Safe NamespacedKey
- Listing Collections
- Material Matcher
- Message Formatter
- String Utility
- Tab Completion Builder
- Task Scheduling
- Template Creation
- TextComponent Builder
- Vault Permission Interface Wrapper