Skip to content

Commit 95ba350

Browse files
committed
refactor: extract enabledMaterials to BlockMaterialPipe class
1 parent 067d32f commit 95ba350

File tree

5 files changed

+107
-151
lines changed

5 files changed

+107
-151
lines changed

src/main/java/gregtech/api/pipenet/block/material/BlockMaterialPipe.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,52 @@
1515

1616
import net.minecraft.block.state.IBlockState;
1717
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
18+
import net.minecraft.creativetab.CreativeTabs;
1819
import net.minecraft.item.Item;
1920
import net.minecraft.item.ItemStack;
21+
import net.minecraft.util.NonNullList;
2022
import net.minecraft.util.ResourceLocation;
2123
import net.minecraftforge.client.model.ModelLoader;
2224
import net.minecraftforge.fml.relauncher.Side;
2325
import net.minecraftforge.fml.relauncher.SideOnly;
2426

27+
import com.google.common.base.Preconditions;
2528
import org.jetbrains.annotations.NotNull;
2629

27-
import java.util.Objects;
30+
import java.util.*;
2831

2932
public abstract class BlockMaterialPipe<
3033
PipeType extends Enum<PipeType> & IPipeType<NodeDataType> & IMaterialPipeType<NodeDataType>, NodeDataType,
3134
WorldPipeNetType extends WorldPipeNet<NodeDataType, ? extends PipeNet<NodeDataType>>>
3235
extends BlockPipe<PipeType, NodeDataType, WorldPipeNetType> {
3336

3437
protected final PipeType pipeType;
38+
protected final Map<Material, NodeDataType> enabledMaterials;
3539
private final MaterialRegistry registry;
3640

3741
public BlockMaterialPipe(@NotNull PipeType pipeType, @NotNull MaterialRegistry registry) {
3842
this.pipeType = pipeType;
43+
this.enabledMaterials = new TreeMap<>();
3944
this.registry = registry;
4045
}
4146

47+
public boolean isValidPipeMaterial(Material material) {
48+
return !getItemPipeType(getItem(material)).getOrePrefix().isIgnored(material);
49+
}
50+
51+
public void addPipeMaterial(Material material, NodeDataType pipeProperties) {
52+
Preconditions.checkNotNull(material, "material was null");
53+
Preconditions.checkNotNull(pipeProperties, "the %s of material %s was null", getPipeTypeClass().getSimpleName(),
54+
material);
55+
Preconditions.checkArgument(material.getRegistry().getNameForObject(material) != null,
56+
"material %s is not registered", material);
57+
this.enabledMaterials.put(material, pipeProperties);
58+
}
59+
60+
public Collection<Material> getEnabledMaterials() {
61+
return Collections.unmodifiableSet(enabledMaterials.keySet());
62+
}
63+
4264
@Override
4365
public NodeDataType createProperties(IPipeTile<PipeType, NodeDataType> pipeTile) {
4466
PipeType pipeType = pipeTile.getPipeType();
@@ -80,7 +102,21 @@ public ItemStack getDropItem(IPipeTile<PipeType, NodeDataType> pipeTile) {
80102
return getItem(material);
81103
}
82104

83-
protected abstract NodeDataType createProperties(PipeType pipeType, Material material);
105+
protected NodeDataType createProperties(PipeType pipeType, Material material) {
106+
return pipeType.modifyProperties(enabledMaterials.getOrDefault(material, getFallbackType()));
107+
}
108+
109+
@Override
110+
protected NodeDataType getFallbackType() {
111+
return enabledMaterials.values().iterator().next();
112+
}
113+
114+
@Override
115+
public void getSubBlocks(@NotNull CreativeTabs itemIn, @NotNull NonNullList<ItemStack> items) {
116+
for (Material material : enabledMaterials.keySet()) {
117+
items.add(getItem(material));
118+
}
119+
}
84120

85121
public OrePrefix getPrefix() {
86122
return pipeType.getOrePrefix();

src/main/java/gregtech/common/CommonProxy.java

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import gregtech.api.unification.material.registry.MaterialRegistry;
1919
import gregtech.api.unification.ore.OrePrefix;
2020
import gregtech.api.unification.ore.StoneType;
21-
import gregtech.api.unification.stack.ItemMaterialInfo;
21+
import gregtech.api.unification.stack.RecyclingData;
2222
import gregtech.api.util.AssemblyLineManager;
2323
import gregtech.api.util.GTLog;
2424
import gregtech.common.blocks.BlockCompressed;
@@ -64,6 +64,7 @@
6464
import net.minecraftforge.event.RegistryEvent;
6565
import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
6666
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
67+
import net.minecraftforge.fml.common.FMLCommonHandler;
6768
import net.minecraftforge.fml.common.Loader;
6869
import net.minecraftforge.fml.common.LoaderState;
6970
import net.minecraftforge.fml.common.Mod;
@@ -88,7 +89,9 @@ public static void registerBlocks(RegistryEvent.Register<Block> event) {
8889
IForgeRegistry<Block> registry = event.getRegistry();
8990

9091
for (MTERegistry r : GregTechAPI.mteManager.getRegistries()) {
91-
registry.register(r.getBlock());
92+
if (!registry.getKeys().isEmpty()) {
93+
registry.register(r.getBlock());
94+
}
9295
}
9396

9497
StoneType.init();
@@ -101,30 +104,42 @@ public static void registerBlocks(RegistryEvent.Register<Block> event) {
101104

102105
if (material.hasProperty(PropertyKey.WIRE)) {
103106
for (BlockCable cable : CABLES.get(materialRegistry.getModid())) {
104-
if (!cable.getItemPipeType(null).isCable() ||
105-
!material.getProperty(PropertyKey.WIRE).isSuperconductor())
106-
cable.addCableMaterial(material, material.getProperty(PropertyKey.WIRE));
107+
if (cable.isValidPipeMaterial(material)) {
108+
cable.addPipeMaterial(material, material.getProperty(PropertyKey.WIRE));
109+
}
107110
}
108111
}
109112
if (material.hasProperty(PropertyKey.FLUID_PIPE)) {
110113
for (BlockFluidPipe pipe : FLUID_PIPES.get(materialRegistry.getModid())) {
111-
if (!pipe.getItemPipeType(pipe.getItem(material)).getOrePrefix().isIgnored(material)) {
114+
if (pipe.isValidPipeMaterial(material)) {
112115
pipe.addPipeMaterial(material, material.getProperty(PropertyKey.FLUID_PIPE));
113116
}
114117
}
115118
}
116119
if (material.hasProperty(PropertyKey.ITEM_PIPE)) {
117120
for (BlockItemPipe pipe : ITEM_PIPES.get(materialRegistry.getModid())) {
118-
if (!pipe.getItemPipeType(pipe.getItem(material)).getOrePrefix().isIgnored(material)) {
121+
if (pipe.isValidPipeMaterial(material)) {
119122
pipe.addPipeMaterial(material, material.getProperty(PropertyKey.ITEM_PIPE));
120123
}
121124
}
122125
}
123126
}
124127

125-
for (BlockCable cable : CABLES.get(materialRegistry.getModid())) registry.register(cable);
126-
for (BlockFluidPipe pipe : FLUID_PIPES.get(materialRegistry.getModid())) registry.register(pipe);
127-
for (BlockItemPipe pipe : ITEM_PIPES.get(materialRegistry.getModid())) registry.register(pipe);
128+
for (BlockCable cable : CABLES.get(materialRegistry.getModid())) {
129+
if (!cable.getEnabledMaterials().isEmpty()) {
130+
registry.register(cable);
131+
}
132+
}
133+
for (BlockFluidPipe pipe : FLUID_PIPES.get(materialRegistry.getModid())) {
134+
if (!pipe.getEnabledMaterials().isEmpty()) {
135+
registry.register(pipe);
136+
}
137+
}
138+
for (BlockItemPipe pipe : ITEM_PIPES.get(materialRegistry.getModid())) {
139+
if (!pipe.getEnabledMaterials().isEmpty()) {
140+
registry.register(pipe);
141+
}
142+
}
128143
}
129144
for (BlockOpticalPipe pipe : OPTICAL_PIPES) registry.register(pipe);
130145
for (BlockLaserPipe pipe : LASER_PIPES) registry.register(pipe);
@@ -235,16 +250,27 @@ public static void registerItems(RegistryEvent.Register<Item> event) {
235250
GTRecipeManager.preLoad();
236251

237252
for (MTERegistry r : GregTechAPI.mteManager.getRegistries()) {
238-
registry.register(createItemBlock(r.getBlock(), MachineItemBlock::new));
253+
if (!r.getKeys().isEmpty()) {
254+
registry.register(createItemBlock(r.getBlock(), MachineItemBlock::new));
255+
}
239256
}
240257

241258
for (MaterialRegistry materialRegistry : GregTechAPI.materialManager.getRegistries()) {
242-
for (BlockCable cable : CABLES.get(materialRegistry.getModid()))
243-
registry.register(createItemBlock(cable, ItemBlockCable::new));
244-
for (BlockFluidPipe pipe : FLUID_PIPES.get(materialRegistry.getModid()))
245-
registry.register(createItemBlock(pipe, ItemBlockFluidPipe::new));
246-
for (BlockItemPipe pipe : ITEM_PIPES.get(materialRegistry.getModid()))
247-
registry.register(createItemBlock(pipe, ItemBlockItemPipe::new));
259+
for (BlockCable cable : CABLES.get(materialRegistry.getModid())) {
260+
if (!cable.getEnabledMaterials().isEmpty()) {
261+
registry.register(createItemBlock(cable, ItemBlockCable::new));
262+
}
263+
}
264+
for (BlockFluidPipe pipe : FLUID_PIPES.get(materialRegistry.getModid())) {
265+
if (!pipe.getEnabledMaterials().isEmpty()) {
266+
registry.register(createItemBlock(pipe, ItemBlockFluidPipe::new));
267+
}
268+
}
269+
for (BlockItemPipe pipe : ITEM_PIPES.get(materialRegistry.getModid())) {
270+
if (!pipe.getEnabledMaterials().isEmpty()) {
271+
registry.register(createItemBlock(pipe, ItemBlockItemPipe::new));
272+
}
273+
}
248274
}
249275
for (BlockOpticalPipe pipe : OPTICAL_PIPES) registry.register(createItemBlock(pipe, ItemBlockOpticalPipe::new));
250276
for (BlockLaserPipe pipe : LASER_PIPES) registry.register(createItemBlock(pipe, ItemBlockLaserPipe::new));
@@ -334,7 +360,7 @@ public static void registerRecipes(RegistryEvent.Register<IRecipe> event) {
334360
MaterialInfoLoader.init();
335361

336362
// post an event for addons to modify unification data before base GT registers recycling recipes
337-
MinecraftForge.EVENT_BUS.post(new GregTechAPI.RegisterEvent<>(null, ItemMaterialInfo.class));
363+
MinecraftForge.EVENT_BUS.post(new GregTechAPI.RegisterEvent<>(null, RecyclingData.class));
338364

339365
GTLog.logger.info("Registering recipes...");
340366

@@ -420,9 +446,13 @@ public void onLoadComplete() {
420446
// If JEI and GS is not loaded, refresh ore dict ingredients
421447
// Not needed if JEI is loaded, as done in the JEI plugin (and this runs after that)
422448
// Not needed if GS is loaded, as done after script loads (and this runs after that)
423-
if (!GregTechAPI.moduleManager.isModuleEnabled(GregTechModules.MODULE_JEI) &&
424-
!GroovyScriptModule.isCurrentlyRunning())
425-
GTRecipeOreInput.refreshStackCache();
449+
if (!GroovyScriptModule.isCurrentlyRunning()) {
450+
// EXCEPTION: IF GrS is not loaded, and JEI is loaded, and we are in a dedicated server env, refresh
451+
// This is due to JEI Plugin Register not taking place on server, and GrS not acting as the backup.
452+
if (!GregTechAPI.moduleManager.isModuleEnabled(GregTechModules.MODULE_JEI) ||
453+
FMLCommonHandler.instance().getSide().isServer())
454+
GTRecipeOreInput.refreshStackCache();
455+
}
426456
}
427457

428458
public boolean isFancyGraphics() {

src/main/java/gregtech/common/pipelike/cable/BlockCable.java

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gregtech.api.pipenet.tile.IPipeTile;
99
import gregtech.api.pipenet.tile.TileEntityPipeBase;
1010
import gregtech.api.unification.material.Material;
11+
import gregtech.api.unification.material.properties.PropertyKey;
1112
import gregtech.api.unification.material.properties.WireProperties;
1213
import gregtech.api.unification.material.registry.MaterialRegistry;
1314
import gregtech.api.util.GTUtility;
@@ -22,7 +23,6 @@
2223
import net.minecraft.block.ITileEntityProvider;
2324
import net.minecraft.block.state.IBlockState;
2425
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
25-
import net.minecraft.creativetab.CreativeTabs;
2626
import net.minecraft.entity.Entity;
2727
import net.minecraft.entity.EntityLivingBase;
2828
import net.minecraft.entity.player.EntityPlayer;
@@ -31,81 +31,47 @@
3131
import net.minecraft.tileentity.TileEntity;
3232
import net.minecraft.util.EnumBlockRenderType;
3333
import net.minecraft.util.EnumFacing;
34-
import net.minecraft.util.NonNullList;
3534
import net.minecraft.util.math.BlockPos;
3635
import net.minecraft.world.IBlockAccess;
3736
import net.minecraft.world.World;
3837
import net.minecraftforge.fml.relauncher.Side;
3938
import net.minecraftforge.fml.relauncher.SideOnly;
4039

41-
import com.google.common.base.Preconditions;
4240
import org.apache.commons.lang3.tuple.Pair;
4341
import org.jetbrains.annotations.NotNull;
4442

45-
import java.util.Collection;
46-
import java.util.Collections;
47-
import java.util.Map;
48-
import java.util.TreeMap;
49-
5043
public class BlockCable extends BlockMaterialPipe<Insulation, WireProperties, WorldENet>
5144
implements ITileEntityProvider {
5245

53-
private final Map<Material, WireProperties> enabledMaterials = new TreeMap<>();
54-
5546
public BlockCable(Insulation cableType, MaterialRegistry registry) {
5647
super(cableType, registry);
5748
setCreativeTab(GTCreativeTabs.TAB_GREGTECH_CABLES);
5849
setHarvestLevel(ToolClasses.WIRE_CUTTER, 1);
5950
}
6051

61-
public void addCableMaterial(Material material, WireProperties wireProperties) {
62-
Preconditions.checkNotNull(material, "material was null");
63-
Preconditions.checkNotNull(wireProperties, "material %s wireProperties was null", material);
64-
Preconditions.checkArgument(material.getRegistry().getNameForObject(material) != null,
65-
"material %s is not registered", material);
66-
if (!pipeType.orePrefix.isIgnored(material)) {
67-
this.enabledMaterials.put(material, wireProperties);
68-
}
69-
}
70-
71-
public Collection<Material> getEnabledMaterials() {
72-
return Collections.unmodifiableSet(enabledMaterials.keySet());
52+
@Override
53+
public boolean isValidPipeMaterial(Material material) {
54+
return super.isValidPipeMaterial(material) && !(getItemPipeType(null).isCable() &&
55+
material.getProperty(PropertyKey.WIRE).isSuperconductor());
7356
}
7457

7558
@Override
7659
public Class<Insulation> getPipeTypeClass() {
7760
return Insulation.class;
7861
}
7962

80-
@Override
81-
protected WireProperties createProperties(Insulation insulation, Material material) {
82-
return insulation.modifyProperties(enabledMaterials.getOrDefault(material, getFallbackType()));
83-
}
84-
8563
@SideOnly(Side.CLIENT)
8664
@NotNull
8765
@Override
8866
public PipeRenderer getPipeRenderer() {
8967
return CableRenderer.INSTANCE;
9068
}
9169

92-
@Override
93-
protected WireProperties getFallbackType() {
94-
return enabledMaterials.values().iterator().next();
95-
}
96-
9770
@Override
9871
public WorldENet getWorldPipeNet(World world) {
9972
return WorldENet.getWorldENet(world);
10073
}
10174

102-
@Override
103-
public void getSubBlocks(@NotNull CreativeTabs itemIn, @NotNull NonNullList<ItemStack> items) {
104-
for (Material material : enabledMaterials.keySet()) {
105-
items.add(getItem(material));
106-
}
107-
}
108-
10975
@Override
11076
protected boolean isPipeTool(@NotNull ItemStack stack) {
11177
return ToolHelper.isTool(stack, ToolClasses.WIRE_CUTTER);
@@ -114,8 +80,7 @@ protected boolean isPipeTool(@NotNull ItemStack stack) {
11480
@Override
11581
public int getLightValue(@NotNull IBlockState state, IBlockAccess world, @NotNull BlockPos pos) {
11682
TileEntity tile = world.getTileEntity(pos);
117-
if (tile instanceof TileEntityCable) {
118-
TileEntityCable cable = (TileEntityCable) tile;
83+
if (tile instanceof TileEntityCable cable) {
11984
int temp = cable.getTemperature();
12085
// max light at 5000 K
12186
// min light at 500 K
@@ -132,7 +97,9 @@ public int getLightValue(@NotNull IBlockState state, IBlockAccess world, @NotNul
13297
@Override
13398
public void breakBlock(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state) {
13499
if (worldIn.isRemote) {
135-
TileEntityCable cable = (TileEntityCable) getPipeTileEntity(worldIn, pos);
100+
IPipeTile<Insulation, WireProperties> pipeTile = getPipeTileEntity(worldIn, pos);
101+
if (pipeTile == null) return;
102+
TileEntityCable cable = (TileEntityCable) pipeTile;
136103
cable.killParticle();
137104
}
138105
super.breakBlock(worldIn, pos, state);
@@ -165,11 +132,12 @@ public void onEntityCollision(World worldIn, @NotNull BlockPos pos, @NotNull IBl
165132
@NotNull Entity entityIn) {
166133
super.onEntityCollision(worldIn, pos, state, entityIn);
167134
if (worldIn.isRemote) return;
168-
Insulation insulation = getPipeTileEntity(worldIn, pos).getPipeType();
169-
if (insulation.insulationLevel == -1 && entityIn instanceof EntityLivingBase) {
170-
EntityLivingBase entityLiving = (EntityLivingBase) entityIn;
171-
TileEntityCable cable = (TileEntityCable) getPipeTileEntity(worldIn, pos);
172-
if (cable != null && cable.getFrameMaterial() == null && cable.getNodeData().getLossPerBlock() > 0) {
135+
IPipeTile<Insulation, WireProperties> pipeTile = getPipeTileEntity(worldIn, pos);
136+
if (pipeTile == null) return;
137+
Insulation insulation = pipeTile.getPipeType();
138+
if (insulation.insulationLevel == -1 && entityIn instanceof EntityLivingBase entityLiving) {
139+
TileEntityCable cable = (TileEntityCable) pipeTile;
140+
if (cable.getFrameMaterial() == null && cable.getNodeData().getLossPerBlock() > 0) {
173141
long voltage = cable.getCurrentMaxVoltage();
174142
double amperage = cable.getAverageAmperage();
175143
if (voltage > 0L && amperage > 0L) {

0 commit comments

Comments
 (0)