Move drop and clear logic into the backpack class

This commit is contained in:
GeorgH93 2017-02-15 19:40:03 +01:00
parent c96e7a4b59
commit 3d4c2cd2e6
4 changed files with 40 additions and 20 deletions

View File

@ -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 * 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 * it under the terms of the GNU General Public License as published by
@ -17,6 +17,7 @@
package at.pcgamingfreaks.Minepacks.Bukkit.API; package at.pcgamingfreaks.Minepacks.Bukkit.API;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
@ -77,4 +78,16 @@ public interface Backpack extends InventoryHolder
* Forces the backpack to be saved * Forces the backpack to be saved
*/ */
void save(); 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);
} }

View File

@ -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 * 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 * it under the terms of the GNU General Public License as published by
@ -21,6 +21,7 @@
import at.pcgamingfreaks.StringUtils; import at.pcgamingfreaks.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -225,4 +226,25 @@ public void save()
hasChanged = false; 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();
}
} }

View File

@ -155,8 +155,7 @@ public void onResult(Backpack backpack)
{ {
if(backpack != null) if(backpack != null)
{ {
backpack.getInventory().clear(); backpack.clear();
backpack.save();
messageBackpackCleaned.send(player); messageBackpackCleaned.send(player);
} }
else else

View File

@ -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 * 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 * it under the terms of the GNU General Public License as published by
@ -22,12 +22,9 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class DropOnDeath extends MinepacksListener public class DropOnDeath extends MinepacksListener
{ {
@ -42,24 +39,13 @@ public void onDeath(PlayerDeathEvent event)
final Player player = event.getEntity(); final Player player = event.getEntity();
if (!player.hasPermission("backpack.keepOnDeath")) if (!player.hasPermission("backpack.keepOnDeath"))
{ {
final World world = player.getWorld();
final Location location = player.getLocation(); final Location location = player.getLocation();
plugin.getBackpack(player, new Callback<Backpack>() plugin.getBackpack(player, new Callback<Backpack>()
{ {
@Override @Override
public void onResult(Backpack backpack) public void onResult(Backpack backpack)
{ {
Inventory backpackInventory = backpack.getInventory(); backpack.drop(location);
for(ItemStack i : backpackInventory.getContents())
{
if(i != null)
{
world.dropItemNaturally(location, i);
backpackInventory.remove(i);
backpack.setChanged();
}
}
backpack.save();
} }
@Override @Override