Skip to content

Instantly share code, notes, and snippets.

@WillR27
Created July 4, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillR27/db0664dd9705ac504fc7 to your computer and use it in GitHub Desktop.
Save WillR27/db0664dd9705ac504fc7 to your computer and use it in GitHub Desktop.
package com.blocklings.entity;
import java.util.List;
import java.util.Random;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import com.blocklings.main.Blocklings;
import com.blocklings.network.CreatePacketServerSide;
import cpw.mods.fml.client.FMLClientHandler;
public class EntityBlockling extends EntityTameable {
public int xp;
public int level;
public int requiredXP;
public double maxHealth;
public double attackDamage;
public int currentUpgradeTier;
public float hitbox;
int taskID;
int targetTaskID;
public float attackTimer;
public int i;
private static Minecraft client = FMLClientHandler.instance().getClient();
public EntityBlockling(World world) {
super(world);
this.xp = 0;
this.level = 1;
this.requiredXP = 250;
this.maxHealth = 4.0D;
this.attackDamage = 1.0D;
this.currentUpgradeTier = 1;
this.hitbox = 1.0F;
this.i = 0;
this.setSize(hitbox, hitbox);
this.getNavigator().setAvoidsWater(false);
this.getNavigator().setCanSwim(true);
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(2, this.aiSit);
this.tasks.addTask(3, new EntityAIAttackOnCollide(this, 1.0D, false));
this.tasks.addTask(4, new EntityAIFollowOwner(this, 1.0D, 12.0F, 8.0F));
this.tasks.addTask(5, new EntityAIWander(this, 1.0D));
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.tasks.addTask(7, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true));
this.targetTasks.addTask(2, new EntityAIOwnerHurtByTarget(this));
this.targetTasks.addTask(3, new EntityAIOwnerHurtTarget(this));
this.setTamed(false);
this.setEntitySize();
}
protected void applyEntityAttributes() {
this.maxHealth = 4.0D;
this.attackDamage = 1.0D;
super.applyEntityAttributes();
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(this.maxHealth);
this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.25D);
this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(this.attackDamage);
}
public boolean getCanSpawnHere() {
int x = MathHelper.floor_double(this.posX);
int y = MathHelper.floor_double(this.boundingBox.minY);
int z = MathHelper.floor_double(this.posZ);
int l = this.worldObj.getFullBlockLightValue(x, y, z);
boolean s = this.worldObj.canBlockSeeTheSky(x, y, z);
boolean b = this.worldObj.getBlock(x, y - 1, z) == Blocks.grass;
List list = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(posX, posY, posZ, posX + 1, posY + 1, posZ + 1).expand(16D, 512D, 16D));
return list.size() < 5 && l > 8 && b && s;
}
public boolean interact(EntityPlayer entityPlayer) {
ItemStack itemstack = entityPlayer.inventory.getCurrentItem();
String playerName = entityPlayer.getCommandSenderName();
boolean tamed = this.isTamed();
GuiNewChat chat = client.ingameGUI.getChatGUI();
if (playerName.equalsIgnoreCase(this.getOwnerName())) {
if (entityPlayer.isSneaking()) {
if (itemstack == null && !this.worldObj.isRemote) {
chat.printChatMessage(new ChatComponentText("XP: " + this.xp + "/" + this.requiredXP + " | " + "Level: " + this.level));
chat.printChatMessage(new ChatComponentText("Health: " + (int) this.getHealth() + "/" + (int) this.maxHealth + " | " + "Attack Damage: " + (int) this.attackDamage));
}
if(itemstack != null) {
Item item = itemstack.getItem();
if(!this.worldObj.isRemote &&
((item == Blocklings.itemWoodenUpgrade && this.currentUpgradeTier == 1 && this.level >= 2) ||
( item == Blocklings.itemCobblestoneUpgrade && this.currentUpgradeTier == 2 && this.level >= 3) ||
( item == Blocklings.itemStoneUpgrade && this.currentUpgradeTier == 3 && this.level >= 4) ||
( item == Blocklings.itemIronUpgrade && this.currentUpgradeTier == 4 && this.level >= 5) ||
( item == Blocklings.itemLapisLazuliUpgrade && this.currentUpgradeTier == 5 && this.level >= 6) ||
( item == Blocklings.itemGoldUpgrade && this.currentUpgradeTier == 6 && this.level >= 7) ||
( item == Blocklings.itemDiamondUpgrade && this.currentUpgradeTier == 7 && this.level >= 8) ||
( item == Blocklings.itemEmeraldUpgrade && this.currentUpgradeTier == 8 && this.level >= 9) ||
( item == Blocklings.itemObsidianUpgrade && this.currentUpgradeTier == 9 && this.level >= 10))
) {
++this.currentUpgradeTier;
setMaxHealth();
setAttackDamage();
this.heal((float)maxHealth - this.getHealth());
if (!entityPlayer.capabilities.isCreativeMode) {
--itemstack.stackSize;
if (itemstack.stackSize <= 0) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, (ItemStack)null);
}
}
String upgradeName = null;
if (item == Blocklings.itemWoodenUpgrade) { upgradeName = "Wooden"; }
else if (item == Blocklings.itemCobblestoneUpgrade) { upgradeName = "Cobblestone"; }
else if (item == Blocklings.itemStoneUpgrade) { upgradeName = "Stone"; }
else if (item == Blocklings.itemIronUpgrade) { upgradeName = "Iron"; }
else if (item == Blocklings.itemLapisLazuliUpgrade) { upgradeName = "Lapis Lazuli"; }
else if (item == Blocklings.itemGoldUpgrade) { upgradeName = "Gold"; }
else if (item == Blocklings.itemDiamondUpgrade) { upgradeName = "Diamond"; }
else if (item == Blocklings.itemEmeraldUpgrade) { upgradeName = "Emerald"; }
else if (item == Blocklings.itemObsidianUpgrade) { upgradeName = "Obsidian"; }
chat.printChatMessage(new ChatComponentText(upgradeName + " Upgrade Applied"));
CreatePacketServerSide.sendS2CEntitySync(this);
}
}
} else if (tamed) {
this.aiSit.setSitting(!this.isSitting());
}
}
if (itemstack != null && !tamed && (itemstack.getItem() == Item.getItemFromBlock(Blocks.red_flower) || itemstack.getItem() == Item.getItemFromBlock(Blocks.yellow_flower))) {
if (!entityPlayer.capabilities.isCreativeMode) {
--itemstack.stackSize;
if (itemstack.stackSize <= 0) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, (ItemStack)null);
}
}
if (this.rand.nextInt(2) == 0) {
this.setTamed(true);
this.setPathToEntity((PathEntity)null);
this.setAttackTarget((EntityLivingBase)null);
this.setOwner(playerName);
this.playTameEffect(true);
this.worldObj.setEntityState(this, (byte)7);
} else {
this.playTameEffect(false);
this.worldObj.setEntityState(this, (byte)6);
}
return true;
}
return super.interact(entityPlayer);
}
public void onLivingUpdate() {
super.onLivingUpdate();
if (this.attackTimer > 0) {
--this.attackTimer;
if (!this.worldObj.isRemote) {
CreatePacketServerSide.sendS2CEntitySync(this);
}
}
if (i < 5) {
if (!this.worldObj.isRemote) {
CreatePacketServerSide.sendS2CEntitySync(this);
i++;
System.out.println(i);
}
}
}
public boolean attackEntityAsMob(Entity entity) {
Random random = new Random();
if (this.attackTimer <= 2) {
this.attackTimer = 8;
}
if (this.isTamed()) {
addXP((int) this.attackDamage + random.nextInt((int) this.attackDamage + (int) this.attackDamage * 3));
setMaxHealth();
setAttackDamage();
}
return entity.attackEntityFrom(DamageSource.causeMobDamage(this), (float) getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue());
}
public void writeEntityToNBT(NBTTagCompound compound) {
super.writeEntityToNBT(compound);
compound.setInteger("XP", this.xp);
compound.setInteger("Level", this.level);
compound.setInteger("Required XP", this.requiredXP);
compound.setDouble("Max Health", this.maxHealth);
compound.setDouble("Attack Damage", this.attackDamage);
compound.setInteger("Current Upgrade Tier", this.currentUpgradeTier);
compound.setFloat("Hitbox", this.hitbox);
}
public void readEntityFromNBT(NBTTagCompound compound) {
super.readEntityFromNBT(compound);
this.xp = compound.getInteger("XP");
this.level = compound.getInteger("Level");
this.requiredXP = compound.getInteger("Required XP");
this.maxHealth = compound.getInteger("Max Health");
this.attackDamage = compound.getInteger("Attack Damage");
this.currentUpgradeTier = compound.getInteger("Current Upgrade Tier");
this.hitbox = compound.getFloat("Hitbox");
}
public void addXP(int addedXP) {
this.xp = this.xp + addedXP;
onLevelUp();
}
public void onLevelUp() {
GuiNewChat chat = client.ingameGUI.getChatGUI();
if (this.xp >= this.requiredXP) {
this.xp = 0;
this.level = level + 1;
calculateRequiredXP();
setMaxHealth();
setEntitySize();
this.heal((float) maxHealth - this.getHealth());
chat.printChatMessage(new ChatComponentText("Your Blockling Levelled Up! It is now level " + this.level));
}
}
public void calculateRequiredXP() {
this.requiredXP = this.level * this.level * 10 * 10;
}
public void setMaxHealth() {
switch (this.level) {
case 1:
this.maxHealth = 4.0;
break;
case 2:
this.maxHealth = 5.0;
break;
case 3:
this.maxHealth = 6.0;
break;
case 4:
this.maxHealth = 11.0;
break;
case 5:
this.maxHealth = 14.0;
break;
case 6:
this.maxHealth = 18.0;
break;
case 7:
this.maxHealth = 22.0;
break;
case 8:
this.maxHealth = 27.0;
break;
case 9:
this.maxHealth = 32.0;
break;
default:
this.maxHealth = 39.0;
break;
}
this.maxHealth += this.currentUpgradeTier;
if (this.currentUpgradeTier == 10) {
++this.maxHealth;
}
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(this.maxHealth);
}
public void setAttackDamage() {
switch (this.level) {
case 1:
this.attackDamage = 1.0;
break;
case 2:
this.attackDamage = 2.0;
break;
case 3:
this.attackDamage = 3.0;
break;
case 4:
this.attackDamage = 4.0;
break;
case 5:
this.attackDamage = 5.0;
break;
case 6:
this.attackDamage = 7.0;
break;
case 7:
this.attackDamage = 9.0;
break;
case 8:
this.attackDamage = 11.0;
break;
case 9:
this.attackDamage = 13.0;
break;
default:
this.attackDamage = 15.0;
break;
}
this.attackDamage += this.currentUpgradeTier;
this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(this.attackDamage);
}
public void setEntitySize() {
this.hitbox = (float) (0.05 * (3.0 * ((double) level + 1.0)));
this.setSize(this.hitbox, this.hitbox);
System.out.println(hitbox);
}
public int getLevel() {
return this.level;
}
public int getCurrentUpgradeTier() {
return this.currentUpgradeTier;
}
public float getAttackTimer() {
return this.attackTimer;
}
public void setLevel(int level) {
this.level = level;
}
public void setCurrentUpgradeTier(int currentUpgradeTier) {
this.currentUpgradeTier = currentUpgradeTier;
}
public void setAttackTimer(float attackTimer) {
this.attackTimer = attackTimer;
}
@Override
public EntityAgeable createChild(EntityAgeable var1) {
return null;
}
public boolean isAIEnabled() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment