forked from bootswithdefer/LogBlock
-
Notifications
You must be signed in to change notification settings - Fork 112
/
BlockStateCodecSign.java
147 lines (140 loc) · 5.14 KB
/
BlockStateCodecSign.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package de.diddiz.LogBlock.blockstate;
import de.diddiz.util.BukkitUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.YamlConfiguration;
public class BlockStateCodecSign implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return BukkitUtils.getAllSignsArray();
}
@Override
public YamlConfiguration serialize(BlockState state) {
if (state instanceof Sign) {
Sign sign = (Sign) state;
String[] lines = sign.getLines();
boolean hasText = false;
for (int i = 0; i < lines.length; i++) {
if (lines[i] != null && lines[i].length() > 0) {
hasText = true;
break;
}
}
DyeColor signColor = sign.getColor();
if (signColor == null) {
signColor = DyeColor.BLACK;
}
if (hasText || signColor != DyeColor.BLACK) {
YamlConfiguration conf = new YamlConfiguration();
if (hasText) {
conf.set("lines", Arrays.asList(lines));
}
if (signColor != DyeColor.BLACK) {
conf.set("color", signColor.name());
}
if (sign.isGlowingText()) {
conf.set("glowing", true);
}
return conf;
}
}
return null;
}
/**
* This is required for the SignChangeEvent, because we have no BlockState there.
*/
public static YamlConfiguration serialize(String[] lines) {
YamlConfiguration conf = new YamlConfiguration();
conf.set("lines", Arrays.asList(lines));
return conf;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Sign) {
Sign sign = (Sign) state;
DyeColor signColor = DyeColor.BLACK;
boolean glowing = false;
List<String> lines = Collections.emptyList();
if (conf != null) {
if (conf.contains("lines")) {
lines = conf.getStringList("lines");
}
if (conf.contains("color")) {
try {
signColor = DyeColor.valueOf(conf.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
glowing = conf.getBoolean("glowing", false);
}
for (int i = 0; i < 4; i++) {
String line = lines.size() > i && lines.get(i) != null ? lines.get(i) : "";
sign.setLine(i, line);
}
sign.setColor(signColor);
sign.setGlowingText(glowing);
}
}
@Override
public String toString(YamlConfiguration state, YamlConfiguration oldState) {
if (state != null) {
List<String> lines = state.getStringList("lines");
List<String> oldLines = Collections.emptyList();
DyeColor signColor = DyeColor.BLACK;
if (state.contains("color")) {
try {
signColor = DyeColor.valueOf(state.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
DyeColor oldSignColor = DyeColor.BLACK;
boolean glowing = state.getBoolean("glowing", false);
boolean oldGlowing = false;
if (oldState != null) {
oldLines = oldState.getStringList("lines");
if (oldState.contains("color")) {
try {
oldSignColor = DyeColor.valueOf(oldState.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
oldGlowing = oldState.getBoolean("glowing", false);
}
StringBuilder sb = new StringBuilder();
if (!lines.equals(oldLines)) {
for (String line : lines) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append("[").append(line).append("]");
}
}
if (signColor != oldSignColor) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append("(color: " + signColor.name().toLowerCase() + ")");
}
if (glowing != oldGlowing) {
if (sb.length() > 0) {
sb.append(" ");
}
if (glowing) {
sb.append("(glowing)");
} else {
sb.append("(not glowing)");
}
}
return sb.toString();
}
return null;
}
}