-
Notifications
You must be signed in to change notification settings - Fork 9
[ref:vaan/feature/procedural-crafting] Procedural Crafting #476
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
Open
Intybyte
wants to merge
47
commits into
master
Choose a base branch
from
vaan/feature/procedural-crafting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
b5dd101
Make AssemblyTypeRecipe
Intybyte d8ec92d
Fix AssemblyTableRecipe
Intybyte 480920b
Add AssemblyTable
Intybyte eae013e
Fixes and crafting support
Intybyte 1cbc86c
Doesn't work... but made some display entity handling
Intybyte e817237
Make entity work
Intybyte 57e9d9f
Add item damage support
Intybyte d9e95fd
Regionize methods
Intybyte 554c324
Add mirror support
Intybyte 9513735
Add method for entity, still doesn't fix it though
Intybyte 8e1809e
Rename to removeAllEntities
Intybyte 899f830
Extract messages
Intybyte edf5758
Only allow 1 to not remove unnecessary items
Intybyte 975437d
Use pylon serializers
Intybyte b38358c
Forgot this
Intybyte 8632365
Cancel breaking
Intybyte a948f1b
Create and use new serializers
Intybyte fb77396
Use normal background
Intybyte c85e357
Blueprint workbench
Intybyte 517dde7
Move classes to new package
Intybyte cecc5fd
Moving to post load did nothing
Intybyte 629cef3
Added proper working displays
Intybyte f1ec3e7
Allow more items and update on output
Intybyte b61d790
Finalize Step.StateDisplay
Intybyte b95c280
No need to update in this case
Intybyte 9953539
empty item is valid? fix
Intybyte e492950
Billboard
Intybyte 2aa2c5e
Extract stuff for future assembly table
Intybyte 35e1cd6
Remove unused
Intybyte 5cf34f5
Solved issue, remove todo
Intybyte 20ce610
Extract procedural crafting section
Intybyte 2283c2f
Add getter annotation
Intybyte 2e37851
More generic recipe
Intybyte 434149c
Fix recipe matching in some edge cases
Intybyte 07cb7c9
Remove unnecessary messages
Intybyte ad86f00
Nuke messages altogether
Intybyte 8ed29f6
Extract more stuff
Intybyte 2ef2b41
Add preserve offset
Intybyte 047fada
PitKiln? really man?
Intybyte 481347c
Extract getItemStep to holder
Intybyte 4224844
Make AssemblyTable
Intybyte 80b504f
Rename methods
Intybyte 8f310ed
Add 4th entity on both mirrors
Intybyte bb262d0
Better name
Intybyte 27f3d4c
Rename field to craftingInventory
Intybyte cd3aaf4
Merge branch 'master' into vaan/feature/procedural-crafting
Intybyte 3334a61
Update en.yml
Intybyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
src/main/java/io/github/pylonmc/pylon/base/content/machines/simple/AssemblyTable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| package io.github.pylonmc.pylon.base.content.machines.simple; | ||
|
|
||
| import io.github.pylonmc.pylon.base.recipes.AssemblyTableRecipe; | ||
| import io.github.pylonmc.pylon.base.recipes.intermediate.Step; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonBreakHandler; | ||
| import io.github.pylonmc.pylon.core.block.base.PylonInteractBlock; | ||
| import io.github.pylonmc.pylon.core.block.context.BlockBreakContext; | ||
| import io.github.pylonmc.pylon.core.block.context.BlockCreateContext; | ||
| import io.github.pylonmc.pylon.core.util.gui.GuiItems; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.block.BlockFace; | ||
| import org.bukkit.entity.Item; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.player.PlayerInteractEvent; | ||
| import org.bukkit.inventory.EquipmentSlot; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.persistence.PersistentDataContainer; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import xyz.xenondevs.inventoryaccess.component.AdventureComponentWrapper; | ||
| import xyz.xenondevs.invui.gui.Gui; | ||
| import xyz.xenondevs.invui.inventory.VirtualInventory; | ||
| import xyz.xenondevs.invui.inventory.event.ItemPostUpdateEvent; | ||
| import xyz.xenondevs.invui.inventory.event.ItemPreUpdateEvent; | ||
| import xyz.xenondevs.invui.inventory.event.UpdateReason; | ||
| import xyz.xenondevs.invui.window.Window; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| public class AssemblyTable extends ProceduralCraftingTable<AssemblyTableRecipe> implements PylonBreakHandler, PylonInteractBlock { | ||
| private final VirtualInventory stepDisplay = new VirtualInventory(1); | ||
|
|
||
| private int offset = 1; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public AssemblyTable(@NotNull Block block, @NotNull BlockCreateContext context) { | ||
| super(block, context); | ||
|
|
||
| this.craftingInventory.setPostUpdateHandler(this::updateInputGrid); | ||
| this.stepDisplay.setPreUpdateHandler(this::cancelEverything); | ||
|
|
||
| updateStep(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public AssemblyTable(@NotNull Block block, @NotNull PersistentDataContainer pdc) { | ||
| super(block, pdc); | ||
|
|
||
| this.craftingInventory.setPostUpdateHandler(this::updateInputGrid); | ||
| this.stepDisplay.setPreUpdateHandler(this::cancelEverything); | ||
| } | ||
|
|
||
| @Override | ||
| protected AssemblyTableRecipe deserializeRecipeKey(NamespacedKey key) { | ||
| return AssemblyTableRecipe.RECIPE_TYPE.getRecipe(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateStep() { | ||
| updateStepItem(); | ||
| updateRecipeEntities(); | ||
|
|
||
| if (currentRecipe != null) { | ||
| this.currentStateDisplay.update(getStep(), currentProgress); | ||
| } else { | ||
| this.currentStateDisplay.setVisibility(false); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void completeRecipe() { | ||
| Location above = getBlock().getRelative(BlockFace.UP).getLocation().toCenterLocation(); | ||
| currentRecipe.results().forEach(stack -> | ||
| above.getWorld().spawn(above, Item.class, (item) -> item.setItemStack(stack)) | ||
| ); | ||
|
|
||
| List<ItemStack> requiredItems = new ArrayList<>(currentRecipe.ingredients()); | ||
| ItemStack[] unsafeItems = craftingInventory.getUnsafeItems(); | ||
| for (int i = 0; i < unsafeItems.length; i++) { | ||
| ItemStack item = unsafeItems[i]; | ||
| if (item == null) continue; | ||
|
|
||
| Iterator<ItemStack> iterator = requiredItems.iterator(); | ||
| while (iterator.hasNext()) { | ||
| ItemStack requirement = iterator.next(); | ||
|
|
||
| if (requirement.getAmount() > item.getAmount()) continue; | ||
| if (!requirement.isSimilar(item)) continue; | ||
|
|
||
| item.subtract(requirement.getAmount()); | ||
| if (item.getAmount() == 0) { | ||
| craftingInventory.setItem(UpdateReason.SUPPRESSED, i, null); | ||
| } | ||
|
|
||
| iterator.remove(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| this.currentRecipe = null; | ||
| this.currentProgress = null; | ||
|
|
||
| updateInputGrid(true); | ||
| removeAddedEntities(); | ||
| } | ||
|
|
||
| private void updateStepItem() { | ||
| ItemStack stack; | ||
| if (currentRecipe == null || currentProgress == null) { | ||
| stack = GuiItems.background().getItemProvider().get(); | ||
| } else { | ||
| Step current = this.getStep(); | ||
| stack = current.asStack(current.uses() - currentProgress.getUsedAmount()); | ||
| } | ||
|
|
||
| this.stepDisplay.setItem(UpdateReason.SUPPRESSED, 0, stack); | ||
| } | ||
|
|
||
| private void cancelEverything(@NotNull ItemPreUpdateEvent event) { | ||
| UpdateReason reason = event.getUpdateReason(); | ||
| if (reason == null || reason.equals(UpdateReason.SUPPRESSED)) return; | ||
|
|
||
| event.setCancelled(true); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBreak(@NotNull List<@NotNull ItemStack> drops, @NotNull BlockBreakContext context) { | ||
| for (ItemStack item : craftingInventory.getItems()) { | ||
| if (item != null) { | ||
| drops.add(item); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public @NotNull Gui createGui() { | ||
| var builder = Gui.normal() | ||
| .setStructure( | ||
| "# # # # # # # # #", | ||
| "# # # x x x # # #", | ||
| "# # # x x x # $ #", | ||
| "# # # x x x # # #", | ||
| "# # # # # # # # #" | ||
| ) | ||
| .addIngredient('x', craftingInventory) | ||
| .addIngredient('$', stepDisplay) | ||
| .addIngredient('#', GuiItems.background()); | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| public void handleGui(PlayerInteractEvent event) { | ||
| if (event.getPlayer().isSneaking() | ||
| || event.useInteractedBlock() == Event.Result.DENY) { | ||
| return; | ||
| } | ||
|
|
||
| event.setUseInteractedBlock(Event.Result.DENY); | ||
| event.setUseItemInHand(Event.Result.DENY); | ||
|
|
||
| Window.single() | ||
| .setGui(createGui()) | ||
| .setTitle(new AdventureComponentWrapper(getNameTranslationKey())) | ||
| .setViewer(event.getPlayer()) | ||
| .build() | ||
| .open(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onInteract(@NotNull PlayerInteractEvent event) { | ||
| if (event.getHand() != EquipmentSlot.HAND) return; | ||
|
|
||
| if (event.getAction().isRightClick()) { | ||
| handleGui(event); | ||
| return; | ||
| } | ||
|
|
||
| Player player = event.getPlayer(); | ||
| ItemStack mainHand = player.getInventory().getItemInMainHand(); | ||
| if (mainHand.isEmpty()) { | ||
| offset++; // left click changes selected recipe, if any | ||
| updateInputGrid(true); | ||
| } else { | ||
| if (currentRecipe == null || currentProgress == null) return; | ||
| boolean outcome = progressRecipe(mainHand, player, true); | ||
| if (outcome) { | ||
| event.setCancelled(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void updateInputGrid(@NotNull ItemPostUpdateEvent event) { | ||
| updateInputGrid(false); | ||
| } | ||
|
|
||
| public void updateInputGrid(boolean preserveOffset) { | ||
| if (!preserveOffset) { | ||
| this.offset = 1; | ||
| } | ||
|
|
||
| this.currentRecipe = AssemblyTableRecipe.findRecipe(craftingInventory.getItems(), offset); | ||
| if (this.currentRecipe == null) { | ||
| this.currentProgress = null; | ||
| } else { | ||
| this.currentProgress = new Step.ActionStep(0, 0); | ||
| } | ||
|
|
||
| updateStep(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
...why do you have a custom gui handler?