Created
May 15, 2014 07:20
-
-
Save macalinao/391918907c04761477f1 to your computer and use it in GitHub Desktop.
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package net.og_mc.mattsponge; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import java.util.logging.Level; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Chunk; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.block.Block; | |
import org.bukkit.craftbukkit.v1_7_R3.CraftChunk; | |
import org.bukkit.craftbukkit.v1_7_R3.util.CraftMagicNumbers; | |
/** | |
* | |
* @author ian | |
*/ | |
public class TrackableBlockCache { | |
public MattSponge plugin; | |
public Set<String> chunksCheckedCache; | |
public Map<TrackerMode, Set<Block>> blocks; | |
public TrackableBlockCache(MattSponge plugin) { | |
this.plugin = plugin; | |
} | |
public void load() { | |
plugin.getLogger().log(Level.INFO, "Populating the initial block data cache, please wait..."); | |
chunksCheckedCache = new HashSet<>(); | |
blocks = new HashMap<>(); | |
for (World w : Bukkit.getWorlds()) { | |
for (Chunk c : w.getLoadedChunks()) { | |
cacheChunkTbs(c); | |
} | |
} | |
} | |
public void save() { | |
// todo | |
} | |
public void addBlock(TrackerMode tm, Block b) { | |
getBlocks(tm).add(b); | |
} | |
public Set<Block> getBlocks(TrackerMode tm) { | |
Set<Block> tBlocks = blocks.get(tm); | |
if (tBlocks == null) { | |
tBlocks = new HashSet<>(); | |
blocks.put(tm, tBlocks); | |
} | |
return tBlocks; | |
} | |
public Set<Block> findNearbyBlocks(final Location l, TrackerMode m, int radius) { | |
Set<Block> found = new HashSet<>(); | |
World w = l.getWorld(); | |
int radSq = radius * radius; | |
Set<Block> all = getBlocks(m); | |
finder: | |
for (Block b : all) { | |
if (!w.equals(b.getWorld())) { | |
continue; | |
} | |
for (Block fd : found) { | |
if (fd.getChunk().equals(b.getChunk())) { | |
continue finder; | |
} | |
} | |
if (b.getLocation().distanceSquared(l) < radSq) { | |
found.add(b); | |
} | |
} | |
Set<Block> sorted = new TreeSet<>(new Comparator<Block>() { | |
@Override | |
public int compare(Block a, Block b) { | |
return (int) (l.distanceSquared(a.getLocation()) - l.distanceSquared(b.getLocation())); | |
} | |
}); | |
sorted.addAll(found); | |
return sorted; | |
} | |
public void cacheChunkTbs(Chunk c) { | |
// plugin.getLogger().log(Level.INFO, "Caching block data for chunk {" + c.getWorld().getName() + ": " + c.getX() + ", " + c.getZ() + "}"); | |
if (!chunksCheckedCache.contains(getChunkId(c))) { | |
forceCacheChunkTbs(c); | |
} | |
} | |
/** | |
* Cache chunk trackable blocks | |
* | |
* @param c | |
*/ | |
public void forceCacheChunkTbs(Chunk c) { | |
for (int x = 0; x < 16; x++) { | |
for (int y = 0; y < 256; y++) { | |
for (int z = 0; z < 16; z++) { | |
int matId = CraftMagicNumbers.getId(((CraftChunk) c).getHandle().getType(x, y, z)); | |
TrackerMode tm = TrackerModes.modesById.get(matId); | |
if (tm != null) { | |
addBlock(tm, c.getBlock(x, y, z)); | |
} | |
} | |
} | |
} | |
chunksCheckedCache.add(getChunkId(c)); | |
} | |
public static final String getChunkId(Chunk c) { | |
return c.getWorld().getName() + (((c.getX() & 0xffff) << 8) | (c.getZ() & 0xffff)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment