Skip to content

Commit

Permalink
feat: refactoring, new visualization using display entities, more ref…
Browse files Browse the repository at this point in the history
…actoring
  • Loading branch information
cerus committed Nov 7, 2023
1 parent aedc0e5 commit df1d3e8
Show file tree
Hide file tree
Showing 26 changed files with 800 additions and 292 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ Compatible with Spigot & Paper 1.16.5, 1.17, 1.17.1, 1.18.1, 1.18.2, 1.19, 1.19.

## How it works

**1.16-1.19.3:**\
The plugin downloads the Minecraft textures directly from Mojang and displays them on invisible maps. Please note that not all items and blocks can be
displayed (for example stairs).

**1.19.4+:**\
The plugin utilizes the new item display entities to display the items. All items and blocks are supported, even custom ones from resource packs.

## Installation

1. Download plugin
Expand All @@ -23,9 +27,11 @@ It's that simple :)

## Media

[YouTube showcase video](https://www.youtube.com/watch?v=dEABvxvE0Zo)
[YouTube showcase video (UPDATED)](https://youtu.be/FoRYLi5V6rQ)\
[YouTube showcase video (OLD)](https://www.youtube.com/watch?v=dEABvxvE0Zo)

<details>
<summary>Images</summary>
<img src="https://i.imgur.com/ZiF6f8J.png" alt="Screenshot of plugin in action">
<img src="https://i.imgur.com/kLYqSFe.png" alt="Screenshot of plugin in action">
<img src="https://i.imgur.com/ZiF6f8J.png" alt="Old screenshot of plugin in action">
</details>
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.0.11</version>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,113 @@
package dev.cerus.visualcrafting.api.version;

import dev.cerus.visualcrafting.api.math.MatrixMath;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.joml.Matrix3f;
import org.joml.Matrix4f;

/**
* A fake item display
*/
public class FakeItemDisplay {

private final Location location;
private final ItemStack itemStack;
private final BlockFace rotationX;
private final BlockFace rotationY;
private final Transform transform;
private static final float SCALE = 0.18f;

private ItemStack itemStack;
private Matrix3f rotationX;
private Matrix3f rotationZ;
private Matrix4f translation;
private Transform transform;
private Location location;

public FakeItemDisplay(final Location location,
final ItemStack itemStack,
final BlockFace rotationX,
final BlockFace rotationY,
final Matrix3f rotationX,
final Matrix3f rotationZ,
final Matrix4f translation,
final Transform transform) {
this.location = location;
this.itemStack = itemStack;
this.rotationX = rotationX;
this.rotationY = rotationY;
this.rotationZ = rotationZ;
this.translation = translation;
this.transform = transform;
}

/**
* Create the transformation matrix for displaying an item
*
* @return the matrix
*/
public Matrix4f getTransformationMatrix() {
return MatrixMath.combine(
this.translation,
MatrixMath.combineAndExpand(
this.rotationX,
this.rotationZ
),
MatrixMath.scale(SCALE, SCALE, 0.0001f)
);
}

public Location getLocation() {
return this.location;
}

public void setLocation(final Location location) {
this.location = location;
}

public ItemStack getItemStack() {
return this.itemStack;
}

public BlockFace getRotationX() {
public void setItemStack(final ItemStack itemStack) {
this.itemStack = itemStack;
}

public Matrix3f getRotationX() {
return this.rotationX;
}

public BlockFace getRotationY() {
return this.rotationY;
public void setRotationX(final Matrix3f rotationX) {
this.rotationX = rotationX;
}

public Matrix3f getRotationZ() {
return this.rotationZ;
}

public void setRotationZ(final Matrix3f rotationZ) {
this.rotationZ = rotationZ;
}

public Matrix4f getTranslation() {
return this.translation;
}

public void setTranslation(final Matrix4f translation) {
this.translation = translation;
}

public Transform getTransform() {
return this.transform;
}

public void setTransform(final Transform transform) {
this.transform = transform;
}

public enum Transform {
NONE,
THIRDPERSON_LEFTHAND,
THIRDPERSON_RIGHTHAND,
FIRSTPERSON_LEFTHAND,
FIRSTPERSON_RIGHTHAND,
FIXED,
GROUND,
GUI,
HEAD,
NONE,
THIRDPERSON_LEFTHAND,
THIRDPERSON_RIGHTHAND
GUI,
GROUND,
FIXED
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.cerus.visualcrafting.api.version;

import dev.cerus.visualcrafting.api.config.Config;
import java.util.EnumSet;
import java.util.function.BiConsumer;
import org.bukkit.Location;
import org.bukkit.Rotation;
Expand All @@ -13,8 +14,8 @@
*/
public abstract class VersionAdapter {

protected static final Feature[] FEATURES_BASE = new Feature[] {Feature.MAPS};
protected static final Feature[] FEATURES_DISPLAY = new Feature[] {Feature.MAPS, Feature.ITEM_DISPLAYS};
protected static final EnumSet<Feature> FEATURES_BASE = EnumSet.of(Feature.MAPS);
protected static final EnumSet<Feature> FEATURES_DISPLAY = EnumSet.of(Feature.MAPS, Feature.ITEM_DISPLAYS);

/**
* Initialize the adapter
Expand Down Expand Up @@ -51,10 +52,27 @@ public abstract class VersionAdapter {
*/
public abstract void updateItemFrame(int frameId, ItemStack itemStack, Rotation rotation, boolean invisible);

/**
* Spawn a fake item display and return the display's id
*
* @param itemDisplay The item display
*
* @return Display entity id
*/
public int spawnItemDisplay(final FakeItemDisplay itemDisplay) {
throw new UnsupportedOperationException();
}

/**
* Update a fake item display
*
* @param displayId The entity id of the display
* @param itemDisplay The display
*/
public void updateItemDisplay(final int displayId, final FakeItemDisplay itemDisplay) {
throw new UnsupportedOperationException();
}

/**
* Remove an entity
*
Expand Down Expand Up @@ -103,7 +121,7 @@ protected byte[] getMapData(final FakeMap fakeMap) {
*
* @return All the features this implementation implements
*/
public Feature[] getImplementedFeatures() {
public EnumSet<Feature> getImplementedFeatures() {
return FEATURES_BASE;
}

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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</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.0.11</version>
<version>1.1.0</version>
</parent>

<artifactId>bukkit-19R3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dev.cerus.visualcrafting.v19r3;

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;
Expand Down Expand Up @@ -68,7 +72,7 @@ public void inject(final Player player) {
.addBefore("packet_handler", "visual_crafting", new ChannelDuplexHandler() {
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
if (msg instanceof PacketPlayInUseEntity useEntity) {
if (msg instanceof final PacketPlayInUseEntity useEntity) {
VersionAdapter19R3.this.handlePacketIn(player, useEntity);
}
super.channelRead(ctx, msg);
Expand Down Expand Up @@ -124,6 +128,26 @@ public int spawnItemFrame(final Location location, final BlockFace direction) {
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.ae,
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(
Expand All @@ -134,6 +158,20 @@ public void updateItemFrame(final int frameId, final ItemStack itemStack, final
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.b<>(10, DataWatcherRegistry.A, nmsTransf.d()),
new DataWatcher.b<>(11, DataWatcherRegistry.A, nmsTransf.f()),
new DataWatcher.b<>(12, DataWatcherRegistry.B, nmsTransf.e()),
new DataWatcher.b<>(13, DataWatcherRegistry.B, nmsTransf.g()),
new DataWatcher.b<>(22, DataWatcherRegistry.h, CraftItemStack.asNMSCopy(itemDisplay.getItemStack())),
new DataWatcher.b<>(23, 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);
Expand Down Expand Up @@ -161,6 +199,11 @@ public void sendMap(final FakeMap 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().b.a(packet);
}
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.0.11</version>
<version>1.1.0</version>
</parent>

<artifactId>bukkit-20R1</artifactId>
Expand Down
Loading

0 comments on commit df1d3e8

Please sign in to comment.