<commit_before>/*******************************************************************************
 * Arekkuusu / Solar 2017
 *
 * This project is licensed under the MIT.
 * The source code is available on github:
 * https://github.com/ArekkuusuJerii/Solar#solar
 ******************************************************************************/
package arekkuusu.solar.common.block.tile;

import arekkuusu.solar.api.state.State;
import arekkuusu.solar.client.util.helper.ProfilerHelper;
import arekkuusu.solar.common.block.ModBlocks;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.BlockSnow;
import net.minecraft.block.material.EnumPushReaction;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Triple;

import java.util.List;
import java.util.Set;

/**
 * Created by <Snack> on 04/02/2018.
 * It's distributed as part of Solar.
 */
public class TileDilaton extends TileBase {

	private boolean powered;

	public void pushExtension(boolean powered) {
		if(!world.isRemote && !(!isActiveLazy() && !powered)) {
			ProfilerHelper.flagSection("[Dilaton] Redstone signal received");
			BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(getPos());
			EnumFacing facing = getFacingLazy();
			int range = powered ? getRedstonePower() + 1 : 16;
			int pointer = 0;
			if(isActiveLazy()) {
				ProfilerHelper.flagSection("[Dilaton] Locate nearest extension");
				boolean found = false;
				for(; pointer < range; pointer++) {
					if(!isPosValid(pos.move(facing)) || pointer >= range) return;
					IBlockState state = world.getBlockState(pos);
					if(state.getBlock() == ModBlocks.DILATON_EXTENSION) {
						world.setBlockToAir(pos);
						found = true;
						break;
					}
				}
				if(!found) return;
				if(!powered) pointer = 0;
			}
			ProfilerHelper.begin("[Dilaton] Gathering pushed blocks");
			if(!powered) facing = facing.getOpposite();
			List<Triple<IBlockState, NBTTagCompound, BlockPos>> pushed = Lists.newArrayList();
			List<BlockPos> removed = Lists.newArrayList();
			Set<BlockPos> deleted = Sets.newHashSet();
			loop : for(; pointer < range; pointer++) {
				if(!isPosValid(pos.move(facing))) return;
				IBlockState next = world.getBlockState(pos);
				if(pos.equals(getPos()) || next.getBlock() == Blocks.OBSIDIAN) break;
				if(next.getBlock() == Blocks.AIR) continue;
				EnumPushReaction reaction = next.getMobilityFlag();
				if(next.getMaterial() == Material.GLASS) reaction = EnumPushReaction.DESTROY;
				else
				if(next.getMaterial() == Material.WATER) reaction = EnumPushReaction.IGNORE;
				switch(reaction) {
					case PUSH_ONLY:
					case NORMAL:
						if(pushed.add(getStateTile(next, pos.toImmutable())) && pushed.size() > 15) break loop;
						else ++range;
						continue loop;
					case DESTROY:
						removed.add(pos.toImmutable());
						continue loop;
					case BLOCK:
						break loop;
					case IGNORE:
				}
			}
			ProfilerHelper.interrupt("[Dilaton] Relocating pushed blocks");
			pos.move(facing.getOpposite());
			for(int i = 0, size = pushed.size(); i < size; i++) {
				if(isPosValid(pos) && isPosReplaceable(pos)) {
					for(int index = pushed.size() - 1; index >= 0; index--) {
						Triple<IBlockState, NBTTagCompound, BlockPos> triplet = pushed.get(index);
						if(setStateTile(triplet.getLeft(), triplet.getMiddle(), pos.toImmutable()))
							removed.add(pos.toImmutable());
						if(deleted.add(triplet.getRight()))
							deleted.removeIf(a -> a.equals(pos));
						pos.move(facing.getOpposite());
					}
					deleted.forEach(delete -> {
						if(world.getTileEntity(delete) != null) world.removeTileEntity(delete);
						world.setBlockToAir(delete);
					});
					break;
				} else if(!pushed.isEmpty()) pushed.remove(pushed.size() - 1);
				pos.move(facing.getOpposite());
			}
			ProfilerHelper.flagSection("[Dilaton] Block drops");
			removed.forEach(p -> {
				IBlockState state = world.getBlockState(p);
				float chance = state.getBlock() instanceof BlockSnow ? -1.0F : 1.0F;
				state.getBlock().dropBlockAsItemWithChance(world, p, state, chance, 0);
				world.setBlockToAir(p);
			});
			ProfilerHelper.end();
			ProfilerHelper.flagSection("[Dilaton] Place extension");<commit_msg>Fix logic loophole when the piston is at maximum capacity<commit_after>/*******************************************************************************
 * Arekkuusu / Solar 2017
 *
 * This project is licensed under the MIT.
 * The source code is available on github:
 * https://github.com/ArekkuusuJerii/Solar#solar
 ******************************************************************************/
package arekkuusu.solar.common.block.tile;

import arekkuusu.solar.api.state.State;
import arekkuusu.solar.client.util.helper.ProfilerHelper;
import arekkuusu.solar.common.block.ModBlocks;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.BlockSnow;
import net.minecraft.block.material.EnumPushReaction;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Triple;

import java.util.List;
import java.util.Set;

/**
 * Created by <Snack> on 04/02/2018.
 * It's distributed as part of Solar.
 */
public class TileDilaton extends TileBase {

	private boolean powered;

	public void pushExtension(boolean powered) {
		if(!world.isRemote && !(!isActiveLazy() && !powered)) {
			ProfilerHelper.flagSection("[Dilaton] Redstone signal received");
			BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(getPos());
			EnumFacing facing = getFacingLazy();
			int range = powered ? getRedstonePower() + 1 : 16;
			int pointer = 0;
			if(isActiveLazy()) {
				ProfilerHelper.flagSection("[Dilaton] Locate nearest extension");
				boolean found = false;
				for(; pointer < range; pointer++) {
					if(!isPosValid(pos.move(facing)) || pointer >= range) return;
					IBlockState state = world.getBlockState(pos);
					if(state.getBlock() == ModBlocks.DILATON_EXTENSION) {
						world.setBlockToAir(pos);
						found = true;
						break;
					}
				}
				if(!found) return;
				if(!powered) pointer = 0;
			}
			ProfilerHelper.begin("[Dilaton] Gathering pushed blocks");
			if(!powered) facing = facing.getOpposite();
			List<Triple<IBlockState, NBTTagCompound, BlockPos>> pushed = Lists.newArrayList();
			List<BlockPos> removed = Lists.newArrayList();
			loop : for(; pointer < range; pointer++) {
				if(pos.move(facing).equals(getPos()) || !isPosValid(pos)) break;
				IBlockState next = world.getBlockState(pos);
				if(next.getBlock() == Blocks.AIR) continue;
				float hardness = next.getBlockHardness(world, pos);
				if(hardness > 2000F || hardness < 0F) break;
				EnumPushReaction reaction;
				if(next.getMaterial() == Material.WATER) reaction = EnumPushReaction.IGNORE;
				else reaction = next.getMobilityFlag();
				switch(reaction) {
					case PUSH_ONLY:
					case NORMAL:
						if(pushed.add(getStateTile(next, pos.toImmutable())) && pushed.size() > 15) break loop;
						else ++range;
						continue loop;
					case DESTROY:
						removed.add(pos.toImmutable());
						continue loop;
					case BLOCK:
						break loop;
					case IGNORE:
				}
			}
			ProfilerHelper.interrupt("[Dilaton] Relocating pushed blocks");
			Set<BlockPos> deleted = Sets.newHashSet();
			if(pushed.size() <= 15) {
				pos.move(facing.getOpposite());
			}
			for(int i = 0, size = pushed.size(); i < size; i++) {
				if(isPosReplaceable(pos)) {
					for(int index = pushed.size() - 1; index >= 0; index--) {
						Triple<IBlockState, NBTTagCompound, BlockPos> triplet = pushed.get(index);
						if(setStateTile(triplet.getLeft(), triplet.getMiddle(), pos.toImmutable()))
							removed.add(pos.toImmutable());
						BlockPos oldPos = triplet.getRight();
						if(deleted.add(oldPos))
							deleted.removeIf(a -> a.equals(pos));
						pos.move(facing.getOpposite());
					}
					deleted.forEach(delete -> {
						if(world.getTileEntity(delete) != null) world.removeTileEntity(delete);
						world.setBlockToAir(delete);
					});
					break;
				} else if(!pushed.isEmpty()) pushed.remove(pushed.size() - 1);
				pos.move(facing.getOpposite());
			}
			ProfilerHelper.flagSection("[Dilaton] Block drops");
			removed.forEach(p -> {
				IBlockState state = world.getBlockState(p);
				float chance = state.getBlock() instanceof BlockSnow ? -1.0F : 1.0F;
				state.getBlock().dropBlockAsItemWithChance(world, p, state, chance, 0);
				world.setBlockToAir(p);
			});
			ProfilerHelper.end();
			ProfilerHelper.flagSection("[Dilaton] Place extension");
{'fraction_non_alphanumeric': 0.16797986430291093, 'fraction_numerical': 0.007113153862989713, 'mean_word_length': 4.131386861313868, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 28, 'https://': 2, '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': '6439583', 'n_tokens_mistral': 3509, 'n_tokens_neox': 2907, 'n_words': 567}