Unicopia/src/main/java/com/minelittlepony/unicopia/item/JarItem.java

164 lines
6.1 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
import com.minelittlepony.unicopia.entity.IItemEntity;
import com.minelittlepony.unicopia.entity.ItemImpl;
import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles;
import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
import com.minelittlepony.unicopia.projectile.ProjectileDelegate;
import net.minecraft.block.Block;
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;
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;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
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));
if (!world.isClient) {
MagicProjectileEntity projectile = new MagicProjectileEntity(world, player);
projectile.setItem(stack);
projectile.setThrowDamage(getProjectileDamage(stack));
2021-12-22 10:15:09 +01:00
projectile.setVelocity(player, player.getPitch(), player.getYaw(), 0, 1.5F, 1);
world.spawnEntity(projectile);
}
player.incrementStat(Stats.USED.getOrCreateStat(this));
2021-08-04 15:38:03 +02:00
if (!player.getAbilities().creativeMode) {
stack.decrement(1);
}
return TypedActionResult.success(stack, world.isClient());
}
protected float getProjectileDamage(ItemStack stack) {
return 0.5F;
}
@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
&& 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);
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) {
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);
}
}
if (lightning) {
ParticleUtils.spawnParticle(projectile.world, UParticles.LIGHTNING_BOLT, projectile.getPos(), Vec3d.ZERO);
}
if (rain || thunder) {
projectile.world.syncWorldEvent(WorldEvents.SPLASH_POTION_SPLASHED, projectile.getBlockPos(), thunder ? 0x888888 : 0xF8F8F8);
for (int i = projectile.world.random.nextInt(3) + 1; i >= 0; i--) {
ParticleUtils.spawnParticle(projectile.world, UParticles.CLOUDS_ESCAPING,
projectile.getX(), projectile.getY(), projectile.getZ(),
projectile.world.random.nextFloat() - 0.5,
0,
projectile.world.random.nextFloat() - 0.5
);
}
}
projectile.world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, projectile.getBlockPos(), Block.getRawIdFromState(Blocks.GLASS.getDefaultState()));
}
}