Guest User

Untitled

a guest
Jun 15th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package com.wwy.mod.te.techno;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.EntityCreature;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.SharedMonsterAttributes;
  12. import net.minecraft.entity.monster.EntityMob;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.ChatComponentText;
  18. import net.minecraft.world.World;
  19.  
  20. import com.wwy.mod.blocks.techno.BlockTechnoReplenisher;
  21. import com.wwy.mod.util.Timer;
  22.  
  23. public class TETechnoReplenisher extends TileEntity {
  24.  
  25.     int datum;
  26.     int cooldown = 2*40;
  27.     int maxCooldown;
  28.     int currentCooldown;
  29.     BlockTechnoReplenisher block;
  30.     Timer timer;
  31.    
  32.     public TETechnoReplenisher(BlockTechnoReplenisher block) {
  33.         this.block = block;
  34.         this.timer = new Timer(80, true, false);
  35.     }
  36.    
  37.     @Override
  38.     public void updateEntity() {
  39.         this.datum += 5;
  40.         this.markDirty();
  41.         this.timer.tick();
  42.         if (this.datum >= 1000 && timer.currentCountdown == 0 && !this.worldObj.isRemote) {
  43.             this.searchAndHeal();
  44.         }
  45.     }
  46.  
  47.     private void searchAndHeal() {
  48.         World world = this.getWorldObj();
  49.         AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xCoord-5, yCoord-5, zCoord-5, xCoord+5, yCoord+5, zCoord+5);
  50.         List<Entity> raw = world.getEntitiesWithinAABB(EntityLivingBase.class, aabb);
  51.         List<Entity> entities = new ArrayList<>();
  52.         List<EntityPlayer> players = new ArrayList<>();
  53.         for (Entity target : raw) {
  54.             if (target instanceof EntityPlayer) {
  55.                 // It is first priority
  56.                 players.add((EntityPlayer) target);
  57.             } else if (target instanceof EntityCreature && !(target instanceof EntityMob)) {
  58.                 entities.add(target);
  59.             }
  60.         }
  61.        
  62.         if (!players.isEmpty()) {
  63.             float lowestHealth = 2000000;
  64.             EntityPlayer priority = null;
  65.             for (EntityPlayer player : players) {
  66.                 if (player.getHealth() < lowestHealth) {
  67.                     lowestHealth = player.getHealth();
  68.                     Minecraft.getMinecraft().thePlayer.sendChatMessage("The health is: " + player.getHealth());
  69.                     priority = player;
  70.                 };
  71.             }
  72.             if (!world.isRemote) {
  73.                 if (priority != null) {
  74.                     priority.heal(4);
  75.                     float f = priority.getHealth();
  76.                     priority.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(f + 4);
  77.                 }
  78.             }
  79.            
  80.             if (priority != null) {
  81.                 List<String> messages = new ArrayList<>();
  82.                 messages.add(priority.getDisplayName() + ", a replenisher has found you as the target.");
  83.                 messages.add("You are healed for 2 hearts, and you have been boosted for another 2 hearts.");
  84.                
  85.                 for (String text : messages) {
  86.                     ChatComponentText message = new ChatComponentText(text);
  87.                     priority.addChatComponentMessage(message);
  88.                 }
  89.                
  90.                 world.spawnParticle("instantSpell", xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
  91.             }
  92.            
  93.             timer.reset();
  94.             this.datum -= 1000;
  95.             return;
  96.         }
  97.        
  98.         if (!entities.isEmpty()) {
  99.             Random random = new Random();
  100.             EntityLivingBase target = (EntityLivingBase) entities.get(random.nextInt(entities.size()));
  101.             target.heal(4);
  102.             Minecraft.getMinecraft().thePlayer.sendChatMessage("Successfully found and healed a Creature.");
  103.             timer.reset();
  104.             this.datum -= 1000;
  105.             return;
  106.         }
  107.     }
  108.  
  109.     public void printInfo() {
  110.         // TODO Auto-generated method stub
  111.         Minecraft.getMinecraft().thePlayer.sendChatMessage("Current datum: " + this.datum);
  112.     }
  113.  
  114.     @Override
  115.     public void readFromNBT(NBTTagCompound tag) {
  116.         if (!worldObj.isRemote) {
  117.             System.out.println("Reading from NBT.");
  118.             super.readFromNBT(tag);
  119.             this.datum = tag.getInteger("Datum");
  120.         }
  121.     }
  122.  
  123.     @Override
  124.     public void writeToNBT(NBTTagCompound tag) {
  125.         if (!worldObj.isRemote) {
  126.             System.out.println("Writing to NBT.");
  127.             super.writeToNBT(tag);
  128.             tag.setInteger("Datum", this.datum);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment