This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelicCraft/src/main/java/com/thebrokenrail/reliccraft/entity/RelicEntity.java

83 lines
2.9 KiB
Java

package com.thebrokenrail.reliccraft.entity;
import com.thebrokenrail.reliccraft.RelicCraft;
import com.thebrokenrail.reliccraft.data.Action;
import com.thebrokenrail.reliccraft.data.Actions;
import com.thebrokenrail.reliccraft.item.RelicItem;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.thrown.ThrownItemEntity;
import net.minecraft.item.Item;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import java.util.List;
import java.util.Objects;
@SuppressWarnings("unused")
public class RelicEntity extends ThrownItemEntity {
public RelicEntity(EntityType<RelicEntity> entityType, World world) {
super(entityType, world);
}
public RelicEntity(World world, LivingEntity owner) {
super(RelicCraft.RELIC_ENTITY, owner, world);
}
public RelicEntity(World world, double x, double y, double z) {
super(RelicCraft.RELIC_ENTITY, x, y, z, world);
}
public RelicEntity(World world) {
super(RelicCraft.RELIC_ENTITY, world);
}
@Override
protected void onCollision(HitResult hitResult) {
if (!getEntityWorld().isClient()) {
String[] actions = RelicItem.getData(getItem()).use.actions;
String id = actions[random.nextInt(actions.length)];
Action action = Actions.get(id);
if (action != null) {
if (hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHitResult = (BlockHitResult) hitResult;
action.execute(getEntityWorld(), getOwner(), blockHitResult.getBlockPos());
} else if (hitResult.getType() == HitResult.Type.ENTITY) {
Entity entity = ((EntityHitResult) hitResult).getEntity();
action.execute(world, getOwner(), entity);
}
}
remove();
}
}
@Override
public void tick() {
super.tick();
if (!getEntityWorld().isClient()) {
List<ServerPlayerEntity> viewers = Objects.requireNonNull(getServer()).getPlayerManager().getPlayerList();
for (ServerPlayerEntity viewer : viewers) {
((ServerWorld) getEntityWorld()).spawnParticles(viewer, ParticleTypes.WITCH, true, getX(), getY(), getZ(), 8, 0.1d, 0.1d, 0.1d, 0d);
}
}
}
@Override
protected Item getDefaultItem() {
return RelicCraft.STAFF_ITEM;
}
@Override
public Packet<?> createSpawnPacket() {
return new EntitySpawnS2CPacket(this);
}
}