forked from NegaNote/GregTech-Modern-Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteSprayCanBehaviour.java
More file actions
395 lines (355 loc) · 16.9 KB
/
InfiniteSprayCanBehaviour.java
File metadata and controls
395 lines (355 loc) · 16.9 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package net.neganote.gtutilities.common.item;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.blockentity.IPaintable;
import com.gregtechceu.gtceu.api.item.component.IAddInformation;
import com.gregtechceu.gtceu.api.item.component.IInteractionItem;
import com.gregtechceu.gtceu.api.pipenet.IPipeNode;
import com.gregtechceu.gtceu.common.data.GTSoundEntries;
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.gregtechceu.gtceu.data.recipe.CustomTags;
import com.gregtechceu.gtceu.utils.BreadthFirstBlockSearch;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.StainedGlassBlock;
import net.minecraft.world.level.block.StainedGlassPaneBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.common.Tags;
import net.minecraftforge.common.util.TriPredicate;
import appeng.api.implementations.blockentities.IColorableBlockEntity;
import appeng.api.util.AEColor;
import com.google.common.collect.ImmutableMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class InfiniteSprayCanBehaviour implements IInteractionItem, IAddInformation {
private static final ImmutableMap<DyeColor, Block> GLASS_MAP;
private static final ImmutableMap<DyeColor, Block> GLASS_PANE_MAP;
private static final ImmutableMap<DyeColor, Block> TERRACOTTA_MAP;
private static final ImmutableMap<DyeColor, Block> WOOL_MAP;
private static final ImmutableMap<DyeColor, Block> CARPET_MAP;
private static final ImmutableMap<DyeColor, Block> CONCRETE_MAP;
private static final ImmutableMap<DyeColor, Block> CONCRETE_POWDER_MAP;
private static final ImmutableMap<DyeColor, Block> SHULKER_BOX_MAP;
private static Block getBlock(DyeColor color, String postfix) {
ResourceLocation id = new ResourceLocation("minecraft", color.getSerializedName() + "_" + postfix);
return BuiltInRegistries.BLOCK.get(id);
}
static {
ImmutableMap.Builder<DyeColor, Block> glassBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> glassPaneBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> terracottaBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> woolBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> carpetBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> concreteBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> concretePowderBuilder = ImmutableMap.builder();
ImmutableMap.Builder<DyeColor, Block> shulkerBoxBuilder = ImmutableMap.builder();
for (DyeColor color : DyeColor.values()) {
glassBuilder.put(color, getBlock(color, "stained_glass"));
glassPaneBuilder.put(color, getBlock(color, "stained_glass_pane"));
terracottaBuilder.put(color, getBlock(color, "terracotta"));
woolBuilder.put(color, getBlock(color, "wool"));
carpetBuilder.put(color, getBlock(color, "carpet"));
concreteBuilder.put(color, getBlock(color, "concrete"));
concretePowderBuilder.put(color, getBlock(color, "concrete_powder"));
shulkerBoxBuilder.put(color, getBlock(color, "shulker_box"));
}
GLASS_MAP = glassBuilder.build();
GLASS_PANE_MAP = glassPaneBuilder.build();
TERRACOTTA_MAP = terracottaBuilder.build();
WOOL_MAP = woolBuilder.build();
CARPET_MAP = carpetBuilder.build();
CONCRETE_MAP = concreteBuilder.build();
CONCRETE_POWDER_MAP = concretePowderBuilder.build();
SHULKER_BOX_MAP = shulkerBoxBuilder.build();
}
private static final TriPredicate<IPaintable, IPaintable, Direction> paintablePredicate = (parent, child, dir) -> {
if (parent == null) return true;
if (!parent.getClass().equals(child.getClass())) {
return false;
}
return parent.getPaintingColor() == child.getPaintingColor();
};
@SuppressWarnings("rawtypes")
private static final TriPredicate<IPipeNode, IPipeNode, Direction> gtPipePredicate = (parent, child, direction) -> {
if (parent == null) return true;
if (!paintablePredicate.test(parent, child, direction)) {
return false;
}
return parent.isConnected(direction) && child.isConnected(direction.getOpposite());
};
@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
Player player = context.getPlayer();
Level level = context.getLevel();
if (player == null) return InteractionResult.PASS;
DyeColor selectedColor = getColor(stack);
int maxBlocksToRecolor = player.isShiftKeyDown() ? ConfigHolder.INSTANCE.tools.sprayCanChainLength : 1;
var pos = context.getClickedPos();
var first = level.getBlockEntity(pos);
if (first == null || !handleSpecialBlockEntities(first, selectedColor, maxBlocksToRecolor, context)) {
handleBlocks(pos, selectedColor, maxBlocksToRecolor, context);
}
GTSoundEntries.SPRAY_CAN_TOOL.play(level, null, player.position(), 1.0f, 1.0f);
return InteractionResult.sidedSuccess(level.isClientSide);
}
@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
DyeColor currentColor = getColor(stack);
if (currentColor != null) {
tooltip.add(Component.translatable("behaviour.paintspray.infinite.tooltip.current_color",
Component.translatable("color.minecraft." + currentColor.getSerializedName())));
} else {
tooltip.add(Component.translatable("behaviour.paintspray.infinite.tooltip.solvent"));
}
tooltip.add(Component.translatable("behaviour.paintspray.infinite.tooltip.info"));
tooltip.add(Component.translatable("behaviour.paintspray.infinite.tooltip.info_1"));
tooltip.add(Component.translatable("behaviour.paintspray.infinite.tooltip.info_2"));
}
public static void setColor(ItemStack stack, @Nullable DyeColor color) {
if (color == null) {
stack.getOrCreateTag().putInt("color", -1);
} else {
stack.getOrCreateTag().putInt("color", color.ordinal());
}
}
@Nullable
public static DyeColor getColor(ItemStack stack) {
CompoundTag tag = stack.getTag();
if (tag == null || !tag.contains("color") || tag.getInt("color") == -1) {
return null;
}
int ordinal = tag.getInt("color");
DyeColor[] colors = DyeColor.values();
if (ordinal >= 0 && ordinal < colors.length) {
return colors[ordinal];
}
return null;
}
private void handleBlocks(BlockPos start, DyeColor color, int limit, UseOnContext context) {
final var level = context.getLevel();
var collected = BreadthFirstBlockSearch
.conditionalBlockPosSearch(start,
(parent, child) -> parent == null ||
level.getBlockState(child).is(level.getBlockState(parent).getBlock()),
limit, limit * 6);
for (var pos : collected) {
tryPaintBlock(level, pos, color);
}
}
private boolean handleSpecialBlockEntities(BlockEntity first, DyeColor color, int limit, UseOnContext context) {
var player = context.getPlayer();
if (player == null) return false;
if (GTCEu.Mods.isAE2Loaded() && first instanceof IColorableBlockEntity) {
var collected = BreadthFirstBlockSearch.conditionalSearch(
IColorableBlockEntity.class,
(IColorableBlockEntity) first,
first.getLevel(),
be -> ((BlockEntity) be).getBlockPos(),
(parent, child, dir) -> {
if (parent == null) return true;
return parent.getColor() == child.getColor();
},
limit,
limit * 6);
AEColor ae2Color = color == null ?
AEColor.TRANSPARENT :
AEColor.values()[color.ordinal()];
for (IColorableBlockEntity colorable : collected) {
if (colorable.getColor() != ae2Color) {
colorable.recolourBlock(null, ae2Color, player);
}
}
return true;
}
else if (first instanceof IPipeNode pipe) {
var collected = BreadthFirstBlockSearch.conditionalSearch(IPipeNode.class, pipe,
first.getLevel(), IPipeNode::getPipePos,
gtPipePredicate, limit, limit * 6);
paintPaintables(collected, color);
return true;
} else if (first instanceof IPaintable paintable) {
var collected = BreadthFirstBlockSearch.conditionalSearch(IPaintable.class, paintable,
first.getLevel(), p -> ((BlockEntity) p).getBlockPos(),
paintablePredicate, limit, limit * 6);
paintPaintables(collected, color);
return true;
}
else if (first instanceof ShulkerBoxBlockEntity shulkerBox) {
var tag = shulkerBox.saveWithoutMetadata();
var level = first.getLevel();
var pos = first.getBlockPos();
recolorBlockNoState(SHULKER_BOX_MAP, color, level, pos, Blocks.SHULKER_BOX);
assert level != null;
if (level.getBlockEntity(pos) instanceof ShulkerBoxBlockEntity newShulker) {
newShulker.load(tag);
}
return true;
}
return false;
}
private <T extends IPaintable> void paintPaintables(Set<T> paintables, DyeColor color) {
for (var c : paintables) {
paintPaintable(c, color);
}
}
private void tryPaintBlock(Level level, BlockPos pos, DyeColor color) {
var blockState = level.getBlockState(pos);
var block = blockState.getBlock();
if (color == null) {
tryStripBlockColor(level, pos, block);
return;
}
if (!recolorBlockState(level, pos, color)) {
tryPaintSpecialBlock(level, pos, block, color);
}
}
private void tryPaintSpecialBlock(Level world, BlockPos pos, Block block, DyeColor color) {
if (block.defaultBlockState().is(Tags.Blocks.GLASS)) {
if (recolorBlockNoState(GLASS_MAP, color, world, pos, Blocks.GLASS)) {
return;
}
}
if (block.defaultBlockState().is(Tags.Blocks.GLASS_PANES)) {
if (recolorBlockNoState(GLASS_PANE_MAP, color, world, pos, Blocks.GLASS_PANE)) {
return;
}
}
if (block.defaultBlockState().is(BlockTags.TERRACOTTA)) {
if (recolorBlockNoState(TERRACOTTA_MAP, color, world, pos, Blocks.TERRACOTTA)) {
return;
}
}
if (block.defaultBlockState().is(BlockTags.WOOL)) {
if (recolorBlockNoState(WOOL_MAP, color, world, pos, null)) {
return;
}
}
if (block.defaultBlockState().is(BlockTags.WOOL_CARPETS)) {
if (recolorBlockNoState(CARPET_MAP, color, world, pos, null)) {
return;
}
}
if (block.defaultBlockState().is(CustomTags.CONCRETE_BLOCK)) {
if (recolorBlockNoState(CONCRETE_MAP, color, world, pos, null)) {
return;
}
}
if (block.defaultBlockState().is(CustomTags.CONCRETE_POWDER_BLOCK)) {
recolorBlockNoState(CONCRETE_POWDER_MAP, color, world, pos, null);
}
}
private static void paintPaintable(IPaintable paintable, DyeColor color) {
if (color == null) {
if (!paintable.isPainted()) {
return;
}
paintable.setPaintingColor(IPaintable.UNPAINTED_COLOR);
} else if (paintable.getPaintingColor() != color.getMapColor().col) {
paintable.setPaintingColor(color.getMapColor().col);
}
}
private static boolean recolorBlockNoState(Map<DyeColor, Block> map, @Nullable DyeColor color,
Level level, BlockPos pos, Block defaultBlock) {
Block newBlock = map.getOrDefault(color, defaultBlock);
if (newBlock == Blocks.AIR) newBlock = defaultBlock;
BlockState old = level.getBlockState(pos);
if (newBlock != null && newBlock != old.getBlock()) {
BlockState state = newBlock.defaultBlockState();
for (Property property : old.getProperties()) {
if (!state.hasProperty(property)) continue;
state.setValue(property, old.getValue(property));
}
level.setBlockAndUpdate(pos, state);
return true;
}
return false;
}
private static void tryStripBlockColor(Level world, BlockPos pos, Block block) {
if (block instanceof StainedGlassBlock) {
world.setBlockAndUpdate(pos, Blocks.GLASS.defaultBlockState());
return;
}
if (block instanceof StainedGlassPaneBlock) {
world.setBlockAndUpdate(pos, Blocks.GLASS_PANE.defaultBlockState());
return;
}
if (block.defaultBlockState().is(BlockTags.TERRACOTTA) && block != Blocks.TERRACOTTA) {
world.setBlockAndUpdate(pos, Blocks.TERRACOTTA.defaultBlockState());
return;
}
if (block.defaultBlockState().is(BlockTags.WOOL) && block != Blocks.WHITE_WOOL) {
world.setBlockAndUpdate(pos, Blocks.WHITE_WOOL.defaultBlockState());
return;
}
if (block.defaultBlockState().is(BlockTags.WOOL_CARPETS) && block != Blocks.WHITE_CARPET) {
world.setBlockAndUpdate(pos, Blocks.WHITE_CARPET.defaultBlockState());
return;
}
if (block.defaultBlockState().is(CustomTags.CONCRETE_BLOCK) && block != Blocks.WHITE_CONCRETE) {
world.setBlockAndUpdate(pos, Blocks.WHITE_CONCRETE.defaultBlockState());
return;
}
if (block.defaultBlockState().is(CustomTags.CONCRETE_POWDER_BLOCK) && block != Blocks.WHITE_CONCRETE_POWDER) {
world.setBlockAndUpdate(pos, Blocks.WHITE_CONCRETE_POWDER.defaultBlockState());
return;
}
BlockState state = world.getBlockState(pos);
for (Property prop : state.getProperties()) {
if (prop.getValueClass() == DyeColor.class) {
BlockState defaultState = block.defaultBlockState();
DyeColor defaultColor = DyeColor.WHITE;
try {
defaultColor = (DyeColor) defaultState.getValue(prop);
} catch (IllegalArgumentException ignored) {}
recolorBlockState(world, pos, defaultColor);
return;
}
}
}
private static boolean recolorBlockState(Level level, BlockPos pos, DyeColor color) {
BlockState state = level.getBlockState(pos);
for (Property property : state.getProperties()) {
if (property.getValueClass() == DyeColor.class) {
level.setBlockAndUpdate(pos, state.setValue(property, color));
return true;
}
}
return false;
}
@Override
public boolean onEntitySwing(@NotNull ItemStack stack, @NotNull LivingEntity entity) {
return false;
}
@Override
public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target, @NotNull LivingEntity attacker) {
return false;
}
@Override
public @NotNull InteractionResult interactLivingEntity(@NotNull ItemStack stack,
@NotNull Player player,
@NotNull LivingEntity interactionTarget,
@NotNull InteractionHand hand) {
return InteractionResult.PASS;
}
}