下面列出了org.bukkit.inventory.meta.MapMeta#org.bukkit.map.MapView 实例代码,或者点击链接到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());
}
}
private void sendAndRenderMap(User u, Player p, String newKey) {
ImageMapRenderer mapRenderer;
try {
mapRenderer = new ImageMapRenderer(p.getName(), newKey, serverIp);
} catch (WriterException e) {
mcAuthenticator.getC().sendDirect(p,
"&cThere was an error rendering your 2FA QR code!");
mcAuthenticator.handleException(e);
return;
}
if (!u.isInventoryStored())
u.storeInventory(p);
MapView map = Bukkit.createMap(p.getWorld());
map.getRenderers().forEach(map::removeRenderer);
map.addRenderer(mapRenderer);
ItemStack mapItem = new ItemStack(Material.MAP, 1, map.getId());
p.getInventory().setHeldItemSlot(0);
p.getInventory().setItemInMainHand(mapItem);
p.sendMap(map);
}
private boolean isOurGraph(ItemStack item) {
if (!LagUtils.isFilledMapSupported()) {
return isOurGraphLegacy(item);
}
if (item.getType() != Material.FILLED_MAP) {
return false;
}
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof MapMeta)) {
return false;
}
MapMeta mapMeta = (MapMeta) meta;
MapView mapView = mapMeta.getMapView();
return mapView != null && isOurRenderer(mapView);
}
private void buildCombinedGraph(Player player, String[] args) {
List<GraphRenderer> renderers = new ArrayList<>();
for (String arg : args) {
GraphRenderer renderer = graphTypes.get(arg);
if (renderer == null) {
sendError(player, "Unknown graph type " + arg);
return;
}
renderers.add(renderer);
}
if (renderers.size() > MAX_COMBINED) {
sendError(player, "Too many graphs");
} else {
CombinedGraph combinedGraph = new CombinedGraph(renderers.toArray(new GraphRenderer[0]));
MapView view = installRenderer(player, combinedGraph);
giveMap(player, view);
}
}
private boolean isOurGraph(ItemStack item) {
if (!LagUtils.isFilledMapSupported()) {
return isOurGraphLegacy(item);
}
if (item.getType() != Material.FILLED_MAP) {
return false;
}
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof MapMeta)) {
return false;
}
MapMeta mapMeta = (MapMeta) meta;
MapView mapView = mapMeta.getMapView();
return mapView != null && isOurRenderer(mapView);
}
private void buildCombinedGraph(Player player, String[] args) {
List<GraphRenderer> renderers = new ArrayList<>();
for (String arg : args) {
GraphRenderer renderer = graphTypes.get(arg);
if (renderer == null) {
sendError(player, "Unknown graph type " + arg);
return;
}
renderers.add(renderer);
}
if (renderers.size() > MAX_COMBINED) {
sendError(player, "Too many graphs");
} else {
CombinedGraph combinedGraph = new CombinedGraph(renderers.toArray(new GraphRenderer[0]));
MapView view = installRenderer(player, combinedGraph);
giveMap(player, view);
}
}
@Override
public void sendMap(MapView map) {
if (getHandle().connection == null) return;
RenderData data = ((CraftMapView) map).render(this);
Collection<MapDecoration> icons = new ArrayList<>();
for (MapCursor cursor : data.cursors) {
if (cursor.isVisible()) {
icons.add(new MapDecoration(MapDecoration.Type.byIcon(cursor.getRawType()), cursor.getX(), cursor.getY(), cursor.getDirection()));
}
}
SPacketMaps packet = new SPacketMaps(map.getId(), map.getScale().getValue(), true, icons, data.buffer, 0, 0, 128, 128);
getHandle().connection.sendPacket(packet);
}
@Test
void build() {
var view = mock(MapView.class);
MapBuilder.of(WATER).self().colour(SILVER).view(view).location("name").scaling(true);
verify(meta).setColor(SILVER);
verify(meta).setMapView(view);
verify(meta).setLocationName("name");
verify(meta).setScaling(true);
}
@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);
}
}
}
private boolean isOurGraphLegacy(ItemStack mapItem) {
if (mapItem.getType() != Material.MAP)
return false;
short mapId = mapItem.getDurability();
MapView mapView = Bukkit.getMap(mapId);
return mapView != null && isOurRenderer(mapView);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!canExecute(sender, command)) {
return true;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (args.length > 0) {
if (args.length > 1) {
buildCombinedGraph(player, args);
} else {
String graph = args[0];
GraphRenderer renderer = graphTypes.get(graph);
if (renderer == null) {
sendError(sender, "Unknown graph type");
} else {
giveMap(player, installRenderer(player, renderer));
}
}
return true;
}
//default is heap usage
GraphRenderer graphRenderer = graphTypes.get("heap");
MapView mapView = installRenderer(player, graphRenderer);
giveMap(player, mapView);
} else {
sendError(sender, "Not implemented for the console");
}
return true;
}
private MapView installRenderer(Player player, GraphRenderer graphType) {
MapView mapView = Bukkit.createMap(player.getWorld());
mapView.getRenderers().forEach(mapView::removeRenderer);
mapView.addRenderer(graphType);
return mapView;
}
@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 sendMap(MapView map) {
if (getHandle().playerNetServerHandler == null) return;
RenderData data = ((CraftMapView) map).render(this);
for (int x = 0; x < 128; ++x) {
byte[] bytes = new byte[131];
bytes[1] = (byte) x;
for (int y = 0; y < 128; ++y) {
bytes[y + 3] = data.buffer[y * 128 + x];
}
net.minecraft.network.play.server.S34PacketMaps packet = new net.minecraft.network.play.server.S34PacketMaps(map.getId(), bytes);
getHandle().playerNetServerHandler.sendPacket(packet);
}
}
@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);
}
}
private boolean isOurGraphLegacy(ItemStack mapItem) {
if (mapItem.getType() != Material.MAP)
return false;
short mapId = mapItem.getDurability();
MapView mapView = Bukkit.getMap(mapId);
return mapView != null && isOurRenderer(mapView);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!canExecute(sender, command)) {
return true;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (args.length > 0) {
if (args.length > 1) {
buildCombinedGraph(player, args);
} else {
String graph = args[0];
GraphRenderer renderer = graphTypes.get(graph);
if (renderer == null) {
sendError(sender, "Unknown graph type");
} else {
giveMap(player, installRenderer(player, renderer));
}
}
return true;
}
//default is heap usage
GraphRenderer graphRenderer = graphTypes.get("heap");
MapView mapView = installRenderer(player, graphRenderer);
giveMap(player, mapView);
} else {
sendError(sender, "Not implemented for the console");
}
return true;
}
private MapView installRenderer(Player player, GraphRenderer graphType) {
MapView mapView = Bukkit.createMap(player.getWorld());
mapView.getRenderers().forEach(mapView::removeRenderer);
mapView.addRenderer(graphType);
return mapView;
}
@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--;
}
public MapInitializeEvent(final MapView mapView) {
this.mapView = mapView;
}
public MapBuilder view(MapView map) {
meta.setMapView(map);
return this;
}
@Override
public MapView getMap(int id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public MapView createMap(World world) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private boolean isOurRenderer(MapView mapView) {
return mapView.getRenderers().stream()
.anyMatch(GraphRenderer.class::isInstance);
}
@Override
public MapView getMap(int id) {
return null;
}
@Override
public MapView createMap(World world) {
return null;
}
private boolean isOurRenderer(MapView mapView) {
return mapView.getRenderers().stream()
.anyMatch(GraphRenderer.class::isInstance);
}
/**
* Render a map and send it to the player in its entirety. This may be
* used when streaming the map in the normal manner is not desirable.
*
* @param map The map to be sent
*/
public void sendMap(MapView map);
/**
* Gets the map from the given item ID.
*
* @param id the id of the map to get
* @return a map view if it exists, or null otherwise
* @deprecated Magic value
*/
@Deprecated
public MapView getMap(short id);
/**
* Create a new map with an automatically assigned ID.
*
* @param world the world the map will belong to
* @return a newly created map view
*/
public MapView createMap(World world);