use of com.cjm721.overloaded.network.packets.RailGunFireMessage 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.network.packets.RailGunFireMessage in project Overloaded by CJ-MC-Mods.
the class CommonProxy method commonSetup.
public void commonSetup(FMLCommonSetupEvent event) {
CapabilityHyperItem.register();
CapabilityHyperEnergy.register();
CapabilityHyperFluid.register();
CapabilityGenericDataStorage.register();
networkWrapper = newSimpleChannel(ResourceLocation.of("overloaded_network", '_'), () -> "1.0", v -> true, v -> true);
int dis = 0;
networkWrapper.registerMessage(dis++, LeftClickBlockMessage.class, LeftClickBlockMessage::toBytes, LeftClickBlockMessage::fromBytes, new PlayerMessageHandler<>(ItemMultiTool::leftClickOnBlockServer));
networkWrapper.registerMessage(dis++, RightClickBlockMessage.class, RightClickBlockMessage::toBytes, RightClickBlockMessage::fromBytes, new PlayerMessageHandler<>(ModItems.multiTool::rightClickWithItem));
networkWrapper.registerMessage(dis++, RayGunMessage.class, RayGunMessage::toBytes, RayGunMessage::fromBytes, new PlayerMessageHandler<>(ModItems.rayGun::handleMessage));
networkWrapper.registerMessage(dis++, MultiArmorSettingsMessage.class, MultiArmorSettingsMessage::toBytes, MultiArmorSettingsMessage::fromBytes, new PlayerMessageHandler<>(ModItems.customHelmet::updateSettings));
networkWrapper.registerMessage(dis++, RailGunFireMessage.class, RailGunFireMessage::toBytes, RailGunFireMessage::fromBytes, new PlayerMessageHandler<>(ModItems.railgun::handleFireMessage));
networkWrapper.registerMessage(dis++, RailGunSettingsMessage.class, RailGunSettingsMessage::toBytes, RailGunSettingsMessage::fromBytes, new PlayerMessageHandler<>(ModItems.railgun::handleSettingsMessage));
networkWrapper.registerMessage(dis++, KeyBindPressedMessage.class, KeyBindPressedMessage::toBytes, KeyBindPressedMessage::fromBytes, new KeyBindPressedHandler());
networkWrapper.registerMessage(dis++, NoClipStatusMessage.class, NoClipStatusMessage::toBytes, NoClipStatusMessage::fromBytes, new NoClipUpdateHandler());
networkWrapper.registerMessage(dis++, ContainerDataMessage.class, ContainerDataMessage::toBytes, ContainerDataMessage::fromBytes, new ContainerDataHandler());
MinecraftForge.EVENT_BUS.register(new ArmorEventHandler());
}
use of com.cjm721.overloaded.network.packets.RailGunFireMessage in project Overloaded by CJ-MC-Mods.
the class ItemRailGun method use.
@Override
@Nonnull
@OnlyIn(Dist.CLIENT)
public ActionResult<ItemStack> use(World worldIn, @Nonnull PlayerEntity playerIn, @Nonnull Hand handIn) {
if (worldIn.isClientSide) {
int distance = OverloadedConfig.INSTANCE.railGun.maxRange;
Vector3d vec3d = playerIn.getEyePosition(Minecraft.getInstance().getFrameTime());
Vector3d vec3d1 = playerIn.getViewVector(Minecraft.getInstance().getFrameTime());
Vector3d vec3d2 = vec3d.add(vec3d1.x * distance, vec3d1.y * distance, vec3d1.z * distance);
float f = 1.0F;
AxisAlignedBB axisalignedbb = playerIn.getBoundingBox().expandTowards(vec3d1.scale(distance)).inflate(1.0D, 1.0D, 1.0D);
EntityRayTraceResult ray = ProjectileHelper.getEntityHitResult(playerIn, vec3d, vec3d2, axisalignedbb, (p_215312_0_) -> !p_215312_0_.isSpectator() && p_215312_0_.isPickable(), distance * distance);
if (ray != null) {
Vector3d moveVev = playerIn.getEyePosition(1).subtract(ray.getLocation()).normalize().scale(-1.0);
Overloaded.proxy.networkWrapper.sendToServer(new RailGunFireMessage(ray.getEntity().getId(), moveVev, handIn));
} else {
Overloaded.proxy.networkWrapper.sendToServer(new RailGunFireMessage(0, Vector3d.ZERO, handIn));
}
}
return new ActionResult<>(ActionResultType.SUCCESS, playerIn.getItemInHand(handIn));
}
Aggregations