mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Make Loot Table data generators more reusable for modders.
This commit is contained in:
1 parent
acaa470dea
commit
3484200f80
4 files changed
+242
-1
No files matched your search
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DirectoryCache;
|
||||
import net.minecraft.data.IDataProvider;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.potion.Effect;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
public abstract class LanguageProvider implements IDataProvider {
|
||||
private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
|
||||
private final Map<String, String> data = new TreeMap<>();
|
||||
private final DataGenerator gen;
|
||||
private final String modid;
|
||||
private final String locale;
|
||||
|
||||
public LanguageProvider(DataGenerator gen, String modid, String locale) {
|
||||
this.gen = gen;
|
||||
this.modid = modid;
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
protected abstract void addTranslations();
|
||||
|
||||
@Override
|
||||
public void act(DirectoryCache cache) throws IOException {
|
||||
addTranslations();
|
||||
if (!data.isEmpty())
|
||||
save(cache, data, this.gen.getOutputFolder().resolve("assets/" + modid + "/lang/" + locale + ".json"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Languages: " + locale;
|
||||
}
|
||||
|
||||
private void save(DirectoryCache cache, Object object, Path target) throws IOException {
|
||||
String data = GSON.toJson(object);
|
||||
String hash = IDataProvider.HASH_FUNCTION.hashUnencodedChars(data).toString();
|
||||
if (!Objects.equals(cache.getPreviousHash(target), hash) || !Files.exists(target)) {
|
||||
Files.createDirectories(target.getParent());
|
||||
|
||||
try (BufferedWriter bufferedwriter = Files.newBufferedWriter(target)) {
|
||||
bufferedwriter.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
cache.func_208316_a(target, hash);
|
||||
}
|
||||
|
||||
protected void addBlock(Supplier<Block> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(Block key, String name) {
|
||||
add(key.getTranslationKey(), name);
|
||||
}
|
||||
|
||||
protected void addItem(Supplier<Item> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(Item key, String name) {
|
||||
add(key.getTranslationKey(), name);
|
||||
}
|
||||
|
||||
protected void addItemStack(Supplier<ItemStack> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(ItemStack key, String name) {
|
||||
add(key.getTranslationKey(), name);
|
||||
}
|
||||
|
||||
protected void addEnchantment(Supplier<Enchantment> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(Enchantment key, String name) {
|
||||
add(key.getName(), name);
|
||||
}
|
||||
|
||||
protected void addBiome(Supplier<Biome> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(Biome key, String name) {
|
||||
add(key.getTranslationKey(), name);
|
||||
}
|
||||
|
||||
protected void addEffect(Supplier<Effect> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(Effect key, String name) {
|
||||
add(key.getName(), name);
|
||||
}
|
||||
|
||||
protected void addEntityType(Supplier<EntityType<?>> key, String name) {
|
||||
add(key.get(), name);
|
||||
}
|
||||
|
||||
protected void add(EntityType<?> key, String name) {
|
||||
add(key.getTranslationKey(), name);
|
||||
}
|
||||
|
||||
protected void add(String key, String value) {
|
||||
if (data.put(key, value) != null)
|
||||
throw new IllegalStateException("Duplicate translation key " + key);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user