Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Fix Item is suspended when the store is deleted
Browse files Browse the repository at this point in the history
Overlay item sale/recycling problem
Admin can break shop
  • Loading branch information
WetABQ authored Nov 11, 2018
1 parent a8d3abf commit e787a8f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
27 changes: 21 additions & 6 deletions src/main/java/io/wetabq/quickshop/QuickShop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cn.nukkit.entity.data.EntityMetadata
import cn.nukkit.item.Item
import cn.nukkit.level.Position
import cn.nukkit.network.protocol.AddItemEntityPacket
import cn.nukkit.network.protocol.RemoveEntityPacket
import cn.nukkit.plugin.PluginBase
import cn.nukkit.utils.TextFormat
import io.wetabq.quickshop.command.QuickShopCommand
Expand Down Expand Up @@ -77,17 +78,17 @@ class QuickShop : PluginBase(){
return pattern.matcher(str).matches()
}

fun addItemEntity(location: Position,item: Item) {
addItemEntity(Server.getInstance().onlinePlayers.values.toTypedArray(),location,item)
fun addItemEntity(location: Position,item: Item,eid : Long) {
addItemEntity(Server.getInstance().onlinePlayers.values.toTypedArray(),location,item,eid)
}

fun addItemEntity(player: Player,location: Position,item: Item) {
addItemEntity(arrayOf(player),location,item)
fun addItemEntity(player: Player,location: Position,item: Item,eid : Long) {
addItemEntity(arrayOf(player),location,item,eid)
}

fun addItemEntity(players: Array<Player>,location: Position,item: Item) {
fun addItemEntity(players: Array<Player>,location: Position,item: Item,eid : Long) {
val addItemEntityPacket = AddItemEntityPacket()
addItemEntityPacket.entityUniqueId = System.currentTimeMillis()
addItemEntityPacket.entityUniqueId = eid
addItemEntityPacket.entityRuntimeId = addItemEntityPacket.entityUniqueId
addItemEntityPacket.item = item
addItemEntityPacket.x = location.x.toInt().toFloat() + 0.5F
Expand All @@ -103,5 +104,19 @@ class QuickShop : PluginBase(){
.putFloat(Entity.DATA_SCALE, 4f)
Server.broadcastPacket(players, addItemEntityPacket)
}

fun removeItemEntity(eid : Long) {
removeItemEntity(Server.getInstance().onlinePlayers.values.toTypedArray(),eid)
}

fun removeItemEntity(player: Player,eid : Long) {
removeItemEntity(arrayOf(player),eid)
}

fun removeItemEntity(players: Array<Player>,eid : Long) {
val removeItemEntityPacket = RemoveEntityPacket()
removeItemEntityPacket.eid = eid
Server.broadcastPacket(players,removeItemEntityPacket)
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/wetabq/quickshop/config/LanguageConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class LanguageConfig : QuickShopConfig("language") {
"&cThat is not your own shop." to "",
"Successfully changed the shop type" to "",
"Successfully changed the shop price" to "",
"Successfully changed the shop unlimited[{}]" to ""
"Successfully changed the shop unlimited[{}]" to "",
"&cPlease enter the correct number" to ""
)

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CreateShopListener : Listener {
val pair = Shop.findShop(event.block)
val sign: BlockEntitySign? = pair?.first
if (sign != null && pair.second == null && event.item.id != Item.SIGN && event.item.id != 0) {
createShopPlayer[event.player.name] = Pair(System.currentTimeMillis() + 5000,event.block)
createShopPlayer[event.player.name] = Pair(System.currentTimeMillis() + 8000,event.block)
event.player.sendMessage(Lang.getMessage("Please enter the &e&lprice &r&ato set up your shop!"))
event.setCancelled()
}
Expand All @@ -53,7 +53,7 @@ class CreateShopListener : Listener {
Lang.getMessage("&aType: {}", arrayOf("BUY"),false),
Lang.getMessage("&eItem: {}", arrayOf(Lang.getItemName(item)),false),
Lang.getMessage("&c&lPrice: {}$ /count", arrayOf(event.message),false))
QuickShop.addItemEntity(chest, Item.get(item.id,item.damage,1))
QuickShop.addItemEntity(chest, Item.get(item.id,item.damage,1),(sign.x.toInt()+sign.z.toInt()).toLong())
player.sendMessage(Lang.getMessage("Successfully created the shop"))
Shop.createShop(player, chest, sign, event.message.toInt(), item, ShopType.BUY)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class InteractionShopListener : Listener {
"&d+------------------+"
,arrayOf(shopData.owner,Lang.getItemName(Item.get(shopData.itemId,shopData.itemMeta)),shopData.price,if(shopData.type == ShopType.BUY) "SELLING" else "BUYING")))
player.sendMessage(Lang.getMessage("Please enter the &l&ecount &r&ayou need to &d&l{}", arrayOf(if(shopData.type == ShopType.BUY) "BUY" else "SELL")))
interactShop[player.name] = Pair(System.currentTimeMillis() + 5000, shop)
interactShop[player.name] = Pair(System.currentTimeMillis() + 8000, shop)
}
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ class InteractionShopListener : Listener {
val key = "${block.x.toInt()}:${block.y.toInt()}:${block.z.toInt()}:${block.level.folderName}"
val shopData = QuickShop.instance.shopConfig.shopData[key]
if (shopData != null) {
if (shopData.owner == player.name) {
if (shopData.owner == player.name || player.isOp) {
player.sendMessage(Lang.getMessage("Successfully removed your shop"))

val e = PlayerRemoveShopEvent(player, shopData)
Expand All @@ -94,6 +94,7 @@ class InteractionShopListener : Listener {
return
}

QuickShop.removeItemEntity((shopData.signX+shopData.signZ).toLong())
QuickShop.instance.shopConfig.removeShop(key)
} else {
event.setCancelled()
Expand All @@ -106,7 +107,7 @@ class InteractionShopListener : Listener {
val key = "${sign.x.toInt()}:${sign.y.toInt()}:${sign.z.toInt()}:${sign.level.folderName}"
val shopData = QuickShop.instance.shopConfig.shopData[key]
if (shopData != null) {
if (shopData.owner == player.name) {
if (shopData.owner == player.name || player.isOp) {
player.sendMessage(Lang.getMessage("Successfully removed your shop"))

val e = PlayerRemoveShopEvent(player, shopData)
Expand All @@ -116,6 +117,7 @@ class InteractionShopListener : Listener {
return
}

QuickShop.removeItemEntity((shopData.signX+shopData.signZ).toLong())
QuickShop.instance.shopConfig.removeShop(key)
} else {
event.setCancelled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlayerListener : Listener {
@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {
for (shopData in QuickShop.instance.shopConfig.shopData.values) {
QuickShop.addItemEntity(event.player, Position(shopData.chestX.toDouble(),shopData.chestY.toDouble(),shopData.chestZ.toDouble()), Item.get(shopData.itemId,shopData.itemMeta,1))
QuickShop.addItemEntity(event.player, Position(shopData.chestX.toDouble(),shopData.chestY.toDouble(),shopData.chestZ.toDouble()), Item.get(shopData.itemId,shopData.itemMeta,1),(shopData.signX+shopData.signZ).toLong())
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/wetabq/quickshop/shop/BuyShop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import me.onebone.economyapi.EconomyAPI
class BuyShop(sign: String) : Shop(sign) {

fun buyItem(player: Player, count: Int) {
if (count <= 0) {
player.sendMessage(Lang.getMessage("&cPlease enter the correct number"))
return
}
val money = EconomyAPI.getInstance().myMoney(player.name)
val price = shopData.price * count
if (money >= price) {
if (this.getShopChest() != null) {
val item = Item.get(shopData.itemId, shopData.itemMeta, count)
if (shopData.unlimited || Shop.hasItem(this.getShopChest()!!.realInventory, item)) {
if (shopData.unlimited || Shop.getItemInInventory(this.getShopChest()!!.realInventory, item)!!.count >= count) {
if (shopData.unlimited || Shop.getItemInInventoryCount(this.getShopChest()!!.realInventory, item) >= count) {
if (!player.inventory.isFull && player.inventory.canAddItem(item)) {

val event = PlayerBuyEvent(player, shopData,count)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wetabq/quickshop/shop/SellShop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SellShop(sign: String) : Shop(sign) {
if (this.getShopChest() != null) {
val item = Item.get(shopData.itemId, shopData.itemMeta, count)
if (Shop.hasItem(player.inventory, item)) {
if (Shop.getItemInInventory(player.inventory, item)!!.count >= count) {
if (Shop.getItemInInventoryCount(player.inventory, item) >= count) {
if (!player.inventory.isFull && player.inventory.canAddItem(item)) {

val event = PlayerSellEvent(player, shopData,count)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/wetabq/quickshop/shop/Shop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ abstract class Shop(sign: String) {
flag = true
}
}
//baseInventory.clearAll()
return flag
}

Expand All @@ -76,6 +77,17 @@ abstract class Shop(sign: String) {
return null
}

@JvmStatic
fun getItemInInventoryCount(baseInventory: BaseInventory,item: Item): Int {
var count = 0
baseInventory.slots.values.forEach {i ->
if (i.id == item.id && i.damage == item.damage) {
count += i.count
}
}
return count
}

@JvmStatic
fun createShop(owner: Player, chest: Position, sign: Position, price: Int, item: Item,type: Int) {
val shopData = ShopData(owner.name,type,price,chest.x.toInt(),chest.y.toInt(),chest.z.toInt(),sign.x.toInt(),sign.z.toInt(),chest.level.folderName,item.id,item.damage,false)
Expand Down

0 comments on commit e787a8f

Please sign in to comment.