Skip to content

Commit d167e21

Browse files
authored
Merge pull request NovaverseMC#5 from FearGames/1.18
1.18 Support
2 parents 36cb1dc + 40f7493 commit d167e21

14 files changed

Lines changed: 949 additions & 362 deletions

File tree

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package it.feargames.tileculling.adapter;
22

33
import com.comphenix.protocol.events.PacketContainer;
4-
import io.netty.buffer.ByteBuf;
54
import org.bukkit.Location;
65
import org.bukkit.block.BlockState;
76
import org.bukkit.block.data.BlockData;
87
import org.bukkit.entity.Player;
98

9+
import java.util.function.Function;
10+
1011
public interface IAdapter {
1112

1213
void updateBlockState(Player player, Location location, BlockData blockData);
1314

1415
void updateBlockData(Player player, Location location, BlockState block);
1516

16-
ByteBuf packetDataSerializer(ByteBuf byteBuf);
17-
18-
int readVarInt(ByteBuf byteBuf);
19-
20-
void writeVarInt(ByteBuf byteBuf, int value);
17+
void transformPacket(Player player, PacketContainer container, Function<String, Boolean> tileEntityTypeFilter);
2118

22-
int getChunkPacketBitmask(PacketContainer packet);
2319
}

plugin/pom.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<artifactId>tileculling-v1_17_R1</artifactId>
2626
<version>1.2.3-SNAPSHOT</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>it.feargames.tileculling</groupId>
30+
<artifactId>tileculling-v1_18_R1</artifactId>
31+
<version>1.2.3-SNAPSHOT</version>
32+
</dependency>
2833
<dependency>
2934
<groupId>com.logisticscraft</groupId>
3035
<artifactId>occlusionculling</artifactId>
@@ -34,10 +39,22 @@
3439

3540
<build>
3641
<plugins>
42+
<plugin>
43+
<artifactId>maven-dependency-plugin</artifactId>
44+
<executions>
45+
<execution>
46+
<id>package-dependencies</id>
47+
<phase>prepare-package</phase>
48+
<goals>
49+
<goal>analyze-only</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
3754
<plugin>
3855
<groupId>org.apache.maven.plugins</groupId>
3956
<artifactId>maven-shade-plugin</artifactId>
40-
<version>3.2.1</version>
57+
<version>3.3.0-SNAPSHOT</version>
4158
<executions>
4259
<execution>
4360
<id>shade</id>
@@ -54,6 +71,20 @@
5471
<shadedPattern>it.feargames.tileculling.libs.com.logisticscraft.occlusionculling</shadedPattern>
5572
</relocation>
5673
</relocations>
74+
<filters>
75+
<filter>
76+
<artifact>*:*</artifact>
77+
<excludes>
78+
<exclude>META-INF/*.SF</exclude>
79+
<exclude>META-INF/*.DSA</exclude>
80+
<exclude>META-INF/*.RSA</exclude>
81+
<exclude>META-INF/*.RSA</exclude>
82+
<exclude>META-INF/*.MF</exclude>
83+
<exclude>META-INF/DEPENDENCIES</exclude>
84+
<exclude>META-INF/**/module-info.class</exclude>
85+
</excludes>
86+
</filter>
87+
</filters>
5788
</configuration>
5889
</plugin>
5990
</plugins>

plugin/src/main/java/it/feargames/tileculling/ChunkTileVisibilityManager.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public ChunkTileVisibilityManager(SettingsHolder settings, IAdapter adapter, Pla
3939
public void updateVisibility(Player player) {
4040
World world = player.getWorld();
4141
Location playerEyeLocation = player.getEyeLocation();
42-
if (playerEyeLocation.getY() > 255 || playerEyeLocation.getY() < 0) { // TODO: 1.17
43-
return;
44-
}
4542

4643
viewerPosition.set(playerEyeLocation.getX(), playerEyeLocation.getY(), playerEyeLocation.getZ());
4744

plugin/src/main/java/it/feargames/tileculling/CullingPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void onEnable() {
3535

3636
try {
3737
String nmsVersion = getServer().getClass().getName().split("\\.")[3].substring(1);
38+
getLogger().info("Loading NMS adapter: " + nmsVersion);
3839
//noinspection unchecked
3940
Class<? extends IAdapter> adapterClass = (Class<? extends IAdapter>) Class.forName("it.feargames.tileculling.adapter.Adapter_" + nmsVersion);
4041
Objects.requireNonNull(adapterClass, "Missing NMS adapter for this MC version!");

plugin/src/main/java/it/feargames/tileculling/occlusionculling/PaperDataProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public boolean isOpaqueFullCube(int x, int y, int z) {
4343
if (snapshot == null) {
4444
throw new IllegalStateException("Chunk not loaded into DataProvider!");
4545
}
46+
if (y < world.getMinHeight() || y > world.getMaxHeight()) {
47+
return false;
48+
}
4649
int relativeX = x & 0xF;
4750
int relativeZ = z & 0xF;
4851
Material material = snapshot.getBlockType(relativeX, y, relativeZ);

plugin/src/main/java/it/feargames/tileculling/protocol/ChunkPacketListener.java

Lines changed: 1 addition & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
import com.comphenix.protocol.PacketType;
44
import com.comphenix.protocol.events.*;
5-
import com.comphenix.protocol.wrappers.nbt.NbtBase;
6-
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
7-
import io.netty.buffer.ByteBuf;
8-
import io.netty.buffer.Unpooled;
95
import it.feargames.tileculling.CullingPlugin;
106
import it.feargames.tileculling.HiddenTileRegistry;
117
import it.feargames.tileculling.PlayerChunkTracker;
128
import it.feargames.tileculling.adapter.IAdapter;
139
import it.feargames.tileculling.util.LocationUtilities;
14-
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
15-
import it.unimi.dsi.fastutil.ints.IntSet;
1610
import org.bukkit.entity.Player;
1711

1812
import java.util.Arrays;
19-
import java.util.Iterator;
20-
import java.util.List;
2113

2214
public class ChunkPacketListener extends PacketAdapter {
2315

@@ -42,159 +34,11 @@ public void onPacketSending(PacketEvent event) {
4234
int chunkZ = packet.getIntegers().read(1);
4335
long chunkKey = LocationUtilities.getChunkKey(chunkX, chunkZ);
4436
if (packet.getType() == PacketType.Play.Server.MAP_CHUNK) {
45-
transformPacket(packet);
37+
adapter.transformPacket(player, packet, hiddenTileRegistry::shouldHide);
4638
playerChunkTracker.trackChunk(player, chunkKey);
4739
} else if (packet.getType() == PacketType.Play.Server.UNLOAD_CHUNK) {
4840
playerChunkTracker.untrackChunk(player, chunkKey);
4941
}
5042
}
5143

52-
public void transformPacket(PacketContainer packet) {
53-
//long start = System.nanoTime();
54-
//int chunkX = packet.getIntegers().read(0);
55-
//int chunkZ = packet.getIntegers().read(1);
56-
57-
List<NbtBase<?>> tileEntities = packet.getListNbtModifier().read(0);
58-
if (tileEntities.isEmpty()) {
59-
return;
60-
}
61-
62-
63-
int bitMask = adapter.getChunkPacketBitmask(packet);
64-
byte[] chunkData = packet.getByteArrays().read(0);
65-
66-
IntSet removedBlocks = null;
67-
for (Iterator<NbtBase<?>> iterator = tileEntities.iterator(); iterator.hasNext(); ) {
68-
NbtBase<?> tileEntity = iterator.next();
69-
NbtCompound compound = (NbtCompound) tileEntity;
70-
71-
String type = compound.getString("id");
72-
if (!hiddenTileRegistry.shouldHide(type)) {
73-
continue;
74-
}
75-
76-
iterator.remove();
77-
78-
if (removedBlocks == null) {
79-
removedBlocks = new IntOpenHashSet();
80-
}
81-
82-
/*
83-
System.err.println("===========================================");
84-
System.err.println("POS " + compound.getInteger("x") + " " + compound.getInteger("y") + " " + compound.getInteger("z"));
85-
System.err.println("CHUNK " + (compound.getInteger("x") >> 4) + (compound.getInteger("z") >> 4));
86-
System.err.println("SECTION " + (compound.getInteger("y") >> 4));
87-
System.err.println("REL " + (compound.getInteger("x") & 0xF) + " " + (compound.getInteger("y") & 0xF) + " " + (compound.getInteger("z") & 0xF));
88-
System.err.println("===========================================");
89-
*/
90-
91-
int rawY = compound.getInteger("y");
92-
byte section = (byte) (rawY >> 4);
93-
byte x = (byte) (compound.getInteger("x") & 0xF);
94-
byte y = (byte) (rawY & 0xF);
95-
byte z = (byte) (compound.getInteger("z") & 0xF);
96-
97-
int key = section + (x << 8) + (y << 16) + (z << 24);
98-
removedBlocks.add(key);
99-
100-
//plugin.getLogger().warning(chunkX + "," + chunkZ + ": Found " + type + " in section " + section + " at " + x + "," + y + "," + z + " key: " + key);
101-
}
102-
if (removedBlocks == null) {
103-
//plugin.getLogger().warning(chunkX + "," + chunkZ + ": No removed blocks. Took " + (System.nanoTime() - start));
104-
return;
105-
}
106-
107-
packet.getListNbtModifier().write(0, tileEntities);
108-
109-
ByteBuf reader = adapter.packetDataSerializer(Unpooled.wrappedBuffer(chunkData));
110-
ByteBuf writer = adapter.packetDataSerializer(Unpooled.buffer(chunkData.length));
111-
112-
for (int sectionY = 0; sectionY < 16; sectionY++) {
113-
if ((bitMask & (1 << sectionY)) == 0) {
114-
continue;
115-
}
116-
117-
short nonAirBlockCount = reader.readShort();
118-
writer.writeShort(nonAirBlockCount);
119-
120-
byte bitsPerBlock = reader.readByte();
121-
if (bitsPerBlock < 4 || bitsPerBlock > 64) {
122-
throw new RuntimeException("Invalid bits per block! (" + bitsPerBlock + ")");
123-
}
124-
writer.writeByte(bitsPerBlock);
125-
126-
int paletteAirIndex;
127-
if (bitsPerBlock > 8) {
128-
// Direct
129-
paletteAirIndex = 0; // Global index
130-
} else {
131-
// Indirect
132-
paletteAirIndex = -1;
133-
134-
int paletteLength = adapter.readVarInt(reader);
135-
adapter.writeVarInt(writer, paletteLength);
136-
137-
for (int i = 0; i < paletteLength; i++) {
138-
int globalId = adapter.readVarInt(reader);
139-
adapter.writeVarInt(writer, globalId);
140-
141-
if (globalId == 0) {
142-
paletteAirIndex = i;
143-
}
144-
}
145-
146-
if (paletteAirIndex == -1) {
147-
throw new RuntimeException("No air found in palette!");
148-
}
149-
}
150-
151-
int dataArrayLength = adapter.readVarInt(reader);
152-
adapter.writeVarInt(writer, dataArrayLength);
153-
154-
long[] dataArray = new long[dataArrayLength];
155-
for (int i = 0; i < dataArrayLength; i++) {
156-
dataArray[i] = reader.readLong();
157-
}
158-
159-
//int individualValueMask = ((1 << bitsPerBlock) - 1);
160-
byte blocksPerLong = (byte) (64 / bitsPerBlock);
161-
162-
for (byte y = 0; y < 16; y++) {
163-
for (byte z = 0; z < 16; z++) {
164-
for (byte x = 0; x < 16; x++) {
165-
int key = sectionY + (x << 8) + (y << 16) + (z << 24);
166-
if (!removedBlocks.contains(key)) {
167-
continue;
168-
}
169-
170-
short blockNumber = (short) ((((y * 16) + z) * 16) + x);
171-
short longIndex = (short) (blockNumber / blocksPerLong);
172-
173-
long previous = dataArray[longIndex];
174-
byte startOffset = (byte) ((blockNumber % blocksPerLong) * bitsPerBlock);
175-
176-
long data = previous;
177-
for (int i = 0; i < bitsPerBlock; i++) {
178-
if ((paletteAirIndex & (1 << i)) == 1) {
179-
data |= (1L << i + startOffset);
180-
} else {
181-
data &= ~(1L << i + startOffset);
182-
}
183-
}
184-
dataArray[longIndex] = data;
185-
}
186-
}
187-
}
188-
189-
for (int i = 0; i < dataArrayLength; i++) {
190-
writer.writeLong(dataArray[i]);
191-
}
192-
}
193-
194-
// Replace data
195-
chunkData = writer.array();
196-
packet.getByteArrays().write(0, chunkData);
197-
198-
//plugin.getLogger().warning(chunkX + "," + chunkZ + ": Processed. Took " + (System.nanoTime() - start));
199-
}
20044
}

plugin/src/main/java/it/feargames/tileculling/util/PaperUtilities.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ public final class PaperUtilities {
1111

1212
static {
1313
try {
14-
GET_NO_TICK_VIEW_DISTANCE_METHOD = World.class.getDeclaredMethod("getNoTickViewDistance");
14+
try {
15+
// 1.18+
16+
GET_NO_TICK_VIEW_DISTANCE_METHOD = World.class.getDeclaredMethod("getViewDistance");
17+
} catch (NoSuchMethodException ignored) {
18+
// Paper 1.16.5 - 1.17
19+
GET_NO_TICK_VIEW_DISTANCE_METHOD = World.class.getDeclaredMethod("getNoTickViewDistance");
20+
}
1521
} catch (NoSuchMethodException ignored) {
22+
// Spigot 1.16.5 - 1.17 (fallback to view distance)
1623
}
1724
}
1825

pom.xml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,32 @@
1313
<module>nms</module>
1414
<module>v1_16_R3</module>
1515
<module>v1_17_R1</module>
16+
<module>v1_18_R1</module>
1617
<module>plugin</module>
1718
</modules>
1819

1920
<properties>
21+
<!-- Environment properties -->
2022
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2123
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<jdk.version>17</jdk.version>
2225
</properties>
2326

27+
<pluginRepositories>
28+
<pluginRepository>
29+
<id>apache.snapshots</id>
30+
<url>https://repository.apache.org/snapshots/</url>
31+
</pluginRepository>
32+
</pluginRepositories>
33+
2434
<repositories>
25-
<repository>
26-
<id>spigot-repo</id>
27-
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
28-
</repository>
2935
<repository>
3036
<id>codemc-repo</id>
31-
<url>https://repo.codemc.io/repository/maven-public/</url>
37+
<url>https://repo.codemc.org/repository/maven-public/</url>
3238
</repository>
3339
<repository>
3440
<id>codemc-nms</id>
35-
<url>https://repo.codemc.io/repository/nms/</url>
41+
<url>https://repo.codemc.org/repository/nms/</url>
3642
</repository>
3743
<repository>
3844
<id>dmulloy2-repo</id>
@@ -44,7 +50,7 @@
4450
<dependency>
4551
<groupId>org.spigotmc</groupId>
4652
<artifactId>spigot-api</artifactId>
47-
<version>1.16.5-R0.1-SNAPSHOT</version>
53+
<version>1.18.1-R0.1-SNAPSHOT</version>
4854
<scope>provided</scope>
4955
</dependency>
5056
<dependency>
@@ -62,11 +68,10 @@
6268
<dependency>
6369
<groupId>com.comphenix.protocol</groupId>
6470
<artifactId>ProtocolLib</artifactId>
65-
<version>4.7.0-SNAPSHOT</version>
71+
<version>4.8.0-SNAPSHOT</version>
6672
<scope>provided</scope>
6773
</dependency>
6874
</dependencies>
69-
7075
<build>
7176
<defaultGoal>clean package</defaultGoal>
7277
<resources>
@@ -76,18 +81,20 @@
7681
</resource>
7782
</resources>
7883
<plugins>
84+
<!-- Include resource files -->
7985
<plugin>
8086
<groupId>org.apache.maven.plugins</groupId>
8187
<artifactId>maven-resources-plugin</artifactId>
82-
<version>3.1.0</version>
88+
<version>3.2.0</version>
8389
</plugin>
90+
<!-- Compile and include classes -->
8491
<plugin>
8592
<groupId>org.apache.maven.plugins</groupId>
8693
<artifactId>maven-compiler-plugin</artifactId>
87-
<version>3.8.0</version>
94+
<version>3.8.1</version>
8895
<configuration>
89-
<source>8</source>
90-
<target>8</target>
96+
<source>${jdk.version}</source>
97+
<target>${jdk.version}</target>
9198
</configuration>
9299
</plugin>
93100
</plugins>

0 commit comments

Comments
 (0)