Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicJelle committed Jul 30, 2023
0 parents commit 99deec0
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 0 deletions.
113 changes: 113 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# User-specific stuff
.idea/

*.iml
*.ipr
*.iws

# IntelliJ
out/

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

target/

pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next

release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml

# Common working directory
run/
114 changes: 114 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?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.technicjelle</groupId>
<artifactId>BlueMapSignExtractor</artifactId>
<version>0.1</version>
<packaging>jar</packaging>

<name>BlueMapSignExtractor</name>
<url>https://github.com/TechnicJelle/BlueMapSignExtractor/</url>

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<relocations>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>com.technicjelle.bluemapsignextractor.shadow</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>com.technicjelle.bluemapsignextractor.shadow</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.TechnicJelle</groupId>
<artifactId>UpdateCheckerJava</artifactId>
<version>v2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.13-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.BlueMap-Minecraft</groupId>
<artifactId>BlueMapAPI</artifactId>
<version>v2.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.BlueMap-Minecraft</groupId>
<artifactId>BlueNBT</artifactId>
<version>v1.2.0</version>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.technicjelle.bluemapsignextractor;

import com.technicjelle.UpdateChecker;
import com.technicjelle.bluemapsignextractor.models.BlockEntity;
import com.technicjelle.bluemapsignextractor.models.Chunk;
import de.bluecolored.bluemap.api.BlueMapAPI;
import de.bluecolored.bluemap.api.BlueMapMap;
import de.bluecolored.bluemap.api.markers.MarkerSet;
import de.bluecolored.bluemap.api.markers.POIMarker;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NBTReader;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.stream.Stream;

public final class BlueMapSignExtractor extends JavaPlugin {
private UpdateChecker updateChecker;

@Override
public void onEnable() {
getLogger().info("BlueMapSignExtractor enabled");

new Metrics(this, 19320);

updateChecker = new UpdateChecker("TechnicJelle", "BlueMapSignExtractor", getDescription().getVersion());
updateChecker.checkAsync();

BlueMapAPI.onEnable(onEnableListener);
}

Consumer<BlueMapAPI> onEnableListener = api -> {
updateChecker.logUpdateMessage(getLogger());
Bukkit.getScheduler().runTaskAsynchronously(this, () -> loadMarkers(api));
};

private void loadMarkers(BlueMapAPI api) {
for (BlueMapMap map : api.getMaps()) {
Path saveFolder = map.getWorld().getSaveFolder();
getLogger().info("Map: " + map.getId() + " " + saveFolder);
Path regionFolder = saveFolder.resolve("region");

try (Stream<Path> s = Files.list(regionFolder)) {
s.filter(p -> p.toString().endsWith(".mca")).forEach(p -> processMCA(map, p));
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Error reading region folder", e);
}
}

getLogger().info("Finished loading markers");
}

@Override
public void onDisable() {
getLogger().info("BlueMapSignExtractor disabled");
BlueMapAPI.unregisterListener(onEnableListener);
}

private void processMCA(BlueMapMap map, Path mcaFile) {
getLogger().info("Processing region " + mcaFile.getFileName().toString());
Random random = new Random();
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
try (InputStream in = MCA.loadChunk(mcaFile, x, z)) {
if (in == null) continue;

NBTReader reader = new NBTReader(in);
BlueNBT nbt = new BlueNBT();
Chunk chunk = nbt.read(reader, Chunk.class);

for (BlockEntity be : chunk.blockEntities) {
if (be.id.contains("sign") && be.getText1() != null) {
POIMarker marker = POIMarker.builder()
.label(be.getText1())
.detail("<p style=\"white-space: nowrap;text-align: center;\">" + be.getText1() + "<br>" + be.getText2() + "<br>" + be.getText3() + "<br>" + be.getText4() + "</p>")
.position(be.x + 0.5, be.y + 0.5, be.z + 0.5)
.build();

MarkerSet markerSet = map.getMarkerSets().computeIfAbsent("signs", id -> MarkerSet.builder()
.label("Signs")
.toggleable(true)
.defaultHidden(false)
.build());

//nice and random key... probably be good enough to prevent duplicates
String key = be.getText1() + be.getText2() + be.getText3() + be.getText4() + random.nextInt();
markerSet.put(key, marker);
}
}
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Error reading mca file", e);
}
}
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/technicjelle/bluemapsignextractor/MCA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.technicjelle.bluemapsignextractor;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.file.Path;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

public class MCA {
interface Compression {
InputStream decompress(InputStream in) throws IOException;
}

public static InputStream loadChunk(Path regionFile, int chunkX, int chunkZ) throws IOException {
RandomAccessFile raf = new RandomAccessFile(regionFile.toFile(), "r");

int xzChunk = Math.floorMod(chunkZ, 32) * 32 + Math.floorMod(chunkX, 32);

raf.seek(xzChunk * 4L);
int offset = raf.read() << 16;
offset |= (raf.read() & 0xFF) << 8;
offset |= raf.read() & 0xFF;
offset *= 4096;

int size = raf.readByte() * 4096;
if (size == 0) return null;

raf.seek(offset + 4); // +4 skip chunk size

byte compressionTypeByte = raf.readByte();
Compression compressionType;
switch (compressionTypeByte) {
case 1:
compressionType = GZIPInputStream::new;
break;
case 2:
compressionType = InflaterInputStream::new;
break;
default:
throw new IOException("Unknown compression type: " + compressionTypeByte);
}

return compressionType.decompress(new FileInputStream(raf.getFD()));
}
}
Loading

0 comments on commit 99deec0

Please sign in to comment.