Skip to content

Commit

Permalink
fix incorrect elements in list (my issue) and duplicate elements (pre…
Browse files Browse the repository at this point in the history
…vious codebase issue)
  • Loading branch information
C0D3-M4513R committed Oct 22, 2023
1 parent b2de173 commit 64f4b5e
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.magmafoundation.magma.craftbukkit;

import net.minecraft.util.SortedArraySet;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.forgespi.language.IModFileInfo;
Expand All @@ -15,7 +16,7 @@
public final class ItemMetaTransformer {

private static final Logger LOGGER = LoggerFactory.getLogger(ItemMetaTransformer.class);
private static final List<String> NOT_TRANSFORMABLE = new ArrayList<>();
private static final SortedArraySet<String> NOT_TRANSFORMABLE = SortedArraySet.create();

//Performance heavy, please only call when needed
public static void loadPrefixes() {
Expand All @@ -37,7 +38,7 @@ public static void loadPrefixes() {
int dots = 0;
for (final ModFileScanData.ClassData clazzData : scanData.getClasses()) {
final String className = clazzData.clazz().getClassName();
if (className == null || className.isEmpty() || !IgnoreUtil.shouldCheck(className)) continue;
if (className == null || className.isEmpty() || numberDots(className) < 1 || !IgnoreUtil.shouldCheck(className)) continue;
if (prefix == null) {
prefix = className;
dots = numberDots(prefix);
Expand All @@ -48,14 +49,21 @@ public static void loadPrefixes() {
while (i < prefix.length() && i < className.length() && prefix.charAt(i) == className.charAt(i)) {
i++;
}
int less_dots = numberDots(prefix.substring(i));
//detect if we would over-shorten the path and exit before we do
if (dots - less_dots <= 1) break;
//`i` is fine to use as the index, because it is guaranteed to be smaller than the length of className and prefix
prefix = prefix.substring(0, i);
dots -= numberDots(prefix.substring(i)); //calculating the amount of dots left in the rest of the prefix.
dots -= less_dots; //calculating the amount of dots left in the rest of the prefix.
//this allows us to fast-fail out of the loop, since we only really care about if the common prefix has 2 or more dots
if (dots <= 1) break;
}
//we fast-fail out of the inner loop. now we can actually properly continue, since we are in the outer loop
if (dots <= 1) continue; //ignore top level packages
if (!isClassTransformable(prefix)) {
LOGGER.debug("Skipping " + prefix + " because it is already not transformable");
continue;
}
LOGGER.debug("Adding " + prefix + " to non-transformable list");
NOT_TRANSFORMABLE.add(prefix);
}
Expand All @@ -76,8 +84,11 @@ public static boolean isTransformable(ItemStack item) {
if (pkg.startsWith("net.minecraft.")) //ignore vanilla
return true;

return isClassTransformable(pkg);
}
public static boolean isClassTransformable(final String name){
for (String s : NOT_TRANSFORMABLE) {
if (pkg.startsWith(s))
if (name.startsWith(s))
return false;
}
return true;
Expand Down

0 comments on commit 64f4b5e

Please sign in to comment.