Skip to content

Commit

Permalink
Import from ushops!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven65 committed Jul 12, 2020
1 parent 9bf96c1 commit 424d70a
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/xyz/mackan/Slabbo/Slabbo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.mackan.Slabbo.commands.SlabboCommand;
import xyz.mackan.Slabbo.commands.SlabboCommandCompletions;
import xyz.mackan.Slabbo.listeners.*;
import xyz.mackan.Slabbo.types.Shop;
import xyz.mackan.Slabbo.utils.ShopUtil;
Expand Down Expand Up @@ -75,6 +76,10 @@ private void setupCommands () {

manager.enableUnstableAPI("help");

manager.getCommandCompletions().registerCompletion("importFiles", c -> {
return SlabboCommandCompletions.getImportFiles();
});

manager.registerCommand(new SlabboCommand());
}

Expand Down
54 changes: 53 additions & 1 deletion src/main/java/xyz/mackan/Slabbo/commands/SlabboCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
import org.bukkit.entity.Player;
import xyz.mackan.Slabbo.GUI.ShopDeletionGUI;
import xyz.mackan.Slabbo.Slabbo;
import xyz.mackan.Slabbo.importers.ImportResult;
import xyz.mackan.Slabbo.importers.UShopImporter;
import xyz.mackan.Slabbo.types.Shop;
import xyz.mackan.Slabbo.utils.DataUtil;
import xyz.mackan.Slabbo.utils.ItemUtil;
import xyz.mackan.Slabbo.utils.ShopUtil;

import java.io.File;
import java.util.Set;
import java.util.UUID;

@CommandAlias("slabbo")
@Description("Base command for Slabbo")
Expand Down Expand Up @@ -65,7 +70,7 @@ public void onToggleAdmin (Player player) {
@Subcommand("destroy")
@Description("Destroys a shop")
@CommandPermission("slabbo.destroy")
public void onModifyQuantity(Player player) {
public void onDestroyShop(Player player) {
Shop lookingAtShop = getLookingAtShop(player);
if (lookingAtShop == null) {
player.sendMessage(ChatColor.RED+"That's not a shop.");
Expand All @@ -81,6 +86,53 @@ public void onModifyQuantity(Player player) {
deletionGUI.openInventory(player);
}

@Subcommand("import")
@Description("Imports shop from another plugin")
@CommandPermission("slabbo.importshops")
@CommandCompletion("ushops @importFiles")
public void onImportShops(Player player, String type, String file) {
File importFile = new File(Slabbo.getDataPath()+"/"+file);

if (!importFile.exists()) {
player.sendMessage(ChatColor.RED+"That file can't be found.");
return;
}

ImportResult result;

switch (type.toLowerCase()) {
case "ushops":
player.sendMessage("Importing shops!");
result = UShopImporter.importUShops(importFile);
break;
default:
player.sendMessage(ChatColor.RED+"That plugin can't be imported.");
return;
}

if (result == null) {
player.sendMessage(ChatColor.RED+"An error occured when importing. See the console for more details.");
return;
}

for (Shop shop : result.shops) {
UUID itemUUID = UUID.randomUUID();

ItemUtil.dropItem(shop.location, shop.item, itemUUID);

shop.droppedItemId = itemUUID;

Slabbo.shopUtil.put(shop.getLocationString(), shop);
}

DataUtil.saveShops();

player.sendMessage(
ChatColor.GREEN +
String.format("Imported %d shops. Skipped %d as shops already exists at their locations.", result.shops.size(), result.skippedShops.size())
);
}

@Subcommand("modify")
@Description("Modifies the shop")
@CommandPermission("slabbo.modify")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.mackan.Slabbo.commands;

import xyz.mackan.Slabbo.Slabbo;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SlabboCommandCompletions {

public static List<String> getImportFiles () {
File folder = new File(Slabbo.getDataPath());

return Arrays.stream(folder.listFiles())
.filter(file -> file.isFile() && !file.getName().equalsIgnoreCase("shops.yml") && file.getName().endsWith(".yml"))
.map(file -> file.getName())
.collect(Collectors.toList());
}
}
15 changes: 15 additions & 0 deletions src/main/java/xyz/mackan/Slabbo/importers/ImportResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xyz.mackan.Slabbo.importers;

import xyz.mackan.Slabbo.types.Shop;

import java.util.List;

public class ImportResult {
public List<Shop> shops;
public List<String> skippedShops;

public ImportResult (List<Shop> shops, List<String> skippedShops) {
this.shops = shops;
this.skippedShops = skippedShops;
}
}
72 changes: 72 additions & 0 deletions src/main/java/xyz/mackan/Slabbo/importers/UShopImporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package xyz.mackan.Slabbo.importers;

import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import xyz.mackan.Slabbo.Slabbo;
import xyz.mackan.Slabbo.types.Shop;
import xyz.mackan.Slabbo.utils.ShopUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class UShopImporter {
public static ImportResult importUShops (File file) {
List<String> skippedShops = new ArrayList<String>();
List<Shop> shops = new ArrayList<Shop>();

FileConfiguration uShopConfig = new YamlConfiguration();

try {
uShopConfig.load(file);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (InvalidConfigurationException e) {
e.printStackTrace();
return null;
}

for (String key : uShopConfig.getKeys(false)) {
if (Slabbo.shopUtil.shops.containsKey(key)) {
skippedShops.add(key);
continue;
}

ConfigurationSection shopConfig = uShopConfig.getConfigurationSection(key);

String shopOwnerId = shopConfig.getString("host");

UUID shopOwnerUUID = UUID.fromString(shopOwnerId);

ItemStack itemStack = shopConfig.getItemStack("item");

int buyPrice = shopConfig.getInt("buyprice");
int sellPrice = shopConfig.getInt("sellPrice");

int quantity = shopConfig.getInt("stack");

int stock = shopConfig.getInt("amount");

boolean isAdmin = shopConfig.getBoolean("admin");

if (isAdmin) {
stock = 0;
}

Location shopLocation = ShopUtil.fromString(key);

Shop shop = new Shop(buyPrice, sellPrice, quantity, shopLocation, itemStack, stock, shopOwnerUUID, isAdmin, null);

shops.add(shop);
}

return new ImportResult(shops, skippedShops);
}
}
21 changes: 21 additions & 0 deletions src/main/java/xyz/mackan/Slabbo/utils/ShopUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xyz.mackan.Slabbo.utils;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import xyz.mackan.Slabbo.types.Shop;

import java.util.*;
Expand Down Expand Up @@ -76,4 +78,23 @@ public static String locationToString (Location location) {
);
}

public static Location fromString(String locString) {
String[] parts = locString.split(",");

String worldName = parts[0];
String xString = parts[1];
String yString = parts[2];
String zString = parts[3];

double x = Double.parseDouble(xString);
double y = Double.parseDouble(yString);
double z = Double.parseDouble(zString);

World world = Bukkit.getWorld(worldName);

if (world == null) return null;

return new Location(world, x, y, z);
}

}

0 comments on commit 424d70a

Please sign in to comment.