diff options
Diffstat (limited to 'src/main/java')
8 files changed, 470 insertions, 0 deletions
diff --git a/src/main/java/com/mondecitronne/homunculus/EntityHomunculus.java b/src/main/java/com/mondecitronne/homunculus/EntityHomunculus.java new file mode 100644 index 0000000..1193014 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/EntityHomunculus.java @@ -0,0 +1,84 @@ +package com.mondecitronne.homunculus; + +import javax.annotation.Nullable; + +import com.mojang.authlib.GameProfile; +import com.mondecitronne.homunculus.proxy.SkinHandlerProxy; + +import io.netty.util.internal.StringUtil; +import net.minecraft.entity.EntityLiving; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTUtil; +import net.minecraft.network.datasync.DataParameter; +import net.minecraft.network.datasync.DataSerializers; +import net.minecraft.network.datasync.EntityDataManager; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.SidedProxy; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class EntityHomunculus extends EntityLiving { + @SidedProxy(clientSide = "com.mondecitronne.homunculus.proxy.SkinHandlerClientProxy", serverSide = "com.mondecitronne.homunculus.proxy.SkinHandlerProxy", modId = "homunculus") + private static SkinHandlerProxy skinProxy; + + private SkinHandlerProxy.SkinOwner skinOwner; + private static final DataParameter<NBTTagCompound> SKIN_OWNER = EntityDataManager.createKey(EntityHomunculus.class, DataSerializers.COMPOUND_TAG); + + public EntityHomunculus(World world) { + super(world); + } + + @Override + protected void entityInit() { + super.entityInit(); + this.getDataManager().register(SKIN_OWNER, new NBTTagCompound()); + } + + private boolean isPlayerProfileUpdated(GameProfile input) { + if (skinOwner == null) { + return input != null; + } else { + GameProfile skinOwnerProfile = skinOwner.getPlayerProfile(); + return !skinOwnerProfile.getName().toLowerCase().equals(input.getName().toLowerCase()) || (input.getId() != null && !input.getId().equals(skinOwnerProfile.getId())); + } + } + + @Nullable + public SkinHandlerProxy.SkinOwner getSkinOwner() { + GameProfile ownerProfile = NBTUtil.readGameProfileFromNBT(this.getDataManager().get(SKIN_OWNER)); + if (isPlayerProfileUpdated(ownerProfile) && !StringUtil.isNullOrEmpty(ownerProfile.getName())) { + skinOwner = skinProxy.createSkinOwner(ownerProfile); + } else if (ownerProfile == null) { + skinOwner = null; + } + return skinOwner; + } + + @Override + public void readFromNBT(NBTTagCompound compound) { + super.readFromNBT(compound); + if (compound.hasKey("SkinOwner")) { + NBTTagCompound ownerCompound = compound.getCompoundTag("SkinOwner"); + if (ownerCompound.hasKey("Name")) { + this.getDataManager().set(SKIN_OWNER, ownerCompound); + } + } + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + super.writeToNBT(compound); + SkinHandlerProxy.SkinOwner owner = getSkinOwner(); + if (owner != null) { + NBTTagCompound profileCompound = new NBTTagCompound(); + NBTUtil.writeGameProfile(profileCompound, owner.getPlayerProfile()); + NBTTagCompound ownerCompound = new NBTTagCompound(); + ownerCompound.setTag("Name", profileCompound.getTag("Name")); + if (profileCompound.hasKey("Id")) { + ownerCompound.setTag("Id", profileCompound.getTag("Id")); + } + compound.setTag("SkinOwner", ownerCompound); + } + return compound; + } +} diff --git a/src/main/java/com/mondecitronne/homunculus/Homunculus.java b/src/main/java/com/mondecitronne/homunculus/Homunculus.java new file mode 100644 index 0000000..deb97b4 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/Homunculus.java @@ -0,0 +1,40 @@ +package com.mondecitronne.homunculus; + +import com.mondecitronne.homunculus.proxy.Proxy; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; +import net.minecraftforge.fml.common.Mod; +import org.apache.logging.log4j.Logger; +import net.minecraftforge.fml.common.SidedProxy; + +@Mod(modid = Homunculus.MODID, name = Homunculus.NAME, version = Homunculus.VERSION) +public class Homunculus { + public static final String MODID = "homunculus"; + public static final String NAME = "Homunculus"; + public static final String VERSION = "1.0"; + + @SidedProxy(clientSide = "com.mondecitronne.homunculus.proxy.ClientProxy", serverSide = "com.mondecitronne.homunculus.proxy.Proxy") + public static Proxy proxy; + + @Mod.Instance + public static Homunculus instance; + + static Logger logger; + + @Mod.EventHandler + public void preInit(FMLPreInitializationEvent event) { + logger = event.getModLog(); + proxy.preInit(event); + } + + @Mod.EventHandler + public void init(FMLInitializationEvent event) { + proxy.init(event); + } + + @Mod.EventHandler + public void postInit(FMLPostInitializationEvent event) { + proxy.postInit(event); + } +} diff --git a/src/main/java/com/mondecitronne/homunculus/PlayerSkin.java b/src/main/java/com/mondecitronne/homunculus/PlayerSkin.java new file mode 100644 index 0000000..9649790 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/PlayerSkin.java @@ -0,0 +1,124 @@ +package com.mondecitronne.homunculus; + +import java.io.File; +import java.util.Map; +import java.util.UUID; + +import javax.annotation.Nullable; + +import com.google.common.collect.Iterables; +import com.mojang.authlib.AuthenticationService; +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.GameProfileRepository; +import com.mojang.authlib.minecraft.MinecraftProfileTexture; +import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; +import com.mojang.authlib.minecraft.MinecraftSessionService; +import com.mojang.authlib.properties.Property; +import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService; + +import net.minecraft.client.Minecraft; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.management.PlayerProfileCache; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StringUtils; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class PlayerSkin { + private GameProfile playerProfile; + private boolean skinLoaded; + private static PlayerProfileCache profileCache; + private static MinecraftSessionService sessionService; + + public static void initProfileCache() { + AuthenticationService authenticationService = new YggdrasilAuthenticationService(Minecraft.getMinecraft().getProxy(), UUID.randomUUID().toString()); + sessionService = authenticationService.createMinecraftSessionService(); + GameProfileRepository profileRepository = authenticationService.createProfileRepository(); + profileCache = new PlayerProfileCache(profileRepository, new File(Minecraft.getMinecraft().gameDir, MinecraftServer.USER_CACHE_FILE.getName())); + } + + public PlayerSkin(GameProfile profile) { + playerProfile = profile; + } + + public void loadSkin() { + if (!skinLoaded) { + playerProfile = populateGameProfile(playerProfile); + skinLoaded = true; + } + } + + public GameProfile getPlayerProfile() { + assert(playerProfile != null); + return playerProfile; + } + + protected MinecraftProfileTexture getProfileTexture() { + Minecraft minecraft = Minecraft.getMinecraft(); + Map<Type, MinecraftProfileTexture> map = minecraft.getSkinManager().loadSkinFromCache(playerProfile); + if (map != null) { + return map.get(Type.SKIN); + } else { + return null; + } + } + + @Nullable + public String getModelType() { + if (getTexture() != null) { + MinecraftProfileTexture tex = getProfileTexture(); + if (tex != null && tex.getMetadata("model") != null) { + return tex.getMetadata("model"); + } + } + return null; + } + + @Nullable + public ResourceLocation getTexture() { + MinecraftProfileTexture tex = getProfileTexture(); + if (tex != null) { + ResourceLocation location = Minecraft.getMinecraft().getSkinManager().loadSkin(tex, Type.SKIN); + if (location != null) { + return location; + } + } + return null; + } + + private static GameProfile populateGameProfile(GameProfile input) { + if (input != null) { + if (input.isComplete() && input.getProperties().containsKey("textures")) { + return input; + } else if (profileCache != null && sessionService != null) { + GameProfile gameProfile = null; + if (!StringUtils.isNullOrEmpty(input.getName())) { + gameProfile = profileCache.getGameProfileForUsername(input.getName()); + if (input.getId() != null && !input.getId().equals(gameProfile.getId())) { + return input; + } + } + + if (gameProfile == null) { + return input; + } else { + Property property = (Property)Iterables.getFirst(gameProfile.getProperties().get("textures"), (Object)null); + + if (property == null) { + gameProfile = sessionService.fillProfileProperties(gameProfile, true); + if (gameProfile == null) { + return input; + } + } + + return gameProfile; + } + } else { + return input; + } + } else { + return input; + } + } +} diff --git a/src/main/java/com/mondecitronne/homunculus/client/RenderHomunculus.java b/src/main/java/com/mondecitronne/homunculus/client/RenderHomunculus.java new file mode 100644 index 0000000..16ade41 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/client/RenderHomunculus.java @@ -0,0 +1,89 @@ +package com.mondecitronne.homunculus.client; + +import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.collect.Maps; +import com.mondecitronne.homunculus.EntityHomunculus; +import com.mondecitronne.homunculus.PlayerSkin; +import com.mondecitronne.homunculus.proxy.SkinHandlerClientProxy; +import com.mondecitronne.homunculus.proxy.SkinHandlerProxy; + +import net.minecraft.client.renderer.entity.Render; +import net.minecraft.client.renderer.entity.RenderLivingBase; +import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraftforge.fml.client.registry.IRenderFactory; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import net.minecraft.client.model.ModelPlayer; +import net.minecraft.util.ResourceLocation; + +@SideOnly(Side.CLIENT) +public class RenderHomunculus extends RenderLivingBase<EntityHomunculus> { + public static final Factory FACTORY = new Factory(); + private final Map<String, ModelPlayer> modelTypes = Maps.<String, ModelPlayer>newHashMap(); + + public RenderHomunculus(RenderManager rendermanagerIn) { + super(rendermanagerIn, new ModelPlayer(0.0F, false), 0.5F); + modelTypes.put("default", new ModelPlayer(0.0F, false)); + modelTypes.put("slim", new ModelPlayer(0.0F, true)); + } + + @Nullable + protected static PlayerSkin getSkin(EntityHomunculus entity) { + SkinHandlerProxy.SkinOwner owner = entity.getSkinOwner(); + if (owner != null) { + PlayerSkin skin = ((SkinHandlerClientProxy.SkinOwner) entity.getSkinOwner()).getPlayerSkin(); + skin.loadSkin(); + return skin; + } else { + return null; + } + } + + @Nonnull + public String getModelType(EntityHomunculus entity) { + String modelType = DefaultPlayerSkin.getSkinType(entity.getUniqueID()); + PlayerSkin skin = getSkin(entity); + if (skin != null) { + String type = skin.getModelType(); + if (type != null) { + modelType = type; + } + } + return modelType; + } + + @Override + protected ResourceLocation getEntityTexture(@Nonnull EntityHomunculus entity) { + ResourceLocation texture = DefaultPlayerSkin.getDefaultSkin(entity.getUniqueID()); + PlayerSkin skin = getSkin(entity); + if (skin != null) { + ResourceLocation tex = skin.getTexture(); + if (tex != null) { + texture = tex; + } + } + return texture; + } + + @Override + public void doRender(EntityHomunculus entity, double x, double y, double z, float entityYaw, float partialTicks) { + mainModel = modelTypes.get(getModelType(entity)); + super.doRender(entity, x, y, z, entityYaw, partialTicks); + } + + @Override + protected boolean canRenderName(EntityHomunculus entity) { + return super.canRenderName(entity) && entity.hasCustomName(); + } + + public static class Factory implements IRenderFactory<EntityHomunculus> { + @Override + public Render<? super EntityHomunculus> createRenderFor(RenderManager manager) { + return new RenderHomunculus(manager); + } + } +} diff --git a/src/main/java/com/mondecitronne/homunculus/proxy/ClientProxy.java b/src/main/java/com/mondecitronne/homunculus/proxy/ClientProxy.java new file mode 100644 index 0000000..58d86ae --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/proxy/ClientProxy.java @@ -0,0 +1,30 @@ +package com.mondecitronne.homunculus.proxy; + +import com.mondecitronne.homunculus.EntityHomunculus; +import com.mondecitronne.homunculus.PlayerSkin; +import com.mondecitronne.homunculus.client.RenderHomunculus; + +import net.minecraftforge.client.event.ModelRegistryEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.client.registry.RenderingRegistry; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@Mod.EventBusSubscriber(Side.CLIENT) +@SideOnly(Side.CLIENT) +public class ClientProxy extends Proxy { + @Override + public void preInit(FMLPreInitializationEvent e) { + super.preInit(e); + PlayerSkin.initProfileCache(); + + RenderingRegistry.registerEntityRenderingHandler(EntityHomunculus.class, RenderHomunculus.FACTORY); + } + + @SubscribeEvent + public static void registerModels(ModelRegistryEvent event) { + } +} + diff --git a/src/main/java/com/mondecitronne/homunculus/proxy/Proxy.java b/src/main/java/com/mondecitronne/homunculus/proxy/Proxy.java new file mode 100644 index 0000000..d32f489 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/proxy/Proxy.java @@ -0,0 +1,37 @@ +package com.mondecitronne.homunculus.proxy; + +import net.minecraft.block.Block; +import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.registry.EntityRegistry; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraft.item.Item; +import net.minecraft.util.ResourceLocation; + +import com.mondecitronne.homunculus.EntityHomunculus; +import com.mondecitronne.homunculus.Homunculus; + +@Mod.EventBusSubscriber +public class Proxy { + public void preInit(FMLPreInitializationEvent e) { + EntityRegistry.registerModEntity(new ResourceLocation(Homunculus.MODID, "homunculus"), EntityHomunculus.class, "homunculus", 0, Homunculus.instance, 64, 3, true); + } + + public void init(FMLInitializationEvent e) { + } + + public void postInit(FMLPostInitializationEvent e) { + } + + @SubscribeEvent + public static void registerBlocks(RegistryEvent.Register<Block> event) { + } + + @SubscribeEvent + public static void registerItems(RegistryEvent.Register<Item> event) { + } +}
\ No newline at end of file diff --git a/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerClientProxy.java b/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerClientProxy.java new file mode 100644 index 0000000..8c7a122 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerClientProxy.java @@ -0,0 +1,42 @@ +package com.mondecitronne.homunculus.proxy; + +import com.mojang.authlib.GameProfile; +import com.mondecitronne.homunculus.PlayerSkin; + +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class SkinHandlerClientProxy extends SkinHandlerProxy { + public class SkinOwner extends SkinHandlerProxy.SkinOwner { + private PlayerSkin skin; + + SkinOwner(GameProfile playerProfileIn) { + super(playerProfileIn); + skin = new PlayerSkin(playerProfileIn); + } + + @Override + public GameProfile getPlayerProfile() { + if (skin != null) { + return skin.getPlayerProfile(); + } else { + return super.getPlayerProfile(); + } + } + + public PlayerSkin getPlayerSkin() { + return skin; + } + + @Override + public void loadSkin() { + skin.loadSkin(); + } + } + + @Override + public SkinOwner createSkinOwner(GameProfile playerProfile) { + return new SkinOwner(playerProfile); + } +} diff --git a/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerProxy.java b/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerProxy.java new file mode 100644 index 0000000..b56ba53 --- /dev/null +++ b/src/main/java/com/mondecitronne/homunculus/proxy/SkinHandlerProxy.java @@ -0,0 +1,24 @@ +package com.mondecitronne.homunculus.proxy; + +import com.mojang.authlib.GameProfile; + +public class SkinHandlerProxy { + public class SkinOwner { + private GameProfile playerProfile; + + SkinOwner(GameProfile playerProfileIn) { + playerProfile = playerProfileIn; + } + + public GameProfile getPlayerProfile() { + return playerProfile; + } + + public void loadSkin() { + } + } + + public SkinOwner createSkinOwner(GameProfile playerProfile) { + return new SkinOwner(playerProfile); + } +}
\ No newline at end of file |
