First iteration of saving the inventory of rental spaces. The code is in
place to pop inventory holders, however commented out while I work with whether to include pre-existing chests and the like, or to refresh them with the rest of the rental space.
This commit is contained in:
parent
a18a88616c
commit
fefe9424b7
0
resources/Abandoned_inventories/.includeme
Normal file
0
resources/Abandoned_inventories/.includeme
Normal file
171
src/me/EtienneDx/RealEstate/AbandonedItems.java
Normal file
171
src/me/EtienneDx/RealEstate/AbandonedItems.java
Normal file
@ -0,0 +1,171 @@
|
||||
package me.EtienneDx.RealEstate;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
|
||||
|
||||
public class AbandonedItems extends YamlConfiguration
|
||||
|
||||
{
|
||||
|
||||
public static List<ItemStack>Saved_Items;
|
||||
public static String owner;
|
||||
private File file;
|
||||
//add items
|
||||
//save items
|
||||
//load items
|
||||
//expunge items
|
||||
public AbandonedItems(String item_owner)
|
||||
{
|
||||
if(!RealEstate.instance.config.SaveInventory) {
|
||||
RealEstate.instance.log.info("Item saving is disabled, but an attempt to call the storage class was made, whoops");
|
||||
return;
|
||||
|
||||
}
|
||||
owner=item_owner;
|
||||
Saved_Items=new ArrayList<ItemStack>();
|
||||
String path= RealEstate.pluginDirPath + "/Abandoned_inventories/"+item_owner+".inventory";
|
||||
file=new File(path);
|
||||
if(file.exists())//user has stuff already saved, load it
|
||||
{
|
||||
load();
|
||||
}
|
||||
}
|
||||
public String getOwner()
|
||||
{
|
||||
return owner;
|
||||
|
||||
}
|
||||
public void add_item(ItemStack addme)
|
||||
{
|
||||
int remaining=addme.getAmount();
|
||||
RealEstate.instance.log.info("Attempting to add: "+addme.getType()+" of stack amount "+addme.getAmount());
|
||||
for(ItemStack a : Saved_Items)
|
||||
{
|
||||
|
||||
if(a.getType()==addme.getType())
|
||||
{
|
||||
RealEstate.instance.log.info("Found an existing stack, combining: "+a.getType()+" With existing size of "+a.getAmount());
|
||||
int cursize=a.getAmount();
|
||||
int maxamount=a.getMaxStackSize();
|
||||
if(cursize<maxamount) //room to add more
|
||||
{
|
||||
RealEstate.instance.log.info("We went over the limit, adding what we can");
|
||||
cursize+=addme.getAmount();
|
||||
cursize=cursize>maxamount? maxamount:cursize;
|
||||
remaining -= maxamount-a.getAmount();
|
||||
a.setAmount(cursize);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
if(remaining >0)//there is atleast one more to put in
|
||||
{
|
||||
RealEstate.instance.log.info("Remaining items: "+remaining+" adding remaining stack to saved items");
|
||||
addme.setAmount(remaining);//just in case we removed some earlier
|
||||
Saved_Items.add(addme);
|
||||
remaining=0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if(Saved_Items.size()==0)//first item being put in, heh
|
||||
{
|
||||
RealEstate.instance.log.info("Adding first item to the save queue");
|
||||
Saved_Items.add(addme);
|
||||
}
|
||||
this.save();
|
||||
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public void load()
|
||||
{
|
||||
|
||||
YamlConfiguration c = YamlConfiguration.loadConfiguration(file);
|
||||
try {
|
||||
Saved_Items=(List<ItemStack>) c.get("inventory");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
//so the file either didn't exist when we tried to load it, or was malformed in some way.
|
||||
|
||||
}
|
||||
//Saved_Items = (List<ItemStack>) c.get("inventory");
|
||||
|
||||
//p.getInventory().setContents(content);
|
||||
//Saved_Items=content;
|
||||
|
||||
|
||||
|
||||
}
|
||||
public void delete_save()
|
||||
{
|
||||
|
||||
file.delete();
|
||||
}
|
||||
public void save()
|
||||
{
|
||||
|
||||
|
||||
YamlConfiguration c = YamlConfiguration.loadConfiguration(file);
|
||||
c.set("inventory", Saved_Items);
|
||||
try {
|
||||
c.save(file);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
//e.printStackTrace();
|
||||
RealEstate.instance.log.info("Failed to save, got exception: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
public List<ItemStack> get_items()
|
||||
{
|
||||
|
||||
return Saved_Items;
|
||||
}
|
||||
public static void purge_items(Player player)
|
||||
{
|
||||
|
||||
|
||||
AbandonedItems abandoned=new AbandonedItems(player.getUniqueId().toString());
|
||||
|
||||
//lets find if they have items to get
|
||||
|
||||
List<ItemStack> recovered_items=abandoned.get_items();
|
||||
if(recovered_items.size()>0) {
|
||||
|
||||
|
||||
|
||||
Location where=player.getLocation();
|
||||
for(ItemStack a: recovered_items)
|
||||
{
|
||||
|
||||
where.getWorld().dropItemNaturally(where, a);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Messages.sendMessage(player, RealEstate.instance.messages.msgSaveHasNoItems);
|
||||
}
|
||||
abandoned.delete_save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -107,6 +107,8 @@ public class Config extends AnnotationConfig
|
||||
@ConfigField(name="RealEstate.Settings.RestoreAdminClaimsOnly",comment="This setting will let us know if the rental areas should only be restored via WorldEdit if it is an admin claim, if true we will restore only admin claims, false will restore all claims (provided it is enabled by the previous setting)")
|
||||
public boolean RestoreAdminOnly=true;
|
||||
|
||||
@ConfigField(name="RealEstate.Settings.SaveInventory",comment="This setting lets me know whether rental properties should attempt to save inventories when they are reverted to original state")
|
||||
public boolean SaveInventory=true;
|
||||
|
||||
public Config()
|
||||
{
|
||||
|
||||
@ -426,7 +426,15 @@ public class Messages extends AnnotationConfig
|
||||
@ConfigField(name="RealEstate.List.NextPage", comment="0: all|sell|rent|lease; 1: next page number")
|
||||
public String msgListNextPage = "$6To see the next page, type $a/re list {0} {1}";
|
||||
|
||||
@ConfigField(name="RealEstate.Info.Claim.Purge.Availabe",comment="0: player name, there are items ready for you to claim")
|
||||
public String msgItemsToClaim="$b There are items ready for you to claim use /re itemclam";
|
||||
|
||||
@ConfigField(name="RealEstate.Info.Item.Saving.Disabled",comment="Saving items in expired rental claims is disabled.")
|
||||
public String msgErrorSavingDisabled="$b Item Saving for rental properties is disabled";
|
||||
|
||||
@ConfigField(name="RealEstate.Info.Item.Saving.Empty",comment="all items have been claimed")
|
||||
public String msgSaveHasNoItems="$b There were no items left in the save file!";
|
||||
|
||||
public Messages()
|
||||
{
|
||||
this.pdf = RealEstate.instance.getDescription();
|
||||
|
||||
@ -395,6 +395,16 @@ public class RECommand extends BaseCommand
|
||||
}
|
||||
}
|
||||
}
|
||||
@Subcommand("purge")
|
||||
@CommandPermission("realestate.admin")
|
||||
public static void perge(Player player)
|
||||
{
|
||||
if(RealEstate.instance.config.SaveInventory) {
|
||||
AbandonedItems.purge_items(player);
|
||||
}
|
||||
else
|
||||
Messages.sendMessage(player, RealEstate.instance.messages.msgErrorSavingDisabled);
|
||||
}
|
||||
|
||||
@Subcommand("cancel")
|
||||
@Conditions("claimHasTransaction")
|
||||
|
||||
@ -12,8 +12,10 @@ import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -55,6 +57,7 @@ public class RealEstate extends JavaPlugin
|
||||
public static RealEstate instance = null;
|
||||
|
||||
public static TransactionsStore transactionsStore = null;
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void onEnable()
|
||||
|
||||
@ -3,23 +3,29 @@ package me.EtienneDx.RealEstate.Transactions;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.earth2me.essentials.User;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
@ -34,18 +40,15 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
||||
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
|
||||
import com.sk89q.worldedit.function.mask.BlockTypeMask;
|
||||
import com.sk89q.worldedit.function.mask.Mask;
|
||||
import com.sk89q.worldedit.function.mask.Masks;
|
||||
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
|
||||
import com.sk89q.worldedit.function.operation.Operation;
|
||||
import com.sk89q.worldedit.function.operation.Operations;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.session.ClipboardHolder;
|
||||
import com.sk89q.worldedit.world.block.BlockCategory;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockType;
|
||||
|
||||
import me.EtienneDx.RealEstate.AbandonedItems;
|
||||
import me.EtienneDx.RealEstate.Messages;
|
||||
import me.EtienneDx.RealEstate.RealEstate;
|
||||
import me.EtienneDx.RealEstate.Utils;
|
||||
@ -226,7 +229,105 @@ public class ClaimRent extends BoughtTransaction
|
||||
return false;
|
||||
|
||||
}
|
||||
public void save_inventories(Claim claim,String mybuyer)
|
||||
{
|
||||
if(!RealEstate.instance.config.SaveInventory) {
|
||||
return;
|
||||
|
||||
}
|
||||
String item_owner=mybuyer;//uuid saved when we started the transaction
|
||||
//so now we need to scan through the entire claim, and find any inventory to save X.x
|
||||
//get chunks within area, get inventories, check if inventory is within the bounds, then process.
|
||||
List<Chunk> chunksToProcess=new ArrayList<Chunk>();
|
||||
Location min=claim.getLesserBoundaryCorner();
|
||||
Location max=claim.getGreaterBoundaryCorner();
|
||||
World world=min.getWorld();
|
||||
|
||||
List<Inventory> lifeboat=new ArrayList<Inventory>();
|
||||
int invsize=0;
|
||||
|
||||
for (double x=min.getX();x<max.getX();x+=16) {
|
||||
for(double z=min.getZ();z<max.getZ();z+=16)
|
||||
{
|
||||
|
||||
// chunksToProcess.add(world.getChunkAt((int)x,(int)z));
|
||||
//Chunk chunk=world.getChunkAt((int)x,(int)z);
|
||||
Location current=new Location(max.getWorld(),x,0,z);
|
||||
Chunk chunk=world.getChunkAt(current);
|
||||
chunksToProcess.add(chunk);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//now we have all the chunks involved, now to find inventories...
|
||||
AbandonedItems item_saver=new AbandonedItems(mybuyer);
|
||||
for(Chunk chunk:chunksToProcess)
|
||||
{
|
||||
for(BlockState tileEntity:chunk.getTileEntities())
|
||||
{
|
||||
if (tileEntity.getX()>min.getX()&&tileEntity.getX()<max.getX())
|
||||
{
|
||||
if (tileEntity.getZ()>min.getZ()&&tileEntity.getZ()<max.getZ())
|
||||
{
|
||||
try
|
||||
{
|
||||
if(tileEntity.getClass().getMethod("getInventory") != null)
|
||||
{
|
||||
|
||||
Method method= tileEntity.getClass().getMethod("getInventory");
|
||||
|
||||
Inventory found_inventory=(Inventory) method.invoke(tileEntity, null);
|
||||
for(ItemStack j : found_inventory.getStorageContents())
|
||||
{
|
||||
if(j !=null) {
|
||||
|
||||
item_saver.add_item(j);
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//blow away the container after we save everything
|
||||
|
||||
Location inventoryloc=tileEntity.getLocation();
|
||||
|
||||
world.setType(inventoryloc, Material.AIR);
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// TODO Auto-generated catch block
|
||||
//RealEstate.instance.log.info("");
|
||||
//nothing to do here, we know some of these are not going to have this method, we don't care.
|
||||
|
||||
RealEstate.instance.log.info("exception raised: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
item_saver.save();
|
||||
//ok, so now i have an array list of inventories, and how many stacks of items we have.... now what?
|
||||
//well bukkit needs a size that is a factor of 9, so next highest factor of 9?
|
||||
/*
|
||||
* while((invsize %9)!=0) {
|
||||
* RealEstate.instance.log.info("Inventory size was only "
|
||||
* +invsize+" not divisible by 9, incrementing"); invsize++; //increase invsize
|
||||
* until it is divisible by 9 } //move everything into one inventory Inventory
|
||||
* saved_inventory=Bukkit.createInventory(null, invsize); for (Inventory
|
||||
* passengers:lifeboat) { for(ItemStack j:passengers.getStorageContents()) {
|
||||
* RealEstate.instance.log.info("Moving item stack of "+j.getType());
|
||||
* saved_inventory.addItem(j);
|
||||
*
|
||||
* } }
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
public static void restore_rental(Claim claim)
|
||||
{
|
||||
|
||||
@ -300,8 +401,11 @@ public class ClaimRent extends BoughtTransaction
|
||||
claimType,
|
||||
location);
|
||||
}
|
||||
save_inventories(claim,buyer.toString());
|
||||
buyer = null;
|
||||
RealEstate.transactionsStore.saveData();
|
||||
|
||||
|
||||
ClaimRent.restore_rental(claim);
|
||||
update();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user