Skip to content

Commit ebc3d29

Browse files
committed
Finish WIP, refactor how containers support dye or argb
1 parent 800f3e3 commit ebc3d29

13 files changed

Lines changed: 334 additions & 213 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package gregtech.api.color;
2+
3+
import net.minecraft.item.EnumDyeColor;
4+
5+
public enum ColorMode {
6+
7+
/**
8+
* Only try spraying a block to an {@link EnumDyeColor}.
9+
*/
10+
DYE(true, false),
11+
/**
12+
* Only try spraying a block to an ARGB value.
13+
*/
14+
ARGB(false, true),
15+
/**
16+
* Try spraying the block to an {@link EnumDyeColor}, and if that failed fall back to ARGB.
17+
*/
18+
PREFER_DYE(true, true),
19+
/**
20+
* Try spraying the block to an ARGB value, and if that failed fall back to {@link EnumDyeColor}.
21+
*/
22+
PREFER_ARGB(true, true);
23+
24+
private final boolean dye;
25+
private final boolean argb;
26+
27+
ColorMode(boolean dye, boolean argb) {
28+
this.dye = dye;
29+
this.argb = argb;
30+
}
31+
32+
public boolean dye() {
33+
return dye;
34+
}
35+
36+
public boolean argb() {
37+
return argb;
38+
}
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package gregtech.api.color;
2+
3+
import net.minecraft.item.EnumDyeColor;
4+
import net.minecraft.util.text.ITextComponent;
5+
import net.minecraft.util.text.TextComponentTranslation;
6+
7+
import com.cleanroommc.modularui.api.drawable.IKey;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public enum ColorModeSupport {
11+
12+
/**
13+
* This block only supports being colored to an {@link EnumDyeColor}.
14+
*/
15+
DYE_ONLY("gregtech.color_mode.error.dye"),
16+
/**
17+
* This block only supports being colored to an ARGB value.
18+
*/
19+
ARGB_ONLY("gregtech.color_mode.error.argb"),
20+
/**
21+
* This block supports being colored to a {@link EnumDyeColor} or ARGB value.
22+
*/
23+
EITHER("gregtech.color_mode.error.either");
24+
25+
@NotNull
26+
private final String errorKey;
27+
28+
ColorModeSupport(@NotNull String errorKey) {
29+
this.errorKey = errorKey;
30+
}
31+
32+
public @NotNull String getErrorTranslationKey() {
33+
return errorKey;
34+
}
35+
36+
public @NotNull IKey getErrorKey() {
37+
return IKey.lang(errorKey);
38+
}
39+
40+
public @NotNull ITextComponent getErrorText() {
41+
return new TextComponentTranslation(errorKey);
42+
}
43+
44+
public boolean supportsMode(@NotNull ColorMode colorMode) {
45+
return switch (this) {
46+
case DYE_ONLY -> colorMode.dye();
47+
case ARGB_ONLY -> colorMode.argb();
48+
case EITHER -> true;
49+
};
50+
}
51+
}

src/main/java/gregtech/api/color/ColoredBlockContainer.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import gregtech.api.color.containers.BedColorContainer;
55
import gregtech.api.color.containers.GTPipeColorContainer;
66
import gregtech.api.color.containers.MTEColorContainer;
7-
import gregtech.api.color.containers.NullColorContainer;
87
import gregtech.api.color.containers.VanillaColorContainer;
98
import gregtech.api.util.GTUtility;
109
import gregtech.api.util.Mods;
11-
import gregtech.common.items.behaviors.spray.AbstractSprayBehavior;
1210

1311
import net.minecraft.entity.player.EntityPlayer;
1412
import net.minecraft.item.EnumDyeColor;
13+
import net.minecraft.util.EnumActionResult;
1514
import net.minecraft.util.EnumFacing;
1615
import net.minecraft.util.ResourceLocation;
1716
import net.minecraft.util.math.BlockPos;
@@ -47,16 +46,20 @@ public static void registerContainer(@NotNull ColoredBlockContainer container) {
4746
CONTAINERS.put(id, container);
4847
}
4948

50-
public static @NotNull ColoredBlockContainer getContainer(@NotNull World world, @NotNull BlockPos pos,
51-
@NotNull EnumFacing facing,
52-
@NotNull EntityPlayer player) {
49+
/**
50+
* Get the color container for the block or tile entity at the provided position. <br/>
51+
* Will return {@code null} if no container was valid.
52+
*/
53+
public static @Nullable ColoredBlockContainer getContainer(@NotNull World world, @NotNull BlockPos pos,
54+
@NotNull EnumFacing facing,
55+
@NotNull EntityPlayer player) {
5356
for (ColoredBlockContainer container : CONTAINERS.values()) {
5457
if (container.isBlockValid(world, pos, facing, player)) {
5558
return container;
5659
}
5760
}
5861

59-
return NullColorContainer.NULL_CONTAINER;
62+
return null;
6063
}
6164

6265
@ApiStatus.Internal
@@ -80,19 +83,26 @@ public ColoredBlockContainer(@NotNull ResourceLocation id) {
8083
public abstract boolean isBlockValid(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
8184
@NotNull EntityPlayer player);
8285

83-
public abstract boolean setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
84-
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor);
86+
public @NotNull EnumActionResult setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
87+
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor) {
88+
return EnumActionResult.PASS;
89+
}
8590

86-
public boolean setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
87-
@NotNull EntityPlayer player, int newColor) {
88-
return false;
91+
public @NotNull EnumActionResult setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
92+
@NotNull EntityPlayer player, int newColor) {
93+
return EnumActionResult.PASS;
8994
}
9095

91-
public abstract boolean removeColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
92-
@NotNull EntityPlayer player);
96+
public @NotNull EnumActionResult removeColor(@NotNull World world, @NotNull BlockPos pos,
97+
@NotNull EnumFacing facing,
98+
@NotNull EntityPlayer player) {
99+
return EnumActionResult.PASS;
100+
}
93101

94-
public abstract @Nullable EnumDyeColor getColor(@NotNull World world, @NotNull BlockPos pos,
95-
@NotNull EnumFacing facing, @NotNull EntityPlayer player);
102+
public @Nullable EnumDyeColor getColor(@NotNull World world, @NotNull BlockPos pos,
103+
@NotNull EnumFacing facing, @NotNull EntityPlayer player) {
104+
return null;
105+
}
96106

97107
public int getColorInt(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
98108
@NotNull EntityPlayer player) {
@@ -110,5 +120,5 @@ public boolean colorMatches(@NotNull World world, @NotNull BlockPos pos, @NotNul
110120
return getColorInt(world, pos, facing, player) == color;
111121
}
112122

113-
public abstract boolean supportsMode(@NotNull AbstractSprayBehavior.ColorMode colorMode);
123+
public abstract @NotNull ColorModeSupport getSupportedColorMode();
114124
}

src/main/java/gregtech/api/color/containers/AE2ColorContainer.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package gregtech.api.color.containers;
22

3+
import gregtech.api.color.ColorModeSupport;
34
import gregtech.api.color.ColoredBlockContainer;
4-
import gregtech.common.items.behaviors.spray.AbstractSprayBehavior;
55

66
import net.minecraft.entity.player.EntityPlayer;
77
import net.minecraft.item.EnumDyeColor;
88
import net.minecraft.tileentity.TileEntity;
9+
import net.minecraft.util.EnumActionResult;
910
import net.minecraft.util.EnumFacing;
1011
import net.minecraft.util.ResourceLocation;
1112
import net.minecraft.util.math.BlockPos;
@@ -23,37 +24,38 @@ public AE2ColorContainer(@NotNull ResourceLocation id) {
2324
}
2425

2526
@Override
26-
public boolean setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
27-
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor) {
27+
public @NotNull EnumActionResult setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
28+
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor) {
2829
if (newColor == null) {
2930
return removeColor(world, pos, facing, player);
3031
}
3132

32-
if (getColor(world, pos, facing, player) == newColor) {
33-
return false;
33+
if (colorMatches(world, pos, facing, player, newColor)) {
34+
return EnumActionResult.PASS;
3435
}
3536

3637
TileEntity te = world.getTileEntity(pos);
3738
if (te instanceof IColorableTile colorableTile) {
3839
if (colorableTile.getColor().dye != newColor) {
39-
colorableTile.recolourBlock(facing, AEColor.values()[newColor.ordinal()], player);
40-
return true;
40+
return colorableTile.recolourBlock(facing, AEColor.values()[newColor.ordinal()], player) ?
41+
EnumActionResult.SUCCESS : EnumActionResult.FAIL;
4142
}
4243
}
4344

44-
return false;
45+
return EnumActionResult.PASS;
4546
}
4647

4748
@Override
48-
public boolean removeColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
49-
@NotNull EntityPlayer player) {
49+
public @NotNull EnumActionResult removeColor(@NotNull World world, @NotNull BlockPos pos,
50+
@NotNull EnumFacing facing,
51+
@NotNull EntityPlayer player) {
5052
TileEntity te = world.getTileEntity(pos);
5153
if (te instanceof IColorableTile colorableTile && colorableTile.getColor() != AEColor.TRANSPARENT) {
52-
colorableTile.recolourBlock(facing, AEColor.TRANSPARENT, player);
53-
return true;
54+
return colorableTile.recolourBlock(facing, AEColor.TRANSPARENT, player) ? EnumActionResult.SUCCESS :
55+
EnumActionResult.PASS;
5456
}
5557

56-
return false;
58+
return EnumActionResult.PASS;
5759
}
5860

5961
@Override
@@ -74,7 +76,7 @@ public boolean isBlockValid(@NotNull World world, @NotNull BlockPos pos, @NotNul
7476
}
7577

7678
@Override
77-
public boolean supportsMode(AbstractSprayBehavior.@NotNull ColorMode colorMode) {
78-
return colorMode == AbstractSprayBehavior.ColorMode.DYE_ONLY;
79+
public @NotNull ColorModeSupport getSupportedColorMode() {
80+
return ColorModeSupport.DYE_ONLY;
7981
}
8082
}

src/main/java/gregtech/api/color/containers/BedColorContainer.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gregtech.api.color.containers;
22

3+
import gregtech.api.color.ColorModeSupport;
34
import gregtech.api.color.ColoredBlockContainer;
4-
import gregtech.common.items.behaviors.spray.AbstractSprayBehavior;
55

66
import net.minecraft.block.BlockBed;
77
import net.minecraft.block.BlockHorizontal;
@@ -10,6 +10,7 @@
1010
import net.minecraft.item.EnumDyeColor;
1111
import net.minecraft.tileentity.TileEntity;
1212
import net.minecraft.tileentity.TileEntityBed;
13+
import net.minecraft.util.EnumActionResult;
1314
import net.minecraft.util.EnumFacing;
1415
import net.minecraft.util.ResourceLocation;
1516
import net.minecraft.util.math.BlockPos;
@@ -25,11 +26,13 @@ public BedColorContainer(@NotNull ResourceLocation id) {
2526
}
2627

2728
@Override
28-
public boolean setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
29-
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor) {
29+
public @NotNull EnumActionResult setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
30+
@NotNull EntityPlayer player, @Nullable EnumDyeColor newColor) {
3031
// There are no uncolored beds.
31-
if (newColor == null || getColor(world, pos, facing, player) == newColor) {
32-
return false;
32+
if (newColor == null) {
33+
return EnumActionResult.FAIL;
34+
} else if (colorMatches(world, pos, facing, player, newColor)) {
35+
return EnumActionResult.PASS;
3336
}
3437

3538
IBlockState bedPart1 = world.getBlockState(pos);
@@ -43,18 +46,12 @@ public boolean setColor(@NotNull World world, @NotNull BlockPos pos, @NotNull En
4346
TileEntity bed1TE = world.getTileEntity(pos);
4447
TileEntity bed2TE = world.getTileEntity(otherPartPos);
4548
if (!(bed1TE instanceof TileEntityBed bed1 && bed2TE instanceof TileEntityBed bed2)) {
46-
return false;
49+
return EnumActionResult.FAIL;
4750
}
4851

4952
bed1.setColor(newColor);
5053
bed2.setColor(newColor);
51-
return true;
52-
}
53-
54-
@Override
55-
public boolean removeColor(@NotNull World world, @NotNull BlockPos pos, @NotNull EnumFacing facing,
56-
@NotNull EntityPlayer player) {
57-
return false;
54+
return EnumActionResult.SUCCESS;
5855
}
5956

6057
@Override
@@ -70,7 +67,7 @@ public boolean isBlockValid(@NotNull World world, @NotNull BlockPos pos, @NotNul
7067
}
7168

7269
@Override
73-
public boolean supportsMode(AbstractSprayBehavior.@NotNull ColorMode colorMode) {
74-
return colorMode == AbstractSprayBehavior.ColorMode.DYE_ONLY;
70+
public @NotNull ColorModeSupport getSupportedColorMode() {
71+
return ColorModeSupport.DYE_ONLY;
7572
}
7673
}

0 commit comments

Comments
 (0)