Skip to content

Commit 81907b9

Browse files
PotatoCraft-Studio#267 Initial 1.19.3 Support for VirtualDisplayItem
1 parent 0fa5bd9 commit 81907b9

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@
361361
<id>sk89q-repo</id>
362362
<url>https://maven.sk89q.com/repo/</url>
363363
</repository>-->
364+
<repository>
365+
<id>dmulloy2-repo</id>
366+
<url>https://repo.dmulloy2.net/repository/public/</url>
367+
</repository>
364368
<repository>
365369
<id>CodeMC</id>
366370
<url>https://repo.codemc.io/repository/maven-public/</url>

src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import com.comphenix.protocol.reflect.StructureModifier;
2929
import com.comphenix.protocol.utility.MinecraftVersion;
3030
import com.comphenix.protocol.wrappers.WrappedChatComponent;
31+
import com.comphenix.protocol.wrappers.WrappedDataValue;
3132
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
33+
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
3234
import org.bukkit.Chunk;
3335
import org.bukkit.Location;
3436
import org.bukkit.Material;
@@ -46,7 +48,16 @@
4648
import org.maxgamer.quickshop.util.GameVersion;
4749
import org.maxgamer.quickshop.util.Util;
4850

49-
import java.util.*;
51+
import java.util.ArrayList;
52+
import java.util.Collection;
53+
import java.util.Collections;
54+
import java.util.Iterator;
55+
import java.util.List;
56+
import java.util.Map;
57+
import java.util.Objects;
58+
import java.util.Optional;
59+
import java.util.Set;
60+
import java.util.UUID;
5061
import java.util.concurrent.ConcurrentHashMap;
5162
import java.util.concurrent.ConcurrentSkipListSet;
5263
import java.util.concurrent.atomic.AtomicBoolean;
@@ -391,7 +402,7 @@ private static PacketContainer createFakeItemSpawnPacket(int entityID, Location
391402
default:
392403
//For 1.14+, we should use EntityType
393404
fakeItemPacket.getEntityTypeModifier().write(0, EntityType.DROPPED_ITEM);
394-
if (VERSION != GameVersion.v1_19_R1) {
405+
if (VERSION.ordinal() < GameVersion.v1_19_R1.ordinal()) {
395406
//int data to marking there have velocity (at last field)
396407
integerStructureModifier.write(integerStructureModifier.getFields().size() - 1, 1);
397408
}
@@ -445,7 +456,7 @@ private static PacketContainer createFakeItemMetaPacket(int entityID, ItemStack
445456

446457
//Must in the certain slot:https://wiki.vg/Entity_metadata#Item
447458
//Is 1.17-?
448-
if (GameVersion.v1_17_R1.ordinal() > VERSION.ordinal()) {
459+
if (VERSION.ordinal() < GameVersion.v1_17_R1.ordinal()) {
449460
// if (version == GameVersion.v1_13_R1 || version == GameVersion.v1_13_R2) {
450461
// //For 1.13 is 6
451462
// wpw.setObject(6, WrappedDataWatcher.Registry.getItemStackSerializer(false), itemStack);
@@ -458,7 +469,24 @@ private static PacketContainer createFakeItemMetaPacket(int entityID, ItemStack
458469
wpw.setObject(8, WrappedDataWatcher.Registry.getItemStackSerializer(false), itemStack);
459470
}
460471
//Add it
461-
fakeItemMetaPacket.getWatchableCollectionModifier().write(0, wpw.getWatchableObjects());
472+
//For 1.19.2+, we need to use DataValue instead of WatchableObject
473+
if (VERSION.ordinal() > GameVersion.v1_19_R1.ordinal()) {
474+
//Check for new version protocolLib
475+
try {
476+
Class.forName("com.comphenix.protocol.wrappers.WrappedDataValue");
477+
} catch (ClassNotFoundException e) {
478+
throw new RuntimeException("Unable to initialize packet, ProtocolLib update needed", e);
479+
}
480+
//Convert List<WrappedWatchableObject> to List<WrappedDataValue>
481+
List<WrappedWatchableObject> wrappedWatchableObjects = wpw.getWatchableObjects();
482+
List<WrappedDataValue> wrappedDataValues = new java.util.ArrayList<>(wrappedWatchableObjects.size());
483+
for (WrappedWatchableObject watchableObject : wrappedWatchableObjects) {
484+
wrappedDataValues.set(watchableObject.getIndex(), new WrappedDataValue(watchableObject.getHandle()));
485+
}
486+
fakeItemMetaPacket.getDataValueCollectionModifier().write(0, wrappedDataValues);
487+
} else {
488+
fakeItemMetaPacket.getWatchableCollectionModifier().write(0, wpw.getWatchableObjects());
489+
}
462490
return fakeItemMetaPacket;
463491
}
464492

@@ -480,7 +508,7 @@ private static PacketContainer createFakeItemVelocityPacket(int entityID) {
480508
private static PacketContainer createFakeItemDestroyPacket(int entityID) {
481509
//Also make a DestroyPacket to remove it
482510
PacketContainer fakeItemDestroyPacket = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.ENTITY_DESTROY);
483-
if (GameVersion.v1_17_R1.ordinal() > VERSION.ordinal()) {
511+
if (VERSION.ordinal() < GameVersion.v1_17_R1.ordinal()) {
484512
//On 1.17-, we need to write an integer array
485513
//Entity to remove
486514
fakeItemDestroyPacket.getIntegerArrays().write(0, new int[]{entityID});
@@ -492,7 +520,7 @@ private static PacketContainer createFakeItemDestroyPacket(int entityID) {
492520
//Entity to remove
493521
fakeItemDestroyPacket.getIntegers().write(0, entityID);
494522
} else {
495-
//On 1.17.1 (may be 1.17.1+? it's enough, Mojang, stop the changes), we need add the int list
523+
//On 1.17.1 (maybe 1.17.1+? it's enough, Mojang, stop the changes), we need add the int list
496524
//Entity to remove
497525
try {
498526
fakeItemDestroyPacket.getIntLists().write(0, Collections.singletonList(entityID));

0 commit comments

Comments
 (0)