下面列出了怎么用org.bukkit.map.MapCanvas的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
// Map
for (int x = 0; x < 128; ++x) {
for (int y = 0; y < 128; ++y) {
canvas.setPixel(x, y, worldMap.colors[y * 128 + x]);
}
}
// Cursors
MapCursorCollection cursors = canvas.getCursors();
while (cursors.size() > 0) {
cursors.removeCursor(cursors.getCursor(0));
}
for (Object key : worldMap.mapDecorations.keySet()) {
// If this cursor is for a player check visibility with vanish system
Player other = Bukkit.getPlayerExact((String) key);
if (other != null && !player.canSee(other)) {
continue;
}
MapDecoration decoration = worldMap.mapDecorations.get(key);
cursors.addCursor(decoration.getX(), decoration.getY(), (byte) (decoration.getRotation() & 15), decoration.getImage());
}
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
int threadCount = threadBean.getThreadCount();
int daemonCount = threadBean.getDaemonThreadCount();
//round up to the nearest multiple of 5
int roundedMax = (int) (5 * (Math.ceil((float) threadCount / 5)));
int threadHeight = getHeightScaled(roundedMax, threadCount);
int daemonHeight = getHeightScaled(roundedMax, daemonCount);
fillBar(canvas, nextPosX, MAX_HEIGHT - threadHeight, MAX_COLOR);
fillBar(canvas, nextPosX, MAX_HEIGHT - daemonHeight, USED_COLOR);
//these is the max number of all threads
return threadCount;
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
for (int i = 0; i < graphRenderers.length; i++) {
GraphRenderer graphRenderer = graphRenderers[i];
int position = this.componentLastPos[i];
position++;
//index starts with 0 so in the end - 1
int maxComponentWidth = (i + 1) * componentWidth + i * SPACES - 1;
if (position > maxComponentWidth) {
//reset it to the start pos
position = i * componentWidth + i * SPACES;
}
graphRenderer.renderGraphTick(canvas, position);
this.componentLastPos[i] = position;
}
return 100;
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
//byte -> mega byte
int max = (int) (heapUsage.getHeapMemoryUsage().getCommitted() / 1024 / 1024);
int used = (int) (heapUsage.getHeapMemoryUsage().getUsed() / 1024 / 1024);
//round to the next 100 e.g. 801 -> 900
int roundedMax = ((max + 99) / 100) * 100;
int maxHeight = getHeightScaled(roundedMax, max);
int usedHeight = getHeightScaled(roundedMax, used);
//x=0 y=0 is the left top point so convert it
int convertedMaxHeight = MAX_HEIGHT - maxHeight;
int convertedUsedHeight = MAX_HEIGHT - usedHeight;
fillBar(canvas, nextPosX, convertedMaxHeight, MAX_COLOR);
fillBar(canvas, nextPosX, convertedUsedHeight, USED_COLOR);
return maxHeight;
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
int threadCount = threadBean.getThreadCount();
int daemonCount = threadBean.getDaemonThreadCount();
//round up to the nearest multiple of 5
int roundedMax = (int) (5 * (Math.ceil((float) threadCount / 5)));
int threadHeight = getHeightScaled(roundedMax, threadCount);
int daemonHeight = getHeightScaled(roundedMax, daemonCount);
fillBar(canvas, nextPosX, MAX_HEIGHT - threadHeight, MAX_COLOR);
fillBar(canvas, nextPosX, MAX_HEIGHT - daemonHeight, USED_COLOR);
//these is the max number of all threads
return threadCount;
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
for (int i = 0; i < graphRenderers.length; i++) {
GraphRenderer graphRenderer = graphRenderers[i];
int position = this.componentLastPos[i];
position++;
//index starts with 0 so in the end - 1
int maxComponentWidth = (i + 1) * componentWidth + i * SPACES - 1;
if (position > maxComponentWidth) {
//reset it to the start pos
position = i * componentWidth + i * SPACES;
}
graphRenderer.renderGraphTick(canvas, position);
this.componentLastPos[i] = position;
}
return 100;
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
//byte -> mega byte
int max = (int) (heapUsage.getHeapMemoryUsage().getCommitted() / 1024 / 1024);
int used = (int) (heapUsage.getHeapMemoryUsage().getUsed() / 1024 / 1024);
//round to the next 100 e.g. 801 -> 900
int roundedMax = ((max + 99) / 100) * 100;
int maxHeight = getHeightScaled(roundedMax, max);
int usedHeight = getHeightScaled(roundedMax, used);
//x=0 y=0 is the left top point so convert it
int convertedMaxHeight = MAX_HEIGHT - maxHeight;
int convertedUsedHeight = MAX_HEIGHT - usedHeight;
fillBar(canvas, nextPosX, convertedMaxHeight, MAX_COLOR);
fillBar(canvas, nextPosX, convertedUsedHeight, USED_COLOR);
return maxHeight;
}
@Override
public void render(MapView mapView, MapCanvas mapCanvas, Player player) {
for (int x = 0; x < 128; x++) {
for (int z = 0; z < 128; z++) {
mapCanvas.setPixel(x, z, bitMatrix.get(x, z) ? FILL_COLOR : MapPalette.WHITE);
}
}
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
int loadedClasses = classBean.getLoadedClassCount();
//round up to the nearest multiple of 5
int roundedMax = (int) (5 * (Math.ceil((float) loadedClasses / 5)));
int loadedHeight = getHeightScaled(roundedMax, loadedClasses);
fillBar(canvas, nextPosX, MAX_HEIGHT - loadedHeight, MAX_COLOR);
//these is the max number
return loadedClasses;
}
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
if (nextUpdate <= 0) {
//paint only every half seconds (20 Ticks / 2)
nextUpdate = 10;
if (nextPosX >= MAX_WIDTH) {
//start again from the beginning
nextPosX = 0;
}
clearBar(canvas, nextPosX);
//make it more visual where the renderer is at the moment
clearBar(canvas, nextPosX + 1);
int maxValue = renderGraphTick(canvas, nextPosX);
//override the color
drawText(canvas, MAX_WIDTH / 2, MAX_HEIGHT / 2, title);
//count indicators
String maxText = Integer.toString(maxValue);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(maxText), 2), TEXT_HEIGHT, maxText);
String midText = Integer.toString(maxValue / 2);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(midText), 2), MAX_HEIGHT / 2, midText);
String zeroText = Integer.toString(0);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(zeroText), 2), MAX_HEIGHT, zeroText);
nextPosX++;
}
nextUpdate--;
}
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
if(CauldronCommand.debug) {
System.out.println("Default Map Render called!");
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste);
} }
// Map
for (int x = 0; x < 128; ++x) {
for (int y = 0; y < 128; ++y) {
canvas.setPixel(x, y, worldMap.colors[y * 128 + x]);
}
}
// Cursors
MapCursorCollection cursors = canvas.getCursors();
while (cursors.size() > 0) {
cursors.removeCursor(cursors.getCursor(0));
}
for (UUID key : worldMap.playersVisibleOnMap.keySet()) { // Spigot string -> uuid
// If this cursor is for a player check visibility with vanish system
Player other = Bukkit.getPlayer(key); // Spigot
if (other != null && !player.canSee(other)) {
continue;
}
net.minecraft.world.storage.MapData.MapCoord decoration = (net.minecraft.world.storage.MapData.MapCoord) worldMap.playersVisibleOnMap.get(key);
cursors.addCursor(decoration.centerX, decoration.centerZ, (byte) (decoration.iconRotation & 15), decoration.iconSize);
}
}
@Override
public int renderGraphTick(MapCanvas canvas, int nextPosX) {
int loadedClasses = classBean.getLoadedClassCount();
//round up to the nearest multiple of 5
int roundedMax = (int) (5 * (Math.ceil((float) loadedClasses / 5)));
int loadedHeight = getHeightScaled(roundedMax, loadedClasses);
fillBar(canvas, nextPosX, MAX_HEIGHT - loadedHeight, MAX_COLOR);
//these is the max number
return loadedClasses;
}
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
if (nextUpdate <= 0) {
//paint only every half seconds (20 Ticks / 2)
nextUpdate = 10;
if (nextPosX >= MAX_WIDTH) {
//start again from the beginning
nextPosX = 0;
}
clearBar(canvas, nextPosX);
//make it more visual where the renderer is at the moment
clearBar(canvas, nextPosX + 1);
int maxValue = renderGraphTick(canvas, nextPosX);
//override the color
drawText(canvas, MAX_WIDTH / 2, MAX_HEIGHT / 2, title);
//count indicators
String maxText = Integer.toString(maxValue);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(maxText), 2), TEXT_HEIGHT, maxText);
String midText = Integer.toString(maxValue / 2);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(midText), 2), MAX_HEIGHT / 2, midText);
String zeroText = Integer.toString(0);
drawText(canvas, MAX_WIDTH - Math.floorDiv(getTextWidth(zeroText), 2), MAX_HEIGHT, zeroText);
nextPosX++;
}
nextUpdate--;
}
protected void clearBar(MapCanvas canvas, int posX) {
//resets the complete y coordinates on this x in order to free unused
for (int yPos = 0; yPos < MAX_HEIGHT; yPos++) {
canvas.setPixel(posX, yPos, (byte) 0);
}
}
protected void clearMap(MapCanvas canvas) {
for (int xPos = 0; xPos < MAX_WIDTH; xPos++) {
fillBar(canvas, xPos, 0, (byte) 0);
}
}
protected void fillBar(MapCanvas canvas, int xPos, int yStart, byte color) {
for (int yPos = yStart; yPos < MAX_HEIGHT; yPos++) {
canvas.setPixel(xPos, yPos, color);
}
}
protected void drawText(MapCanvas canvas, int midX, int midY, String text) {
int textWidth = getTextWidth(text);
canvas.drawText(midX - (textWidth / 2), midY - (TEXT_HEIGHT / 2), MinecraftFont.Font, text);
}
protected void clearBar(MapCanvas canvas, int posX) {
//resets the complete y coordinates on this x in order to free unused
for (int yPos = 0; yPos < MAX_HEIGHT; yPos++) {
canvas.setPixel(posX, yPos, (byte) 0);
}
}
protected void clearMap(MapCanvas canvas) {
for (int xPos = 0; xPos < MAX_WIDTH; xPos++) {
fillBar(canvas, xPos, 0, (byte) 0);
}
}
protected void fillBar(MapCanvas canvas, int xPos, int yStart, byte color) {
for (int yPos = yStart; yPos < MAX_HEIGHT; yPos++) {
canvas.setPixel(xPos, yPos, color);
}
}
protected void drawText(MapCanvas canvas, int midX, int midY, String text) {
int textWidth = getTextWidth(text);
canvas.drawText(midX - (textWidth / 2), midY - (TEXT_HEIGHT / 2), MinecraftFont.Font, text);
}
public abstract int renderGraphTick(MapCanvas canvas, int nextPosX);
public abstract int renderGraphTick(MapCanvas canvas, int nextPosX);