make robots be able to wear player heads

This commit is contained in:
Hea3veN 2015-06-09 22:36:37 -03:00
parent 7f7e09a022
commit 6b8d5bb6c6
2 changed files with 92 additions and 1 deletions

View file

@ -11,8 +11,13 @@ package buildcraft.robotics;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.WeakHashMap;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
@ -22,9 +27,12 @@ import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemSkull;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
@ -33,6 +41,7 @@ import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.util.StringUtils;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
@ -41,6 +50,7 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
@ -1084,11 +1094,60 @@ public class EntityRobot extends EntityRobotBase implements
player.swingItem();
}
return true;
} else if (wearables.size() < 8 && stack.getItem() instanceof ItemSkull) {
if (!worldObj.isRemote) {
ItemStack skullStack =stack.splitStack(1);
initSkullItem(skullStack);
wearables.add(skullStack);
syncWearablesToClient();
} else {
player.swingItem();
}
return true;
} else {
return super.interact(player);
}
}
private void initSkullItem(ItemStack skullStack) {
if (skullStack.hasTagCompound()) {
NBTTagCompound nbttagcompound = skullStack.getTagCompound();
GameProfile gameProfile = null;
if (nbttagcompound.hasKey("SkullOwner", NBT.TAG_COMPOUND)) {
gameProfile = NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("SkullOwner"));
} else if (nbttagcompound.hasKey("SkullOwner", NBT.TAG_STRING)
&& !StringUtils.isNullOrEmpty(nbttagcompound.getString("SkullOwner"))) {
gameProfile = new GameProfile((UUID) null, nbttagcompound.getString("SkullOwner"));
}
if (!StringUtils.isNullOrEmpty(gameProfile.getName())) {
if (!gameProfile.isComplete()
|| !gameProfile.getProperties().containsKey("textures")) {
gameProfile = MinecraftServer.getServer().func_152358_ax()
.func_152655_a(gameProfile.getName());
if (gameProfile != null) {
Property property = (Property) Iterables.getFirst(gameProfile
.getProperties().get("textures"), (Object) null);
if (property == null) {
gameProfile = MinecraftServer.getServer().func_147130_as()
.fillProfileProperties(gameProfile, true);
}
}
}
}
if (gameProfile != null && gameProfile.isComplete()
&& gameProfile.getProperties().containsKey("textures")) {
NBTTagCompound profileNBT = new NBTTagCompound();
NBTUtil.func_152460_a(profileNBT, gameProfile);
nbttagcompound.setTag("SkullOwner", profileNBT);
} else {
nbttagcompound.removeTag("SkullOwner");
}
}
}
private void syncWearablesToClient() {
BuildCraftCore.instance.sendToEntity(new PacketCommand(this, "syncWearables", new CommandWriter() {
public void write(ByteBuf data) {

View file

@ -9,6 +9,10 @@
package buildcraft.robotics.render;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.mojang.authlib.GameProfile;
import org.lwjgl.opengl.GL11;
@ -21,10 +25,14 @@ import net.minecraft.client.renderer.entity.RenderBiped;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.tileentity.TileEntitySkullRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemSkull;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.ForgeHooksClient;
@ -54,6 +62,8 @@ public class RenderRobot extends Render implements IItemRenderer {
};
private ModelRenderer box, helmetBox;
private Map<String, GameProfile> gameProfileCache = new HashMap<String, GameProfile>();
public RenderRobot() {
customRenderItem = new RenderItem() {
@Override
@ -242,9 +252,31 @@ public class RenderRobot extends Render implements IItemRenderer {
helmetBox.render(1 / 16F);
}
GL11.glPopMatrix();
} else if (wearable.getItem() instanceof ItemSkull) {
doRenderSkull(wearable);
}
}
private void doRenderSkull(ItemStack wearable) {
GL11.glPushMatrix();
GL11.glScalef(1.0125F, 1.0125F, 1.0125F);
GameProfile gameProfile = null;
if (wearable.hasTagCompound()) {
NBTTagCompound nbt = wearable.getTagCompound();
if (nbt.hasKey("Name")) {
gameProfile = gameProfileCache.get(nbt.getString("Name"));
} else if (nbt.hasKey("SkullOwner")) {
gameProfile = NBTUtil.func_152459_a(nbt.getCompoundTag("SkullOwner"));
nbt.setString("Name", gameProfile.getName());
gameProfileCache.put(gameProfile.getName(), gameProfile);
}
}
TileEntitySkullRenderer.field_147536_b.func_152674_a(-0.5F, -0.25F, -0.5F, 1, -90.0F,
wearable.getItemDamage(), gameProfile);
GL11.glPopMatrix();
}
private void doRenderRobot(float factor, TextureManager texManager, float storagePercent, boolean isAsleep) {
box.render(factor);