2021-02-14 16:53:44 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.entity.IItemEntity;
|
|
|
|
import com.minelittlepony.unicopia.entity.ItemImpl;
|
2021-12-28 01:20:08 +01:00
|
|
|
import com.minelittlepony.unicopia.particle.ParticleUtils;
|
2021-02-19 15:56:01 +01:00
|
|
|
import com.minelittlepony.unicopia.particle.UParticles;
|
2021-02-14 16:53:44 +01:00
|
|
|
import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
|
|
|
|
import com.minelittlepony.unicopia.projectile.ProjectileDelegate;
|
|
|
|
|
2021-08-25 14:22:11 +02:00
|
|
|
import net.minecraft.block.Block;
|
2021-02-14 16:53:44 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.ItemEntity;
|
|
|
|
import net.minecraft.entity.LightningEntity;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.entity.Entity.RemovalReason;
|
2021-02-14 16:53:44 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.sound.SoundCategory;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
|
|
|
import net.minecraft.stat.Stats;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-12-28 01:20:08 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2021-02-14 16:53:44 +01:00
|
|
|
import net.minecraft.world.World;
|
2021-08-25 14:22:11 +02:00
|
|
|
import net.minecraft.world.WorldEvents;
|
2021-02-14 16:53:44 +01:00
|
|
|
import net.minecraft.world.level.ServerWorldProperties;
|
|
|
|
|
|
|
|
public class JarItem extends Item implements ProjectileDelegate, ItemImpl.TickableItem {
|
|
|
|
|
|
|
|
private final boolean rain;
|
|
|
|
private final boolean thunder;
|
|
|
|
private final boolean lightning;
|
|
|
|
|
|
|
|
public JarItem(Settings settings, boolean rain, boolean thunder, boolean lightning) {
|
|
|
|
super(settings);
|
|
|
|
this.rain = rain;
|
|
|
|
this.thunder = thunder;
|
|
|
|
this.lightning = lightning;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
|
|
|
|
|
|
|
world.playSound(null, player.getX(), player.getY(), player.getZ(),
|
|
|
|
SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL,
|
|
|
|
0.5F,
|
2021-08-04 15:38:03 +02:00
|
|
|
0.4F / (world.random.nextFloat() * 0.4F + 0.8F));
|
2021-02-14 16:53:44 +01:00
|
|
|
|
|
|
|
if (!world.isClient) {
|
|
|
|
MagicProjectileEntity projectile = new MagicProjectileEntity(world, player);
|
|
|
|
projectile.setItem(stack);
|
2021-02-14 22:13:36 +01:00
|
|
|
projectile.setThrowDamage(getProjectileDamage(stack));
|
2021-12-22 10:15:09 +01:00
|
|
|
projectile.setVelocity(player, player.getPitch(), player.getYaw(), 0, 1.5F, 1);
|
2021-02-14 16:53:44 +01:00
|
|
|
|
|
|
|
world.spawnEntity(projectile);
|
|
|
|
}
|
|
|
|
|
|
|
|
player.incrementStat(Stats.USED.getOrCreateStat(this));
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
if (!player.getAbilities().creativeMode) {
|
2021-02-14 16:53:44 +01:00
|
|
|
stack.decrement(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TypedActionResult.success(stack, world.isClient());
|
|
|
|
}
|
|
|
|
|
2021-02-14 22:13:36 +01:00
|
|
|
protected float getProjectileDamage(ItemStack stack) {
|
|
|
|
return 0.5F;
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:53:44 +01:00
|
|
|
@Override
|
|
|
|
public ActionResult onGroundTick(IItemEntity item) {
|
|
|
|
ItemEntity entity = item.get().getMaster();
|
|
|
|
|
|
|
|
entity.setInvulnerable(true);
|
|
|
|
|
|
|
|
if (!lightning
|
|
|
|
&& !entity.world.isClient
|
2021-08-04 15:38:03 +02:00
|
|
|
&& !entity.isRemoved()
|
|
|
|
&& entity.getItemAge() > 100
|
2021-02-14 16:53:44 +01:00
|
|
|
&& entity.world.isThundering()
|
|
|
|
&& entity.world.isSkyVisible(entity.getBlockPos())
|
|
|
|
&& entity.world.random.nextInt(130) == 0) {
|
|
|
|
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(entity.world);
|
|
|
|
lightning.refreshPositionAfterTeleport(entity.getX(), entity.getY(), entity.getZ());
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
entity.remove(RemovalReason.DISCARDED);
|
2021-02-14 16:53:44 +01:00
|
|
|
entity.world.spawnEntity(lightning);
|
|
|
|
|
|
|
|
ItemEntity neu = EntityType.ITEM.create(entity.world);
|
|
|
|
neu.copyPositionAndRotation(entity);
|
|
|
|
neu.setStack(new ItemStack(this == UItems.RAIN_CLOUD_JAR ? UItems.STORM_CLOUD_JAR : UItems.LIGHTNING_JAR));
|
|
|
|
neu.setInvulnerable(true);
|
|
|
|
|
|
|
|
entity.world.spawnEntity(neu);
|
|
|
|
|
|
|
|
ItemEntity copy = EntityType.ITEM.create(entity.world);
|
|
|
|
copy.copyPositionAndRotation(entity);
|
|
|
|
copy.setInvulnerable(true);
|
|
|
|
copy.setStack(entity.getStack());
|
|
|
|
copy.getStack().decrement(1);
|
|
|
|
|
|
|
|
entity.world.spawnEntity(copy);
|
|
|
|
}
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImpact(MagicProjectileEntity projectile, BlockPos pos, BlockState state) {
|
|
|
|
onImpact(projectile);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImpact(MagicProjectileEntity projectile, Entity entity) {
|
|
|
|
onImpact(projectile);
|
|
|
|
}
|
|
|
|
|
2021-02-14 21:15:30 +01:00
|
|
|
protected void onImpact(MagicProjectileEntity projectile) {
|
2021-02-14 16:53:44 +01:00
|
|
|
if (!projectile.isClient()) {
|
|
|
|
ServerWorld world = (ServerWorld)projectile.world;
|
|
|
|
|
|
|
|
if (rain || thunder) {
|
|
|
|
ServerWorldProperties props = ((ServerWorldProperties)world.getLevelProperties());
|
|
|
|
|
|
|
|
int time = Math.max(Math.max(props.getRainTime(), props.getThunderTime()), 40);
|
|
|
|
world.setWeather(0, time, rain, thunder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lightning) {
|
|
|
|
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(world);
|
|
|
|
lightning.refreshPositionAfterTeleport(projectile.getX(), projectile.getY(), projectile.getZ());
|
|
|
|
|
|
|
|
world.spawnEntity(lightning);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 01:20:08 +01:00
|
|
|
if (lightning) {
|
|
|
|
ParticleUtils.spawnParticle(projectile.world, UParticles.LIGHTNING_BOLT, projectile.getPos(), Vec3d.ZERO);
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:53:44 +01:00
|
|
|
if (rain || thunder) {
|
2021-08-25 14:22:11 +02:00
|
|
|
projectile.world.syncWorldEvent(WorldEvents.SPLASH_POTION_SPLASHED, projectile.getBlockPos(), thunder ? 0x888888 : 0xF8F8F8);
|
2021-02-19 15:56:01 +01:00
|
|
|
|
|
|
|
for (int i = projectile.world.random.nextInt(3) + 1; i >= 0; i--) {
|
2021-12-29 20:15:46 +01:00
|
|
|
ParticleUtils.spawnParticle(projectile.world, UParticles.CLOUDS_ESCAPING,
|
2021-02-19 15:56:01 +01:00
|
|
|
projectile.getX(), projectile.getY(), projectile.getZ(),
|
|
|
|
projectile.world.random.nextFloat() - 0.5,
|
|
|
|
0,
|
|
|
|
projectile.world.random.nextFloat() - 0.5
|
|
|
|
);
|
|
|
|
}
|
2021-02-14 16:53:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:22:11 +02:00
|
|
|
projectile.world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, projectile.getBlockPos(), Block.getRawIdFromState(Blocks.GLASS.getDefaultState()));
|
2021-02-14 16:53:44 +01:00
|
|
|
}
|
|
|
|
}
|