Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases - load temp aliases, when aliases are missing #4034

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
Expand Down Expand Up @@ -396,6 +398,22 @@ public static void load() {
Skript.exception(e);
}
}

/**
* Temporarily create an alias for a material which may not have an alias yet.
*/
private static void loadMissingAliases() {
if (!Skript.methodExists(Material.class, "getKey"))
return;
for (Material material : Material.values()) {
if (!provider.hasAliasForMaterial(material)) {
NamespacedKey key = material.getKey();
String name = key.getKey().replace("_", " ");
parser.loadAlias(name + "¦s", key.toString());
Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key.toString());
}
}
}

private static void loadInternal() throws IOException {
Path dataFolder = Skript.getInstance().getDataFolder().toPath();
Expand All @@ -419,6 +437,7 @@ private static void loadInternal() throws IOException {
Path aliasesPath = zipFs.getPath("/", "aliases-english");
assert aliasesPath != null;
loadDirectory(aliasesPath);
loadMissingAliases();
}
} catch (URISyntaxException e) {
assert false;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/ch/njol/skript/aliases/AliasesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class AliasesProvider {
* All aliases that are currently loaded by this provider.
*/
private final Map<String, ItemType> aliases;

/**
* All materials that are currently loaded by this provider.
*/
private final List<Material> materials;

/**
* Tags are in JSON format. We may need GSON when merging tags
Expand Down Expand Up @@ -166,6 +171,7 @@ public AliasesProvider(int expectedCount, @Nullable AliasesProvider parent) {
this.aliases = new HashMap<>(expectedCount);
this.variations = new HashMap<>(expectedCount / 20);
this.aliasesMap = new AliasesMap();
this.materials = new ArrayList<>();

this.gson = new Gson();
}
Expand Down Expand Up @@ -259,6 +265,8 @@ public void addAlias(AliasName name, String id, @Nullable Map<String, Object> ta
if (material == null) { // If server doesn't recognize id, do not proceed
throw new InvalidMinecraftIdException(id);
}
if (!materials.contains(material))
materials.add(material);

// Hacky: get related entity from block states
String entityName = blockStates.remove("relatedEntity");
Expand Down Expand Up @@ -378,4 +386,13 @@ public int getAliasCount() {
return aliases.size();
}

/**
* Check if this provider has an alias for the given material.
* @param material Material to check alias for
* @return True if this material has an alias
*/
public boolean hasAliasForMaterial(Material material) {
return materials.contains(material);
}

}