3434import net .minecraft .world .level .levelgen .feature .configurations .TreeConfiguration ;
3535import net .minecraftforge .common .MinecraftForge ;
3636import net .minecraftforge .event .RegisterCommandsEvent ;
37+ import net .minecraftforge .event .entity .player .PlayerEvent ;
38+ import net .minecraftforge .event .level .LevelEvent ;
3739import net .minecraftforge .event .server .ServerStartingEvent ;
40+ import net .minecraftforge .event .server .ServerStoppingEvent ;
3841import net .minecraftforge .eventbus .api .IEventBus ;
3942import net .minecraftforge .eventbus .api .SubscribeEvent ;
4043import net .minecraftforge .fml .common .Mod ;
@@ -67,6 +70,7 @@ public class VoxelSniperForge implements IVoxelsniper {
6770
6871 private final Map <UUID , ForgePlayer > players = new HashMap <>();
6972 private final Map <String , ForgeWorld > worlds = new HashMap <>();
73+
7074 public static VoxelSniperForge getInstance () {
7175 return instance ;
7276 }
@@ -86,6 +90,7 @@ public VoxelSniperForge() {
8690
8791 // Register ourselves for server and other game events we are interested in
8892 MinecraftForge .EVENT_BUS .register (this );
93+ MinecraftForge .EVENT_BUS .register (new ForgeVoxelSniperListener (this ));
8994 }
9095
9196 private void commonSetup (final FMLCommonSetupEvent event ) {
@@ -107,19 +112,32 @@ public final void registerPermissionNodes(final PermissionGatherEvent.Nodes even
107112 public void onServerStarting (ServerStartingEvent event ) {
108113 this .fileHandler = new ForgeFileHandler (this );
109114 SchematicReader .initialize ();
110- MinecraftForge .EVENT_BUS .register (new ForgeVoxelSniperListener (this ));
111115 Messages .load (this );
112116
113117 voxelSniperConfiguration = new VoxelSniperConfiguration (this );
114- // Bukkit.getPluginManager().registerEvents(this.voxelSniperListener, this);
115- // Bukkit.getPluginManager().registerEvents(this, this);
116- // getLogger().info("Registered Sniper Listener.");
117118
118119 var level = ServerLifecycleHooks .getCurrentServer ().getAllLevels ().iterator ().next ();
119120 this .biomeRegistry = level .registryAccess ().registryOrThrow (Registries .BIOME );
120121 this .featureRegistry = level .registryAccess ().registryOrThrow (Registries .CONFIGURED_FEATURE );
121122 VoxelCommandManager .getInstance ().registerBrushSubcommands ();
123+ }
122124
125+ @ SubscribeEvent
126+ public void onServerStopping (ServerStoppingEvent event ) {
127+ worlds .clear ();
128+ players .clear ();
129+ }
130+
131+ @ SubscribeEvent
132+ public void onLevelUnload (LevelEvent .Unload event ) {
133+ if (event .getLevel () instanceof ServerLevel ) {
134+ worlds .remove (event .getLevel ().toString ());
135+ }
136+ }
137+
138+ @ SubscribeEvent
139+ public void onPlayerLeave (PlayerEvent .PlayerLoggedOutEvent event ) {
140+ players .remove (event .getEntity ().getUUID ());
123141 }
124142
125143 // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
@@ -144,7 +162,9 @@ public IPlayer getPlayer(String name) {
144162 return getPlayer (ServerLifecycleHooks .getCurrentServer ().getPlayerList ().getPlayerByName (name ));
145163 }
146164
147- public IPlayer getPlayer (@ NotNull ServerPlayer p ) {
165+ @ Nullable
166+ public IPlayer getPlayer (@ Nullable ServerPlayer p ) {
167+ if (p == null ) return null ;
148168 if (this .players .get (p .getUUID ()) != null ) return this .players .get (p .getUUID ());
149169 ForgePlayer res = new ForgePlayer (p );
150170 this .players .put (res .getUniqueId (), res );
0 commit comments