Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
iConomyChestShop version 2.73, supports all major economy plugins, so…
Browse files Browse the repository at this point in the history
…me fixes and made it more flexible
  • Loading branch information
Acrobot committed May 12, 2011
1 parent 7c5c36b commit 7da9232
Show file tree
Hide file tree
Showing 21 changed files with 901 additions and 206 deletions.
22 changes: 14 additions & 8 deletions com/Acrobot/iConomyChestShop/Basic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Acrobot.iConomyChestShop;

import com.Acrobot.iConomyChestShop.Chests.MinecraftChest;
import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaChest;
import info.somethingodd.bukkit.OddItem.OddItem;
import java.io.File;
Expand Down Expand Up @@ -166,14 +167,17 @@ public static String stripName(String name) {

//What do I even have to say?
public static int getItemAmountFromInventory(Inventory inv, ItemStack is) {
return getItemAmount(inv.getContents(), is);
}

public static int getItemAmount(ItemStack[] items, ItemStack is){
int id = is.getTypeId();
boolean checkDurability = true;
if(ConfigManager.getBoolean("allowUsedItemsToBeSold") && id >= 256 && id <= 317){
checkDurability = false;
}
int count = 0;
ItemStack Items[] = inv.getContents();
for (ItemStack Item : Items) {
for (ItemStack Item : items) {
if (Item == null) {
continue;
}
Expand Down Expand Up @@ -224,6 +228,7 @@ public static void removeItemStackFromInventory(Inventory inv, ItemStack is, int
public static void addItemToInventory(MinecartManiaChest chest, ItemStack is, int left){
addItemToInventory(chest.getInventory(), is, left);
}

//Adds item to inventory
public static void addItemToInventory(Inventory inv, ItemStack is, int left){
int maxStackSize = is.getType().getMaxStackSize();
Expand Down Expand Up @@ -253,14 +258,14 @@ public static void addItemToInventory(Inventory inv, ItemStack is, int left){
}
}

public static Chest findChest(Block block){
public static MinecraftChest findChest(Block block){
if(!ConfigManager.getString("position").toUpperCase().equals("ANY")){
Block faceBlock = block.getFace(BlockFace.valueOf(ConfigManager.getString("position").toUpperCase()), ConfigManager.getInt("distance"));
return (faceBlock != null && faceBlock.getType() == Material.CHEST ? (Chest) faceBlock.getState() : null);
return (faceBlock != null && faceBlock.getType() == Material.CHEST ? new MinecraftChest((Chest) faceBlock.getState()) : null);
}
for(BlockFace bf : BlockFace.values()){
if(block.getFace(bf) != null && block.getFace(bf).getType() == Material.CHEST){
return (Chest) block.getFace(bf).getState();
return new MinecraftChest((Chest) block.getFace(bf).getState());
}
}
return null;
Expand Down Expand Up @@ -292,15 +297,16 @@ public static boolean checkFreeSpace(MinecartManiaChest chest,ItemStack is, int

//Checks if there is enough free space in inventory
public static boolean checkFreeSpace(Inventory inv, ItemStack is, int left){
ItemStack[] contents = inv.getContents();
return checkFreeSpace(inv.getContents(), is, left);
}
public static boolean checkFreeSpace(ItemStack[] contents, ItemStack is, int left){
Material type = is.getType();
short durability = is.getDurability();
int maxStack = is.getType().getMaxStackSize();
for(int i=0; i < inv.getSize(); i++){
for(ItemStack curitem : contents){
if(left <= 0){
return true;
}
ItemStack curitem = contents[i];
if(curitem == null){
left = left - maxStack;
continue;
Expand Down
19 changes: 19 additions & 0 deletions com/Acrobot/iConomyChestShop/Chests/ChestObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.Acrobot.iConomyChestShop.Chests;

import org.bukkit.inventory.ItemStack;

public interface ChestObject {
public ItemStack[] getContents();

public void setSlot(int slot, ItemStack item);
public void clearSlot(int slot);

public void addItem(ItemStack item, int amount);
public void removeItem(ItemStack item, int amount);

public int amount(ItemStack item);
public boolean hasEnough(ItemStack item, int amount);
public boolean fits(ItemStack item, int amount);

public int getSize();
}
77 changes: 77 additions & 0 deletions com/Acrobot/iConomyChestShop/Chests/GiftPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.Acrobot.iConomyChestShop.Chests;

import com.Acrobot.iConomyChestShop.Basic;
import com.Acrobot.iConomyChestShop.ConfigManager;
import com.aranai.virtualchest.VirtualChest;
import org.bukkit.inventory.ItemStack;

/**
* Compatibility with VirtualChests - future relases
* @author Acrobot
*/
public class GiftPost implements ChestObject{
private final VirtualChest chest;

public GiftPost(VirtualChest v){
this.chest = v;
}

public ItemStack[] getContents() {
return chest.getContents();
}

public void setSlot(int slot, ItemStack item) {
chest.setItem(slot, item);
}

public void clearSlot(int slot) {
chest.setItem(slot, null);
}

public void addItem(ItemStack item, int amount) {
item.setAmount(amount);
chest.addItem(item);
}

public void removeItem(ItemStack item, int amount) {
int type = item.getTypeId();
boolean checkDurability = true;
if(ConfigManager.getBoolean("allowUsedItemsToBeSold") && type >= 256 && type <= 317){
checkDurability = false;
}
for (int i = 0; i < chest.usedCases(); i++) {
ItemStack is = chest.getItem(i);
if (is != null) {
short durability = item.getDurability();
int isAmount = is.getAmount();
if (is.getTypeId() == type && (!checkDurability || durability == -1 || (is.getDurability() == durability))) {
if (isAmount - amount > 0) {
chest.setItem(i, new ItemStack(type, isAmount - amount, durability));
return;
} else if (isAmount - amount == 0) {
return;
} else {
amount -= isAmount;
chest.setItem(i, null);
}
}
}
}
}

public int amount(ItemStack item) {
return Basic.getItemAmount(chest.getContents(), item);
}

public boolean hasEnough(ItemStack item, int amount) {
return amount(item) >= amount;
}

public boolean fits(ItemStack item, int amount) {
return true;
}

public int getSize() {
return chest.usedCases();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Acrobot.iConomyChestShop;
package com.Acrobot.iConomyChestShop.Chests;

import com.Acrobot.iConomyChestShop.Basic;
import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaChest;
import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaDoubleChest;
import org.bukkit.block.Chest;
Expand All @@ -9,18 +10,23 @@
*
* @author Acrobot
*/
public class ChestObject {
private final MinecartManiaChest main;
private final MinecartManiaChest extended;
public class MinecraftChest implements ChestObject{
public final MinecartManiaChest main;
public final MinecartManiaChest extended;

ChestObject(Chest main){
public MinecraftChest(Chest main){
this.main = new MinecartManiaChest(main);
this.extended = this.main.getNeighborChest();
}

public boolean exists(){
return main != null;
}

private MinecartManiaDoubleChest doubleChest(){
return new MinecartManiaDoubleChest(main, extended);
}

public ItemStack[] getContents(){
if(extended != null){
return doubleChest().getContents();
Expand Down Expand Up @@ -71,7 +77,7 @@ public boolean hasEnough(ItemStack item, int amount){
return amount(item) >= amount;
}

public boolean hasFreeSpace(ItemStack item, int amount){
public boolean fits(ItemStack item, int amount){
if(extended != null){
return Basic.checkFreeSpace(main, item, amount) || Basic.checkFreeSpace(extended, item, amount);
}
Expand Down
4 changes: 2 additions & 2 deletions com/Acrobot/iConomyChestShop/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void buyingString(int amount, String item, String owner, Player pl
String str = getLanguage("You_bought_items");
str = str.replace("<amount>", amount + "");
str = str.replace("<item>", item);
str = str.replace("<owner>", owner);
str = str.replace("<owner>", owner.replace("[", ""));
str = str.replace("<cost>", formattedBalance(cost));

player.sendMessage(str);
Expand All @@ -60,7 +60,7 @@ public static void sellingString(int amount, String item, String owner, Player p
String str = getLanguage("You_sold_items");
str = str.replace("<amount>", amount + "");
str = str.replace("<item>", item);
str = str.replace("<owner>", owner);
str = str.replace("<owner>", owner.replace("[", ""));
str = str.replace("<cost>", formattedBalance(cost));

player.sendMessage(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static Block getNeightborChestBlock(Block block){
* @param x coordinate to search
* @param y coordinate to search
* @param z coordinate to search
* @return neighbor chest
*/
public static MinecartManiaChest getNeighborChest(World w, int x, int y, int z) {
if (MinecartManiaWorld.getBlockAt(w, x - 1, y, z).getTypeId() == Material.CHEST.getId()) {
Expand Down
26 changes: 21 additions & 5 deletions com/Acrobot/iConomyChestShop/Shop.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.Acrobot.iConomyChestShop;

import com.Acrobot.iConomyChestShop.Chests.ChestObject;
import com.Acrobot.iConomyChestShop.Chests.MinecraftChest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -67,8 +69,11 @@ public boolean buy(Player player){
if(!isAdminShop()){
chest.removeItem(stock, stockAmount);
EconomyManager.add(owner, getBuyPrice());
}else if(EconomyManager.hasAccount(owner)){
EconomyManager.add(owner, getBuyPrice());
}else{
String account = ConfigManager.getString("serverAccountName");
if(!account.equals("") && EconomyManager.hasAccount(account)){
EconomyManager.add(account, getSellPrice());
}
}
stock.setAmount(stockAmount);
Basic.addItemToInventory(player.getInventory(), stock, stockAmount);
Expand Down Expand Up @@ -102,8 +107,11 @@ public boolean sell(Player player){
}
chest.addItem(stock, stockAmount);
EconomyManager.substract(owner, getSellPrice());
}else if(EconomyManager.hasAccount(owner)){
EconomyManager.substract(owner, getSellPrice());
}else{
String account = ConfigManager.getString("serverAccountName");
if(!account.equals("") && EconomyManager.hasAccount(account)){
EconomyManager.substract(account, getSellPrice());
}
}

Basic.removeItemStackFromInventory(player.getInventory(), stock, stockAmount);
Expand All @@ -120,6 +128,14 @@ public boolean sell(Player player){
public boolean isAdminShop(){
return owner.toLowerCase().replace(" ", "").equals("adminshop");
}

public String shopOwnerName(){
if(isAdminShop()){
return ConfigManager.getString("serverAccountName");
}else{
return owner;
}
}
////////////////////////////////////////////////////


Expand Down Expand Up @@ -148,7 +164,7 @@ private boolean isBuying(){
}

private boolean hasFreeSpace(){
return chest.hasFreeSpace(stock, stockAmount);
return chest.fits(stock, stockAmount);
}

private boolean playerHasFreeSpace(Player player){
Expand Down
11 changes: 4 additions & 7 deletions com/Acrobot/iConomyChestShop/ShopManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.Acrobot.iConomyChestShop;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import com.Acrobot.iConomyChestShop.Chests.ChestObject;
import com.Acrobot.iConomyChestShop.Chests.MinecraftChest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
Expand All @@ -19,11 +17,10 @@ public class ShopManager extends PlayerListener {
public static void transaction(PlayerInteractEvent event) {
boolean reverseButtons = ConfigManager.getBoolean("reverseButtons");
Sign sign = (Sign) event.getClickedBlock().getState();
Chest chest = Basic.findChest(event.getClickedBlock());
Player player = event.getPlayer();
boolean adminShop = sign.getLine(0).toLowerCase().replace(" ", "").equals("adminshop");

Shop shop = new Shop(sign, (!adminShop ? new ChestObject(chest) : null));
Shop shop = new Shop(sign, (!adminShop ? Basic.findChest(event.getClickedBlock()) : null));

Action action = event.getAction();

Expand Down Expand Up @@ -69,7 +66,7 @@ public static void buy(Shop shop, Player player) {
}

public static void sell(Shop shop, Player player) {
if (EconomyManager.hasAccount(shop.owner) && !EconomyManager.hasEnough(shop.owner, shop.getSellPrice())) {
if (EconomyManager.hasAccount(shop.shopOwnerName()) && !EconomyManager.hasEnough(shop.shopOwnerName(), shop.getSellPrice())) {
player.sendMessage(ConfigManager.getLanguage("Seller_has_not_enough_money"));
return;
}
Expand Down
Loading

0 comments on commit 7da9232

Please sign in to comment.