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

import java.util.Set;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.block.Sign;
import org.bukkit.block.Skull;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import com.elmakers.mine.bukkit.utilities.InventoryUtils;

public class MaterialAndData {
	protected Material material;
	protected byte data;
	protected String[] signLines = null;
	protected String commandLine = null;
	protected String customName = null;
	protected ItemStack[] inventoryContents = null;
	protected boolean isValid = true;

	public MaterialAndData() {
		material = Material.AIR;
		data = 0;
	}
	
	public MaterialAndData(Block block) {
		updateFrom(block);
	}
	
	public MaterialAndData(MaterialAndData other) {
		updateFrom(other);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public int hashCode() {
		// Note that this does not incorporate any metadata!
		return (material.getId() << 8) | data;
	}
	
	@Override
	public boolean equals(Object obj) {
	    if (!(obj instanceof MaterialAndData)) {
	        return false;
	    }
	    
	    MaterialAndData other = (MaterialAndData)obj;
        return other.data == data && other.material == material;
	}
	
	public void updateFrom(MaterialAndData other) {
		material = other.material;
		data = other.data;
		commandLine = other.commandLine;
		inventoryContents = other.inventoryContents;
		signLines = other.signLines;
		customName = other.customName;
		isValid = other.isValid;
	}
	
	public MaterialAndData(final Material material) {
		this.material = material;
		this.data = 0;
	}
	
	public MaterialAndData(final Material material, final  byte data) {
		this.material = material;
		this.data = data;
	}
	
	public void setMaterial(Material material, byte data) {
		this.material = material;
		this.data = data;
		signLines = null;
		commandLine = null;
		inventoryContents = null;
		customName = null;
		isValid = true;
	}
	
	public void setMaterial(Material material) {
		setMaterial(material, (byte)0);
	}
	
	public void updateFrom(Block block) {
		updateFrom(block, null);
	}
	
	@SuppressWarnings("deprecation")
	public void updateFrom(Block block, Set<Material> restrictedMaterials) {
		if (!block.getChunk().isLoaded()) {
			block.getChunk().load(true);
			return;
		}

		Material blockMaterial = block.getType();
		if (restrictedMaterials != null && restrictedMaterials.contains(blockMaterial)) {
			isValid = false;
			return;
		}
		// Look for special block states
		signLines = null;
		commandLine = null;
		inventoryContents = null;
		customName = null;
		
		BlockState blockState = block.getState();
		if (blockState instanceof Sign) {
			Sign sign = (Sign)blockState;
			signLines = sign.getLines();
		} else if (blockState instanceof CommandBlock){
			CommandBlock command = (CommandBlock)blockState;
			commandLine = command.getCommand();
			customName = command.getName();
		} else if (blockState instanceof InventoryHolder) {
			InventoryHolder holder = (InventoryHolder)blockState;
			Inventory holderInventory = holder.getInventory();
			inventoryContents = holderInventory.getContents();
		} else if (blockState instanceof Skull) {
			Skull skull = (Skull)blockState;
			customName = skull.getOwner();
		}
		
		material = blockMaterial;
		data = block.getData();
		isValid = true;
	}
	
	@SuppressWarnings("deprecation")
	public void modify(Block block) {
		if (!isValid) return;
		
		try {
			// Clear chests so they don't dump their contents.
			BlockState oldState = block.getState();
			if (oldState instanceof InventoryHolder) {
				InventoryHolder holder = (InventoryHolder)oldState;
				Inventory inventory = holder.getInventory();
				inventory.clear();
			}
			
			block.setType(material);
			block.setData(data);
			BlockState blockState = block.getState();
			if (blockState instanceof Sign && signLines != null) {
				Sign sign = (Sign)blockState;
				for (int i = 0; i < signLines.length; i++) {
					sign.setLine(i, signLines[i]);
				}<commit_msg>Catch a weird NPE getting command block data.<commit_after>package com.elmakers.mine.bukkit.blocks;

import java.util.Set;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.block.Sign;
import org.bukkit.block.Skull;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import com.elmakers.mine.bukkit.utilities.InventoryUtils;

public class MaterialAndData {
	protected Material material;
	protected byte data;
	protected String[] signLines = null;
	protected String commandLine = null;
	protected String customName = null;
	protected ItemStack[] inventoryContents = null;
	protected boolean isValid = true;

	public MaterialAndData() {
		material = Material.AIR;
		data = 0;
	}
	
	public MaterialAndData(Block block) {
		updateFrom(block);
	}
	
	public MaterialAndData(MaterialAndData other) {
		updateFrom(other);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public int hashCode() {
		// Note that this does not incorporate any metadata!
		return (material.getId() << 8) | data;
	}
	
	@Override
	public boolean equals(Object obj) {
	    if (!(obj instanceof MaterialAndData)) {
	        return false;
	    }
	    
	    MaterialAndData other = (MaterialAndData)obj;
        return other.data == data && other.material == material;
	}
	
	public void updateFrom(MaterialAndData other) {
		material = other.material;
		data = other.data;
		commandLine = other.commandLine;
		inventoryContents = other.inventoryContents;
		signLines = other.signLines;
		customName = other.customName;
		isValid = other.isValid;
	}
	
	public MaterialAndData(final Material material) {
		this.material = material;
		this.data = 0;
	}
	
	public MaterialAndData(final Material material, final  byte data) {
		this.material = material;
		this.data = data;
	}
	
	public void setMaterial(Material material, byte data) {
		this.material = material;
		this.data = data;
		signLines = null;
		commandLine = null;
		inventoryContents = null;
		customName = null;
		isValid = true;
	}
	
	public void setMaterial(Material material) {
		setMaterial(material, (byte)0);
	}
	
	public void updateFrom(Block block) {
		updateFrom(block, null);
	}
	
	@SuppressWarnings("deprecation")
	public void updateFrom(Block block, Set<Material> restrictedMaterials) {
		if (!block.getChunk().isLoaded()) {
			block.getChunk().load(true);
			return;
		}

		Material blockMaterial = block.getType();
		if (restrictedMaterials != null && restrictedMaterials.contains(blockMaterial)) {
			isValid = false;
			return;
		}
		// Look for special block states
		signLines = null;
		commandLine = null;
		inventoryContents = null;
		customName = null;
		
		try {
			BlockState blockState = block.getState();
			if (blockState instanceof Sign) {
				Sign sign = (Sign)blockState;
				signLines = sign.getLines();
			} else if (blockState instanceof CommandBlock){
				// This seems to occasionally throw exceptions...
				CommandBlock command = (CommandBlock)blockState;
				commandLine = command.getCommand();
				customName = command.getName();
			} else if (blockState instanceof InventoryHolder) {
				InventoryHolder holder = (InventoryHolder)blockState;
				Inventory holderInventory = holder.getInventory();
				inventoryContents = holderInventory.getContents();
			} else if (blockState instanceof Skull) {
				Skull skull = (Skull)blockState;
				customName = skull.getOwner();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		material = blockMaterial;
		data = block.getData();
		isValid = true;
	}
	
	@SuppressWarnings("deprecation")
	public void modify(Block block) {
		if (!isValid) return;
		
		try {
			// Clear chests so they don't dump their contents.
			BlockState oldState = block.getState();
			if (oldState instanceof InventoryHolder) {
				InventoryHolder holder = (InventoryHolder)oldState;
				Inventory inventory = holder.getInventory();
				inventory.clear();
			}
			
			block.setType(material);
			block.setData(data);
			BlockState blockState = block.getState();
			if (blockState instanceof Sign && signLines != null) {
				Sign sign = (Sign)blockState;
				for (int i = 0; i < signLines.length; i++) {
					sign.setLine(i, signLines[i]);
				}
{'fraction_non_alphanumeric': 0.10640813431071175, 'fraction_numerical': 0.0011823126034523528, 'mean_word_length': 4.017200474495848, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '678809', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2512, 'n_words': 644}