Advertisement
Guest User

Untitled

a guest
Apr 16th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.93 KB | Gaming | 0 0
  1. /**
  2.  * EntityBuilder is a utility class for creating and modifying Bukkit entities with various attributes and properties.
  3.  */
  4.  
  5. import lombok.Getter;
  6. import lombok.Setter;
  7. import org.bukkit.DyeColor;
  8. import org.bukkit.Location;
  9. import org.bukkit.attribute.Attributable;
  10. import org.bukkit.attribute.Attribute;
  11. import org.bukkit.entity.*;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.scheduler.BukkitRunnable;
  14.  
  15. import java.util.Objects;
  16.  
  17. @Getter
  18. public class EntityBuilder {
  19.  
  20.     // Instance variables
  21.    
  22.     private Entity entity; // The entity being built or modified
  23.     private Location location; // The location where the entity is spawned
  24.     private final EntityType entityType; // The type of entity being built
  25.    
  26.     /**
  27.      * Constructs a new EntityBuilder object with the specified location and entity type.
  28.      *
  29.      * @param location    The location where the entity will be spawned
  30.      * @param entityType  The type of entity to be spawned
  31.      */
  32.     public EntityBuilder(Location location, EntityType entityType) {
  33.         this.location = location;
  34.         this.entityType = entityType;
  35.         this.entity = location.getWorld().spawnEntity(location, entityType);
  36.     }
  37.  
  38.     // Methods for setting entity properties
  39.    
  40.     /**
  41.      * Sets the custom name of the entity.
  42.      *
  43.      * @param name The custom name to be set
  44.      * @return This EntityBuilder instance for method chaining
  45.      */
  46.     public EntityBuilder setName(String name) {
  47.         entity.setCustomName(name);
  48.         entity.setCustomNameVisible(true);
  49.         return this;
  50.     }
  51.  
  52.     /**
  53.      * Sets the maximum health and current health of the entity.
  54.      *
  55.      * @param health The health value to be set
  56.      * @return This EntityBuilder instance for method chaining
  57.      */
  58.     public EntityBuilder setHealth(double health) {
  59.         ((LivingEntity) entity).setMaxHealth(health);
  60.         ((LivingEntity) entity).setHealth(health);
  61.         return this;
  62.     }
  63.  
  64.     /**
  65.      * Sets the entity to be in its baby form if applicable.
  66.      *
  67.      * @return This EntityBuilder instance for method chaining
  68.      */
  69.     public EntityBuilder setIsBaby() {
  70.         if (entity instanceof Ageable) {
  71.             ((Ageable) entity).setBaby();
  72.         }
  73.         return this;
  74.     }
  75.  
  76.     // Additional methods for setting entity properties...
  77.    
  78.     /**
  79.      * Sets whether the entity can pick up items.
  80.      *
  81.      * @param canPickupItems True if the entity can pick up items, false otherwise
  82.      * @return This EntityBuilder instance for method chaining
  83.      */
  84.     public EntityBuilder setCanPickupItems(boolean canPickupItems) {
  85.         ((LivingEntity) entity).setCanPickupItems(canPickupItems);
  86.         return this;
  87.     }
  88.  
  89.     /**
  90.      * Sets the color of the entity if it is a Sheep.
  91.      *
  92.      * @param color The color to set
  93.      * @return This EntityBuilder instance for method chaining
  94.      */
  95.     public EntityBuilder setColor(DyeColor color) {
  96.         if(entity instanceof Sheep) {
  97.             ((Sheep) entity).setColor(color);
  98.         }
  99.         return this;
  100.     }
  101.  
  102.     /**
  103.      * Sets whether the entity is silent.
  104.      *
  105.      * @param silent True if the entity is silent, false otherwise
  106.      * @return This EntityBuilder instance for method chaining
  107.      */
  108.     public EntityBuilder setSilent(boolean silent) {
  109.         entity.setSilent(silent);
  110.         return this;
  111.     }
  112.  
  113.     // Additional methods for setting entity properties...
  114.  
  115.     /**
  116.      * Sets whether the entity has AI.
  117.      *
  118.      * @param allowAI True if AI is enabled, false otherwise
  119.      * @return This EntityBuilder instance for method chaining
  120.      */
  121.     public EntityBuilder setAI(boolean allowAI) {
  122.         if(entity instanceof LivingEntity) {
  123.             ((LivingEntity) entity).setAI(allowAI);
  124.         }
  125.         return this;
  126.     }
  127.  
  128.     /**
  129.      * Sets the velocity of the entity.
  130.      *
  131.      * @param x The x-component of the velocity
  132.      * @param y The y-component of the velocity
  133.      * @param z The z-component of the velocity
  134.      * @return This EntityBuilder instance for method chaining
  135.      */
  136.     public EntityBuilder setVelocity(double x, double y, double z) {
  137.         entity.setVelocity(entity.getVelocity().setX(x));
  138.         entity.setVelocity(entity.getVelocity().setY(y));
  139.         entity.setVelocity(entity.getVelocity().setZ(z));
  140.         return this;
  141.     }
  142.  
  143.     // Additional methods for setting entity properties...
  144.  
  145.     /**
  146.      * Sets whether the entity is affected by gravity.
  147.      *
  148.      * @param gravity True if the entity is affected by gravity, false otherwise
  149.      * @return This EntityBuilder instance for method chaining
  150.      */
  151.     public EntityBuilder setGravity(boolean gravity) {
  152.         entity.setGravity(gravity);
  153.         return this;
  154.     }
  155.  
  156.     /**
  157.      * Sets whether the entity is invulnerable.
  158.      *
  159.      * @param invulnerable True if the entity is invulnerable, false otherwise
  160.      * @return This EntityBuilder instance for method chaining
  161.      */
  162.     public EntityBuilder setInvulnerable(boolean invulnerable) {
  163.         entity.setInvulnerable(invulnerable);
  164.         return this;
  165.     }
  166.  
  167.     // Additional methods for setting entity properties...
  168.  
  169.     /**
  170.      * Sets whether the entity is glowing.
  171.      *
  172.      * @param glowing True if the entity is glowing, false otherwise
  173.      * @return This EntityBuilder instance for method chaining
  174.      */
  175.     public EntityBuilder setGlowing(boolean glowing) {
  176.         entity.setGlowing(glowing);
  177.         return this;
  178.     }
  179.  
  180.     /**
  181.      * Sets the fire ticks of the entity.
  182.      *
  183.      * @param seconds The duration of fire in seconds
  184.      * @return This EntityBuilder instance for method chaining
  185.      */
  186.     public EntityBuilder setFireTicks(int seconds) {
  187.         entity.setFireTicks(seconds * 20);
  188.         return this;
  189.     }
  190.  
  191.     // Additional methods for setting entity properties...
  192.  
  193.     /**
  194.      * Sets the freeze ticks of the entity.
  195.      *
  196.      * @param seconds The duration of freeze in seconds
  197.      * @return This EntityBuilder instance for method chaining
  198.      */
  199.     public EntityBuilder setFreezeTicks(int seconds) {
  200.         entity.setFreezeTicks(seconds * 20);
  201.         return this;
  202.     }
  203.  
  204.     /**
  205.      * Sets the life duration of the entity with the given name.
  206.      *
  207.      * @param entityName The name of the entity
  208.      * @param seconds    The duration of life in seconds
  209.      * @return This EntityBuilder instance for method chaining
  210.      */
  211.     public EntityBuilder setLifeDuration(String entityName, int seconds) {
  212.         new BukkitRunnable() {
  213.             @Override
  214.             public void run() {
  215.                 setLifeDuration(entityName);
  216.             }
  217.         }.runTaskLater(plugin, seconds * 20L); //get here the plugin main instance to create a bukkitRunnable task
  218.         return this;
  219.     }
  220.  
  221.     // Additional methods for setting entity properties...
  222.  
  223.     /**
  224.      * Sets the item in the entity's main hand.
  225.      *
  226.      * @param mainHand The item stack for the main hand
  227.      * @return This EntityBuilder instance for method chaining
  228.      */
  229.     public EntityBuilder setMainHand(ItemStack mainHand) {
  230.         if(entity instanceof LivingEntity) {
  231.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setItemInMainHand(mainHand);
  232.         }
  233.         return this;
  234.     }
  235.  
  236.     // Additional methods for setting entity properties...
  237.  
  238.     /**
  239.      * Sets the item in the entity's off hand.
  240.      *
  241.      * @param offHand The item stack for the off hand
  242.      * @return This EntityBuilder instance for method chaining
  243.      */
  244.     public EntityBuilder setOffHand(ItemStack offHand) {
  245.         if(entity instanceof LivingEntity) {
  246.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setItemInOffHand(offHand);
  247.         }
  248.         return this;
  249.     }
  250.  
  251.     // Additional methods for setting entity properties...
  252.  
  253.     /**
  254.      * Sets the equipment of the entity.
  255.      *
  256.      * @param helmet     The helmet item stack
  257.      * @param chestplate The chestplate item stack
  258.      * @param leggings   The leggings item stack
  259.      * @param boots      The boots item stack
  260.      * @return This EntityBuilder instance for method chaining
  261.      */
  262.     public EntityBuilder setEquipment(ItemStack helmet, ItemStack chestplate, ItemStack leggings, ItemStack boots) {
  263.         if(entity instanceof LivingEntity) {
  264.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmet(helmet);
  265.             ((LivingEntity) entity).getEquipment().setChestplate(chestplate);
  266.             ((LivingEntity) entity).getEquipment().setLeggings(leggings);
  267.             ((LivingEntity) entity).getEquipment().setBoots(boots);
  268.         }
  269.         return this;
  270.     }
  271.  
  272.     // Additional methods for setting entity properties...
  273.  
  274.     /**
  275.      * Sets the equipment of the entity.
  276.      *
  277.      * @param equipment The array of ItemStacks representing the equipment
  278.      * @return This EntityBuilder instance for method chaining
  279.      */
  280.     public EntityBuilder setEquipment(ItemStack[] equipment) {
  281.         if(entity instanceof LivingEntity) {
  282.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setArmorContents(equipment);
  283.         }
  284.         return this;
  285.     }
  286.  
  287.     // Additional methods for setting entity properties...
  288.  
  289.     /**
  290.      * Sets the equipment drop chance of the entity.
  291.      *
  292.      * @param dropChanceHelmet     The drop chance of the helmet
  293.      * @param dropChanceChestplate The drop chance of the chestplate
  294.      * @param dropChanceLeggings   The drop chance of the leggings
  295.      * @param dropChanceBoots      The drop chance of the boots
  296.      * @param dropChanceMainHand   The drop chance of the main hand
  297.      * @param dropChanceOffHand    The drop chance of the off hand
  298.      * @return This EntityBuilder instance for method chaining
  299.      */
  300.     public EntityBuilder setEquipmentDropChance(int dropChanceHelmet, int dropChanceChestplate, int dropChanceLeggings, int dropChanceBoots, int dropChanceMainHand, int dropChanceOffHand) {
  301.         if(entity instanceof LivingEntity) {
  302.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmetDropChance(dropChanceHelmet);
  303.             ((LivingEntity) entity).getEquipment().setChestplateDropChance(dropChanceChestplate);
  304.             ((LivingEntity) entity).getEquipment().setLeggingsDropChance(dropChanceLeggings);
  305.             ((LivingEntity) entity).getEquipment().setBootsDropChance(dropChanceBoots);
  306.             ((LivingEntity) entity).getEquipment().setItemInMainHandDropChance(dropChanceMainHand);
  307.             ((LivingEntity) entity).getEquipment().setItemInOffHandDropChance(dropChanceOffHand);
  308.         }
  309.         return this;
  310.     }
  311.  
  312.     // Additional methods for setting entity properties...
  313.  
  314.     /**
  315.      * Sets the equipment drop chance of the entity.
  316.      *
  317.      * @param dropChanceHelmet     The drop chance of the helmet
  318.      * @param dropChanceChestplate The drop chance of the chestplate
  319.      * @param dropChanceLeggings   The drop chance of the leggings
  320.      * @param dropChanceBoots      The drop chance of the boots
  321.      * @return This EntityBuilder instance for method chaining
  322.      */
  323.     public EntityBuilder setEquipmentDropChance(int dropChanceHelmet, int dropChanceChestplate, int dropChanceLeggings, int dropChanceBoots) {
  324.         if(entity instanceof LivingEntity) {
  325.             Objects.requireNonNull(((LivingEntity) entity).getEquipment()).setHelmetDropChance(dropChanceHelmet);
  326.             ((LivingEntity) entity).getEquipment().setChestplateDropChance(dropChanceChestplate);
  327.             ((LivingEntity) entity).getEquipment().setLeggingsDropChance(dropChanceLeggings);
  328.             ((LivingEntity) entity).getEquipment().setBootsDropChance(dropChanceBoots);
  329.         }
  330.         return this;
  331.     }
  332.  
  333.     // Additional methods for setting entity properties...
  334.  
  335.     /**
  336.      * Sets whether the entity is removed when far away.
  337.      *
  338.      * @param removeWhenFarAway True if the entity should be removed when far away, false otherwise
  339.      * @return This EntityBuilder instance for method chaining
  340.      */
  341.     public EntityBuilder setRemoveWhenFarAway(boolean removeWhenFarAway) {
  342.         if(entity instanceof LivingEntity) {
  343.             ((LivingEntity) entity).setRemoveWhenFarAway(removeWhenFarAway);
  344.         }
  345.         return this;
  346.     }
  347.  
  348.     // Methods for setting entity attributes
  349.  
  350.     /**
  351.      * Sets the attack damage of the entity.
  352.      *
  353.      * @param damage The attack damage value to be set
  354.      * @return This EntityBuilder instance for method chaining
  355.      */
  356.     public EntityBuilder setAttackDamage(double damage) {
  357.         if(entity instanceof Attributable) {
  358.             Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_ATTACK_DAMAGE)).setBaseValue(damage);
  359.         }
  360.         return this;
  361.     }
  362.  
  363.     // Additional methods for setting entity attributes...
  364.  
  365.     /**
  366.      * Sets the movement speed of the entity.
  367.      *
  368.      * @param speed The movement speed value to be set
  369.      * @return This EntityBuilder instance for method chaining
  370.      */
  371.     public EntityBuilder setMovementSpeed(double speed) {
  372.         if(entity instanceof Attributable) {
  373.             Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)).setBaseValue(speed);
  374.         }
  375.         return this;
  376.     }
  377.  
  378.     // Additional methods for setting entity attributes...
  379.  
  380.     /**
  381.      * Sets the attack speed of the entity.
  382.      *
  383.      * @param attackSpeed The attack speed value to be set
  384.      * @return This EntityBuilder instance for method chaining
  385.      */
  386.     public EntityBuilder setAttackSpeed(double attackSpeed) {
  387.         if(entity instanceof Attributable) {
  388.             Objects.requireNonNull(((LivingEntity) entity).getAttribute(Attribute.GENERIC_ATTACK_SPEED)).setBaseValue(attackSpeed);
  389.         }
  390.         return this;
  391.     }
  392.  
  393.     // Additional methods for setting entity attributes...
  394.  
  395. }
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement