Advertisement
Brickfix

TileEntityTotemBlock.java

Mar 10th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. package com.brickfix.totemmod.blocks;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.command.CommandBase;
  6. import net.minecraft.command.NumberInvalidException;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.effect.EntityLightningBolt;
  9. import net.minecraft.entity.item.EntityFireworkRocket;
  10. import net.minecraft.init.Blocks;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.nbt.NBTTagList;
  16. import net.minecraft.network.NetworkManager;
  17. import net.minecraft.network.Packet;
  18. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  19. import net.minecraft.server.gui.IUpdatePlayerListBox;
  20. import net.minecraft.tileentity.TileEntity;
  21. import net.minecraft.util.BlockPos;
  22. import net.minecraft.util.EnumParticleTypes;
  23. import net.minecraft.util.Vec3;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.fml.relauncher.Side;
  26. import net.minecraftforge.fml.relauncher.SideOnly;
  27.  
  28. import com.brickfix.totemmod.TotemBlockLogic;
  29. import com.brickfix.totemmod.TotemMod;
  30. import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
  31.  
  32. public class TileEntityTotemBlock extends TileEntity implements IUpdatePlayerListBox
  33. {
  34.     public int cooldown;
  35.     public boolean isCharged;
  36.    
  37.     private final TotemBlockLogic totemLogic = new TotemBlockLogic()
  38.     {
  39.         @Override
  40.         public String getCommand()
  41.         {
  42.             return "/kill @e[type=Skeleton,r=5]";
  43.         }
  44.        
  45.         @Override
  46.         public BlockPos getPosition()
  47.         {
  48.             return TileEntityTotemBlock.this.pos;
  49.         }
  50.  
  51.         @Override
  52.         public Vec3 getPositionVector()
  53.         {
  54.             return new Vec3((double)TileEntityTotemBlock.this.pos.getX() + 0.5D, (double)TileEntityTotemBlock.this.pos.getY() + 0.5D, (double)TileEntityTotemBlock.this.pos.getZ() + 0.5D);
  55.         }
  56.  
  57.         @Override
  58.         public World getEntityWorld()
  59.         {
  60.             return TileEntityTotemBlock.this.worldObj;
  61.         }
  62.        
  63.     };
  64.    
  65.     public int getCooldown()
  66.     {
  67.         return 100;
  68.     }
  69.    
  70.     public TotemBlockLogic getTotemLogic()
  71.     {
  72.         return this.totemLogic;
  73.     }
  74.    
  75.     public void writeToNBT(NBTTagCompound compound)
  76.     {
  77.         super.writeToNBT(compound);
  78.         compound.setInteger("Cooldown", this.cooldown);
  79.         compound.setBoolean("Charged", this.isCharged);
  80.        
  81.     }
  82.     public void readFromNBT(NBTTagCompound compound)
  83.     {
  84.         super.readFromNBT(compound);
  85.         this.cooldown = compound.getInteger("Cooldown");
  86.         this.isCharged = compound.getBoolean("Charged");
  87.     }
  88.    
  89.     static
  90.     {
  91.         //addMapping(TileEntityTotemBlock.class, "TotemBlock");
  92.     }
  93.    
  94.     @Override
  95.     public Packet getDescriptionPacket()
  96.     {
  97.         NBTTagCompound nbtTagCompound = new NBTTagCompound();
  98.         writeToNBT(nbtTagCompound);
  99.         int metadata = getBlockMetadata();
  100.         return new S35PacketUpdateTileEntity(this.pos, metadata, nbtTagCompound);
  101.     }
  102.  
  103.     @Override
  104.     public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
  105.     {
  106.         readFromNBT(pkt.getNbtCompound());
  107.     }
  108.    
  109.     public void invalidate()
  110.     {
  111.         this.updateContainingBlockInfo();
  112.         super.invalidate();
  113.     }
  114.    
  115.     public void update()
  116.     {
  117.         if (this.worldObj.isRemote)
  118.         {
  119.             //System.out.println("Updated ClientSide");
  120.             return;
  121.         }
  122.        
  123.         this.isCharged = isCharged();
  124.         //System.out.println("Updated ServerSide");
  125.         if (this.cooldown==0)
  126.         {
  127.             int i=0;
  128.            
  129.             if (this.isCharged)
  130.             {
  131.                 i = totemLogic.trigger(this.worldObj);
  132.             }
  133.             if (i!=0)
  134.             {
  135.                 this.cooldown = this.getCooldown();
  136.                 Item rm = Items.fireworks;
  137.                
  138.                 NBTTagCompound tag0 = new NBTTagCompound();
  139.                 NBTTagList explosions = new NBTTagList();
  140.                 NBTTagCompound tag1 = new NBTTagCompound();
  141.                 NBTTagCompound tag2 = new NBTTagCompound();
  142.                
  143.                 int[] array = new int[1];
  144.                 array[0] = 65536*230;
  145.                
  146.                 tag2.setIntArray("Colors", array);
  147.                 tag2.setBoolean("Flicker", true);
  148.                
  149.                 explosions.appendTag(tag2);
  150.                
  151.                 tag1.setTag("Explosions", explosions);
  152.                 tag1.setByte("Flight", (byte) 0);
  153.                
  154.                 tag0.setTag("Fireworks", tag1);
  155.                
  156.                
  157.                 ItemStack modifier = new ItemStack(rm);
  158.                
  159.                 modifier.setTagCompound(tag0);
  160.                
  161.                 Entity effect = new EntityFireworkRocket(this.worldObj, pos.getX()+0.5D, pos.getY()+0.5D, pos.getZ()+0.5D, modifier);
  162.                
  163.                 this.worldObj.spawnEntityInWorld(effect);
  164.                
  165.             }
  166.            
  167.            
  168.         }
  169.         else if (this.cooldown > 0)
  170.         {
  171.             this.cooldown--;
  172.         }      
  173.     }
  174.    
  175.     public boolean isCharged()
  176.     {
  177.         boolean flag = this.isCharged;
  178.         boolean flag2 = false;
  179.         BlockPos pos = this.pos;
  180.         if ((this.worldObj.getBlockState(pos.up()) == Blocks.obsidian.getDefaultState()) && (this.worldObj.getBlockState(pos.down()) == Blocks.obsidian.getDefaultState()))
  181.         {
  182.             flag2 = true;
  183.         }
  184.        
  185.         if (flag==false && flag2==true)
  186.         {
  187.             this.lightningStrikes(worldObj, pos);
  188.         }
  189.        
  190.         return flag2;
  191.     }
  192.    
  193.     protected void lightningStrikes(World world, BlockPos pos)
  194.     {
  195.        
  196.         double dx = (double)pos.getX();
  197.         double dy = (double)pos.getY();
  198.         double dz = (double)pos.getZ();
  199.        
  200.         String sx = String.valueOf(pos.getX());
  201.         String sy = String.valueOf(pos.getY());
  202.         String sz = String.valueOf(pos.getZ());
  203.        
  204.         try
  205.         {
  206.             dx = CommandBase.func_175761_b(dx, sx, true);
  207.             dy = CommandBase.func_175761_b(dy, sy, false);
  208.             dz = CommandBase.func_175761_b(dz, sz, true);
  209.         }
  210.         catch (NumberInvalidException e)
  211.         {
  212.             e.printStackTrace();
  213.             return;
  214.         }
  215.        
  216.        
  217.         world.addWeatherEffect(new EntityLightningBolt(world, dx, dy, dz));
  218.     }
  219.    
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement