diff --git a/assets/sprites/blocks/storage/trading-post.png b/assets/sprites/blocks/storage/trading-post.png new file mode 100644 index 00000000..f179f4f8 Binary files /dev/null and b/assets/sprites/blocks/storage/trading-post.png differ diff --git a/src/betamindy/BetaMindy.java b/src/betamindy/BetaMindy.java index d0138707..9d4407b4 100644 --- a/src/betamindy/BetaMindy.java +++ b/src/betamindy/BetaMindy.java @@ -156,6 +156,8 @@ public void init(){ } }); + new JsonShopPackageLoader().loadPackages(); + hardmode.init(); Events.run(Trigger.update, () -> { if(state.isPlaying()) hardmode.update(); diff --git a/src/betamindy/content/MindyBlocks.java b/src/betamindy/content/MindyBlocks.java index eaa84e36..09bf09f0 100644 --- a/src/betamindy/content/MindyBlocks.java +++ b/src/betamindy/content/MindyBlocks.java @@ -1302,7 +1302,7 @@ public boolean isHidden(){ requirements(Category.effect, uwu ? BuildVisibility.shown : BuildVisibility.sandboxOnly, with(Items.copper, 1)); size = 3; defaultAnucoins = 1000; - purchases = new PurchaseItem[]{package1, new ItemItem(MindyItems.source, 420, 69), new LiquidItem(Liquids.cryofluid, 5, 50f), new LiquidItem(Liquids.slag, 5, 50f), milk, coffee, herbTea, flowerTea, cocktail, holyRouter, new PurchaseInvBlock(starPen, 1, 1), new PurchaseInvBlock(Blocks.arc, 1699, 3)}; + purchases = new PurchaseItem[]{package1, new ItemItem(MindyItems.source, 420, 69), new LiquidItem(Liquids.cryofluid, 5, 50f), new LiquidItem(Liquids.slag, 5, 50f), milk, coffee, herbTea, flowerTea, cocktail, holyRouter, new PurchaseInvBlock(starPen, 1, 1)}; sellAllItems = sellAllUnits = sellAllBlocks = navigationBar = true; alwaysUnlocked = uwu; }}; diff --git a/src/betamindy/type/shop/ItemType.java b/src/betamindy/type/shop/ItemType.java new file mode 100644 index 00000000..2f96b2ee --- /dev/null +++ b/src/betamindy/type/shop/ItemType.java @@ -0,0 +1,10 @@ +package betamindy.type.shop; + + +public enum ItemType { + Package, + Runnable, + Block, + BlockInv, + Liquid; +} diff --git a/src/betamindy/type/shop/JsonShopPackage.java b/src/betamindy/type/shop/JsonShopPackage.java new file mode 100644 index 00000000..5517af3f --- /dev/null +++ b/src/betamindy/type/shop/JsonShopPackage.java @@ -0,0 +1,52 @@ +package betamindy.type.shop; + +import arc.func.*; +import arc.util.Log; +import betamindy.type.*; +import mindustry.ctype.UnlockableContent; +import mindustry.gen.*; +import mindustry.type.*; +import mindustry.world.Block; +//import rhino.*; + +public class JsonShopPackage { + public Boolf purchased, unlocked; + + public void invoke(Boolf purchased, Boolf unlocked){ + this.purchased = purchased; + this.unlocked = unlocked; + } + + public PurchaseItem NewJsonShopPackage(String name, ItemType itemType, ItemStack[] items, Integer cost, String code, UnlockableContent cItem, Integer amount, boolean iAbort){ + PurchaseItem item; + + //For later fixing and usage at stable v7 + /*if(code == null) return null; + + Context ctx = Context.enter(); + Scriptable scope = ctx.initStandardObjects(); + + Object wrappedStock = Context.javaToJS(this, scope); + ScriptableObject.putProperty(scope, "pkg", wrappedStock); + + ctx.evaluateString(scope, code.replace("\n", ""), "PackageScript", 1, null); + + Context.exit(); + + item = new PurchaseRunnable(name, cost){{ + purchased = e -> this.purchased.get(e); + + unlocked = e -> this.unlocked.get(e); + }};*/ + + item = switch (itemType) { + case Package -> new PackageShopItem(name, items){{abort = iAbort;}}; + case Runnable -> new PurchaseRunnable(null, 0){{abort = iAbort;}}; + case Block -> new BlockItem((Block) cItem, cost){{abort = iAbort;}}; + case BlockInv -> new PurchaseInvBlock((Block) cItem, cost, amount){{abort = iAbort;}}; + case Liquid -> new LiquidItem((Liquid) cItem, cost, amount){{abort = iAbort;}}; + }; + + return item; + } +} diff --git a/src/betamindy/type/shop/ShopPackageType.java b/src/betamindy/type/shop/ShopPackageType.java new file mode 100644 index 00000000..9d659b7e --- /dev/null +++ b/src/betamindy/type/shop/ShopPackageType.java @@ -0,0 +1,26 @@ +package betamindy.type.shop; + +import mindustry.type.*; + +import java.util.*; + +public class ShopPackageType { + public String itemType, shop, item, codePath; + public Integer cost, amount; + public Boolean abort; + public ItemStack[] items = {}; + + @Override + public String toString(){ + return "ShopPackageType{" + + "itemType=" + itemType + + "item=" + item + + "codePath=" + codePath + + ", cost=" + cost + + ", amount=" + amount + + ", abort=" + abort + + ", shop=" + shop + + ", items=" + Arrays.toString(items) + + '}'; + } +} diff --git a/src/betamindy/util/JsonShopPackageLoader.java b/src/betamindy/util/JsonShopPackageLoader.java new file mode 100644 index 00000000..e6942aa2 --- /dev/null +++ b/src/betamindy/util/JsonShopPackageLoader.java @@ -0,0 +1,87 @@ +package betamindy.util; + +import arc.files.*; +import arc.util.*; +import arc.util.serialization.*; +import betamindy.content.*; +import betamindy.type.*; +import betamindy.type.shop.*; +import betamindy.world.blocks.storage.*; +import mindustry.ctype.*; +import mindustry.io.*; +import mindustry.type.*; + +import static mindustry.Vars.*; + +public class JsonShopPackageLoader { + public void loadPackages() { + mods.list().each(e -> { + if(!e.isJava()) { + Fi path = e.root.child("content"); + if(path != null) path = path.child("shopItems"); + final Fi finalPath = path; + + if(path != null){ + path.walk(c -> { + if(c.extEquals("json")/* ||c.extEquals("hjson")*/){ + ShopPackageType pack; + + /*idk how does this work so i'll just ignore hjson + if(c.extEquals("hjson")){ + pack = JsonIO.json.fromJson(ShopPackageType.class, String.valueOf(Jval.read(c.readString()).toString(Jval.Jformat.plain))); + } else {*/ + pack = JsonIO.json.fromJson(ShopPackageType.class, c.readString()); + //} + + String name = e.meta.name + "." + c.nameWithoutExtension(); + + if(pack != null){ + String itemType = pack.itemType; + Shop shop = (Shop) MindyBlocks.extraShop; + ItemStack[] items = pack.items; + + if(pack.shop != null) { + try { + shop = (Shop) content.block("betamindy-" + pack.shop); + } catch(Exception ex){ + Log.err(e.meta.name + ": Shop \"" + pack.shop + "\" doesn't exist or can't be found. Defaulted to Extra Shop."); + } + } + + Fi codeFile = finalPath.child(pack.codePath + ".js"); + String code = null; + + if(codeFile.exists()){ + code = codeFile.readString(); + Log.info(code); + } + + ItemType type = ItemType.valueOf(itemType); + UnlockableContent cItem = switch (type){ + case Block, BlockInv -> content.block(pack.item); + case Liquid -> content.getByName(ContentType.liquid, pack.item); + default -> null; + }; + + boolean abort = false; + if(pack.abort != null) abort = pack.abort; + + PurchaseItem item = new JsonShopPackage().NewJsonShopPackage(name, type, items, pack.cost == null ? 69 : pack.cost, code, cItem, pack.amount, abort); + + if(item instanceof PurchaseRunnable){ + Log.info("Runnables in json are not supported yet. Please wait until v7."); + } else { + if (item != null && shop != null) { + shop.jsonItems.add(item); + } else { + Log.err("Package error for package " + name); + } + } + } + } + }); + } + } + }); + } +} diff --git a/src/betamindy/world/blocks/storage/Shop.java b/src/betamindy/world/blocks/storage/Shop.java index 6d5c88b7..e429f860 100644 --- a/src/betamindy/world/blocks/storage/Shop.java +++ b/src/betamindy/world/blocks/storage/Shop.java @@ -36,7 +36,7 @@ import static arc.Core.atlas; import static mindustry.Vars.*; -@SuppressWarnings("al") +@SuppressWarnings("all") public class Shop extends PayloadAcceptor { public int defaultAnucoins = 0; public TextureRegion spinRegion; @@ -45,6 +45,7 @@ public class Shop extends PayloadAcceptor { public float spinShadowRadius = 15f; public boolean drawSpinSprite = false; + public Seq jsonItems = new Seq<>(); public @Nullable PurchaseItem[] purchases; public @Nullable Block[] soldBlocks; public boolean sellAllItems = false; @@ -52,7 +53,7 @@ public class Shop extends PayloadAcceptor { public boolean sellAllBlocks = false; public boolean navigationBar = false; - BaseDialog shopDialog; + BaseDialog shopDialog = null; String searchString = ""; Cell itemCell; @@ -130,6 +131,13 @@ public void init(){ unitScores = BetaMindy.unitScores; if(purchases != null) { + if(jsonItems.size > 0) { + Seq temp = new Seq(); + temp.addAll(purchases); + temp.addAll(jsonItems); + purchases = temp.toArray(); + } + for (PurchaseItem purchase : purchases) { if (purchase instanceof PackageShopItem p && p.cost == 0) { p.definePrice(); @@ -570,6 +578,11 @@ public void buildConfiguration(Table table) { shopDialog.addCloseButton(); } + if(shopDialog == null) { + shopDialog = new BaseDialog(Core.bundle.get("ui.shop.title")); + shopDialog.addCloseButton(); + } + float width = Math.min(Core.graphics.getWidth(), Core.graphics.getHeight()); //float height = Math.max(Core.graphics.getWidth(), Core.graphics.getHeight());