1414import static java .awt .Color .BLUE ;
1515import static java .lang .Double .parseDouble ;
1616import static java .lang .Integer .parseInt ;
17+ import static java .util .regex .Pattern .compile ;
1718import static net .minecraft .ChatFormatting .DARK_GREEN ;
1819import static net .minecraft .ChatFormatting .DARK_RED ;
1920import static net .minecraft .ChatFormatting .GOLD ;
2526
2627public record StockMarketEntry (Company company , double price , double changeMoney , double changePercentage , int ownership , double buyPrice ) {
2728
28- private static final Pattern PRICE_PATTERN = Pattern . compile ("Kurs: (?<price>\\ d+\\ .\\ d+)\\ $" );
29- private static final Pattern CHANGE_PATTERN = Pattern . compile ("Änderung: (?<money>-?\\ d+\\ .\\ d+)\\ $ \\ ((?<percentage>-?\\ d+\\ .\\ d+)%\\ ) [▼▲]" );
30- private static final Pattern OWNERSHIP_PATTERN = Pattern . compile ("Besitz: (?<ownership>\\ d+)/\\ d+" );
31- private static final Pattern BUY_PRICE_PATTERN = Pattern . compile ("EK-Preis: (?<price>-?\\ d+\\ .\\ d+)\\ $" );
29+ private static final Pattern PRICE_PATTERN = compile ("Kurs: (?<price>\\ d+\\ .\\ d+)\\ $" );
30+ private static final Pattern CHANGE_PATTERN = compile ("Änderung: (?<money>-?\\ d+\\ .\\ d+)\\ $ \\ ((?<percentage>-?\\ d+\\ .\\ d+)%\\ ) [▼▲]" );
31+ private static final Pattern OWNERSHIP_PATTERN = compile ("Besitz: (?<ownership>\\ d+)/\\ d+" );
32+ private static final Pattern BUY_PRICE_PATTERN = compile ("EK-Preis: (?<price>-?\\ d+\\ .\\ d+)\\ $" );
3233
3334 public @ Nullable Color getColor () {
34- double diff = this .price - this .buyPrice ;
35-
35+ // check if price is below or equal to min value
3636 if (this .price <= this .company .getMinValue ()) {
3737 return BLUE ;
3838 }
3939
40+ // check if user has bought any stock
41+ if (this .ownership == 0 ) {
42+ return null ;
43+ }
44+
45+ // get color value
46+ Integer colorValue = getColorValue ();
47+ if (colorValue == null ) {
48+ return null ;
49+ }
50+
51+ return new Color (colorValue );
52+ }
53+
54+ private @ Nullable Integer getColorValue () {
4055 Integer colorValue ;
56+ double diff = this .price - this .buyPrice ;
4157
4258 if (diff >= 75 ) {
4359 colorValue = DARK_GREEN .getColor ();
@@ -53,11 +69,7 @@ public record StockMarketEntry(Company company, double price, double changeMoney
5369 colorValue = DARK_RED .getColor ();
5470 }
5571
56- if (colorValue == null ) {
57- return null ;
58- }
59-
60- return new Color (colorValue );
72+ return colorValue ;
6173 }
6274
6375 public static @ Nullable StockMarketEntry fromItemStack (@ NotNull ItemStack itemStack ) {
@@ -116,10 +128,6 @@ public record StockMarketEntry(Company company, double price, double changeMoney
116128 }
117129 }
118130
119- if (price == 0 ) {
120- return null ;
121- }
122-
123131 return new StockMarketEntry (company , price , changeMoney , changePercentage , ownership , buyPrice );
124132 }
125133}
0 commit comments