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

package com.hypixel.hytale.builtin.adventure.teleporter;

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.builtin.teleport.TeleportPlugin;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.system.RefChangeSystem;
import com.hypixel.hytale.builtin.adventure.teleporter.page.TeleporterSettingsPageSupplier;
import com.hypixel.hytale.codec.lookup.StringCodecMapCodec;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.server.OpenCustomUIInteraction;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.builtin.adventure.teleporter.interaction.server.TeleporterInteraction;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.Interaction;
import com.hypixel.hytale.builtin.adventure.teleporter.system.CreateWarpWhenTeleporterPlacedSystem;
import com.hypixel.hytale.component.system.ISystem;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
import com.hypixel.hytale.builtin.adventure.teleporter.component.Teleporter;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;

public class TeleporterPlugin extends JavaPlugin
{
    private static TeleporterPlugin instance;
    private ComponentType<ChunkStore, Teleporter> teleporterComponentType;
    
    public static TeleporterPlugin get() {
        return TeleporterPlugin.instance;
    }
    
    public TeleporterPlugin(@Nonnull final JavaPluginInit init) {
        super(init);
        TeleporterPlugin.instance = this;
    }
    
    @Override
    protected void setup() {
        this.teleporterComponentType = this.getChunkStoreRegistry().registerComponent(Teleporter.class, "Teleporter", Teleporter.CODEC);
        this.getChunkStoreRegistry().registerSystem((ISystem<ChunkStore>)new TeleporterOwnedWarpRefChangeSystem());
        this.getChunkStoreRegistry().registerSystem(new TeleporterOwnedWarpRefSystem());
        this.getChunkStoreRegistry().registerSystem((ISystem<ChunkStore>)new CreateWarpWhenTeleporterPlacedSystem());
        this.getCodecRegistry(Interaction.CODEC).register("Teleporter", TeleporterInteraction.class, TeleporterInteraction.CODEC);
        this.getCodecRegistry((StringCodecMapCodec<Object, BuilderCodec<TeleporterSettingsPageSupplier>>)OpenCustomUIInteraction.PAGE_CODEC).register("Teleporter", TeleporterSettingsPageSupplier.class, TeleporterSettingsPageSupplier.CODEC);
    }
    
    public ComponentType<ChunkStore, Teleporter> getTeleporterComponentType() {
        return this.teleporterComponentType;
    }
    
    private static class TeleporterOwnedWarpRefChangeSystem extends RefChangeSystem<ChunkStore, Teleporter>
    {
        @Nonnull
        @Override
        public ComponentType<ChunkStore, Teleporter> componentType() {
            return Teleporter.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final Teleporter component, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<ChunkStore> ref, @Nullable final Teleporter oldComponent, @Nonnull final Teleporter newComponent, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            final String ownedWarp = oldComponent.getOwnedWarp();
            if (ownedWarp != null && !ownedWarp.isEmpty() && !ownedWarp.equals(newComponent.getOwnedWarp())) {
                TeleportPlugin.get().getWarps().remove(ownedWarp.toLowerCase());
                TeleportPlugin.get().saveWarps();
                oldComponent.setOwnedWarp(null);
            }
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<ChunkStore> ref, @Nonnull final Teleporter component, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            final String ownedWarp = component.getOwnedWarp();
            if (ownedWarp != null && !ownedWarp.isEmpty()) {
                TeleportPlugin.get().getWarps().remove(ownedWarp.toLowerCase());
                TeleportPlugin.get().saveWarps();
                component.setOwnedWarp(null);
            }
        }
        
        @Nonnull
        @Override
        public Query<ChunkStore> getQuery() {
            return (Query<ChunkStore>)Query.any();
        }
    }
    
    private static class TeleporterOwnedWarpRefSystem extends RefSystem<ChunkStore>
    {
        public static final ComponentType<ChunkStore, Teleporter> COMPONENT_TYPE;
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            switch (reason) {
                case LOAD: {
                    final Teleporter component = commandBuffer.getComponent(ref, TeleporterOwnedWarpRefSystem.COMPONENT_TYPE);
                    final String ownedWarp = component.getOwnedWarp();
                    if (ownedWarp == null || ownedWarp.isEmpty() || !TeleportPlugin.get().getWarps().containsKey(ownedWarp.toLowerCase())) {}
                    break;
                }
            }
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<ChunkStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            if (reason == RemoveReason.REMOVE) {
                final Teleporter component = commandBuffer.getComponent(ref, TeleporterOwnedWarpRefSystem.COMPONENT_TYPE);
                final String ownedWarp = component.getOwnedWarp();
                if (ownedWarp != null && !ownedWarp.isEmpty()) {
                    TeleportPlugin.get().getWarps().remove(ownedWarp.toLowerCase());
                    TeleportPlugin.get().saveWarps();
                    component.setOwnedWarp(null);
                }
            }
        }
        
        @Override
        public Query<ChunkStore> getQuery() {
            return TeleporterOwnedWarpRefSystem.COMPONENT_TYPE;
        }
        
        static {
            COMPONENT_TYPE = Teleporter.getComponentType();
        }
    }
}
