Skip to content

Commit

Permalink
Changed the plugin from overriding the MobSpawnerAbstract field of sp…
Browse files Browse the repository at this point in the history
…awners as it may conflict with other plugins
  • Loading branch information
OmerBenGera committed Sep 23, 2022
1 parent 40065e6 commit 843922a
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 833 deletions.
Expand Up @@ -16,7 +16,7 @@
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
import com.bgsoftware.superiorskyblock.nms.algorithms.NMSCachedBlock;
import com.bgsoftware.superiorskyblock.nms.v117.generator.IslandsGeneratorImpl;
import com.bgsoftware.superiorskyblock.nms.v117.spawners.BaseSpawnerNotifier;
import com.bgsoftware.superiorskyblock.nms.v117.spawners.TickingSpawnerBlockEntityNotifier;
import com.bgsoftware.superiorskyblock.nms.v117.world.PropertiesMapper;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
import com.destroystokyo.paper.antixray.ChunkPacketBlockController;
Expand All @@ -26,14 +26,14 @@
import net.minecraft.network.protocol.game.ClientboundInitializeBorderPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.BaseSpawner;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
Expand All @@ -58,6 +58,8 @@
import org.bukkit.generator.ChunkGenerator;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.IntFunction;

Expand All @@ -67,9 +69,8 @@ public class NMSWorldImpl implements NMSWorld {
private static final ReflectField<Object> CHUNK_PACKET_BLOCK_CONTROLLER = new ReflectField<>(Level.class,
Object.class, "chunkPacketBlockController")
.removeFinal();
private static final ReflectField<BaseSpawner> BAES_SPAWNER = new ReflectField<BaseSpawner>(
SpawnerBlockEntity.class, BaseSpawner.class, Modifier.PRIVATE | Modifier.FINAL, 1)
.removeFinal();
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);

private final SuperiorSkyblockPlugin plugin;
private final Singleton<SignsListener> signsListener;
Expand Down Expand Up @@ -110,13 +111,21 @@ public void listenSpawner(CreatureSpawner creatureSpawner, IntFunction<Integer>
if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity))
return;

BaseSpawner baseSpawner = spawnerBlockEntity.getSpawner();
List<TickingBlockEntity> blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS.get(serverLevel);
Iterator<TickingBlockEntity> blockEntityTickersIterator = blockEntityTickers.iterator();
List<TickingBlockEntity> tickersToAdd = new ArrayList<>();

if (!(baseSpawner instanceof BaseSpawnerNotifier)) {
BaseSpawnerNotifier baseSpawnerNotifier = new BaseSpawnerNotifier(baseSpawner, delayChangeCallback);
BAES_SPAWNER.set(spawnerBlockEntity, baseSpawnerNotifier);
baseSpawnerNotifier.updateDelay();
while (blockEntityTickersIterator.hasNext()) {
TickingBlockEntity tickingBlockEntity = blockEntityTickersIterator.next();
if (tickingBlockEntity.getPos().equals(blockPos) &&
!(tickingBlockEntity instanceof TickingSpawnerBlockEntityNotifier)) {
blockEntityTickersIterator.remove();
tickersToAdd.add(new TickingSpawnerBlockEntityNotifier(spawnerBlockEntity, tickingBlockEntity, delayChangeCallback));
}
}

if (!tickersToAdd.isEmpty())
blockEntityTickers.addAll(tickersToAdd);
}

@Override
Expand Down

This file was deleted.

@@ -0,0 +1,57 @@
package com.bgsoftware.superiorskyblock.nms.v117.spawners;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BaseSpawner;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.entity.TickingBlockEntity;

import java.util.function.IntFunction;

public class TickingSpawnerBlockEntityNotifier implements TickingBlockEntity {

private final SpawnerBlockEntity spawnerBlockEntity;
private final TickingBlockEntity tickingBlockEntity;
private final IntFunction<Integer> delayChangeCallback;

public TickingSpawnerBlockEntityNotifier(SpawnerBlockEntity spawnerBlockEntity, TickingBlockEntity tickingBlockEntity,
IntFunction<Integer> delayChangeCallback) {
this.spawnerBlockEntity = spawnerBlockEntity;
this.tickingBlockEntity = tickingBlockEntity;
this.delayChangeCallback = delayChangeCallback;
updateDelay();
}

@Override
public void tick() {
BaseSpawner baseSpawner = this.spawnerBlockEntity.getSpawner();
int startDelay = baseSpawner.spawnDelay;
try {
tickingBlockEntity.tick();
} finally {
int newDelay = baseSpawner.spawnDelay;
if (newDelay > startDelay)
updateDelay();
}
}

@Override
public boolean isRemoved() {
return tickingBlockEntity.isRemoved();
}

@Override
public BlockPos getPos() {
return tickingBlockEntity.getPos();
}

@Override
public String getType() {
return tickingBlockEntity.getType();
}

public void updateDelay() {
BaseSpawner baseSpawner = spawnerBlockEntity.getSpawner();
baseSpawner.spawnDelay = delayChangeCallback.apply(baseSpawner.spawnDelay);
}

}
Expand Up @@ -17,7 +17,7 @@
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
import com.bgsoftware.superiorskyblock.nms.algorithms.NMSCachedBlock;
import com.bgsoftware.superiorskyblock.nms.v1181.generator.IslandsGeneratorImpl;
import com.bgsoftware.superiorskyblock.nms.v1181.spawners.BaseSpawnerNotifier;
import com.bgsoftware.superiorskyblock.nms.v1181.spawners.TickingSpawnerBlockEntityNotifier;
import com.bgsoftware.superiorskyblock.nms.v1181.world.PropertiesMapper;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
import com.destroystokyo.paper.antixray.ChunkPacketBlockController;
Expand All @@ -36,6 +36,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
Expand Down Expand Up @@ -63,6 +64,8 @@
import org.bukkit.generator.ChunkGenerator;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.IntFunction;

Expand All @@ -72,9 +75,8 @@ public class NMSWorldImpl implements NMSWorld {
private static final ReflectField<Object> CHUNK_PACKET_BLOCK_CONTROLLER = new ReflectField<>(Level.class,
Object.class, "chunkPacketBlockController")
.removeFinal();
private static final ReflectField<BaseSpawner> BAES_SPAWNER = new ReflectField<BaseSpawner>(
SpawnerBlockEntity.class, BaseSpawner.class, Modifier.PRIVATE | Modifier.FINAL, 1)
.removeFinal();
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);

private final SuperiorSkyblockPlugin plugin;
private final Singleton<SignsListener> signsListener;
Expand Down Expand Up @@ -115,13 +117,21 @@ public void listenSpawner(CreatureSpawner creatureSpawner, IntFunction<Integer>
if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity))
return;

BaseSpawner baseSpawner = spawnerBlockEntity.getSpawner();
List<TickingBlockEntity> blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS.get(serverLevel);
Iterator<TickingBlockEntity> blockEntityTickersIterator = blockEntityTickers.iterator();
List<TickingBlockEntity> tickersToAdd = new ArrayList<>();

if (!(baseSpawner instanceof BaseSpawnerNotifier)) {
BaseSpawnerNotifier baseSpawnerNotifier = new BaseSpawnerNotifier(baseSpawner, delayChangeCallback);
BAES_SPAWNER.set(spawnerBlockEntity, baseSpawnerNotifier);
baseSpawnerNotifier.updateDelay();
while (blockEntityTickersIterator.hasNext()) {
TickingBlockEntity tickingBlockEntity = blockEntityTickersIterator.next();
if (tickingBlockEntity.getPos().equals(blockPos) &&
!(tickingBlockEntity instanceof TickingSpawnerBlockEntityNotifier)) {
blockEntityTickersIterator.remove();
tickersToAdd.add(new TickingSpawnerBlockEntityNotifier(spawnerBlockEntity, tickingBlockEntity, delayChangeCallback));
}
}

if (!tickersToAdd.isEmpty())
blockEntityTickers.addAll(tickersToAdd);
}

@Override
Expand Down

0 comments on commit 843922a

Please sign in to comment.