forked from elBukkit/EffectLib
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTextEffect.java
More file actions
137 lines (112 loc) · 3.72 KB
/
TextEffect.java
File metadata and controls
137 lines (112 loc) · 3.72 KB
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
package de.slikey.effectlib.effect;
import java.awt.Font;
import java.awt.Color;
import java.util.Objects;
import java.awt.image.BufferedImage;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.util.Vector;
import de.slikey.effectlib.Effect;
import de.slikey.effectlib.EffectType;
import de.slikey.effectlib.EffectManager;
import de.slikey.effectlib.util.MathUtils;
import de.slikey.effectlib.util.VectorUtils;
import de.slikey.effectlib.util.StringParser;
public class TextEffect extends Effect {
/**
* Text to display
*/
public String text = "Text";
/**
* Invert the text
*/
public boolean invert = false;
/**
* Each stepX pixel will be shown. Saves packets for lower fontsizes.
*/
public int stepX = 1;
/**
* Each stepY pixel will be shown. Saves packets for lower fontsizes.
*/
public int stepY = 1;
/**
* Scale the font down
*/
public float size = (float) 1 / 5;
/**
* Set this only to true if you are working with changing text. I'll advice
* the parser to recalculate the BufferedImage every iteration.
* Recommended FALSE
*/
public boolean realtime = false;
/**
* Font to create the Text
*/
public Font font;
/**
* Contains an image version of the String
*/
protected BufferedImage image = null;
/**
* Track the text used most recently when parsing
*/
private String lastParsedText = null;
/**
* Track the font used most recently when parsing
*/
private Font lastParsedFont = null;
public TextEffect(EffectManager effectManager) {
super(effectManager);
font = new Font("Tahoma", Font.PLAIN, 16);
type = EffectType.REPEATING;
particle = Particle.FIREWORK;
period = 40;
iterations = 20;
}
public void setFont(Font font) {
this.font = font;
}
@Override
public void onRun() {
if (font == null) {
cancel();
return;
}
Location location = getLocation();
if (location == null) {
cancel();
return;
}
int clr;
Vector v;
try {
if (image == null || shouldRecalculateImage()) {
lastParsedText = text;
lastParsedFont = font;
// Use last parsed references instead for additional thread safety
image = StringParser.stringToBufferedImage(lastParsedFont, lastParsedText);
}
for (int y = 0; y < image.getHeight(); y += stepY) {
for (int x = 0; x < image.getWidth(); x += stepX) {
clr = image.getRGB(x, y);
if (!invert && Color.black.getRGB() != clr) continue;
else if (invert && Color.black.getRGB() == clr) continue;
v = new Vector((float) image.getWidth() / 2 - x, (float) image.getHeight() / 2 - y, 0).multiply(size);
VectorUtils.rotateAroundAxisY(v, -location.getYaw() * MathUtils.degreesToRadians);
display(particle, location.add(v));
location.subtract(v);
}
}
} catch (Exception ex) {
// This seems to happen on bad characters in strings,
// I'm choosing to ignore the exception and cancel the effect for now.
cancel();
}
}
private boolean shouldRecalculateImage() {
// Don't bother if we don't use real time updates
if (!realtime) return false;
// Text content or font is different, recalculate
return !Objects.equals(lastParsedText, text) || !Objects.equals(lastParsedFont, font);
}
}