From a98e7c921cbf0a492f41b33d465e49405b43677d Mon Sep 17 00:00:00 2001 From: BluSunrize Date: Wed, 26 Jun 2024 22:50:07 +0200 Subject: [PATCH] Change revolvers to use the vanilla cooldown system --- changelog.md | 2 + .../client/ItemOverlayUtils.java | 41 ---- .../common/items/RevolverItem.java | 191 ++++++++---------- 3 files changed, 85 insertions(+), 149 deletions(-) diff --git a/changelog.md b/changelog.md index 6055c68d39..c9dae391e3 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,8 @@ - Change villager trades for revolver pieces and mineral deposit maps to re-roll (BluSunrize) - After buying the item, the trade will be replaced with a new one - This only works on villagers acquired AFTER this update! +- Change revolvers to use the vanilla cooldown system (BluSunrize) + - This does unfortunately make dualwielding less powerful, because cooldowns affect all revolvers - Prevent crates from being opened after being sealed (BluSunrize) - Fix villagers being unable to interact with IE doors (BluSunrize) - Fix steel doors dropping twice (BluSunrize) diff --git a/src/main/java/blusunrize/immersiveengineering/client/ItemOverlayUtils.java b/src/main/java/blusunrize/immersiveengineering/client/ItemOverlayUtils.java index d4a0b28832..a8aff8d267 100644 --- a/src/main/java/blusunrize/immersiveengineering/client/ItemOverlayUtils.java +++ b/src/main/java/blusunrize/immersiveengineering/client/ItemOverlayUtils.java @@ -58,51 +58,10 @@ public static void renderRevolverOverlay(MultiBufferSource.BufferSource buffer, float dy = scaledHeight-64; PoseStack transform = graphics.pose(); transform.pushPose(); - transform.pushPose(); transform.translate(dx, dy, 0); transform.scale(.5f, .5f, 1); RevolverScreen.drawExternalGUI(bullets, bulletAmount, graphics); transform.popPose(); - - if(equipped.getItem() instanceof RevolverItem) - { - int cd = ((RevolverItem)equipped.getItem()).getShootCooldown(equipped); - float cdMax = ((RevolverItem)equipped.getItem()).getMaxShootCooldown(equipped); - float cooldown = 1-cd/cdMax; - if(cooldown > 0) - { - transform.translate(scaledWidth/2+(right?1: -6), scaledHeight/2-7, 0); - - float h1 = cooldown > .33?.5f: cooldown*1.5f; - float h2 = cooldown; - float x2 = cooldown < .75?1: 4*(1-cooldown); - - float uMin = (88+(right?0: 7*x2))/256f; - float uMax = (88+(right?7*x2: 0))/256f; - float vMin1 = (112+(right?h1: h2)*15)/256f; - float vMin2 = (112+(right?h2: h1)*15)/256f; - - VertexConsumer builder = getHudElementsBuilder(buffer); - Matrix4f mat = transform.last().pose(); - builder.vertex(mat, (right?0: 1-x2)*7, 15, 0) - .color(1F, 1F, 1F, 1F) - .uv(uMin, 127/256f) - .endVertex(); - builder.vertex(mat, (right?x2: 1)*7, 15, 0) - .color(1F, 1F, 1F, 1F) - .uv(uMax, 127/256f) - .endVertex(); - builder.vertex(mat, (right?x2: 1)*7, (right?h2: h1)*15, 0) - .color(1F, 1F, 1F, 1F) - .uv(uMax, vMin2) - .endVertex(); - builder.vertex(mat, (right?0: 1-x2)*7, (right?h1: h2)*15, 0) - .color(1F, 1F, 1F, 1F) - .uv(uMin, vMin1) - .endVertex(); - } - } - transform.popPose(); } } diff --git a/src/main/java/blusunrize/immersiveengineering/common/items/RevolverItem.java b/src/main/java/blusunrize/immersiveengineering/common/items/RevolverItem.java index cb56dc8fb6..8483a5af0e 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/items/RevolverItem.java +++ b/src/main/java/blusunrize/immersiveengineering/common/items/RevolverItem.java @@ -45,7 +45,6 @@ import net.minecraft.util.Mth; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EquipmentSlot; @@ -215,14 +214,6 @@ public void inventoryTick(@Nonnull ItemStack stack, @Nonnull Level world, @Nonnu else ItemNBTHelper.putInt(stack, "reload", reload); } - if(ItemNBTHelper.hasKey(stack, "cooldown")) - { - int cooldown = ItemNBTHelper.getInt(stack, "cooldown")-1; - if(cooldown <= 0) - ItemNBTHelper.remove(stack, "cooldown"); - else - ItemNBTHelper.putInt(stack, "cooldown", cooldown); - } } } @@ -238,98 +229,104 @@ public UseAnim getUseAnimation(@Nonnull ItemStack stack) public InteractionResultHolder use(Level world, Player player, @Nonnull InteractionHand hand) { ItemStack revolver = player.getItemInHand(hand); - if(!world.isClientSide) + if(player.isShiftKeyDown()) { - if(player.isShiftKeyDown()) - { - openGui(player, hand); - return new InteractionResultHolder<>(InteractionResult.SUCCESS, revolver); - } - else if(player.getAttackStrengthScale(1) >= 1) + openGui(player, hand); + return InteractionResultHolder.sidedSuccess(revolver, world.isClientSide()); + } + + // not yet fully drawn + if(player.getAttackStrengthScale(1) < 1) + return InteractionResultHolder.pass(revolver); + + if(this.getUpgrades(revolver).getBoolean("nerf")) + { + world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 1f, 0.6f); + return InteractionResultHolder.sidedSuccess(revolver, world.isClientSide()); + } + + // on cooldown, can't be used + if(player.getCooldowns().isOnCooldown(this)) + return InteractionResultHolder.pass(revolver); + + NonNullList bullets = getBullets(revolver); + // check if empty and try to use speedloader + if(bullets.stream().noneMatch(stack -> stack.getItem() instanceof BulletItem)) + { + if(useSpeedloader(world, player, revolver, hand, bullets)) + return InteractionResultHolder.sidedSuccess(revolver, world.isClientSide()); + } + + ItemStack bulletStack = bullets.get(0); + Item bullet0 = bulletStack.getItem(); + if(bullet0 instanceof BulletItem) + { + IBullet bullet = ((BulletItem)bullet0).getType(); + if(bullet!=null) { - if(this.getUpgrades(revolver).getBoolean("nerf")) - world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 1f, 0.6f); + if(!world.isClientSide()) + { + float noise = fireProjectile(world, player, revolver, bullet, bulletStack); + bullets.set(0, bullet.getCasing(bullets.get(0)).copy()); + // alert nearby enemies + Utils.attractEnemies(player, 64*noise); + // Revolvers with more than 60% noise reduction do not trigger sculk sensors + if(noise > .2f) + { + // anything louder than default is considered an explosion + GameEvent eventTriggered = noise > 0.5?GameEvent.EXPLODE: GameEvent.PROJECTILE_SHOOT; + player.gameEvent(eventTriggered); + } + } else { - if(getShootCooldown(revolver) > 0||ItemNBTHelper.hasKey(revolver, "reload")) - return new InteractionResultHolder<>(InteractionResult.PASS, revolver); - - NonNullList bullets = getBullets(revolver); - - if(isEmpty(revolver, false)) - for(int i = 0; i < player.getInventory().getContainerSize(); i++) - { - ItemStack stack = player.getInventory().getItem(i); - if(stack.getItem() instanceof SpeedloaderItem&&!((SpeedloaderItem)stack.getItem()).isEmpty(stack)) - { - for(ItemStack b : bullets) - if(!b.isEmpty()) - world.addFreshEntity(new ItemEntity(world, player.getX(), player.getY(), player.getZ(), b)); - setBullets(revolver, ((SpeedloaderItem)stack.getItem()).getContainedItems(stack), true); - ((SpeedloaderItem)stack.getItem()).setContainedItems(stack, NonNullList.withSize(8, ItemStack.EMPTY)); - player.getInventory().setChanged(); - if(player instanceof ServerPlayer) - PacketDistributor.PLAYER.with((ServerPlayer)player).send(new MessageSpeedloaderSync(i, hand)); - - ItemNBTHelper.putInt(revolver, "reload", 60); - return new InteractionResultHolder<>(InteractionResult.SUCCESS, revolver); - } - } - - if(!ItemNBTHelper.hasKey(revolver, "reload")) + // fancy particle effects for shaders + ShaderAndCase shader = ShaderRegistry.getStoredShaderAndCase(revolver); + if(shader!=null) { - ItemStack bulletStack = bullets.get(0); - Item bullet0 = bulletStack.getItem(); - if(bullet0 instanceof BulletItem) - { - IBullet bullet = ((BulletItem)bullet0).getType(); - if(bullet!=null) - { - float noise = fireProjectile(world, player, revolver, bullet, bulletStack); - bullets.set(0, bullet.getCasing(bullets.get(0)).copy()); - // alert nearby enemies - Utils.attractEnemies(player, 64*noise); - // Revolvers with more than 60% noise reduction do not trigger sculk sensors - if(noise > .2f) - { - // anything louder than default is considered an explosion - GameEvent eventTriggered = noise > 0.5?GameEvent.EXPLODE: GameEvent.PROJECTILE_SHOOT; - world.gameEvent(eventTriggered, player.position(), GameEvent.Context.of(player)); - } - } - else - world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.NOTE_BLOCK_HAT.value(), SoundSource.PLAYERS, 1f, 1f); - } - else - world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.NOTE_BLOCK_HAT.value(), SoundSource.PLAYERS, 1f, 1f); - - rotateCylinder(revolver, player, true, bullets); - ItemNBTHelper.putInt(revolver, "cooldown", getMaxShootCooldown(revolver)); - return new InteractionResultHolder<>(InteractionResult.SUCCESS, revolver); + Vec3 pos = Utils.getLivingFrontPos(player, .75, player.getBbHeight()*.75, ItemUtils.getLivingHand(player, hand), false, 1); + shader.registryEntry().getEffectFunction().execute(world, shader.shader(), revolver, + shader.sCase().getShaderType().toString(), pos, + Vec3.directionFromRotation(player.getRotationVector()), .125f); } } } + else + world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.NOTE_BLOCK_HAT.value(), SoundSource.PLAYERS, 1f, 1f); } - else if(!player.isShiftKeyDown()) + else + world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.NOTE_BLOCK_HAT.value(), SoundSource.PLAYERS, 1f, 1f); + + rotateCylinder(revolver, player, true, bullets); + player.getCooldowns().addCooldown(this, getMaxShootCooldown(revolver)); + return InteractionResultHolder.sidedSuccess(revolver, world.isClientSide()); + } + + public boolean useSpeedloader(Level level, Player player, ItemStack revolver, InteractionHand hand, NonNullList bullets) + { + for(int i = 0; i < player.getInventory().getContainerSize(); i++) { - if(getShootCooldown(revolver) > 0||ItemNBTHelper.hasKey(revolver, "reload")) - return new InteractionResultHolder<>(InteractionResult.PASS, revolver); - NonNullList bullets = getBullets(revolver); - if(!bullets.get(0).isEmpty()&&bullets.get(0).getItem() instanceof BulletItem) + ItemStack stack = player.getInventory().getItem(i); + if(stack.getItem() instanceof SpeedloaderItem&&!((SpeedloaderItem)stack.getItem()).isEmpty(stack)) { - ShaderAndCase shader = ShaderRegistry.getStoredShaderAndCase(revolver); - if(shader!=null) + if(!level.isClientSide()) { - - Vec3 pos = Utils.getLivingFrontPos(player, .75, player.getBbHeight()*.75, ItemUtils.getLivingHand(player, hand), false, 1); - shader.registryEntry().getEffectFunction().execute(world, shader.shader(), revolver, - shader.sCase().getShaderType().toString(), pos, - Vec3.directionFromRotation(player.getRotationVector()), .125f); + for(ItemStack b : bullets) + if(!b.isEmpty()) + level.addFreshEntity(new ItemEntity(level, player.getX(), player.getY(), player.getZ(), b)); + setBullets(revolver, ((SpeedloaderItem)stack.getItem()).getContainedItems(stack), true); + ((SpeedloaderItem)stack.getItem()).setContainedItems(stack, NonNullList.withSize(8, ItemStack.EMPTY)); + player.getInventory().setChanged(); + if(player instanceof ServerPlayer) + PacketDistributor.PLAYER.with((ServerPlayer)player).send(new MessageSpeedloaderSync(i, hand)); } + // set cooldown & animation timer + ItemNBTHelper.putInt(revolver, "reload", 60); + player.getCooldowns().addCooldown(this, 60); + return true; } - return new InteractionResultHolder<>(InteractionResult.SUCCESS, revolver); } - return new InteractionResultHolder<>(InteractionResult.SUCCESS, revolver); + return false; } public static float fireProjectile(Level world, LivingEntity shooter, ItemStack revolver, IBullet bullet, ItemStack bulletStack) @@ -361,11 +358,6 @@ public static float fireProjectile(Level world, LivingEntity shooter, ItemStack return noise; } - public int getShootCooldown(ItemStack stack) - { - return ItemNBTHelper.getInt(stack, "cooldown"); - } - public int getMaxShootCooldown(ItemStack stack) { if(hasUpgradeValue(stack, RevolverPerk.COOLDOWN.getNBTKey())) @@ -426,23 +418,6 @@ public void rotateCylinder(ItemStack revolver, Player player, boolean forward) rotateCylinder(revolver, player, forward, bullets); } - public boolean isEmpty(ItemStack stack, boolean allowCasing) - { - IItemHandler inv = stack.getCapability(ItemHandler.ITEM); - if(inv==null) - return true; - for(int i = 0; i < inv.getSlots(); i++) - { - ItemStack b = inv.getStackInSlot(i); - boolean isValid = true; - if(!allowCasing) - isValid = b.getItem() instanceof BulletItem; - if(!b.isEmpty()&&isValid) - return false; - } - return true; - } - /* ------------- UPGRADES & PERKS ------------- */ @Override