Skip to content

Commit

Permalink
Adds support to SparklyPaper (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Nov 16, 2024
1 parent 438bf69 commit 217d78e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Supplier;

public final class NMSSpawnersImpl implements NMSSpawners {

private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC = new ReflectField<>(
Level.class, List.class, Modifier.PUBLIC | Modifier.FINAL, 1);
private static final boolean isSparklyPaper = ((Supplier<Boolean>) () -> {
try {
Class.forName("net.sparklypower.sparklypaper.BlockEntityTickersList");
return true;
} catch (ClassNotFoundException error) {
return false;
}
}).get();

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

Expand Down Expand Up @@ -89,12 +100,7 @@ public void updateStackedSpawners(Chunk chunk) {
int chunkX = chunk.getX();
int chunkZ = chunk.getZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

List<TickingBlockEntity> watchersToAdd = new LinkedList<>();
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -129,12 +135,7 @@ public void updateStackedSpawner(StackedSpawner stackedSpawner) {
int blockY = location.getBlockY();
int blockZ = location.getBlockZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

TickingBlockEntity watcherToAdd = null;
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -333,4 +334,12 @@ public SyncedCreatureSpawner createSyncedSpawner(CreatureSpawner creatureSpawner
return new SyncedCreatureSpawnerImpl(bukkitWorld, spawnerBlockEntity);
}

private static List<TickingBlockEntity> getBlockEntityTickers(Level level) {
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(level);
if (isSparklyPaper && LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.get(level);
return level.blockEntityTickers;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bgsoftware.wildstacker.nms.v1_20_3.spawner;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildstacker.WildStackerPlugin;
import com.bgsoftware.wildstacker.api.objects.StackedSpawner;
import net.minecraft.core.BlockPos;
Expand All @@ -12,6 +13,9 @@

public class SpawnerWatcherTickingBlockEntity implements TickingBlockEntity {

private static final ReflectMethod<Long> GET_CHUNK_COORD_KEY = new ReflectMethod<>(
TickingBlockEntity.class, "getChunkCoordinateKey");

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

private static final String CLASS_NAME = SpawnerWatcherTickingBlockEntity.class.getSimpleName();
Expand Down Expand Up @@ -92,4 +96,8 @@ public String getType() {
return this.type;
}

public long getChunkCoordinateKey() {
return GET_CHUNK_COORD_KEY.invoke(this.original);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Supplier;

public final class NMSSpawnersImpl implements NMSSpawners {

private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC = new ReflectField<>(
Level.class, List.class, Modifier.PUBLIC | Modifier.FINAL, 1);
private static final boolean isSparklyPaper = ((Supplier<Boolean>) () -> {
try {
Class.forName("net.sparklypower.sparklypaper.BlockEntityTickersList");
return true;
} catch (ClassNotFoundException error) {
return false;
}
}).get();

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

Expand Down Expand Up @@ -89,12 +100,7 @@ public void updateStackedSpawners(Chunk chunk) {
int chunkX = chunk.getX();
int chunkZ = chunk.getZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

List<TickingBlockEntity> watchersToAdd = new LinkedList<>();
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -129,12 +135,7 @@ public void updateStackedSpawner(StackedSpawner stackedSpawner) {
int blockY = location.getBlockY();
int blockZ = location.getBlockZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

TickingBlockEntity watcherToAdd = null;
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -333,4 +334,12 @@ public SyncedCreatureSpawner createSyncedSpawner(CreatureSpawner creatureSpawner
return new SyncedCreatureSpawnerImpl(bukkitWorld, spawnerBlockEntity);
}

private static List<TickingBlockEntity> getBlockEntityTickers(Level level) {
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(level);
if (isSparklyPaper && LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.get(level);
return level.blockEntityTickers;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bgsoftware.wildstacker.nms.v1_20_4.spawner;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildstacker.WildStackerPlugin;
import com.bgsoftware.wildstacker.api.objects.StackedSpawner;
import net.minecraft.core.BlockPos;
Expand All @@ -12,6 +13,9 @@

public class SpawnerWatcherTickingBlockEntity implements TickingBlockEntity {

private static final ReflectMethod<Long> GET_CHUNK_COORD_KEY = new ReflectMethod<>(
TickingBlockEntity.class, "getChunkCoordinateKey");

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

private static final String CLASS_NAME = SpawnerWatcherTickingBlockEntity.class.getSimpleName();
Expand Down Expand Up @@ -76,4 +80,8 @@ public String getType() {
return this.type;
}

public long getChunkCoordinateKey() {
return GET_CHUNK_COORD_KEY.invoke(this.original);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Supplier;

public final class NMSSpawnersImpl implements NMSSpawners {

private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC = new ReflectField<>(
Level.class, List.class, Modifier.PUBLIC | Modifier.FINAL, 1);
private static final boolean isSparklyPaper = ((Supplier<Boolean>) () -> {
try {
Class.forName("net.sparklypower.sparklypaper.BlockEntityTickersList");
return true;
} catch (ClassNotFoundException error) {
return false;
}
}).get();

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

Expand Down Expand Up @@ -89,12 +100,7 @@ public void updateStackedSpawners(Chunk chunk) {
int chunkX = chunk.getX();
int chunkZ = chunk.getZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

List<TickingBlockEntity> watchersToAdd = new LinkedList<>();
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -129,12 +135,7 @@ public void updateStackedSpawner(StackedSpawner stackedSpawner) {
int blockY = location.getBlockY();
int blockZ = location.getBlockZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

TickingBlockEntity watcherToAdd = null;
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -333,4 +334,12 @@ public SyncedCreatureSpawner createSyncedSpawner(CreatureSpawner creatureSpawner
return new SyncedCreatureSpawnerImpl(bukkitWorld, spawnerBlockEntity);
}

private static List<TickingBlockEntity> getBlockEntityTickers(Level level) {
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(level);
if (isSparklyPaper && LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.get(level);
return level.blockEntityTickers;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bgsoftware.wildstacker.nms.v1_21.spawner;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildstacker.WildStackerPlugin;
import com.bgsoftware.wildstacker.api.objects.StackedSpawner;
import net.minecraft.core.BlockPos;
Expand All @@ -12,6 +13,9 @@

public class SpawnerWatcherTickingBlockEntity implements TickingBlockEntity {

private static final ReflectMethod<Long> GET_CHUNK_COORD_KEY = new ReflectMethod<>(
TickingBlockEntity.class, "getChunkCoordinateKey");

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

private static final String CLASS_NAME = SpawnerWatcherTickingBlockEntity.class.getSimpleName();
Expand Down Expand Up @@ -76,4 +80,8 @@ public String getType() {
return this.type;
}

public long getChunkCoordinateKey() {
return GET_CHUNK_COORD_KEY.invoke(this.original);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Supplier;

public final class NMSSpawnersImpl implements NMSSpawners {

private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC = new ReflectField<>(
Level.class, List.class, Modifier.PUBLIC | Modifier.FINAL, 1);
private static final boolean isSparklyPaper = ((Supplier<Boolean>) () -> {
try {
Class.forName("net.sparklypower.sparklypaper.BlockEntityTickersList");
return true;
} catch (ClassNotFoundException error) {
return false;
}
}).get();

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

Expand Down Expand Up @@ -89,12 +100,7 @@ public void updateStackedSpawners(Chunk chunk) {
int chunkX = chunk.getX();
int chunkZ = chunk.getZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

List<TickingBlockEntity> watchersToAdd = new LinkedList<>();
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -129,12 +135,7 @@ public void updateStackedSpawner(StackedSpawner stackedSpawner) {
int blockY = location.getBlockY();
int blockZ = location.getBlockZ();

List<TickingBlockEntity> blockEntityTickers;
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid()) {
blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(serverLevel);
} else {
blockEntityTickers = serverLevel.blockEntityTickers;
}
List<TickingBlockEntity> blockEntityTickers = getBlockEntityTickers(serverLevel);

TickingBlockEntity watcherToAdd = null;
Iterator<TickingBlockEntity> blockEntityIterator = blockEntityTickers.iterator();
Expand Down Expand Up @@ -333,4 +334,12 @@ public SyncedCreatureSpawner createSyncedSpawner(CreatureSpawner creatureSpawner
return new SyncedCreatureSpawnerImpl(bukkitWorld, spawnerBlockEntity);
}

private static List<TickingBlockEntity> getBlockEntityTickers(Level level) {
if (LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PROTECTED.get(level);
if (isSparklyPaper && LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.isValid())
return LEVEL_BLOCK_ENTITY_TICKERS_PUBLIC.get(level);
return level.blockEntityTickers;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bgsoftware.wildstacker.nms.v1_21_3.spawner;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildstacker.WildStackerPlugin;
import com.bgsoftware.wildstacker.api.objects.StackedSpawner;
import net.minecraft.core.BlockPos;
Expand All @@ -12,6 +13,9 @@

public class SpawnerWatcherTickingBlockEntity implements TickingBlockEntity {

private static final ReflectMethod<Long> GET_CHUNK_COORD_KEY = new ReflectMethod<>(
TickingBlockEntity.class, "getChunkCoordinateKey");

private static final WildStackerPlugin plugin = WildStackerPlugin.getPlugin();

private static final String CLASS_NAME = SpawnerWatcherTickingBlockEntity.class.getSimpleName();
Expand Down Expand Up @@ -76,4 +80,8 @@ public String getType() {
return this.type;
}

public long getChunkCoordinateKey() {
return GET_CHUNK_COORD_KEY.invoke(this.original);
}

}

0 comments on commit 217d78e

Please sign in to comment.