All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.nukkit.item.ItemSpawnEgg Maven / Gradle / Ivy
package cn.nukkit.item;
import cn.nukkit.Player;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.block.Block;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.CreatureSpawnEvent;
import cn.nukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.level.vibration.VibrationEvent;
import cn.nukkit.level.vibration.VibrationType;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import javax.annotation.Nullable;
import java.util.Random;
/**
* @author MagicDroidX (Nukkit Project)
*/
public class ItemSpawnEgg extends Item {
public ItemSpawnEgg() {
this(0, 1);
}
public ItemSpawnEgg(Integer meta) {
this(meta, 1);
}
public ItemSpawnEgg(Integer meta, int count) {
this(SPAWN_EGG, meta, count, "Spawn Egg");
updateName();
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
protected ItemSpawnEgg(int id, Integer meta, int count, String name) {
super(id, meta, count, name);
}
@Override
public void setDamage(Integer meta) {
super.setDamage(meta);
updateName();
}
@PowerNukkitOnly
@Since("FUTURE")
protected void updateName() {
String entityName = getEntityName();
if (entityName == null) {
name = "Spawn Egg";
} else {
name = entityName + " Spawn Egg";
}
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
if (player.isAdventure()) {
return false;
}
FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);
if (chunk == null) {
return false;
}
CompoundTag nbt = new CompoundTag()
.putList(new ListTag("Pos")
.add(new DoubleTag("", block.getX() + 0.5))
.add(new DoubleTag("", target.getBoundingBox() == null ? block.getY() : target.getBoundingBox().getMaxY() + 0.0001f))
.add(new DoubleTag("", block.getZ() + 0.5)))
.putList(new ListTag("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag("Rotation")
.add(new FloatTag("", new Random().nextFloat() * 360))
.add(new FloatTag("", 0)));
if (this.hasCustomName()) {
nbt.putString("CustomName", this.getCustomName());
}
int networkId = getEntityNetworkId();
CreatureSpawnEvent ev = new CreatureSpawnEvent(networkId, block, nbt, SpawnReason.SPAWN_EGG);
level.getServer().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
return false;
}
Entity entity = Entity.createEntity(networkId, chunk, nbt);
if (entity != null) {
if (player.isSurvival()) {
player.getInventory().decreaseCount(player.getInventory().getHeldItemIndex());
}
entity.spawnToAll();
level.getVibrationManager().callVibrationEvent(new VibrationEvent(player, entity.clone(), VibrationType.ENTITY_PLACE));
return true;
}
return false;
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public int getEntityNetworkId() {
return this.meta;
}
@PowerNukkitOnly
@Since("1.19.21-r1")
@Nullable
public String getEntityName() {
String saveId = Entity.getSaveId(getEntityNetworkId());
if (saveId == null) {
return null;
}
return switch (saveId) {
case "VillagerV1" -> "Villager";
case "ZombieVillagerV1" -> "Zombie Villager";
case "NPC" -> "NPC";
default -> String.join(" ", saveId.split("(?=\\p{Lu})"));
};
}
}