Skip to content

Commit

Permalink
feat: 1.20.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
cerus committed Sep 21, 2023
1 parent 1e06555 commit 73bd285
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ of <a href="https://github.com/cerus/packet-maps">packet-maps</a>.</p>
• Advanced engine features
like [alpha compositing](https://en.wikipedia.org/wiki/Alpha_compositing) ([Image](https://cerus.dev/img/maps_alpha_composition.png))\
• Efficient click handling\
• Supports 1.16.5 - 1.20
• Supports 1.16.5 - 1.20.2

**What is the point of the plugin module?**\
See [FAQ](#FAQ)
Expand All @@ -50,7 +50,7 @@ See [FAQ](#FAQ)
<dependency>
<groupId>dev.cerus.maps</groupId>
<artifactId>common</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<scope>provided</scope> <!-- "provided" if the maps plugin is on the server, "compile" if not -->
</dependency>

Expand All @@ -59,7 +59,7 @@ See [FAQ](#FAQ)
<dependency>
<groupId>dev.cerus.maps</groupId>
<artifactId>plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -136,7 +136,7 @@ public class MyPlugin extends JavaPlugin {

### Building

Requirements: Java 16, Git, Maven, CraftBukkit 1.16.5, 1.17.1, 1.18.1, 1.18.2, 1.19.1, 1.19.3, 1.19.4 and 1.20 installed in local Maven repo
Requirements: Java 16, Git, Maven, CraftBukkit 1.16.5, 1.17.1, 1.18.1, 1.18.2, 1.19.1, 1.19.3, 1.19.4, 1.20 and 1.20.2 installed in local Maven repo

Simply clone the repository, navigate into the directory and run `mvn clean package`. The plugin will be in `plugin/target` and the api
in `common/target`.
Expand Down
2 changes: 1 addition & 1 deletion bukkit-16_R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-17_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18_R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19_R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.maps</groupId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19_R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.maps</groupId>
<artifactId>parent</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>

<artifactId>bukkit-19_R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.maps</groupId>
<artifactId>parent</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
</parent>

<artifactId>bukkit-20_R1</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions bukkit-20_R2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>dev.cerus.maps</groupId>
<artifactId>parent</artifactId>
<version>3.8.1</version>
</parent>

<artifactId>bukkit-20_R2</artifactId>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>dev.cerus.maps</groupId>
<artifactId>common</artifactId>
<version>${parent.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package dev.cerus.maps.version;

import dev.cerus.maps.api.version.PacketListener;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Field;
import java.util.Arrays;
import net.minecraft.network.protocol.game.ClientboundBlockChangedAckPacket;
import net.minecraft.network.protocol.game.PacketPlayInBlockDig;
import net.minecraft.network.protocol.game.PacketPlayInBlockPlace;
import net.minecraft.network.protocol.game.PacketPlayInUseEntity;
import net.minecraft.network.protocol.game.PacketPlayInUseItem;
import net.minecraft.world.EnumHand;
import net.minecraft.world.phys.MovingObjectPositionBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_20_R2.block.CraftBlock;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class PacketHandler20R2 extends ChannelDuplexHandler {

private static final Field actionField;
private static final Object attackAction;

static {
try {
actionField = PacketPlayInUseEntity.class.getDeclaredField("b");
actionField.setAccessible(true);
final Field attackActionField = PacketPlayInUseEntity.class.getDeclaredField("d");
attackActionField.setAccessible(true);
attackAction = attackActionField.get(null);
} catch (final NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private final Player player;
private final PacketListener listener;
private final JavaPlugin plugin;

public PacketHandler20R2(final Player player, final PacketListener listener, final JavaPlugin plugin) {
this.player = player;
this.listener = listener;
this.plugin = plugin;
}

private static Object getAction(final PacketPlayInUseEntity packet) {
try {
return actionField.get(packet);
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private static EnumHand getHand(final PacketPlayInUseEntity packet) {
try {
final Object action = getAction(packet);
final Field handField = Arrays.stream(action.getClass().getDeclaredFields())
.filter(field -> field.getType() == EnumHand.class)
.findAny().orElse(null);
if (handField == null) {
return null;
}
handField.setAccessible(true);
return (EnumHand) handField.get(action);
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
if ((((msg instanceof final PacketPlayInUseItem useItem && useItem.a() != EnumHand.b) || msg instanceof PacketPlayInBlockPlace)
&& this.listener.handlePlayerRightClick(this.player)) || (msg instanceof final PacketPlayInUseEntity useEntity && getHand(useEntity) != EnumHand.b
&& (getAction(useEntity) == attackAction ? this.listener.handlePlayerLeftClick(this.player)
: this.listener.handlePlayerRightClick(this.player))) || (msg instanceof PacketPlayInBlockDig
&& this.listener.handlePlayerLeftClick(this.player))) {
if (msg instanceof final PacketPlayInBlockDig dig) {
// To prevent de-syncs we need to tell the client that the block has not changed
final Location location = new Location(
this.player.getWorld(),
dig.a().u(),
dig.a().v(),
dig.a().w()
);
// If we don't acknowledge the client's block change it won't accept further block change packets
((CraftPlayer) this.player).getHandle().c.a(new ClientboundBlockChangedAckPacket(dig.f()));
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
}
if (msg instanceof final PacketPlayInUseItem useItem) {
// To prevent de-syncs we need to tell the client that the block has not changed
final MovingObjectPositionBlock pos = useItem.d();
if (pos != null && pos.a() != null) {
final Location location = new Location(
this.player.getWorld(),
pos.a().u(),
pos.a().v(),
pos.a().w()
).getBlock().getRelative(CraftBlock.notchToBlockFace(pos.b())).getLocation();
// If we don't acknowledge the client's block change it won't accept further block change packets
((CraftPlayer) this.player).getHandle().c.a(new ClientboundBlockChangedAckPacket(useItem.e()));
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
}
}
return;
}
super.channelRead(ctx, msg);
}

}
Loading

0 comments on commit 73bd285

Please sign in to comment.