Skip to content

Commit 931ebe1

Browse files
committed
Add click handling (3.3.0)
1 parent efd3a10 commit 931ebe1

File tree

37 files changed

+1333
-40
lines changed

37 files changed

+1333
-40
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target/
22
.idea/
33
*.iml
4-
dependency-reduced-pom.xml
4+
dependency-reduced-pom.xml
5+
javadoc/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ See [FAQ](#FAQ)
4848
<dependency>
4949
<groupId>dev.cerus.maps</groupId>
5050
<artifactId>common</artifactId>
51-
<version>3.2.0</version>
51+
<version>3.3.0</version>
5252
<scope>provided</scope> <!-- "provided" if the maps plugin is on the server, "compile" if not -->
5353
</dependency>
5454

@@ -57,7 +57,7 @@ See [FAQ](#FAQ)
5757
<dependency>
5858
<groupId>dev.cerus.maps</groupId>
5959
<artifactId>plugin</artifactId>
60-
<version>3.2.0</version>
60+
<version>3.3.0</version>
6161
<scope>provided</scope>
6262
</dependency>
6363
</dependencies>

bukkit-16_R3/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>parent</artifactId>
77
<groupId>dev.cerus.maps</groupId>
8-
<version>3.2.0</version>
8+
<version>3.3.0</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dev.cerus.maps.version;
2+
3+
import dev.cerus.maps.api.version.PacketListener;
4+
import io.netty.channel.ChannelDuplexHandler;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import net.minecraft.server.v1_16_R3.EnumHand;
7+
import net.minecraft.server.v1_16_R3.MovingObjectPositionBlock;
8+
import net.minecraft.server.v1_16_R3.PacketPlayInBlockDig;
9+
import net.minecraft.server.v1_16_R3.PacketPlayInBlockPlace;
10+
import net.minecraft.server.v1_16_R3.PacketPlayInUseEntity;
11+
import net.minecraft.server.v1_16_R3.PacketPlayInUseItem;
12+
import org.bukkit.Bukkit;
13+
import org.bukkit.Location;
14+
import org.bukkit.craftbukkit.v1_16_R3.block.CraftBlock;
15+
import org.bukkit.entity.Player;
16+
import org.bukkit.plugin.java.JavaPlugin;
17+
18+
public class PacketHandler16R3 extends ChannelDuplexHandler {
19+
20+
private final Player player;
21+
private final PacketListener listener;
22+
private final JavaPlugin plugin;
23+
24+
public PacketHandler16R3(final Player player, final PacketListener listener, final JavaPlugin plugin) {
25+
this.player = player;
26+
this.listener = listener;
27+
this.plugin = plugin;
28+
}
29+
30+
@Override
31+
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
32+
if ((((msg instanceof PacketPlayInUseItem useItem && useItem.b() != EnumHand.OFF_HAND) || msg instanceof PacketPlayInBlockPlace)
33+
&& this.listener.handlePlayerRightClick(this.player)) || (msg instanceof PacketPlayInUseEntity useEntity && useEntity.d() == null
34+
&& (useEntity.b() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK ? this.listener.handlePlayerLeftClick(this.player)
35+
: this.listener.handlePlayerRightClick(this.player))) || (msg instanceof PacketPlayInBlockDig
36+
&& this.listener.handlePlayerLeftClick(this.player))) {
37+
if (msg instanceof PacketPlayInBlockDig dig) {
38+
// To prevent de-syncs we need to tell the client that the block has not changed
39+
final Location location = new Location(
40+
this.player.getWorld(),
41+
dig.b().getX(),
42+
dig.b().getY(),
43+
dig.b().getZ()
44+
);
45+
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
46+
}
47+
if (msg instanceof PacketPlayInUseItem useItem) {
48+
// To prevent de-syncs we need to tell the client that the block has not changed
49+
final MovingObjectPositionBlock pos = useItem.c();
50+
if (pos != null && pos.getBlockPosition() != null) {
51+
final Location location = new Location(
52+
this.player.getWorld(),
53+
pos.getBlockPosition().getX(),
54+
pos.getBlockPosition().getY(),
55+
pos.getBlockPosition().getZ()
56+
).getBlock().getRelative(CraftBlock.notchToBlockFace(pos.getDirection())).getLocation();
57+
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
58+
}
59+
}
60+
return;
61+
}
62+
super.channelRead(ctx, msg);
63+
}
64+
65+
}

bukkit-16_R3/src/main/java/dev/cerus/maps/version/VersionAdapter16R3.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.cerus.maps.api.ClientsideMap;
44
import dev.cerus.maps.api.Frame;
5+
import dev.cerus.maps.api.version.PacketListener;
56
import dev.cerus.maps.api.version.VersionAdapter;
67
import dev.cerus.maps.util.ReflectionUtil;
78
import java.util.Arrays;
@@ -26,6 +27,7 @@
2627
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
2728
import org.bukkit.entity.Player;
2829
import org.bukkit.inventory.meta.MapMeta;
30+
import org.bukkit.plugin.java.JavaPlugin;
2931

3032
public class VersionAdapter16R3 implements VersionAdapter {
3133

@@ -116,4 +118,10 @@ public void sendPacket(final Player player, final Object packet) {
116118
((CraftPlayer) player).getHandle().playerConnection.sendPacket((Packet<?>) packet);
117119
}
118120

121+
@Override
122+
public void inject(final Player player, final PacketListener listener, final JavaPlugin plugin) {
123+
((CraftPlayer) player).getHandle().playerConnection.a().channel.pipeline()
124+
.addBefore("packet_handler", "maps_listener", new PacketHandler16R3(player, listener, plugin));
125+
}
126+
119127
}

bukkit-17_R1/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>parent</artifactId>
77
<groupId>dev.cerus.maps</groupId>
8-
<version>3.2.0</version>
8+
<version>3.3.0</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package dev.cerus.maps.version;
2+
3+
import dev.cerus.maps.api.version.PacketListener;
4+
import io.netty.channel.ChannelDuplexHandler;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import java.lang.reflect.Field;
7+
import java.util.Arrays;
8+
import net.minecraft.network.protocol.game.PacketPlayInBlockDig;
9+
import net.minecraft.network.protocol.game.PacketPlayInBlockPlace;
10+
import net.minecraft.network.protocol.game.PacketPlayInUseEntity;
11+
import net.minecraft.network.protocol.game.PacketPlayInUseItem;
12+
import net.minecraft.world.EnumHand;
13+
import net.minecraft.world.phys.MovingObjectPositionBlock;
14+
import org.bukkit.Bukkit;
15+
import org.bukkit.Location;
16+
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock;
17+
import org.bukkit.entity.Player;
18+
import org.bukkit.plugin.java.JavaPlugin;
19+
20+
public class PacketHandler17R1 extends ChannelDuplexHandler {
21+
22+
private static final Field actionField;
23+
private static final Object attackAction;
24+
25+
static {
26+
try {
27+
actionField = PacketPlayInUseEntity.class.getDeclaredField("b");
28+
actionField.setAccessible(true);
29+
final Field attackActionField = PacketPlayInUseEntity.class.getDeclaredField("d");
30+
attackActionField.setAccessible(true);
31+
attackAction = attackActionField.get(null);
32+
} catch (final NoSuchFieldException | IllegalAccessException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
37+
private final Player player;
38+
private final PacketListener listener;
39+
private final JavaPlugin plugin;
40+
41+
public PacketHandler17R1(final Player player, final PacketListener listener, final JavaPlugin plugin) {
42+
this.player = player;
43+
this.listener = listener;
44+
this.plugin = plugin;
45+
}
46+
47+
private static Object getAction(final PacketPlayInUseEntity packet) {
48+
try {
49+
return actionField.get(packet);
50+
} catch (final IllegalAccessException e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
55+
private static EnumHand getHand(final PacketPlayInUseEntity packet) {
56+
try {
57+
final Object action = getAction(packet);
58+
final Field handField = Arrays.stream(action.getClass().getDeclaredFields())
59+
.filter(field -> field.getType() == EnumHand.class)
60+
.findAny().orElse(null);
61+
if (handField == null) {
62+
return null;
63+
}
64+
handField.setAccessible(true);
65+
return (EnumHand) handField.get(action);
66+
} catch (final IllegalAccessException e) {
67+
throw new RuntimeException(e);
68+
}
69+
}
70+
71+
@Override
72+
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
73+
if ((((msg instanceof PacketPlayInUseItem useItem && useItem.b() != EnumHand.b) || msg instanceof PacketPlayInBlockPlace)
74+
&& this.listener.handlePlayerRightClick(this.player)) || (msg instanceof PacketPlayInUseEntity useEntity && getHand(useEntity) != EnumHand.b
75+
&& (getAction(useEntity) == attackAction ? this.listener.handlePlayerLeftClick(this.player)
76+
: this.listener.handlePlayerRightClick(this.player))) || (msg instanceof PacketPlayInBlockDig
77+
&& this.listener.handlePlayerLeftClick(this.player))) {
78+
if (msg instanceof PacketPlayInBlockDig dig) {
79+
// To prevent de-syncs we need to tell the client that the block has not changed
80+
final Location location = new Location(
81+
this.player.getWorld(),
82+
dig.b().getX(),
83+
dig.b().getY(),
84+
dig.b().getZ()
85+
);
86+
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
87+
}
88+
if (msg instanceof PacketPlayInUseItem useItem) {
89+
// To prevent de-syncs we need to tell the client that the block has not changed
90+
final MovingObjectPositionBlock pos = useItem.c();
91+
if (pos != null && pos.getBlockPosition() != null) {
92+
final Location location = new Location(
93+
this.player.getWorld(),
94+
pos.getBlockPosition().getX(),
95+
pos.getBlockPosition().getY(),
96+
pos.getBlockPosition().getZ()
97+
).getBlock().getRelative(CraftBlock.notchToBlockFace(pos.getDirection())).getLocation();
98+
Bukkit.getScheduler().runTask(this.plugin, () -> this.player.sendBlockChange(location, location.getBlock().getBlockData()));
99+
}
100+
}
101+
return;
102+
}
103+
super.channelRead(ctx, msg);
104+
}
105+
106+
}

bukkit-17_R1/src/main/java/dev/cerus/maps/version/VersionAdapter17R1.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.cerus.maps.api.ClientsideMap;
44
import dev.cerus.maps.api.Frame;
5+
import dev.cerus.maps.api.version.PacketListener;
56
import dev.cerus.maps.api.version.VersionAdapter;
67
import dev.cerus.maps.util.ReflectionUtil;
78
import java.util.Arrays;
@@ -27,15 +28,16 @@
2728
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
2829
import org.bukkit.entity.Player;
2930
import org.bukkit.inventory.meta.MapMeta;
31+
import org.bukkit.plugin.java.JavaPlugin;
3032

3133
public class VersionAdapter17R1 implements VersionAdapter {
3234

3335
@Override
3436
public Object makeMapPacket(final boolean ignoreBounds, final ClientsideMap map) {
3537
final int x = ignoreBounds ? 0 : map.getX();
3638
final int y = ignoreBounds ? 0 : map.getY();
37-
final int w = ignoreBounds ? 128 : map.getWidth();
38-
final int h = ignoreBounds ? 128 : map.getHeight();
39+
final int w = ignoreBounds ? 128 : Math.max(1, map.getWidth());
40+
final int h = ignoreBounds ? 128 : Math.max(1, map.getHeight());
3941

4042
final byte[] data;
4143
if (ignoreBounds) {
@@ -59,7 +61,7 @@ public Object makeMapPacket(final boolean ignoreBounds, final ClientsideMap map)
5961
cursor.getCompressedX(),
6062
cursor.getCompressedY(),
6163
cursor.getDirection(),
62-
IChatBaseComponent.ChatSerializer.a(cursor.getCaptionString())
64+
!cursor.hasCaption() ? null : IChatBaseComponent.ChatSerializer.a(cursor.getCaptionString())
6365
))
6466
.collect(Collectors.toList()),
6567
new WorldMap.b(
@@ -134,4 +136,10 @@ public void sendPacket(final Player player, final Object packet) {
134136
((CraftPlayer) player).getHandle().b.sendPacket((Packet<?>) packet);
135137
}
136138

139+
@Override
140+
public void inject(final Player player, final PacketListener listener, final JavaPlugin plugin) {
141+
((CraftPlayer) player).getHandle().b.a().k.pipeline()
142+
.addBefore("packet_handler", "maps_listener", new PacketHandler17R1(player, listener, plugin));
143+
}
144+
137145
}

bukkit-18_R1/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>parent</artifactId>
77
<groupId>dev.cerus.maps</groupId>
8-
<version>3.2.0</version>
8+
<version>3.3.0</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

0 commit comments

Comments
 (0)