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

Drop items instead of deleting them #27

Open
wants to merge 6 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import io.github.coachluck.backpacksplus.BackPacksPlus;
import io.github.coachluck.backpacksplus.api.BackPackUtil;
import io.github.coachluck.backpacksplus.utils.lang.MessageKey;
import java.util.LinkedList;
import java.util.List;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
Expand Down Expand Up @@ -96,11 +100,15 @@ public void run() {

timer++;

PlayerInventory inventory = player.getInventory();
ItemStack[] items = player.getInventory().getContents();
List<ItemStack> toDrop = new LinkedList<>();

int count = 0;
int removedCount = 0;
for (ItemStack itemStack : inventory.getContents()) {

for (int i = 0; i < items.length; ++i) {
ItemStack itemStack = items[i];

if (itemStack == null)
continue;
ItemMeta meta = itemStack.getItemMeta();
Expand All @@ -110,18 +118,18 @@ public void run() {
int prevCount = count;
count = count + itemStack.getAmount();
if (count > limit) {
int amountToKeep = limit - prevCount;
int amountToKeep = Math.max(0, limit - prevCount);
int difference = itemStack.getAmount() - amountToKeep;
removedCount = removedCount + difference;

if(difference > 0) {
if (difference > 0) {
// Drop item first
itemStack.setAmount(difference);
player.getInventory().remove(itemStack);
}

if(amountToKeep > 0) {
toDrop.add(itemStack.clone());

// Then delete it
itemStack.setAmount(amountToKeep);
player.getInventory().addItem(itemStack);
player.getInventory().setItem(i, itemStack);
}
}
}
Expand All @@ -130,6 +138,14 @@ public void run() {
return;
}

Location location = player.getLocation();
World world = player.getWorld();

Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin,
() -> toDrop.forEach(dropStack -> world
.dropItem(location, dropStack, item -> item.setPickupDelay(40)))
);
plugin.getMessageService().sendMessage(player, MessageKey.OVER_LIMIT,
Integer.toString(removedCount), Integer.toString(limit));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ General:
BadArgs: "&cFalsche Argumente! &eschreib &7/bpp help &efür hilfe."
Console: "&8[&cBack&ePacks&b+&8] &cSie müssen ein Spieler sein, um diesen Befehl verwenden zu können!"
NotFound: "&e%backpack% &cexistiert nicht."
OverLimit: "&7wurden entfernt &c%removed% Rucksäcke aus deinem Inventar &7weil du nur
OverLimit: "&c%removed% Rucksäcke wurden auf den Boden geworfen &7weil du nur
&e%limit% &7gleichzeitig tragen darfst."
BackPack:
ItemNotAllowed: "%backpack% &cSie können &e%item% &cnicht darin speichern."
Expand All @@ -33,4 +33,4 @@ Help:
- " &8- &7Gibt dem Spieler den angegebenen Rucksack"
- " &e/bpp &areload &8- &7Ladet das plugin neu."
- "&7&m "
- ""
- ""
4 changes: 2 additions & 2 deletions src/main/resources/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ General:
BadArgs: "&cIncorrect arguments! &eType &7/bpp help &efor help."
Console: "&8[&cBack&ePacks&b+&8] &cYou must be a player to use this command!"
NotFound: "&e%backpack% &cdoes not exist."
OverLimit: "&7Removed &c%removed% backpacks &7because you are only allowed to carry &e%limit% &7at once."
OverLimit: "&7Dropped &c%removed% backpacks &7because you are only allowed to carry &e%limit% &7at once."
BackPack:
ItemNotAllowed: "%backpack% &cdoes not let you store &e%item% &cinside."
OnCraft:
Expand All @@ -33,4 +33,4 @@ Help:
- " &8- &7Give the player the specified backpack"
- " &e/bpp &areload &8- &7Reloads the plugin."
- "&7&m "
- ""
- ""