0.1
This commit is contained in:
parent
04dd29edec
commit
10bf8c7123
9
pom.xml
9
pom.xml
|
@ -35,11 +35,18 @@
|
|||
<groupId>com.io.yutian</groupId>
|
||||
<artifactId>pixelpaper</artifactId>
|
||||
<version>1.18.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.io.yutian</groupId>
|
||||
<artifactId>AuLib</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.7</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.milkbowl</groupId>
|
||||
<artifactId>Vault</artifactId>
|
||||
<version>1.7.3</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -6,11 +6,18 @@ import com.io.yutian.auchestshop.listener.ShopListener;
|
|||
import com.io.yutian.auchestshop.manager.InteractiveManager;
|
||||
import com.io.yutian.auchestshop.manager.ShopManager;
|
||||
import com.io.yutian.aulib.lang.Lang;
|
||||
import net.milkbowl.vault.Vault;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class AuChestShop extends JavaPlugin {
|
||||
|
||||
private static AuChestShop instance;
|
||||
private static Economy economy;
|
||||
|
||||
private static SQLIO sqlIO;
|
||||
private static ShopManager shopManager;
|
||||
|
@ -21,6 +28,17 @@ public class AuChestShop extends JavaPlugin {
|
|||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
RegisteredServiceProvider<Economy> economyProvider = null;
|
||||
try {
|
||||
economyProvider = Bukkit.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (economyProvider != null) {
|
||||
economy = economyProvider.getProvider();
|
||||
}
|
||||
|
||||
sqlIO = new SQLIO();
|
||||
commandManager = new CommandManager();
|
||||
shopManager = new ShopManager();
|
||||
|
@ -37,6 +55,7 @@ public class AuChestShop extends JavaPlugin {
|
|||
e.printStackTrace();
|
||||
}
|
||||
shopManager.loadAll(sqlIO);
|
||||
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
|
@ -56,6 +75,10 @@ public class AuChestShop extends JavaPlugin {
|
|||
return sqlIO;
|
||||
}
|
||||
|
||||
public static Economy getEconomy() {
|
||||
return economy;
|
||||
}
|
||||
|
||||
public static ShopManager getShopManager() {
|
||||
return shopManager;
|
||||
}
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
package com.io.yutian.auchestshop.command.subs;
|
||||
|
||||
import com.io.yutian.auchestshop.AuChestShop;
|
||||
import com.io.yutian.auchestshop.shop.Shop;
|
||||
import com.io.yutian.auchestshop.shop.ShopLocation;
|
||||
import com.io.yutian.auchestshop.shop.ShopType;
|
||||
import com.io.yutian.auchestshop.shop.log.ShopLog;
|
||||
import com.io.yutian.auchestshop.util.ItemStackUtil;
|
||||
import com.io.yutian.aulib.command.CommandContext;
|
||||
import com.io.yutian.aulib.command.ICommand;
|
||||
import com.io.yutian.aulib.command.argument.Argument;
|
||||
import com.io.yutian.aulib.command.argument.ArgumentTypes;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandTest extends ICommand {
|
||||
|
||||
|
@ -28,23 +26,11 @@ public class CommandTest extends ICommand {
|
|||
public void executes(CommandContext commandContext) {
|
||||
CommandSender sender = commandContext.getSender();
|
||||
String type = commandContext.getArgumentsValue("type").getString();
|
||||
if (type.equalsIgnoreCase("create")) {
|
||||
Shop shop = new Shop(UUID.randomUUID(), UUID.randomUUID(), new ShopLocation("world", 0, 10, 0), null, 10, ShopType.BUYING, false, new ShopLog());
|
||||
shops.add(shop);
|
||||
sender.sendMessage("Shop created!");
|
||||
} else if (type.equalsIgnoreCase("save")) {
|
||||
AuChestShop.getSQLIO().saveAllShops(shops);
|
||||
sender.sendMessage("Shop saved!");
|
||||
} else if (type.equalsIgnoreCase("list")) {
|
||||
System.out.println(shops);
|
||||
for (Shop shop : shops) {
|
||||
sender.sendMessage(shop.getUUID());
|
||||
sender.sendMessage(shop.getOwner());
|
||||
}
|
||||
} else if (type.equalsIgnoreCase("load")) {
|
||||
shops = AuChestShop.getSQLIO().loadAllShops();
|
||||
System.out.println(shops);
|
||||
sender.sendMessage("Loaded all shops!");
|
||||
if (type.equalsIgnoreCase("eq")) {
|
||||
Player player = (Player) sender;
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
ItemStack itemStack1 = player.getInventory().getItemInOffHand();
|
||||
sender.sendMessage("result: "+ItemStackUtil.matches(itemStack, itemStack1));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ public class SQLIO {
|
|||
.addColumn("data", "TEXT NOT NULL")
|
||||
.setIndex("uuid", IndexType.PRIMARY_KEY)
|
||||
.setTableSettings("")
|
||||
.build().execute(null);
|
||||
.build().executeAsync(null);
|
||||
}
|
||||
|
||||
public List<Shop> loadAllShops() {
|
||||
|
@ -76,7 +76,7 @@ public class SQLIO {
|
|||
.addCondition("uuid", shop.getUUID().toString())
|
||||
.setColumnValues("owner", shop.getOwner().toString())
|
||||
.setColumnValues("data", dataJson.toString())
|
||||
.build().execute();
|
||||
.build().executeAsync();
|
||||
} else {
|
||||
insertedShops.add(shop);
|
||||
}
|
||||
|
@ -103,10 +103,8 @@ public class SQLIO {
|
|||
JSONObject dataJson = SerializeHelper.serialize(shop);
|
||||
sqlManager.createUpdate("shops")
|
||||
.addCondition("uuid", shop.getUUID().toString())
|
||||
.setLimit(1)
|
||||
.setColumnValues("owner", shop.getOwner().toString())
|
||||
.setColumnValues("data", dataJson.toString())
|
||||
.build().execute();
|
||||
.build().executeAsync();
|
||||
} else {
|
||||
JSONObject dataJson = SerializeHelper.serialize(shop);
|
||||
sqlManager.createInsert("shops").setIgnore(false).setColumnNames("uuid", "owner", "data").setParams(new Object[]{shop.getUUID().toString(), shop.getOwner().toString(), dataJson.toString()}).executeAsync();
|
||||
|
@ -117,6 +115,18 @@ public class SQLIO {
|
|||
}
|
||||
}
|
||||
|
||||
public void deleteShop(Shop shop) {
|
||||
try (SQLQuery sqlQuery = sqlManager.createQuery().inTable("shops").addCondition("uuid", shop.getUUID().toString()).setLimit(1).build().execute()) {
|
||||
try (ResultSet resultSet = sqlQuery.getResultSet()) {
|
||||
if (resultSet.next()) {
|
||||
sqlManager.createDelete("shops").addCondition("uuid", shop.getUUID().toString()).build().executeAsync();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (sqlManager == null) {
|
||||
return;
|
||||
|
|
|
@ -38,14 +38,15 @@ public class CreateShopGui extends Gui {
|
|||
addButton(11, new Button(new ItemStackBuilder(Material.DIAMOND).setDisplayName("§a出售").build()).click((player1, clickType) -> {
|
||||
created = true;
|
||||
AuChestShop.getShopManager().createShop(player1, block, ShopType.SELLING);
|
||||
player1.sendMessage(Lang.get(""));
|
||||
player1.closeInventory();
|
||||
player.sendMessage(Lang.get("auchestshop.create-shop.success"));
|
||||
runnable.run();
|
||||
}));
|
||||
addButton(15, new Button(new ItemStackBuilder(Material.EMERALD).setDisplayName("§a收购").build()).click((player1, clickType) -> {
|
||||
created = true;
|
||||
AuChestShop.getShopManager().createShop(player1, block, ShopType.BUYING);
|
||||
player1.closeInventory();
|
||||
player.sendMessage(Lang.get("auchestshop.create-shop.success"));
|
||||
runnable.run();
|
||||
}));
|
||||
}
|
||||
|
@ -56,6 +57,5 @@ public class CreateShopGui extends Gui {
|
|||
block.setType(Material.AIR);
|
||||
PlayerInventoryUtil.giveItemStack(player, itemStack);
|
||||
}
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,9 @@ import com.io.yutian.auchestshop.AuChestShop;
|
|||
import com.io.yutian.auchestshop.gui.CreateShopGui;
|
||||
import com.io.yutian.auchestshop.shop.Shop;
|
||||
import com.io.yutian.auchestshop.util.ExpiringSet;
|
||||
import com.io.yutian.auchestshop.util.ShopUtil;
|
||||
import com.io.yutian.aulib.lang.Lang;
|
||||
import com.io.yutian.aulib.listener.IListener;
|
||||
import com.io.yutian.aulib.nbt.NBTItem;
|
||||
import com.io.yutian.aulib.nbt.NBTString;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
|
@ -48,8 +48,9 @@ public class ShopListener extends IListener {
|
|||
if (!AuChestShop.getInteractiveManager().containsKey(uuid)) {
|
||||
return;
|
||||
}
|
||||
AuChestShop.getInteractiveManager().handler(uuid, event.getMessage());
|
||||
AuChestShop.getInteractiveManager().handler(player, event.getMessage());
|
||||
event.setCancelled(true);
|
||||
AuChestShop.getInteractiveManager().remove(player.getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
|
@ -57,10 +58,13 @@ public class ShopListener extends IListener {
|
|||
Player player = event.getPlayer();
|
||||
Block block = event.getBlock();
|
||||
ItemStack itemStack = event.getItemInHand();
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
if (nbtItem.has("item_type", NBTString.TYPE_ID)) {
|
||||
String itemType = (String) nbtItem.get("item_type").getValue();
|
||||
if (itemType.equals("chest_shop")) {
|
||||
if (ShopUtil.isShopItem(itemStack)) {
|
||||
Block block1 = block.getRelative(BlockFace.UP, 1);
|
||||
if (!block1.getType().equals(Material.AIR)) {
|
||||
player.sendMessage(Lang.get("auchestshop.create-shop.up-has-block"));
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
CreateShopGui createShopGui = new CreateShopGui(player, block, itemStack, ()->{
|
||||
cachedLocations.remove(block.getLocation());
|
||||
});
|
||||
|
@ -68,7 +72,6 @@ public class ShopListener extends IListener {
|
|||
cachedLocations.add(block.getLocation());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
|
@ -76,7 +79,25 @@ public class ShopListener extends IListener {
|
|||
Block block = event.getBlock();
|
||||
if (cachedLocations.contains(block.getLocation())) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
Pair<Shop, ClickType> shopSearched = searchShop(player, block);
|
||||
if (shopSearched.key() == null || shopSearched.value() == ClickType.AIR) {
|
||||
return;
|
||||
}
|
||||
ClickType clickType = shopSearched.value();
|
||||
if (clickType == ClickType.LOWER) {
|
||||
Block block1 = block.getRelative(BlockFace.UP, 1);
|
||||
block1.setType(Material.AIR);
|
||||
} else if (clickType == ClickType.UPPER) {
|
||||
Block block1 = block.getRelative(BlockFace.DOWN, 1);
|
||||
block.setType(Material.AIR);
|
||||
block1.breakNaturally(true);
|
||||
event.setCancelled(true);
|
||||
}
|
||||
Shop shop = shopSearched.key();
|
||||
AuChestShop.getShopManager().removeShop(shop);
|
||||
player.sendMessage(Lang.get("auchestshop.remove-shop"));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
|
@ -127,7 +148,7 @@ public class ShopListener extends IListener {
|
|||
}
|
||||
Shop shop = shopSearched.key();
|
||||
ClickType clickType = shopSearched.value();
|
||||
System.out.println(shop);
|
||||
AuChestShop.getShopManager().handleInteract(player, event, shop, clickType);
|
||||
}
|
||||
|
||||
private Pair<Shop, ClickType> searchShop(Player player, Block block) {
|
||||
|
@ -154,7 +175,7 @@ public class ShopListener extends IListener {
|
|||
return Pair.of(shop, ClickType.LOWER);
|
||||
}
|
||||
|
||||
enum ClickType {
|
||||
public enum ClickType {
|
||||
UPPER,
|
||||
LOWER,
|
||||
AIR;
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package com.io.yutian.auchestshop.manager;
|
||||
|
||||
import com.io.yutian.auchestshop.AuChestShop;
|
||||
import com.io.yutian.auchestshop.shop.Shop;
|
||||
import com.io.yutian.auchestshop.shop.ShopAction;
|
||||
import com.io.yutian.auchestshop.shop.ShopInteractiveInfo;
|
||||
import com.io.yutian.aulib.lang.Lang;
|
||||
import com.io.yutian.aulib.util.StringUtil;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -10,10 +17,47 @@ public class InteractiveManager {
|
|||
|
||||
private final Map<UUID, ShopInteractiveInfo> actions = new HashMap<>();
|
||||
|
||||
public void handler(UUID uuid, String message) {
|
||||
ShopInteractiveInfo info = actions.get(uuid);
|
||||
public void handler(Player player, String message) {
|
||||
if (message.equalsIgnoreCase("cancel") || message.equalsIgnoreCase("取消")) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.cancel"));
|
||||
return;
|
||||
}
|
||||
ShopInteractiveInfo info = actions.get(player.getUniqueId());
|
||||
ShopAction action = info.getAction();
|
||||
String arg = message.trim();
|
||||
Shop shop = info.getShop();
|
||||
if (AuChestShop.getShopManager().getShop(shop.getUUID()) == null) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.cancel"));
|
||||
return;
|
||||
}
|
||||
if (action == ShopAction.CREATE) {
|
||||
if (!StringUtil.isDouble(arg)) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.error-1"));
|
||||
return;
|
||||
}
|
||||
double i = Double.parseDouble(arg);
|
||||
if (i <= 0) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.error-2"));
|
||||
return;
|
||||
}
|
||||
int length = arg.contains(".") ? arg.length() - arg.indexOf(".") + 1 : 0;
|
||||
if (length >= 3) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.error-3"));
|
||||
return;
|
||||
}
|
||||
if (i >= Double.MAX_VALUE) {
|
||||
player.sendMessage(Lang.get("auchestshop.action.error-4"));
|
||||
return;
|
||||
}
|
||||
ItemStack itemStack = info.getItemStack();
|
||||
shop.setItem(itemStack);
|
||||
shop.setPrice(i);
|
||||
AuChestShop.getSQLIO().saveShop(shop);
|
||||
player.sendMessage(Lang.get("auchestshop.action.success-create"));
|
||||
} else if (action == ShopAction.PURCHASE) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public ShopInteractiveInfo put(UUID uuid, ShopInteractiveInfo info) {
|
||||
return this.actions.put(uuid, info);
|
||||
|
|
|
@ -4,17 +4,24 @@ import com.google.common.collect.Maps;
|
|||
import com.google.common.collect.Sets;
|
||||
import com.io.yutian.auchestshop.AuChestShop;
|
||||
import com.io.yutian.auchestshop.database.SQLIO;
|
||||
import com.io.yutian.auchestshop.shop.Shop;
|
||||
import com.io.yutian.auchestshop.shop.ShopChunk;
|
||||
import com.io.yutian.auchestshop.shop.ShopLocation;
|
||||
import com.io.yutian.auchestshop.shop.ShopType;
|
||||
import com.io.yutian.auchestshop.listener.ShopListener;
|
||||
import com.io.yutian.auchestshop.shop.*;
|
||||
import com.io.yutian.auchestshop.util.ItemStackUtil;
|
||||
import com.io.yutian.auchestshop.util.ShopUtil;
|
||||
import com.io.yutian.aulib.lang.Lang;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.data.type.Chest;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -40,9 +47,52 @@ public class ShopManager {
|
|||
Shop shop = new Shop(player.getUniqueId(), shopLocation, shopType);
|
||||
addShop(shop);
|
||||
AuChestShop.getSQLIO().saveShop(shop);
|
||||
Block block1 = block.getRelative(BlockFace.UP, 1);
|
||||
block1.setType(block.getType());
|
||||
Chest chest = (Chest) block1.getState().getBlockData();
|
||||
chest.setDoubleBlockHalf(Chest.ChestDoubleBlockHalfType.UPPER);
|
||||
chest.setShapeLevel(12);
|
||||
block1.setBlockData(chest);
|
||||
Chest chest1 = (Chest) block.getState().getBlockData();
|
||||
chest1.setDoubleBlockHalf(Chest.ChestDoubleBlockHalfType.LOWER);
|
||||
chest1.setShapeLevel(12);
|
||||
block.setBlockData(chest1);
|
||||
return shop;
|
||||
}
|
||||
|
||||
public void handleInteract(Player player, PlayerInteractEvent event, Shop shop, ShopListener.ClickType clickType) {
|
||||
Action action = event.getAction();
|
||||
if (action == Action.LEFT_CLICK_BLOCK) {
|
||||
if (shop.getItem() == null) {
|
||||
if (event.getItem() != null && !event.getItem().getType().equals(Material.AIR)) {
|
||||
ShopInteractiveInfo interactiveInfo = new ShopInteractiveInfo(event.getItem().clone(), shop.getLocation(), shop, ShopAction.CREATE, clickType, System.currentTimeMillis());
|
||||
ItemStack item = event.getItem().clone();
|
||||
player.sendMessage(Lang.get("auchestshop.action.message-1", ItemStackUtil.getItemName(item)));
|
||||
AuChestShop.getInteractiveManager().put(player.getUniqueId(), interactiveInfo);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
ItemStack item = shop.getItem();
|
||||
Inventory inventory = shop.getInventory();
|
||||
int i = shop.getShopType() == ShopType.BUYING ? ItemStackUtil.countSpace(inventory, item) : ItemStackUtil.countItems(inventory, item);
|
||||
player.sendMessage(Lang.get("auchestshop.action.message-" + (shop.getShopType() == ShopType.BUYING ? 2 : 3), ItemStackUtil.getItemName(item), i));
|
||||
ShopInteractiveInfo interactiveInfo = new ShopInteractiveInfo(null, shop.getLocation(), shop, ShopAction.PURCHASE, clickType, System.currentTimeMillis());
|
||||
AuChestShop.getInteractiveManager().put(player.getUniqueId(), interactiveInfo);
|
||||
} else if (action == Action.RIGHT_CLICK_BLOCK) {
|
||||
if (player.isSneaking() && shop.getOwner().equals(player.getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
ItemStack item = shop.getItem();
|
||||
Inventory inventory = shop.getInventory();
|
||||
int i = shop.getShopType() == ShopType.BUYING ? ItemStackUtil.countSpace(inventory, item) : ItemStackUtil.countItems(inventory, item);
|
||||
player.sendMessage(Lang.get("auchestshop.action.message-" + (shop.getShopType() == ShopType.BUYING ? 2 : 3), ItemStackUtil.getItemName(item), i));
|
||||
ShopInteractiveInfo interactiveInfo = new ShopInteractiveInfo(null, shop.getLocation(), shop, ShopAction.PURCHASE, clickType, System.currentTimeMillis());
|
||||
AuChestShop.getInteractiveManager().put(player.getUniqueId(), interactiveInfo);
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull List<Shop> getAllShops() {
|
||||
List<Shop> shopsCollected = new ArrayList<>();
|
||||
for (Map<ShopChunk, Map<ShopLocation, Shop>> shopMapData : getShops().values()) {
|
||||
|
@ -146,7 +196,14 @@ public class ShopManager {
|
|||
}
|
||||
|
||||
public void removeShop(@NotNull Shop shop) {
|
||||
ShopLocation location = shop.getLocation();
|
||||
ShopChunk shopChunk = ShopChunk.fromLocation(location);
|
||||
final Map<ShopLocation, Shop> inChunk = getShops(shopChunk);
|
||||
if (inChunk != null) {
|
||||
inChunk.remove(location);
|
||||
}
|
||||
this.loadedShops.remove(shop);
|
||||
AuChestShop.getSQLIO().deleteShop(shop);
|
||||
}
|
||||
|
||||
public Set<Shop> getLoadedShops() {
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package com.io.yutian.auchestshop.shop;
|
||||
|
||||
import com.io.yutian.auchestshop.shop.log.ShopLog;
|
||||
import com.io.yutian.aulib.serialize.SerializeIgnore;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Objects;
|
||||
|
@ -17,6 +24,11 @@ public class Shop {
|
|||
private boolean unlimited;
|
||||
private ShopLog shopLog;
|
||||
|
||||
@SerializeIgnore
|
||||
protected Location chestLocation;
|
||||
@SerializeIgnore
|
||||
protected Block chestBlock;
|
||||
|
||||
public Shop(UUID owner, ShopLocation location, ShopType shopType) {
|
||||
this(UUID.randomUUID(), owner, location, null, 0, shopType, false, new ShopLog());
|
||||
}
|
||||
|
@ -30,6 +42,33 @@ public class Shop {
|
|||
this.shopType = shopType;
|
||||
this.unlimited = unlimited;
|
||||
this.shopLog = shopLog;
|
||||
updateChest();
|
||||
}
|
||||
|
||||
public void updateChest() {
|
||||
World world = Bukkit.getWorld(location.getWorld());
|
||||
this.chestLocation = new Location(world, location.getX(), location.getY(), location.getZ());
|
||||
this.chestBlock = this.chestLocation.getBlock();
|
||||
}
|
||||
|
||||
public Inventory getInventory() {
|
||||
if (chestLocation == null) {
|
||||
updateChest();
|
||||
}
|
||||
Chest chest = (Chest) chestBlock.getState();
|
||||
return chest.getInventory();
|
||||
}
|
||||
|
||||
public int getInventoryCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setItem(ItemStack item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public UUID getUUID() {
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package com.io.yutian.auchestshop.shop;
|
||||
|
||||
public enum ShopAction {
|
||||
|
||||
PURCHASE,
|
||||
CREATE;
|
||||
|
||||
}
|
|
@ -38,6 +38,9 @@ public class ShopChunk {
|
|||
return equals(world.getName(), x, z);
|
||||
}
|
||||
|
||||
public static ShopChunk fromLocation(@NotNull ShopLocation location) {
|
||||
return new ShopChunk(location.getWorld(), location.getX() >> 4, location.getZ() >> 4);
|
||||
}
|
||||
|
||||
public static ShopChunk fromLocation(@NotNull Location location) {
|
||||
return new ShopChunk(location.getWorld().getName(), location.getBlockX() >> 4, location.getBlockZ() >> 4);
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
package com.io.yutian.auchestshop.shop;
|
||||
|
||||
import org.bukkit.event.block.Action;
|
||||
import com.io.yutian.auchestshop.listener.ShopListener;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ShopInteractiveInfo {
|
||||
|
||||
private ItemStack itemStack;
|
||||
private ShopLocation shopLocation;
|
||||
private Action action;
|
||||
private Shop shop;
|
||||
private ShopAction action;
|
||||
private ShopListener.ClickType clickType;
|
||||
private long time;
|
||||
|
||||
public ShopInteractiveInfo(ItemStack itemStack, ShopLocation shopLocation, Action action, long time) {
|
||||
public ShopInteractiveInfo(ItemStack itemStack, ShopLocation shopLocation, Shop shop, ShopAction action, ShopListener.ClickType clickType, long time) {
|
||||
this.itemStack = itemStack;
|
||||
this.shopLocation = shopLocation;
|
||||
this.shop = shop;
|
||||
this.action = action;
|
||||
this.clickType = clickType;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
|
@ -25,10 +29,18 @@ public class ShopInteractiveInfo {
|
|||
return shopLocation;
|
||||
}
|
||||
|
||||
public Action getAction() {
|
||||
public Shop getShop() {
|
||||
return shop;
|
||||
}
|
||||
|
||||
public ShopAction getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public ShopListener.ClickType getClickType() {
|
||||
return clickType;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package com.io.yutian.auchestshop.util;
|
||||
|
||||
import com.io.yutian.aulib.nbt.NBTCompound;
|
||||
import com.io.yutian.aulib.nbt.NBTItem;
|
||||
import com.io.yutian.aulib.util.LangUtil;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ItemStackUtil {
|
||||
|
||||
public static boolean matches(@Nullable ItemStack original, @Nullable ItemStack tester) {
|
||||
if (original == null || tester == null) {
|
||||
return false;
|
||||
}
|
||||
original = original.clone();
|
||||
original.setAmount(1);
|
||||
tester = tester.clone();
|
||||
tester.setAmount(1);
|
||||
if (!original.isSimilar(tester)) {
|
||||
return false;
|
||||
}
|
||||
NBTItem nbtOriginal = new NBTItem(original);
|
||||
NBTItem nbtTester = new NBTItem(tester);
|
||||
NBTCompound nbtCompound0 = nbtOriginal.getTag();
|
||||
NBTCompound nbtCompound1 = nbtTester.getTag();
|
||||
return nbtCompound0.equals(nbtCompound1);
|
||||
}
|
||||
|
||||
public static int countItems(Inventory inventory, @NotNull ItemStack item) {
|
||||
if (inventory == null) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (ItemStack itemStack : inventory) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
if (matches(item, itemStack)) {
|
||||
count+= itemStack.getAmount();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int countSpace(Inventory inventory, @NotNull ItemStack item) {
|
||||
if (inventory == null) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
int itemMaxStackSize = item.getMaxStackSize();
|
||||
for (ItemStack itemStack : inventory) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
count+=itemMaxStackSize;
|
||||
} else if (matches(item, itemStack)) {
|
||||
count+=itemStack.getAmount() >= itemMaxStackSize ? 0 : itemMaxStackSize - itemStack.getAmount();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static String getItemName(ItemStack itemStack) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta != null && meta.hasDisplayName()) {
|
||||
return meta.getDisplayName();
|
||||
}
|
||||
Material material = itemStack.getType();
|
||||
String id = material.name().toLowerCase();
|
||||
boolean isBlock = material.isBlock();
|
||||
String prefix = isBlock ? "block" : "item";
|
||||
return LangUtil.getLang(prefix+".minecraft."+id);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,13 @@
|
|||
package com.io.yutian.auchestshop.util;
|
||||
|
||||
import com.io.yutian.aulib.nbt.NBTItem;
|
||||
import com.io.yutian.aulib.nbt.NBTString;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Tag;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.Directional;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
@ -18,6 +21,15 @@ public class ShopUtil {
|
|||
SHOPABLES.add(Material.TRAPPED_CHEST);
|
||||
}
|
||||
|
||||
public static boolean isShopItem(ItemStack itemStack) {
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
if (!nbtItem.has("item_type", NBTString.TYPE_ID)) {
|
||||
return false;
|
||||
}
|
||||
String itemType = (String) nbtItem.get("item_type").getValue();
|
||||
return itemType.equalsIgnoreCase("chest_shop");
|
||||
}
|
||||
|
||||
public static Block getAttached(@NotNull Block block) {
|
||||
BlockData blockData = block.getBlockData();
|
||||
if (blockData instanceof final Directional directional) {
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
auchestshop:
|
||||
create-shop:
|
||||
success: "成功创建商店"
|
||||
up-has-block: "§c创建失败,商店上方存在非法方块"
|
||||
success: "§a成功创建商店"
|
||||
error-1: "§c商店创建已取消"
|
||||
remove-shop: "§a成功移除商店"
|
||||
action:
|
||||
cancel: "§a操作已取消"
|
||||
message-1: "§a请在聊天中输入交易1个§e$0§a所需的价格。"
|
||||
message-2: "§a聊天栏中输入想出售§e$0§a的物品数量,您现在可以卖出§e$1§a件物品。输入§ball§a来出售全部物品"
|
||||
message-3: "§a聊天栏中输入想购买§e$0§a的物品数量,您可以购买§e$1§a件物品。输入§ball§a来购买全部物品"
|
||||
error-1: "§c请输入有效的数字"
|
||||
error-2: "§c请输入大于0的整数"
|
||||
error-3: "§c您的价格中的小数点后位数超过上限"
|
||||
error-4: "§c该值超过了最大值"
|
||||
error-5: "§c请输入一个有效的价格"
|
||||
success-create: "§a成功设置商店物品"
|
Loading…
Reference in New Issue
Block a user