2016-11-17 05:45:04 +01:00
|
|
|
package com.minelittlepony;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2015-12-09 04:14:42 +01:00
|
|
|
import com.google.common.collect.Maps;
|
2016-08-22 23:32:03 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonParseException;
|
2016-11-17 05:45:04 +01:00
|
|
|
import com.minelittlepony.model.PMAPI;
|
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-11-25 05:40:19 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2017-06-13 05:55:50 +02:00
|
|
|
import java.io.Reader;
|
2016-11-25 05:40:19 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.stream.Collectors;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
public class PonyManager implements IResourceManagerReloadListener {
|
2015-12-09 04:14:42 +01:00
|
|
|
|
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");
|
2017-06-13 05:55:50 +02:00
|
|
|
|
|
|
|
private static final Gson GSON = new Gson();
|
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
private List<ResourceLocation> backgroundPonyList = Lists.newArrayList();
|
2015-11-17 06:17:35 +01:00
|
|
|
|
|
|
|
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();
|
2015-11-17 06:17:35 +01:00
|
|
|
|
|
|
|
public PonyManager(PonyConfig config) {
|
|
|
|
this.config = config;
|
2015-08-02 00:36:33 +02:00
|
|
|
initmodels();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initmodels() {
|
2016-11-25 05:40:19 +01:00
|
|
|
MineLittlePony.logger.info("Initializing models...");
|
2015-08-02 00:36:33 +02:00
|
|
|
PMAPI.init();
|
2016-11-25 05:40:19 +01:00
|
|
|
MineLittlePony.logger.info("Done initializing models.");
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public Pony getPony(ResourceLocation skinResourceLocation) {
|
2017-06-13 05:55:50 +02:00
|
|
|
return this.poniesCache.computeIfAbsent(skinResourceLocation, Pony::new);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public Pony getPony(AbstractClientPlayer player) {
|
2017-06-13 05:55:50 +02:00
|
|
|
|
|
|
|
Pony myLittlePony = this.poniesCache.computeIfAbsent(player.getLocationSkin(), res -> new Pony(player));
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.getMetadata().getRace() == PonyRace.HUMAN) {
|
2015-08-02 00:36:33 +02:00
|
|
|
myLittlePony = this.getPonyFromBackgroundResourceRegistry(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
return myLittlePony;
|
|
|
|
}
|
|
|
|
|
2017-06-16 07:41:36 +02:00
|
|
|
public Pony removePony(ResourceLocation location) {
|
|
|
|
return this.poniesCache.remove(location);
|
|
|
|
}
|
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
private ResourceLocation getBackgroundPonyResource(UUID id) {
|
2015-12-09 04:14:42 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-12-09 04:14:42 +01:00
|
|
|
return backgroundPonyList.get(backgroundIndex);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2015-12-09 04:14:42 +01:00
|
|
|
return STEVE;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 05:55:50 +02:00
|
|
|
private Pony getPonyFromBackgroundResourceRegistry(AbstractClientPlayer player) {
|
2015-08-02 00:36:33 +02:00
|
|
|
ResourceLocation textureResourceLocation;
|
2015-12-09 04:14:42 +01:00
|
|
|
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) {
|
2017-06-13 05:55:50 +02:00
|
|
|
this.poniesCache.clear();
|
2016-08-22 23:32:03 +02:00
|
|
|
this.backgroudPoniesCache.clear();
|
|
|
|
this.backgroundPonyList.clear();
|
|
|
|
try {
|
|
|
|
for (IResource res : resourceManager.getAllResources(BGPONIES_JSON)) {
|
2017-06-13 05:55:50 +02:00
|
|
|
try (Reader reader = new InputStreamReader((res.getInputStream()))) {
|
|
|
|
BackgroundPonies ponies = GSON.fromJson(reader, BackgroundPonies.class);
|
2016-08-22 23:32:03 +02:00
|
|
|
if (ponies.override) {
|
|
|
|
this.backgroundPonyList.clear();
|
|
|
|
}
|
|
|
|
this.backgroundPonyList.addAll(ponies.getPonies());
|
|
|
|
} catch (JsonParseException e) {
|
2016-11-25 05:40:19 +01:00
|
|
|
MineLittlePony.logger.error("Invalid bgponies.json in " + res.getResourcePackName(), e);
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
// this isn't the exception you're looking for.
|
|
|
|
}
|
2016-12-20 11:29:12 +01:00
|
|
|
MineLittlePony.logger.info("Detected {} background ponies installed.", getNumberOfPonies());
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2015-12-09 04:14:42 +01:00
|
|
|
return backgroundPonyList.size();
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2016-08-22 23:32:03 +02:00
|
|
|
|
2016-11-25 05:40:19 +01:00
|
|
|
private static class BackgroundPonies {
|
2016-08-22 23:32:03 +02:00
|
|
|
|
2016-11-25 05:40:19 +01:00
|
|
|
private boolean override;
|
2016-08-22 23:32:03 +02:00
|
|
|
private List<String> ponies;
|
|
|
|
|
2016-11-25 05:40:19 +01:00
|
|
|
private BackgroundPonies(List<String> ponies, boolean override) {
|
|
|
|
this.ponies = ponies;
|
|
|
|
this.override = override;
|
|
|
|
}
|
|
|
|
|
|
|
|
private 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() {
|
2016-11-25 05:40:19 +01:00
|
|
|
return this.ponies.stream().map(this::apply).collect(Collectors.toList());
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|