Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Weapon reload freeze #418

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 57 additions & 61 deletions src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,15 @@ public void setStateManager(StateManager<WeaponState, ? super PlayerWeaponInstan

.in(this)
.change(WeaponState.COMPOUND_RELOAD_FINISH).to(WeaponState.COMPOUND_RELOAD_FINISHED)
.when(magazineAttached)
.withPermit((s, es) -> 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)
Expand Down Expand Up @@ -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<Weapon> 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<Weapon> 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);
Koud-Wind marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -592,40 +593,35 @@ private void processActualCompoundPermit(CompoundPermit p, PlayerWeaponInstance
Weapon weapon = instance.getWeapon();

List<ItemMagazine> compatibleMagazines = weapon.getCompatibleMagazines().stream().filter(compatibleMagazine -> WeaponAttachmentAspect.hasRequiredAttachments(compatibleMagazine, instance)).collect(Collectors.toList());

// Unload weapon

ItemAttachment<Weapon> 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<Weapon>) 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<Weapon>) 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);
Koud-Wind marked this conversation as resolved.
Show resolved Hide resolved
if (!player.inventory.addItemStackToInventory(attachmentItemStack))
player.dropItem(attachmentItemStack, false);
p.setStatus(Status.GRANTED);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/paneedah/weaponlib/WeaponRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down