// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.portals.systems;

import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.system.RefSystem;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.system.RefChangeSystem;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.Universe;
import javax.annotation.Nullable;
import com.hypixel.hytale.builtin.portals.components.PortalDevice;

public final class CloseWorldWhenBreakingDeviceSystems
{
    private CloseWorldWhenBreakingDeviceSystems() {
    }
    
    private static void maybeCloseFragmentWorld(@Nullable final PortalDevice device) {
        if (device == null) {
            return;
        }
        final World world = device.getDestinationWorld();
        if (world == null || world.getPlayerCount() > 0) {
            return;
        }
        Universe.get().removeWorld(world.getName());
    }
    
    public static class ComponentRemoved extends RefChangeSystem<ChunkStore, PortalDevice>
    {
        @Override
        public ComponentType<ChunkStore, PortalDevice> componentType() {
            return PortalDevice.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final PortalDevice component, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<ChunkStore> ref, @Nullable final PortalDevice oldComponent, @Nonnull final PortalDevice newComponent, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<ChunkStore> ref, @Nonnull final PortalDevice component, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            CloseWorldWhenBreakingDeviceSystems.maybeCloseFragmentWorld(component);
        }
        
        @Override
        public Query<ChunkStore> getQuery() {
            return this.componentType();
        }
    }
    
    public static class EntityRemoved extends RefSystem<ChunkStore>
    {
        @Override
        public void onEntityAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<ChunkStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            final PortalDevice device = store.getComponent(ref, PortalDevice.getComponentType());
            CloseWorldWhenBreakingDeviceSystems.maybeCloseFragmentWorld(device);
        }
        
        @Override
        public Query<ChunkStore> getQuery() {
            return PortalDevice.getComponentType();
        }
    }
}
