Skip to content

Instantly share code, notes, and snippets.

@EmmaEwert
Forked from WillR27/gist:338ecfe4a6d1e343c40f
Last active August 29, 2015 14:03
Show Gist options
  • Save EmmaEwert/bd874849632050cc627d to your computer and use it in GitHub Desktop.
Save EmmaEwert/bd874849632050cc627d to your computer and use it in GitHub Desktop.
package com.blocklings.entity;
import java.util.List;
import java.util.Random;
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.EntityAIBeg;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILeapAtTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAITarget;
import net.minecraft.entity.ai.EntityAITargetNonTamed;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.passive.EntitySheep;
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;
public float attackTimer;
public int i; // Where is this used?
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;
taskId = 1;
targetTaskId = 1
this.setSize(hitbox, hitbox);
this.getNavigator().setAvoidsWater(false);
this.getNavigator().setCanSwim(true);
this.tasks.addTask(taskId++, new EntityAISwimming(this));
this.tasks.addTask(taskId++, this.aiSit);
this.tasks.addTask(taskId++, new EntityAIAttackOnCollide(this, 1.0D, false));
this.tasks.addTask(taskId++, new EntityAIFollowOwner(this, 1.0D, 12.0F, 8.0F));
this.tasks.addTask(taskId++, new EntityAIWander(this, 1.0D));
this.tasks.addTask(taskId++, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.tasks.addTask(taskId++, new EntityAILookIdle(this));
this.targetTasks.addTask(targetTaskId++, new EntityAIHurtByTarget(this, true));
this.targetTasks.addTask(targetTaskId++, new EntityAIOwnerHurtByTarget(this));
this.targetTasks.addTask(targetTaskId++, 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));
// You may want to return early from this method, or call each method directly in the comparisons of the boolean statement below,
// to avoid unnecessary calls to the various methods above.
return list.size() < 5 && l > 8 && b && s;
}
public boolean interact(EntityPlayer entityPlayer) {
ItemStack itemstack = entityPlayer.inventory.getCurrentItem();
String playerName = entityPlayer.getCommandSenderName();
// You are not doing anything with packets, and from what I could figure out from your code, you are relying on the world not being remote.
if (this.worldObj.isRemote) {
return super(entityPlayer);
}
boolean tamed = this.isTamed();
if (playerName.equalsIgnoreCase(this.getOwnerName())) {
if (entityPlayer.isSneaking()) {
GuiNewChat chat = client.ingameGUI.getChatGUI();
if (itemstack == null) {
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));
} else {
Item item = itemstack.getItem();
if (
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) {
// Does this not happen automatically?
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, (ItemStack)null);
}
}
// This part can be avoided if the localised names of your items are eg. "Wooden Upgrade", etc.
String upgradeName;
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);
}
}
// Is the tamed flag not implied from the fact that the given player is the owner?
} else if (tamed) {
this.aiSit.setSitting(!this.isSitting());
}
} else if (!tamed && item == Item.getItemFromBlock(Blocks.red_flower) || item == Item.getItemFromBlock(Blocks.yellow_flower)) {
if (!entityPlayer.capabilities.isCreativeMode) {
--itemstack.stackSize;
if (itemstack.stackSize <= 0) {
// Does this not happen automatically?
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 < 3 && !worldObj.isRemote) {
CreatePacketServerSide.sendS2CEntitySync(this);
i++;
}
}
public boolean attackEntityAsMob(Entity entity) {
// Why not use the random you already have as a field in your class?
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() {
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 += Math.min((double)this.currentUpgradeTier, 10.0);
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 += Math.min((double)this.currentUpgradeTier, 10.0);
this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(this.attackDamage);
}
public void setEntitySize() {
this.hitbox = 0.05 * (3.0 * ((double)level + 1.0));
this.setSize(this.hitbox, this.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