Skip to content

Commit 7a6d17d

Browse files
authored
fix: fork texture util where required (#3054)
- should fix #3049
1 parent b27f671 commit 7a6d17d

File tree

9 files changed

+223
-49
lines changed

9 files changed

+223
-49
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/AngleColorPattern.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fastasyncworldedit.core.function.pattern;
22

33
import com.fastasyncworldedit.core.util.TextureHolder;
4+
import com.fastasyncworldedit.core.util.TextureUtil;
45
import com.sk89q.worldedit.WorldEditException;
56
import com.sk89q.worldedit.extent.Extent;
67
import com.sk89q.worldedit.function.pattern.Pattern;
@@ -13,7 +14,7 @@
1314

1415
public class AngleColorPattern extends AnglePattern {
1516

16-
protected transient TextureHolder holder;
17+
protected transient TextureUtil util;
1718

1819
/**
1920
* Create a new {@link Pattern} instance
@@ -24,7 +25,20 @@ public class AngleColorPattern extends AnglePattern {
2425
*/
2526
public AngleColorPattern(Extent extent, TextureHolder holder, int distance) {
2627
super(extent, distance);
27-
this.holder = holder.getTextureUtil();
28+
this.util = holder.getTextureUtil();
29+
}
30+
31+
/**
32+
* Create a new {@link Pattern} instance
33+
*
34+
* @param extent extent to set to
35+
* @param distance distance to use to calculate angle
36+
* @param util {@link TextureUtil} to use to get textures
37+
* @since TODO
38+
*/
39+
private AngleColorPattern(Extent extent, int distance, TextureUtil util) {
40+
super(extent, distance);
41+
this.util = util;
2842
}
2943

3044
private int getColor(int color, int slope) {
@@ -66,15 +80,15 @@ public BaseBlock applyBlock(BlockVector3 position) {
6680
BlockType type = block.getBlockType();
6781
int color;
6882
if (type == BlockTypes.GRASS_BLOCK) {
69-
color = holder.getTextureUtil().getColor(extent.getBiome(position));
83+
color = util.getColor(extent.getBiome(position));
7084
} else {
71-
color = holder.getTextureUtil().getColor(type);
85+
color = util.getColor(type);
7286
}
7387
if (color == 0) {
7488
return block;
7589
}
7690
int newColor = getColor(color, slope);
77-
return holder.getTextureUtil().getNearestBlock(newColor).getDefaultState().toBaseBlock();
91+
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
7892
}
7993

8094
@Override
@@ -87,19 +101,24 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
87101
BlockType type = block.getBlockType();
88102
int color;
89103
if (type == BlockTypes.GRASS_BLOCK) {
90-
color = holder.getTextureUtil().getColor(extent.getBiome(get));
104+
color = util.getColor(extent.getBiome(get));
91105
} else {
92-
color = holder.getTextureUtil().getColor(type);
106+
color = util.getColor(type);
93107
}
94108
if (color == 0) {
95109
return false;
96110
}
97111
int newColor = getColor(color, slope);
98-
BlockType newBlock = holder.getTextureUtil().getNearestBlock(newColor);
112+
BlockType newBlock = util.getNearestBlock(newColor);
99113
if (newBlock == null) {
100114
return false;
101115
}
102116
return set.setBlock(extent, newBlock.getDefaultState());
103117
}
104118

119+
@Override
120+
public Pattern fork() {
121+
return new AngleColorPattern(extent, distance, util.fork());
122+
}
123+
105124
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/AverageColorPattern.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class AverageColorPattern extends AbstractExtentPattern {
1717

18-
private final transient TextureHolder holder;
18+
private final transient TextureUtil util;
1919
private final int color;
2020

2121
/**
@@ -30,21 +30,34 @@ public class AverageColorPattern extends AbstractExtentPattern {
3030
*/
3131
public AverageColorPattern(Extent extent, TextureHolder holder, int r, int g, int b, int a) {
3232
super(extent);
33-
this.holder = holder;
33+
this.util = holder.getTextureUtil();
3434
this.color = new Color(MathMan.clamp(r, 0, 255), MathMan.clamp(g, 0, 255), MathMan.clamp(b, 0, 255), MathMan.clamp(a, 0
3535
, 255)).getRGB();
3636
}
3737

38+
/**
39+
* Create a new {@link Pattern} instance
40+
*
41+
* @param extent extent to set to
42+
* @param util {@link TextureUtil} to use to get textures
43+
* @param color color to saturate to
44+
* @since TODO
45+
*/
46+
private AverageColorPattern(Extent extent, TextureUtil util, int color) {
47+
super(extent);
48+
this.util = util;
49+
this.color = color;
50+
}
51+
3852
@Override
3953
public BaseBlock applyBlock(BlockVector3 position) {
4054
BaseBlock block = getExtent().getFullBlock(position);
41-
TextureUtil util = holder.getTextureUtil();
4255
BlockType type = block.getBlockType();
4356
int currentColor;
4457
if (type == BlockTypes.GRASS_BLOCK) {
45-
currentColor = holder.getTextureUtil().getColor(getExtent().getBiome(position));
58+
currentColor = util.getColor(getExtent().getBiome(position));
4659
} else {
47-
currentColor = holder.getTextureUtil().getColor(type);
60+
currentColor = util.getColor(type);
4861
}
4962
int newColor = TextureUtil.averageColor(currentColor, color);
5063
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
@@ -53,12 +66,11 @@ public BaseBlock applyBlock(BlockVector3 position) {
5366
@Override
5467
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
5568
BlockType blockType = get.getBlock(extent).getBlockType();
56-
TextureUtil util = holder.getTextureUtil();
5769
int currentColor;
5870
if (blockType == BlockTypes.GRASS_BLOCK) {
59-
currentColor = holder.getTextureUtil().getColor(extent.getBiome(get));
71+
currentColor = util.getColor(extent.getBiome(get));
6072
} else {
61-
currentColor = holder.getTextureUtil().getColor(blockType);
73+
currentColor = util.getColor(blockType);
6274
}
6375
if (currentColor == 0) {
6476
return false;
@@ -71,4 +83,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
7183
return set.setBlock(extent, newBlock.getDefaultState());
7284
}
7385

86+
@Override
87+
public Pattern fork() {
88+
return new AverageColorPattern(getExtent(), util, color);
89+
}
90+
7491
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/DesaturatePattern.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
public class DesaturatePattern extends AbstractPattern {
1515

16-
private final TextureHolder holder;
17-
private final Extent extent;
16+
private final transient TextureUtil util;
17+
private final transient Extent extent;
1818
private final double value;
1919

2020
/**
@@ -26,19 +26,32 @@ public class DesaturatePattern extends AbstractPattern {
2626
*/
2727
public DesaturatePattern(Extent extent, TextureHolder holder, double value) {
2828
this.extent = extent;
29-
this.holder = holder;
29+
this.util = holder.getTextureUtil();
30+
this.value = Math.max(0, Math.min(1, value));
31+
}
32+
33+
/**
34+
* Create a new {@link Pattern} instance
35+
*
36+
* @param extent extent to set to
37+
* @param value decimal percent to desaturate by (0 -> 1)
38+
* @param util {@link TextureUtil} to use for textures
39+
* @since TODO
40+
*/
41+
private DesaturatePattern(Extent extent, double value, TextureUtil util) {
42+
this.extent = extent;
43+
this.util = util;
3044
this.value = Math.max(0, Math.min(1, value));
3145
}
3246

3347
@Override
3448
public BaseBlock applyBlock(BlockVector3 position) {
3549
BlockType type = extent.getBlock(position).getBlockType();
36-
TextureUtil util = holder.getTextureUtil();
3750
int color;
3851
if (type == BlockTypes.GRASS_BLOCK) {
39-
color = holder.getTextureUtil().getColor(extent.getBiome(position));
52+
color = util.getColor(extent.getBiome(position));
4053
} else {
41-
color = holder.getTextureUtil().getColor(type);
54+
color = util.getColor(type);
4255
}
4356
return util.getNearestBlock(color).getDefaultState().toBaseBlock();
4457
}
@@ -58,12 +71,11 @@ private int getColor(int color) {
5871
@Override
5972
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
6073
BlockType type = get.getBlock(extent).getBlockType();
61-
TextureUtil util = holder.getTextureUtil();
6274
int color;
6375
if (type == BlockTypes.GRASS_BLOCK) {
64-
color = holder.getTextureUtil().getColor(extent.getBiome(get));
76+
color = util.getColor(extent.getBiome(get));
6577
} else {
66-
color = holder.getTextureUtil().getColor(type);
78+
color = util.getColor(type);
6779
}
6880
int newColor = getColor(color);
6981
if (newColor == color) {
@@ -76,4 +88,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
7688
return set.setBlock(extent, newType.getDefaultState());
7789
}
7890

91+
@Override
92+
public Pattern fork() {
93+
return new DesaturatePattern(extent, value, util);
94+
}
95+
7996
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/SaturatePattern.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
public class SaturatePattern extends AbstractPattern {
1818

19-
private final TextureHolder holder;
19+
private final transient TextureUtil util;
20+
private final transient Extent extent;
2021
private final int color;
21-
private final Extent extent;
2222

2323
/**
2424
* Create a new {@link Pattern} instance
@@ -32,20 +32,33 @@ public class SaturatePattern extends AbstractPattern {
3232
*/
3333
public SaturatePattern(Extent extent, TextureHolder holder, int r, int g, int b, int a) {
3434
this.extent = extent;
35-
this.holder = holder;
35+
this.util = holder.getTextureUtil();
3636
this.color = new Color(MathMan.clamp(r, 0, 255), MathMan.clamp(g, 0, 255), MathMan.clamp(b, 0, 255), MathMan.clamp(a, 0
3737
, 255)).getRGB();
3838
}
3939

40+
/**
41+
* Create a new {@link Pattern} instance
42+
*
43+
* @param extent extent to set to
44+
* @param util {@link TextureUtil} to use to get textures
45+
* @param color color to saturate to
46+
* @since TODO
47+
*/
48+
private SaturatePattern(Extent extent, TextureUtil util, int color) {
49+
this.extent = extent;
50+
this.util = util;
51+
this.color = color;
52+
}
53+
4054
@Override
4155
public BaseBlock applyBlock(BlockVector3 position) {
4256
BlockType type = extent.getBlock(position).getBlockType();
43-
TextureUtil util = holder.getTextureUtil();
4457
int currentColor;
4558
if (type == BlockTypes.GRASS_BLOCK) {
46-
currentColor = holder.getTextureUtil().getColor(extent.getBiome(position));
59+
currentColor = util.getColor(extent.getBiome(position));
4760
} else {
48-
currentColor = holder.getTextureUtil().getColor(type);
61+
currentColor = util.getColor(type);
4962
}
5063
int newColor = TextureUtil.multiplyColor(currentColor, color);
5164
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
@@ -54,12 +67,11 @@ public BaseBlock applyBlock(BlockVector3 position) {
5467
@Override
5568
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
5669
BlockType type = get.getBlock(extent).getBlockType();
57-
TextureUtil util = holder.getTextureUtil();
5870
int currentColor;
5971
if (type == BlockTypes.GRASS_BLOCK) {
60-
currentColor = holder.getTextureUtil().getColor(extent.getBiome(get));
72+
currentColor = util.getColor(extent.getBiome(get));
6173
} else {
62-
currentColor = holder.getTextureUtil().getColor(type);
74+
currentColor = util.getColor(type);
6375
}
6476
if (currentColor == 0) {
6577
return false;
@@ -72,4 +84,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
7284
return set.setBlock(extent, newBlock.getDefaultState());
7385
}
7486

87+
@Override
88+
public Pattern fork() {
89+
return new SaturatePattern(extent, util.fork(), color);
90+
}
91+
7592
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/ShadePattern.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
public class ShadePattern extends AbstractPattern {
1717

18-
private final TextureUtil util;
19-
private final Extent extent;
18+
private final transient TextureUtil util;
19+
private final transient Extent extent;
2020
private final boolean darken;
2121

2222
/**
@@ -33,6 +33,21 @@ public ShadePattern(Extent extent, TextureHolder holder, boolean darken) {
3333
this.darken = darken;
3434
}
3535

36+
/**
37+
* Create a new {@link Pattern} instance
38+
*
39+
* @param extent extent to set to
40+
* @param darken if the shade should darken or lighten colours
41+
* @param util {@link TextureUtil} to use for textures
42+
* @since TODO
43+
*/
44+
private ShadePattern(Extent extent, boolean darken, TextureUtil util) {
45+
checkNotNull(extent);
46+
this.extent = extent;
47+
this.util = util;
48+
this.darken = darken;
49+
}
50+
3651
@Override
3752
public BaseBlock applyBlock(BlockVector3 position) {
3853
BlockType block = extent.getBlock(position).getBlockType();
@@ -62,4 +77,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
6277
return false;
6378
}
6479

80+
@Override
81+
public Pattern fork() {
82+
return new ShadePattern(extent, darken, util.fork());
83+
}
84+
6585
}

0 commit comments

Comments
 (0)