package com.hypixel.hytale.builtin.portals.systems; import com.hypixel.hytale.builtin.portals.components.PortalDevice; import com.hypixel.hytale.component.AddReason; import com.hypixel.hytale.component.CommandBuffer; import com.hypixel.hytale.component.ComponentType; import com.hypixel.hytale.component.Ref; import com.hypixel.hytale.component.RemoveReason; import com.hypixel.hytale.component.Store; import com.hypixel.hytale.component.query.Query; import com.hypixel.hytale.component.system.RefChangeSystem; import com.hypixel.hytale.component.system.RefSystem; import com.hypixel.hytale.server.core.universe.Universe; import com.hypixel.hytale.server.core.universe.world.World; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import javax.annotation.Nullable; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl; public final class CloseWorldWhenBreakingDeviceSystems { private CloseWorldWhenBreakingDeviceSystems() { } private static void maybeCloseFragmentWorld(@Nullable PortalDevice device) { if (device != null) { World world = device.getDestinationWorld(); if (world != null) { if (world.getPlayerCount() <= 0) { Universe.get().removeWorld(world.getName()); } } } } public static class ComponentRemoved extends RefChangeSystem { @Override public ComponentType componentType() { return PortalDevice.getComponentType(); } public void onComponentAdded( @NonNullDecl Ref ref, @NonNullDecl PortalDevice component, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer ) { } public void onComponentSet( @NonNullDecl Ref ref, @NullableDecl PortalDevice oldComponent, @NonNullDecl PortalDevice newComponent, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer ) { } public void onComponentRemoved( @NonNullDecl Ref ref, @NonNullDecl PortalDevice component, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer ) { CloseWorldWhenBreakingDeviceSystems.maybeCloseFragmentWorld(component); } @Override public Query getQuery() { return this.componentType(); } } public static class EntityRemoved extends RefSystem { @Override public void onEntityAdded( @NonNullDecl Ref ref, @NonNullDecl AddReason reason, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer ) { } @Override public void onEntityRemove( @NonNullDecl Ref ref, @NonNullDecl RemoveReason reason, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer ) { PortalDevice device = store.getComponent(ref, PortalDevice.getComponentType()); CloseWorldWhenBreakingDeviceSystems.maybeCloseFragmentWorld(device); } @Override public Query getQuery() { return PortalDevice.getComponentType(); } } }