Map stuff: First commit (or second?)

This commit is contained in:
libraryaddict 2015-05-08 07:31:03 +12:00
parent f2c8086421
commit 5ec04263d4
5 changed files with 443 additions and 16 deletions

View File

@ -0,0 +1,42 @@
package mineplex.core.map;
import net.minecraft.server.v1_7_R4.WorldMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ChunkMap
{
public ItemStack getItem(Location loc)
{
MapView map = Bukkit.createMap(Bukkit.getWorlds().get(0));
for (MapRenderer r : map.getRenderers())
{
map.removeRenderer(r);
}
ItemStack item = new ItemStack(Material.MAP);
item.setDurability(map.getId());
WorldMap worldMap = new WorldMap("map_" + map.getId());
ChunkMapRenderer renderer = new ChunkMapRenderer(worldMap);
map.addRenderer(renderer);
worldMap.scale=(byte)3;
worldMap.centerX = loc.getBlockX();
worldMap.centerZ = loc.getBlockZ();
worldMap.c();
renderer.setupMap(loc.getWorld());
return item;
}
}

View File

@ -0,0 +1,203 @@
package mineplex.core.map;
import java.util.HashMap;
import java.util.Map.Entry;
import net.minecraft.server.v1_7_R4.Block;
import net.minecraft.server.v1_7_R4.Blocks;
import net.minecraft.server.v1_7_R4.Chunk;
import net.minecraft.server.v1_7_R4.MaterialMapColor;
import net.minecraft.server.v1_7_R4.MathHelper;
import net.minecraft.server.v1_7_R4.WorldMap;
import net.minecraft.util.com.google.common.collect.HashMultiset;
import net.minecraft.util.com.google.common.collect.Iterables;
import net.minecraft.util.com.google.common.collect.Multisets;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ChunkMapRenderer extends MapRenderer
{
private final WorldMap worldmap;
private byte[] colors = new byte[128 * 128 * 128];
public ChunkMapRenderer(WorldMap worldMap)
{
super(false);
this.worldmap = worldMap;
}
public void setupMap(World world)
{
int i = 1 << worldmap.scale;
int j = worldmap.centerX;
int k = worldmap.centerZ;
int l = MathHelper.floor(0) / i + 64;
int i1 = MathHelper.floor(0) / i + 64;
int j1 = 128 / i;
for (int k1 = l - j1 + 1; k1 < l + j1; k1++)
{
int l1 = 255;
int i2 = 0;
double d0 = 0.0D;
for (int j2 = i1 - j1 - 1; j2 < i1 + j1; j2++)
{
if ((k1 >= 0) && (j2 >= -1) && (k1 < 128) && (j2 < 128))
{
int k2 = k1 - l;
int l2 = j2 - i1;
boolean flag = k2 * k2 + l2 * l2 > (j1 - 2) * (j1 - 2);
int i3 = (j / i + k1 - 64) * i;
int j3 = (k / i + j2 - 64) * i;
HashMultiset hashmultiset = HashMultiset.create();
Chunk chunk = ((CraftWorld) world).getHandle().getChunkAtWorldCoords(i3, j3);
if (!chunk.isEmpty())
{
int k3 = i3 & 0xF;
int l3 = j3 & 0xF;
int i4 = 0;
double d1 = 0.0D;
for (int j4 = 0; j4 < i; j4++)
{
for (int k4 = 0; k4 < i; k4++)
{
int l4 = chunk.b(j4 + k3, k4 + l3) + 1;
Block block = Blocks.AIR;
int i5 = 0;
if (l4 > 1)
{
do
{
l4--;
block = chunk.getType(j4 + k3, l4, k4 + l3);
i5 = chunk.getData(j4 + k3, l4, k4 + l3);
}
while ((block.f(i5) == MaterialMapColor.b) && (l4 > 0));
if ((l4 > 0) && (block.getMaterial().isLiquid()))
{
int j5 = l4 - 1;
Block block1;
do
{
block1 = chunk.getType(j4 + k3, j5--, k4 + l3);
i4++;
}
while ((j5 > 0) && (block1.getMaterial().isLiquid()));
}
}
d1 += l4 / (i * i);
hashmultiset.add(block.f(i5));
}
}
i4 /= i * i;
double d2 = (d1 - d0) * 4.0D / (i + 4) + ((k1 + j2 & 0x1) - 0.5D) * 0.4D;
byte b0 = 1;
if (d2 > 0.6D)
{
b0 = 2;
}
if (d2 < -0.6D)
{
b0 = 0;
}
MaterialMapColor materialmapcolor = (MaterialMapColor) Iterables.getFirst(
Multisets.copyHighestCountFirst(hashmultiset), MaterialMapColor.b);
if (materialmapcolor == MaterialMapColor.n)
{
d2 = i4 * 0.1D + (k1 + j2 & 0x1) * 0.2D;
b0 = 1;
if (d2 < 0.5D)
{
b0 = 2;
}
if (d2 > 0.9D)
{
b0 = 0;
}
}
d0 = d1;
if ((j2 >= 0) && (k2 * k2 + l2 * l2 < j1 * j1) && ((!flag) || ((k1 + j2 & 0x1) != 0)))
{
byte b1 = colors[(k1 + j2 * 128)];
byte b2 = (byte) (materialmapcolor.M * 4 + b0);
if (b1 != b2)
{
if (l1 > j2)
{
l1 = j2;
}
if (i2 < j2)
{
i2 = j2;
}
colors[(k1 + j2 * 128)] = b2;
}
}
}
}
}
if (l1 <= i2)
worldmap.flagDirty(k1, l1, i2);
}
}
@Override
public void render(MapView view, MapCanvas canvas, Player player)
{
int scale = 1 << worldmap.scale;
byte i = 30;
HashMap<Entry, Byte> map = new HashMap<Entry, Byte>();
for (int mapX = 0; mapX < 128; mapX++)
{
for (int mapZ = 0; mapZ < 128; mapZ++)
{
byte color = (byte) 0;// worldmap.colors[(z * 128 + x)];
int bX = (worldmap.centerX + ((mapX - 64) * scale)) & 0x000F;
int bZ = (worldmap.centerZ + ((mapZ - 64) * scale)) & 0x000F;
if (bX == 0 || bX == 15 || bZ == 0 || bZ == 15)
{
Entry<Integer, Integer> entry = new HashMap.SimpleEntry((mapX - bX) / 16, (mapZ - bZ) / 16);
if (!map.containsKey(entry))
{
map.put(entry, i++);
}
color = map.get(entry);
}
else
{
color = colors[(mapZ * 128 + mapX)];
}
canvas.setPixel(mapX, mapZ, color);
}
}
}
}

View File

@ -1,6 +1,5 @@
package mineplex.core.map;
import java.awt.Image;
import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
@ -10,21 +9,22 @@ import org.bukkit.map.MapView;
public class ImageMapRenderer extends MapRenderer
{
private Image _image;
private boolean _first = true;
private BufferedImage _image;
private boolean _first = true;
public ImageMapRenderer(BufferedImage image)
{
_image = image;
}
public ImageMapRenderer(BufferedImage image)
{
_image = image;
}
@Override
public void render(MapView view, MapCanvas canvas, Player player)
{
if (_image != null && _first)
{
canvas.drawImage(0, 0, _image);
_first = false;
}
}
@Override
public void render(MapView view, MapCanvas canvas, Player player)
{
if (_image != null && _first)
{
canvas.drawImage(0, 0, _image);
_first = false;
}
}
}

View File

@ -0,0 +1,182 @@
package mineplex.core.map;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.imageio.ImageIO;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class MapText
{
private static HashMap<Character, BufferedImage> _characters = new HashMap<Character, BufferedImage>();
private void loadCharacters()
{
try
{
InputStream inputStream = getClass().getResourceAsStream("ascii.png");
BufferedImage image = ImageIO.read(inputStream);
char[] text = new char[]
{
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
'_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
int x = 0;
int y = 16;
for (char c : text)
{
grab(c, image, x, y);
if (x < 15 * 8)
{
x += 8;
}
else
{
x = 0;
y += 8;
}
}
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void grab(Character character, BufferedImage image, int imageX, int imageY)
{
BufferedImage newImage = image.getSubimage(imageX, imageY, 8, 8);
int width = character == ' ' ? 4 : 0;
if (width == 0)
{
for (int x = 0; x < 8; x++)
{
width++;
boolean foundNonTrans = false;
for (int y = 0; y < 8; y++)
{
int pixel = newImage.getRGB(x, y);
if ((pixel >> 24) != 0x00)
{
foundNonTrans = true;
break;
}
}
if (!foundNonTrans)
{
break;
}
}
}
newImage = newImage.getSubimage(0, 0, width, 8);
_characters.put(character, newImage);
}
private ArrayList<String> split(String text)
{
ArrayList<String> returns = new ArrayList<String>();
int lineWidth = 0;
String current = "";
for (String word : text.split("(?<= )"))
{
int length = 0;
for (char c : word.toCharArray())
{
length += _characters.get(c).getWidth();
}
if (lineWidth + length >= 127)
{
lineWidth = 0;
returns.add(current);
current = "";
}
current += word;
lineWidth += length;
}
returns.add(current);
return returns;
}
public ItemStack getMap(String... text)
{
if (_characters.isEmpty())
{
loadCharacters();
}
BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
int height = 1;
for (String string : text)
{
for (String line : split(string))
{
int length = 1;
for (char c : line.toCharArray())
{
BufferedImage img = _characters.get(c);
if (img == null)
{
System.out.print("Error: '" + c + "' has no image associated");
continue;
}
g.drawImage(img, length, height, null);
length += img.getWidth();
}
height += 8;
}
}
MapView map = Bukkit.createMap(Bukkit.getWorlds().get(0));
for (MapRenderer r : map.getRenderers())
{
map.removeRenderer(r);
}
map.addRenderer(new ImageMapRenderer(image));
ItemStack item = new ItemStack(Material.MAP);
item.setDurability(map.getId());
return item;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB