Repository navigation
feat: cancel a vehicle build from the station menu #36
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import org.bukkit.Location; | ||
| import org.bukkit.Particle; | ||
| import org.bukkit.Sound; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.entity.TextDisplay; | ||
|
|
||
| import net.tfminecraft.vfbuilders.api.ConstructionFreezes; | ||
|
|
@@ -39,6 +40,7 @@ public class ActiveStation { | |
|
|
||
| private TextDisplay timerDisplay; | ||
| private String freezeReason; | ||
| private long buildId; | ||
|
|
||
|
|
||
| public ActiveStation(Location loc, Station stored) { | ||
|
|
@@ -97,6 +99,7 @@ public void selectBlueprint(Blueprint b, UUID constructorUuid) { | |
|
|
||
| public void selectBlueprint(Blueprint b, UUID constructorUuid, UUID constructorCharacterId) { | ||
| blueprint = b; | ||
| buildId++; | ||
| timeLeft = b.getTime(); | ||
| this.constructorUuid = constructorUuid; | ||
| this.constructorCharacterId = constructorCharacterId; | ||
|
|
@@ -199,6 +202,14 @@ public void complete() { | |
| } | ||
|
|
||
| public void cancelConstruction() { | ||
| cancelConstruction(null); | ||
| } | ||
|
|
||
| /** | ||
| * Cancels the build. The materials go to {@code refundTo} when set (any that do not fit drop at | ||
| * their feet), otherwise they drop at the station. | ||
| */ | ||
| public void cancelConstruction(Player refundTo) { | ||
| if (outputUuid != null) { | ||
| VehiclePersistence persistence = VehiclePersistence.current(); | ||
| if (persistence != null && persistence.repository().find(outputUuid).isPresent()) { | ||
|
|
@@ -215,7 +226,8 @@ public void cancelConstruction() { | |
| UUID character = constructorCharacterId; | ||
| if (!clearConstructionDurably()) return; | ||
| if (cancelled != null) { | ||
| cancelled.drop(loc.clone().add(0.5, 1, 0.5)); | ||
| if (refundTo != null) cancelled.give(refundTo); | ||
| else cancelled.drop(loc.clone().add(0.5, 1, 0.5)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve every refund stack before the build is cleared.
The no-player path in Resolve all input stacks before
|
||
| Bukkit.getPluginManager().callEvent( | ||
| new VehicleConstructionCancelEvent( | ||
| account, character, cancelled, this)); | ||
|
|
@@ -324,6 +336,29 @@ public void rebindDefinitions() { | |
| cancelConstruction(); | ||
| } | ||
|
|
||
| /** True while cancelling still refunds: time is left and no vehicle has been spawned. */ | ||
| public boolean isCancellable() { | ||
| return blueprint != null && outputUuid == null && timeLeft > 0; | ||
| } | ||
|
|
||
| /** | ||
| * True for the account that started the build, playing the character it was started as | ||
| * (any character of that account when none was recorded). | ||
| */ | ||
| public boolean isConstructor(UUID account, UUID character) { | ||
| return constructorUuid != null && constructorUuid.equals(account) | ||
| && (constructorCharacterId == null || constructorCharacterId.equals(character)); | ||
| } | ||
|
|
||
| /** Changes with every build started here, so a menu can tell its build from a later one. */ | ||
| public long getBuildId() { | ||
| return buildId; | ||
| } | ||
|
|
||
| public String getFreezeReason() { | ||
| return freezeReason; | ||
| } | ||
|
|
||
| public void setConstructorUuid(UUID constructorUuid) { | ||
| this.constructorUuid = constructorUuid; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,4 +171,21 @@ public void drop(Location loc) { | |
| loc.getWorld().dropItemNaturally(loc.clone().add(0.5, 1, 0.5), item); | ||
| } | ||
| } | ||
|
|
||
| /** Gives the inputs back to the player in normal stacks; whatever does not fit drops at their feet. */ | ||
| public void give(Player p) { | ||
| for (Map.Entry<String, Integer> entry : inputs.entrySet()) { | ||
| ItemStack item = api.getCreator().getItemFromPath(entry.getKey()); | ||
| int left = entry.getValue(); | ||
| while (left > 0) { | ||
| ItemStack stack = item.clone(); | ||
| stack.setAmount(Math.min(left, item.getMaxStackSize())); | ||
| left -= stack.getAmount(); | ||
| for (ItemStack spill : p.getInventory().addItem(stack).values()) { | ||
| p.getWorld().dropItemNaturally(p.getLocation(), spill); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep refund items that Paper cannot drop. If the player has room for 64 of 70 iron ingots and a server listener cancels the overflow Keep undelivered items in a recoverable refund record. Retry only those items, not the 64 already in the inventory. Keep the existing drop behaviour when Paper accepts the spawn.
|
||
| } | ||
| } | ||
| } | ||
| p.playSound(p, Sound.BLOCK_BARREL_CLOSE, 1f, 1.3f); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,6 @@ | |
| public enum VFBGUI { | ||
| CATEGORY, | ||
| BLUEPRINT, | ||
| PROGRESS, | ||
| CONFIRM_CANCEL, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refund the materials taken when the build started.
If a player pays 64 iron ingots and a reload changes the blueprint cost to 16, a fresh cancellation menu refunds only 16.
rebindDefinitions()replaces the blueprint used by the refund, and the saved station records the blueprint ID but not the paid inputs. The old station-drop path has the same problem. (raw.githubusercontent.com)Store the paid inputs with the active build and use that record for both refund routes. Preserve it across reloads and restarts. For an unchanged recipe, the refund should remain the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not changed in this PR. The refund comes from the current blueprint, which is how breaking the station has always worked. Storing the paid inputs needs a new field in the saved stations data, and recipes rarely change while a build is running. Noted as a follow-up.