mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-30 16:28:00 +01:00
Minor cleanup
This commit is contained in:
parent
bd49bdd52e
commit
96d2e7b1f9
4 changed files with 48 additions and 37 deletions
|
@ -6,12 +6,10 @@ import java.util.Objects;
|
|||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.minelittlepony.common.util.settings.ToStringAdapter;
|
||||
import com.minelittlepony.unicopia.util.PosHelper;
|
||||
import com.minelittlepony.unicopia.util.Resources;
|
||||
import com.minelittlepony.unicopia.util.Weighted;
|
||||
|
||||
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
||||
|
@ -30,9 +28,6 @@ import net.minecraft.world.World;
|
|||
|
||||
public class TreeTypeLoader extends JsonDataLoader implements IdentifiableResourceReloadListener {
|
||||
private static final Identifier ID = new Identifier("unicopia", "data/tree_type");
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(Identifier.class, new ToStringAdapter<>(Identifier::new))
|
||||
.create();
|
||||
|
||||
public static final TreeTypeLoader INSTANCE = new TreeTypeLoader();
|
||||
|
||||
|
@ -42,7 +37,7 @@ public class TreeTypeLoader extends JsonDataLoader implements IdentifiableResour
|
|||
private final TreeType any2x = createDynamic(true);
|
||||
|
||||
TreeTypeLoader() {
|
||||
super(GSON, "tree_types");
|
||||
super(Resources.GSON, "tree_types");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -109,7 +104,7 @@ public class TreeTypeLoader extends JsonDataLoader implements IdentifiableResour
|
|||
|
||||
for (Map.Entry<Identifier, JsonElement> entry : resources.entrySet()) {
|
||||
try {
|
||||
TreeTypeDef typeDef = GSON.fromJson(entry.getValue(), TreeTypeDef.class);
|
||||
TreeTypeDef typeDef = Resources.GSON.fromJson(entry.getValue(), TreeTypeDef.class);
|
||||
|
||||
if (typeDef != null) {
|
||||
entries.add(new TreeTypeImpl(
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.minelittlepony.unicopia.item.enchantment;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -13,14 +11,12 @@ import java.util.stream.Stream;
|
|||
|
||||
import org.apache.commons.lang3.reflect.TypeUtils;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.minelittlepony.unicopia.Unicopia;
|
||||
import com.minelittlepony.unicopia.ability.data.tree.TreeTypeLoader;
|
||||
import com.minelittlepony.unicopia.entity.Living;
|
||||
import com.minelittlepony.unicopia.util.Resources;
|
||||
|
||||
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
||||
import net.minecraft.entity.EquipmentSlot;
|
||||
import net.minecraft.resource.Resource;
|
||||
import net.minecraft.resource.ResourceManager;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
|
@ -76,8 +72,8 @@ public class PoisonedJokeEnchantment extends SimpleEnchantment implements Identi
|
|||
clientProfiler.startTick();
|
||||
clientProfiler.push("Loading poisoned joke sound options");
|
||||
|
||||
sounds = getResources(manager, FILE)
|
||||
.flatMap(this::loadFile)
|
||||
sounds = Resources.getResources(manager, FILE)
|
||||
.flatMap(r -> Resources.loadFile(r, TYPE, "Failed to load sounds file at "))
|
||||
.distinct()
|
||||
.flatMap(this::findSound)
|
||||
.collect(Collectors.toList());
|
||||
|
@ -87,29 +83,10 @@ public class PoisonedJokeEnchantment extends SimpleEnchantment implements Identi
|
|||
}, clientExecutor);
|
||||
}
|
||||
|
||||
private Stream<Resource> getResources(ResourceManager manager, Identifier id) {
|
||||
try {
|
||||
return manager.getAllResources(id).stream();
|
||||
} catch (IOException ignored) { }
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
private Stream<Identifier> loadFile(Resource res) throws JsonParseException {
|
||||
try (Resource resource = res) {
|
||||
return (TreeTypeLoader.GSON.<List<Identifier>>fromJson(new InputStreamReader(resource.getInputStream()), TYPE)).stream();
|
||||
} catch (JsonParseException e) {
|
||||
Unicopia.LOGGER.warn("Failed to load sounds file at " + res.getResourcePackName(), e);
|
||||
} catch (IOException ignored) {}
|
||||
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
private Stream<SoundEvent> findSound(Identifier id) {
|
||||
SoundEvent value = Registry.SOUND_EVENT.getOrEmpty(id).orElse(null);
|
||||
if (value == null) {
|
||||
return Registry.SOUND_EVENT.getOrEmpty(id).map(Stream::of).orElseGet(() -> {
|
||||
Unicopia.LOGGER.warn("Could not find sound with id {}", id);
|
||||
return Stream.empty();
|
||||
}
|
||||
return Stream.of(value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import com.mojang.serialization.Lifecycle;
|
|||
|
||||
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.DefaultedRegistry;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
import net.minecraft.util.registry.SimpleRegistry;
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.minelittlepony.unicopia.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.minelittlepony.common.util.settings.ToStringAdapter;
|
||||
import com.minelittlepony.unicopia.Unicopia;
|
||||
|
||||
import net.minecraft.resource.Resource;
|
||||
import net.minecraft.resource.ResourceManager;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public interface Resources {
|
||||
Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(Identifier.class, new ToStringAdapter<>(Identifier::new))
|
||||
.create();
|
||||
|
||||
static Stream<Resource> getResources(ResourceManager manager, Identifier id) {
|
||||
try {
|
||||
return manager.getAllResources(id).stream();
|
||||
} catch (IOException ignored) { }
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
static Stream<Identifier> loadFile(Resource res, Type type, String msg) throws JsonParseException {
|
||||
try (Resource resource = res) {
|
||||
return (GSON.<List<Identifier>>fromJson(new InputStreamReader(resource.getInputStream()), type)).stream();
|
||||
} catch (JsonParseException e) {
|
||||
Unicopia.LOGGER.warn(msg + res.getResourcePackName(), e);
|
||||
} catch (IOException ignored) {}
|
||||
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue