2021-02-14 16:53:44 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
2021-12-29 20:16:12 +01:00
|
|
|
import com.minelittlepony.unicopia.AwaitTickQueue;
|
2022-01-04 23:43:07 +01:00
|
|
|
import com.minelittlepony.unicopia.USounds;
|
2021-02-14 16:53:44 +01:00
|
|
|
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-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;
|
2022-03-27 16:02:14 +02:00
|
|
|
import net.minecraft.entity.projectile.ProjectileEntity;
|
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.item.ItemStack;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2022-03-27 16:02:14 +02:00
|
|
|
import net.minecraft.sound.SoundEvent;
|
2021-02-14 16:53:44 +01:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-12-29 20:16:12 +01:00
|
|
|
import net.minecraft.util.math.ChunkSectionPos;
|
2021-12-28 01:20:08 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2021-12-29 20:16:12 +01:00
|
|
|
import net.minecraft.world.Heightmap;
|
2021-08-25 14:22:11 +02:00
|
|
|
import net.minecraft.world.WorldEvents;
|
2021-02-14 16:53:44 +01:00
|
|
|
|
2022-03-27 16:02:14 +02:00
|
|
|
public class JarItem extends ProjectileItem implements ItemImpl.GroundTickCallback {
|
2021-02-14 16:53:44 +01:00
|
|
|
|
|
|
|
private final boolean rain;
|
|
|
|
private final boolean thunder;
|
|
|
|
private final boolean lightning;
|
|
|
|
|
|
|
|
public JarItem(Settings settings, boolean rain, boolean thunder, boolean lightning) {
|
2022-03-27 16:02:14 +02:00
|
|
|
super(settings, 0.5F);
|
2021-02-14 16:53:44 +01:00
|
|
|
this.rain = rain;
|
|
|
|
this.thunder = thunder;
|
|
|
|
this.lightning = lightning;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2022-03-27 16:02:14 +02:00
|
|
|
protected SoundEvent getThrowSound(ItemStack stack) {
|
|
|
|
return USounds.ENTITY_JAR_THROW;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImpact(ProjectileEntity projectile, BlockPos pos, BlockState state) {
|
2021-02-14 16:53:44 +01:00
|
|
|
onImpact(projectile);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-03-27 16:02:14 +02:00
|
|
|
public void onImpact(ProjectileEntity projectile, Entity entity) {
|
2021-02-14 16:53:44 +01:00
|
|
|
onImpact(projectile);
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:02:14 +02:00
|
|
|
protected void onImpact(ProjectileEntity projectile) {
|
|
|
|
if (!projectile.world.isClient()) {
|
2021-02-14 16:53:44 +01:00
|
|
|
ServerWorld world = (ServerWorld)projectile.world;
|
|
|
|
|
|
|
|
if (rain || thunder) {
|
2021-12-29 20:16:12 +01:00
|
|
|
// clear weather time = number of ticks for which the weather is clear
|
|
|
|
// rain time = ticks until rain gets toggled (reset the tick after toggling)
|
|
|
|
// thunder time = ticks until thundering gets toggled (reset the tick after toggling)
|
|
|
|
|
|
|
|
// clear weather time
|
|
|
|
// Number of ticks weather must stay clear.
|
|
|
|
// Raining and thundering, and raining/thundering times are kept to false and 0
|
|
|
|
// when clear weather time is <= 0
|
|
|
|
// - wait for thunder time to reach zero then toggle thundering
|
|
|
|
// - wait for rain time to reach zero then toggle raining
|
|
|
|
// when thunder time is <= 0
|
|
|
|
// - randomly pick a new value for thunder time
|
|
|
|
// when rain time is <= 0
|
|
|
|
// - randomly pick a new value for rain time
|
|
|
|
|
|
|
|
world.setWeather(0, 0, rain, thunder);
|
|
|
|
|
|
|
|
if (thunder) {
|
|
|
|
for (int i = world.random.nextInt(7); i > 0; i--) {
|
|
|
|
AwaitTickQueue.scheduleTask(world, w -> {
|
|
|
|
LightningEntity bolt = EntityType.LIGHTNING_BOLT.create(world);
|
|
|
|
bolt.setCosmetic(true);
|
|
|
|
bolt.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(world.getTopPosition(Heightmap.Type.MOTION_BLOCKING, world.getRandomPosInChunk(
|
|
|
|
ChunkSectionPos.getBlockCoord(ChunkSectionPos.getSectionCoord(projectile.getX())),
|
|
|
|
0,
|
|
|
|
ChunkSectionPos.getBlockCoord(ChunkSectionPos.getSectionCoord(projectile.getZ())),
|
|
|
|
15
|
|
|
|
)).up(32)));
|
|
|
|
world.spawnEntity(bolt);
|
|
|
|
}, 15 + world.random.nextInt(12));
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|