Skip to content

Instantly share code, notes, and snippets.

@Redrield
Last active June 17, 2017 02:04
Show Gist options
  • Save Redrield/b15358227323f72426f6ae3e8a8804ca to your computer and use it in GitHub Desktop.
Save Redrield/b15358227323f72426f6ae3e8a8804ca to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.redrield</groupId>
<artifactId>QrTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>QrTest</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<version>3.6.1</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
/*
This Source Code Form is subject to the terms of the Mozilla
Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at https://mozilla.org/MPL/2.0.
*/
package com.redrield.qrtest;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.UnsupportedEncodingException;
/**
@author Redrield
@date 16/06/2017
*/
public final class QrTest extends JavaPlugin implements Listener{
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
try {
BitMatrix bm = makeQrCode();
ItemStack mapItem = new ItemStack(Material.MAP);
e.getPlayer().getInventory().setItemInMainHand(mapItem);
MapView itemView = Bukkit.getMap(mapItem.getDurability());
itemView.getRenderers().forEach(itemView::removeRenderer);
itemView.addRenderer(new MapRenderer() {
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
canvas.drawImage(0, 0, MatrixToImageWriter.toBufferedImage(bm));
}
});
} catch (UnsupportedEncodingException | WriterException e1) {
e1.printStackTrace();
}
}
private BitMatrix makeQrCode() throws UnsupportedEncodingException, WriterException {
return new MultiFormatWriter().encode("Hello",
BarcodeFormat.QR_CODE, 128, 128);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment