mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2024-11-25 22:07:59 +01:00
Make Ponies and metadata immutable
This commit is contained in:
parent
825fc22f55
commit
62e77acc38
7 changed files with 79 additions and 85 deletions
|
@ -14,71 +14,71 @@ import net.minecraft.client.resources.IResource;
|
|||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
public class Pony {
|
||||
|
||||
private static final AtomicInteger ponyCount = new AtomicInteger();
|
||||
private final int ponyId = ponyCount.getAndIncrement();
|
||||
|
||||
private ResourceLocation textureResourceLocation;
|
||||
public PonyData metadata = new PonyData();
|
||||
|
||||
private int skinCheckCount;
|
||||
private boolean skinChecked;
|
||||
private final ResourceLocation texture;
|
||||
private final PonyData metadata;
|
||||
|
||||
public Pony(AbstractClientPlayer player) {
|
||||
this.textureResourceLocation = player.getLocationSkin();
|
||||
MineLittlePony.logger.debug("+ Initialising new pony #{} for player {} ({}) with resource location {}.", this.ponyId,
|
||||
player.getName(), player.getUniqueID(), this.textureResourceLocation);
|
||||
this.checkSkin(this.textureResourceLocation);
|
||||
this.texture = player.getLocationSkin();
|
||||
this.metadata = this.checkSkin(this.texture);
|
||||
|
||||
MineLittlePony.logger.debug("+ Initialising new pony #{} for player {} ({}) with resource location {}.",
|
||||
this.ponyId, player.getName(), player.getUniqueID(), this.texture);
|
||||
}
|
||||
|
||||
public Pony(ResourceLocation aTextureResourceLocation) {
|
||||
this.textureResourceLocation = aTextureResourceLocation;
|
||||
MineLittlePony.logger.debug("+ Initialising new pony #{} with resource location {}.", this.ponyId, this.textureResourceLocation);
|
||||
this.checkSkin(this.textureResourceLocation);
|
||||
public Pony(ResourceLocation resourceLocation) {
|
||||
this(resourceLocation, null);
|
||||
}
|
||||
|
||||
public void invalidateSkinCheck() {
|
||||
this.skinChecked = false;
|
||||
metadata = new PonyData();
|
||||
public Pony(ResourceLocation aTextureResourceLocation, @Nullable PonyData meta) {
|
||||
this.texture = aTextureResourceLocation;
|
||||
this.metadata = meta != null ? meta : this.checkSkin(this.texture);
|
||||
|
||||
MineLittlePony.logger.debug("+ Initialising new pony #{} with resource location {}.", this.ponyId, this.texture);
|
||||
}
|
||||
|
||||
public void checkSkin() {
|
||||
if (!this.skinChecked) {
|
||||
this.checkSkin(this.textureResourceLocation);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkSkin(ResourceLocation textureResourceLocation) {
|
||||
if(!checkPonyMeta(textureResourceLocation)) {
|
||||
private PonyData checkSkin(ResourceLocation textureResourceLocation) {
|
||||
PonyData data = checkPonyMeta(textureResourceLocation);
|
||||
if (data == null) {
|
||||
BufferedImage skinImage = this.getBufferedImage(textureResourceLocation);
|
||||
if (skinImage != null) {
|
||||
this.checkSkin(skinImage);
|
||||
data = this.checkSkin(skinImage);
|
||||
} else {
|
||||
data = new PonyData();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private boolean checkPonyMeta(ResourceLocation location) {
|
||||
@Nullable
|
||||
private PonyData checkPonyMeta(ResourceLocation location) {
|
||||
try {
|
||||
IResource res = Minecraft.getMinecraft().getResourceManager().getResource(location);
|
||||
if (res.hasMetadata()) {
|
||||
PonyData data = res.getMetadata(PonyDataSerialzier.NAME);
|
||||
if (data != null) {
|
||||
metadata = data;
|
||||
this.skinChecked = true;
|
||||
return data;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
// Ignore uploaded texture
|
||||
} catch (IOException e) {
|
||||
MineLittlePony.logger.warn("Unable to read {} metadata", location, e);
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@ -113,10 +113,9 @@ public class Pony {
|
|||
return skinImage;
|
||||
}
|
||||
|
||||
public void checkSkin(BufferedImage bufferedimage) {
|
||||
MineLittlePony.logger.debug("\tStart skin check #{} for pony #{} with image {}.", ++this.skinCheckCount, this.ponyId, bufferedimage);
|
||||
metadata = PonyData.parse(bufferedimage);
|
||||
this.skinChecked = true;
|
||||
private PonyData checkSkin(BufferedImage bufferedimage) {
|
||||
MineLittlePony.logger.debug("\tStart skin check for pony #{} with image {}.", this.ponyId, bufferedimage);
|
||||
return PonyData.parse(bufferedimage);
|
||||
}
|
||||
|
||||
public boolean isPegasusFlying(EntityPlayer player) {
|
||||
|
@ -149,8 +148,11 @@ public class Pony {
|
|||
return model;
|
||||
}
|
||||
|
||||
public ResourceLocation getTextureResourceLocation() {
|
||||
return this.textureResourceLocation;
|
||||
public ResourceLocation getTexture() {
|
||||
return this.texture;
|
||||
}
|
||||
|
||||
public PonyData getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ import com.google.common.collect.ImmutableBiMap;
|
|||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Map;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
public class PonyData implements IPonyData {
|
||||
|
||||
private static final Map<Integer, PonyRace> RACE_COLORS = ImmutableBiMap.<Integer, PonyRace>builder()
|
||||
|
@ -26,11 +28,11 @@ public class PonyData implements IPonyData {
|
|||
.put(0x534b76, PonySize.TALL)
|
||||
.build();
|
||||
|
||||
private PonyRace race;
|
||||
private TailLengths tailSize;
|
||||
private PonyGender gender;
|
||||
private PonySize size;
|
||||
private int glowColor;
|
||||
private final PonyRace race;
|
||||
private final TailLengths tailSize;
|
||||
private final PonyGender gender;
|
||||
private final PonySize size;
|
||||
private final int glowColor;
|
||||
|
||||
public PonyData() {
|
||||
this(PonyRace.HUMAN, TailLengths.FULL, PonyGender.MARE, PonySize.NORMAL, 0x4444aa);
|
||||
|
@ -48,34 +50,18 @@ public class PonyData implements IPonyData {
|
|||
return race;
|
||||
}
|
||||
|
||||
public void setRace(PonyRace race) {
|
||||
this.race = race;
|
||||
}
|
||||
|
||||
public TailLengths getTail() {
|
||||
return tailSize;
|
||||
}
|
||||
|
||||
public void setTail(TailLengths tailSize) {
|
||||
this.tailSize = tailSize;
|
||||
}
|
||||
|
||||
public PonyGender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(PonyGender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public PonySize getSize() {
|
||||
return MineLittlePony.getConfig().sizes ? size : PonySize.NORMAL;
|
||||
}
|
||||
|
||||
public void setSize(PonySize size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public int getGlowColor() {
|
||||
return glowColor;
|
||||
}
|
||||
|
@ -84,10 +70,6 @@ public class PonyData implements IPonyData {
|
|||
return this.race != null && this.race.hasHorn() && this.glowColor != 0;
|
||||
}
|
||||
|
||||
public void setGlowColor(int glowColor) {
|
||||
this.glowColor = glowColor & 0xffffff;
|
||||
}
|
||||
|
||||
static PonyData parse(BufferedImage image) {
|
||||
int racePx = TriggerPixels.RACE.readColor(image);
|
||||
PonyRace race = RACE_COLORS.getOrDefault(racePx, PonyRace.HUMAN);
|
||||
|
|
|
@ -46,21 +46,25 @@ public class PonyManager implements IResourceManagerReloadListener {
|
|||
MineLittlePony.logger.info("Done initializing models.");
|
||||
}
|
||||
|
||||
public Pony getPonyFromResourceRegistry(ResourceLocation skinResourceLocation) {
|
||||
public Pony getPony(ResourceLocation skinResourceLocation) {
|
||||
return this.poniesCache.computeIfAbsent(skinResourceLocation, Pony::new);
|
||||
}
|
||||
|
||||
public Pony getPonyFromResourceRegistry(AbstractClientPlayer player) {
|
||||
public Pony getPony(AbstractClientPlayer player) {
|
||||
|
||||
Pony myLittlePony = this.poniesCache.computeIfAbsent(player.getLocationSkin(), res -> new Pony(player));
|
||||
|
||||
if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.metadata.getRace() == PonyRace.HUMAN) {
|
||||
if (config.getPonyLevel() == PonyLevel.PONIES && myLittlePony.getMetadata().getRace() == PonyRace.HUMAN) {
|
||||
myLittlePony = this.getPonyFromBackgroundResourceRegistry(player);
|
||||
}
|
||||
|
||||
return myLittlePony;
|
||||
}
|
||||
|
||||
public Pony removePony(ResourceLocation location) {
|
||||
return this.poniesCache.remove(location);
|
||||
}
|
||||
|
||||
private ResourceLocation getBackgroundPonyResource(UUID id) {
|
||||
if (getNumberOfPonies() > 0) {
|
||||
int backgroundIndex = id.hashCode() % this.getNumberOfPonies();
|
||||
|
|
|
@ -25,20 +25,20 @@ public class GuiSkinsMineLP extends GuiSkins {
|
|||
@Override
|
||||
protected void onSetLocalSkin(BufferedImage skin, MinecraftProfileTexture.Type type) {
|
||||
MineLittlePony.logger.debug("Invalidating old local skin, checking updated local skin");
|
||||
ponyManager.getPonyFromResourceRegistry(this.localPlayer.getSkinTexture()).checkSkin(skin);
|
||||
ponyManager.removePony(this.localPlayer.getSkinTexture());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetRemoteSkin(MinecraftProfileTexture.Type type) {
|
||||
MineLittlePony.logger.debug("Invalidating old remote skin, checking updated remote skin");
|
||||
ponyManager.getPonyFromResourceRegistry(this.remotePlayer.getSkinTexture()).invalidateSkinCheck();
|
||||
ponyManager.removePony(this.remotePlayer.getSkinTexture());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuiClosed() {
|
||||
super.onGuiClosed();
|
||||
ponyManager.getPonyFromResourceRegistry(this.localPlayer.getSkinTexture()).invalidateSkinCheck();
|
||||
ponyManager.getPonyFromResourceRegistry(this.remotePlayer.getSkinTexture()).invalidateSkinCheck();
|
||||
ponyManager.removePony(this.localPlayer.getSkinTexture());
|
||||
ponyManager.removePony(this.remotePlayer.getSkinTexture());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import net.minecraft.entity.EntityLivingBase;
|
|||
import net.minecraft.init.Items;
|
||||
import net.minecraft.inventory.EntityEquipmentSlot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class RenderPonyModel extends RenderPlayerModel<EntityPonyModel> {
|
||||
|
||||
|
@ -25,13 +26,15 @@ public class RenderPonyModel extends RenderPlayerModel<EntityPonyModel> {
|
|||
|
||||
@Override
|
||||
public ModelPlayer getEntityModel(EntityPonyModel playermodel) {
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPonyFromResourceRegistry(this.getEntityTexture(playermodel));
|
||||
thePony.invalidateSkinCheck();
|
||||
thePony.checkSkin();
|
||||
ResourceLocation loc = this.getEntityTexture(playermodel);
|
||||
if (loc == null) {
|
||||
return super.getEntityModel(playermodel);
|
||||
}
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPony(loc);
|
||||
|
||||
// TODO small arms
|
||||
PlayerModel pm = thePony.getModel(true, false);
|
||||
pm.apply(thePony.metadata);
|
||||
pm.apply(thePony.getMetadata());
|
||||
|
||||
return pm.getModel();
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public abstract class MixinRenderPlayer extends RenderLivingBase<AbstractClientP
|
|||
this.playerModel.getModel().isSleeping = player.isPlayerSleeping();
|
||||
|
||||
if (MineLittlePony.getConfig().showscale && this.playerModel.getModel().metadata.getRace() != PonyRace.HUMAN) {
|
||||
PonySize size = thePony.metadata.getSize();
|
||||
PonySize size = thePony.getMetadata().getSize();
|
||||
if (size == PonySize.FOAL) {
|
||||
this.shadowSize = 0.25F;
|
||||
} else if (size == PonySize.NORMAL) {
|
||||
|
@ -102,7 +102,7 @@ public abstract class MixinRenderPlayer extends RenderLivingBase<AbstractClientP
|
|||
private void setupPlayerScale(AbstractClientPlayer player, double xPosition, double yPosition, double zPosition, CallbackInfo ci) {
|
||||
|
||||
if (MineLittlePony.getConfig().showscale && !(playerModel.getModel() instanceof ModelHumanPlayer)) {
|
||||
PonySize size = thePony.metadata.getSize();
|
||||
PonySize size = thePony.getMetadata().getSize();
|
||||
if (size == PonySize.LARGE) {
|
||||
GlStateManager.scale(0.9F, 0.9F, 0.9F);
|
||||
} else if (size == PonySize.NORMAL || size == PonySize.FOAL) {
|
||||
|
@ -236,24 +236,22 @@ public abstract class MixinRenderPlayer extends RenderLivingBase<AbstractClientP
|
|||
}
|
||||
|
||||
private void updateModel(AbstractClientPlayer player) {
|
||||
this.thePony = MineLittlePony.getInstance().getManager().getPonyFromResourceRegistry(player);
|
||||
thePony.invalidateSkinCheck();
|
||||
thePony.checkSkin();
|
||||
this.thePony = MineLittlePony.getInstance().getManager().getPony(player);
|
||||
this.playerModel = this.getModel(player);
|
||||
this.mainModel = this.playerModel.getModel();
|
||||
this.playerModel.apply(thePony.metadata);
|
||||
this.playerModel.apply(thePony.getMetadata());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ResourceLocation getEntityTexture(AbstractClientPlayer player) {
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPonyFromResourceRegistry(player);
|
||||
return thePony.getTextureResourceLocation();
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPony(player);
|
||||
return thePony.getTexture();
|
||||
}
|
||||
|
||||
private PlayerModel getModel(AbstractClientPlayer player) {
|
||||
ResourceLocation skin = getEntityTexture(player);
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPonyFromResourceRegistry(skin);
|
||||
Pony thePony = MineLittlePony.getInstance().getManager().getPony(skin);
|
||||
return thePony.getModel(false, this.smallArms);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,11 +18,16 @@ public class RenderPonySkeleton<Skeleton extends AbstractSkeleton> extends Rende
|
|||
|
||||
public RenderPonySkeleton(RenderManager rm) {
|
||||
super(rm, PMAPI.skeleton);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addLayers() {
|
||||
super.addLayers();
|
||||
this.addLayer(new LayerBipedArmor(this) {
|
||||
@Override
|
||||
protected void initArmor() {
|
||||
this.modelLeggings = PMAPI.skeleton.getArmor().modelArmor;
|
||||
this.modelArmor = PMAPI.skeleton.getArmor().modelArmorChestplate;
|
||||
this.modelLeggings = getPony().getArmor().modelArmor;
|
||||
this.modelArmor = getPony().getArmor().modelArmorChestplate;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue