diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b14d8e6 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: Build + +on: + push: + branches: + - '**' + tags-ignore: + - 'v*.*.*' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + - id: sha + run: | + SHA=${{ github.sha }} + echo "::set-output name=sha8::${SHA:0:8}" + - uses: gradle/gradle-build-action@v2 + with: + arguments: | + build + -Pversion=0.0.0+${{ steps.sha.outputs.sha8 }} + - uses: actions/upload-artifact@v3 + with: + name: Artifacts (${{ steps.sha.outputs.sha8 }}) + path: | + bungee/build/libs/MapModCompanion-Bungee.jar + spigot/build/libs/MapModCompanion-Spigot.jar diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..fc76bb4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,41 @@ +name: Release + +on: + push: + tags: + - 'v*.*.*' + +permissions: + contents: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + - name: Detect version from the tag name + id: version + run: | + TAG=${{ github.ref }} + VERSION=${TAG#refs/tags/v} + echo "::set-output name=version::$VERSION" + IS_PRE_RELEASE=$([[ $VERSION =~ .+(alpha|beta|pre|rc).* ]] && echo "true" || echo "false") + echo "::set-output name=pre-release::$IS_PRE_RELEASE" + - uses: gradle/gradle-build-action@v2 + with: + arguments: | + build + -Pversion=${{ steps.version.outputs.version }} + - uses: softprops/action-gh-release@v1 + with: + name: ${{ steps.version.outputs.version }} + prerelease: ${{ steps.version.outputs.pre-release }} + generate_release_notes: true + draft: true + files: | + bungee/build/libs/MapModCompanion-Bungee.jar + spigot/build/libs/MapModCompanion-Spigot.jar diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..148337e --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ +debug*/ + +### IntelliJ IDEA ### +.idea/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..55a80dc --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Artur Khusainov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8434860 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Companion for map mods +Unofficial BungeeCord and Spigot (Paper) companion plugin for +[Xaero's Minimap](https://www.curseforge.com/minecraft/mc-mods/xaeros-minimap) +(and their [World Map](https://www.curseforge.com/minecraft/mc-mods/xaeros-world-map)), +[JourneyMap](https://www.curseforge.com/minecraft/mc-mods/journeymap) and +VoxelMap. + +This plugin fixes Multi-world detection in multiplayer by simulating mod presence on +the server side. + +This plugin was inspired by @kosma's [worldnamepacket](https://github.com/kosma/worldnamepacket), +which supported Velocity, Fabric and Spigot at the time of writing. + +## Support table +| Mod | Version | Status | +|------------------------------------------------------------------------------------|-----------------------------|-------------| +| [Xaero's Minimap](https://www.curseforge.com/minecraft/mc-mods/xaeros-minimap) | v22.13.2 (1.19.2) | ✅ Supported | +| [Xaero's World Map](https://www.curseforge.com/minecraft/mc-mods/xaeros-world-map) | v22.13.2 (1.19.2) | ✅ Supported | +| [JourneyMap](https://www.curseforge.com/minecraft/mc-mods/journeymap) | 5.9.0beta2 (1.18.2, 1.19.2) | ✅ Supported | +| VoxelMap (Refresh-MC) | 1.11.10 (1.19.2) | ✅ Supported | + +## Installation +1. Download the latest release from [Releases](https://github.com/turikhay/xaeros-minimap-companion/releases) page +2. Put each of them into the corresponding plugins folder +3. That's it. No configuration required. You can restart your servers now. + +⚠️ **NOTE** Spigot plugin can be used without BungeeCord counterpart, but it's highly recommended to install +plugins on both sides. On the contrary, BungeeCord plugin is useless if you don't install Spigot plugin on +downstream servers. + +## Alternatives +- [worldnamepacket](https://github.com/kosma/worldnamepacket) (Velocity, Fabric, Spigot) +- [JourneyMap Server](https://www.curseforge.com/minecraft/mc-mods/journeymap-server) (Spigot) +- [Minimap server](https://github.com/Ewpratten/MinimapServer) (Spigot) \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..536a44e --- /dev/null +++ b/build.gradle @@ -0,0 +1,33 @@ +plugins { + id 'java' + id 'kr.entree.spigradle' version '2.4.2' apply false + id 'kr.entree.spigradle.bungee' version '2.4.2' apply false + id 'com.github.johnrengelman.shadow' version '7.1.2' apply false +} + +group 'com.turikhay.mc' + +subprojects { + apply plugin: 'java' + + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + + group rootProject.group + version rootProject.version + + repositories { + mavenCentral() + } + + dependencies { + compileOnly 'com.github.spotbugs:spotbugs-annotations:4.7.1' + + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + } + + test { + useJUnitPlatform() + } +} diff --git a/bungee/build.gradle b/bungee/build.gradle new file mode 100644 index 0000000..7d47351 --- /dev/null +++ b/bungee/build.gradle @@ -0,0 +1,22 @@ +plugins { + id 'kr.entree.spigradle.bungee' + id 'com.github.johnrengelman.shadow' +} + +bungee { + name 'MapModCompanion' + author 'turikhay' +} + +assemble { + dependsOn shadowJar +} + +shadowJar { + archiveName("MapModCompanion-Bungee.jar") +} + +dependencies { + implementation project(":common") + compileOnly bungeecord() +} diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/CompanionBungee.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/CompanionBungee.java new file mode 100644 index 0000000..dd20363 --- /dev/null +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/CompanionBungee.java @@ -0,0 +1,20 @@ +package com.turikhay.mc.mapmodcompanion.bungee; + +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; + +import java.util.Arrays; +import java.util.List; + +public class CompanionBungee extends Plugin implements Listener { + + List> handlers = Arrays.asList( + new XaerosPacketHandler(this), + new WorldIdPacketHandler(this) + ); + + @Override + public void onEnable() { + handlers.forEach(PacketHandler::init); + } +} diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/PacketHandler.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/PacketHandler.java new file mode 100644 index 0000000..039de9d --- /dev/null +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/PacketHandler.java @@ -0,0 +1,58 @@ +package com.turikhay.mc.mapmodcompanion.bungee; + +import com.turikhay.mc.mapmodcompanion.IdMessagePacket; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; + +import javax.annotation.Nullable; +import java.util.Arrays; + +public abstract class PacketHandler, Id extends IdMessagePacket> implements Listener { + private final String channelName; + private final CompanionBungee plugin; + + public PacketHandler(String channelName, CompanionBungee plugin) { + this.channelName = channelName; + this.plugin = plugin; + } + + public void init() { + plugin.getProxy().registerChannel(channelName); + plugin.getProxy().getPluginManager().registerListener(plugin, this); + } + + /* + Intercept world IDs received from the downstream servers + in order to (hopefully) resolve ID conflicts that may occur + */ + @EventHandler + public void onPluginMessageSentToPlayer(PluginMessageEvent event) { + if (!event.getTag().equals(channelName)) { + return; + } + if (!(event.getSender() instanceof Server)) { + return; + } + Server server = (Server) event.getSender(); + if (!(event.getReceiver() instanceof ProxiedPlayer)) { + return; + } + ProxiedPlayer player = (ProxiedPlayer) event.getReceiver(); + byte[] data = event.getData(); + Id oldId = tryRead(data); + if (oldId == null) { + plugin.getLogger().warning("Possibly corrupted packet from " + channelName + ": " + Arrays.toString(data)); + return; + } + event.setCancelled(true); + Id2 newId = oldId.combineWith(getId(server)); + player.sendData(channelName, IdMessagePacket.bytesPacket(newId)); + } + + @Nullable + public abstract Id tryRead(byte[] data); + public abstract Id2 getId(Server server); +} diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java new file mode 100644 index 0000000..a85df17 --- /dev/null +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java @@ -0,0 +1,25 @@ +package com.turikhay.mc.mapmodcompanion.bungee; + +import com.turikhay.mc.mapmodcompanion.worldid.WorldId; +import net.md_5.bungee.api.connection.Server; +import org.jetbrains.annotations.Nullable; + +import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_CHANNEL_NAME; + +public class WorldIdPacketHandler extends PacketHandler { + + public WorldIdPacketHandler(CompanionBungee plugin) { + super(WORLD_ID_CHANNEL_NAME, plugin); + } + + @Nullable + @Override + public WorldId tryRead(byte[] data) { + return WorldId.tryRead(data); + } + + @Override + public WorldId getId(Server server) { + return WorldId.createTruncatingLength(server.getInfo().getName()); + } +} diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/XaerosPacketHandler.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/XaerosPacketHandler.java new file mode 100644 index 0000000..73a94ec --- /dev/null +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/XaerosPacketHandler.java @@ -0,0 +1,26 @@ +package com.turikhay.mc.mapmodcompanion.bungee; + +import com.turikhay.mc.mapmodcompanion.xaeros.LevelMapProperties; +import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.plugin.Listener; +import org.jetbrains.annotations.Nullable; + +import static com.turikhay.mc.mapmodcompanion.xaeros.XaerosCompanion.XAEROS_CHANNEL_NAME; + +public class XaerosPacketHandler extends PacketHandler implements Listener { + + public XaerosPacketHandler(CompanionBungee plugin) { + super(XAEROS_CHANNEL_NAME, plugin); + } + + @Nullable + @Override + public LevelMapProperties tryRead(byte[] data) { + return LevelMapProperties.tryRead(data); + } + + @Override + public LevelMapProperties getId(Server server) { + return new LevelMapProperties(server.getInfo().getName().hashCode()); + } +} diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/package-info.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/package-info.java new file mode 100644 index 0000000..5141a1a --- /dev/null +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package com.turikhay.mc.mapmodcompanion.bungee; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/common/build.gradle b/common/build.gradle new file mode 100644 index 0000000..e69de29 diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/IdMessagePacket.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/IdMessagePacket.java new file mode 100644 index 0000000..208bb15 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/IdMessagePacket.java @@ -0,0 +1,20 @@ +package com.turikhay.mc.mapmodcompanion; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface IdMessagePacket> { + CombinableWith combineWith(CombinableWith packet); + void constructPacket(DataOutputStream out) throws IOException; + + static byte[] bytesPacket(IdMessagePacket id) { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try (DataOutputStream out = new DataOutputStream(buffer)) { + id.constructPacket(out); + } catch (IOException e) { + throw new Error(e); + } + return buffer.toByteArray(); + } +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/package-info.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/package-info.java new file mode 100644 index 0000000..e5093b6 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package com.turikhay.mc.mapmodcompanion; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java new file mode 100644 index 0000000..e56cc49 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java @@ -0,0 +1,80 @@ +package com.turikhay.mc.mapmodcompanion.worldid; + +import com.turikhay.mc.mapmodcompanion.IdMessagePacket; + +import javax.annotation.Nullable; +import java.io.*; +import java.nio.charset.StandardCharsets; + +import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_MAX_LENGTH; + +public class WorldId implements IdMessagePacket { + private static final int MAX_ID_LENGTH = WORLD_ID_MAX_LENGTH; + private static final int HALF_LENGTH = MAX_ID_LENGTH / 2 - 1; + private static final int MAGIC_MARKER = 42; + + private final String id; + + private WorldId(String id) { + this.id = id; + } + + @Override + public WorldId combineWith(WorldId packet) { + return createTruncatingLength( + truncate(packet.id, HALF_LENGTH) + '_' + truncate(id, HALF_LENGTH) + ); + } + + @Override + public void constructPacket(DataOutputStream out) throws IOException { + out.writeByte(0); // packetId + out.writeByte(MAGIC_MARKER); // 42 (literally) + byte[] data = id.getBytes(StandardCharsets.UTF_8); + out.write(data.length); // length + out.write(data); // UTF + } + + @Nullable + public static WorldId tryRead(byte[] data) { + DataInputStream in = new DataInputStream(new ByteArrayInputStream(data)); + try { + if (in.readByte() != 0) { + return null; + } + if (in.readByte() != MAGIC_MARKER) { + return null; + } + int length = in.readByte(); + byte[] buf = new byte[length]; + int read = in.read(buf, 0, length); + if (read < length) { + return null; + } + return new WorldId(new String(buf, StandardCharsets.UTF_8)); + } catch (IOException ignored) { + } + return null; + } + + @Override + public String toString() { + return "WorldId{" + + "id='" + id + '\'' + + '}'; + } + + public static WorldId createTruncatingLength(String id) { + return new WorldId(truncate(id, MAX_ID_LENGTH)); + } + + private static String truncate(String text, int size) { + while (text.getBytes(StandardCharsets.UTF_8).length > size) { + if (text.length() == 1) { + throw new IllegalArgumentException("cannot truncate"); + } + text = text.substring(0, text.length() - 1); + } + return text; + } +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldIdCompanion.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldIdCompanion.java new file mode 100644 index 0000000..cc45b91 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldIdCompanion.java @@ -0,0 +1,14 @@ +package com.turikhay.mc.mapmodcompanion.worldid; + +public class WorldIdCompanion { + public static final String WORLD_ID_CHANNEL_NAME = "worldinfo:world_id"; + + public static final int WORLD_ID_MAX_LENGTH = Integer.parseInt( + System.getProperty(WorldIdCompanion.class.getPackage().getName() + ".max_length", + String.valueOf(Byte.MAX_VALUE)) + ); + + public static final int WORLD_ID_PACKET_DELAY = Integer.parseInt( + System.getProperty(WorldIdCompanion.class.getPackage().getName() + ".delay", "5") + ); +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/package-info.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/package-info.java new file mode 100644 index 0000000..d30745b --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package com.turikhay.mc.mapmodcompanion.worldid; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/LevelMapProperties.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/LevelMapProperties.java new file mode 100644 index 0000000..e1c6bd2 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/LevelMapProperties.java @@ -0,0 +1,46 @@ +package com.turikhay.mc.mapmodcompanion.xaeros; + +import com.turikhay.mc.mapmodcompanion.IdMessagePacket; + +import javax.annotation.Nullable; +import java.io.*; +import java.util.Objects; + +public class LevelMapProperties implements IdMessagePacket { + private final int id; + + public LevelMapProperties(int id) { + this.id = id; + } + + @Override + public LevelMapProperties combineWith(LevelMapProperties packet) { + return new LevelMapProperties(Objects.hash(id, packet.id)); + } + + @Override + public void constructPacket(DataOutputStream out) throws IOException { + out.write(0); // LevelMapProperties { + out.writeInt(id); // id } + } + + @Nullable + public static LevelMapProperties tryRead(byte[] data) { + DataInputStream in = new DataInputStream(new ByteArrayInputStream(data)); + try { + int marker = in.readByte(); + if (marker == 0) { + return new LevelMapProperties(in.readInt()); + } + } catch (IOException ignored) { // EOF? + } + return null; + } + + @Override + public String toString() { + return "LevelMapProperties{" + + "id=" + id + + '}'; + } +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/XaerosCompanion.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/XaerosCompanion.java new file mode 100644 index 0000000..863ccc4 --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/XaerosCompanion.java @@ -0,0 +1,8 @@ +package com.turikhay.mc.mapmodcompanion.xaeros; + +public class XaerosCompanion { + public static final String XAEROS_CHANNEL_NAME = "xaeroworldmap:main"; + public static final int XAEROS_PACKET_REPEAT_TIMES = Integer.parseInt( + System.getProperty(XaerosCompanion.class.getPackage().getName() + ".repeats", "3") + ); +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/package-info.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/package-info.java new file mode 100644 index 0000000..c9ac53a --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/xaeros/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package com.turikhay.mc.mapmodcompanion.xaeros; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..66f9e80 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +version=0.0.0-SNAPSHOT \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..41dfb87 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..b520177 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,5 @@ +rootProject.name = 'mapmodcompanion' +include 'common' +include 'bungee' +include 'spigot' + diff --git a/spigot/build.gradle b/spigot/build.gradle new file mode 100644 index 0000000..f8855e8 --- /dev/null +++ b/spigot/build.gradle @@ -0,0 +1,49 @@ +plugins { + id 'kr.entree.spigradle' + id 'com.github.johnrengelman.shadow' +} + +//configurations { +// bukkitNeeds +// bukkitProvides +// compileOnly.extendsFrom(bukkitProvides) +//} + +spigot { + name = 'MapModCompanion' + authors = ['turikhay'] + apiVersion = '1.13' + debug { + jvmArgs "-Dlog4j.configurationFile=${projectDir}${File.separatorChar}log4j2-debug.xml" + } + afterEvaluate { +// def shaded = project.configurations.bukkitNeeds.resolvedConfiguration.resolvedArtifacts.collect { +// it.id.componentIdentifier.displayName +// }.join(",") + excludeLibraries( + "${group}:common:${version}", +// shaded + ) + } +} + +dependencies { + implementation project(':common') + compileOnly spigot('1.19.2') +} + +assemble { + dependsOn shadowJar +} + +shadowJar { + archiveName("MapModCompanion-Spigot.jar") +// dependencies { +// exclude(dependency { dep -> +// project.configurations.bukkitProvides.resolvedConfiguration.resolvedArtifacts.any { +// !project.configurations.bukkitNeeds.resolvedConfiguration.resolvedArtifacts.contains(it) +// && it.id.componentIdentifier.displayName == dep.name +// } +// }) +// } +} \ No newline at end of file diff --git a/spigot/log4j2-debug.xml b/spigot/log4j2-debug.xml new file mode 100644 index 0000000..f273785 --- /dev/null +++ b/spigot/log4j2-debug.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java new file mode 100644 index 0000000..eecad6b --- /dev/null +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java @@ -0,0 +1,19 @@ +package com.turikhay.mc.mapmodcompanion.spigot; + +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Arrays; +import java.util.List; + +public class CompanionSpigot extends JavaPlugin implements Listener { + List> handlers = Arrays.asList( + new XaerosHandler(this), + new WorldIdHandler(this) + ); + + @Override + public void onEnable() { + handlers.forEach(Handler::init); + } +} diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/Handler.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/Handler.java new file mode 100644 index 0000000..bf6e970 --- /dev/null +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/Handler.java @@ -0,0 +1,52 @@ +package com.turikhay.mc.mapmodcompanion.spigot; + +import com.turikhay.mc.mapmodcompanion.IdMessagePacket; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerJoinEvent; + +public abstract class Handler> implements Listener { + private final String channelName; + protected final CompanionSpigot plugin; + + public Handler(String channelName, CompanionSpigot plugin) { + this.channelName = channelName; + this.plugin = plugin; + } + + public void init() { + plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channelName); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onPlayerJoined(PlayerJoinEvent event) { + sendLevelId(event.getPlayer(), EventSource.JOIN); + } + + @EventHandler + public void onWorldChanged(PlayerChangedWorldEvent event) { + sendLevelId(event.getPlayer(), EventSource.WORLD_CHANGE); + } + + public void sendLevelId(Player player, EventSource source) { + Id id = getId(player.getWorld()); + byte[] data = IdMessagePacket.bytesPacket(id); + scheduleLevelIdPacket( + () -> player.sendPluginMessage(plugin, channelName, data), + source + ); + } + + public abstract void scheduleLevelIdPacket(Runnable r, EventSource source); + public abstract Id getId(World world); + + public enum EventSource { + JOIN, + WORLD_CHANGE, + PLUGIN_MESSAGE + } +} diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java new file mode 100644 index 0000000..2214541 --- /dev/null +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java @@ -0,0 +1,43 @@ +package com.turikhay.mc.mapmodcompanion.spigot; + +import com.turikhay.mc.mapmodcompanion.worldid.WorldId; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.messaging.PluginMessageListener; +import org.jetbrains.annotations.NotNull; + +import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_CHANNEL_NAME; +import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_PACKET_DELAY; + +public class WorldIdHandler extends Handler implements Listener, PluginMessageListener { + + public WorldIdHandler(CompanionSpigot plugin) { + super(WORLD_ID_CHANNEL_NAME, plugin); + } + + @Override + public void init() { + super.init(); + plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, WORLD_ID_CHANNEL_NAME, this); + } + + @Override + public void scheduleLevelIdPacket(Runnable r, EventSource source) { + plugin.getServer().getScheduler().runTaskLater(plugin, r, 20L * WORLD_ID_PACKET_DELAY); + } + + @Override + public WorldId getId(World world) { + return WorldId.createTruncatingLength(world.getUID().toString()); + } + + @Override + public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + if (!channel.equals(WORLD_ID_CHANNEL_NAME)) { + return; + } + // JourneyMap Server also sends this packet unconditionally + sendLevelId(player, EventSource.PLUGIN_MESSAGE); + } +} diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaerosHandler.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaerosHandler.java new file mode 100644 index 0000000..f4b69f2 --- /dev/null +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaerosHandler.java @@ -0,0 +1,32 @@ +package com.turikhay.mc.mapmodcompanion.spigot; + +import com.turikhay.mc.mapmodcompanion.xaeros.LevelMapProperties; +import org.bukkit.World; +import org.bukkit.event.Listener; + +import static com.turikhay.mc.mapmodcompanion.xaeros.XaerosCompanion.XAEROS_CHANNEL_NAME; +import static com.turikhay.mc.mapmodcompanion.xaeros.XaerosCompanion.XAEROS_PACKET_REPEAT_TIMES; + +public class XaerosHandler extends Handler implements Listener { + public XaerosHandler(CompanionSpigot plugin) { + super(XAEROS_CHANNEL_NAME, plugin); + } + + @Override + public void scheduleLevelIdPacket(Runnable r, EventSource source) { + if (source == EventSource.JOIN) { + // Sometimes Xaero's World Map is not initialized at the time they receive our packet, + // leading to world not being recognized. We fix this issue by sending our packet more than once. + for (int i = 0; i < XAEROS_PACKET_REPEAT_TIMES; i++) { + plugin.getServer().getScheduler().runTaskLater(plugin, r, 20L * i); + } + } else { + r.run(); + } + } + + @Override + public LevelMapProperties getId(World world) { + return new LevelMapProperties(world.getUID().hashCode()); + } +} diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/package-info.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/package-info.java new file mode 100644 index 0000000..460ac5d --- /dev/null +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package com.turikhay.mc.mapmodcompanion.spigot; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file