Lights

Discussion in 'Plugin Development' started by kmccmk9, May 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    kmccmk9

    Hello all,

    I am trying to make a portable lamp (head lamp). Looking at some old plugins' source code I have determined the only way to simulate this effect is to change the light level of the area around the player every time they move. Can someone shed a little more light on exactly what I need to do? I understand the theory but maybe some code could help. I'm not asking for complete help I just need a little help registering the player listener for event type move and how to actually change the light level. Thank you.
     
  2. Offline

    ZeusAllMighty11

    Basic stuff you need:


    Update Player chunk @ Player
    Code:java
    1.  
    2. public static void updateChunk(Player player)
    3. {
    4. CraftWorld cWorld = (CraftWorld)player.getWorld();
    5. EntityPlayer ep = ((CraftPlayer)player).getHandle();
    6.  
    7. Chunk c = cWorld.getChunkAt(player.getLocation());
    8. ChunkCoordIntPair coord0 = new ChunkCoordIntPair(c.getX(), c.getZ());
    9.  
    10. ep.chunkCoordIntPairQueue.add(0, coord0);
    11. }
    12.  

    Update chunk @ player get location
    Code:java
    1.  
    2.  
    3. public static void updateChunk(Player player, Location location)
    4. {
    5. CraftWorld cWorld = (CraftWorld)player.getWorld();
    6. EntityPlayer ep = ((CraftPlayer)player).getHandle();
    7. Chunk c = cWorld.getChunkAt(location);
    8. ChunkCoordIntPair coord = new ChunkCoordIntPair(c.getX(), c.getZ());
    9. ep.chunkCoordIntPairQueue.add(0, coord);
    10. }
    11.  

    Create Light source @ Playerlocation
    Code:java
    1.  
    2.  
    3. public static void createLightSource(Location toPlayerLocation, Player player)
    4. {
    5. CraftWorld cWorld = (CraftWorld)toPlayerLocation.getWorld();
    6.  
    7. int xNew = toPlayerLocation.getBlockX();
    8. int yNew = toPlayerLocation.getBlockY() + 2;
    9. int zNew = toPlayerLocation.getBlockZ();
    10.  
    11. int lightLevel = 15;
    12.  
    13. cWorld.getHandle().a(EnumSkyBlock.BLOCK, xNew, yNew, zNew, lightLevel);
    14.  
    15. Location newSource = new Location(cWorld, xNew, yNew - 1, zNew);
    16. Material blockMaterial = newSource.getBlock().getType();
    17. byte blockData = newSource.getBlock().getData();
    18. newSource.getBlock().setType(blockMaterial);
    19. newSource.getBlock().setData(blockData);
    20. updateChunk(player);
    21. }
    22.  

    create light source @ player
    Code:java
    1.  
    2.  
    3. public static void createLightSource(Player player)
    4. {
    5. CraftWorld cWorld = (CraftWorld)player.getWorld();
    6. Location playerLocation = player.getLocation().getBlock().getLocation();
    7.  
    8. int xNew = playerLocation.getBlockX();
    9. int yNew = playerLocation.getBlockY() + 2;
    10. int zNew = playerLocation.getBlockZ();
    11.  
    12. int lightLevel = 15;
    13.  
    14. cWorld.getHandle().a(EnumSkyBlock.BLOCK, xNew, yNew, zNew, lightLevel);
    15.  
    16. Location newSource = new Location(cWorld, xNew, yNew - 1, zNew);
    17. Material blockMaterial = newSource.getBlock().getType();
    18. byte blockData = newSource.getBlock().getData();
    19. newSource.getBlock().setType(blockMaterial);
    20. newSource.getBlock().setData(blockData);
    21. updateChunk(player);
    22. }
    23.  

    Delete LIght source from Location got from Player
    Code:java
    1.  
    2.  
    3. public static void deleteLightSource(Location fromPlayerLocation, Player player)
    4. {
    5. CraftWorld cWorld = (CraftWorld)fromPlayerLocation.getWorld();
    6.  
    7. int xPrevious = fromPlayerLocation.getBlockX();
    8. int yPrevious = fromPlayerLocation.getBlockY() + 2;
    9. int zPrevious = fromPlayerLocation.getBlockZ();
    10.  
    11. Location previousSource = new Location(cWorld, xPrevious, yPrevious, zPrevious);
    12. Material blockMaterial = previousSource.getBlock().getType();
    13. byte blockData = previousSource.getBlock().getData();
    14. previousSource.getBlock().setType(blockMaterial);
    15. previousSource.getBlock().setData(blockData);
    16. updateChunk(player);
    17. }
    18.  
     
    kmccmk9 likes this.
  3. Offline

    kmccmk9

    That was very helpful. Thank you very much!
     
  4. Offline

    ZeusAllMighty11


    Notice that if you get errors, it's because I abbreviated CraftWorld and such. :p
    For that just do
    craftworld craftworld = new cworld

    I think. :p
    i'm java noob
     
    kmccmk9 likes this.
  5. Offline

    kmccmk9

    Okay so quick question. To create an effective headlamp. I need to delete and redraw the light everytime the player moves?
     
  6. Offline

    ZeusAllMighty11

    I guess that's one way, or you can loop it and make it change while player moves so you don't have an infinite light source
     
  7. Offline

    kmccmk9

    What do you mean loop? What would I be looping the creation, deletion or both?
     
  8. Offline

    ZeusAllMighty11

    The creation, while checking for block update to see if the light source is in the radius.


    Just do it your way, it may work better.
     
    kmccmk9 likes this.
  9. Offline

    kmccmk9

    Okay I'll work on it over the weekend and upcoming week. Thanks for your help.
     
Thread Status:
Not open for further replies.

Share This Page