-
-
Notifications
You must be signed in to change notification settings - Fork 20
GH-865 Fix Illegal Stack and no space in /give command.
#865
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae4c931
Fix Illegal Stack and no space in `/give` command.
vLuckyyy 032dad7
Fix style violations reported by Checkstyle.
vLuckyyy c4d40cf
Don't use directly field's from EnchantmentLevelPair record.
vLuckyyy daa5dbc
Fix `Material#isItem` usage.
vLuckyyy f724ebd
Follow @Rollczi feedback.
vLuckyyy 3944b70
Fix give processing
Rollczi db7f436
Support offhand
Rollczi 8cc81b3
Remove InventoryUtil
Rollczi 9940ccc
Cleanup messages.
Rollczi 1053ac4
Merge remote-tracking branch 'origin/master' into fix-illegalstack-gi…
Rollczi 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
135 changes: 135 additions & 0 deletions
135
...ore-core/src/main/java/com/eternalcode/core/feature/essentials/item/give/GiveService.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,135 @@ | ||
| package com.eternalcode.core.feature.essentials.item.give; | ||
|
|
||
| import com.eternalcode.core.configuration.implementation.PluginConfiguration; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Service; | ||
| import com.eternalcode.core.notice.NoticeService; | ||
| import java.util.Optional; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.PlayerInventory; | ||
|
|
||
| @Service | ||
| class GiveService { | ||
|
|
||
| private final PluginConfiguration pluginConfiguration; | ||
| private final NoticeService noticeService; | ||
|
|
||
| @Inject | ||
| public GiveService(PluginConfiguration pluginConfiguration, NoticeService noticeService) { | ||
| this.pluginConfiguration = pluginConfiguration; | ||
| this.noticeService = noticeService; | ||
| } | ||
|
|
||
| public boolean giveItem(CommandSender sender, Player player, Material material, int amount) { | ||
| if (this.isInvalidMaterial(material)) { | ||
| this.noticeService.create() | ||
| .notice(translation -> translation.item().giveNotItem()) | ||
| .sender(sender) | ||
| .send(); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| PlayerInventory inventory = player.getInventory(); | ||
| GiveResult giveResult = this.processGive(new PlayerContents(inventory.getStorageContents(), inventory.getItemInOffHand()), new ItemStack(material, amount)); | ||
| Optional<ItemStack> rest = giveResult.rest(); | ||
|
|
||
| if (rest.isPresent() && !this.pluginConfiguration.items.dropOnFullInventory) { | ||
| this.noticeService.create() | ||
| .notice(translation -> translation.item().giveNoSpace()) | ||
| .sender(sender) | ||
| .send(); | ||
| return false; | ||
| } | ||
|
|
||
| inventory.setStorageContents(giveResult.contents().storage); | ||
| inventory.setItemInOffHand(giveResult.contents().extraSlot); | ||
|
|
||
| if (rest.isPresent()) { | ||
| player.getWorld().dropItemNaturally(player.getLocation(), rest.get()); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private boolean isInvalidMaterial(Material material) { | ||
| return !material.isItem(); | ||
| } | ||
|
|
||
| private GiveResult processGive(PlayerContents contents, ItemStack itemToGive) { | ||
| for (int i = 0; i < contents.size(); i++) { | ||
| if (itemToGive.getAmount() < 0) { | ||
| throw new IllegalArgumentException("Item amount cannot be negative"); | ||
| } | ||
|
|
||
| if (itemToGive.getAmount() == 0) { | ||
| return new GiveResult(contents, Optional.empty()); | ||
| } | ||
|
|
||
| ItemStack content = contents.get(i); | ||
|
|
||
| if (content == null || content.getType().isAir()) { | ||
| contents.set(i, processContentSlot(itemToGive, 0, itemToGive.clone())); | ||
| continue; | ||
| } | ||
|
|
||
| if (!content.isSimilar(itemToGive)) { | ||
| continue; | ||
| } | ||
|
|
||
| contents.set(i, processContentSlot(itemToGive, content.getAmount(), content.clone())); | ||
| } | ||
|
|
||
| if (itemToGive.getAmount() > 0) { | ||
| return new GiveResult(contents, Optional.of(itemToGive)); | ||
| } | ||
|
|
||
| return new GiveResult(contents, Optional.empty()); | ||
| } | ||
|
|
||
| private static ItemStack processContentSlot(ItemStack itemToGive, int amount, ItemStack cloned) { | ||
| int amountToConsume = Math.min(itemToGive.getAmount(), itemToGive.getMaxStackSize() - amount); | ||
|
|
||
| cloned.setAmount(amount + amountToConsume); | ||
| itemToGive.setAmount(itemToGive.getAmount() - amountToConsume); | ||
|
|
||
| return cloned; | ||
| } | ||
|
|
||
| private record GiveResult(PlayerContents contents, Optional<ItemStack> rest) {} | ||
|
|
||
| private static class PlayerContents { | ||
| private final ItemStack[] storage; | ||
| private ItemStack extraSlot; | ||
|
|
||
| private PlayerContents(ItemStack[] storage, ItemStack extraSlot) { | ||
| this.storage = storage; | ||
| this.extraSlot = extraSlot; | ||
| } | ||
|
|
||
| int size() { | ||
| return this.storage.length + 1; | ||
| } | ||
|
|
||
| ItemStack get(int index) { | ||
| if (index == this.size() - 1) { | ||
| return this.extraSlot; | ||
| } | ||
|
|
||
| return this.storage[index]; | ||
| } | ||
|
|
||
| void set(int index, ItemStack item) { | ||
| if (index == this.size() - 1) { | ||
| this.extraSlot = item; | ||
| return; | ||
| } | ||
|
|
||
| this.storage[index] = item; | ||
| } | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.