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);
}
}
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();
}
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);
}
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();
}
Aggregations