package org.spongepowered.test;

import com.google.inject.Inject;
import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;

import java.util.UUID;

@Plugin(id = "cause-stack-test", name = "Cause Stack Test", description = "A plugin testing the cause stack", version = "0.0.0")
public class CauseStackTest {
    
    @Inject private Logger logger;
    
    @Listener(order = Order.FIRST, beforeModifications = true)
    public void onGameStartedServer(GameStartedServerEvent event) {
        UUID randomUUID = UUID.randomUUID();
        
        // Initial Cause
        Sponge.getCauseStackManager().pushCause(randomUUID);
        
        // old_min_depth: 0 (Gets incremented to 2 after creating StackFrame).
        // lastCauseSize: 0.
        try (CauseStackManager.StackFrame stackFrameFirst = Sponge.getCauseStackManager().pushCauseFrame()) {
            // These have to be the same in order to increase the duplicateCauses count.
            String duplicateCause = "Duplicate Cause Test";
            
            // Initial Cause
            stackFrameFirst.pushCause(duplicateCause);
            
            // Duplicate Causes
            stackFrameFirst.pushCause(duplicateCause);
            stackFrameFirst.pushCause(duplicateCause);
            stackFrameFirst.pushCause(duplicateCause);
            stackFrameFirst.pushCause(duplicateCause);
            stackFrameFirst.pushCause(duplicateCause);
            
            // old_min_depth: 2 (Plugin, UUID) (Gets incremented to 3 after creating StackFrame).
            // lastCauseSize: 5.
            try (CauseStackManager.StackFrame stackFrameSecond = Sponge.getCauseStackManager().pushCauseFrame()) {
                // This will result in index 2 (Which will be our randomUUID) of the duplicateCauses in the SpongeCauseStackManager to be set to 5.
            }
        }
        
        // Pop initial push.
        Sponge.getCauseStackManager().popCause();
        
        // Check for duplication.
        int count = 0;
        while (Sponge.getCauseStackManager().peekCause() == randomUUID) {
            Sponge.getCauseStackManager().popCause();
            count++;
        }
        
        if (count > 0) {
            this.logger.error("===================================================================");
            this.logger.error("CauseStack Duplication detected.");
            this.logger.error(count + " Objects were found when 0 was expected.");
            this.logger.error("===================================================================");
        }
    }
}
@@ -1,8 +1,1256 @@
+/*%0A * This file is part of Sponge, licensed under the MIT License (MIT).%0A *%0A * Copyright (c) SpongePowered %3Chttps://www.spongepowered.org%3E%0A * Copyright (c) contributors%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A * THE SOFTWARE.%0A */%0A
 package 

Add License