Guest User

Untitled

a guest
Jun 15th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 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.BlockLibrary;
  21. import com.wwy.mod.blocks.techno.BlockTechnoReplenisher;
  22. import com.wwy.mod.util.Timer;
  23.  
  24. public class TETechnoReplenisher extends TileEntity {
  25.  
  26.     int datum = 0;
  27.     int cooldown = 2*40;
  28.     int maxCooldown = 2*40;
  29.     int currentCooldown = maxCooldown;
  30.     BlockTechnoReplenisher block;
  31.    
  32.     public TETechnoReplenisher() {
  33.         this.blockType = BlockLibrary.blockTechnoReplenisher;
  34.     }
  35.    
  36.     @Override
  37.     public void updateEntity() {
  38.         this.datum += 5;
  39.         this.markDirty();
  40.         Random random = new Random();
  41.         /*
  42.         if (random.nextInt(20) == 1) {
  43.             Minecraft.getMinecraft().thePlayer.sendChatMessage("Random check:");
  44.             Minecraft.getMinecraft().thePlayer.sendChatMessage("World Object status (IsRemote): " + this.worldObj.isRemote);
  45.         }
  46.         */
  47.         if (currentCooldown == 0) {
  48.             if (this.datum >= 1000) {
  49.                 // Minecraft.getMinecraft().thePlayer.sendChatMessage("Current Techno Points is 1000. Executing command.");
  50.                 // this.printInfo();
  51.                 if (!worldObj.isRemote) {
  52.                     this.searchAndHeal();
  53.                     return;
  54.                 }
  55.                 return;
  56.             } else {
  57.                 // Datum point is less than 1000
  58.                 // Minecraft.getMinecraft().thePlayer.sendChatMessage("Current Cooldown is 0, waiting for techno points.");
  59.             }
  60.         } else {
  61.             if (this.currentCooldown > 0) {
  62.                 this.currentCooldown--;
  63.                 return;
  64.             } else {
  65.                 return;
  66.             }
  67.         }
  68.     }
  69.  
  70.     private void searchAndHeal() {
  71.         World world = this.getWorldObj();
  72.         AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xCoord-5, yCoord-5, zCoord-5, xCoord+5, yCoord+5, zCoord+5);
  73.         List<Entity> raw = world.getEntitiesWithinAABB(EntityLivingBase.class, aabb);
  74.         List<Entity> entities = new ArrayList<>();
  75.         List<EntityPlayer> players = new ArrayList<>();
  76.         for (Entity target : raw) {
  77.             if (target instanceof EntityPlayer) {
  78.                 // It is first priority
  79.                 players.add((EntityPlayer) target);
  80.             } else if (target instanceof EntityCreature && !(target instanceof EntityMob)) {
  81.                 entities.add(target);
  82.             }
  83.         }
  84.        
  85.         if (!players.isEmpty()) {
  86.             float largestHealth = -1;
  87.             EntityPlayer priority = null;
  88.             List<EntityPlayer> list = new ArrayList<>();
  89.             for (EntityPlayer player: players) {
  90.                 if (player.getHealth() > largestHealth) {
  91.                     largestHealth = player.getHealth();
  92.                     player.addChatMessage(new ChatComponentText("Your health is " + player.getHealth()));
  93.                     priority = player;
  94.                 }
  95.             }
  96.            
  97.             if (true) {
  98.                 if (!list.isEmpty()) {
  99.                     priority = list.get(list.size());
  100.                     priority.heal(4);
  101.                     float f = priority.getHealth();
  102.                     priority.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(f + 4);
  103.                    
  104.                     List<String> messages = new ArrayList<>();
  105.                     messages.add(priority.getDisplayName() + ", a replenisher has found you as the target.");
  106.                     messages.add("You are healed for 2 hearts, and you have been boosted for another 2 hearts.");
  107.                    
  108.                     for (String text : messages) {
  109.                         ChatComponentText message = new ChatComponentText(text);
  110.                         priority.addChatComponentMessage(message);
  111.                     }
  112.                    
  113.                     world.spawnParticle("instantSpell", xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
  114.                 }
  115.             }
  116.            
  117.             this.currentCooldown = this.maxCooldown;
  118.             this.datum -= 1000;
  119.             return;
  120.         }
  121.        
  122.         if (!entities.isEmpty()) {
  123.             Random random = new Random();
  124.             EntityLivingBase target = (EntityLivingBase) entities.get(random.nextInt(entities.size()));
  125.             target.heal(4);
  126.             Minecraft.getMinecraft().thePlayer.sendChatMessage("Successfully found and healed a Creature.");
  127.             this.currentCooldown = this.maxCooldown;
  128.             this.datum -= 1000;
  129.             return;
  130.         }
  131.     }
  132.  
  133.     public void printInfo() {
  134.         // TODO Auto-generated method stub
  135.         Minecraft.getMinecraft().thePlayer.sendChatMessage("Current datum: " + this.datum);
  136.         Minecraft.getMinecraft().thePlayer.sendChatMessage("Current countdown: " + this.currentCooldown);
  137.         Minecraft.getMinecraft().thePlayer.sendChatMessage("Current world Obj (isRemote): " + this.worldObj.isRemote);
  138.     }
  139.  
  140.     @Override
  141.     public void readFromNBT(NBTTagCompound tag) {
  142.         System.out.println("Reading from NBT.");
  143.         super.readFromNBT(tag);
  144.         this.datum = tag.getInteger("Datum");
  145.         this.currentCooldown = tag.getInteger("currentCooldown");
  146.         this.maxCooldown = tag.getInteger("maxCooldown");
  147.     }
  148.  
  149.     @Override
  150.     public void writeToNBT(NBTTagCompound tag) {
  151.         System.out.println("Writing to NBT.");
  152.         super.writeToNBT(tag);
  153.         tag.setInteger("Datum", this.datum);
  154.         tag.setInteger("currentCooldown", this.currentCooldown);
  155.         tag.setInteger("maxCooldown", this.maxCooldown);
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment