DarkBladeDev

ModelEngineManager.java | API issue

Apr 18th, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | Source Code | 0 0
  1. package com.darkbladedev.integrations.modelengine;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.entity.Entity;
  5. import org.bukkit.entity.LivingEntity;
  6. import org.bukkit.plugin.Plugin;
  7.  
  8. import com.darkbladedev.AnimalsAndStuff;
  9. import com.darkbladedev.utils.ColorText;
  10. import com.ticxo.modelengine.api.ModelEngineAPI;
  11. import com.ticxo.modelengine.api.model.ActiveModel;
  12. import com.ticxo.modelengine.api.model.ModeledEntity;
  13.  
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.logging.Level;
  17.  
  18. public class ModelEngineManager {
  19.    
  20.     private final AnimalsAndStuff plugin;
  21.     private boolean modelEngineEnabled = false;
  22.     private ModelEngineAPI modelEngineAPI;
  23.     private Map<String, String> mobModelMap = new HashMap<>();
  24.     private String modelId;
  25.    
  26.     public ModelEngineManager(AnimalsAndStuff plugin) {
  27.         this.plugin = plugin;
  28.         this.initialize();
  29.     }
  30.    
  31.     public void initialize() {
  32.         // Verificar si ModelEngine está presente
  33.         Plugin modelEngine = Bukkit.getPluginManager().getPlugin("ModelEngine");
  34.         if (modelEngine != null && modelEngine.isEnabled()) {
  35.             modelEngineEnabled = true;
  36.             Bukkit.getConsoleSender().sendMessage(ColorText.Colorize("&aModelEngine detected! Enabling model features."));
  37.            
  38.             // Cargar configuración de modelos
  39.             loadModelConfiguration();
  40.            
  41.             // Intentar obtener la API de ModelEngine
  42.             try {
  43.                 this.modelEngineAPI = ModelEngineAPI.getAPI(); // HERE IS THE API INITIALIZATION TRY
  44.                
  45.                 Bukkit.getConsoleSender().sendMessage(ColorText.Colorize("&aModelEngine connection established successfully."));
  46.             } catch (Exception e) {
  47.                 plugin.getLogger().log(Level.WARNING, "Error on MEG connection task", e);
  48.                 modelEngineEnabled = false;
  49.             }
  50.         } else {
  51.             Bukkit.getConsoleSender().sendMessage(ColorText.Colorize("&cModelEngine not detected. 3D models are not available now."));
  52.         }
  53.     }
  54.    
  55.     private void loadModelConfiguration() {
  56.         if (plugin.getConfig().contains("models.mobs")) {
  57.             for (String mobKey : plugin.getConfig().getConfigurationSection("models.mobs").getKeys(false)) {
  58.                 String modelId = plugin.getConfig().getString("models.mobs." + mobKey);
  59.                 if (modelId != null && !modelId.isEmpty()) {
  60.                     mobModelMap.put(mobKey, modelId);
  61.                     Bukkit.getConsoleSender().sendMessage(ColorText.Colorize("&aModel loaded for " + mobKey + ": " + modelId));
  62.                 }
  63.             }
  64.         }
  65.     }
  66.    
  67.     /**
  68.      * Aplica un modelo 3D a una entidad si ModelEngine está disponible
  69.      * @param entity La entidad a la que aplicar el modelo
  70.      * @param mobKey La clave del mob personalizado
  71.      * @return true si se aplicó el modelo, false en caso contrario
  72.      */
  73.     public boolean applyModel(LivingEntity entity, String mobKey) {
  74.         if (!modelEngineEnabled || modelEngineAPI == null) {
  75.             plugin.getLogger().severe("ModelEngine not enabled/loaded, Model application process failed."); // HERE IS THE ERROR MESSAGE
  76.             return false;
  77.         }
  78.        
  79.         // Verificar si hay un modelo configurado para este mob
  80.         String modelId = mobModelMap.get(mobKey);
  81.         if (modelId == null || modelId.isEmpty()) {
  82.             plugin.getLogger().log(Level.SEVERE, mobKey + " model not found. Skipping model application.");
  83.             return false;
  84.         }
  85.        
  86.         plugin.getLogger().info("Applying model (" + modelId + ") to " + mobKey);
  87.        
  88.         try {            
  89.             // Crear un ActiveModel
  90.             ModeledEntity modeledEntity = ModelEngineAPI.createModeledEntity(entity);
  91.            
  92.             ActiveModel activeModel = ModelEngineAPI.createActiveModel(modelId);
  93.  
  94.             // Aplicar el modelo a la entidad
  95.             modeledEntity.addModel(activeModel, true);
  96.  
  97.             return true;
  98.         } catch (Exception e) {
  99.             plugin.getLogger().log(Level.WARNING, "Error applying model to " + mobKey, e);
  100.             return false;
  101.         }
  102.     }
  103.    
  104.     /**
  105.      * Elimina el modelo 3D de una entidad
  106.      * @param entity La entidad de la que eliminar el modelo
  107.      */
  108.     public void removeModel(Entity entity) {
  109.         if (!modelEngineEnabled || modelEngineAPI == null) {
  110.             plugin.getLogger().severe("ModelEngine not enabled/loaded, Model remove process failed.");
  111.             return;
  112.         }
  113.  
  114.         try {
  115.             for (String mobKey : plugin.getConfig().getConfigurationSection("models.mobs").getKeys(false)) {
  116.                 modelId = plugin.getConfig().getString("models.mobs." + mobKey);
  117.                
  118.                 // Eliminar el modelo directamente usando la API
  119.                 ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(entity.getUniqueId());
  120.                 modeledEntity.removeModel(getModelForMob(modelId));
  121.             }
  122.         } catch (Exception e) {
  123.             plugin.getLogger().log(Level.WARNING, "Error removing model", e);
  124.         }
  125.     }
  126.    
  127.     /**
  128.      * Verifica si ModelEngine está habilitado
  129.      * @return true si ModelEngine está habilitado, false en caso contrario
  130.      */
  131.     public boolean isModelEngineEnabled() {
  132.         return modelEngineEnabled;
  133.     }
  134.    
  135.     /**
  136.      * Obtiene el ID del modelo para un mob específico
  137.      * @param mobKey La clave del mob
  138.      * @return El ID del modelo, o null si no hay ninguno configurado
  139.      */
  140.     public String getModelForMob(String mobKey) {
  141.         return mobModelMap.get(mobKey);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment