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();
}
}
});
}
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 + "!");
}
}
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 + "!");
}
}
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 + "!");
}
}
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());
}
}
}
}
}
Aggregations