Last active
August 29, 2015 14:26
-
-
Save xaniox/660ab3e75a026475313a to your computer and use it in GitHub Desktop.
A simple class which makes use of the LightAPI by EvilSpawn and spawns light sources when the player holds a torch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.matzefratze123.torchlight; | |
import java.lang.ref.WeakReference; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import ru.BeYkeRYkt.LightAPI.LightAPI; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
public class LightTask extends BukkitRunnable { | |
//Max = 15, Min = 1 | |
private static final int LIGHT_LEVEL = 15; | |
private Map<WeakReference<Player>, Location> recentLightLocations; | |
private List<Location> spawn; | |
private List<Location> delete; | |
private boolean running; | |
public LightTask() { | |
this.recentLightLocations = Maps.newHashMap(); | |
this.spawn = Lists.newArrayList(); | |
this.delete = Lists.newArrayList(); | |
} | |
public void startTask(Plugin plugin) { | |
if (running) { | |
throw new IllegalStateException("Task already running!"); | |
} | |
//Higher this if running into performance issues | |
runTaskTimer(plugin, 3L, 3L); | |
running = true; | |
} | |
public void run() { | |
spawn.clear(); | |
delete.clear(); | |
for (Player player : Bukkit.getOnlinePlayers()) { | |
ItemStack inHand = player.getItemInHand(); | |
WeakReference<Player> ref = null; | |
Location recent = null; | |
Iterator<Entry<WeakReference<Player>, Location>> iterator = recentLightLocations.entrySet().iterator(); | |
while (iterator.hasNext()) { | |
Entry<WeakReference<Player>, Location> entry = iterator.next(); | |
WeakReference<Player> keyRef = entry.getKey(); | |
Player refPlayer = keyRef.get(); | |
if (refPlayer != player) { | |
if (refPlayer == null) { | |
iterator.remove(); | |
} | |
continue; | |
} | |
recent = entry.getValue(); | |
ref = keyRef; | |
break; | |
} | |
if (inHand.getType() != Material.TORCH) { | |
if (recent != null) { | |
delete.add(recent); | |
} | |
continue; | |
} | |
Location now = player.getLocation(); | |
if (recent != null) { | |
if (locationBlockEquals(recent, now)) { | |
continue; | |
} else { | |
delete.add(recent); | |
} | |
} | |
spawn.add(now); | |
recentLightLocations.put(ref == null ? new WeakReference<Player>(player) : ref, now); | |
} | |
//It is extremely important to delete first | |
//otherwise light bugs may occur | |
if (!delete.isEmpty()) { | |
LightAPI.deleteLight(delete); | |
} | |
if (!spawn.isEmpty()) { | |
LightAPI.createLight(spawn, LIGHT_LEVEL); | |
} | |
} | |
private boolean locationBlockEquals(Location loc, Location other) { | |
return loc.getBlockX() == other.getBlockX() | |
&& loc.getBlockY() == other.getBlockY() | |
&& loc.getBlockZ() == other.getBlockZ(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment