-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IDI Working with AE2 block entity and first test of storagee indexing
- Loading branch information
Showing
4 changed files
with
177 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 9 additions & 82 deletions
91
...n/java/net/nussi/dedicated_applied_energistics/blocks/InterDimensionalInterfaceBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,30 @@ | ||
package net.nussi.dedicated_applied_energistics.blocks; | ||
|
||
import appeng.init.InitBlockEntities; | ||
import appeng.api.orientation.IOrientationStrategy; | ||
import appeng.api.orientation.OrientationStrategies; | ||
import appeng.block.AEBaseEntityBlock; | ||
import com.mojang.logging.LogUtils; | ||
import com.mojang.serialization.MapCodec; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.EntityBlock; | ||
import net.minecraft.world.level.block.HorizontalDirectionalBlock; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockBehaviour; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.api.distmarker.OnlyIn; | ||
import net.minecraftforge.event.level.BlockEvent; | ||
import net.minecraftforge.event.level.ChunkEvent; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.nussi.dedicated_applied_energistics.DedicatedAppliedEnegistics; | ||
import net.minecraft.world.level.block.state.StateDefinition; | ||
import net.nussi.dedicated_applied_energistics.blockentities.InterDimensionalInterfaceBlockEntity; | ||
import net.nussi.dedicated_applied_energistics.init.BlockEntityTypeInit; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.HashMap; | ||
|
||
import static net.minecraft.world.level.levelgen.structure.Structure.simpleCodec; | ||
import static net.nussi.dedicated_applied_energistics.DedicatedAppliedEnegistics.MODID; | ||
|
||
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.FORGE) | ||
public class InterDimensionalInterfaceBlock extends Block implements EntityBlock { | ||
public class InterDimensionalInterfaceBlock extends AEBaseEntityBlock<InterDimensionalInterfaceBlockEntity> { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
private static final HashMap<BlockPos, InterDimensionalInterfaceBlockEntity> blockEntities = new HashMap<>(); | ||
|
||
public InterDimensionalInterfaceBlock(Properties properties) { | ||
super(properties); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { | ||
InterDimensionalInterfaceBlockEntity blockEntity = blockEntities.get(pos); | ||
if(blockEntity != null) { | ||
blockEntity.onStop(); | ||
} | ||
blockEntity = BlockEntityTypeInit.INTER_DIMENSIONAL_INTERFACE_ENTITY_TYPE.get().create(pos, state); | ||
blockEntities.put(pos, blockEntity); | ||
|
||
return blockEntity; | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { | ||
super.createBlockStateDefinition(builder); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) { | ||
return type == BlockEntityTypeInit.INTER_DIMENSIONAL_INTERFACE_ENTITY_TYPE.get() ? InterDimensionalInterfaceBlockEntity::tick : null; | ||
} | ||
|
||
public static void onStartBlockPos(BlockPos pos) { | ||
InterDimensionalInterfaceBlockEntity blockEntity = blockEntities.get(pos); | ||
if(blockEntity == null) { | ||
LOGGER.debug("Tried to start block entity that dosen't exist."); | ||
return; | ||
} | ||
blockEntity.onStart(); | ||
} | ||
|
||
public static void onStopBlockPos(BlockPos pos) { | ||
InterDimensionalInterfaceBlockEntity blockEntity = blockEntities.get(pos); | ||
if(blockEntity == null) { | ||
LOGGER.debug("Tried to stop block entity that dosen't exist."); | ||
return; | ||
} | ||
blockEntity.onStop(); | ||
public IOrientationStrategy getOrientationStrategy() { | ||
return OrientationStrategies.none(); | ||
} | ||
|
||
@SubscribeEvent | ||
public static void onBlockPlace(BlockEvent.EntityPlaceEvent event) { | ||
onStartBlockPos(event.getPos()); | ||
} | ||
|
||
@SubscribeEvent | ||
public static void onBlockBreak(BlockEvent.BreakEvent event) { | ||
onStopBlockPos(event.getPos()); | ||
} | ||
|
||
|
||
@SubscribeEvent | ||
public static void onBlockLoad(ChunkEvent.Load event) { | ||
for (BlockPos blockEntityPositions : event.getChunk().getBlockEntitiesPos()) { | ||
onStartBlockPos(blockEntityPositions); | ||
} | ||
} | ||
|
||
@SubscribeEvent | ||
public static void onBlockUnload(ChunkEvent.Unload event) { | ||
for (BlockPos blockEntityPositions : event.getChunk().getBlockEntitiesPos()) { | ||
onStopBlockPos(blockEntityPositions); | ||
} | ||
} | ||
} |
54 changes: 53 additions & 1 deletion
54
src/main/java/net/nussi/dedicated_applied_energistics/init/BlockEntityTypeInit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,68 @@ | ||
package net.nussi.dedicated_applied_energistics.init; | ||
|
||
import appeng.block.AEBaseEntityBlock; | ||
import appeng.blockentity.AEBaseBlockEntity; | ||
import appeng.blockentity.ClientTickingBlockEntity; | ||
import appeng.blockentity.ServerTickingBlockEntity; | ||
import appeng.core.AppEng; | ||
import appeng.core.definitions.AEBlockEntities; | ||
import appeng.core.definitions.BlockDefinition; | ||
import com.google.common.base.Preconditions; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraftforge.registries.DeferredRegister; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
import net.minecraftforge.registries.RegistryObject; | ||
import net.nussi.dedicated_applied_energistics.blockentities.InterDimensionalInterfaceBlockEntity; | ||
import net.nussi.dedicated_applied_energistics.blocks.InterDimensionalInterfaceBlock; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static net.nussi.dedicated_applied_energistics.DedicatedAppliedEnegistics.MODID; | ||
|
||
public class BlockEntityTypeInit { | ||
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, MODID); | ||
|
||
public static final RegistryObject<BlockEntityType<InterDimensionalInterfaceBlockEntity>> INTER_DIMENSIONAL_INTERFACE_ENTITY_TYPE = BLOCK_ENTITY_TYPES.register("inter_dimensional_interface", () -> BlockEntityType.Builder.of(InterDimensionalInterfaceBlockEntity::new, BlockInit.INTER_DIMENSIONAL_INTERFACE_BLOCK.get()).build(null)); | ||
// public static final RegistryObject<BlockEntityType<InterDimensionalInterfaceBlockEntity>> INTER_DIMENSIONAL_INTERFACE_ENTITY_TYPE = BLOCK_ENTITY_TYPES.register("inter_dimensional_interface", () -> | ||
// BlockEntityType.Builder | ||
// .of(InterDimensionalInterfaceBlockEntity::new, BlockInit.INTER_DIMENSIONAL_INTERFACE_BLOCK.get()) | ||
// .build(null)); | ||
|
||
public static final RegistryObject<BlockEntityType<InterDimensionalInterfaceBlockEntity>> INTER_DIMENSIONAL_INTERFACE_ENTITY_TYPE = BLOCK_ENTITY_TYPES.register("temp", () -> | ||
create(InterDimensionalInterfaceBlockEntity.class, BlockEntityType.Builder | ||
.of(InterDimensionalInterfaceBlockEntity::new, BlockInit.INTER_DIMENSIONAL_INTERFACE_BLOCK.get()) | ||
.build(null), (AEBaseEntityBlock<InterDimensionalInterfaceBlockEntity>) BlockInit.INTER_DIMENSIONAL_INTERFACE_BLOCK.get())); | ||
|
||
|
||
public static <T extends AEBaseBlockEntity> BlockEntityType<T> create(Class<T> entityClass, | ||
BlockEntityType<T> type, | ||
AEBaseEntityBlock<T> block) { | ||
|
||
// If the block entity classes implement specific interfaces, automatically register them | ||
// as tickers with the blocks that create that entity. | ||
BlockEntityTicker<T> serverTicker = null; | ||
if (ServerTickingBlockEntity.class.isAssignableFrom(entityClass)) { | ||
serverTicker = (level, pos, state, entity) -> { | ||
((ServerTickingBlockEntity) entity).serverTick(); | ||
}; | ||
} | ||
BlockEntityTicker<T> clientTicker = null; | ||
if (ClientTickingBlockEntity.class.isAssignableFrom(entityClass)) { | ||
clientTicker = (level, pos, state, entity) -> { | ||
((ClientTickingBlockEntity) entity).clientTick(); | ||
}; | ||
} | ||
|
||
block.setBlockEntity(entityClass, type, clientTicker, serverTicker); | ||
|
||
return type; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters