Search in sources :

Example 1 with GENERIC_DATA_STORAGE

use of com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage.GENERIC_DATA_STORAGE in project Overloaded by CJ-MC-Mods.

the class ItemRailGun method handleFireMessage.

public void handleFireMessage(@Nonnull ServerPlayerEntity player, @Nonnull RailGunFireMessage message) {
    ItemStack itemStack = player.getItemInHand(message.hand);
    if (itemStack.getItem() != this) {
        return;
    }
    LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
    if (!opEnergy.isPresent()) {
        Overloaded.logger.warn("RailGun has no Energy Capability? NBT: " + itemStack.getTag());
        return;
    }
    IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    LazyOptional<IGenericDataStorage> opSettingCapability = itemStack.getCapability(GENERIC_DATA_STORAGE);
    if (!opSettingCapability.isPresent()) {
        Overloaded.logger.warn("RailGun has no GenericData Capability? NBT: " + itemStack.getTag());
        return;
    }
    IGenericDataStorage settingCapability = opSettingCapability.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    settingCapability.suggestUpdate();
    int energyRequired = settingCapability.getIntegerMap().getOrDefault(RAILGUN_POWER_KEY, OverloadedConfig.INSTANCE.railGun.minEnergy);
    if (energy.getEnergyStored() < energyRequired) {
        player.displayClientMessage(new StringTextComponent("Not enough power to fire."), true);
        return;
    }
    int energyExtracted = energy.extractEnergy(energyRequired, false);
    @Nullable Entity entity = player.level.getEntity(message.id);
    if (entity == null || !entity.isAlive()) {
        return;
    } else if (player.distanceTo(entity) > OverloadedConfig.INSTANCE.rayGun.maxRange) {
        player.displayClientMessage(new StringTextComponent("Target out of range."), true);
    } else if (entity.hurt(DamageSource.playerAttack(player), (float) (OverloadedConfig.INSTANCE.railGun.damagePerRF * energyExtracted))) {
        Vector3d knockback = message.moveVector.scale(energyExtracted * OverloadedConfig.INSTANCE.railGun.knockbackPerRF);
        entity.push(knockback.x, knockback.y, knockback.z);
    }
}
Also used : IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 2 with GENERIC_DATA_STORAGE

use of com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage.GENERIC_DATA_STORAGE in project Overloaded by CJ-MC-Mods.

the class ArmorEventHandler method getHelmetDataStorage.

@Nonnull
private static IGenericDataStorage getHelmetDataStorage(PlayerEntity player) {
    for (ItemStack stack : player.inventory.armor) {
        if (stack.getItem() instanceof ItemMultiHelmet) {
            IGenericDataStorage cap = stack.getCapability(GENERIC_DATA_STORAGE).orElse(new GenericDataStorage());
            cap.suggestUpdate();
            return cap;
        }
    }
    return new GenericDataStorage();
}
Also used : IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage) IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage) GenericDataStorage(com.cjm721.overloaded.storage.GenericDataStorage) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 3 with GENERIC_DATA_STORAGE

use of com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage.GENERIC_DATA_STORAGE in project Overloaded by CJ-MC-Mods.

the class ItemRailGun method handleSettingsMessage.

public void handleSettingsMessage(@Nonnull ServerPlayerEntity player, @Nonnull RailGunSettingsMessage message) {
    ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
    if (itemStack.getItem() != this) {
        return;
    }
    LazyOptional<IGenericDataStorage> opCap = itemStack.getCapability(GENERIC_DATA_STORAGE);
    if (!opCap.isPresent()) {
        Overloaded.logger.warn("RailGun has no GenericData Capability? NBT: " + itemStack.getTag());
        return;
    }
    IGenericDataStorage cap = opCap.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    Map<String, Integer> integerMap = cap.getIntegerMap();
    int power = integerMap.getOrDefault(RAILGUN_POWER_KEY, 0) + message.powerDelta;
    power = Ints.constrainToRange(power, OverloadedConfig.INSTANCE.railGun.minEnergy, OverloadedConfig.INSTANCE.railGun.maxEnergy);
    integerMap.put(RAILGUN_POWER_KEY, power);
    cap.suggestSave();
    player.displayClientMessage(new StringTextComponent("Power usage set to: " + NumberFormat.getInstance().format(power)), true);
}
Also used : IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack)

Example 4 with GENERIC_DATA_STORAGE

use of com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage.GENERIC_DATA_STORAGE in project Overloaded by CJ-MC-Mods.

the class ItemMultiHelmet method updateSettings.

private void updateSettings(ItemStack itemStack, MultiArmorSettingsMessage message) {
    LazyOptional<IGenericDataStorage> opSettings = itemStack.getCapability(GENERIC_DATA_STORAGE);
    if (!opSettings.isPresent()) {
        Overloaded.logger.warn("MultiHelmet has no GenericData Capability? NBT: " + itemStack.getTag());
        return;
    }
    IGenericDataStorage settings = opSettings.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    settings.suggestUpdate();
    Map<String, Float> floats = settings.getFloatMap();
    floats.put(DataKeys.FLIGHT_SPEED, Floats.constrainToRange(message.flightSpeed, 0, (float) OverloadedConfig.INSTANCE.multiArmorConfig.maxFlightSpeed));
    floats.put(DataKeys.GROUND_SPEED, Floats.constrainToRange(message.groundSpeed, 0, (float) OverloadedConfig.INSTANCE.multiArmorConfig.maxGroundSpeed));
    Map<String, Boolean> booleans = settings.getBooleanMap();
    booleans.put(DataKeys.NOCLIP_FLIGHT_LOCK, message.noclipFlightLock);
    booleans.put(DataKeys.FLIGHT, message.flight);
    booleans.put(DataKeys.FEED, message.feed);
    booleans.put(DataKeys.HEAL, message.heal);
    booleans.put(DataKeys.REMOVE_HARMFUL, message.removeHarmful);
    booleans.put(DataKeys.GIVE_AIR, message.air);
    booleans.put(DataKeys.EXTINGUISH, message.extinguish);
    settings.suggestSave();
}
Also used : IGenericDataStorage(com.cjm721.overloaded.storage.IGenericDataStorage)

Aggregations

IGenericDataStorage (com.cjm721.overloaded.storage.IGenericDataStorage)4 ItemStack (net.minecraft.item.ItemStack)3 StringTextComponent (net.minecraft.util.text.StringTextComponent)2 GenericDataStorage (com.cjm721.overloaded.storage.GenericDataStorage)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ClientPlayerEntity (net.minecraft.client.entity.player.ClientPlayerEntity)1 Entity (net.minecraft.entity.Entity)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 Vector3d (net.minecraft.util.math.vector.Vector3d)1 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)1