diff --git a/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java b/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java index e3dd6dfb7..3190f898d 100644 --- a/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java +++ b/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java @@ -273,9 +273,15 @@ public void setStateManager(StateManager new CompoundPermit(s), modContext.getPlayerItemInstanceRegistry()::update, permitManager) .manual() + .in(this) + .change(WeaponState.COMPOUND_RELOAD_FINISH).to(WeaponState.PAUSED) + .when(weaponInstance -> WeaponAttachmentAspect.getActiveAttachment(AttachmentCategory.MAGAZINE, weaponInstance) == null) + .automatic() + .in(this) .change(WeaponState.COMPOUND_RELOAD_FINISHED).to(WeaponState.READY) .when(shouldFinishCompoundReload) @@ -402,43 +408,38 @@ public void clientCompoundReload(PlayerWeaponInstance instance) { public void reloadMainHeldItem(EntityPlayer player) { PlayerWeaponInstance instance = modContext.getPlayerItemInstanceRegistry().getMainHandItemInstance(player, PlayerWeaponInstance.class); + if (instance == null) + return; - if (instance != null) { - if (AnimationModeProcessor.getInstance().isLegacyMode()) { - furtherLoadInstructionsReceived(instance); - stateManager.changeState(this, instance, WeaponState.READY); + if (AnimationModeProcessor.getInstance().isLegacyMode()) { + furtherLoadInstructionsReceived(instance); + stateManager.changeState(this, instance, WeaponState.READY); + return; + } + + if (WeaponAttachmentAspect.getActiveAttachment(AttachmentCategory.MAGAZINE, instance) == null) { + ItemStack nextAttachment = getNextBestMagazineStack(instance); + if (instance.getWeapon().getRenderer().getBuilder().isHasLoadEmpty() && nextAttachment != null && Tags.getAmmo(nextAttachment) == 0) + instance.getWeapon().getRenderer().setShouldDoEmptyVariant(true); + + stateManager.changeState(this, instance, WeaponState.AWAIT_FURTHER_LOAD_INSTRUCTIONS, WeaponState.READY); + } else { + if (instance.getState() != WeaponState.READY && instance.getState() != WeaponState.COMPOUND_REQUESTED) + return; + + ItemAttachment nextAttachment = getNextMagazine(instance); + if (nextAttachment == null) + return; + + instance.markReloadDirt(); + instance.markMagSwapReady(); + if (instance.getAmmo() == 0) { + instance.getWeapon().getRenderer().setMagicMag(instance, nextAttachment, WeaponState.COMPOUND_RELOAD_EMPTY); + stateManager.changeState(this, instance, WeaponState.COMPOUND_RELOAD_EMPTY); } else { - if (WeaponAttachmentAspect.getActiveAttachment(AttachmentCategory.MAGAZINE, instance) == null) { - ItemStack nextAttachment = getNextBestMagazineStack(instance); - if (instance.getWeapon().getRenderer().getBuilder().isHasLoadEmpty() && nextAttachment != null && Tags.getAmmo(nextAttachment) == 0) - instance.getWeapon().getRenderer().setShouldDoEmptyVariant(true); - - stateManager.changeState(this, instance, WeaponState.AWAIT_FURTHER_LOAD_INSTRUCTIONS, WeaponState.READY); - } else { - if (instance.getState() != WeaponState.READY && instance.getState() != WeaponState.COMPOUND_REQUESTED) - return; - - ItemAttachment nextAttachment = getNextMagazine(instance); - - instance.markReloadDirt(); - instance.markMagSwapReady(); - if (instance.getAmmo() == 0) { - if (nextAttachment != null) - instance.getWeapon().getRenderer().setMagicMag(instance, nextAttachment, WeaponState.COMPOUND_RELOAD_EMPTY); - - else - return; - - stateManager.changeState(this, instance, WeaponState.COMPOUND_RELOAD_EMPTY); - } else { - if (nextAttachment != null) - instance.getWeapon().getRenderer().setMagicMag(instance, nextAttachment, WeaponState.COMPOUND_RELOAD); - else - return; - instance.setIsAwaitingCompoundInstructions(true); - stateManager.changeState(this, instance, WeaponState.COMPOUND_REQUESTED, WeaponState.READY); - } - } + instance.getWeapon().getRenderer().setMagicMag(instance, nextAttachment, WeaponState.COMPOUND_RELOAD); + instance.setIsAwaitingCompoundInstructions(true); + stateManager.changeState(this, instance, WeaponState.COMPOUND_REQUESTED, WeaponState.READY); } } } @@ -592,40 +593,35 @@ private void processActualCompoundPermit(CompoundPermit p, PlayerWeaponInstance Weapon weapon = instance.getWeapon(); List compatibleMagazines = weapon.getCompatibleMagazines().stream().filter(compatibleMagazine -> WeaponAttachmentAspect.hasRequiredAttachments(compatibleMagazine, instance)).collect(Collectors.toList()); - - // Unload weapon - - ItemAttachment attachment = modContext.getAttachmentAspect().removeAttachment(AttachmentCategory.MAGAZINE, instance); - - // processUnloadPermit(new UnloadPermit(p.getState()), instance); - - int originalAmmo = instance.getAmmo(); - - // Mag list is empty - if (compatibleMagazines.isEmpty()) return; - ItemStack magazineStack = MWCUtil.consumeItemsFromPlayerInventory(compatibleMagazines, (stack1, stack2) -> Integer.compare(Tags.getAmmo(stack1), Tags.getAmmo(stack2)), player); - - if (magazineStack == null) + previousMagazine = modContext.getAttachmentAspect().removeAttachment(AttachmentCategory.MAGAZINE, instance); + if (previousMagazine == null) { + p.setStatus(Status.DENIED); return; - - //ItemStack magazineStack = ItemStack.EMPTY; - int ammo = Tags.getAmmo(magazineStack); - Tags.setAmmo(weaponItemStack, ammo); - WeaponAttachmentAspect.addAttachment((ItemAttachment) magazineStack.getItem(), instance); - instance.setAmmo(ammo); + } - p.setStatus(Status.GRANTED); + // processUnloadPermit(new UnloadPermit(p.getState()), instance); + int originalAmmo = instance.getAmmo(); - if (attachment == null) - p.setStatus(Status.DENIED); - else if (attachment instanceof ItemMagazine && !player.isCreative()) { - ItemStack attachmentItemStack = ((ItemMagazine) attachment).create(originalAmmo); + ItemStack magazineStack = MWCUtil.consumeItemsFromPlayerInventory(compatibleMagazines, Comparator.comparingInt(Tags::getAmmo), player); + if (magazineStack != null) { + //ItemStack magazineStack = ItemStack.EMPTY; + int ammo = Tags.getAmmo(magazineStack); + Tags.setAmmo(weaponItemStack, ammo); + instance.setAmmo(ammo); + WeaponAttachmentAspect.addAttachment((ItemAttachment) magazineStack.getItem(), instance); + p.setStatus(Status.GRANTED); + } else { + Tags.setAmmo(weaponItemStack, 0); + instance.setAmmo(0); + } + + if (previousMagazine instanceof ItemMagazine && !player.isCreative()) { + ItemStack attachmentItemStack = ((ItemMagazine) previousMagazine).create(originalAmmo); if (!player.inventory.addItemStackToInventory(attachmentItemStack)) player.dropItem(attachmentItemStack, false); - p.setStatus(Status.GRANTED); } } diff --git a/src/main/java/com/paneedah/weaponlib/WeaponRenderer.java b/src/main/java/com/paneedah/weaponlib/WeaponRenderer.java index c4a501817..1f50c3e7c 100644 --- a/src/main/java/com/paneedah/weaponlib/WeaponRenderer.java +++ b/src/main/java/com/paneedah/weaponlib/WeaponRenderer.java @@ -2327,7 +2327,7 @@ protected StateDescriptor getFirstPersonStateDescriptor(EntityLivingBase player, rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().zoomRandomizingAmplitude; } else { - currentState = RenderableState.NORMAL; + currentState = RenderableState.RECOILED; rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().firingRandomizingAmplitude; } @@ -2354,7 +2354,7 @@ protected StateDescriptor getFirstPersonStateDescriptor(EntityLivingBase player, } amplitude = getBuilder().zoomRandomizingAmplitude; } else { - currentState = RenderableState.NORMAL; + currentState = RenderableState.SHOOTING; if(!isLongPaused) { rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().firingRandomizingAmplitude; @@ -2533,7 +2533,7 @@ protected StateDescriptor getThirdPersonStateDescriptor(EntityLivingBase player, rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().zoomRandomizingAmplitude; } else { - currentState = RenderableState.NORMAL; + currentState = RenderableState.RECOILED; rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().firingRandomizingAmplitude; } @@ -2559,7 +2559,7 @@ protected StateDescriptor getThirdPersonStateDescriptor(EntityLivingBase player, } amplitude = getBuilder().zoomRandomizingAmplitude; } else { - currentState = RenderableState.NORMAL; + currentState = RenderableState.SHOOTING; if(!isLongPaused) { rate = getBuilder().firingRandomizingRate; amplitude = getBuilder().firingRandomizingAmplitude;