Skip to content

Commit 84b7c83

Browse files
committed
Improve the serialize on itemstack
Make more parts serializable with json.
1 parent a11f1b8 commit 84b7c83

File tree

8 files changed

+230
-32
lines changed

8 files changed

+230
-32
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/BannerMeta.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.inventory.meta.ItemMeta;
1111

1212
import javax.annotation.Nonnull;
13+
import javax.annotation.Nullable;
1314
import java.util.ArrayList;
1415
import java.util.Arrays;
1516
import java.util.List;
@@ -39,6 +40,7 @@ public class BannerMeta {
3940
*
4041
* @return the base {@link DyeColor} of the banner, or null if not set.
4142
*/
43+
@Nullable
4244
public DyeColor getBannerBaseColor() {
4345
return bannerBaseColor;
4446
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/ColorMeta.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import org.broken.arrow.library.logging.Validate;
55
import org.bukkit.Color;
66

7+
import javax.annotation.Nonnull;
78
import java.util.logging.Level;
89

910
public class ColorMeta {
1011
private static final Logging logger = new Logging(ColorMeta.class);
1112
private String rgb;
13+
private Color color;
1214
private int red = -1;
1315
private int green = -1;
1416
private int blue = -1;
1517

18+
1619
/**
1720
* Get red color.
1821
*
@@ -49,18 +52,58 @@ public String getRgb() {
4952
return rgb;
5053
}
5154

55+
/**
56+
* Gets the color as an RGB integer.
57+
*
58+
* @return An integer representation of this color, as 0xRRGGBB
59+
*/
60+
public int toRgb() {
61+
return color.asRGB();
62+
}
63+
64+
/**
65+
* Gets the color as an ARGB integer.
66+
*
67+
* @return An integer representation of this color, as 0xAARRGGBB
68+
*/
69+
public int toArgb() {
70+
return color.asARGB();
71+
}
72+
5273
/**
5374
* Set the 3 colors from Bukkit Color.
5475
*
5576
* @param color you want to convert to split up in three colors.
5677
*/
57-
public void setRgb(final Color color) {
58-
final int colorRed = color.getRed();
59-
final int colorGreen = color.getGreen();
60-
final int colorBlue = color.getBlue();
61-
this.setRgb(colorRed, colorGreen, colorBlue);
78+
public void setRgb(@Nonnull final Color color) {
79+
this.setColor(color);
80+
}
81+
82+
/**
83+
* Sets a new color object from an integer that contains the red,
84+
* green, and blue bytes in the lowest order 24 bits.
85+
*
86+
* @param rgb the integer storing the red, green, and blue values
87+
* @throws IllegalArgumentException if any data is in the highest order 8
88+
* bits
89+
*/
90+
public void setRgb(final int rgb) {
91+
final Color color = Color.fromRGB(rgb);
92+
this.setRgb(color);
6293
}
6394

95+
/**
96+
* Creates a new color object from an integer that contains the alpha, red,
97+
* green, and blue bytes.
98+
*
99+
* @param argb the integer storing the alpha, red, green, and blue values
100+
*/
101+
public void setArgb(final int argb) {
102+
final Color color = Color.fromARGB(argb);
103+
this.setRgb(color);
104+
}
105+
106+
64107
/**
65108
* Set the 3 colors from a string. The color order is R,B,G
66109
* and the string should look like this 20,15,47.
@@ -70,36 +113,54 @@ public void setRgb(final Color color) {
70113
public void setRgb(final String rgb) {
71114
this.rgb = rgb;
72115
final String[] colors = this.getRgb() != null ? this.getRgb().split(",") : new String[]{""};
73-
Validate.checkBoolean(colors.length < 3, "rgb is not format correctly. Should be formatted like this 'r,b,g'. Example '20,15,47'. Current input is " + rgb + " length "+ colors.length);
116+
Validate.checkBoolean(colors.length < 3, "rgb is not format correctly. Should be formatted like this 'r,b,g'. Example '20,15,47'. Current input is " + rgb + " length " + colors.length);
74117
try {
75118
final int colorRed = Integer.parseInt(colors[0]);
76119
final int colorGreen = Integer.parseInt(colors[2]);
77120
final int colorBlue = Integer.parseInt(colors[1]);
78121

79-
this.setRgb(colorRed, colorGreen, colorBlue);
122+
this.setRgb(255,colorRed, colorGreen, colorBlue);
80123
} catch (final NumberFormatException exception) {
81124
logger.log(Level.WARNING, exception, () -> "you don´t use numbers inside this string. Your input: " + rgb);
82125
}
83126
}
84127

128+
/**
129+
* Creates a new Color object from an alpha, red, green, and blue
130+
*
131+
* @param alpha integer from 0-255
132+
* @param red integer from 0-255
133+
* @param green integer from 0-255
134+
* @param blue integer from 0-255
135+
* @throws Validate.ValidateExceptions if any value is strictly {@literal >255 or <0}
136+
*/
137+
public void setRgb(final int alpha,final int red, final int green, final int blue) {
138+
Validate.checkBoolean(red < 0 || green < 0 || blue < 0, "You can't use negative numbers for the rbg color.");
139+
final Color color = Color.fromARGB(alpha,red, green, blue);
140+
this.setColor(color);
141+
}
85142

86143
/**
87144
* Set the rbg color.
88145
*
89-
* @param red color.
90-
* @param green color.
91-
* @param blue color.
146+
* @param color The color object to set.
92147
*/
93-
public void setRgb(final int red, final int green, final int blue) {
94-
Validate.checkBoolean(red < 0 || green < 0 || blue < 0, "You can't use negative numbers for the rbg color.");
148+
private void setColor(final Color color) {
149+
Validate.checkBoolean(color == null, "You can't set color to null.");
95150

96-
this.rgb = red + "," + green + "," + blue;
97-
this.red = red;
98-
this.green = green;
99-
this.blue = blue;
151+
final int colorRed = color.getRed();
152+
final int colorGreen = color.getGreen();
153+
final int colorBlue = color.getBlue();
154+
155+
this.rgb = colorRed + "," + colorGreen + "," + colorBlue;
156+
this.color = color;
157+
this.red = colorRed;
158+
this.green = colorGreen;
159+
this.blue = colorBlue;
100160

101161
}
102162

163+
103164
/**
104165
* Retrieve if all colors is set.
105166
*

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/AttributeModifierWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class AttributeModifierWrapper {
2121
private String equipmentSlot;
2222
private String attribute;
2323
private String name;
24-
private double amount;
2524
private String operation;
25+
private double amount;
2626

2727
/**
2828
* Creates a wrapper from the given {@link Attribute} and {@link AttributeModifier}.

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/itemstack/SerializeItem.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class SerializeItem {
4545
private List<org.broken.arrow.library.itemcreator.meta.BannerMeta> patterns;
4646
private Material type;
4747
private Color armorColor;
48-
private DyeColor baseColor;
4948
private String name;
5049
private List<String> lore;
5150
private Integer customModelData;
@@ -314,14 +313,6 @@ public Color getArmorColor() {
314313
return armorColor;
315314
}
316315

317-
/**
318-
* Retrieve the item-stack banner base color.
319-
*
320-
* @return the banner base color, or null if not set
321-
*/
322-
public DyeColor getBaseColor() {
323-
return baseColor;
324-
}
325316

326317
/**
327318
* Retrieve the item-stack banner patterns.
@@ -365,15 +356,15 @@ public boolean equals(final Object o) {
365356
Objects.equals(skinPlayerId, that.skinPlayerId) &&
366357
Objects.equals(potionEffects, that.potionEffects) &&
367358
Objects.equals(attributeModifiers, that.attributeModifiers) &&
368-
Objects.equals(armorColor, that.armorColor) && baseColor == that.baseColor &&
359+
Objects.equals(armorColor, that.armorColor) &&
369360
Objects.equals(patterns, that.patterns) &&
370361
Objects.equals(fireworkMeta, that.fireworkMeta) &&
371362
Objects.equals(bookMenta, that.bookMenta);
372363
}
373364

374365
@Override
375366
public int hashCode() {
376-
return Objects.hash(type, amount, name, lore, enchantments, customModelData, unbreakable, itemFlags, skullOwner, skinPlayerId, potionEffects, attributeModifiers, armorColor, baseColor, patterns, fireworkMeta, bookMenta);
367+
return Objects.hash(type, amount, name, lore, enchantments, customModelData, unbreakable, itemFlags, skullOwner, skinPlayerId, potionEffects, attributeModifiers, armorColor, patterns, fireworkMeta, bookMenta);
377368
}
378369

379370
private void setOwnerToMeta(@Nonnull final SkullMeta skull) {
@@ -433,10 +424,9 @@ private static void retrieveBannerMeta(final ItemMeta meta, final SerializeItem
433424
if (meta instanceof BannerMeta) {
434425
final BannerMeta bannerMeta = (BannerMeta) meta;
435426
org.broken.arrow.library.itemcreator.meta.BannerMeta bannerData = new org.broken.arrow.library.itemcreator.meta.BannerMeta();
436-
data.baseColor = bannerMeta.getBaseColor();
427+
bannerData.setBannerBaseColor(bannerMeta.getBaseColor());
437428
data.patterns = bannerMeta.getPatterns().stream()
438429
.map(pattern -> {
439-
bannerData.setBannerBaseColor(pattern.getColor());
440430
bannerData.addPatterns(pattern);
441431
return bannerData;
442432
})
@@ -459,6 +449,7 @@ private static void retrieveAttributeModifiers(final ItemMeta meta, final Serial
459449
private void setBannerMeta(final ItemMeta meta) {
460450
if (meta instanceof BannerMeta && patterns != null) {
461451
BannerMeta bannerMeta = (BannerMeta) meta;
452+
final DyeColor baseColor = bannerMeta.getBaseColor();
462453
if (baseColor != null) bannerMeta.setBaseColor(baseColor);
463454
bannerMeta.setPatterns(patterns.stream().flatMap(bannerPatterns -> bannerPatterns.getPatterns().stream())
464455
.collect(Collectors.toList()));

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/jsonhelper/JsonReaderHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ public void forEachObjectField(ThrowingFieldHandler handler) throws IOException
162162
String name = nextName();
163163
handler.handle(name, this);
164164
}
165-
166165
}
167166

168167
/**
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.broken.arrow.library.itemcreator.serialization.typeadapter;
2+
3+
import com.google.gson.stream.JsonReader;
4+
import com.google.gson.stream.JsonWriter;
5+
import org.broken.arrow.library.itemcreator.meta.BannerMeta;
6+
import org.broken.arrow.library.itemcreator.serialization.jsonhelper.JsonReaderHelper;
7+
import org.broken.arrow.library.itemcreator.serialization.jsonhelper.JsonWriterHelper;
8+
import org.broken.arrow.library.itemcreator.utility.PatternTypeUtil;
9+
import org.broken.arrow.library.logging.Logging;
10+
import org.bukkit.Color;
11+
import org.bukkit.DyeColor;
12+
import org.bukkit.block.banner.Pattern;
13+
import org.bukkit.block.banner.PatternType;
14+
15+
import javax.annotation.Nonnull;
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
public class BannerMetaAdapter extends BaseTypeAdapter<BannerMeta> {
20+
private static final Logging logger = new Logging(BannerMetaAdapter.class);
21+
22+
@Override
23+
protected void checkedWrite(final JsonWriter out, @Nonnull final BannerMeta value) throws IOException {
24+
final JsonWriterHelper json = new JsonWriterHelper(out);
25+
final List<Pattern> patterns = value.getPatterns();
26+
final DyeColor bannerBaseColor = value.getBannerBaseColor();
27+
28+
if (bannerBaseColor != null)
29+
json.value("banner_base_color", bannerBaseColor.getColor().asRGB());
30+
31+
json.forEachObject("patterns", patterns, pattern -> {
32+
json.value("color", pattern.getColor().getColor().asRGB());
33+
json.value("pattern", PatternTypeUtil.toId(pattern.getPattern()));
34+
});
35+
json.finish();
36+
}
37+
38+
@Override
39+
public BannerMeta read(final JsonReader jsonReader) throws IOException {
40+
JsonReaderHelper json = new JsonReaderHelper(jsonReader);
41+
BannerMeta meta = new BannerMeta();
42+
json.forEachObjectField((name, reader) -> {
43+
switch (name) {
44+
case "banner_base_color":
45+
final int baseColor = reader.nextInt();
46+
if (baseColor > 0) {
47+
meta.setBannerBaseColor(DyeColor.getByColor(Color.fromRGB(baseColor)));
48+
}
49+
break;
50+
case "patterns":
51+
setBannerPatterns(reader, meta);
52+
break;
53+
default:
54+
reader.skipValue();
55+
}
56+
});
57+
json.endObject();
58+
return meta;
59+
}
60+
61+
private void setBannerPatterns(final JsonReaderHelper reader, final BannerMeta meta) throws IOException {
62+
reader.forEachObjectInArray(potionReader -> {
63+
PatternWarper patternWarper = new PatternWarper();
64+
potionReader.forEachObjectField((fieldName, fieldReader) -> {
65+
switch (fieldName) {
66+
case "color":
67+
final int color = reader.nextInt();
68+
if (color > 0) {
69+
patternWarper.setColor(color);
70+
}
71+
break;
72+
case "pattern":
73+
final String pattern = reader.nextString();
74+
patternWarper.setPattern(pattern);
75+
break;
76+
default:
77+
fieldReader.skipValue();
78+
}
79+
});
80+
if (patternWarper.isValid()) {
81+
return meta.addPatterns(patternWarper.getPattern());
82+
} else {
83+
logger.warn(() -> "The pattern or color is invalid, the current pattern is: '" + patternWarper + "'.");
84+
return null;
85+
}
86+
});
87+
}
88+
89+
90+
public static class PatternWarper {
91+
private DyeColor color;
92+
private PatternType pattern;
93+
94+
public void setColor(final int color) {
95+
this.color = DyeColor.getByColor(Color.fromRGB(color));
96+
}
97+
98+
public void setPattern(final String pattern) {
99+
this.pattern = PatternTypeUtil.fromString(pattern);
100+
}
101+
102+
public Pattern getPattern() {
103+
return new Pattern(color, pattern);
104+
}
105+
106+
public boolean isValid() {
107+
return color != null && pattern != null;
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return "PatternWarper{" +
113+
"color=" + color +
114+
", pattern=" + pattern +
115+
'}';
116+
}
117+
}
118+
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/typeadapter/BaseTypeAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.TypeAdapter;
44
import com.google.gson.stream.JsonWriter;
55

6+
import javax.annotation.Nonnull;
67
import java.io.IOException;
78

89
public abstract class BaseTypeAdapter<T> extends TypeAdapter<T> {
@@ -20,5 +21,5 @@ public final void write(JsonWriter out, T value) throws IOException {
2021
* Adapter implementations override this method instead of write().
2122
* The null check is already handled.
2223
*/
23-
protected abstract void checkedWrite(JsonWriter out, T value) throws IOException;
24+
protected abstract void checkedWrite(JsonWriter out,@Nonnull T value) throws IOException;
2425
}

0 commit comments

Comments
 (0)