63 lines
1.9 KiB
Java
63 lines
1.9 KiB
Java
package eu.oskar3123.spigot2fa.map;
|
|
|
|
import eu.oskar3123.spigot2fa.Main;
|
|
import eu.oskar3123.spigot2fa.tfa.TFA;
|
|
import net.glxn.qrgen.javase.QRCode;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.map.*;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.util.UUID;
|
|
|
|
public class QRMapRenderer extends MapRenderer
|
|
{
|
|
|
|
private Main plugin;
|
|
private String line1;
|
|
private String line2;
|
|
private Image image;
|
|
private UUID uuid;
|
|
|
|
public QRMapRenderer(Main plugin, String secretKey, Player player) throws IOException
|
|
{
|
|
this.plugin = plugin;
|
|
this.uuid = player.getUniqueId();
|
|
String[] formattedSecretKey = secretKey .replaceAll("((?:.{4} ){4})", "$1;").split(";");
|
|
this.line1 = formattedSecretKey[0].trim();
|
|
this.line2 = formattedSecretKey[1].trim();
|
|
initImage(TFA.getGoogleAuthenticatorBarCode(secretKey, "Mineworlds", player.getName() + " (" + player.getUniqueId().toString() + ")"));
|
|
}
|
|
|
|
private void initImage(String qrValue) throws IOException
|
|
{
|
|
byte[] imageData = QRCode.from(qrValue).withSize(110, 110).stream().toByteArray();
|
|
image = ImageIO.read(new ByteArrayInputStream(imageData));
|
|
}
|
|
|
|
@Override
|
|
public void render(MapView map, MapCanvas canvas, Player player)
|
|
{
|
|
map.setUnlimitedTracking(false);
|
|
map.setCenterX(player.getLocation().getBlockX() + 256);
|
|
byte color = MapPalette.matchColor(255, 255, 255);
|
|
for (int x = 0; x < 128; x++)
|
|
{
|
|
for (int y = 0; y < 128; y++)
|
|
{
|
|
canvas.setPixel(x, y, color);
|
|
}
|
|
}
|
|
if (!player.getUniqueId().equals(uuid))
|
|
{
|
|
return;
|
|
}
|
|
canvas.drawImage(9, 18, image);
|
|
canvas.drawText(2, 2, MinecraftFont.Font, line1);
|
|
canvas.drawText(2, 11, MinecraftFont.Font, line2);
|
|
}
|
|
|
|
}
|