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

207 lines
6.9 KiB
Java
Raw Normal View History

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;
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;
import com.minelittlepony.pony.data.Pony;
import com.minelittlepony.pony.data.PonyLevel;
import net.minecraft.client.Minecraft;
2015-08-02 00:36:33 +02:00
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraft.client.network.NetworkPlayerInfo;
2018-04-24 17:12:23 +02:00
import net.minecraft.client.resources.DefaultPlayerSkin;
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;
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
/**
* The PonyManager is responsible for reading and recoding all the pony data associated with an entity of skin.
*
*/
2016-08-22 23:32:03 +02:00
public class PonyManager implements IResourceManagerReloadListener {
2018-04-26 23:53:03 +02: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");
public static final ResourceLocation BGPONIES_JSON = new ResourceLocation("minelittlepony", "textures/entity/pony/bgponies.json");
private static final Gson GSON = new Gson();
/**
* All currently loaded background ponies.
*/
2016-08-22 23:32:03 +02:00
private List<ResourceLocation> backgroundPonyList = Lists.newArrayList();
private PonyConfig config;
private Map<ResourceLocation, Pony>
poniesCache = Maps.newHashMap(),
backgroudPoniesCache = Maps.newHashMap();
public PonyManager(PonyConfig config) {
this.config = config;
2015-08-02 00:36:33 +02:00
initmodels();
}
private 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
}
/**
* Gets or creates a pony for the given skin resource and vanilla model type.
*
* @param resource A texture resource
*/
public Pony getPony(ResourceLocation resource, boolean slim) {
return poniesCache.computeIfAbsent(resource, res -> new Pony(res, slim));
}
/**
* Gets or creates a pony for the given player.
* Delegates to the background-ponies registry if no pony skins were available and client settings allows it.
*
* @param player the player
*/
2017-06-16 07:41:36 +02:00
public Pony getPony(AbstractClientPlayer player) {
ResourceLocation skin = player.getLocationSkin();
UUID uuid = player.getGameProfile().getId();
if (skin == null) return getDefaultPony(uuid);
return getPony(skin, uuid);
}
public Pony getPony(NetworkPlayerInfo playerInfo) {
ResourceLocation skin = playerInfo.getLocationSkin();
UUID uuid = playerInfo.getGameProfile().getId();
if (skin == null) return getDefaultPony(uuid);
return getPony(skin, uuid);
2015-08-02 00:36:33 +02:00
}
/**
* Gets or creates a pony for the given skin resource and entity id.
*
* Whether is has slim arms is determined by the id.
*
* Delegates to the background-ponies registry if no pony skins were available and client settings allows it.
*
* @param resource A texture resource
* @param uuid id of a player or entity
*/
2018-04-24 17:12:23 +02:00
public Pony getPony(ResourceLocation resource, UUID uuid) {
Pony pony = getPony(resource, isSlimSkin(uuid));
2018-04-24 17:12:23 +02:00
if (config.getPonyLevel() == PonyLevel.PONIES && pony.getMetadata().getRace().isHuman()) {
return getBackgroundPony(uuid);
}
2018-04-24 17:12:23 +02:00
return pony;
2017-06-16 07:41:36 +02:00
}
/**
* Gets the default pony. Either STEVE/ALEX, or a background pony based on client settings.
*
* @param uuid id of a player or entity
*/
2018-04-24 17:12:23 +02:00
public Pony getDefaultPony(UUID uuid) {
if (config.getPonyLevel() != PonyLevel.PONIES) {
return getPony(DefaultPlayerSkin.getDefaultSkin(uuid), isSlimSkin(uuid));
2015-08-02 00:36:33 +02:00
}
2018-04-24 17:12:23 +02:00
return getBackgroundPony(uuid);
2015-08-02 00:36:33 +02:00
}
2018-04-24 17:12:23 +02:00
private Pony getBackgroundPony(UUID uuid) {
if (getNumberOfPonies() == 0 || isUser(uuid)) {
return getPony(getDefaultSkin(uuid), isSlimSkin(uuid));
}
2018-04-24 17:12:23 +02:00
int bgi = uuid.hashCode() % getNumberOfPonies();
while (bgi < 0) bgi += getNumberOfPonies();
2018-04-24 17:12:23 +02:00
return getPony(backgroundPonyList.get(bgi), false);
}
private boolean isUser(UUID uuid) {
return Minecraft.getMinecraft().player != null && Minecraft.getMinecraft().player.getUniqueID().equals(uuid);
}
2015-08-02 00:36:33 +02:00
/**
* De-registers a pony from the cache.
*/
public Pony removePony(ResourceLocation resource) {
return poniesCache.remove(resource);
2015-08-02 00:36:33 +02:00
}
2016-08-22 23:32:03 +02:00
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
poniesCache.clear();
backgroudPoniesCache.clear();
backgroundPonyList.clear();
2016-08-22 23:32:03 +02:00
try {
for (IResource res : resourceManager.getAllResources(BGPONIES_JSON)) {
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) {
backgroundPonyList.clear();
2016-08-22 23:32:03 +02:00
}
backgroundPonyList.addAll(ponies.getPonies());
2016-08-22 23:32:03 +02:00
} 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
}
}
2018-04-12 16:21:19 +02:00
} catch (IOException ignored) {
2016-08-22 23:32:03 +02:00
// 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) {
2018-04-24 17:12:23 +02:00
return isSlimSkin(uuid) ? ALEX : STEVE;
}
/**
* Returns true if the given uuid is of a player would would use the ALEX skin type.
*/
2018-04-24 17:12:23 +02:00
public static boolean isSlimSkin(UUID uuid) {
2018-04-26 17:20:22 +02:00
return (uuid.hashCode() & 1) == 1;
2016-01-26 09:16:11 +01:00
}
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
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() {
return ponies.stream().map(this::apply).collect(Collectors.toList());
2016-08-22 23:32:03 +02:00
}
}
2015-08-02 00:36:33 +02:00
}