MineLittlePony/src/main/java/com/brohoof/minelittlepony/PonyManager.java

165 lines
6 KiB
Java
Raw Normal View History

2015-11-17 06:09:04 +01:00
package com.brohoof.minelittlepony;
2015-08-02 00:36:33 +02:00
2016-08-22 23:32:03 +02:00
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
2015-08-02 00:36:33 +02:00
import java.util.List;
import java.util.Map;
2016-01-26 09:16:11 +01:00
import java.util.UUID;
2015-08-02 00:36:33 +02:00
2016-08-22 23:32:03 +02:00
import org.apache.commons.compress.utils.IOUtils;
2015-11-17 06:09:04 +01:00
import com.brohoof.minelittlepony.model.PMAPI;
import com.brohoof.minelittlepony.util.MineLPLogger;
2016-08-22 23:32:03 +02:00
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
2016-08-22 23:32:03 +02:00
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
2015-08-02 00:36:33 +02:00
import net.minecraft.client.entity.AbstractClientPlayer;
2016-08-22 23:32:03 +02:00
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
2015-08-02 00:36:33 +02:00
import net.minecraft.util.ResourceLocation;
2016-08-22 23:32:03 +02:00
public class PonyManager implements IResourceManagerReloadListener {
2016-11-17 05:45:04 +01:00
public static final ResourceLocation STEVE = new ResourceLocation("minelittlepony", "textures/entity/steve_pony.png");
public static final ResourceLocation ALEX = new ResourceLocation("minelittlepony", "textures/entity/alex_pony.png");
private static final ResourceLocation BGPONIES_JSON = new ResourceLocation("minelittlepony", "textures/entity/pony/bgponies.json");
2016-08-22 23:32:03 +02:00
private List<ResourceLocation> backgroundPonyList = Lists.newArrayList();
private PonyConfig config;
2016-08-22 23:32:03 +02:00
private Map<ResourceLocation, Pony> poniesCache = Maps.newHashMap();
private Map<ResourceLocation, Pony> backgroudPoniesCache = Maps.newHashMap();
public PonyManager(PonyConfig config) {
this.config = config;
2015-08-02 00:36:33 +02:00
initmodels();
}
public void initmodels() {
MineLPLogger.info("Initializing models...");
PMAPI.init();
MineLPLogger.info("Done initializing models.");
}
private Pony getPonyFromResourceRegistry(ResourceLocation skinResourceLocation, AbstractClientPlayer player) {
Pony myLittlePony;
2016-08-22 23:32:03 +02:00
if (!this.poniesCache.containsKey(skinResourceLocation)) {
2015-08-02 00:36:33 +02:00
if (player != null) {
myLittlePony = new Pony(player);
} else {
myLittlePony = new Pony(skinResourceLocation);
}
2016-08-22 23:32:03 +02:00
this.poniesCache.put(skinResourceLocation, myLittlePony);
2015-08-02 00:36:33 +02:00
} else {
2016-08-22 23:32:03 +02:00
myLittlePony = this.poniesCache.get(skinResourceLocation);
2015-08-02 00:36:33 +02:00
}
return myLittlePony;
}
public Pony getPonyFromResourceRegistry(ResourceLocation skinResourceLocation) {
return this.getPonyFromResourceRegistry(skinResourceLocation, (AbstractClientPlayer) null);
}
public Pony getPonyFromResourceRegistry(AbstractClientPlayer player) {
Pony myLittlePony = this.getPonyFromResourceRegistry(player.getLocationSkin(), player);
2016-01-26 09:16:11 +01:00
if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.metadata.getRace() == null) {
2015-08-02 00:36:33 +02:00
myLittlePony = this.getPonyFromBackgroundResourceRegistry(player);
}
return myLittlePony;
}
2016-01-26 09:16:11 +01:00
public ResourceLocation getBackgroundPonyResource(UUID id) {
if (getNumberOfPonies() > 0) {
2016-01-26 09:16:11 +01:00
int backgroundIndex = id.hashCode() % this.getNumberOfPonies();
2015-08-02 00:36:33 +02:00
if (backgroundIndex < 0) {
backgroundIndex += this.getNumberOfPonies();
}
return backgroundPonyList.get(backgroundIndex);
2015-08-02 00:36:33 +02:00
}
return STEVE;
2015-08-02 00:36:33 +02:00
}
public Pony getPonyFromBackgroundResourceRegistry(AbstractClientPlayer player) {
ResourceLocation textureResourceLocation;
if (player.isUser()) {
2016-01-26 09:16:11 +01:00
textureResourceLocation = getDefaultSkin(player.getUniqueID());
2015-08-02 00:36:33 +02:00
} else {
2016-01-26 09:16:11 +01:00
textureResourceLocation = this.getBackgroundPonyResource(player.getUniqueID());
2015-08-02 00:36:33 +02:00
}
Pony myLittlePony;
2016-08-22 23:32:03 +02:00
if (!this.backgroudPoniesCache.containsKey(textureResourceLocation)) {
2015-08-02 00:36:33 +02:00
myLittlePony = new Pony(textureResourceLocation);
2016-08-22 23:32:03 +02:00
this.backgroudPoniesCache.put(textureResourceLocation, myLittlePony);
2015-08-02 00:36:33 +02:00
} else {
2016-08-22 23:32:03 +02:00
myLittlePony = this.backgroudPoniesCache.get(textureResourceLocation);
2015-08-02 00:36:33 +02:00
}
return myLittlePony;
}
2016-08-22 23:32:03 +02:00
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
// TODO Auto-generated method stub
this.backgroudPoniesCache.clear();
this.backgroundPonyList.clear();
try {
for (IResource res : resourceManager.getAllResources(BGPONIES_JSON)) {
try {
BackgroundPonies ponies = getBackgroundPonies(res.getInputStream());
if (ponies.override) {
this.backgroundPonyList.clear();
}
this.backgroundPonyList.addAll(ponies.getPonies());
} catch (JsonParseException e) {
MineLPLogger.error(e, "Invalid bgponies.json in {}", res.getResourcePackName());
}
}
} catch (IOException e) {
// this isn't the exception you're looking for.
}
MineLPLogger.info("Detected %d background ponies installed.", getNumberOfPonies());
}
private BackgroundPonies getBackgroundPonies(InputStream stream) {
try {
return new Gson().fromJson(new InputStreamReader(stream), BackgroundPonies.class);
} finally {
IOUtils.closeQuietly(stream);
}
}
2016-11-17 05:45:04 +01:00
private ResourceLocation getDefaultSkin(UUID uuid) {
2016-01-26 09:16:11 +01:00
return (uuid.hashCode() & 1) == 0 ? STEVE : ALEX;
}
2015-08-02 00:36:33 +02:00
2016-11-17 05:45:04 +01:00
private int getNumberOfPonies() {
return backgroundPonyList.size();
2015-08-02 00:36:33 +02:00
}
2016-08-22 23:32:03 +02:00
private static class BackgroundPonies implements Function<String, ResourceLocation> {
public boolean override;
private List<String> ponies;
@Override
public ResourceLocation apply(String input) {
2016-11-17 05:45:04 +01:00
return new ResourceLocation("minelittlepony", String.format("textures/entity/pony/%s.png", input));
2016-08-22 23:32:03 +02:00
}
public List<ResourceLocation> getPonies() {
return Lists.transform(ponies, this);
}
}
2015-08-02 00:36:33 +02:00
}