Skip to content

fix: fork texture util where required #3054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fastasyncworldedit.core.function.pattern;

import com.fastasyncworldedit.core.util.TextureHolder;
import com.fastasyncworldedit.core.util.TextureUtil;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.pattern.Pattern;
Expand All @@ -13,7 +14,7 @@

public class AngleColorPattern extends AnglePattern {

protected transient TextureHolder holder;
protected transient TextureUtil util;

/**
* Create a new {@link Pattern} instance
Expand All @@ -24,7 +25,20 @@ public class AngleColorPattern extends AnglePattern {
*/
public AngleColorPattern(Extent extent, TextureHolder holder, int distance) {
super(extent, distance);
this.holder = holder.getTextureUtil();
this.util = holder.getTextureUtil();
}

/**
* Create a new {@link Pattern} instance
*
* @param extent extent to set to
* @param distance distance to use to calculate angle
* @param util {@link TextureUtil} to use to get textures
* @since TODO
*/
private AngleColorPattern(Extent extent, int distance, TextureUtil util) {
super(extent, distance);
this.util = util;
}

private int getColor(int color, int slope) {
Expand Down Expand Up @@ -66,15 +80,15 @@ public BaseBlock applyBlock(BlockVector3 position) {
BlockType type = block.getBlockType();
int color;
if (type == BlockTypes.GRASS_BLOCK) {
color = holder.getTextureUtil().getColor(extent.getBiome(position));
color = util.getColor(extent.getBiome(position));
} else {
color = holder.getTextureUtil().getColor(type);
color = util.getColor(type);
}
if (color == 0) {
return block;
}
int newColor = getColor(color, slope);
return holder.getTextureUtil().getNearestBlock(newColor).getDefaultState().toBaseBlock();
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
}

@Override
Expand All @@ -87,19 +101,24 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
BlockType type = block.getBlockType();
int color;
if (type == BlockTypes.GRASS_BLOCK) {
color = holder.getTextureUtil().getColor(extent.getBiome(get));
color = util.getColor(extent.getBiome(get));
} else {
color = holder.getTextureUtil().getColor(type);
color = util.getColor(type);
}
if (color == 0) {
return false;
}
int newColor = getColor(color, slope);
BlockType newBlock = holder.getTextureUtil().getNearestBlock(newColor);
BlockType newBlock = util.getNearestBlock(newColor);
if (newBlock == null) {
return false;
}
return set.setBlock(extent, newBlock.getDefaultState());
}

@Override
public Pattern fork() {
return new AngleColorPattern(extent, distance, util.fork());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class AverageColorPattern extends AbstractExtentPattern {

private final transient TextureHolder holder;
private final transient TextureUtil util;
private final int color;

/**
Expand All @@ -30,21 +30,34 @@ public class AverageColorPattern extends AbstractExtentPattern {
*/
public AverageColorPattern(Extent extent, TextureHolder holder, int r, int g, int b, int a) {
super(extent);
this.holder = holder;
this.util = holder.getTextureUtil();
this.color = new Color(MathMan.clamp(r, 0, 255), MathMan.clamp(g, 0, 255), MathMan.clamp(b, 0, 255), MathMan.clamp(a, 0
, 255)).getRGB();
}

/**
* Create a new {@link Pattern} instance
*
* @param extent extent to set to
* @param util {@link TextureUtil} to use to get textures
* @param color color to saturate to
* @since TODO
*/
private AverageColorPattern(Extent extent, TextureUtil util, int color) {
super(extent);
this.util = util;
this.color = color;
}

@Override
public BaseBlock applyBlock(BlockVector3 position) {
BaseBlock block = getExtent().getFullBlock(position);
TextureUtil util = holder.getTextureUtil();
BlockType type = block.getBlockType();
int currentColor;
if (type == BlockTypes.GRASS_BLOCK) {
currentColor = holder.getTextureUtil().getColor(getExtent().getBiome(position));
currentColor = util.getColor(getExtent().getBiome(position));
} else {
currentColor = holder.getTextureUtil().getColor(type);
currentColor = util.getColor(type);
}
int newColor = TextureUtil.averageColor(currentColor, color);
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
Expand All @@ -53,12 +66,11 @@ public BaseBlock applyBlock(BlockVector3 position) {
@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
BlockType blockType = get.getBlock(extent).getBlockType();
TextureUtil util = holder.getTextureUtil();
int currentColor;
if (blockType == BlockTypes.GRASS_BLOCK) {
currentColor = holder.getTextureUtil().getColor(extent.getBiome(get));
currentColor = util.getColor(extent.getBiome(get));
} else {
currentColor = holder.getTextureUtil().getColor(blockType);
currentColor = util.getColor(blockType);
}
if (currentColor == 0) {
return false;
Expand All @@ -71,4 +83,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
return set.setBlock(extent, newBlock.getDefaultState());
}

@Override
public Pattern fork() {
return new AverageColorPattern(getExtent(), util, color);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public class DesaturatePattern extends AbstractPattern {

private final TextureHolder holder;
private final Extent extent;
private final transient TextureUtil util;
private final transient Extent extent;
private final double value;

/**
Expand All @@ -26,19 +26,32 @@ public class DesaturatePattern extends AbstractPattern {
*/
public DesaturatePattern(Extent extent, TextureHolder holder, double value) {
this.extent = extent;
this.holder = holder;
this.util = holder.getTextureUtil();
this.value = Math.max(0, Math.min(1, value));
}

/**
* Create a new {@link Pattern} instance
*
* @param extent extent to set to
* @param value decimal percent to desaturate by (0 -> 1)
* @param util {@link TextureUtil} to use for textures
* @since TODO
*/
private DesaturatePattern(Extent extent, double value, TextureUtil util) {
this.extent = extent;
this.util = util;
this.value = Math.max(0, Math.min(1, value));
}

@Override
public BaseBlock applyBlock(BlockVector3 position) {
BlockType type = extent.getBlock(position).getBlockType();
TextureUtil util = holder.getTextureUtil();
int color;
if (type == BlockTypes.GRASS_BLOCK) {
color = holder.getTextureUtil().getColor(extent.getBiome(position));
color = util.getColor(extent.getBiome(position));
} else {
color = holder.getTextureUtil().getColor(type);
color = util.getColor(type);
}
return util.getNearestBlock(color).getDefaultState().toBaseBlock();
}
Expand All @@ -58,12 +71,11 @@ private int getColor(int color) {
@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
BlockType type = get.getBlock(extent).getBlockType();
TextureUtil util = holder.getTextureUtil();
int color;
if (type == BlockTypes.GRASS_BLOCK) {
color = holder.getTextureUtil().getColor(extent.getBiome(get));
color = util.getColor(extent.getBiome(get));
} else {
color = holder.getTextureUtil().getColor(type);
color = util.getColor(type);
}
int newColor = getColor(color);
if (newColor == color) {
Expand All @@ -76,4 +88,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
return set.setBlock(extent, newType.getDefaultState());
}

@Override
public Pattern fork() {
return new DesaturatePattern(extent, value, util);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

public class SaturatePattern extends AbstractPattern {

private final TextureHolder holder;
private final transient TextureUtil util;
private final transient Extent extent;
private final int color;
private final Extent extent;

/**
* Create a new {@link Pattern} instance
Expand All @@ -32,20 +32,33 @@ public class SaturatePattern extends AbstractPattern {
*/
public SaturatePattern(Extent extent, TextureHolder holder, int r, int g, int b, int a) {
this.extent = extent;
this.holder = holder;
this.util = holder.getTextureUtil();
this.color = new Color(MathMan.clamp(r, 0, 255), MathMan.clamp(g, 0, 255), MathMan.clamp(b, 0, 255), MathMan.clamp(a, 0
, 255)).getRGB();
}

/**
* Create a new {@link Pattern} instance
*
* @param extent extent to set to
* @param util {@link TextureUtil} to use to get textures
* @param color color to saturate to
* @since TODO
*/
private SaturatePattern(Extent extent, TextureUtil util, int color) {
this.extent = extent;
this.util = util;
this.color = color;
}

@Override
public BaseBlock applyBlock(BlockVector3 position) {
BlockType type = extent.getBlock(position).getBlockType();
TextureUtil util = holder.getTextureUtil();
int currentColor;
if (type == BlockTypes.GRASS_BLOCK) {
currentColor = holder.getTextureUtil().getColor(extent.getBiome(position));
currentColor = util.getColor(extent.getBiome(position));
} else {
currentColor = holder.getTextureUtil().getColor(type);
currentColor = util.getColor(type);
}
int newColor = TextureUtil.multiplyColor(currentColor, color);
return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
Expand All @@ -54,12 +67,11 @@ public BaseBlock applyBlock(BlockVector3 position) {
@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
BlockType type = get.getBlock(extent).getBlockType();
TextureUtil util = holder.getTextureUtil();
int currentColor;
if (type == BlockTypes.GRASS_BLOCK) {
currentColor = holder.getTextureUtil().getColor(extent.getBiome(get));
currentColor = util.getColor(extent.getBiome(get));
} else {
currentColor = holder.getTextureUtil().getColor(type);
currentColor = util.getColor(type);
}
if (currentColor == 0) {
return false;
Expand All @@ -72,4 +84,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
return set.setBlock(extent, newBlock.getDefaultState());
}

@Override
public Pattern fork() {
return new SaturatePattern(extent, util.fork(), color);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

public class ShadePattern extends AbstractPattern {

private final TextureUtil util;
private final Extent extent;
private final transient TextureUtil util;
private final transient Extent extent;
private final boolean darken;

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

/**
* Create a new {@link Pattern} instance
*
* @param extent extent to set to
* @param darken if the shade should darken or lighten colours
* @param util {@link TextureUtil} to use for textures
* @since TODO
*/
private ShadePattern(Extent extent, boolean darken, TextureUtil util) {
checkNotNull(extent);
this.extent = extent;
this.util = util;
this.darken = darken;
}

@Override
public BaseBlock applyBlock(BlockVector3 position) {
BlockType block = extent.getBlock(position).getBlockType();
Expand Down Expand Up @@ -62,4 +77,9 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
return false;
}

@Override
public Pattern fork() {
return new ShadePattern(extent, darken, util.fork());
}

}
Loading
Loading