Search in sources :

Example 1 with MineData

use of me.untouchedodin0.privatemines.mine.data.MineData in project PrivateMines by UntouchedOdin0.

the class MineFactory method create.

/**
 * Creates a mine for the {@link Player} at {@link Location} with {@link MineType}
 *
 * @param player   the player the mine should be created for
 * @param location the location of the mine
 * @param mineType the type of mine to paste
 */
public void create(Player player, Location location, MineType mineType) {
    Instant start = Instant.now();
    UUID uuid = player.getUniqueId();
    File schematicFile = new File("plugins/PrivateMines/schematics/" + mineType.getFile());
    Mine mine = new Mine(privateMines);
    Map<String, Boolean> flags = mineType.getFlags();
    Map<Material, Double> prices = new HashMap<>();
    // Material[] mats = Material.values();
    Map<Material, Double> materials = mineType.getMaterials();
    if (materials != null) {
        prices.putAll(materials);
    }
    Shop shop = new ShopBuilder().setOwner(uuid).setPrices(prices).build();
    String regionName = String.format("mine-%s", player.getUniqueId());
    ClipboardFormat clipboardFormat = ClipboardFormats.findByFile(schematicFile);
    BlockVector3 vector = BlockVector3.at(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    SchematicStorage storage = privateMines.getSchematicStorage();
    SchematicIterator.MineBlocks mineBlocks = storage.getMineBlocksMap().get(schematicFile);
    Task.asyncDelayed(() -> {
        if (clipboardFormat != null) {
            try (ClipboardReader clipboardReader = clipboardFormat.getReader(new FileInputStream(schematicFile))) {
                World world = BukkitAdapter.adapt(Objects.requireNonNull(location.getWorld()));
                EditSession editSession = WorldEdit.getInstance().newEditSessionBuilder().world(world).fastMode(true).build();
                LocalSession localSession = new LocalSession();
                Clipboard clipboard = clipboardReader.read();
                ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard);
                // mb0|0,50,-150
                // mb1|13,48,-144
                // mb2|-12,20,-116
                // cbo|0,50,-151
                // min|-30000000,-64,-30000000
                // loc|763.6692645437984,140.45037494032877,728.8705431310638
                // 
                // 763,140,729 Sponge
                // 751,110,763 Lower Rails Sponge - mb0 + mb2 -> 763 - 0 +-12 = 751
                // 776,138,735 Upper Rails Sponge - mb0 + mb1 -> 763 - 0 + 13 = 776
                BlockVector3 lrailsV = vector.subtract(mineBlocks.getSpawnLocation()).add(mineBlocks.getCorner2().add(0, 0, 1));
                BlockVector3 urailsV = vector.subtract(mineBlocks.getSpawnLocation()).add(mineBlocks.getCorner1().add(0, 0, 1));
                Location spongeL = new Location(location.getWorld(), vector.getBlockX(), vector.getBlockY(), vector.getBlockZ() + 1);
                Location lrailsL = new Location(location.getWorld(), lrailsV.getBlockX(), lrailsV.getBlockY(), lrailsV.getBlockZ());
                Location urailsL = new Location(location.getWorld(), urailsV.getBlockX(), urailsV.getBlockY(), urailsV.getBlockZ());
                localSession.setClipboard(clipboardHolder);
                Operation operation = clipboardHolder.createPaste(editSession).to(vector).ignoreAirBlocks(true).build();
                try {
                    Operations.complete(operation);
                    editSession.close();
                } catch (WorldEditException worldEditException) {
                    worldEditException.printStackTrace();
                }
                Region region = clipboard.getRegion();
                Region newRegion;
                BlockVector3 clipboardOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
                Vector3 realTo = vector.toVector3().add(clipboardHolder.getTransform().apply(clipboardOffset.toVector3()));
                Vector3 max = realTo.add(clipboardHolder.getTransform().apply(region.getMaximumPoint().subtract(region.getMinimumPoint()).toVector3()));
                RegionSelector regionSelector = new CuboidRegionSelector(world, realTo.toBlockPoint(), max.toBlockPoint());
                localSession.setRegionSelector(world, regionSelector);
                regionSelector.learnChanges();
                // noinspection DanglingJavadoc
                try {
                    newRegion = regionSelector.getRegion();
                    Location fullMin = BukkitAdapter.adapt(BukkitAdapter.adapt(world), newRegion.getMinimumPoint());
                    Location fullMax = BukkitAdapter.adapt(BukkitAdapter.adapt(world), newRegion.getMaximumPoint());
                    ProtectedCuboidRegion miningWorldGuardRegion = new ProtectedCuboidRegion(regionName, lrailsV, urailsV);
                    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
                    RegionManager regionManager = container.get(world);
                    if (regionManager != null) {
                        regionManager.addRegion(miningWorldGuardRegion);
                    }
                    /**
                     *                         This sadly has to be called synchronously else it'll throw a
                     *                         {@link java.lang.IllegalStateException}
                     *                         This is due to how WorldGuard handles their flags...
                     *                         @see com.sk89q.worldguard.bukkit.protection.events.flags.FlagContextCreateEvent
                     */
                    Task.syncDelayed(() -> {
                        if (flags != null) {
                            flags.forEach((s, aBoolean) -> {
                                Flag<?> flag = Flags.fuzzyMatchFlag(WorldGuard.getInstance().getFlagRegistry(), s);
                                if (aBoolean) {
                                    try {
                                        Utils.setFlag(miningWorldGuardRegion, flag, "allow");
                                    } catch (InvalidFlagFormat e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    try {
                                        Utils.setFlag(miningWorldGuardRegion, flag, "deny");
                                    } catch (InvalidFlagFormat e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    });
                    MineData mineData = new MineDataBuilder().setOwner(uuid).setMinimumMining(lrailsL).setMaximumMining(urailsL).setMinimumFullRegion(fullMin).setMaximumFullRegion(fullMax).setSpawnLocation(spongeL).setMineLocation(location).setMineType(mineType).setShop(shop).build();
                    mine.setMineData(mineData);
                    mine.saveMineData(player, mineData);
                } catch (IncompleteRegionException e) {
                    e.printStackTrace();
                }
                try (EditSession session = WorldEdit.getInstance().newEditSessionBuilder().world(world).fastMode(true).build()) {
                    BlockVector3 spawn = BlockVector3.at(spongeL.getBlockX(), spongeL.getBlockY(), spongeL.getBlockZ());
                    Pattern pattern = BukkitAdapter.adapt(Material.AIR.createBlockData());
                    session.setBlock(spawn, pattern);
                }
                if (privateMines.getMineStorage().hasMine(player.getUniqueId())) {
                    privateMines.getMineStorage().replaceMine(player.getUniqueId(), mine);
                } else {
                    privateMines.getMineStorage().addMine(player.getUniqueId(), mine);
                }
                mine.resetNoCheck();
                TextComponent teleportMessage = new TextComponent(ChatColor.GREEN + "Click me to teleport to your mine!");
                teleportMessage.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/privatemines teleport"));
                // player.spigot().sendMessage(teleportMessage);
                Instant finished = Instant.now();
                Duration creationDuration = Duration.between(start, finished);
                final long microseconds = TimeUnit.NANOSECONDS.toMillis(creationDuration.toNanos());
                privateMines.getLogger().info("Mine creation time: " + microseconds + " milliseconds");
                Task.syncDelayed(() -> player.teleport(spongeL));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) RegionContainer(com.sk89q.worldguard.protection.regions.RegionContainer) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) Operation(com.sk89q.worldedit.function.operation.Operation) World(com.sk89q.worldedit.world.World) ShopBuilder(me.untouchedodin0.privatemines.playershops.ShopBuilder) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) InvalidFlagFormat(com.sk89q.worldguard.protection.flags.InvalidFlagFormat) Mine(me.untouchedodin0.privatemines.mine.Mine) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) UUID(java.util.UUID) SchematicStorage(me.untouchedodin0.privatemines.storage.SchematicStorage) TextComponent(net.md_5.bungee.api.chat.TextComponent) Pattern(com.sk89q.worldedit.function.pattern.Pattern) MineDataBuilder(me.untouchedodin0.privatemines.mine.data.MineDataBuilder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) Instant(java.time.Instant) Material(org.bukkit.Material) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) Duration(java.time.Duration) CuboidRegionSelector(com.sk89q.worldedit.regions.selector.CuboidRegionSelector) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) FileInputStream(java.io.FileInputStream) RegionSelector(com.sk89q.worldedit.regions.RegionSelector) CuboidRegionSelector(com.sk89q.worldedit.regions.selector.CuboidRegionSelector) MineData(me.untouchedodin0.privatemines.mine.data.MineData) Shop(me.untouchedodin0.privatemines.playershops.Shop) Region(com.sk89q.worldedit.regions.Region) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) File(java.io.File) SchematicIterator(me.untouchedodin0.privatemines.iterator.SchematicIterator) Location(org.bukkit.Location)

Example 2 with MineData

use of me.untouchedodin0.privatemines.mine.data.MineData in project PrivateMines by UntouchedOdin0.

the class UPCSellListener method onAutoSell.

@EventHandler
public void onAutoSell(UltraPrisonAutoSellEvent autoSellEvent) {
    Player player = autoSellEvent.getPlayer();
    Location playerLocation = player.getLocation();
    Mine mine = mineStorage.getClosest(playerLocation);
    MineData mineData = mine.getMineData();
    Player owner = Bukkit.getOfflinePlayer(mineData.getMineOwner()).getPlayer();
    if (player.equals(owner))
        return;
    double tax = autoSellEvent.getMoneyToDeposit() / 100.0 * mineData.getTax();
    double sellPrice = autoSellEvent.getMoneyToDeposit();
    double afterTax = sellPrice - tax;
    autoSellEvent.setMoneyToDeposit(afterTax);
    if (owner != null) {
        Utils.sendActionBar(owner, "You've received $" + tax + " in taxes from " + player.getDisplayName() + ChatColor.GREEN + "!");
    }
}
Also used : MineData(me.untouchedodin0.privatemines.mine.data.MineData) Player(org.bukkit.entity.Player) Mine(me.untouchedodin0.privatemines.mine.Mine) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with MineData

use of me.untouchedodin0.privatemines.mine.data.MineData in project PrivateMines by UntouchedOdin0.

the class UPCSellListener method onSellAll.

@EventHandler
public void onSellAll(UltraPrisonSellAllEvent sellAllEvent) {
    Economy economy = PrivateMines.getEconomy();
    Player player = sellAllEvent.getPlayer();
    Location playerLocation = player.getLocation();
    Mine mine = mineStorage.getClosest(playerLocation);
    MineData mineData = mine.getMineData();
    Player owner = Bukkit.getOfflinePlayer(mineData.getMineOwner()).getPlayer();
    if (player.equals(owner))
        return;
    double tax = sellAllEvent.getSellPrice() / 100.0 * mineData.getTax();
    double sellPrice = sellAllEvent.getSellPrice();
    double afterTax = sellPrice - tax;
    sellAllEvent.setSellPrice(afterTax);
    economy.depositPlayer(owner, tax);
    if (owner != null) {
        owner.sendMessage(ChatColor.GREEN + "You've received $" + tax + " in taxes from " + player.getDisplayName() + ChatColor.GREEN + "!");
    }
}
Also used : MineData(me.untouchedodin0.privatemines.mine.data.MineData) Player(org.bukkit.entity.Player) Economy(net.milkbowl.vault.economy.Economy) Mine(me.untouchedodin0.privatemines.mine.Mine) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 4 with MineData

use of me.untouchedodin0.privatemines.mine.data.MineData in project PrivateMines by UntouchedOdin0.

the class AutoSellListener method onAutoSell.

@EventHandler
public void onAutoSell(AutoSellEvent autoSellEvent) {
    Economy economy = PrivateMines.getEconomy();
    Player player = autoSellEvent.getPlayer();
    Location location = player.getLocation();
    Mine mine = mineStorage.getClosest(location);
    MineData mineData = mine.getMineData();
    Player owner = Bukkit.getOfflinePlayer(mineData.getMineOwner()).getPlayer();
    if (player.equals(owner))
        return;
    double tax = autoSellEvent.getPrice() / 100.0 * mineData.getTax();
    double sellPrice = autoSellEvent.getPrice();
    double afterTax = sellPrice - tax;
    autoSellEvent.setMultiplier(afterTax);
    economy.depositPlayer(owner, tax);
    if (owner != null) {
        owner.sendMessage(ChatColor.GREEN + "You've received $" + tax + " in taxes from " + player.getDisplayName() + ChatColor.GREEN + "!");
    }
}
Also used : MineData(me.untouchedodin0.privatemines.mine.data.MineData) Player(org.bukkit.entity.Player) Economy(net.milkbowl.vault.economy.Economy) Mine(me.untouchedodin0.privatemines.mine.Mine) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 5 with MineData

use of me.untouchedodin0.privatemines.mine.data.MineData in project PrivateMines by UntouchedOdin0.

the class Mine method upgrade.

public void upgrade() {
    MineTypeManager mineTypeManager = privateMines.getMineTypeManager();
    MineFactory mineFactory = privateMines.getMineFactory();
    MineData mineData = getMineData();
    UUID mineOwner = mineData.getMineOwner();
    Player player = Bukkit.getOfflinePlayer(mineOwner).getPlayer();
    MineType currentType = mineTypeManager.getMineType(mineData.getMineType());
    MineType nextType = mineTypeManager.getNextMineType(currentType.getName());
    double upgradeCost = nextType.getUpgradeCost();
    if (player != null) {
        if (currentType == nextType) {
            privateMines.getLogger().info("Failed to upgrade " + player.getName() + "'s mine as it was fully upgraded!");
        } else {
            if (upgradeCost == 0) {
                Location mineLocation = mineData.getMineLocation();
                delete(mineOwner);
                mineFactory.create(Objects.requireNonNull(player), mineLocation, nextType);
            } else {
                Economy economy = PrivateMines.getEconomy();
                double balance = economy.getBalance(player);
                if (balance < upgradeCost) {
                    player.sendMessage(ChatColor.RED + "You don't have enough money to upgrade your mine!");
                } else {
                    Location mineLocation = mineData.getMineLocation();
                    delete(mineOwner);
                    mineFactory.create(Objects.requireNonNull(player), mineLocation, nextType);
                    economy.withdrawPlayer(player, nextType.getUpgradeCost());
                }
            }
        }
    }
}
Also used : MineData(me.untouchedodin0.privatemines.mine.data.MineData) Player(org.bukkit.entity.Player) Economy(net.milkbowl.vault.economy.Economy) MineType(me.untouchedodin0.kotlin.mine.type.MineType) MineFactory(me.untouchedodin0.privatemines.factory.MineFactory)

Aggregations

MineData (me.untouchedodin0.plugin.mines.data.MineData)20 Mine (me.untouchedodin0.plugin.mines.Mine)16 MineData (me.untouchedodin0.privatemines.mine.data.MineData)16 Player (org.bukkit.entity.Player)13 CommandHook (redempt.redlib.commandmanager.CommandHook)13 Location (org.bukkit.Location)12 Mine (me.untouchedodin0.privatemines.mine.Mine)11 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 File (java.io.File)8 MineType (me.untouchedodin0.plugin.mines.MineType)6 Economy (net.milkbowl.vault.economy.Economy)6 UUID (java.util.UUID)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 MineType (me.untouchedodin0.kotlin.mine.type.MineType)5 IWrappedRegion (org.codemc.worldguardwrapper.region.IWrappedRegion)5 CuboidRegion (redempt.redlib.region.CuboidRegion)5 Gson (com.google.gson.Gson)4 Pattern (com.sk89q.worldedit.function.pattern.Pattern)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4