Skip to content

Commit 4b1d235

Browse files
committed
New shop info sign storage method
1 parent 085e371 commit 4b1d235

File tree

5 files changed

+171
-30
lines changed

5 files changed

+171
-30
lines changed

src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ public class ContainerShop implements Shop {
6363
@JsonUtil.Hidden
6464
@EqualsAndHashCode.Exclude
6565
private static final String SHOP_SIGN_PREFIX = "§d§o §r";
66-
@EqualsAndHashCode.Exclude
67-
@JsonUtil.Hidden
68-
private static final String SHOP_SIGN_PATTERN = "§d§o ";
66+
6967
@NotNull
7068
private final Location location;
7169
private final YamlConfiguration extra;
@@ -1117,7 +1115,6 @@ public void setShopType(@NotNull ShopType newShopType) {
11171115
if (b == null) {
11181116
continue;
11191117
}
1120-
11211118
BlockState state = PaperLib.getBlockState(b, false).getState();
11221119
if (!(state instanceof Sign)) {
11231120
continue;
@@ -1126,37 +1123,25 @@ public void setShopType(@NotNull ShopType newShopType) {
11261123
continue;
11271124
}
11281125
Sign sign = (Sign) state;
1129-
String[] lines = sign.getLines();
1130-
if (lines[0].isEmpty() && lines[1].isEmpty() && lines[2].isEmpty() && lines[3].isEmpty()) {
1131-
signs.add(sign); //NEW SIGN
1126+
if(!isShopSign(sign)){
11321127
continue;
11331128
}
1134-
1135-
if (lines[1].startsWith(SHOP_SIGN_PATTERN)) {
1136-
signs.add(sign);
1137-
} else {
1138-
String header = lines[0];
1139-
String adminShopHeader = plugin.text().of("signs.header", plugin.text().of("admin-shop").forLocale()).forLocale();
1140-
String signHeaderUsername = plugin.text().of("signs.header", this.ownerName(true)).forLocale();
1141-
if (header.contains(adminShopHeader) || header.contains(signHeaderUsername)) {
1142-
signs.add(sign);
1143-
//TEXT SIGN
1144-
//continue
1145-
} else {
1146-
adminShopHeader = plugin.text().of("signs.header", plugin.text().of("admin-shop").forLocale(), "").forLocale();
1147-
signHeaderUsername = plugin.text().of("signs.header", this.ownerName(true), "").forLocale();
1148-
adminShopHeader = ChatColor.stripColor(adminShopHeader).trim();
1149-
signHeaderUsername = ChatColor.stripColor(signHeaderUsername).trim();
1150-
if (header.contains(adminShopHeader) || header.contains(signHeaderUsername)) {
1151-
signs.add(sign);
1152-
}
1153-
}
1154-
}
1129+
claimShopSign(sign);
1130+
signs.add(sign);
11551131
}
11561132

11571133
return signs;
11581134
}
11591135

1136+
private ShopSignStorage saveToShopSignStorage(){
1137+
return new ShopSignStorage(getLocation().getWorld().getName(),getLocation().getBlockX(),getLocation().getBlockY(),getLocation().getBlockZ());
1138+
}
1139+
1140+
@Override
1141+
public void claimShopSign(@NotNull Sign sign){
1142+
sign.getPersistentDataContainer().set(SHOP_NAMESPACED_KEY,ShopSignPersistentDataType.INSTANCE,saveToShopSignStorage());
1143+
}
1144+
11601145
/**
11611146
* @return The list of players who can manage the shop.
11621147
*/
@@ -1562,6 +1547,6 @@ public void openPreview(@NotNull Player player) {
15621547

15631548
@Override
15641549
public ShopInfoStorage saveToInfoStorage() {
1565-
return new ShopInfoStorage(BlockPosition.of(getLocation()), ShopModerator.serialize(getModerator()), getPrice(), Util.serialize(getItem()), isUnlimited() ? 1 : 0, getShopType().toID(), saveExtraToYaml(), getCurrency(), isDisableDisplay(), getTaxAccount());
1550+
return new ShopInfoStorage(getLocation().getWorld().getName(),BlockPosition.of(getLocation()), ShopModerator.serialize(getModerator()), getPrice(), Util.serialize(getItem()), isUnlimited() ? 1 : 0, getShopType().toID(), saveExtraToYaml(), getCurrency(), isDisableDisplay(), getTaxAccount());
15661551
}
15671552
}

src/main/java/org/maxgamer/quickshop/shop/Shop.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
package org.maxgamer.quickshop.shop;
2121

22+
import org.bukkit.ChatColor;
2223
import org.bukkit.Location;
24+
import org.bukkit.NamespacedKey;
2325
import org.bukkit.block.Block;
2426
import org.bukkit.block.Sign;
2527
import org.bukkit.configuration.ConfigurationSection;
@@ -29,12 +31,16 @@
2931
import org.bukkit.plugin.Plugin;
3032
import org.jetbrains.annotations.NotNull;
3133
import org.jetbrains.annotations.Nullable;
34+
import org.maxgamer.quickshop.QuickShop;
3235
import org.maxgamer.quickshop.util.ComponentPackge;
3336

3437
import java.util.List;
38+
import java.util.Objects;
3539
import java.util.UUID;
3640

3741
public interface Shop {
42+
NamespacedKey SHOP_NAMESPACED_KEY = new NamespacedKey(QuickShop.getInstance(),"shopsign");
43+
String SHOP_SIGN_PATTERN = "§d§o ";
3844
/**
3945
* Add x ItemStack to the shop inventory
4046
*
@@ -535,5 +541,63 @@ default List<ComponentPackge> getSignText(String locale) {
535541
*/
536542
void setTaxAccount(@Nullable UUID taxAccount);
537543

544+
/**
545+
* Claim a sign as shop sign (modern method)
546+
* @param sign The shop sign
547+
*/
548+
void claimShopSign(@NotNull Sign sign);
549+
550+
/**
551+
* Checks if a Sign is a ShopSign
552+
* @param sign Target sign
553+
* @return Is shop info sign
554+
*/
555+
default boolean isShopSign(@NotNull Sign sign){
556+
return isShopSign(sign,null);
557+
}
558+
559+
/**
560+
* Checks if a Sign is a ShopSign and also check if a ShopSign is specific shop's ShopSign.
561+
* @param sign Target sign
562+
* @param shop Target shop (null if you don't want check sign owner)
563+
*
564+
* @return Is specific shop's ShopSign.
565+
*/
566+
default boolean isShopSign(@NotNull Sign sign, @Nullable Shop shop){
567+
// Check for new shop sign
568+
String[] lines = sign.getLines();
569+
if (lines[0].isEmpty() && lines[1].isEmpty() && lines[2].isEmpty() && lines[3].isEmpty()) {
570+
return true;
571+
}
572+
573+
// Check for exists shop sign (modern)
574+
if(sign.getPersistentDataContainer().has(SHOP_NAMESPACED_KEY,ShopSignPersistentDataType.INSTANCE)){
575+
if(shop != null){
576+
ShopSignStorage shopSignStorage = sign.getPersistentDataContainer().get(SHOP_NAMESPACED_KEY,ShopSignPersistentDataType.INSTANCE);
577+
return Objects.equals(shopSignStorage, new ShopSignStorage(getLocation().getWorld().getName(),getLocation().getBlockX(),getLocation().getBlockY(),getLocation().getBlockZ()));
578+
}
579+
return true;
580+
}
581+
582+
// Check for exists shop sign (legacy upgrade)
583+
if (lines[1].startsWith(SHOP_SIGN_PATTERN)) {
584+
return true;
585+
} else {
586+
String header = lines[0];
587+
String adminShopHeader = QuickShop.getInstance().text().of("signs.header",QuickShop.getInstance().text().of("admin-shop").forLocale()).forLocale();
588+
String signHeaderUsername = QuickShop.getInstance().text().of("signs.header", this.ownerName(true)).forLocale();
589+
if (header.contains(adminShopHeader) || header.contains(signHeaderUsername)) {
590+
return true;
591+
//TEXT SIGN
592+
//continue
593+
} else {
594+
adminShopHeader =QuickShop.getInstance().text().of("signs.header", QuickShop.getInstance().text().of("admin-shop").forLocale(), "").forLocale();
595+
signHeaderUsername = QuickShop.getInstance().text().of("signs.header", this.ownerName(true), "").forLocale();
596+
adminShopHeader = ChatColor.stripColor(adminShopHeader).trim();
597+
signHeaderUsername = ChatColor.stripColor(signHeaderUsername).trim();
598+
return header.contains(adminShopHeader) || header.contains(signHeaderUsername);
599+
}
600+
}
601+
}
538602

539603
}

src/main/java/org/maxgamer/quickshop/shop/ShopInfoStorage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
@Data
3434
@Builder
3535
public class ShopInfoStorage {
36-
private final BlockPosition location;
36+
private final String world;
37+
private final BlockPosition position;
3738
private final String moderator;
3839
private final double price;
3940
private final String item;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is a part of project QuickShop, the name is DisplayItemPersistentDataType.java
3+
* Copyright (C) PotatoCraft Studio and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package org.maxgamer.quickshop.shop;
21+
22+
import org.bukkit.persistence.PersistentDataAdapterContext;
23+
import org.bukkit.persistence.PersistentDataType;
24+
import org.jetbrains.annotations.NotNull;
25+
import org.maxgamer.quickshop.util.JsonUtil;
26+
27+
public class ShopSignPersistentDataType
28+
implements PersistentDataType<String, ShopSignStorage> {
29+
static final ShopSignPersistentDataType INSTANCE = new ShopSignPersistentDataType();
30+
31+
@Override
32+
public @NotNull Class<String> getPrimitiveType() {
33+
return String.class;
34+
}
35+
36+
@Override
37+
public @NotNull Class<ShopSignStorage> getComplexType() {
38+
return ShopSignStorage.class;
39+
}
40+
41+
@NotNull
42+
@Override
43+
public String toPrimitive(
44+
@NotNull ShopSignStorage complex, @NotNull PersistentDataAdapterContext context) {
45+
return JsonUtil.getGson().toJson(complex);
46+
}
47+
48+
@Override
49+
public @NotNull ShopSignStorage fromPrimitive(
50+
@NotNull String primitive, @NotNull PersistentDataAdapterContext context) {
51+
return JsonUtil.getGson().fromJson(primitive, ShopSignStorage.class);
52+
}
53+
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is a part of project QuickShop, the name is ShopInfoStorage.java
3+
* Copyright (C) PotatoCraft Studio and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package org.maxgamer.quickshop.shop;
21+
22+
import lombok.AllArgsConstructor;
23+
import lombok.Builder;
24+
import lombok.Data;
25+
26+
/**
27+
* TODO This class used for storage the shop sign
28+
*/
29+
@AllArgsConstructor
30+
@Data
31+
@Builder
32+
public class ShopSignStorage {
33+
private String world;
34+
private int x;
35+
private int y;
36+
private int z;
37+
}

0 commit comments

Comments
 (0)