Guest User

Untitled

a guest
Nov 3rd, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.42 KB | None | 0 0
  1. //TrafficLightBlock
  2. package co.uk.silvania.roads.tileentities.blocks;
  3.  
  4. import java.util.List;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockContainer;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.client.renderer.texture.IconRegister;
  10. import net.minecraft.creativetab.CreativeTabs;
  11. import net.minecraft.entity.EntityLivingBase;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.Icon;
  16. import net.minecraft.util.MathHelper;
  17. import net.minecraft.world.World;
  18. import co.uk.silvania.roads.Roads;
  19. import co.uk.silvania.roads.client.RenderIds;
  20. import co.uk.silvania.roads.tileentities.entities.TileEntityTrafficLightEntity;
  21. import cpw.mods.fml.relauncher.Side;
  22. import cpw.mods.fml.relauncher.SideOnly;
  23.  
  24. public class TileEntityTrafficLightBlock extends BlockContainer {
  25.    
  26.     public static int newMeta;
  27.    
  28.     String powerState = "";
  29.  
  30.     public TileEntityTrafficLightBlock(int id) {
  31.         super(id, Material.iron);
  32.         this.setHardness(1.0F);
  33.         this.setCreativeTab(Roads.tabRoads);
  34.         this.setLightOpacity(0);
  35.         this.setBlockBounds(0.35F, 0.0F, 0.35F, 0.65F, 0.9F, 0.65F);
  36.     }
  37.  
  38.     public void onNeighborBlockChange(World world, int x, int y, int z, int blockId) {
  39.         TileEntityTrafficLightEntity tileEntity = (TileEntityTrafficLightEntity)world.getBlockTileEntity(x, y, z);
  40.         int meta;
  41.         meta = world.getBlockMetadata(x, y, z);
  42.         if(!world.isRemote) {
  43.             if(tileEntity != null) {
  44.                 if (world.isBlockIndirectlyGettingPowered(x, y, z)) {
  45.                     tileEntity.isPowered = true;
  46.                     System.out.println("It's getting powered!");
  47.                     //These printlns report correctly. This one is shown when power is activated to the block
  48.                 } else {
  49.                     tileEntity.isPowered = false;
  50.                     System.out.println("It's not getting powered.");
  51.                     //And this one comes on when the power is removed in any way. So, the physical power isn't the issue...
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57.    
  58.     @Override
  59.     public TileEntity createNewTileEntity(World world) {
  60.         return new TileEntityTrafficLightEntity();
  61.     }
  62.  
  63.     @Override
  64.     public boolean isOpaqueCube() {
  65.         return false;
  66.     }
  67.  
  68.     @Override
  69.     public boolean renderAsNormalBlock() {
  70.         return false;
  71.     }
  72.    
  73.     @Override
  74.     public int getRenderType() {
  75.         return -1;
  76.     }
  77.  
  78.     @Override
  79.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack itemStack) {
  80.         int blockSet = world.getBlockMetadata(x, y, z) / 4;
  81.         int direction = MathHelper.floor_double((double)(entityliving.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3;
  82.         int newMeta = (blockSet * 4) + direction;
  83.         world.setBlockMetadataWithNotify(x, y, z, newMeta, 0);
  84.     }
  85.  
  86.     @SideOnly(Side.CLIENT)
  87.     private Icon red;
  88.     @SideOnly(Side.CLIENT)
  89.     private Icon green;
  90.     @SideOnly(Side.CLIENT)
  91.     private Icon redamber;
  92.     @SideOnly(Side.CLIENT)
  93.     private Icon amber;
  94.  
  95.     @SideOnly(Side.CLIENT)
  96.     public void registerIcons(IconRegister iconRegister) {
  97.         red = iconRegister.registerIcon("Roads:trafficLight0");
  98.         green = iconRegister.registerIcon("Roads:trafficLight4");
  99.         redamber = iconRegister.registerIcon("Roads:trafficLight8");
  100.         amber = iconRegister.registerIcon("Roads:trafficLight12");
  101.     }
  102.  
  103.     @SideOnly(Side.CLIENT)
  104.     public Icon getIcon(int par1, int meta) {
  105.         if (meta == 0) {
  106.             //System.out.println("Red!");
  107.             return red;
  108.         }
  109.         if (meta == 4) {
  110.             //System.out.println("Green!");
  111.             return green;
  112.         }
  113.         if (meta == 8) {
  114.             //System.out.println("Red/Amber!");
  115.             return redamber;
  116.         }
  117.         if (meta == 12) {
  118.             //System.out.println("Amber!!");
  119.             return amber;
  120.         }
  121.         System.out.println("Nothin'!");
  122.         return amber;  
  123.     }
  124.  
  125.     @SideOnly(Side.CLIENT)
  126.     public void getSubBlocks(int par1, CreativeTabs creativeTabs, List list) {
  127.         list.add(new ItemStack(par1, 1, 0));
  128.         list.add(new ItemStack(par1, 1, 4));
  129.         list.add(new ItemStack(par1, 1, 8));
  130.         list.add(new ItemStack(par1, 1, 12));
  131.     }
  132. }
  133.  
  134.  
  135.  
  136. //TrafficLightEntity
  137. package co.uk.silvania.roads.tileentities.entities;
  138.  
  139. import net.minecraft.nbt.NBTTagCompound;
  140. import net.minecraft.tileentity.TileEntity;
  141.  
  142. public class TileEntityTrafficLightEntity extends TileEntity {
  143.  
  144.     public boolean isPowered = false;
  145.    
  146.     public void checkRedstonePower() {
  147.         isPowered = worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
  148.     }
  149.    
  150.     @Override
  151.     public void writeToNBT(NBTTagCompound nbt) {
  152.         super.writeToNBT(nbt);
  153.         nbt.setBoolean("isPowered", isPowered);
  154.         System.out.println("Stored NBT to Tile Entity. Current value: " + isPowered);
  155.     }
  156.    
  157.     @Override
  158.     public void readFromNBT(NBTTagCompound nbt) {
  159.         super.readFromNBT(nbt);
  160.         this.isPowered = nbt.getBoolean("isPowered");
  161.         System.out.println("Reading NBT from Tile Entity. Current value: " + isPowered);
  162.     }
  163.  
  164. }
  165.  
  166.  
  167. //TrafficLightRenderer
  168. package co.uk.silvania.roads.tileentities.renderers;
  169.  
  170. import net.minecraft.client.Minecraft;
  171. import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
  172. import net.minecraft.entity.Entity;
  173. import net.minecraft.tileentity.TileEntity;
  174. import net.minecraft.util.ResourceLocation;
  175. import net.minecraftforge.common.ForgeDirection;
  176.  
  177. import org.lwjgl.opengl.GL11;
  178.  
  179. import co.uk.silvania.roads.client.models.RoadSlopeModel;
  180. import co.uk.silvania.roads.client.models.TrafficLightModel;
  181. import co.uk.silvania.roads.tileentities.blocks.TileEntityTrafficLightBlock;
  182. import co.uk.silvania.roads.tileentities.entities.TileEntityTrafficLightEntity;
  183.  
  184.  
  185. public class TileEntityTrafficLightRenderer extends TileEntitySpecialRenderer {
  186.  
  187.     private TrafficLightModel model;
  188.  
  189.     public TileEntityTrafficLightRenderer() {
  190.         model = new TrafficLightModel();
  191.     }
  192.  
  193.     @Override
  194.     public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {
  195.         int rotation = 180;
  196.         switch (te.getBlockMetadata() % 4) {
  197.             case 0:
  198.                 rotation = 0;
  199.                 break;
  200.             case 3:
  201.                 rotation = 90;
  202.                 break;
  203.             case 2:
  204.                 rotation = 180;
  205.                 break;
  206.             case 1:
  207.                 rotation = 270;
  208.                 break;
  209.         }
  210.  
  211.         GL11.glPushMatrix();
  212.         int i = te.getBlockMetadata();
  213.        
  214.         TileEntityTrafficLightEntity TileEntityStoplight = (TileEntityTrafficLightEntity) te;
  215.         if (((TileEntityTrafficLightEntity) te).isPowered == false) {
  216.             Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation("roads", "textures/entities/TrafficLightGreen.png"));
  217.         }
  218.         else if (((TileEntityTrafficLightEntity) te).isPowered == true) {
  219.             Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation("roads", "textures/entities/TrafficLightRed.png"));
  220.         } else
  221.             System.out.println("Time to cry in the corner.");
  222.            
  223.         GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
  224.         GL11.glRotatef(rotation, 0.0F, 1.0F, 0.0F);
  225.         GL11.glScalef(1.0F, -1F, -1F);
  226.         model.render((Entity) null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
  227.         GL11.glPopMatrix();
  228.         //}
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment