Skip to content

Commit

Permalink
1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cerus committed Nov 18, 2024
1 parent c48c5f8 commit 685e561
Show file tree
Hide file tree
Showing 19 changed files with 484 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-16R3/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-17R1/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R1/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R2/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R1/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R2/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.visualcrafting</groupId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

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

<artifactId>bukkit-19R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-20R1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-20R2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-20R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-20R4</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-21R1</artifactId>
Expand Down
36 changes: 36 additions & 0 deletions bukkit-21R2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.5</version>
</parent>

<artifactId>bukkit-21R2</artifactId>

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

<dependencies>
<dependency>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>api</artifactId>
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.21.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package dev.cerus.visualcrafting.v21r2;

import com.mojang.math.Transformation;
import dev.cerus.visualcrafting.api.config.Config;
import dev.cerus.visualcrafting.api.version.FakeItemDisplay;
import dev.cerus.visualcrafting.api.version.FakeMap;
import dev.cerus.visualcrafting.api.version.Feature;
import dev.cerus.visualcrafting.api.version.VersionAdapter;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.UUID;
import java.util.function.BiConsumer;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.PacketPlayInUseEntity;
import net.minecraft.network.protocol.game.PacketPlayOutEntityDestroy;
import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata;
import net.minecraft.network.protocol.game.PacketPlayOutMap;
import net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity;
import net.minecraft.network.syncher.DataWatcher;
import net.minecraft.network.syncher.DataWatcherRegistry;
import net.minecraft.server.network.PlayerConnection;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.level.saveddata.maps.MapId;
import net.minecraft.world.level.saveddata.maps.WorldMap;
import net.minecraft.world.phys.Vec3D;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Rotation;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_21_R2.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_21_R2.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class VersionAdapter21R2 extends VersionAdapter {

private Config config;
private BiConsumer<Player, Integer> entityClickCallback;
private int nextEntityId;
private int nextMapId;
private Field netManField;

private NetworkManager getNetworkManager(final PlayerConnection b) {
try {
if (this.netManField == null) {
this.netManField = ServerCommonPacketListenerImpl.class.getDeclaredField("e");
this.netManField.setAccessible(true);
}
return (NetworkManager) this.netManField.get(b);
} catch (final NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Override
public void init(final Config config, final BiConsumer<Player, Integer> entityClickCallback) {
this.config = config;
this.entityClickCallback = entityClickCallback;
this.nextEntityId = config.entityIdRangeMin();
this.nextMapId = config.mapIdRangeMin();
}

@Override
public void inject(final Player player) {
if (this.config.enablePacketListening()) {
final NetworkManager netMan = this.getNetworkManager(((CraftPlayer) player).getHandle().f);
netMan.n.pipeline()
.addBefore("packet_handler", "visual_crafting", new ChannelDuplexHandler() {
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
if (msg instanceof final PacketPlayInUseEntity useEntity) {
VersionAdapter21R2.this.handlePacketIn(player, useEntity);
}
super.channelRead(ctx, msg);
}
});
}
}

private void handlePacketIn(final Player player, final PacketPlayInUseEntity packet) {
try {
final Field a = packet.getClass().getDeclaredField("b");
a.setAccessible(true);
this.entityClickCallback.accept(player, (Integer) a.get(packet));
} catch (final NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}

@Override
public int spawnItemFrame(final Location location, final BlockFace direction) {
final int eid = this.getNewEntityId();
final PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(
eid,
UUID.randomUUID(),
location.getBlockX(),
location.getBlockY(),
location.getBlockZ(),
direction == BlockFace.DOWN ? 90 : direction == BlockFace.UP ? -90 : 0,
switch (direction) {
case NORTH -> -180;
case EAST -> -90;
case WEST -> 90;
default -> 0;
},
EntityTypes.at,
switch (direction) {
case UP -> 1;
case NORTH -> 2;
case SOUTH -> 3;
case WEST -> 4;
case EAST -> 5;
default -> 0;
},
new Vec3D(0, 0, 0),
switch (direction) {
case NORTH -> -180;
case EAST -> -90;
case WEST -> 90;
default -> 0;
}
);
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
return eid;
}

@Override
public int spawnItemDisplay(final FakeItemDisplay itemDisplay) {
final int eid = this.getNewEntityId();
final PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(
eid,
UUID.randomUUID(),
itemDisplay.getLocation().getBlockX(),
itemDisplay.getLocation().getBlockY(),
itemDisplay.getLocation().getBlockZ(),
0,
0,
EntityTypes.as,
0,
new Vec3D(0, 0, 0),
0
);
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
return eid;
}

@Override
public void updateItemFrame(final int frameId, final ItemStack itemStack, final Rotation rotation, final boolean invisible) {
final PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(frameId, Arrays.asList(
new DataWatcher.c<>(8, DataWatcherRegistry.h, CraftItemStack.asNMSCopy(itemStack)),
new DataWatcher.c<>(9, DataWatcherRegistry.b, rotation.ordinal()),
new DataWatcher.c<>(0, DataWatcherRegistry.a, (byte) (invisible ? 0x20 : 0))
));
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
}

@Override
public void updateItemDisplay(final int displayId, final FakeItemDisplay itemDisplay) {
final Transformation nmsTransf = new Transformation(itemDisplay.getTransformationMatrix());
final PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(displayId, Arrays.asList(
new DataWatcher.c<>(11, DataWatcherRegistry.D, nmsTransf.d()),
new DataWatcher.c<>(12, DataWatcherRegistry.D, nmsTransf.f()),
new DataWatcher.c<>(13, DataWatcherRegistry.E, nmsTransf.e()),
new DataWatcher.c<>(14, DataWatcherRegistry.E, nmsTransf.g()),
new DataWatcher.c<>(23, DataWatcherRegistry.h, CraftItemStack.asNMSCopy(itemDisplay.getItemStack())),
new DataWatcher.c<>(24, DataWatcherRegistry.a, (byte) itemDisplay.getTransform().ordinal())
));
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
}

@Override
public void destroyEntity(final int entityId) {
final PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(entityId);
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
}

@Override
public FakeMap createMap() {
int mapId = getNewMapId();
return this.createMap(mapId, new MapId(mapId));
}

@Override
public void sendMap(final FakeMap map) {
final PacketPlayOutMap packet = new PacketPlayOutMap(
(MapId) getMapHandle(map),
(byte) 0,
true,
Collections.emptyList(),
new WorldMap.c(0,
0,
128,
128,
this.getMapData(map))
);
Bukkit.getOnlinePlayers().forEach(player -> this.sendPacket(player, packet));
}

@Override
public EnumSet<Feature> getImplementedFeatures() {
return FEATURES_DISPLAY;
}

private void sendPacket(final Player player, final Packet<?> packet) {
((CraftPlayer) player).getHandle().f.b(packet);
}

private int getNewEntityId() {
if (this.nextEntityId >= this.config.entityIdRangeMax()) {
this.nextEntityId = this.config.entityIdRangeMin();
return this.nextEntityId;
} else {
return this.nextEntityId++;
}
}

private int getNewMapId() {
if (this.nextMapId >= this.config.mapIdRangeMax()) {
this.nextMapId = this.config.mapIdRangeMin();
return this.nextMapId;
} else {
return this.nextMapId++;
}
}

}
Loading

0 comments on commit 685e561

Please sign in to comment.