Initial setup
This commit is contained in:
15
src/main/java/mod/treestar/shopmod/SilverShop.java
Normal file
15
src/main/java/mod/treestar/shopmod/SilverShop.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package mod.treestar.shopmod;
|
||||
|
||||
import mod.treestar.shopmod.itemprovider.SilverShopItemProvider;
|
||||
import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SilverShop implements WurmServerMod {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
}
|
||||
}
|
||||
74
src/main/java/mod/treestar/shopmod/SilverShopService.java
Normal file
74
src/main/java/mod/treestar/shopmod/SilverShopService.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package mod.treestar.shopmod;
|
||||
|
||||
import com.wurmonline.server.players.Player;
|
||||
import mod.treestar.shopmod.categoryprovider.SilverShopCategoryProvider;
|
||||
import mod.treestar.shopmod.datamodels.SilverShopCategory;
|
||||
import mod.treestar.shopmod.datamodels.SilverShopItem;
|
||||
import mod.treestar.shopmod.datamodels.SilverShopPurchaseOrder;
|
||||
import mod.treestar.shopmod.itemprovider.SilverShopItemProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class SilverShopService {
|
||||
private HashMap<Integer, SilverShopCategory> categories;
|
||||
private HashMap<Integer, SilverShopItem> items;
|
||||
private List<SilverShopCategoryProvider> categoryProviders;
|
||||
private List<SilverShopItemProvider> itemProviders;
|
||||
|
||||
private static SilverShopService instance;
|
||||
|
||||
public void init() {
|
||||
for (SilverShopCategoryProvider categoryProvider : categoryProviders) {
|
||||
|
||||
List<SilverShopCategory> providerCategories = categoryProvider.getCategories();
|
||||
for (SilverShopCategory category : providerCategories) {
|
||||
if (categories.containsKey(category.getId())) {
|
||||
// Ignore it. The newer category simply won't update any details.
|
||||
continue;
|
||||
}
|
||||
|
||||
categories.put(category.getId(), category);
|
||||
}
|
||||
}
|
||||
for (SilverShopItemProvider itemProvider : itemProviders) {
|
||||
List<SilverShopItem> providerItems = itemProvider.getItems();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canPurchase(SilverShopPurchaseOrder purchaseOrder, Player shopper) {
|
||||
long totalCost = purchaseOrder.getTotalCost();
|
||||
|
||||
if(shopper.getMoney() < totalCost) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean attemptPurchase(SilverShopPurchaseOrder purchaseOrder, Player shopper) {
|
||||
if(!canPurchase(purchaseOrder, shopper)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long totalCost = purchaseOrder.getTotalCost();
|
||||
try {
|
||||
if (shopper.chargeMoney(totalCost)) {
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static SilverShopService getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new SilverShopService();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package mod.treestar.shopmod.categoryprovider;
|
||||
|
||||
import mod.treestar.shopmod.datamodels.SilverShopCategory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SilverShopCategoryProvider {
|
||||
void init();
|
||||
List<SilverShopCategory> getCategories();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package mod.treestar.shopmod.datamodels;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A category containing some number of items for the silver shop.
|
||||
*/
|
||||
public class SilverShopCategory {
|
||||
private int id;
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
private List<SilverShopItem> items;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public List<SilverShopItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<SilverShopItem> items) {
|
||||
this.items = items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package mod.treestar.shopmod.datamodels;
|
||||
|
||||
import mod.treestar.shopmod.purchasehandlers.SilverShopItemPurchaseEffect;
|
||||
|
||||
/**
|
||||
* Represents a single item for purchase in the silver shop. This may be an in-game item, service, etc.
|
||||
*/
|
||||
public class SilverShopItem {
|
||||
private int id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String image;
|
||||
private long ironPrice;
|
||||
private int categoryId;
|
||||
|
||||
private SilverShopItemPurchaseEffect purchaseHandler;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public long getIronPrice() {
|
||||
return ironPrice;
|
||||
}
|
||||
|
||||
public void setIronPrice(long ironPrice) {
|
||||
this.ironPrice = ironPrice;
|
||||
}
|
||||
|
||||
public SilverShopItemPurchaseEffect getPurchaseHandler() {
|
||||
return purchaseHandler;
|
||||
}
|
||||
|
||||
public void setPurchaseHandler(SilverShopItemPurchaseEffect purchaseHandler) {
|
||||
this.purchaseHandler = purchaseHandler;
|
||||
}
|
||||
|
||||
public int getCategoryId() {
|
||||
return categoryId;
|
||||
}
|
||||
|
||||
public void setCategoryId(int categoryId) {
|
||||
this.categoryId = categoryId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package mod.treestar.shopmod.datamodels;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A single purchase in the shop.
|
||||
*/
|
||||
public class SilverShopPurchaseOrder {
|
||||
private List<SilverShopItem> shoppingCart;
|
||||
|
||||
public List<SilverShopItem> getShoppingCart() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
public void setShoppingCart(List<SilverShopItem> shoppingCart) {
|
||||
this.shoppingCart = shoppingCart;
|
||||
}
|
||||
|
||||
public long getTotalCost() {
|
||||
return getShoppingCart().stream().mapToLong(SilverShopItem::getIronPrice).sum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package mod.treestar.shopmod.itemprovider;
|
||||
|
||||
import mod.treestar.shopmod.datamodels.SilverShopCategory;
|
||||
import mod.treestar.shopmod.datamodels.SilverShopItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An interface representing a provider of silver shop items.
|
||||
*/
|
||||
public interface SilverShopItemProvider {
|
||||
void init();
|
||||
List<SilverShopItem> getItems();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package mod.treestar.shopmod.purchasehandlers;
|
||||
|
||||
import com.wurmonline.server.players.Player;
|
||||
|
||||
public interface SilverShopItemPurchaseEffect {
|
||||
/**
|
||||
* What happens after a player successfully purchases the item. This assumes the transaction was completed successfully.
|
||||
* @param player the player that performed the purchase
|
||||
*/
|
||||
void onPurchase(Player player);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package mod.treestar.shopmod.purchasehandlers;
|
||||
|
||||
import com.wurmonline.server.FailedException;
|
||||
import com.wurmonline.server.items.*;
|
||||
import com.wurmonline.server.players.Player;
|
||||
|
||||
/**
|
||||
* Simple item purchase effect that gives a player a specific item on a successful purchase.
|
||||
* @see SilverShopItemPurchaseEffect
|
||||
*/
|
||||
public class SilverShopWurmItemPurchaseEffect implements SilverShopItemPurchaseEffect {
|
||||
private int itemTemplateId;
|
||||
private float ql;
|
||||
private boolean randomQl;
|
||||
private byte rarity;
|
||||
|
||||
@Override
|
||||
public void onPurchase(Player player) {
|
||||
try {
|
||||
Item item = ItemFactory.createItem(itemTemplateId, ql, (byte) 0, (byte) 0, null);
|
||||
player.getInventory().insertItem(item);
|
||||
ItemTemplate template = ItemTemplateFactory.getInstance().getTemplate(itemTemplateId);
|
||||
player.sendSystemMessage(String.format("You receive a %s.", template.getName()));
|
||||
} catch (NoSuchTemplateException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (FailedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user