From 3d4c2cd2e6b1a393f99f2b4e9c8776a6da2eaaea Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Wed, 15 Feb 2017 19:40:03 +0100 Subject: [PATCH] Move drop and clear logic into the backpack class --- .../Minepacks/Bukkit/API/Backpack.java | 15 +++++++++++- .../Minepacks/Bukkit/Backpack.java | 24 ++++++++++++++++++- .../Minepacks/Bukkit/Commands/OnCommand.java | 3 +-- .../Bukkit/Listener/DropOnDeath.java | 18 ++------------ 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/API/Backpack.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/API/Backpack.java index c474127..6970cf1 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/API/Backpack.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/API/Backpack.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 GeorgH93 + * Copyright (C) 2016, 2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,7 @@ package at.pcgamingfreaks.Minepacks.Bukkit.API; +import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; @@ -77,4 +78,16 @@ public interface Backpack extends InventoryHolder * Forces the backpack to be saved */ void save(); + + /** + * Removes all items from the backpack. + */ + void clear(); + + /** + * Drops the content of the backpack to the ground on a given location. + * + * @param location The location the content of the backpack should be dropped to. + */ + void drop(Location location); } \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java index 33ee662..be6dc8c 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 GeorgH93 + * Copyright (C) 2016, 2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ import at.pcgamingfreaks.StringUtils; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -225,4 +226,25 @@ public void save() hasChanged = false; } } + + @Override + public void clear() + { + bp.clear(); + setChanged(); + save(); + } + + @Override + public void drop(Location location) + { + for(ItemStack i : bp.getContents()) + { + if(i != null) + { + location.getWorld().dropItemNaturally(location, i); + } + } + clear(); + } } \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java index 155d18b..ddc2588 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java @@ -155,8 +155,7 @@ public void onResult(Backpack backpack) { if(backpack != null) { - backpack.getInventory().clear(); - backpack.save(); + backpack.clear(); messageBackpackCleaned.send(player); } else diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/DropOnDeath.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/DropOnDeath.java index 0bd9f90..871f3ca 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/DropOnDeath.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/DropOnDeath.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 GeorgH93 + * Copyright (C) 2016, 2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,12 +22,9 @@ import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; public class DropOnDeath extends MinepacksListener { @@ -42,24 +39,13 @@ public void onDeath(PlayerDeathEvent event) final Player player = event.getEntity(); if (!player.hasPermission("backpack.keepOnDeath")) { - final World world = player.getWorld(); final Location location = player.getLocation(); plugin.getBackpack(player, new Callback() { @Override public void onResult(Backpack backpack) { - Inventory backpackInventory = backpack.getInventory(); - for(ItemStack i : backpackInventory.getContents()) - { - if(i != null) - { - world.dropItemNaturally(location, i); - backpackInventory.remove(i); - backpack.setChanged(); - } - } - backpack.save(); + backpack.drop(location); } @Override