Skip to content

Commit

Permalink
spotless and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
vrejhead committed Dec 26, 2024
1 parent 40c8276 commit 99c6dcd
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,58 +234,69 @@ public static TraceabilityPredicate metaTileEntities(MetaTileEntity... metaTileE
ResourceLocation[] ids = Arrays.stream(metaTileEntities).filter(Objects::nonNull)
.map(tile -> tile.metaTileEntityId).toArray(ResourceLocation[]::new);
return tilePredicate((state, tile) -> ArrayUtils.contains(ids, tile.metaTileEntityId),
getCandidates(metaTileEntities));
getCandidates(() -> EnumFacing.NORTH, metaTileEntities));
}

@SafeVarargs
public static <T extends MetaTileEntity & ITieredMetaTileEntity> TraceabilityPredicate tieredMTEs(BiPredicate<Map<String, String>, T> pred, T... metaTileEntities) {
public static <
T extends MetaTileEntity & ITieredMetaTileEntity> TraceabilityPredicate tieredMTEs(BiPredicate<Map<String, String>, T> pred,
T... metaTileEntities) {
ResourceLocation[] ids = Arrays.stream(metaTileEntities).filter(Objects::nonNull)
.map(tile -> tile.metaTileEntityId).toArray(ResourceLocation[]::new);
return tilePredicate((state, tile) -> ArrayUtils.contains(ids, tile.metaTileEntityId),
getCandidates(pred, metaTileEntities));
}

private static Function<Map<String, String>, BlockInfo[]> getCandidates(MetaTileEntity... metaTileEntities) {
public static Function<Map<String, String>, BlockInfo[]> getCandidates(Supplier<EnumFacing> facing,
MetaTileEntity... metaTileEntities) {
return map -> Arrays.stream(metaTileEntities).filter(Objects::nonNull).map(tile -> {
MetaTileEntityHolder holder = new MetaTileEntityHolder();
holder.setMetaTileEntity(tile);
holder.getMetaTileEntity().onPlacement();
holder.getMetaTileEntity().setFrontFacing(EnumFacing.SOUTH);
holder.getMetaTileEntity().setFrontFacing(facing.get());
return new BlockInfo(tile.getBlock().getDefaultState(), holder);
}).toArray(BlockInfo[]::new);
}

// generic hell
@SafeVarargs
public static <T extends MetaTileEntity & ITieredMetaTileEntity> Function<Map<String, String>, BlockInfo[]> getCandidates(BiPredicate<Map<String, String>, T> pred, T... metaTileEntities) {
public static <
T extends MetaTileEntity & ITieredMetaTileEntity> Function<Map<String, String>, BlockInfo[]> getCandidates(BiPredicate<Map<String, String>, T> pred,
T... metaTileEntities) {
return map -> Arrays.stream(metaTileEntities).filter(Objects::nonNull)
.filter(i -> pred.test(map, i))
.map(tile -> {
MetaTileEntityHolder holder = new MetaTileEntityHolder();
holder.setMetaTileEntity(tile);
holder.getMetaTileEntity().onPlacement();
holder.getMetaTileEntity().setFrontFacing(EnumFacing.SOUTH);
return new BlockInfo(tile.getBlock().getDefaultState(), holder);
}).toArray(BlockInfo[]::new);
.filter(i -> pred.test(map, i))
.map(tile -> {
MetaTileEntityHolder holder = new MetaTileEntityHolder();
holder.setMetaTileEntity(tile);
holder.getMetaTileEntity().onPlacement();
holder.getMetaTileEntity().setFrontFacing(EnumFacing.SOUTH);
return new BlockInfo(tile.getBlock().getDefaultState(), holder);
}).toArray(BlockInfo[]::new);
}

private static Function<Map<String, String>, BlockInfo[]> getCandidates(String key, IBlockState... allowedStates) {
public static Function<Map<String, String>, BlockInfo[]> getCandidates(String key, IBlockState... allowedStates) {
return map -> {
if (map.containsKey(key)) {
return new BlockInfo[] { new BlockInfo(allowedStates[MathHelper.clamp(GTUtility.parseInt(map.get(key)), 0, allowedStates.length - 1)]) };
return new BlockInfo[] { new BlockInfo(allowedStates[MathHelper.clamp(GTUtility.parseInt(map.get(key)),
0, allowedStates.length - 1)]) };
}
return Arrays.stream(allowedStates).map(BlockInfo::new).toArray(BlockInfo[]::new);
};
}

public static TraceabilityPredicate abilities(MultiblockAbility<?>... allowedAbilities) {
public static TraceabilityPredicate abilities(Supplier<EnumFacing> facing,
MultiblockAbility<?>... allowedAbilities) {
return tilePredicate((state, tile) -> tile instanceof IMultiblockAbilityPart<?> &&
ArrayUtils.contains(allowedAbilities, ((IMultiblockAbilityPart<?>) tile).getAbility()),
getCandidates(Arrays.stream(allowedAbilities)
getCandidates(facing, Arrays.stream(allowedAbilities)
.flatMap(ability -> MultiblockAbility.REGISTRY.get(ability).stream())
.toArray(MetaTileEntity[]::new)));
}

public static TraceabilityPredicate abilities(MultiblockAbility<?>... allowedAbilities) {
return abilities(() -> EnumFacing.NORTH, allowedAbilities);
}

public static TraceabilityPredicate states(IBlockState... allowedStates) {
return states(null, allowedStates);
}
Expand Down Expand Up @@ -596,6 +607,7 @@ public void invalidateStructure(String name) {
// i am sorry
Object[] added = { null };
List<Object> dummyList = new ArrayList<>() {

@Override
public boolean add(Object e) {
added[0] = e;
Expand All @@ -606,7 +618,7 @@ public boolean add(Object e) {
multiblockParts.removeIf(part -> {
if (name.equals(part.getSubstructureName())) {
if (part instanceof IMultiblockAbilityPart<?>) {
//noinspection unchecked
// noinspection unchecked
IMultiblockAbilityPart<Object> ability = (IMultiblockAbilityPart<Object>) part;
added[0] = null;
ability.registerAbilities(dummyList);
Expand Down Expand Up @@ -871,7 +883,8 @@ public void autoBuild(EntityPlayer player, Map<String, String> map, String subst
}

/**
* Autobuild the multiblock, this is like {@link MultiblockControllerBase#autoBuild(EntityPlayer, Map, String)} but if
* Autobuild the multiblock, this is like {@link MultiblockControllerBase#autoBuild(EntityPlayer, Map, String)} but
* if
* you have the predicate map for other uses. This does mutate the map passed in.
*/
public void autoBuild(EntityPlayer player, Map<String, String> map,
Expand All @@ -898,6 +911,7 @@ public void autoBuild(EntityPlayer player, Map<String, String> map,
MetaTileEntityHolder newHolder = new MetaTileEntityHolder();
newHolder.setMetaTileEntity(holder.getMetaTileEntity());
newHolder.getMetaTileEntity().onPlacement();
newHolder.getMetaTileEntity().setFrontFacing(holder.getMetaTileEntity().getFrontFacing());
if (removed.hasTagCompound())
newHolder.getMetaTileEntity().initFromItemStackData(removed.getTagCompound());

Expand Down Expand Up @@ -931,7 +945,6 @@ public void autoBuild(EntityPlayer player, Map<String, String> map,
};

for (Long2ObjectMap.Entry<TraceabilityPredicate> entry : predicates.long2ObjectEntrySet()) {
// todo add autobuild key params here, also remove layer stuff from rest of the code elsewhere
TraceabilityPredicate pred = entry.getValue();
if (simpleIndex.getInt(pred) >= pred.simple.size()) continue;

Expand Down
46 changes: 16 additions & 30 deletions src/main/java/gregtech/api/pattern/TraceabilityPredicate.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package gregtech.api.pattern;

import com.google.common.collect.Iterators;

import gregtech.api.GregTechAPI;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.multiblock.MultiblockControllerBase;
import gregtech.api.util.BlockInfo;

import gregtech.api.util.GTUtility;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Function;
Expand All @@ -41,12 +33,13 @@ public class TraceabilityPredicate {
worldState -> GregTechAPI.HEATING_COILS.containsKey(worldState.getBlockState()) ? null :
PatternError.PLACEHOLDER,
map -> GregTechAPI.HEATING_COILS.entrySet().stream()
.filter(e -> e.getValue().getTier() == GTUtility.parseInt(map.get("coilTier"), 1))
.filter(e -> !map.containsKey("coilTier") ||
e.getValue().getTier() == GTUtility.parseInt(map.get("coilTier"), 1))
// sort to make autogenerated jei previews not pick random coils each game load
.sorted(Comparator.comparingInt(entry -> entry.getValue().getTier()))
.map(entry -> new BlockInfo(entry.getKey(), null))
.toArray(BlockInfo[]::new))
.addTooltips("gregtech.multiblock.pattern.error.coils");
.addTooltips("gregtech.multiblock.pattern.error.coils");

public final List<SimplePredicate> simple = new ArrayList<>();
protected boolean isCenter;
Expand Down Expand Up @@ -283,18 +276,19 @@ public PatternError testRaw(BlockWorldState worldState) {
public PatternError testLimited(BlockWorldState worldState,
Object2IntMap<SimplePredicate> globalCache,
Object2IntMap<SimplePredicate> layerCache) {
PatternError error = testGlobal(worldState, globalCache);
PatternError error = testGlobal(worldState, globalCache, layerCache);
if (error != null) return error;
return testLayer(worldState, layerCache);
}

public PatternError testGlobal(BlockWorldState worldState, Object2IntMap<SimplePredicate> cache) {
public PatternError testGlobal(BlockWorldState worldState, Object2IntMap<SimplePredicate> global,
Object2IntMap<SimplePredicate> layer) {
PatternError result = predicate.apply(worldState);

if (cache != null && !cache.containsKey(this)) cache.put(this, 0);
if ((minGlobalCount == -1 && maxGlobalCount == -1) || cache == null || result != null) return result;
if (!global.containsKey(this)) global.put(this, 0);
if ((minGlobalCount == -1 && maxGlobalCount == -1) || result != null || layer == null) return result;

int count = cache.put(this, cache.getInt(this) + 1) + 1;
int count = layer.put(this, layer.getInt(this) + 1) + 1 + global.getInt(this);
if (maxGlobalCount == -1 || count <= maxGlobalCount) return null;

return new SinglePredicateError(this, 0);
Expand All @@ -303,27 +297,19 @@ public PatternError testGlobal(BlockWorldState worldState, Object2IntMap<SimpleP
public PatternError testLayer(BlockWorldState worldState, Object2IntMap<SimplePredicate> cache) {
PatternError result = predicate.apply(worldState);

if ((minLayerCount == -1 && maxLayerCount == -1) || cache == null || result != null) return result;
if ((minLayerCount == -1 && maxLayerCount == -1) || result != null) return result;

int count = cache.put(this, cache.getInt(this) + 1) + 1;
if (maxLayerCount == -1 || count <= maxLayerCount) return null;
if (maxLayerCount == -1 || cache.getInt(this) <= maxLayerCount) return null;

return new SinglePredicateError(this, 2);
}

public List<ItemStack> getCandidates() {
return candidates == null ? Collections.emptyList() : Arrays.stream(this.candidates.apply(Collections.emptyMap()))
.filter(info -> info.getBlockState().getBlock() != Blocks.AIR).map(info -> {
IBlockState blockState = info.getBlockState();
MetaTileEntity metaTileEntity = info.getTileEntity() instanceof IGregTechTileEntity ?
((IGregTechTileEntity) info.getTileEntity()).getMetaTileEntity() : null;
if (metaTileEntity != null) {
return metaTileEntity.getStackForm();
} else {
return new ItemStack(Item.getItemFromBlock(blockState.getBlock()), 1,
blockState.getBlock().damageDropped(blockState));
}
}).collect(Collectors.toList());
return candidates == null ? Collections.emptyList() :
Arrays.stream(this.candidates.apply(Collections.emptyMap()))
.filter(info -> info.getBlockState().getBlock() != Blocks.AIR)
.map(BlockInfo::toItem)
.collect(Collectors.toList());
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/gregtech/api/pattern/pattern/BlockPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gregtech.api.pattern.PatternError;
import gregtech.api.pattern.TraceabilityPredicate;
import gregtech.api.util.BlockInfo;
import gregtech.api.util.GTLog;
import gregtech.api.util.RelativeDirection;

import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -254,15 +253,20 @@ public boolean checkAisle(GreggyBlockPos controllerPos, EnumFacing frontFacing,
// offset the string start once after every string
stringStart.offset(absoluteString);
charPos.from(stringStart);
}

// layer minimum checks
for (Object2IntMap.Entry<TraceabilityPredicate.SimplePredicate> entry : layerCount.object2IntEntrySet()) {
if (entry.getIntValue() < entry.getKey().minLayerCount) {
state.setError(new TraceabilityPredicate.SinglePredicateError(entry.getKey(), 3));
return false;
}
// layer minimum checks
for (Object2IntMap.Entry<TraceabilityPredicate.SimplePredicate> entry : layerCount.object2IntEntrySet()) {
if (entry.getIntValue() < entry.getKey().minLayerCount) {
state.setError(new TraceabilityPredicate.SinglePredicateError(entry.getKey(), 3));
return false;
}
}

for (Object2IntMap.Entry<TraceabilityPredicate.SimplePredicate> entry : layerCount.object2IntEntrySet()) {
globalCount.put(entry.getKey(), globalCount.getInt(entry.getKey()) + entry.getIntValue());
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import gregtech.api.util.RelativeDirection;
import gregtech.api.util.function.QuadFunction;

import it.unimi.dsi.fastutil.longs.Long2ObjectRBTreeMap;

import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
Expand All @@ -21,6 +19,7 @@

import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectRBTreeMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMaps;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
Expand Down Expand Up @@ -156,7 +155,6 @@ public boolean checkPatternAt(World world, BlockPos centerPos, EnumFacing frontF
// this basically reshuffles the coordinates into absolute form from relative form
pos.zero().offset(absolutes[0], arr[0]).offset(absolutes[1], arr[1]).offset(absolutes[2], arr[2]);
// translate from the origin to the center
// set the pos with world coordinates
worldState.setPos(pos.add(translation));

if (predicate != TraceabilityPredicate.ANY) {
Expand Down Expand Up @@ -213,7 +211,8 @@ public Long2ObjectSortedMap<TraceabilityPredicate> getDefaultShape(MultiblockCon
TraceabilityPredicate predicate = predicateFunction.apply(pos, bounds);

int[] arr = pos.getAll();
pos.zero().offset(absolutes[0], arr[0]).offset(absolutes[1], arr[1]).offset(absolutes[2], arr[2]).add(translation);
pos.zero().offset(absolutes[0], arr[0]).offset(absolutes[1], arr[1]).offset(absolutes[2], arr[2])
.add(translation);

if (predicate != TraceabilityPredicate.ANY && predicate != TraceabilityPredicate.AIR) {
predicates.put(pos.toLong(), predicate);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gregtech/api/util/BlockInfo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package gregtech.api.util;

import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -36,6 +40,16 @@ public BlockInfo(IBlockState blockState, TileEntity tileEntity) {
"Cannot create block info with tile entity for block not having it");
}

public ItemStack toItem() {
MetaTileEntity metaTileEntity = tileEntity instanceof IGregTechTileEntity igtte ? igtte.getMetaTileEntity() :
null;
if (metaTileEntity != null) {
return metaTileEntity.getStackForm();
} else {
return GTUtility.toItem(blockState);
}
}

public IBlockState getBlockState() {
return blockState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import gregtech.api.mui.GTGuis;
import gregtech.api.mui.factory.MetaItemGuiFactory;
import gregtech.api.pattern.PatternError;
import gregtech.api.pattern.pattern.IBlockPattern;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.handler.BlockPosHighlightRenderer;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -45,7 +44,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -195,6 +193,7 @@ public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPo
player.sendMessage(
new TextComponentTranslation("gregtech.multiblock.pattern.error_message_header"));
player.sendMessage(new TextComponentString(error.getErrorInfo()));
if (error.getPos() != null) BlockPosHighlightRenderer.renderBlockBoxHighLight(error.getPos(), 5000);
return EnumActionResult.SUCCESS;
}
}
Expand Down
Loading

0 comments on commit 99c6dcd

Please sign in to comment.