Skip to content

Commit e017047

Browse files
committed
Initial commit
0 parents  commit e017047

File tree

25 files changed

+1733
-0
lines changed

25 files changed

+1733
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target/
2+
.idea/
3+
*.iml
4+
dependency-reduced-pom.xml

CONTRIBUTING.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Contribution guidelines
2+
3+
Thank you for your interest in contributing to this project! There are a few rules to follow in order to ensure quality. If you do not follow the
4+
guidelines, your contribution might be rejected.
5+
6+
1. Make sure to create an issue describing what you would like to do
7+
- Please wait for a maintainer to approve the proposed changes.
8+
- If there is already an open issue for the thing you would like to do you just need to claim it ("I would like to work on this") and wait for a
9+
maintainer to assign the issue to you.
10+
2. Make sure to work in a fork and in a separate branch
11+
- **Create your branch from the `develop` branch**
12+
- For features the branch is usually called `feature/my-cool-feature` or `feature-my-cool-feature`.
13+
- For bugfixes the branch is usually called `fix/my-cool-fix` or `fix-my-cool-fix`.
14+
- For documentation changes the branch is usually called `docs/my-cool-docs-change` or `docs-my-cool-docs-change`.
15+
3. Only create a pull request if your changes are ready to be reviewed and merged
16+
- **Merge into `develop`, not `main`**
17+
- If you want to change something after you have created your pull request please write a small notice in your pr. When you're done, make sure to
18+
ping a maintainer / request a review.
19+
4. Please use the issue and pull request templates
20+
21+
# Code guidelines
22+
23+
Please follow the [Google Java styleguide](https://google.github.io/styleguide/javaguide.html) and try to match the code style of the rest of the
24+
project. Please also try to document your code, especially in complex and convoluted methods.
25+
26+
### Maintainers
27+
28+
[@cerus](https://github.com/cerus)

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<img width="20%" height="20%" align="left" src="https://cerus.dev/img/mc_map_item.png" alt="Minecraft map item">
2+
3+
<h2 align="center">maps</h2>
4+
<p align="center">maps is a simple Spigot plugin and api for creating clientside maps and map screens. It is the successor
5+
of <a href="https://github.com/cerus/packet-maps">packet-maps</a>.</p>
6+
7+
<hr>
8+
<p align="center"><sub><sup>Made with ♥ by <a href="https://github.com/cerus">Cerus</a></sup></sub></p>
9+
<br>
10+
11+
### Navigation
12+
13+
[Features](#Features)\
14+
[Quick start for developers](#Quick-start-for-developers)\
15+
[Building](#Building)\
16+
[FAQ](#FAQ)\
17+
[Contributing](#Contributing)
18+
19+
<hr>
20+
21+
### Features
22+
23+
> **Please note:** This is not a standalone plugin, it is a toolkit for other plugins. You will only be able to create and manage map screens with this plugin.
24+
25+
• Clientside maps\
26+
• Map screens (arrangement of clientside maps)\
27+
• Simple and reasonably lightweight\
28+
• Easy to use developer api
29+
30+
**What is the point of the plugin module?**\
31+
See [FAQ](#FAQ)
32+
33+
<hr>
34+
35+
### Quick start for developers
36+
37+
> Please take a look at the wiki for an in-depth explanation of the api.
38+
39+
**Maven setup**
40+
41+
```xml
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>dev.cerus.maps</groupId>
46+
<artifactId>common</artifactId>
47+
<version>1.0.0</version>
48+
<scope>provided</scope> <!-- "provided" if the maps plugin is on the server, "compile" if not -->
49+
</dependency>
50+
51+
<!-- You need the plugin module to access the map screen registry of the plugin. -->
52+
<!-- Don't add this dependency if you have your own storage solution. -->
53+
<dependency>
54+
<groupId>dev.cerus.maps</groupId>
55+
<artifactId>plugin</artifactId>
56+
<version>1.0.0</version>
57+
<scope>provided</scope>
58+
</dependency>
59+
</dependencies>
60+
```
61+
62+
**Quickstart**
63+
64+
```java
65+
public class MyPlugin extends JavaPlugin {
66+
67+
@Override
68+
public void onEnable() {
69+
// This example depends on the "common" and "plugin" dependency.
70+
71+
// Something important to keep in mind when using the plugin for storage:
72+
// The plugin loads the map screens 3 seconds after startup. (Check out
73+
// the MapsPlugin.java file for an explanation)
74+
75+
this.getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
76+
for (final MapScreen screen : MapScreenRegistry.getScreens()) {
77+
final MapScreenGraphics graphics = screen.getGraphics();
78+
graphics.fill((byte) MapColor.WHITE_2.getId());
79+
graphics.drawText(5, 5, "There are " + Bukkit.getOnlinePlayers().size() + " players on the server", (byte) MapColor.BLACK_2.getId(), 2);
80+
screen.update(MapScreen.DirtyHandlingPolicy.IGNORE); // Send map screen to all online players
81+
}
82+
}, 4 * 20, 20);
83+
getCommand("mapstest").setExecutor(this);
84+
}
85+
86+
@Override
87+
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
88+
// It's been ages since I've used the normal Bukkit command system
89+
// so please forgive me if I use any bad practices here
90+
if (!(sender instanceof Player player)) {
91+
return true;
92+
}
93+
if (!command.getName().equals("mapstest")) {
94+
return true;
95+
}
96+
97+
// This is really unsafe. Always do proper checks before casting
98+
// things, but this will do for the sake of this quick start.
99+
final ItemStack item = player.getInventory().getItemInMainHand();
100+
final MapMeta mapMeta = (MapMeta) item.getItemMeta();
101+
final int mapId = mapMeta.getMapView().getId();
102+
103+
final ClientsideMap clientsideMap = new ClientsideMap(mapId);
104+
final ClientsideMapGraphics graphics = clientsideMap.getGraphics();
105+
graphics.fill((byte) MapColor.BLACK_2.getId());
106+
graphics.drawText(5, 5, "Hello,", (byte) MapColor.BLACK_2.getId(), 1);
107+
graphics.drawText(5, 5 + MinecraftFont.Font.getHeight() + 5, player.getName(), (byte) MapColor.WHITE_2.getId(), 2);
108+
return true;
109+
}
110+
111+
}
112+
```
113+
114+
<hr>
115+
116+
### Building
117+
118+
Requirements: Java 16, Git, Maven, Craftbukkit 1.17.1 installed in local Maven repo
119+
120+
Simply clone the repository, navigate into the directory and run `mvn clean package`. The plugin will be in `plugin/target` and the api
121+
in `common/target`.
122+
123+
<hr>
124+
125+
### FAQ
126+
127+
**Why is there a plugin module if maps is not a standalone plugin?**\
128+
The plugin handles the creation, management and storage of map screens. You do not need the plugin if you make your own creation, management and
129+
storage solution.
130+
131+
Please feel free to open an issue or contact me if you have any questions that were not answered here.
132+
133+
<hr>
134+
135+
### Contributing
136+
137+
Thank you for your interest in contributing to this project! Before you do anything though please read the [contribution guidelines](CONTRIBUTING.md)
138+
thoroughly. Contributions that do not conform to the guidelines might be rejected.

bukkit-17_R1/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>maps</artifactId>
7+
<groupId>dev.cerus</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>dev.cerus.maps</groupId>
13+
<artifactId>bukkit-17_R1</artifactId>
14+
15+
<properties>
16+
<maven.compiler.target>16</maven.compiler.target>
17+
<maven.compiler.source>16</maven.compiler.source>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>dev.cerus.maps</groupId>
24+
<artifactId>common</artifactId>
25+
<version>1.0.0</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.bukkit</groupId>
30+
<artifactId>craftbukkit</artifactId>
31+
<version>1.17.1-R0.1-SNAPSHOT</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dev.cerus.maps.version;
2+
3+
import dev.cerus.maps.api.ClientsideMap;
4+
import dev.cerus.maps.api.version.VersionAdapter;
5+
import dev.cerus.maps.util.ReflectionUtil;
6+
import java.util.Collections;
7+
import net.minecraft.network.protocol.Packet;
8+
import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata;
9+
import net.minecraft.network.protocol.game.PacketPlayOutMap;
10+
import net.minecraft.network.syncher.DataWatcher;
11+
import net.minecraft.network.syncher.DataWatcherObject;
12+
import net.minecraft.network.syncher.DataWatcherRegistry;
13+
import net.minecraft.world.item.ItemStack;
14+
import net.minecraft.world.level.saveddata.maps.WorldMap;
15+
import org.bukkit.Material;
16+
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer;
17+
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
18+
import org.bukkit.entity.Player;
19+
import org.bukkit.inventory.meta.MapMeta;
20+
21+
public class VersionAdapter17R1 implements VersionAdapter {
22+
23+
@Override
24+
public Object makeMapPacket(final ClientsideMap map) {
25+
return new PacketPlayOutMap(map.getId(),
26+
(byte) 0,
27+
true,
28+
Collections.emptyList(),
29+
new WorldMap.b(0,
30+
0,
31+
128,
32+
128,
33+
map.getGraphics().getData()));
34+
}
35+
36+
@Override
37+
public Object makeFramePacket(final int frameId, final ClientsideMap map) {
38+
final PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(frameId, new DataWatcher(null), false);
39+
40+
final org.bukkit.inventory.ItemStack mapItem = new org.bukkit.inventory.ItemStack(Material.FILLED_MAP, 1);
41+
final MapMeta mapMeta = (MapMeta) mapItem.getItemMeta();
42+
mapMeta.setMapId(map.getId());
43+
mapItem.setItemMeta(mapMeta);
44+
45+
try {
46+
final DataWatcherObject<ItemStack> itemObj = DataWatcherRegistry.g.a((byte) 8);
47+
final DataWatcher.Item<ItemStack> itemItem = new DataWatcher.Item<>(itemObj, CraftItemStack.asNMSCopy(mapItem));
48+
ReflectionUtil.set("b", packet.getClass(), packet, Collections.singletonList(itemItem));
49+
} catch (final NoSuchFieldException | IllegalAccessException e) {
50+
e.printStackTrace();
51+
}
52+
return packet;
53+
}
54+
55+
@Override
56+
public void sendPacket(final Player player, final Object packet) {
57+
((CraftPlayer) player).getHandle().b.sendPacket((Packet<?>) packet);
58+
}
59+
60+
}

common/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>maps</artifactId>
7+
<groupId>dev.cerus</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>dev.cerus.maps</groupId>
13+
<artifactId>common</artifactId>
14+
15+
<properties>
16+
<maven.compiler.target>16</maven.compiler.target>
17+
<maven.compiler.source>16</maven.compiler.source>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<repositories>
22+
<repository>
23+
<id>spigot-repo</id>
24+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
25+
</repository>
26+
</repositories>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.spigotmc</groupId>
31+
<artifactId>spigot-api</artifactId>
32+
<version>1.17.1-R0.1-SNAPSHOT</version>
33+
<scope>provided</scope>
34+
</dependency>
35+
</dependencies>
36+
37+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.cerus.maps.api;
2+
3+
import dev.cerus.maps.api.graphics.ClientsideMapGraphics;
4+
import dev.cerus.maps.api.version.VersionAdapter;
5+
import org.bukkit.entity.Player;
6+
7+
public class ClientsideMap {
8+
9+
private static int COUNTER = Integer.MIN_VALUE;
10+
11+
private final int id;
12+
private final ClientsideMapGraphics graphics;
13+
14+
public ClientsideMap() {
15+
this(COUNTER++);
16+
}
17+
18+
public ClientsideMap(final int id) {
19+
this.id = id;
20+
this.graphics = new ClientsideMapGraphics();
21+
}
22+
23+
public void sendTo(final VersionAdapter versionAdapter, final Player player) {
24+
versionAdapter.sendPacket(player, versionAdapter.makeMapPacket(this));
25+
}
26+
27+
public ClientsideMapGraphics getGraphics() {
28+
return this.graphics;
29+
}
30+
31+
public int getId() {
32+
return this.id;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)