<commit_before>package com.elmakers.mine.bukkit.block;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.elmakers.mine.bukkit.spell.UndoableSpell;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;

import com.elmakers.mine.bukkit.api.block.BlockData;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.block.batch.UndoBatch;
import com.elmakers.mine.bukkit.entity.EntityData;

/**
 * Implements a Collection of Blocks, for quick getting/putting while iterating
 * over a set or area of blocks.
 * 
 * This stores BlockData objects, which are hashable via their Persisted
 * inheritance, and their LocationData id (which itself has a hash function
 * based on world name and BlockVector's hash function)
 * 
 */
public class UndoList extends BlockList implements com.elmakers.mine.bukkit.api.block.UndoList
{
    protected static Map<Long, BlockData> modified = new HashMap<Long, BlockData>();

    protected List<WeakReference<Entity>> 	entities;
    protected List<Runnable>				runnables;
    protected HashMap<UUID, EntityData> 	modifiedEntities;

    protected final Mage			owner;
    protected final Plugin		   	plugin;

    protected boolean               undone              = false;
    protected int                  	timeToLive          = 0;
    protected boolean               applyPhysics        = false;

    protected boolean				bypass		 	    = false;
    protected final long			createdTime;
    protected long					modifiedTime;
    protected long                  scheduledTime;

    // Doubly-linked list
    protected final UndoableSpell   spell;
    protected UndoQueue             undoQueue;
    protected UndoList              next;
    protected UndoList              previous;

    protected String				name;

    private boolean                 undoEntityEffects = true;

    public UndoList(Mage mage, UndoableSpell spell, String name)
    {
        this(mage, spell);
        this.name = name;
    }

    public UndoList(Mage mage, UndoableSpell spell)
    {
        this.owner = mage;
        this.spell = spell;
        this.plugin = owner.getController().getPlugin();
        createdTime = System.currentTimeMillis();
        modifiedTime = createdTime;
    }

    @Override
    public int size()
    {
        return (
                (blockList == null ? 0 :blockList.size())
            + 	(entities == null ? 0 : entities.size()))
            + 	(runnables == null ? 0 : runnables.size());
    }

    @Override
    public boolean isEmpty()
    {
        return (
            (blockList == null || blockList.isEmpty())
        && 	(entities == null || entities.isEmpty())
        && 	(runnables == null || runnables.isEmpty()));
    }

    public boolean isComplete()
    {
        return undone;
    }

    public void setScheduleUndo(int ttl)
    {
        timeToLive = ttl;
        scheduledTime = System.currentTimeMillis() + timeToLive;
    }

    public int getScheduledUndo()
    {
        return timeToLive;
    }

    @Override
    public boolean add(BlockData blockData)
    {
        if (!super.add(blockData)) return false;
        modifiedTime = System.currentTimeMillis();
        if (bypass) return true;

        register(blockData);
        blockData.setUndoList(this);
        return true;
    }

    public static BlockData register(Block block)
    {
        BlockData blockData = new com.elmakers.mine.bukkit.block.BlockData(block);
        register(blockData);
        return blockData;
    }

    public static void register(BlockData blockData)
    {
        BlockData priorState = modified.get(blockData.getId());
        if (priorState != null)
        {
            priorState.setNextState(blockData);
            blockData.setPriorState(priorState);
        }

        modified.put(blockData.getId(), blockData);
    }

    public void commit()
    {
        unlink();
        if (blockList == null) return;

        for (BlockData block : blockList)
        {
            commit(block);
        }
    }

    public void commit(BlockData block)
    {
        modified.remove(block.getId());
        block.commit();
    }

    @Override
    public boolean remove(Object o)
    {
        if (o instanceof BlockData)
        {<commit_msg>Revert a change I had made to how commit() works<commit_after>package com.elmakers.mine.bukkit.block;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.elmakers.mine.bukkit.spell.UndoableSpell;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;

import com.elmakers.mine.bukkit.api.block.BlockData;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.block.batch.UndoBatch;
import com.elmakers.mine.bukkit.entity.EntityData;

/**
 * Implements a Collection of Blocks, for quick getting/putting while iterating
 * over a set or area of blocks.
 * 
 * This stores BlockData objects, which are hashable via their Persisted
 * inheritance, and their LocationData id (which itself has a hash function
 * based on world name and BlockVector's hash function)
 * 
 */
public class UndoList extends BlockList implements com.elmakers.mine.bukkit.api.block.UndoList
{
    protected static Map<Long, BlockData> modified = new HashMap<Long, BlockData>();

    protected List<WeakReference<Entity>> 	entities;
    protected List<Runnable>				runnables;
    protected HashMap<UUID, EntityData> 	modifiedEntities;

    protected final Mage			owner;
    protected final Plugin		   	plugin;

    protected boolean               undone              = false;
    protected int                  	timeToLive          = 0;
    protected boolean               applyPhysics        = false;

    protected boolean				bypass		 	    = false;
    protected final long			createdTime;
    protected long					modifiedTime;
    protected long                  scheduledTime;

    // Doubly-linked list
    protected final UndoableSpell   spell;
    protected UndoQueue             undoQueue;
    protected UndoList              next;
    protected UndoList              previous;

    protected String				name;

    private boolean                 undoEntityEffects = true;

    public UndoList(Mage mage, UndoableSpell spell, String name)
    {
        this(mage, spell);
        this.name = name;
    }

    public UndoList(Mage mage, UndoableSpell spell)
    {
        this.owner = mage;
        this.spell = spell;
        this.plugin = owner.getController().getPlugin();
        createdTime = System.currentTimeMillis();
        modifiedTime = createdTime;
    }

    @Override
    public int size()
    {
        return (
                (blockList == null ? 0 :blockList.size())
            + 	(entities == null ? 0 : entities.size()))
            + 	(runnables == null ? 0 : runnables.size());
    }

    @Override
    public boolean isEmpty()
    {
        return (
            (blockList == null || blockList.isEmpty())
        && 	(entities == null || entities.isEmpty())
        && 	(runnables == null || runnables.isEmpty()));
    }

    public boolean isComplete()
    {
        return undone;
    }

    public void setScheduleUndo(int ttl)
    {
        timeToLive = ttl;
        scheduledTime = System.currentTimeMillis() + timeToLive;
    }

    public int getScheduledUndo()
    {
        return timeToLive;
    }

    @Override
    public boolean add(BlockData blockData)
    {
        if (!super.add(blockData)) return false;
        modifiedTime = System.currentTimeMillis();
        if (bypass) return true;

        register(blockData);
        blockData.setUndoList(this);
        return true;
    }

    public static BlockData register(Block block)
    {
        BlockData blockData = new com.elmakers.mine.bukkit.block.BlockData(block);
        register(blockData);
        return blockData;
    }

    public static void register(BlockData blockData)
    {
        BlockData priorState = modified.get(blockData.getId());
        if (priorState != null)
        {
            priorState.setNextState(blockData);
            blockData.setPriorState(priorState);
        }

        modified.put(blockData.getId(), blockData);
    }

    public void commit()
    {
        unlink();
        if (blockList == null) return;

        for (BlockData block : blockList)
        {
            commit(block);
        }
    }

    public void commit(BlockData block)
    {
        modified.remove(block.getId());
        BlockData currentState = modified.get(block.getId());
        if (currentState == block)
        {
            modified.remove(block.getId());
        }
        block.commit();
    }

    @Override
    public boolean remove(Object o)
    {
        if (o instanceof BlockData)
        {
{'fraction_non_alphanumeric': 0.09227650949434606, 'fraction_numerical': 0.0008534243652656284, 'mean_word_length': 2.339864624153901, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393550', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2626, 'n_words': 650}