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.
Basic stuff you need: Update Player chunk @ Player Code:java public static void updateChunk(Player player) { CraftWorld cWorld = (CraftWorld)player.getWorld(); EntityPlayer ep = ((CraftPlayer)player).getHandle(); Chunk c = cWorld.getChunkAt(player.getLocation()); ChunkCoordIntPair coord0 = new ChunkCoordIntPair(c.getX(), c.getZ()); ep.chunkCoordIntPairQueue.add(0, coord0); } Update chunk @ player get location Code:java public static void updateChunk(Player player, Location location) { CraftWorld cWorld = (CraftWorld)player.getWorld(); EntityPlayer ep = ((CraftPlayer)player).getHandle(); Chunk c = cWorld.getChunkAt(location); ChunkCoordIntPair coord = new ChunkCoordIntPair(c.getX(), c.getZ()); ep.chunkCoordIntPairQueue.add(0, coord); } Create Light source @ Playerlocation Code:java public static void createLightSource(Location toPlayerLocation, Player player) { CraftWorld cWorld = (CraftWorld)toPlayerLocation.getWorld(); int xNew = toPlayerLocation.getBlockX(); int yNew = toPlayerLocation.getBlockY() + 2; int zNew = toPlayerLocation.getBlockZ(); int lightLevel = 15; cWorld.getHandle().a(EnumSkyBlock.BLOCK, xNew, yNew, zNew, lightLevel); Location newSource = new Location(cWorld, xNew, yNew - 1, zNew); Material blockMaterial = newSource.getBlock().getType(); byte blockData = newSource.getBlock().getData(); newSource.getBlock().setType(blockMaterial); newSource.getBlock().setData(blockData); updateChunk(player); } create light source @ player Code:java public static void createLightSource(Player player) { CraftWorld cWorld = (CraftWorld)player.getWorld(); Location playerLocation = player.getLocation().getBlock().getLocation(); int xNew = playerLocation.getBlockX(); int yNew = playerLocation.getBlockY() + 2; int zNew = playerLocation.getBlockZ(); int lightLevel = 15; cWorld.getHandle().a(EnumSkyBlock.BLOCK, xNew, yNew, zNew, lightLevel); Location newSource = new Location(cWorld, xNew, yNew - 1, zNew); Material blockMaterial = newSource.getBlock().getType(); byte blockData = newSource.getBlock().getData(); newSource.getBlock().setType(blockMaterial); newSource.getBlock().setData(blockData); updateChunk(player); } Delete LIght source from Location got from Player Code:java public static void deleteLightSource(Location fromPlayerLocation, Player player) { CraftWorld cWorld = (CraftWorld)fromPlayerLocation.getWorld(); int xPrevious = fromPlayerLocation.getBlockX(); int yPrevious = fromPlayerLocation.getBlockY() + 2; int zPrevious = fromPlayerLocation.getBlockZ(); Location previousSource = new Location(cWorld, xPrevious, yPrevious, zPrevious); Material blockMaterial = previousSource.getBlock().getType(); byte blockData = previousSource.getBlock().getData(); previousSource.getBlock().setType(blockMaterial); previousSource.getBlock().setData(blockData); updateChunk(player); }
Notice that if you get errors, it's because I abbreviated CraftWorld and such. For that just do craftworld craftworld = new cworld I think. i'm java noob
Okay so quick question. To create an effective headlamp. I need to delete and redraw the light everytime the player moves?
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
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.