Search in sources :

Example 1 with Configuration

use of me.deecaad.core.file.Configuration in project MechanicsMain by WeaponMechanics.

the class WeaponMechanics method loadConfig.

void loadConfig() {
    debug.debug("Loading and serializing config");
    try {
        List<?> serializers = new SerializerInstancer(new JarFile(getFile())).createAllInstances(getClassLoader());
        // noinspection unchecked
        MechanicsCore.addSerializers(getPlugin(), (List<Serializer<?>>) serializers);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    if (configurations == null) {
        configurations = new LinkedConfig();
    } else {
        configurations.clear();
    }
    List<IValidator> validators = null;
    try {
        // Find all validators in WeaponMechanics
        validators = new JarInstancer(new JarFile(getFile())).createAllInstances(IValidator.class, getClassLoader(), true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Fill configuration mappings (except config.yml)
    Configuration temp = new FileReader(debug, MechanicsCore.getListOfSerializers(), validators).fillAllFiles(getDataFolder(), "config.yml");
    try {
        configurations.add(temp);
    } catch (DuplicateKeyException e) {
        debug.error("Error loading config: " + e.getMessage());
    }
}
Also used : IValidator(me.deecaad.core.file.IValidator) Configuration(me.deecaad.core.file.Configuration) LinkedConfig(me.deecaad.core.file.LinkedConfig) JarInstancer(me.deecaad.core.file.JarInstancer) SerializerInstancer(me.deecaad.core.file.SerializerInstancer) FileReader(me.deecaad.core.file.FileReader) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) DuplicateKeyException(me.deecaad.core.file.DuplicateKeyException) Serializer(me.deecaad.core.file.Serializer)

Example 2 with Configuration

use of me.deecaad.core.file.Configuration in project MechanicsMain by WeaponMechanics.

the class DamageHandler method tryUse.

/**
 * @return false if damaging was cancelled
 */
public boolean tryUse(LivingEntity victim, double damage, DamagePoint point, boolean isBackstab, LivingEntity shooter, String weaponTitle, ItemStack weaponStack, double distanceTravelled) {
    Configuration config = getConfigurations();
    boolean isFriendlyFire = config.getBool(weaponTitle + ".Damage.Enable_Friendly_Fire");
    if (!isFriendlyFire && !DamageUtil.canHarm(shooter, victim)) {
        return false;
    }
    boolean isOwnerImmune = config.getBool(weaponTitle + ".Damage.Enable_Owner_Immunity");
    if (isOwnerImmune && victim.equals(shooter)) {
        return false;
    }
    // Critical Hit chance
    double chance = config.getDouble(weaponTitle + ".Damage.Critical_Hit.Chance", -1);
    boolean isCritical = chance != -1 && NumberUtil.chance((chance / 100));
    int armorDamage = config.getInt(weaponTitle + ".Damage.Armor_Damage");
    int fireTicks = config.getInt(weaponTitle + ".Damage.Fire_Ticks");
    WeaponDamageEntityEvent damageEntityEvent = new WeaponDamageEntityEvent(weaponTitle, weaponStack, shooter, victim, damage, isBackstab, isCritical, point, armorDamage, fireTicks, distanceTravelled);
    Bukkit.getPluginManager().callEvent(damageEntityEvent);
    if (damageEntityEvent.isCancelled())
        return false;
    fireTicks = damageEntityEvent.getFireTicks();
    point = damageEntityEvent.getPoint();
    if (DamageUtil.apply(shooter, victim, damageEntityEvent.getFinalDamage())) {
        WeaponMechanics.debug.debug("Damage was cancelled");
        // Damage was cancelled
        return false;
    }
    DamageUtil.damageArmor(victim, damageEntityEvent.getArmorDamage(), point);
    // Fire ticks
    if (fireTicks > 0) {
        victim.setFireTicks(fireTicks);
    }
    EntityWrapper shooterWrapper = WeaponMechanics.getEntityWrapper(shooter, true);
    CastData shooterCast;
    if (shooterWrapper != null) {
        shooterCast = new CastData(shooterWrapper, weaponTitle, weaponStack);
    } else {
        shooterCast = new CastData(shooter, weaponTitle, weaponStack);
    }
    shooterCast.setData(CommonDataTags.TARGET_LOCATION.name(), victim.getLocation());
    shooterCast.setData(CommonDataTags.SHOOTER_NAME.name(), shooter.getName());
    shooterCast.setData(CommonDataTags.VICTIM_NAME.name(), victim.getName());
    EntityWrapper victimWrapper = WeaponMechanics.getEntityWrapper(victim, true);
    CastData victimCast;
    if (victimWrapper != null) {
        victimCast = new CastData(victimWrapper, weaponTitle, weaponStack);
    } else {
        victimCast = new CastData(victim, weaponTitle, weaponStack);
    }
    victimCast.setData(CommonDataTags.TARGET_LOCATION.name(), shooter.getLocation());
    victimCast.setData(CommonDataTags.SHOOTER_NAME.name(), shooter.getName());
    victimCast.setData(CommonDataTags.VICTIM_NAME.name(), victim.getName());
    // On all damage
    useMechanics(config, shooterCast, victimCast, weaponTitle + ".Damage");
    // On point
    if (point != null)
        useMechanics(config, shooterCast, victimCast, weaponTitle + ".Damage." + point.getReadable());
    // On backstab
    if (damageEntityEvent.isBackstab()) {
        useMechanics(config, shooterCast, victimCast, weaponTitle + ".Damage.Backstab");
    }
    // On critical
    if (damageEntityEvent.isCritical()) {
        useMechanics(config, shooterCast, victimCast, weaponTitle + ".Damage.Critical_Hit");
    }
    if (victim.isDead() || victim.getHealth() <= 0.0) {
        Bukkit.getPluginManager().callEvent(new WeaponKillEntityEvent(weaponTitle, weaponStack, shooter, victim, damageEntityEvent));
        // On kill
        useMechanics(config, shooterCast, victimCast, weaponTitle + ".Damage.Kill");
    }
    return true;
}
Also used : CastData(me.deecaad.weaponmechanics.mechanics.CastData) Configuration(me.deecaad.core.file.Configuration) WeaponDamageEntityEvent(me.deecaad.weaponmechanics.weapon.weaponevents.WeaponDamageEntityEvent) EntityWrapper(me.deecaad.weaponmechanics.wrappers.EntityWrapper) WeaponKillEntityEvent(me.deecaad.weaponmechanics.weapon.weaponevents.WeaponKillEntityEvent)

Example 3 with Configuration

use of me.deecaad.core.file.Configuration in project MechanicsMain by WeaponMechanics.

the class MeleeHandler method meleeWithoutTimings.

private boolean meleeWithoutTimings(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield, @Nullable LivingEntity knownVictim) {
    Configuration config = getConfigurations();
    HandData handData = entityWrapper.getMainHandData();
    int meleeHitDelay = config.getInt(weaponTitle + ".Melee.Melee_Hit_Delay");
    if (meleeHitDelay != 0 && !NumberUtil.hasMillisPassed(handData.getLastMeleeTime(), meleeHitDelay))
        return false;
    int meleeMissDelay = config.getInt(weaponTitle + ".Melee.Melee_Miss.Melee_Miss_Delay");
    if (meleeMissDelay != 0 && !NumberUtil.hasMillisPassed(handData.getLastMeleeMissTime(), meleeMissDelay))
        return false;
    double meleeRange = config.getDouble(weaponTitle + ".Melee.Melee_Range");
    LivingEntity shooter = entityWrapper.getEntity();
    Location eyeLocation = shooter.getEyeLocation();
    Vector direction = eyeLocation.getDirection();
    RayTraceResult hit = getHit(shooter, eyeLocation, direction, meleeRange, knownVictim);
    boolean result = false;
    if (hit != null) {
        result = weaponHandler.getShootHandler().shootWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield);
        if (result) {
            if (meleeHitDelay != 0) {
                handData.setLastMeleeTime(System.currentTimeMillis());
                if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Melee_Hit_Delay") && shooter.getType() == EntityType.PLAYER) {
                    CompatibilityAPI.getEntityCompatibility().setCooldown((Player) shooter, weaponStack.getType(), meleeHitDelay / 50);
                }
            }
            hit.handleMeleeHit(shooter, direction, weaponTitle, weaponStack);
        }
    } else {
        // Handle miss
        Mechanics meleeMissMechanics = getConfigurations().getObject(weaponTitle + ".Melee.Melee_Miss.Mechanics", Mechanics.class);
        if (meleeMissMechanics != null)
            meleeMissMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
        if (getConfigurations().getBool(weaponTitle + ".Melee.Melee_Miss.Consume_On_Miss")) {
            weaponHandler.getShootHandler().shootWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield);
            result = true;
        }
        if (meleeMissDelay != 0) {
            handData.setLastMeleeMissTime(System.currentTimeMillis());
            if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Melee_Miss_Delay") && shooter.getType() == EntityType.PLAYER) {
                CompatibilityAPI.getEntityCompatibility().setCooldown((Player) shooter, weaponStack.getType(), meleeMissDelay / 50);
            }
        }
    }
    return result;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) CastData(me.deecaad.weaponmechanics.mechanics.CastData) Configuration(me.deecaad.core.file.Configuration) RayTraceResult(me.deecaad.weaponmechanics.weapon.projectile.weaponprojectile.RayTraceResult) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponMechanics(me.deecaad.weaponmechanics.WeaponMechanics) Vector(org.bukkit.util.Vector) HandData(me.deecaad.weaponmechanics.wrappers.HandData) Location(org.bukkit.Location)

Example 4 with Configuration

use of me.deecaad.core.file.Configuration in project MechanicsMain by WeaponMechanics.

the class MeleeHandler method tryUse.

/**
 * Tries to use melee
 *
 * @param entityWrapper the entity who used trigger
 * @param weaponTitle   the weapon title
 * @param weaponStack   the weapon stack
 * @param slot          the slot used on trigger
 * @param triggerType   the trigger type trying to activate melee
 * @param dualWield     whether this was dual wield
 * @return true if was able to melee
 */
public boolean tryUse(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield, @Nullable LivingEntity knownVictim) {
    if (triggerType != TriggerType.MELEE)
        return false;
    Configuration config = getConfigurations();
    if (!config.getBool(weaponTitle + ".Melee.Enable_Melee")) {
        // Change weapon title to match the attachment
        weaponTitle = config.getString(weaponTitle + ".Melee.Melee_Attachment");
        // Melee isn't used for this weapon nor the melee attachment is defined
        if (weaponTitle == null)
            return false;
    }
    MCTiming meleeHandlerTiming = WeaponMechanics.timing("Melee Handler").startTiming();
    boolean result = meleeWithoutTimings(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield, knownVictim);
    meleeHandlerTiming.stopTiming();
    return result;
}
Also used : Configuration(me.deecaad.core.file.Configuration) MCTiming(co.aikar.timings.lib.MCTiming)

Example 5 with Configuration

use of me.deecaad.core.file.Configuration in project MechanicsMain by WeaponMechanics.

the class ScopeHandler method tryUse.

/**
 * Tries to use scope
 *
 * @param entityWrapper the entity who used trigger
 * @param weaponTitle the weapon title
 * @param weaponStack the weapon stack
 * @param slot the slot used on trigger
 * @param triggerType the trigger type trying to activate scope
 * @return true if the scope used successfully to zoom in, our or stack
 */
public boolean tryUse(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield) {
    Configuration config = getConfigurations();
    // Don't try to scope if either one of the hands is reloading
    if (entityWrapper.getMainHandData().isReloading() || entityWrapper.getOffHandData().isReloading()) {
        return false;
    }
    ZoomData zoomData;
    // Only allow using zoom at one hand at time
    if (slot == EquipmentSlot.HAND) {
        if (entityWrapper.getOffHandData().getZoomData().isZooming()) {
            return false;
        }
        zoomData = entityWrapper.getMainHandData().getZoomData();
    } else {
        if (entityWrapper.getMainHandData().getZoomData().isZooming()) {
            return false;
        }
        zoomData = entityWrapper.getOffHandData().getZoomData();
    }
    Trigger trigger = config.getObject(weaponTitle + ".Scope.Trigger", Trigger.class);
    if (trigger == null)
        return false;
    // Check if entity is already zooming
    if (zoomData.isZooming()) {
        Trigger offTrigger = config.getObject(weaponTitle + ".Scope.Zoom_Off.Trigger", Trigger.class);
        // If off trigger is valid -> zoom out even if stacking hasn't reached maximum stacks
        if (offTrigger != null && offTrigger.check(triggerType, slot, entityWrapper)) {
            return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
        }
        // If trigger is valid zoom in or out depending on situation
        if (trigger.check(triggerType, slot, entityWrapper)) {
            int maximumStacks = config.getInt(weaponTitle + ".Scope.Zoom_Stacking.Maximum_Stacks");
            if (maximumStacks <= 0) {
                // Should turn off
                return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
            }
            if (zoomData.getZoomStacks() < maximumStacks) {
                // Zoom in handles stacking on its own
                return zoomIn(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
            }
            // Should turn off (because zoom stacks have reached maximum stacks)
            return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
        }
    } else if (trigger.check(triggerType, slot, entityWrapper)) {
        // Try zooming in since entity is not zooming
        return zoomIn(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
    }
    return false;
}
Also used : Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger) Configuration(me.deecaad.core.file.Configuration) ZoomData(me.deecaad.weaponmechanics.wrappers.ZoomData)

Aggregations

Configuration (me.deecaad.core.file.Configuration)19 CastData (me.deecaad.weaponmechanics.mechanics.CastData)7 Mechanics (me.deecaad.weaponmechanics.mechanics.Mechanics)6 LivingEntity (org.bukkit.entity.LivingEntity)6 WeaponMechanics (me.deecaad.weaponmechanics.WeaponMechanics)5 PlayerWrapper (me.deecaad.weaponmechanics.wrappers.PlayerWrapper)5 Location (org.bukkit.Location)5 WeaponInfoDisplay (me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay)4 HandData (me.deecaad.weaponmechanics.wrappers.HandData)4 ItemStack (org.bukkit.inventory.ItemStack)4 Vector (org.bukkit.util.Vector)4 ReloadHandler (me.deecaad.weaponmechanics.weapon.reload.ReloadHandler)3 Trigger (me.deecaad.weaponmechanics.weapon.trigger.Trigger)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 IOException (java.io.IOException)2 JarFile (java.util.jar.JarFile)2 FileReader (me.deecaad.core.file.FileReader)2 IValidator (me.deecaad.core.file.IValidator)2 LinkedConfig (me.deecaad.core.file.LinkedConfig)2 FirearmAction (me.deecaad.weaponmechanics.weapon.firearm.FirearmAction)2