Skip to content

Commit

Permalink
Fixes asset managers stackoverflowing
Browse files Browse the repository at this point in the history
Improves ItemStackModder
  • Loading branch information
anjoismysign committed Oct 7, 2023
1 parent 35a91d0 commit 53a90de
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 118 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.696.8</version>
<version>1.696.9</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.696.8</version>
<version>1.696.9</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.696.8</version>
<version>1.696.10</version>
<packaging>pom</packaging>

<properties>
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/us/mytheria/bloblib/api/BlobLibMessageAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import us.mytheria.bloblib.managers.MessageManager;

import java.io.File;
import java.util.Objects;

public class BlobLibMessageAPI {
private static BlobLibMessageAPI instance;
Expand Down Expand Up @@ -54,24 +55,46 @@ public String getMessagesFilePath() {
}

/**
* Gets the message from the default locale.
*
* @param key The key of the message
* @return The message
*/
@Nullable
public ReferenceBlobMessage getMessage(String key) {
public ReferenceBlobMessage getMessage(@NotNull String key) {
Objects.requireNonNull(key);
return getMessageManager().getMessage(key);
}

/**
* Gets the message from the locale.
* If message is not available in the locale, returns the default message.
*
* @param key The key of the message
* @param locale The locale of the message
* @return The message
*/
@Nullable
public ReferenceBlobMessage getMessage(String key, String locale) {
public ReferenceBlobMessage getMessage(@NotNull String key, @NotNull String locale) {
Objects.requireNonNull(key);
Objects.requireNonNull(locale);
return getMessageManager().getMessage(key, locale);
}

/**
* Gets the locale of the player and returns the message.
* If message is not available in the locale, returns the default message.
*
* @param key The key of the message
* @param player The player to get the locale from
* @return The message
*/
@Nullable
public ReferenceBlobMessage getMessage(@NotNull String key, @NotNull Player player) {
Objects.requireNonNull(player);
return getMessageManager().getMessage(key, player.getLocale());
}

/**
* @param key The key of the message
* @param locale The locale of the message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package us.mytheria.bloblib.entities;

import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.api.BlobLibMessageAPI;
import us.mytheria.bloblib.entities.message.*;
import us.mytheria.bloblib.utilities.TextColor;
Expand All @@ -15,15 +15,18 @@
* Recommended method is parse(ConfigurationSection section)
*/
public class BlobMessageReader {
public static SerialBlobMessage read(ConfigurationSection section) {
return read(section, "en_us");
}

/**
* Will read a BlobMessage from a ConfigurationSection
*
* @param section The section to read from
* @return The BlobMessage
*/
public static SerialBlobMessage read(ConfigurationSection section) {
public static SerialBlobMessage read(ConfigurationSection section, @NotNull String locale) {
String type = section.getString("Type");
@Nullable String locale = section.getString("Locale", null);
Optional<BlobSound> sound = section.contains("BlobSound") ?
BlobSoundReader.parse(section) : Optional.empty();
switch (type) {
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/us/mytheria/bloblib/entities/Rep.java

This file was deleted.

60 changes: 22 additions & 38 deletions src/main/java/us/mytheria/bloblib/itemstack/ItemStackModder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import us.mytheria.bloblib.entities.Rep;
import us.mytheria.bloblib.utilities.TextColor;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

public final class ItemStackModder {
private final ItemStack itemStack;
Expand Down Expand Up @@ -73,28 +73,38 @@ public ItemStackModder unflag(Collection<ItemFlag> flags) {
return unflag(flags.toArray(new ItemFlag[0]));
}

public ItemStackModder displayName(String name, char colorChar) {
return itemMeta(itemMeta -> itemMeta.setDisplayName(TextColor.CUSTOM_PARSE(colorChar, name)));
}

public ItemStackModder displayName(String name) {
return itemMeta(itemMeta -> itemMeta.setDisplayName(name));
return displayName(name, '&');
}

public ItemStackModder lore(String line, char colorChar) {
return lore(List.of(line), colorChar);
}

public ItemStackModder lore(String line) {
return lore(List.of(line));
return lore(line, '&');
}

public ItemStackModder lore(String... lore) {
return itemMeta(itemMeta -> itemMeta.setLore(List.of(lore)));
public ItemStackModder lore(char colorChar, String... lore) {
List<String> list = Stream.of(lore).map(line -> TextColor.CUSTOM_PARSE(colorChar, line)).toList();
return itemMeta(itemMeta -> itemMeta.setLore(list));
}

public ItemStackModder lore(List<String> lore) {
return itemMeta(itemMeta -> itemMeta.setLore(lore));
public ItemStackModder lore(String... lore) {
return lore('&', lore);
}

public ItemStackModder lore(Function<List<String>, List<String>> lore) {
return itemMeta(itemMeta -> itemMeta.setLore(lore.apply(itemMeta.getLore())));
public ItemStackModder lore(List<String> lore, char colorChar) {
List<String> list = lore.stream().map(line -> TextColor.CUSTOM_PARSE(colorChar, line)).toList();
return itemMeta(itemMeta -> itemMeta.setLore(list));
}

public ItemStackModder lore(List<String> lore, Rep... reps) {
return lore(Rep.lace(lore, reps));
public ItemStackModder lore(List<String> lore) {
return lore(lore, '&');
}

private List<String> getLore() {
Expand All @@ -103,18 +113,6 @@ private List<String> getLore() {
return meta.getLore();
}

public ItemStackModder lore(Rep... reps) {
List<String> lore = getLore();
if (lore == null) return this;
return lore(Rep.lace(lore, reps));
}

public ItemStackModder lore(Collection<Rep> reps) {
List<String> lore = getLore();
if (lore == null) return this;
return lore(Rep.lace(lore, reps));
}

/**
* Clears the lore of the item.
*/
Expand Down Expand Up @@ -252,18 +250,4 @@ public ItemStackModder replace(String regex, String replacement) {
}
});
}

public ItemStackModder replace(Rep... reps) {
for (Rep rep : reps) {
replace(rep.getCheck(), rep.getReplace());
}
return this;
}

public ItemStackModder replace(Collection<Rep> reps) {
for (Rep rep : reps) {
replace(rep.getCheck(), rep.getReplace());
}
return this;
}
}
42 changes: 23 additions & 19 deletions src/main/java/us/mytheria/bloblib/managers/MessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

public class MessageManager {
private final BlobLib main;
private HashMap<String, SerialBlobMessage> messages;
private HashMap<String, Set<String>> pluginMessages;
private HashMap<String, Integer> duplicates;
private HashMap<String, Map<String, SerialBlobMessage>> locales;
Expand All @@ -28,7 +27,6 @@ public void reload() {
}

public void load() {
messages = new HashMap<>();
locales = new HashMap<>();
pluginMessages = new HashMap<>();
duplicates = new HashMap<>();
Expand Down Expand Up @@ -57,7 +55,7 @@ public void unload(BlobPlugin plugin) {
Iterator<String> iterator = messages.iterator();
while (iterator.hasNext()) {
String inventoryName = iterator.next();
this.messages.remove(inventoryName);
this.locales.forEach((locale, map) -> map.remove(inventoryName));
iterator.remove();
}
pluginMessages.remove(pluginName);
Expand Down Expand Up @@ -91,42 +89,42 @@ private void loadFiles(File path) {

private void loadYamlConfiguration(File file) {
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
String locale = yamlConfiguration.getString("Locale", "en_us");
yamlConfiguration.getKeys(true).forEach(reference -> {
if (!yamlConfiguration.isConfigurationSection(reference))
return;
ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference);
if (!section.contains("Type") && !section.isString("Type"))
return;
if (messages.containsKey(reference)) {
addDuplicate(reference);
return;
}
SerialBlobMessage message = BlobMessageReader.read(section);
messages.put(reference, message);
SerialBlobMessage message = BlobMessageReader.read(section, locale);
addOrCreateLocale(message, reference);
});
}

private void loadYamlConfiguration(File file, BlobPlugin plugin) {
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
String locale = yamlConfiguration.getString("Locale", "en_us");
yamlConfiguration.getKeys(true).forEach(reference -> {
if (!yamlConfiguration.isConfigurationSection(reference))
return;
ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference);
if (!section.contains("Type") && !section.isString("Type"))
return;
if (messages.containsKey(reference)) {
addDuplicate(reference);
if (!addOrCreateLocale(BlobMessageReader.read(section, locale), reference))
return;
}
messages.put(reference, BlobMessageReader.read(section));
pluginMessages.get(plugin.getName()).add(reference);
});
}

private void addOrCreateLocale(SerialBlobMessage message, String reference) {
private boolean addOrCreateLocale(SerialBlobMessage message, String reference) {
String locale = message.getLocale();
locales.computeIfAbsent(locale, k -> new HashMap<>()).put(reference, message);
Map<String, SerialBlobMessage> localeMap = locales.computeIfAbsent(locale, k -> new HashMap<>());
if (localeMap.containsKey(reference)) {
addDuplicate(reference);
return false;
}
localeMap.put(reference, message);
return true;
}

/**
Expand Down Expand Up @@ -157,12 +155,12 @@ private void addDuplicate(String key) {
}

public void noPermission(Player player) {
messages.get("System.No-Permission").handle(player);
locales.get("en_us").get("System.No-Permission").handle(player);
}

@Nullable
public ReferenceBlobMessage getMessage(String key) {
return new ReferenceBlobMessage(messages.get(key), key);
return new ReferenceBlobMessage(locales.get("en_us").get(key), key);
}

@Nullable
Expand All @@ -174,14 +172,20 @@ public ReferenceBlobMessage getMessage(String key, String locale) {
}

public void playAndSend(Player player, String key) {
SerialBlobMessage message = messages.get(key);
ReferenceBlobMessage message = getMessage(key, player.getLocale());
if (message == null)
throw new NullPointerException("Message '" + key + "' does not exist!");
message.handle(player);
}

/**
* @param player The player to send the message to
* @param key The key of the message
* @deprecated Use {@link #playAndSend(Player, String)} instead
*/
@Deprecated
public void send(Player player, String key) {
SerialBlobMessage message = messages.get(key);
ReferenceBlobMessage message = getMessage(key, player.getLocale());
if (message == null)
throw new NullPointerException("Message '" + key + "' does not exist!");
message.send(player);
Expand Down

0 comments on commit 53a90de

Please sign in to comment.