Skip to content

Commit e3a1ebc

Browse files
committed
simplify and remove optional chaining
add helper methods in GTUtility
1 parent 32da5ec commit e3a1ebc

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

src/main/java/gregtech/api/block/machines/BlockMachine.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import net.minecraft.world.Explosion;
5555
import net.minecraft.world.IBlockAccess;
5656
import net.minecraft.world.World;
57-
import net.minecraft.world.chunk.Chunk;
5857
import net.minecraftforge.common.property.ExtendedBlockState;
5958
import net.minecraftforge.common.property.IExtendedBlockState;
6059
import net.minecraftforge.common.property.IUnlistedProperty;
@@ -74,7 +73,6 @@
7473
import java.util.Collections;
7574
import java.util.List;
7675
import java.util.Objects;
77-
import java.util.Optional;
7876
import java.util.Random;
7977
import java.util.Set;
8078

@@ -267,14 +265,13 @@ public boolean recolorBlock(@NotNull World world, @NotNull BlockPos pos, @NotNul
267265
@Override
268266
public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
269267
@NotNull EntityLivingBase placer, ItemStack stack) {
270-
MetaTileEntity metaTileEntity = Optional.of(stack)
271-
.map(GTUtility::getMetaTileEntity)
272-
.map(MetaTileEntity::copy)
273-
.orElse(null);
268+
MetaTileEntity sampleMetaTileEntity = GTUtility.getMetaTileEntity(stack);
274269

275-
if (metaTileEntity == null)
270+
if (sampleMetaTileEntity == null)
276271
return;
277272

273+
MetaTileEntity metaTileEntity = sampleMetaTileEntity.copy();
274+
278275
var stackTag = stack.getTagCompound();
279276
NBTTagCompound mteTag = null;
280277
if (stackTag != null && !stackTag.isEmpty()) {
@@ -300,7 +297,7 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
300297
metaTileEntity.readMTETag(mteTag);
301298
}
302299

303-
if (worldIn.getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) == null) {
300+
if (!GTUtility.hasTileEntity(worldIn, pos)) {
304301
worldIn.setTileEntity(pos, metaTileEntity);
305302
}
306303

src/main/java/gregtech/api/util/GTUtility.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import net.minecraft.world.IBlockAccess;
4444
import net.minecraft.world.World;
4545
import net.minecraft.world.biome.Biome;
46+
import net.minecraft.world.chunk.Chunk;
4647
import net.minecraftforge.common.BiomeDictionary;
4748
import net.minecraftforge.common.util.Constants;
4849
import net.minecraftforge.fluids.Fluid;
@@ -769,6 +770,30 @@ public static MetaTileEntity getMetaTileEntity(ItemStack stack) {
769770
return registry.getObjectById(stack.getItemDamage());
770771
}
771772

773+
public static MetaTileEntity getMetaTileEntity(NBTTagCompound tagCompound) {
774+
if (tagCompound.hasKey("MetaId")) {
775+
return getMetaTileEntity(tagCompound.getString("MetaId"));
776+
}
777+
return null;
778+
}
779+
780+
public static MetaTileEntity getMetaTileEntity(String resloc) {
781+
return getMetaTileEntity(new ResourceLocation(resloc));
782+
}
783+
784+
public static MetaTileEntity getMetaTileEntity(ResourceLocation location) {
785+
return getMetaTileEntity(location.getNamespace(), location);
786+
}
787+
788+
public static MetaTileEntity getMetaTileEntity(String modid, ResourceLocation location) {
789+
return GregTechAPI.mteManager.getRegistry(modid).getObject(location);
790+
}
791+
792+
public static boolean hasTileEntity(World world, BlockPos pos) {
793+
// it is technically possible for the world to have a TE, but not the chunk
794+
return world.getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) != null;
795+
}
796+
772797
/**
773798
* @param world the world containing the block
774799
* @param blockPos the position of the block to check

src/main/java/gregtech/client/utils/ClientHandlerHooks.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import gregtech.api.GregTechAPI;
44
import gregtech.api.metatileentity.MetaTileEntity;
55
import gregtech.api.metatileentity.registry.MTERegistry;
6+
import gregtech.api.util.GTUtility;
67

78
import net.minecraft.nbt.NBTTagCompound;
89
import net.minecraft.util.ResourceLocation;
910
import net.minecraft.util.math.BlockPos;
1011
import net.minecraft.world.World;
11-
import net.minecraft.world.chunk.Chunk;
1212

1313
import org.spongepowered.asm.mixin.Unique;
1414

@@ -35,9 +35,11 @@ public static void handleTags(World world, List<NBTTagCompound> tagCompounds) {
3535
* @param tagCompound TileEntity tag data
3636
*/
3737
public static void handleTag(World world, NBTTagCompound tagCompound) {
38-
MetaTileEntity mte = fromTag(tagCompound);
38+
MetaTileEntity mte = GTUtility.getMetaTileEntity(tagCompound);
3939
if (mte != null) {
40-
BlockPos pos = new BlockPos(tagCompound.getInteger("x"), tagCompound.getInteger("y"),
40+
BlockPos pos = new BlockPos(
41+
tagCompound.getInteger("x"),
42+
tagCompound.getInteger("y"),
4143
tagCompound.getInteger("z"));
4244
placeTile(world, mte, pos);
4345
}
@@ -69,9 +71,8 @@ private static void placeTile(World world, MetaTileEntity mte, BlockPos pos) {
6971
// set te in world directly
7072
// check if world contains a TE at this pos?
7173
// is null checking good enough?
72-
if (world.getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) == null) {
73-
if (world.getBlockState(pos).getBlock() != mte.getBlock())
74-
world.setBlockState(pos, mte.getBlock().getDefaultState());
74+
if (!GTUtility.hasTileEntity(world, pos)) {
75+
world.setBlockState(pos, mte.getBlock().getDefaultState());
7576
world.setTileEntity(pos, mte.copy());
7677
}
7778
}

src/main/java/gregtech/mixins/minecraft/TileEntityMixin.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package gregtech.mixins.minecraft;
22

3-
import gregtech.api.GregTechAPI;
43
import gregtech.api.metatileentity.GTBaseTileEntity;
54
import gregtech.api.metatileentity.MetaTileEntity;
65
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
7-
import gregtech.api.util.GTLog;
6+
import gregtech.api.util.GTUtility;
87

98
import net.minecraft.nbt.NBTTagCompound;
109
import net.minecraft.tileentity.TileEntity;
11-
import net.minecraft.util.ResourceLocation;
1210

1311
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
1412
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
@@ -36,11 +34,7 @@ private static Object wrapNewInstance(Class<? extends TileEntity> instance,
3634
@Local(argsOnly = true) NBTTagCompound tagCompound) {
3735
if (IGregTechTileEntity.class.isAssignableFrom(instance)) {
3836
// this is necessary to avoid the no args constructor call
39-
var location = new ResourceLocation(tagCompound.getString("MetaId"));
40-
MetaTileEntity mte = GregTechAPI.mteManager
41-
.getRegistry(location.getNamespace())
42-
.getObject(location);
43-
37+
MetaTileEntity mte = GTUtility.getMetaTileEntity(tagCompound.getString("MetaId"));
4438
if (mte != null) return mte.copy();
4539
}
4640
return original.call(instance);

0 commit comments

Comments
 (0)