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

216 lines
9.4 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.ImmutableList;
2016-08-22 23:32:03 +02:00
import com.google.common.collect.ImmutableMap;
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;
import net.minecraft.entity.monster.SkeletonType;
import net.minecraft.entity.monster.ZombieType;
2015-08-02 00:36:33 +02:00
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.util.ResourceLocation;
2016-08-22 23:32:03 +02:00
public class PonyManager implements IResourceManagerReloadListener {
2016-08-22 23:32:03 +02:00
public static final String NAMESPACE = "minelittlepony";
2015-12-09 22:05:21 +01:00
public static final ResourceLocation ZOMBIE = new ResourceLocation(NAMESPACE, "textures/entity/zombie/zombie_pony.png");
2016-08-22 23:32:03 +02:00
public static final ResourceLocation ZOMBIE_VILLAGER = new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_villager_pony.png");
public static final Map<ZombieType, ResourceLocation> ZOMBIES = Maps.immutableEnumMap(ImmutableMap.<ZombieType, ResourceLocation> builder()
.put(ZombieType.NORMAL, ZOMBIE)
.put(ZombieType.HUSK, new ResourceLocation(NAMESPACE, "textures/entity/zombie/zombie_husk_pony.png"))
.put(ZombieType.VILLAGER_FARMER, new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_farmer_pony.png"))
.put(ZombieType.VILLAGER_LIBRARIAN, new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_librarian_pony.png"))
.put(ZombieType.VILLAGER_PRIEST, new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_priest_pony.png"))
.put(ZombieType.VILLAGER_SMITH, new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_smith_pony.png"))
.put(ZombieType.VILLAGER_BUTCHER, new ResourceLocation(NAMESPACE, "textures/entity/zombie_villager/zombie_butcher_pony.png"))
.build());
public static final List<ResourceLocation> VILLAGER_LIST = ImmutableList.<ResourceLocation> builder()
2015-12-09 22:05:21 +01:00
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/farmer_pony.png"))
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/librarian_pony.png"))
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/priest_pony.png"))
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/smith_pony.png"))
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/butcher_pony.png"))
.add(new ResourceLocation(NAMESPACE, "textures/entity/villager/villager_pony.png"))
.build();
2016-08-22 23:32:03 +02:00
public static final ResourceLocation PIGMAN = new ResourceLocation(NAMESPACE, "textures/entity/zombie/zombie_pigman_pony.png");
public static final ResourceLocation SKELETON = new ResourceLocation(NAMESPACE, "textures/entity/skeleton/skeleton_pony.png");
public static final Map<SkeletonType, ResourceLocation> SKELETONS = Maps.immutableEnumMap(ImmutableMap.<SkeletonType, ResourceLocation> builder()
.put(SkeletonType.NORMAL, SKELETON)
.put(SkeletonType.WITHER, new ResourceLocation(NAMESPACE, "textures/entity/skeleton/skeleton_wither_pony.png"))
.put(SkeletonType.STRAY, new ResourceLocation(NAMESPACE, "textures/entity/skeleton/stray_pony.png"))
2016-08-22 23:32:03 +02:00
.build());
public static final ResourceLocation STEVE = new ResourceLocation(NAMESPACE, "textures/entity/steve_pony.png");
public static final ResourceLocation ALEX = new ResourceLocation(NAMESPACE, "textures/entity/alex_pony.png");
private static final ResourceLocation BGPONIES_JSON = new ResourceLocation(NAMESPACE, "textures/entity/pony/bgponies.json");
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;
}
public Pony getPonyFromResourceRegistry(EntityVillager entity) {
int profession = entity.getProfession();
ResourceLocation villagerResourceLocation;
try {
2016-08-22 23:32:03 +02:00
villagerResourceLocation = VILLAGER_LIST.get(profession);
2015-08-02 00:36:33 +02:00
} catch (IndexOutOfBoundsException var5) {
2016-08-22 23:32:03 +02:00
villagerResourceLocation = VILLAGER_LIST.get(5);
2015-08-02 00:36:33 +02:00
}
Pony myLittlePony = this.getPonyFromResourceRegistry(villagerResourceLocation);
// myLittlePony.setVillager(profession);
2015-08-02 00:36:33 +02:00
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-01-26 09:16:11 +01:00
public static ResourceLocation getDefaultSkin(UUID uuid) {
return (uuid.hashCode() & 1) == 0 ? STEVE : ALEX;
}
2015-08-02 00:36:33 +02:00
public 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) {
return new ResourceLocation(NAMESPACE, String.format("textures/entity/pony/%s.png", input));
}
public List<ResourceLocation> getPonies() {
return Lists.transform(ponies, this);
}
}
2015-08-02 00:36:33 +02:00
}