Do not hardcode tag names and remove not needed README comment (#32)

This commit is contained in:
Alex
2024-10-27 17:05:35 +01:00
committed by GitHub
parent 5e90ad5869
commit b583525f2c
2 changed files with 7 additions and 21 deletions
@@ -7,6 +7,7 @@ import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.registry.Registry; import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.EnchantmentTags;
import net.minecraft.registry.tag.TagKey; import net.minecraft.registry.tag.TagKey;
import net.minecraft.registry.entry.RegistryEntryList; import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
@@ -14,7 +15,6 @@ import net.minecraft.registry.entry.RegistryEntry;
import java.util.List; import java.util.List;
import java.util.Set;
public class EnchantmentsDataGenerator implements IDataGenerator { public class EnchantmentsDataGenerator implements IDataGenerator {
public static String getEnchantmentTargetName(RegistryEntryList<Item> target) { public static String getEnchantmentTargetName(RegistryEntryList<Item> target) {
@@ -22,16 +22,6 @@ public class EnchantmentsDataGenerator implements IDataGenerator {
return tagKey.id().getPath().split("/")[1]; return tagKey.id().getPath().split("/")[1];
} }
private static boolean isEnchantmentInTag(Enchantment enchantment, String tag) {
return DGU.getWorld()
.getRegistryManager()
.get(RegistryKeys.ENCHANTMENT)
.streamTagsAndEntries()
.filter(tagKeyNamedPair -> tagKeyNamedPair.getFirst().id().equals(Identifier.of(tag)))
.flatMap(tagKeyNamedPair -> tagKeyNamedPair.getSecond().stream())
.anyMatch(enchantmentRegistryEntry -> enchantmentRegistryEntry.value() == enchantment);
}
//Equation enchantment costs follow is a * level + b, so we can easily retrieve a and b by passing zero level //Equation enchantment costs follow is a * level + b, so we can easily retrieve a and b by passing zero level
private static JsonObject generateEnchantmentMinPowerCoefficients(Enchantment enchantment) { private static JsonObject generateEnchantmentMinPowerCoefficients(Enchantment enchantment) {
int b = enchantment.getMinPower(0); int b = enchantment.getMinPower(0);
@@ -55,6 +45,7 @@ public class EnchantmentsDataGenerator implements IDataGenerator {
public static JsonObject generateEnchantment(Registry<Enchantment> registry, Enchantment enchantment) { public static JsonObject generateEnchantment(Registry<Enchantment> registry, Enchantment enchantment) {
JsonObject enchantmentDesc = new JsonObject(); JsonObject enchantmentDesc = new JsonObject();
RegistryEntry<Enchantment> enchantmentEntry = registry.getEntry(enchantment);
Identifier registryKey = registry.getKey(enchantment).orElseThrow().getValue(); Identifier registryKey = registry.getKey(enchantment).orElseThrow().getValue();
enchantmentDesc.addProperty("id", registry.getRawId(enchantment)); enchantmentDesc.addProperty("id", registry.getRawId(enchantment));
@@ -67,16 +58,12 @@ public class EnchantmentsDataGenerator implements IDataGenerator {
enchantmentDesc.add("minCost", generateEnchantmentMinPowerCoefficients(enchantment)); enchantmentDesc.add("minCost", generateEnchantmentMinPowerCoefficients(enchantment));
enchantmentDesc.add("maxCost", generateEnchantmentMaxPowerCoefficients(enchantment)); enchantmentDesc.add("maxCost", generateEnchantmentMaxPowerCoefficients(enchantment));
enchantmentDesc.addProperty("treasureOnly", isEnchantmentInTag(enchantment, "treasure")); enchantmentDesc.addProperty("treasureOnly", enchantmentEntry.isIn(EnchantmentTags.TREASURE));
enchantmentDesc.addProperty("curse", isEnchantmentInTag(enchantment, "curse")); enchantmentDesc.addProperty("curse", enchantmentEntry.isIn(EnchantmentTags.CURSE));
List<Enchantment> incompatibleEnchantments = registry.stream() List<Enchantment> incompatibleEnchantments = registry.stream()
.filter(other -> { .filter(other -> !Enchantment.canBeCombined(enchantmentEntry, registry.getEntry(other)))
RegistryEntry<Enchantment> enchantmentEntry = registry.getEntry(enchantment);
RegistryEntry<Enchantment> otherEntry = registry.getEntry(other);
return !Enchantment.canBeCombined(enchantmentEntry, otherEntry);
})
.filter(other -> other != enchantment) .filter(other -> other != enchantment)
.toList(); .toList();
@@ -88,8 +75,8 @@ public class EnchantmentsDataGenerator implements IDataGenerator {
enchantmentDesc.add("exclude", excludes); enchantmentDesc.add("exclude", excludes);
enchantmentDesc.addProperty("category", getEnchantmentTargetName(enchantment.getApplicableItems())); enchantmentDesc.addProperty("category", getEnchantmentTargetName(enchantment.getApplicableItems()));
enchantmentDesc.addProperty("weight", enchantment.getWeight()); enchantmentDesc.addProperty("weight", enchantment.getWeight());
enchantmentDesc.addProperty("tradeable", isEnchantmentInTag(enchantment, "tradeable")); enchantmentDesc.addProperty("tradeable", enchantmentEntry.isIn(EnchantmentTags.TRADEABLE));
enchantmentDesc.addProperty("discoverable", isEnchantmentInTag(enchantment, "on_random_loot")); enchantmentDesc.addProperty("discoverable", enchantmentEntry.isIn(EnchantmentTags.ON_RANDOM_LOOT));
return enchantmentDesc; return enchantmentDesc;
} }
-1
View File
@@ -31,7 +31,6 @@ Then also add it to the `.github/workflows/build.yml` file.
Then copy the code of the most recently released version into the new module directory. Then copy the code of the most recently released version into the new module directory.
Then you need to change the values in the `build.gradle` file. Then you need to change the values in the `build.gradle` file.
At last, you need to fix all code issues that are caused by the new version. At last, you need to fix all code issues that are caused by the new version.
if there are any new curse, treasure, or non-tradeable, or non-discoverable enchantments in the new version, you need to add them manually to the `EnchantmentsDataGenerator` class.
For that, use an IDE like IntelliJ IDEA to fix the issues. For that, use an IDE like IntelliJ IDEA to fix the issues.
Once everything compiles, you can commit the changes, push them to your fork and create a pull request. Once everything compiles, you can commit the changes, push them to your fork and create a pull request.
Once your PR was accepted and merged, the new version will be available in the next release. Once your PR was accepted and merged, the new version will be available in the next release.