18地图相关
获得一张地图指令:
/**
* @author houyunfei
*/
public class MapExecutor implements CommandExecutor {
private MapView view;
{
/**
* 给Map添加View视图
*
* 通过Bukkit的createMap来生成一个MapView
*/
view = Bukkit.createMap(Bukkit.getWorlds().get(0));
// 修改视图的信息
view.setScale(MapView.Scale.CLOSEST);
view.setLocked(true);
view.setTrackingPosition(true); // 是否追踪玩家,并显示玩家的cursor true: 显示Cursor,false: 不显示Cursor
view.setUnlimitedTracking(false); // 是否在玩家离开View时仍然标注玩家的cursor true: 标注玩家的cursor,false: 不标注玩家的cursor
// 删除原有用于渲染世界的Render
view.getRenderers().forEach(view::removeRenderer);
// 添加自己的render
view.addRenderer(new FirstRender());
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
PlayerInventory inventory = player.getInventory();
// 直接new出Map的物品槽
ItemStack itemStack = new ItemStack(Material.FILLED_MAP, 1);
// 既然是可以展示画面的特殊物品,相比也有个对应的Meta
MapMeta mapMeta = (MapMeta) itemStack.getItemMeta();
// 设置meta信息
mapMeta.setLocationName("这是地点:" + player.getLocation().getBlockX() + "," + player.getLocation().getBlockY() + "," + player.getLocation().getBlockZ());
mapMeta.setColor(Color.fromRGB(255, 0, 0));
// 以后获取到的都是相同的View
mapMeta.setMapView(view);
// 查看默认生成的View信息
int id = view.getId();
int centerX = view.getCenterX();
int centerZ = view.getCenterZ();
MapView.Scale scale = view.getScale();
sender.sendMessage("视图id:" + id);
sender.sendMessage("视图中心点X坐标:" + centerX);
sender.sendMessage("视图中心点Z坐标:" + centerZ);
sender.sendMessage("视图缩放比例:" + scale);
// 获取代码创建的Map信息
String locationName = mapMeta.getLocationName();
sender.sendMessage("locationName: " + locationName);
Color color = mapMeta.getColor();
sender.sendMessage("color: " + color);
itemStack.setItemMeta(mapMeta);
inventory.addItem(itemStack);
return true;
}
sender.sendMessage("只有玩家才能使用此命令");
return true;
}
}
渲染地图逻辑:
public class FirstRender extends MapRenderer {
public FirstRender() {
/**
* 不分开渲染
*/
super(false);
try {
image = ImageIO.read(new URL("https://www.baidu.com/img/bd_logo1.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Image image;
private MapCursorCollection cursors = new MapCursorCollection();
{
MapCursor cursor1 = new MapCursor((byte) 0, (byte) 0, (byte) 4, MapCursor.Type.MANSION, true);
MapCursor cursor2 = new MapCursor((byte) 0, (byte) 0, (byte) 0, MapCursor.Type.BANNER_PURPLE, true);
cursors.addCursor(cursor1);
cursors.addCursor(cursor2);
}
public void setImage(Image image) {
this.image = image;
}
/**
* 高频率调用,所以请务必不要在这里进行很耗时的操作
*/
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
if (null != player) {
Bukkit.broadcastMessage(player.getName() + "玩家,你好!现在是:" + System.currentTimeMillis());
} else {
Bukkit.broadcastMessage("游客,你好!现在是:" + System.currentTimeMillis());
}
/**
* 需改为128 X 128 像素大小的图片
*/
canvas.drawImage(0, 0, image);
/**
* 在View中渲染英文字符(不要带有MinecraftFont.Font中不存在的字符)
*/
canvas.drawText((int) player.getLocation().getX(), (int) player.getLocation().getZ(), MinecraftFont.Font, "Hello World!");
MapCursor cursor1 = cursors.getCursor(0); // 宅邸
MapCursor cursor2 = cursors.getCursor(1); // 旗帜
Location cxk = Bukkit.getPlayer("cxk").getLocation();
byte x = (byte) cxk.getX();
byte y = (byte) cxk.getZ();
Location cxk2 = Bukkit.getPlayer("cxk2").getLocation();
byte x2 = (byte) cxk2.getX();
byte y2 = (byte) cxk2.getZ();
cursor1.setX(x);
cursor1.setY(y);
cursor2.setX(x2);
cursor2.setY(y2);
canvas.setCursors(cursors);
}
}