Skip to content

Commit c94bdd6

Browse files
committed
Trying use packet hack for sign multi-language support
1 parent 36764ff commit c94bdd6

File tree

7 files changed

+126
-27
lines changed

7 files changed

+126
-27
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@
417417
<id>savagelabs</id>
418418
<url>https://nexus.savagelabs.net/repository/maven-releases/</url>
419419
</repository>-->
420+
<repository>
421+
<id>MG-Dev Jenkins CI Maven Repository</id>
422+
<url>https://ci.mg-dev.eu/plugin/repository/everything/</url>
423+
</repository>
420424
</repositories>
421425

422426
<dependencies>
@@ -946,5 +950,11 @@
946950
<version>2.11.0</version>
947951
<scope>compile</scope>
948952
</dependency>
953+
<dependency>
954+
<groupId>com.bergerkiller.bukkit</groupId>
955+
<artifactId>BKCommonLib</artifactId>
956+
<version>1.17.1-v3</version>
957+
<scope>provided</scope>
958+
</dependency>
949959
</dependencies>
950960
</project>

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Find.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import io.papermc.lib.PaperLib;
2323
import lombok.AllArgsConstructor;
24+
import net.md_5.bungee.api.chat.TextComponent;
2425
import org.bukkit.Location;
2526
import org.bukkit.entity.Player;
2627
import org.bukkit.event.player.PlayerTeleportEvent;
@@ -132,8 +133,8 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
132133
Location location = shop.getLocation();
133134
// "nearby-shop-entry": "&a- Info:{0} &aPrice:&b{1} &ax:&b{2} &ay:&b{3} &az:&b{4} &adistance: &b{5} &ablock(s)"
134135
stringBuilder.append(plugin.text().of(sender, "nearby-shop-entry",
135-
shop.getSignText()[1],
136-
shop.getSignText()[3],
136+
new TextComponent(shop.getSignText(sender.getLocale()).get(1).getComponents()).toLegacyText(),
137+
new TextComponent(shop.getSignText(sender.getLocale()).get(3).getComponents()).toLegacyText(),
137138
String.valueOf(location.getBlockX()),
138139
String.valueOf(location.getBlockY()),
139140
String.valueOf(location.getBlockZ()),

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import io.papermc.lib.PaperLib;
2424
import lombok.EqualsAndHashCode;
2525
import me.lucko.helper.serialize.BlockPosition;
26+
import net.md_5.bungee.api.chat.ComponentBuilder;
27+
import net.md_5.bungee.api.chat.TextComponent;
28+
import net.md_5.bungee.api.chat.TranslatableComponent;
2629
import org.bukkit.*;
2730
import org.bukkit.block.Block;
2831
import org.bukkit.block.BlockFace;
@@ -43,6 +46,7 @@
4346
import org.jetbrains.annotations.Nullable;
4447
import org.maxgamer.quickshop.QuickShop;
4548
import org.maxgamer.quickshop.event.*;
49+
import org.maxgamer.quickshop.util.ComponentPackge;
4650
import org.maxgamer.quickshop.util.JsonUtil;
4751
import org.maxgamer.quickshop.util.PriceLimiter;
4852
import org.maxgamer.quickshop.util.Util;
@@ -688,14 +692,13 @@ public boolean inventoryAvailable() {
688692
}
689693

690694
@Override
691-
public String[] getSignText() {
695+
public List<ComponentPackge> getSignText(@NotNull String locale) {
692696
Util.ensureThread(false);
693-
String[] lines = new String[4];
694-
697+
List<ComponentPackge> lines = new ArrayList<>();
695698
//Line 1
696699
OfflinePlayer player = plugin.getServer().getOfflinePlayer(this.getOwner());
697700
String statusStringKey = inventoryAvailable() ? "signs.status-available" : "signs.status-unavailable";
698-
lines[0] = plugin.text().of("signs.header", this.ownerName(false), plugin.text().of(statusStringKey).forLocale()).forLocale();
701+
lines.add(new ComponentPackge(TextComponent.fromLegacyText(plugin.text().of("signs.header", this.ownerName(false), plugin.text().of(statusStringKey).forLocale(locale)).forLocale(locale))));
699702

700703
//Line 2
701704
String tradingStringKey;
@@ -718,35 +721,48 @@ public String[] getSignText() {
718721
tradingStringKey = "MissingKey for shop type:" + shopType;
719722
noRemainingStringKey = "MissingKey for shop type:" + shopType;
720723
}
724+
String line2;
721725
switch (shopRemaining) {
722726
//Unlimited
723727
case -1:
724-
lines[1] = plugin.text().of(tradingStringKey, plugin.text().of("signs.unlimited").forLocale()).forLocale();
728+
line2 =plugin.text().of(tradingStringKey, plugin.text().of("signs.unlimited").forLocale(locale)).forLocale(locale);
725729
break;
726730
//No remaining
727731
case 0:
728-
lines[1] = plugin.text().of(noRemainingStringKey).forLocale();
732+
line2 =plugin.text().of(noRemainingStringKey).forLocale(locale);
729733
break;
730734
//Has remaining
731735
default:
732-
lines[1] = plugin.text().of(tradingStringKey, Integer.toString(shopRemaining)).forLocale();
736+
line2 =plugin.text().of(tradingStringKey, Integer.toString(shopRemaining)).forLocale(locale);
733737
}
738+
lines.add(new ComponentPackge(TextComponent.fromLegacyText(shopSignPrefix+line2+" ")));
734739

735740
//line 3
736-
lines[2] = plugin.text().of("signs.item", Util.getItemStackName(this.getItem())).forLocale();
741+
if(this.getItem().hasItemMeta() && this.getItem().getItemMeta().hasDisplayName()){
742+
TextComponent left = new TextComponent(plugin.text().of("signs.item-left").forLocale());
743+
TranslatableComponent mediumItem = new TranslatableComponent("item."+getItem().getType().getKey().getNamespace()+"."+getItem().getType().getKey().getKey());
744+
TextComponent right = new TextComponent(plugin.text().of("signs.item-right").forLocale());
745+
lines.add(new ComponentPackge(new ComponentBuilder()
746+
.append(left)
747+
.append(mediumItem)
748+
.append(right)
749+
.create()));
750+
}else{
751+
lines.add(new ComponentPackge(new ComponentBuilder().append( TextComponent.fromLegacyText(plugin.text().of("signs.item-left").forLocale()))
752+
.append(TextComponent.fromLegacyText(Util.getItemStackName(getItem())))
753+
.append(TextComponent.fromLegacyText(plugin.text().of("signs.item-right").forLocale())).create()));
754+
}
737755

738756
//line 4
757+
String line4;
739758
if (this.isStackingShop()) {
740-
lines[3] = plugin.text().of("signs.stack-price",
759+
line4 = plugin.text().of("signs.stack-price",
741760
Util.format(this.getPrice(), this), Integer.toString(item.getAmount()),
742761
Util.getItemStackName(item)).forLocale();
743762
} else {
744-
lines[3] = plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale();
763+
line4 = plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale();
745764
}
746-
747-
//New pattern for recognizing shop sign
748-
lines[1] = shopSignPrefix + lines[1] + " ";
749-
765+
lines.add(new ComponentPackge(TextComponent.fromLegacyText(line4)));
750766
return lines;
751767
}
752768

@@ -756,15 +772,12 @@ public String[] getSignText() {
756772
* @param lines The array of lines to change. Index is line number.
757773
*/
758774
@Override
759-
public void setSignText(@NotNull String[] lines) {
775+
public void setSignText(@NotNull List<ComponentPackge> lines) {
760776
Util.ensureThread(false);
761777
List<Sign> signs = this.getSigns();
762778
for (Sign sign : signs) {
763-
if (Arrays.equals(sign.getLines(), lines)) {
764-
continue;
765-
}
766-
for (int i = 0; i < lines.length; i++) {
767-
sign.setLine(i, lines[i]);
779+
for (int i = 0; i < lines.size(); i++) {
780+
sign.setLine(i, new TextComponent(lines.get(i).getComponents()).toLegacyText());
768781
}
769782
if (plugin.getGameVersion().isSignTextDyeSupport()) {
770783
DyeColor dyeColor = Util.getDyeColor();
@@ -790,7 +803,7 @@ public void setSignText() {
790803
if (!Util.isLoaded(this.location)) {
791804
return;
792805
}
793-
this.setSignText(getSignText());
806+
this.setSignText(getSignText("en_us"));
794807
}
795808

796809
/**
@@ -1535,6 +1548,7 @@ public void openPreview(@NotNull Player player) {
15351548
inventoryPreview = new InventoryPreview(plugin, getItem().clone());
15361549
}
15371550
inventoryPreview.show(player);
1551+
15381552
}
15391553

15401554
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.bukkit.plugin.Plugin;
3030
import org.jetbrains.annotations.NotNull;
3131
import org.jetbrains.annotations.Nullable;
32+
import org.maxgamer.quickshop.util.ComponentPackge;
3233

3334
import java.util.List;
3435
import java.util.UUID;
@@ -179,8 +180,7 @@ public interface Shop {
179180
* Line 2: Shop Item Name
180181
* Line 3: Price
181182
*/
182-
@NotNull
183-
default String[] getSignText() {
183+
default List<ComponentPackge> getSignText(String locale) {
184184
//backward support
185185
throw new UnsupportedOperationException();
186186
}
@@ -190,7 +190,7 @@ default String[] getSignText() {
190190
*
191191
* @param paramArrayOfString The texts you want set
192192
*/
193-
void setSignText(@NotNull String[] paramArrayOfString);
193+
void setSignText(@NotNull List<ComponentPackge> paramArrayOfString);
194194

195195
/**
196196
* Update shop data to database
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.maxgamer.quickshop.util;
2+
3+
import net.md_5.bungee.api.chat.BaseComponent;
4+
5+
public class ComponentPackge {
6+
private BaseComponent[] components;
7+
public ComponentPackge(BaseComponent... components){
8+
this.components = components;
9+
}
10+
11+
public BaseComponent[] getComponents() {
12+
return components;
13+
}
14+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.maxgamer.quickshop.util.hack.packet;
2+
3+
import com.bergerkiller.bukkit.common.nbt.CommonTagCompound;
4+
import com.bergerkiller.bukkit.common.protocol.CommonPacket;
5+
import com.bergerkiller.bukkit.common.protocol.PacketType;
6+
import com.bergerkiller.bukkit.common.utils.BlockUtil;
7+
import com.bergerkiller.bukkit.common.utils.PacketUtil;
8+
import net.md_5.bungee.chat.ComponentSerializer;
9+
import org.apache.commons.lang.Validate;
10+
import org.bukkit.Bukkit;
11+
import org.bukkit.block.Sign;
12+
import org.bukkit.entity.Player;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.maxgamer.quickshop.QuickShop;
15+
import org.maxgamer.quickshop.util.ComponentPackge;
16+
17+
import java.util.List;
18+
19+
/**
20+
* Little treat, we modify the sign component to translatable component if possible...
21+
*/
22+
public class SignPacketHack {
23+
24+
private final QuickShop plugin;
25+
26+
public SignPacketHack(QuickShop plugin){
27+
this.plugin = plugin;
28+
Validate.isTrue(Bukkit.getPluginManager().isPluginEnabled("BKCommonLib"),"Require BKCommonLib installed!");
29+
}
30+
31+
public void sendLines(@NotNull Sign sign, @NotNull List<ComponentPackge> lines, Player player) {
32+
if (player != null) {
33+
CommonPacket updatePacket = BlockUtil.getUpdatePacket(sign);
34+
if (updatePacket != null) {
35+
applyToSign(updatePacket,lines);
36+
PacketUtil.sendPacket(player, updatePacket);
37+
}
38+
}
39+
}
40+
41+
public void applyToSign(@NotNull CommonPacket updatePacket,@NotNull List<ComponentPackge> lines){
42+
if (updatePacket.getType() == PacketType.OUT_TILE_ENTITY_DATA) {
43+
// >= MC 1.10.2
44+
45+
CommonTagCompound compound = updatePacket.read(PacketType.OUT_TILE_ENTITY_DATA.data);
46+
// ====================================================================
47+
48+
for (int i = 0; i < lines.size(); i++) {
49+
String key = "Text" + (i+1);
50+
String text = ComponentSerializer.toString(lines.get(i).getComponents());
51+
compound.putValue(key, text);
52+
}
53+
54+
// ====================================================================
55+
updatePacket.write(PacketType.OUT_TILE_ENTITY_DATA.data, compound);
56+
}
57+
}
58+
59+
}

src/main/resources/lang-original/messages.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@
190190
"header": "{1}{0}",
191191
"buying": "Buying {0}",
192192
"stack-buying": "Buying {0}",
193-
"item": "{0}",
193+
"item-left": "",
194+
"item-right": "",
194195
"price": "{0} each",
195196
"stack-price": "{0} per {1}x {2}",
196197
"unlimited": "Unlimited",

0 commit comments

Comments
 (0)