Skip to content

Commit 32b284d

Browse files
committed
Add background color to text display spec
1 parent 206fecc commit 32b284d

File tree

11 files changed

+181
-11
lines changed

11 files changed

+181
-11
lines changed

src/main/java/com/laytonsmith/abstraction/Convertor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ public interface Convertor {
201201
*/
202202
MCColor GetColor(int red, int green, int blue);
203203

204+
/**
205+
* Returns a transparent color object for this server.
206+
*
207+
* @param red
208+
* @param green
209+
* @param blue
210+
* @param alpha
211+
* @return
212+
*/
213+
MCColor GetColor(int red, int green, int blue, int alpha);
214+
204215
/**
205216
* Returns a color object given the color name. The color name must come from the standard color types, or a
206217
* FormatException is thrown.

src/main/java/com/laytonsmith/abstraction/MCColor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public interface MCColor {
9797
*/
9898
Map<String, MCColor> STANDARD_COLORS = Internal.buildColors();
9999

100+
int getAlpha();
101+
100102
int getRed();
101103

102104
int getGreen();
@@ -113,6 +115,17 @@ public interface MCColor {
113115
*/
114116
MCColor build(int red, int green, int blue);
115117

118+
/**
119+
* Returns a NEW instance of a color, given the specified ARGB values.
120+
*
121+
* @param alpha
122+
* @param red
123+
* @param green
124+
* @param blue
125+
* @return
126+
*/
127+
MCColor build(int alpha, int red, int green, int blue);
128+
116129
class Internal {
117130

118131
private static Map<String, MCColor> buildColors() {

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitConvertor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,11 @@ public MCColor GetColor(int red, int green, int blue) {
775775
return BukkitMCColor.GetMCColor(Color.fromRGB(red, green, blue));
776776
}
777777

778+
@Override
779+
public MCColor GetColor(int red, int green, int blue, int alpha) {
780+
return BukkitMCColor.GetMCColor(Color.fromARGB(alpha, red, green, blue));
781+
}
782+
778783
@Override
779784
public MCColor GetColor(String colorName, Target t) throws CREFormatException {
780785
return ConvertorHelper.GetColor(colorName, t);

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCColor.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public final class BukkitMCColor implements MCColor {
88
private static final BukkitMCColor BUILDER = new BukkitMCColor();
99

1010
public static MCColor GetMCColor(Color c) {
11-
return BUILDER.build(c.getRed(), c.getGreen(), c.getBlue());
11+
return BUILDER.build(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
1212
}
1313

1414
public static Color GetColor(MCColor c) {
15-
return Color.fromRGB(c.getRed(), c.getGreen(), c.getBlue());
15+
return Color.fromARGB(c.getAlpha(), c.getRed(), c.getGreen(), c.getBlue());
1616
}
1717

1818
private BukkitMCColor() {
@@ -21,6 +21,12 @@ private BukkitMCColor() {
2121
private int red;
2222
private int green;
2323
private int blue;
24+
private int alpha;
25+
26+
@Override
27+
public int getAlpha() {
28+
return alpha;
29+
}
2430

2531
@Override
2632
public int getRed() {
@@ -40,6 +46,17 @@ public int getBlue() {
4046
@Override
4147
public MCColor build(int red, int green, int blue) {
4248
BukkitMCColor color = new BukkitMCColor();
49+
color.alpha = 255;
50+
color.red = red;
51+
color.green = green;
52+
color.blue = blue;
53+
return color;
54+
}
55+
56+
@Override
57+
public MCColor build(int red, int green, int blue, int alpha) {
58+
BukkitMCColor color = new BukkitMCColor();
59+
color.alpha = alpha;
4360
color.red = red;
4461
color.green = green;
4562
color.blue = blue;
@@ -49,6 +66,7 @@ public MCColor build(int red, int green, int blue) {
4966
@Override
5067
public int hashCode() {
5168
int hash = 5;
69+
hash = 11 * hash + this.alpha;
5270
hash = 11 * hash + this.red;
5371
hash = 11 * hash + this.green;
5472
hash = 11 * hash + this.blue;
@@ -73,6 +91,9 @@ public boolean equals(Object obj) {
7391
if(this.blue != other.blue) {
7492
return false;
7593
}
94+
if(this.alpha != other.alpha) {
95+
return false;
96+
}
7697
return true;
7798
}
7899
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCTextDisplay.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.laytonsmith.abstraction.bukkit.entities;
22

3+
import com.laytonsmith.abstraction.MCColor;
4+
import com.laytonsmith.abstraction.bukkit.BukkitMCColor;
35
import com.laytonsmith.abstraction.entities.MCTextDisplay;
6+
import org.bukkit.Color;
47
import org.bukkit.entity.Entity;
58
import org.bukkit.entity.TextDisplay;
69
import org.bukkit.entity.TextDisplay.TextAlignment;
@@ -25,13 +28,26 @@ public void setAlignment(MCTextDisplay.Alignment alignment) {
2528
}
2629

2730
@Override
28-
public boolean usesDefaultBackground() {
29-
return td.isDefaultBackground();
31+
public MCColor getBackgroundColor() {
32+
if(td.isDefaultBackground()) {
33+
return null;
34+
}
35+
Color color = td.getBackgroundColor();
36+
if(color == null) {
37+
return null;
38+
}
39+
return BukkitMCColor.GetMCColor(color);
3040
}
3141

3242
@Override
33-
public void setUsesDefaultBackground(boolean defaultBackground) {
34-
td.setDefaultBackground(defaultBackground);
43+
public void setBackgroundColor(MCColor color) {
44+
if(color == null) {
45+
td.setDefaultBackground(true);
46+
td.setBackgroundColor(null);
47+
} else {
48+
td.setDefaultBackground(false);
49+
td.setBackgroundColor(BukkitMCColor.GetColor(color));
50+
}
3551
}
3652

3753
@Override

src/main/java/com/laytonsmith/abstraction/entities/MCTextDisplay.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.laytonsmith.abstraction.entities;
22

3+
import com.laytonsmith.abstraction.MCColor;
4+
35
public interface MCTextDisplay extends MCDisplay {
46

57
Alignment getAlignment();
68

79
void setAlignment(Alignment alignment);
810

9-
boolean usesDefaultBackground();
11+
MCColor getBackgroundColor();
1012

11-
void setUsesDefaultBackground(boolean defaultBackground);
13+
void setBackgroundColor(MCColor color);
1214

1315
int getLineWidth();
1416

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,8 @@ public CArray color(MCColor color, Target t) {
15641564

15651565
/**
15661566
* Returns an MCColor given a colorArray, which supports the following three format recipeTypes (in this order of
1567-
* priority) array(r: 0, g: 0, b: 0) array(red: 0, green: 0, blue: 0) array(0, 0, 0)
1567+
* priority) array(r: 0, g: 0, b: 0) array(red: 0, green: 0, blue: 0) array(0, 0, 0).
1568+
* Optionally accepts an alpha channel for the keys: 'a', 'alpha', and 3 respectively.
15681569
*
15691570
* @param color
15701571
* @param t
@@ -1595,13 +1596,44 @@ public MCColor color(CArray color, Target t) {
15951596
} else {
15961597
blue = ArgumentValidation.getInt32(color.get(2, t), t);
15971598
}
1599+
if(color.size() > 3) {
1600+
int alpha;
1601+
if(color.containsKey("a")) {
1602+
alpha = ArgumentValidation.getInt32(color.get("a", t), t);
1603+
} else if(color.containsKey("alpha")) {
1604+
alpha = ArgumentValidation.getInt32(color.get("alpha", t), t);
1605+
} else {
1606+
alpha = ArgumentValidation.getInt32(color.get(3, t), t);
1607+
}
1608+
try {
1609+
return StaticLayer.GetConvertor().GetColor(red, green, blue, alpha);
1610+
} catch (IllegalArgumentException ex) {
1611+
throw new CRERangeException(ex.getMessage(), t, ex);
1612+
}
1613+
}
15981614
try {
15991615
return StaticLayer.GetConvertor().GetColor(red, green, blue);
16001616
} catch (IllegalArgumentException ex) {
16011617
throw new CRERangeException(ex.getMessage(), t, ex);
16021618
}
16031619
}
16041620

1621+
/**
1622+
* Returns a CArray with an alpha channel from a given MCColor. It will be in the format array(a: 0, r: 0, g: 0, b: 0)
1623+
*
1624+
* @param color
1625+
* @param t
1626+
* @return
1627+
*/
1628+
public CArray transparentColor(MCColor color, Target t) {
1629+
CArray ca = CArray.GetAssociativeArray(t);
1630+
ca.set("r", new CInt(color.getRed(), t), t);
1631+
ca.set("g", new CInt(color.getGreen(), t), t);
1632+
ca.set("b", new CInt(color.getBlue(), t), t);
1633+
ca.set("a", new CInt(color.getAlpha(), t), t);
1634+
return ca;
1635+
}
1636+
16051637
/**
16061638
* Gets a vector object, given a Vector.
16071639
*

src/main/java/com/laytonsmith/core/functions/EntityManagement.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,13 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
22602260
specArray.set(entity_spec.KEY_DISPLAY_TEXT_LINE_WIDTH, new CInt(tDisplay.getLineWidth(), t), t);
22612261
specArray.set(entity_spec.KEY_DISPLAY_TEXT_SEE_THROUGH, CBoolean.get(tDisplay.isVisibleThroughBlocks()), t);
22622262
specArray.set(entity_spec.KEY_DISPLAY_TEXT_SHADOW, CBoolean.get(tDisplay.hasShadow()), t);
2263+
MCColor color = tDisplay.getBackgroundColor();
2264+
if(color == null) {
2265+
specArray.set(entity_spec.KEY_DISPLAY_TEXT_BACKGROUND_COLOR, CNull.NULL, t);
2266+
} else {
2267+
specArray.set(entity_spec.KEY_DISPLAY_TEXT_BACKGROUND_COLOR,
2268+
ObjectGenerator.GetGenerator().transparentColor(color, t), t);
2269+
}
22632270
long opacity = tDisplay.getOpacity();
22642271
if(opacity < 0) {
22652272
opacity += 256;
@@ -2391,6 +2398,7 @@ public MSVersion since() {
23912398
private static final String KEY_DISPLAY_TEXT_LINE_WIDTH = "linewidth";
23922399
private static final String KEY_DISPLAY_TEXT_SEE_THROUGH = "seethrough";
23932400
private static final String KEY_DISPLAY_TEXT_SHADOW = "shadow";
2401+
private static final String KEY_DISPLAY_TEXT_BACKGROUND_COLOR = "bgcolor";
23942402
private static final String KEY_DISPLAY_TEXT_OPACITY = "opacity";
23952403
private static final String KEY_DROPPED_ITEM_ITEMSTACK = "itemstack";
23962404
private static final String KEY_DROPPED_ITEM_PICKUPDELAY = "pickupdelay";
@@ -3777,6 +3785,16 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
37773785
case entity_spec.KEY_DISPLAY_TEXT_SHADOW:
37783786
tDisplay.setHasShadow(ArgumentValidation.getBooleanObject(specArray.get(index, t), t));
37793787
break;
3788+
case entity_spec.KEY_DISPLAY_TEXT_BACKGROUND_COLOR:
3789+
Mixed color = specArray.get(index, t);
3790+
if(color.isInstanceOf(CArray.TYPE)) {
3791+
tDisplay.setBackgroundColor(ObjectGenerator.GetGenerator().color((CArray) color, t));
3792+
} else if(color instanceof CNull) {
3793+
tDisplay.setBackgroundColor(null);
3794+
} else {
3795+
throw new CRECastException("Expected a color array for text display background color.", t);
3796+
}
3797+
break;
37803798
case entity_spec.KEY_DISPLAY_TEXT_OPACITY:
37813799
long opacity = ArgumentValidation.getInt(specArray.get(index, t), t);
37823800
if(opacity < 0 || opacity > 255) {

src/main/java/com/laytonsmith/tools/Interpreter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ public MCColor GetColor(int red, int green, int blue) {
12231223
throw new UnsupportedOperationException("This method is not supported from a shell.");
12241224
}
12251225

1226+
@Override
1227+
public MCColor GetColor(int red, int green, int blue, int alpha) {
1228+
throw new UnsupportedOperationException("This method is not supported from a shell.");
1229+
}
1230+
12261231
@Override
12271232
public MCColor GetColor(String colorName, Target t) throws CREFormatException {
12281233
throw new UnsupportedOperationException("This method is not supported from a shell.");

src/main/resources/functionDocs/entity_spec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ without knowing the rotations on the other axis or of other body parts beforehan
303303
* %KEY_DISPLAY_TEXT_ALIGNMENT%: The text alignment. Can be CENTER (default), LEFT or RIGHT.
304304
* %KEY_DISPLAY_TEXT%: The text for this display entity.
305305
* %KEY_DISPLAY_TEXT_LINE_WIDTH%: The max width for a line of text before splitting. (default: 200)
306-
* %KEY_DISPLAY_TEXT_SEE_THROUGH%: Whether the text is visible through blocks. (default: false)
306+
* %KEY_DISPLAY_TEXT_SEE_THROUGH%: Whether the text is visible through blocks and entities are visible through the background. (default: false)
307307
* %KEY_DISPLAY_TEXT_SHADOW%: Whether the text has a shadow. (default: false) Not to be confused with an entity shadow.
308-
* %KEY_DISPLAY_TEXT_OPACITY%: The alpha opacity of the text from 0 to 255. (default: 255)
308+
* %KEY_DISPLAY_TEXT_OPACITY%: The alpha opacity of the text from 0 to 255. (default: 255) If value is less than 26, the text will not be rendered.
309+
* %KEY_DISPLAY_TEXT_BACKGROUND_COLOR%: An transparent color array for the background. ("r", "g", "b" and "a" keys with 0-255 integer values) If null, it will use the default text background. If alpha channel is less than 26, the background will not be rendered.
309310
|-
310311
| TRIDENT
311312
|

0 commit comments

Comments
 (0)