Skip to content

Commit

Permalink
Repository Init
Browse files Browse the repository at this point in the history
  • Loading branch information
nahkd123 committed Jul 28, 2021
0 parents commit 16ca033
Show file tree
Hide file tree
Showing 21 changed files with 1,450 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://github.com/github/gitignore/blob/master/Maven.gitignore
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse
.settings/
.classpath
.project
40 changes: 40 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.studiomango</groupId>
<artifactId>stonks</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Stonks</name>
<description>Stocks market in your server</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xyz.studiomango</groupId>
<artifactId>featherpowders</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Stonks
_get rich_

## Requirements
- [Spigot 1.17](http://spigotmc.org/wiki/buildtools)
- [FeatherPowders](https://github.com/MangoPlex/FeatherPowders)
- Custom Items Driver plugin for FeatherPowders (We suggest [ManyItems](https://github.com/MangoPlex/ManyItems))
130 changes: 130 additions & 0 deletions src/main/java/stonks/Stonks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package stonks;

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

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import featherpowders.items.CustomStack;
import featherpowders.items.CustomType;
import featherpowders.items.ItemsDriver;
import stonks.commands.MarketCommand;
import stonks.events.ChatEventsHandler;
import stonks.players.StonksPlayer;
import stonks.stock.OfferType;
import stonks.stock.StockInfo;
import stonks.stock.StockOffer;

public class Stonks extends JavaPlugin {

public static double TAX;
public static double MIN_PRICE;
public static double PRICE_CAP;
public static int MAX_UNITS;
public static ArrayList<StockInfo> STOCKS = new ArrayList<>();
public static ItemsDriver<CustomType, CustomStack> DRIVER;

private static File stocksDataFile;
private static YamlConfiguration stocksData;

public static StockInfo getStockInfo(String id) {
return STOCKS.stream().filter(p -> p.itemType.dataId.equals(id)).findFirst().orElse(null);
}

@Override
public void onEnable() {
DRIVER = ItemsDriver.getDefaultDriver();
if (DRIVER == null) {
getServer().getConsoleSender().sendMessage(new String[] {
"",
"§cFATAL ERROR: §fNo custom items driver found (FeatherPowder items driver)",
"§fPlease install a plugin that handle custom items",
"§fThe plugin must uses custom items driver API that's provided by FeatherPowder",
"",
"§eStonks will be disabled",
""
});
getServer().getPluginManager().disablePlugin(this);
return;
}

saveResource("config.yml", false);
reloadConfig();

TAX = getConfig().getDouble("market.tax", 0.01);
PRICE_CAP = getConfig().getDouble("market.unitPriceCap", 50_000_000.0);
MIN_PRICE = getConfig().getDouble("market.minimumPrice", 0.5);
MAX_UNITS = getConfig().getInt("market.unitsCap", 25600);

StonksPlayer.init(this);
stocksDataFile = new File(getDataFolder(), "stocks_data.yml");
stocksData = YamlConfiguration.loadConfiguration(stocksDataFile);

for (String itemID : getConfig().getStringList("stocks")) {
CustomType itemType = DRIVER.fromDataID(itemID);
if (itemType == null) {
getServer().getConsoleSender().sendMessage("§e[Stonks] Item with ID " + itemID + " not found");
continue;
}

StockInfo info = new StockInfo(itemType);
STOCKS.add(info);
}
getServer().getConsoleSender().sendMessage("§e[Stonks] §f" + STOCKS.size() + " stocks registered in market!");

getServer().getConsoleSender().sendMessage("§e[Stonks] §fLoading market offers data...");
for (String id : stocksData.getKeys(false)) {
StockInfo info = getStockInfo(id);
for (String uuid : stocksData.getConfigurationSection(id).getKeys(false)) {
UUID uuidObj = UUID.fromString(uuid);
UUID owner = UUID.fromString(stocksData.getString(id + "." + uuid + ".owner"));
OfferType type = OfferType.valueOf(stocksData.getString(id + "." + uuid + ".type"));
double pricePerUnit = stocksData.getDouble(id + "." + uuid + ".pricePerUnit");
int units = stocksData.getInt(id + "." + uuid + ".units");
int filled = stocksData.getInt(id + "." + uuid + ".filled");

StockOffer offer = new StockOffer(info, owner, type, units, pricePerUnit);
offer.offerUUID = uuidObj;
offer.unitsFilled = filled;

if (type == OfferType.BUY_OFFER) info.detailedBuyOffers.add(offer);
if (type == OfferType.SELL_OFFER) info.detailedSellOffers.add(offer);
}
}
getServer().getConsoleSender().sendMessage("§e[Stonks] §fMarket data loaded");

getServer().getPluginManager().registerEvents(new ChatEventsHandler(), this);

getCommand("market").setExecutor(new MarketCommand());

getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
STOCKS.forEach(stock -> { stock.calculatePrice(false); });
}, 0, 20);
}

@Override
public void onDisable() {
try {
stocksData.save(stocksDataFile);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void updateMarketOffer(StockOffer offer) {
if (offer.unitsFilled == offer.units && stocksData.contains(offer.stock.itemType.dataId + "." + offer.offerUUID.toString())) {
stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString(), null);
return;
}

stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString() + ".owner", offer.ownerUUID.toString());
stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString() + ".type", offer.offerType.toString());
stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString() + ".pricePerUnit", offer.pricePerUnit);
stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString() + ".units", offer.units);
stocksData.set(offer.stock.itemType.dataId + "." + offer.offerUUID.toString() + ".filled", offer.unitsFilled);
}

}
97 changes: 97 additions & 0 deletions src/main/java/stonks/StonksHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package stonks;

import java.util.HashMap;
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import featherpowders.items.CustomStack;
import featherpowders.ui.chest.ChestUI;
import stonks.stock.OfferType;
import stonks.stock.StockInfo;
import stonks.ui.StockUI;

public class StonksHelper {

// TODO: Hook with Vault Economy Service

public static boolean hasAtLeastMoney(Player player, double amount) {
return true;
}

public static void takeMoney(Player player, double amount) {}
public static void giveMoney(Player player, double amount) {}

public static HashMap<UUID, OfferType> offeringCustom = new HashMap<>();
public static HashMap<UUID, Integer> offeringUnits = new HashMap<>();
public static HashMap<UUID, StockUI> stockUI = new HashMap<>();
public static HashMap<UUID, ChestUI> offeringPreviousUI = new HashMap<>();

public static void markBuyOfferCustom(Player player) {
offeringCustom.put(player.getUniqueId(), OfferType.BUY_OFFER);
}

public static void markSellOfferCustom(Player player) {
offeringCustom.put(player.getUniqueId(), OfferType.SELL_OFFER);
}

public static void clearOfferMarking(Player player) {
offeringCustom.remove(player.getUniqueId());
offeringUnits.remove(player.getUniqueId());
stockUI.remove(player.getUniqueId());
}

public static int countUnitsInInventory(Player player, StockInfo stock) {
int units = 0;
for (CustomStack stack : Stonks.DRIVER.fromBukkit(player.getInventory().getContents())) {
if (stack == null) continue;
if (stack.type == stock.itemType) units += stack.amount;
}
return units;
}

public static int canAddUnits(Player player, StockInfo stock) {
int units = 0;
ItemStack sample = stock.createUnit(1);

for (ItemStack item : player.getInventory().getStorageContents()) {
if (item == null || item.getType() == Material.AIR || item.getAmount() == 0) {
units += sample.getType().getMaxStackSize();
continue;
}
if (item.isSimilar(sample)) units += sample.getType().getMaxStackSize() - item.getAmount();
}
return units;
}

public static void addOrDrop(Player player, StockInfo stock, int amount) {
int canAdd = canAddUnits(player, stock);
if (canAdd >= amount) {
player.getInventory().addItem(stock.createUnit(amount));
} else {
player.getInventory().addItem(stock.createUnit(canAdd));
player.getWorld().dropItem(player.getLocation(), stock.createUnit(amount - canAdd));
}
}

public static void instantBuy(Player player, StockInfo stock, int amount) {
double value = stock.calculateInstantBuy(amount);
if (value < 0) {
player.closeInventory();
player.sendMessage("§cCannot perform action: §fNot enough units §7§o(try something smaller)");
return;
}
if (!hasAtLeastMoney(player, value)) {
player.closeInventory();
player.sendMessage("§cNot enough money!");
return;
}

takeMoney(player, value);
stock.performInstantBuy(player, amount);
player.sendMessage("§7You've bought " + amount + " units for " + value);
}

}
21 changes: 21 additions & 0 deletions src/main/java/stonks/commands/MarketCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stonks.commands;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import featherpowders.commands.ArgumentsMatch;
import featherpowders.commands.Command;
import featherpowders.ui.PlayerUI;
import stonks.ui.MarketUI;

public class MarketCommand extends Command {

@ArgumentsMatch(pattern = {})
public void index(CommandSender sender) {
if (sender instanceof Player player) {
MarketUI ui = new MarketUI(player, 0);
PlayerUI.openUI(player, ui);
} else sendError(sender, "Cannot open market in console", "But why doe?");
}

}
Loading

0 comments on commit 16ca033

Please sign in to comment.