Skip to content

Commit

Permalink
Merge pull request #215 from KevinDaGame/forge/fix/clear_cache_betwee…
Browse files Browse the repository at this point in the history
…n_game_instances

Clear worlds and player cache when logical server stops
  • Loading branch information
KevinDaGame authored Aug 6, 2023
2 parents 75c88a9 + 2087312 commit c9de9ed
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -67,6 +70,7 @@ public class VoxelSniperForge implements IVoxelsniper {

private final Map<UUID, ForgePlayer> players = new HashMap<>();
private final Map<String, ForgeWorld> worlds = new HashMap<>();

public static VoxelSniperForge getInstance() {
return instance;
}
Expand All @@ -86,6 +90,7 @@ public VoxelSniperForge() {

// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.register(new ForgeVoxelSniperListener(this));
}

private void commonSetup(final FMLCommonSetupEvent event) {
Expand All @@ -107,19 +112,32 @@ public final void registerPermissionNodes(final PermissionGatherEvent.Nodes even
public void onServerStarting(ServerStartingEvent event) {
this.fileHandler = new ForgeFileHandler(this);
SchematicReader.initialize();
MinecraftForge.EVENT_BUS.register(new ForgeVoxelSniperListener(this));
Messages.load(this);

voxelSniperConfiguration = new VoxelSniperConfiguration(this);
// Bukkit.getPluginManager().registerEvents(this.voxelSniperListener, this);
// Bukkit.getPluginManager().registerEvents(this, this);
// getLogger().info("Registered Sniper Listener.");

var level = ServerLifecycleHooks.getCurrentServer().getAllLevels().iterator().next();
this.biomeRegistry = level.registryAccess().registryOrThrow(Registries.BIOME);
this.featureRegistry = level.registryAccess().registryOrThrow(Registries.CONFIGURED_FEATURE);
VoxelCommandManager.getInstance().registerBrushSubcommands();
}

@SubscribeEvent
public void onServerStopping(ServerStoppingEvent event) {
worlds.clear();
players.clear();
}

@SubscribeEvent
public void onLevelUnload(LevelEvent.Unload event) {
if (event.getLevel() instanceof ServerLevel) {
worlds.remove(event.getLevel().toString());
}
}

@SubscribeEvent
public void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event) {
players.remove(event.getEntity().getUUID());
}

// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
Expand All @@ -144,7 +162,9 @@ public IPlayer getPlayer(String name) {
return getPlayer(ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayerByName(name));
}

public IPlayer getPlayer(@NotNull ServerPlayer p) {
@Nullable
public IPlayer getPlayer(@Nullable ServerPlayer p) {
if (p == null) return null;
if (this.players.get(p.getUUID()) != null) return this.players.get(p.getUUID());
ForgePlayer res = new ForgePlayer(p);
this.players.put(res.getUniqueId(), res);
Expand Down

0 comments on commit c9de9ed

Please sign in to comment.