<commit_before>package team.creative.itemphysic.server;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.stats.Stats;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickEmpty;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import team.creative.itemphysic.ItemPhysic;
import team.creative.itemphysic.common.CommonPhysic;

public class ItemPhysicServer {
	
	public static void init(FMLCommonSetupEvent event) {
		MinecraftForge.EVENT_BUS.register(ItemPhysicServer.class);
	}
	
	public static int tempDroppower = 1;
	
	@SubscribeEvent
	public static void onDespawn(ItemExpireEvent event) {
		if (ItemPhysic.CONFIG.general.despawnItem == -1)
			try {
				age.set(event.getEntityItem(), 1);
				event.setCanceled(true);
				event.setExtraLife(0);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
	}
	
	@SubscribeEvent
	public static void onToos(ItemTossEvent event) {
		event.getEntityItem().setMotion(event.getEntityItem().getMotion().scale(tempDroppower));
		tempDroppower = 1;
	}
	
	/*
	 * replace with
	 * if (this.areEyesInFluid(FluidTags.WATER)) {
	 *      this.applyFloatMotion();
	 * } else if (!this.hasNoGravity()) {
	 *      this.setMotion(this.getMotion().add(0.0D, -0.04D, 0.0D));
	 * }
	 */
	public static boolean updatePre(ItemEntity item) {
		ItemStack stack = item.getItem();
		fluid.set(CommonPhysic.getFluid(item));
		if (fluid.get() == null) {
			if (!item.hasNoGravity())
				item.setMotion(item.getMotion().add(0.0D, -0.04D, 0.0D));
		}
		
		double density = fluid.get().getAttributes().getDensity() / 1000D;
		double speed = -1 / density * 0.01;
		if (ItemPhysic.CONFIG.general.swimmingItems.canPass(stack))
			speed = 0.05;
		
		double speedreduction = (speed - item.getMotion().y) / 2;
		double maxSpeedReduction = 0.05;
		if (speedreduction < -maxSpeedReduction)
			speedreduction = -maxSpeedReduction;
		if (speedreduction > maxSpeedReduction)
			speedreduction = maxSpeedReduction;
		item.setMotion(item.getMotion().add(0, speedreduction, 0));
		return true;
	}
	
	/*
	 * replace with
	 * if (this.world.getFluidState(new BlockPos(this)).isTagged(FluidTags.LAVA)) {
	 *    this.setMotion((double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F), (double)0.2F, (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F));
	 *    this.playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
	 * }
	 */
	public static void updateBurn(ItemEntity item) {
		if (item.world.getFluidState(new BlockPos(item)).isTagged(FluidTags.LAVA) && ItemPhysic.CONFIG.general.burningItems.canPass(item.getItem())) {
			try {
				Random rand = (Random) ItemPhysicServer.rand.get(item);
				item.setMotion((rand.nextFloat() - rand.nextFloat()) * 0.2F, 0.2F, (rand.nextFloat() - rand.nextFloat()) * 0.2F);
				item.playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + rand.nextFloat() * 0.4F);
				for (int i = 0; i < 100; i++)
					item.world.addParticle(ParticleTypes.SMOKE, item.getPosX(), item.getPosY(), item.getPosZ(), (rand.nextFloat() * 0.1) - 0.05, 0.2 * rand.nextDouble(), (rand.nextFloat() * 0.1) - 0.05);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		
		if (ItemPhysic.CONFIG.general.enableIgniting && !item.world.isRemote && item.onGround && Math.random() <= 0.1 && ItemPhysic.CONFIG.general.ignitingItems.canPass(item.getItem())) {
			BlockState state = item.world.getBlockState(new BlockPos(item).down());
			if (state.getMaterial().isFlammable() && item.world.getBlockState(new BlockPos(item)).getMaterial().isReplaceable())
				item.world.setBlockState(new BlockPos(item), Blocks.FIRE.getDefaultState());
		}
	}
	
	public static final ThreadLocal<Fluid> fluid = new ThreadLocal<>();
	
	//Remove this.setMotion(this.getMotion().mul((double)f, 0.98D, (double)f));
	//Replace with: if (this.onGround) { this.setMotion(this.getMotion().mul(1.0D, -0.5D, 1.0D)); }
	public static void update(ItemEntity item, float f) {
		if (ItemPhysic.CONFIG.general.swimmingItems.canPass(item.getItem()) && fluid.get() != null)
			item.setMotion(item.getMotion().mul(1 / (fluid.get().getAttributes().getDensity() / 950D * 1.5), 1, 1 / (fluid.get().getAttributes().getDensity() / 950D * 1.5)));
		
		if (fluid.get() == null) {
			item.setMotion(item.getMotion().mul(f, 0.98D, f));
			
			if (item.onGround)
				item.setMotion(item.getMotion().mul(1.0D, -0.5D, 1.0D));
		} else
			item.setMotion(item.getMotion().mul(1 / (fluid.get().getAttributes().getDensity() / 950D), 1, 1 / (fluid.get().getAttributes().getDensity() / 950D)));
		
		if (ItemPhysic.CONFIG.general.despawnItem != -1 && item.lifespan == 6000 && item.lifespan != ItemPhysic.CONFIG.general.despawnItem)
			item.lifespan = ItemPhysic.CONFIG.general.despawnItem;
		
	}
	
	public static void updateFallState(ItemEntity item, double y, boolean onGroundIn, BlockState state, BlockPos pos) {
		if (onGroundIn && item.fallDistance > 0.0F && ItemPhysic.CONFIG.general.fallSounds)
			item.playSound(SoundEvents.BLOCK_WOOD_FALL, Math.min(1, item.fallDistance / 10), (float) Math.random() * 1F + 1);
	}
	
	public static boolean onCollideWithPlayer(ItemEntity item, PlayerEntity par1EntityPlayer) {
		if (ItemPhysic.CONFIG.pickup.customPickup && (!par1EntityPlayer.isDiscrete()) || !ItemPhysic.CONFIG.pickup.pickupWhenSneaking)
			return true;<commit_msg>Fixed burning physics Fixed tick crash<commit_after>package team.creative.itemphysic.server;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.stats.Stats;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickEmpty;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import team.creative.itemphysic.ItemPhysic;
import team.creative.itemphysic.common.CommonPhysic;

public class ItemPhysicServer {
	
	public static void init(FMLCommonSetupEvent event) {
		MinecraftForge.EVENT_BUS.register(ItemPhysicServer.class);
	}
	
	public static int tempDroppower = 1;
	
	@SubscribeEvent
	public static void onDespawn(ItemExpireEvent event) {
		if (ItemPhysic.CONFIG.general.despawnItem == -1)
			try {
				age.set(event.getEntityItem(), 1);
				event.setCanceled(true);
				event.setExtraLife(0);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
	}
	
	@SubscribeEvent
	public static void onToos(ItemTossEvent event) {
		event.getEntityItem().setMotion(event.getEntityItem().getMotion().scale(tempDroppower));
		tempDroppower = 1;
	}
	
	/*
	 * replace with
	 * if (this.areEyesInFluid(FluidTags.WATER)) {
	 *      this.applyFloatMotion();
	 * } else if (!this.hasNoGravity()) {
	 *      this.setMotion(this.getMotion().add(0.0D, -0.04D, 0.0D));
	 * }
	 */
	public static void updatePre(ItemEntity item) {
		ItemStack stack = item.getItem();
		fluid.set(CommonPhysic.getFluid(item));
		if (fluid.get() == null) {
			if (!item.hasNoGravity())
				item.setMotion(item.getMotion().add(0.0D, -0.04D, 0.0D));
			return;
		}
		
		double density = fluid.get().getAttributes().getDensity() / 1000D;
		double speed = -1 / density * 0.01;
		if (ItemPhysic.CONFIG.general.swimmingItems.canPass(stack))
			speed = 0.05;
		
		double speedreduction = (speed - item.getMotion().y) / 2;
		double maxSpeedReduction = 0.05;
		if (speedreduction < -maxSpeedReduction)
			speedreduction = -maxSpeedReduction;
		if (speedreduction > maxSpeedReduction)
			speedreduction = maxSpeedReduction;
		item.setMotion(item.getMotion().add(0, speedreduction, 0));
	}
	
	/*
	 * replace with
	 * if (this.world.getFluidState(new BlockPos(this)).isTagged(FluidTags.LAVA)) {
	 *    this.setMotion((double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F), (double)0.2F, (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F));
	 *    this.playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
	 * }
	 */
	public static void updateBurn(ItemEntity item) {
		try {
			Random rand = (Random) ItemPhysicServer.rand.get(item);
			if (item.world.getFluidState(new BlockPos(item)).isTagged(FluidTags.LAVA) && ItemPhysic.CONFIG.general.burningItems.canPass(item.getItem())) {
				item.playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + rand.nextFloat() * 0.4F);
				item.remove();
				for (int i = 0; i < 100; i++)
					item.world.addParticle(ParticleTypes.SMOKE, item.getPosX(), item.getPosY(), item.getPosZ(), (rand.nextFloat() * 0.1) - 0.05, 0.2 * rand.nextDouble(), (rand.nextFloat() * 0.1) - 0.05);
			}
			
			if (item.isBurning() && ItemPhysic.CONFIG.general.burningItems.canPass(item.getItem())) {
				item.playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + rand.nextFloat() * 0.4F);
				item.remove();
				for (int i = 0; i < 100; i++)
					item.world.addParticle(ParticleTypes.SMOKE, item.getPosX(), item.getPosY(), item.getPosZ(), (rand.nextFloat() * 0.1) - 0.05, 0.2 * rand.nextDouble(), (rand.nextFloat() * 0.1) - 0.05);
			}
			
			if (ItemPhysic.CONFIG.general.enableIgniting && !item.world.isRemote && item.onGround && Math.random() <= 0.1 && ItemPhysic.CONFIG.general.ignitingItems.canPass(item.getItem())) {
				BlockState state = item.world.getBlockState(new BlockPos(item).down());
				if (state.getMaterial().isFlammable() && item.world.getBlockState(new BlockPos(item)).getMaterial().isReplaceable())
					item.world.setBlockState(new BlockPos(item), Blocks.FIRE.getDefaultState());
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public static final ThreadLocal<Fluid> fluid = new ThreadLocal<>();
	
	//Remove this.setMotion(this.getMotion().mul((double)f, 0.98D, (double)f));
	//Replace with: if (this.onGround) { this.setMotion(this.getMotion().mul(1.0D, -0.5D, 1.0D)); }
	public static void update(ItemEntity item, float f) {
		
		//if (ItemPhysic.CONFIG.general.swimmingItems.canPass(item.getItem()) && fluid.get() != null)
		//item.setMotion(item.getMotion().mul(1 / (fluid.get().getAttributes().getDensity() / 950D * 1.5), 1, 1 / (fluid.get().getAttributes().getDensity() / 950D * 1.5)));
		
		if (fluid.get() == null) {
			item.setMotion(item.getMotion().mul(f, 0.98D, f));
			
			if (item.onGround)
				item.setMotion(item.getMotion().mul(1.0D, -0.5D, 1.0D));
		} else
			item.setMotion(item.getMotion().mul(1 / (fluid.get().getAttributes().getDensity() / 900D), 1, 1 / (fluid.get().getAttributes().getDensity() / 900D)));
		
		if (ItemPhysic.CONFIG.general.despawnItem != -1 && item.lifespan == 6000 && item.lifespan != ItemPhysic.CONFIG.general.despawnItem)
			item.lifespan = ItemPhysic.CONFIG.general.despawnItem;
		
	}
	
	public static void updateFallState(ItemEntity item, double y, boolean onGroundIn, BlockState state, BlockPos pos) {
		if (onGroundIn && item.fallDistance > 0.0F && ItemPhysic.CONFIG.general.fallSounds)
			item.playSound(SoundEvents.BLOCK_WOOL_FALL, Math.min(1, item.fallDistance / 10), (float) Math.random() * 1F + 1);
	}
	
	public static boolean onCollideWithPlayer(ItemEntity item, PlayerEntity par1EntityPlayer) {
		if (ItemPhysic.CONFIG.pickup.customPickup && (!par1EntityPlayer.isDiscrete()) || !ItemPhysic.CONFIG.pickup.pickupWhenSneaking)
			return true;
{'fraction_non_alphanumeric': 0.1479552333356796, 'fraction_numerical': 0.019708594354895474, 'mean_word_length': 4.775609756097561, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, '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': '938433', 'n_tokens_mistral': 5379, 'n_tokens_neox': 4841, 'n_words': 781}