Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * EntityBuilder is a utility class for creating and modifying Bukkit entities with various attributes and properties.
- */
- import lombok.Getter;
- import lombok.Setter;
- import org.bukkit.DyeColor;
- import org.bukkit.Location;
- import org.bukkit.attribute.Attributable;
- import org.bukkit.attribute.Attribute;
- import org.bukkit.entity.*;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.scheduler.BukkitRunnable;
- import java.util.Objects;
- @Getter
- public class EntityBuilder {
- // Instance variables
- private Entity entity; // The entity being built or modified
- private Location location; // The location where the entity is spawned
- private final EntityType entityType; // The type of entity being built
- /**
- * Constructs a new EntityBuilder object with the specified location and entity type.
- *
- * @param location The location where the entity will be spawned
- * @param entityType The type of entity to be spawned
- */
- public EntityBuilder(Location location, EntityType entityType) {
- this.location = location;
- this.entityType = entityType;
- this.entity = location.getWorld().spawnEntity(location, entityType);
- }
- // Methods for setting entity properties
- /**
- * Sets the custom name of the entity.
- *
- * @param name The custom name to be set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setName(String name) {
- entity.setCustomName(name);
- entity.setCustomNameVisible(true);
- return this;
- }
- /**
- * Sets the maximum health and current health of the entity.
- *
- * @param health The health value to be set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setHealth(double health) {
- ((LivingEntity) entity).setMaxHealth(health);
- ((LivingEntity) entity).setHealth(health);
- return this;
- }
- /**
- * Sets the entity to be in its baby form if applicable.
- *
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setIsBaby() {
- if (entity instanceof Ageable) {
- ((Ageable) entity).setBaby();
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets whether the entity can pick up items.
- *
- * @param canPickupItems True if the entity can pick up items, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setCanPickupItems(boolean canPickupItems) {
- ((LivingEntity) entity).setCanPickupItems(canPickupItems);
- return this;
- }
- /**
- * Sets the color of the entity if it is a Sheep.
- *
- * @param color The color to set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setColor(DyeColor color) {
- if(entity instanceof Sheep) {
- ((Sheep) entity).setColor(color);
- }
- return this;
- }
- /**
- * Sets whether the entity is silent.
- *
- * @param silent True if the entity is silent, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setSilent(boolean silent) {
- entity.setSilent(silent);
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets whether the entity has AI.
- *
- * @param allowAI True if AI is enabled, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setAI(boolean allowAI) {
- if(entity instanceof LivingEntity) {
- ((LivingEntity) entity).setAI(allowAI);
- }
- return this;
- }
- /**
- * Sets the velocity of the entity.
- *
- * @param x The x-component of the velocity
- * @param y The y-component of the velocity
- * @param z The z-component of the velocity
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setVelocity(double x, double y, double z) {
- entity.setVelocity(entity.getVelocity().setX(x));
- entity.setVelocity(entity.getVelocity().setY(y));
- entity.setVelocity(entity.getVelocity().setZ(z));
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets whether the entity is affected by gravity.
- *
- * @param gravity True if the entity is affected by gravity, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setGravity(boolean gravity) {
- entity.setGravity(gravity);
- return this;
- }
- /**
- * Sets whether the entity is invulnerable.
- *
- * @param invulnerable True if the entity is invulnerable, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setInvulnerable(boolean invulnerable) {
- entity.setInvulnerable(invulnerable);
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets whether the entity is glowing.
- *
- * @param glowing True if the entity is glowing, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setGlowing(boolean glowing) {
- entity.setGlowing(glowing);
- return this;
- }
- /**
- * Sets the fire ticks of the entity.
- *
- * @param seconds The duration of fire in seconds
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setFireTicks(int seconds) {
- entity.setFireTicks(seconds * 20);
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the freeze ticks of the entity.
- *
- * @param seconds The duration of freeze in seconds
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setFreezeTicks(int seconds) {
- entity.setFreezeTicks(seconds * 20);
- return this;
- }
- /**
- * Sets the life duration of the entity with the given name.
- *
- * @param entityName The name of the entity
- * @param seconds The duration of life in seconds
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setLifeDuration(String entityName, int seconds) {
- new BukkitRunnable() {
- @Override
- public void run() {
- setLifeDuration(entityName);
- }
- }.runTaskLater(plugin, seconds * 20L); //get here the plugin main instance to create a bukkitRunnable task
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the item in the entity's main hand.
- *
- * @param mainHand The item stack for the main hand
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setMainHand(ItemStack mainHand) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setItemInMainHand(mainHand);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the item in the entity's off hand.
- *
- * @param offHand The item stack for the off hand
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setOffHand(ItemStack offHand) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setItemInOffHand(offHand);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the equipment of the entity.
- *
- * @param helmet The helmet item stack
- * @param chestplate The chestplate item stack
- * @param leggings The leggings item stack
- * @param boots The boots item stack
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setEquipment(ItemStack helmet, ItemStack chestplate, ItemStack leggings, ItemStack boots) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmet(helmet);
- ((LivingEntity) entity).getEquipment().setChestplate(chestplate);
- ((LivingEntity) entity).getEquipment().setLeggings(leggings);
- ((LivingEntity) entity).getEquipment().setBoots(boots);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the equipment of the entity.
- *
- * @param equipment The array of ItemStacks representing the equipment
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setEquipment(ItemStack[] equipment) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setArmorContents(equipment);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the equipment drop chance of the entity.
- *
- * @param dropChanceHelmet The drop chance of the helmet
- * @param dropChanceChestplate The drop chance of the chestplate
- * @param dropChanceLeggings The drop chance of the leggings
- * @param dropChanceBoots The drop chance of the boots
- * @param dropChanceMainHand The drop chance of the main hand
- * @param dropChanceOffHand The drop chance of the off hand
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setEquipmentDropChance(int dropChanceHelmet, int dropChanceChestplate, int dropChanceLeggings, int dropChanceBoots, int dropChanceMainHand, int dropChanceOffHand) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmetDropChance(dropChanceHelmet);
- ((LivingEntity) entity).getEquipment().setChestplateDropChance(dropChanceChestplate);
- ((LivingEntity) entity).getEquipment().setLeggingsDropChance(dropChanceLeggings);
- ((LivingEntity) entity).getEquipment().setBootsDropChance(dropChanceBoots);
- ((LivingEntity) entity).getEquipment().setItemInMainHandDropChance(dropChanceMainHand);
- ((LivingEntity) entity).getEquipment().setItemInOffHandDropChance(dropChanceOffHand);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets the equipment drop chance of the entity.
- *
- * @param dropChanceHelmet The drop chance of the helmet
- * @param dropChanceChestplate The drop chance of the chestplate
- * @param dropChanceLeggings The drop chance of the leggings
- * @param dropChanceBoots The drop chance of the boots
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setEquipmentDropChance(int dropChanceHelmet, int dropChanceChestplate, int dropChanceLeggings, int dropChanceBoots) {
- if(entity instanceof LivingEntity) {
- Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmetDropChance(dropChanceHelmet);
- ((LivingEntity) entity).getEquipment().setChestplateDropChance(dropChanceChestplate);
- ((LivingEntity) entity).getEquipment().setLeggingsDropChance(dropChanceLeggings);
- ((LivingEntity) entity).getEquipment().setBootsDropChance(dropChanceBoots);
- }
- return this;
- }
- // Additional methods for setting entity properties...
- /**
- * Sets whether the entity is removed when far away.
- *
- * @param removeWhenFarAway True if the entity should be removed when far away, false otherwise
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setRemoveWhenFarAway(boolean removeWhenFarAway) {
- if(entity instanceof LivingEntity) {
- ((LivingEntity) entity).setRemoveWhenFarAway(removeWhenFarAway);
- }
- return this;
- }
- // Methods for setting entity attributes
- /**
- * Sets the attack damage of the entity.
- *
- * @param damage The attack damage value to be set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setAttackDamage(double damage) {
- if(entity instanceof Attributable) {
- Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_ATTACK_DAMAGE)).setBaseValue(damage);
- }
- return this;
- }
- // Additional methods for setting entity attributes...
- /**
- * Sets the movement speed of the entity.
- *
- * @param speed The movement speed value to be set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setMovementSpeed(double speed) {
- if(entity instanceof Attributable) {
- Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)).setBaseValue(speed);
- }
- return this;
- }
- // Additional methods for setting entity attributes...
- /**
- * Sets the attack speed of the entity.
- *
- * @param attackSpeed The attack speed value to be set
- * @return This EntityBuilder instance for method chaining
- */
- public EntityBuilder setAttackSpeed(double attackSpeed) {
- if(entity instanceof Attributable) {
- Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_ATTACK_SPEED)).setBaseValue(attackSpeed);
- }
- return this;
- }
- // Additional methods for setting entity attributes...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement