diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..00a51af --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# These are explicitly windows files and should use crlf +*.bat text eol=crlf + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc76166 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Ignore Gradle project-specific cache directory +.gradle + +gradle +gradlew +gradlew.bat + +.classpath +.project +.settings +*.launch + +# Ignore Gradle build output directory +build +bin +run diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..14e23c6 --- /dev/null +++ b/build.gradle @@ -0,0 +1,93 @@ +plugins { + id 'fabric-loom' version '0.7-SNAPSHOT' + id 'maven-publish' +} + +sourceCompatibility = JavaVersion.VERSION_1_8 +targetCompatibility = JavaVersion.VERSION_1_8 + +archivesBaseName = project.archives_base_name +version = project.mod_version +group = project.maven_group + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + modImplementation "com.google.zxing:javase:${project.zxing_version}" + include "com.google.zxing:javase:${project.zxing_version}" + include "com.google.zxing:core:${project.zxing_version}" +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too + // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used. + // We'll use that if it's available, but otherwise we'll use the older option. + def targetVersion = 8 + if (JavaVersion.current().isJava9Compatible()) { + it.options.release = targetVersion + } +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() +} + +jar { + from("LICENSE") { + rename { "${it}_${project.archivesBaseName}"} + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + // add all the jars that should be included when publishing to maven + artifact(remapJar) { + builtBy remapJar + } + artifact(sourcesJar) { + builtBy remapSourcesJar + } + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..3d5bba0 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,18 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G + +# Fabric Properties + # check these on https://fabricmc.net/versions.html + minecraft_version=1.16.5 + yarn_mappings=1.16.5+build.9 + loader_version=0.11.3 + +# Mod Properties + mod_version = 0.1 + maven_group = de.rangun + archives_base_name = pandacrossing + +# Dependencies + # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api (or https://fabricmc.net/versions.html) + fabric_version=0.34.2+1.16 + zxing_version=3.4.1 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f91a4fe --- /dev/null +++ b/settings.gradle @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + gradlePluginPortal() + } +} diff --git a/src/main/java/de/rangun/pandacrossing/PandaCrossingMod.java b/src/main/java/de/rangun/pandacrossing/PandaCrossingMod.java new file mode 100644 index 0000000..af2af7a --- /dev/null +++ b/src/main/java/de/rangun/pandacrossing/PandaCrossingMod.java @@ -0,0 +1,111 @@ +/* + * Copyright 2021 by Heiko Schäfer + * + * This file is part of PandaCrossing. + * + * PandaCrossing is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * PandaCrossing is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with PandaCrossing. If not, see . + */ + +package de.rangun.pandacrossing; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.QRCodeWriter; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.minecraft.block.Blocks; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.LiteralText; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; + +public class PandaCrossingMod implements ModInitializer { + + private interface IBlockPlacer { + void placeBlock(int x, int y, boolean b); + }; + + private static void createQRCode(IBlockPlacer bp, String qrCodeData, int qrCodeheight, int qrCodewidth) + throws WriterException, UnsupportedEncodingException { + + @SuppressWarnings("rawtypes") + final Map hintMap = new HashMap(); + + hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); + hintMap.put(EncodeHintType.MARGIN, 1); + + final BitMatrix matrix = new QRCodeWriter().encode( + new String(qrCodeData.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8), BarcodeFormat.QR_CODE, + qrCodewidth, qrCodeheight, hintMap); + + for (int y = 0; y < matrix.getHeight(); ++y) { + for (int x = 0; x < matrix.getWidth(); ++x) { + bp.placeBlock(x, y, matrix.get(x, y)); + } + } + } + + @Override + public void onInitialize() { + + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { + dispatcher.register(literal("qr").then(argument("text", greedyString()).executes(ctx -> { + + final String txt = getString(ctx, "text"); + final ServerPlayerEntity player = ctx.getSource().getPlayer(); + final World world = player.getServerWorld(); + final Vec3d playerPos = player.getPos(); + final BlockPos curPos = new BlockPos(playerPos.getX(), playerPos.getY() - 1.0d, playerPos.getZ()); + + try { + + createQRCode(new IBlockPlacer() { + + @Override + public void placeBlock(int x, int y, boolean b) { + + world.setBlockState(curPos.add(x, 0, y), b ? Blocks.BLACK_CONCRETE.getDefaultState() + : Blocks.WHITE_CONCRETE.getDefaultState()); + } + + }, txt, 21, 21); + + } catch (Exception e) { + e.printStackTrace(); + } + + return 1; + })).executes(ctx -> { + ctx.getSource().sendFeedback(new LiteralText("Some usage"), false); + + return 1; + })); + }); + } +} diff --git a/src/main/resources/assets/panda_crossing/icon.png b/src/main/resources/assets/panda_crossing/icon.png new file mode 100644 index 0000000..047b91f Binary files /dev/null and b/src/main/resources/assets/panda_crossing/icon.png differ diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..2ef9abf --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,36 @@ +{ + "schemaVersion": 1, + "id": "panda_crossing", + "version": "${version}", + + "name": "Panda Crossing QR Generator", + "description": "Creates QR codes", + "authors": [ + "Velnias75" + ], + "contact": { + "discord": "https://discord.me/agoramc", + "sources": "https://github.com/FabricMC/fabric-example-mod" + }, + + "license": "GPL-3.0-or-later", + "icon": "assets/panda_crossing/icon.png", + + "environment": "*", + "entrypoints": { + "main": [ + "de.rangun.pandacrossing.PandaCrossingMod" + ] + }, + "mixins": [ + ], + + "depends": { + "fabricloader": ">=0.7.4", + "fabric": "*", + "minecraft": "1.16.x" + }, + "suggests": { + "another-mod": "*" + } +}