Skip to content

Commit 273c742

Browse files
committed
Revert "Fix build"
This reverts commit 52c389f.
1 parent 52c389f commit 273c742

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* This file is a part of project QuickShop, the name is Lang.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.util.language;
21+
22+
import com.google.common.collect.ImmutableList;
23+
import lombok.Getter;
24+
import org.apache.commons.lang.StringUtils;
25+
import org.bukkit.command.CommandSender;
26+
import org.bukkit.configuration.file.FileConfiguration;
27+
import org.bukkit.configuration.file.YamlConfiguration;
28+
import org.jetbrains.annotations.NotNull;
29+
import org.jetbrains.annotations.Nullable;
30+
import org.maxgamer.quickshop.util.language.formatter.FilledFormatter;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.util.function.Consumer;
35+
36+
public class Lang {
37+
private final File file;
38+
private final Consumer<FileConfiguration> upgrading;
39+
private final ImmutableList<Formatter> formatters;
40+
@Getter
41+
private FileConfiguration map;
42+
43+
/**
44+
* Creating Language utils from a file
45+
*
46+
* @param file The Yaml language file
47+
* @param upgrading The upgrading method, executing everytime and allows devs to pre-processing or upgrading their
48+
* language files.
49+
* @param formatters The formatters used to formatting texts passthroughs this utils..
50+
*/
51+
public Lang(@NotNull File file, @NotNull Consumer<FileConfiguration> upgrading, @Nullable Formatter... formatters) {
52+
this.file = file;
53+
this.upgrading = upgrading;
54+
if (formatters != null) {
55+
this.formatters = ImmutableList.copyOf(formatters);
56+
} else {
57+
this.formatters = ImmutableList.of(new FilledFormatter());
58+
}
59+
this.reload();
60+
}
61+
62+
/**
63+
* Clean up and reload the data from drive
64+
*/
65+
public void reload() {
66+
if (!this.file.exists()) {
67+
try {
68+
//noinspection ResultOfMethodCallIgnored
69+
this.file.createNewFile();
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
}
73+
}
74+
this.map = YamlConfiguration.loadConfiguration(this.file);
75+
this.upgrading.accept(map);
76+
}
77+
78+
/**
79+
* Save the changes of the language file
80+
*/
81+
public void save() {
82+
try {
83+
this.map.save(this.file);
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
/**
90+
* Set the data
91+
*
92+
* @param path The data path
93+
* @param data The data
94+
*/
95+
public void set(@NotNull String path, @Nullable Object data) {
96+
this.map.set(path, data);
97+
}
98+
99+
/**
100+
* Formatting the phrase by formatters
101+
*
102+
* @param raw Original text
103+
* @param sender (optional) The text sender
104+
* @param args (optional) The args used for formatting
105+
* @return Formatted text
106+
*/
107+
public String format(@Nullable String raw, @Nullable CommandSender sender, @Nullable String... args) {
108+
if (StringUtils.isNotEmpty(raw)) {
109+
for (Formatter formatter : formatters) {
110+
raw = formatter.format(raw, sender, args);
111+
}
112+
}
113+
return raw;
114+
}
115+
116+
/**
117+
* Formatting the phrase by formatters
118+
*
119+
* @param raw Original text
120+
* @param args (optional) The args used for formatting
121+
* @return Formatted text
122+
*/
123+
public String format(@Nullable String raw, @Nullable String... args) {
124+
return format(raw, null, args);
125+
}
126+
127+
/**
128+
* Creating a Phrase from location
129+
* Phrase object allows developer to advanced and faster controlling phrase
130+
*
131+
* @param path The text path
132+
* @return A Phrase
133+
*/
134+
@NotNull
135+
public Phrase create(@NotNull String path) {
136+
return new Phrase(this, path);
137+
}
138+
139+
/**
140+
* Getting text from language file
141+
*
142+
* @param path The phrase location
143+
* @param args (optional) The args used for formatting
144+
* @return The formatted text
145+
*/
146+
@NotNull
147+
public String getString(@NotNull String path, @Nullable String... args) {
148+
return new Phrase(this, path).get(args);
149+
}
150+
151+
/**
152+
* Getting text from language file
153+
*
154+
* @param path The phrase location
155+
* @param sender (optional) The text sender
156+
* @param args (optional) The args used for formatting
157+
* @return The formatted text
158+
*/
159+
@NotNull
160+
public String getString(@NotNull String path, @NotNull CommandSender sender, @Nullable String... args) {
161+
return new Phrase(this, path).get(sender, args);
162+
}
163+
164+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* This file is a part of project QuickShop, the name is Phrase.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.util.language;
21+
22+
import org.bukkit.command.CommandSender;
23+
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
25+
26+
/**
27+
* An object allow fast and advanced control for phrase
28+
*/
29+
public class Phrase {
30+
private final Lang lang;
31+
private final String original;
32+
private String last;
33+
private boolean baked = false;
34+
private boolean ready = false;
35+
36+
/**
37+
* Create a phrase
38+
*
39+
* @param lang Lang class object
40+
* @param path Phrase location
41+
*/
42+
public Phrase(@NotNull Lang lang, @NotNull String path) {
43+
this.lang = lang;
44+
this.original = lang.getMap().getString(path);
45+
this.last = original;
46+
}
47+
48+
/**
49+
* Create a phrase
50+
*
51+
* @param lang Lang class object
52+
* @param path Phrase location
53+
* @param defaults Default value
54+
*/
55+
public Phrase(@NotNull Lang lang, @NotNull String path, @Nullable String defaults) {
56+
this.lang = lang;
57+
this.original = lang.getMap().getString(path, defaults);
58+
this.last = original;
59+
}
60+
61+
/**
62+
* Apply args to original string
63+
*
64+
* @param args The args used for formatter
65+
*/
66+
public void bake(@Nullable String... args) {
67+
this.bake(null, args);
68+
}
69+
70+
/**
71+
* Apply args to original string
72+
*
73+
* @param args The args used for formatter
74+
* @param sender The sender used for formatter
75+
*/
76+
public void bake(@Nullable CommandSender sender, @Nullable String... args) {
77+
if (args != null) {
78+
this.last = this.lang.format(this.original, sender, args);
79+
} else {
80+
this.last = original;
81+
}
82+
this.baked = true;
83+
this.ready = true;
84+
}
85+
86+
/**
87+
* Getting the baked string
88+
*
89+
* @param args The args used for formatter
90+
* @return Baked string
91+
*/
92+
public String get(@Nullable String... args) {
93+
this.bake(args);
94+
this.ready = false;
95+
return this.last();
96+
}
97+
98+
/**
99+
* Getting the baked string
100+
*
101+
* @param args The args used for formatter
102+
* @param sender The sender used for formatter
103+
* @return Baked string
104+
*/
105+
public String get(@NotNull CommandSender sender, @Nullable String... args) {
106+
this.bake(sender, args);
107+
this.ready = false;
108+
return this.last();
109+
}
110+
111+
/**
112+
* Returns last processed string
113+
* It may or may not baked.
114+
*
115+
* @return Last processed string
116+
*/
117+
public String last() {
118+
return this.last;
119+
}
120+
121+
/**
122+
* Returns this phrase has been baked
123+
*
124+
* @return Does phrase baked
125+
*/
126+
public boolean isBaked() {
127+
return this.baked;
128+
}
129+
130+
/**
131+
* Returns Phrase ready for getting
132+
* Phrase will get ready after baking, but will set to no-ready after get once.
133+
*
134+
* @return Ready
135+
*/
136+
public boolean isReady() {
137+
return this.ready;
138+
}
139+
}

0 commit comments

Comments
 (0)