@Override public EntityRef getExistingBlockEntityAt(Vector3i blockPosition) { if (GameThread.isCurrentThread()) { EntityRef result = blockEntityLookup.get(blockPosition); return (result == null) ? EntityRef.NULL : result; } logger.error("Attempted to get block entity off-thread"); return EntityRef.NULL; }
@Override public boolean hasPermanentBlockEntity(Vector3i blockPos) { if (GameThread.isCurrentThread()) { EntityRef blockEntity = blockEntityLookup.get(blockPos); return blockEntity != null && !temporaryBlockEntities.contains(blockEntity); } logger.error("Attempted check whether a block entity is permanent, off thread"); return false; }
@Override public EntityRef getEntityAt(Vector3i blockPosition) { if (GameThread.isCurrentThread()) { EntityRef entity = getExistingEntityAt(blockPosition); if (!entity.exists()) { return getBlockEntityAt(blockPosition); } return entity; } logger.error("Attempted to get block entity off-thread"); return EntityRef.NULL; }
@Override public EntityRef getBlockEntityAt(Vector3i blockPosition) { if (GameThread.isCurrentThread()) { EntityRef blockEntity = getExistingBlockEntityAt(blockPosition); if (!blockEntity.exists() && isBlockRelevant(blockPosition.x, blockPosition.y, blockPosition.z)) { Block block = getBlock(blockPosition.x, blockPosition.y, blockPosition.z); blockEntity = createBlockEntity(blockPosition, block); } return blockEntity; } logger.error("Attempted to get block entity off-thread"); return EntityRef.NULL; }
@Override @SafeVarargs public final Block setBlockRetainComponent( Vector3i pos, Block type, Class<? extends Component>... components) { if (GameThread.isCurrentThread()) { EntityRef blockEntity = getBlockEntityAt(pos); Block oldType = super.setBlock(pos, type); if (oldType != null) { updateBlockEntity(blockEntity, pos, oldType, type, false, Sets.newHashSet(components)); } return oldType; } return null; }
@Override public Block setBlockForceUpdateEntity(Vector3i pos, Block type) { if (GameThread.isCurrentThread()) { EntityRef blockEntity = getBlockEntityAt(pos); Block oldType = super.setBlock(pos, type); if (oldType != null) { updateBlockEntity( blockEntity, pos, oldType, type, true, Collections.<Class<? extends Component>>emptySet()); } return oldType; } return null; }
@Before public void setup() { GameThread.setGameThread(); AssetManager assetManager = CoreRegistry.put( AssetManager.class, new AssetManager(new ModuleManagerImpl(new ModuleSecurityManager()))); assetManager.setAssetFactory( AssetType.PREFAB, new AssetFactory<PrefabData, Prefab>() { @Override public Prefab buildAsset(AssetUri uri, PrefabData data) { return new PojoPrefab(uri, data); } }); EntitySystemBuilder builder = new EntitySystemBuilder(); CoreRegistry.put(ComponentSystemManager.class, mock(ComponentSystemManager.class)); blockManager = CoreRegistry.put( BlockManager.class, new BlockManagerImpl(mock(WorldAtlas.class), new DefaultBlockFamilyFactoryRegistry())); NetworkSystem networkSystem = mock(NetworkSystem.class); when(networkSystem.getMode()).thenReturn(NetworkMode.NONE); entityManager = builder.build(moduleManager, networkSystem, new ReflectionReflectFactory()); worldStub = new WorldProviderCoreStub(BlockManager.getAir()); worldProvider = new EntityAwareWorldProvider(worldStub, entityManager); plainBlock = new Block(); blockManager.addBlockFamily( new SymmetricFamily(new BlockUri("test:plainBlock"), plainBlock), true); blockWithString = new Block(); PrefabData prefabData = new PrefabData(); prefabData.addComponent(new StringComponent("Test")); Assets.generateAsset( new AssetUri(AssetType.PREFAB, "test:prefabWithString"), prefabData, Prefab.class); blockWithString.setPrefab("test:prefabWithString"); blockManager.addBlockFamily( new SymmetricFamily(new BlockUri("test:blockWithString"), blockWithString), true); blockWithDifferentString = new Block(); prefabData = new PrefabData(); prefabData.addComponent(new StringComponent("Test2")); Assets.generateAsset( new AssetUri(AssetType.PREFAB, "test:prefabWithDifferentString"), prefabData, Prefab.class); blockWithDifferentString.setPrefab("test:prefabWithDifferentString"); blockManager.addBlockFamily( new SymmetricFamily( new BlockUri("test:blockWithDifferentString"), blockWithDifferentString), true); blockWithRetainedComponent = new Block(); prefabData = new PrefabData(); prefabData.addComponent(new RetainedOnBlockChangeComponent(3)); Assets.generateAsset( new AssetUri(AssetType.PREFAB, "test:prefabWithRetainedComponent"), prefabData, Prefab.class); blockWithRetainedComponent.setPrefab("test:prefabWithRetainedComponent"); blockManager.addBlockFamily( new SymmetricFamily( new BlockUri("test:blockWithRetainedComponent"), blockWithRetainedComponent), true); blockInFamilyOne = new Block(); blockInFamilyOne.setKeepActive(true); blockInFamilyOne.setPrefab("test:prefabWithString"); blockInFamilyTwo = new Block(); blockInFamilyTwo.setPrefab("test:prefabWithString"); blockInFamilyTwo.setKeepActive(true); blockManager.addBlockFamily( new HorizontalBlockFamily( new BlockUri("test:blockFamily"), ImmutableMap.<Side, Block>of( Side.FRONT, blockInFamilyOne, Side.LEFT, blockInFamilyTwo, Side.RIGHT, blockInFamilyTwo, Side.BACK, blockInFamilyOne), Collections.<String>emptyList()), true); keepActiveBlock = new Block(); keepActiveBlock.setKeepActive(true); keepActiveBlock.setPrefab("test:prefabWithString"); blockManager.addBlockFamily( new SymmetricFamily(new BlockUri("test:keepActiveBlock"), keepActiveBlock), true); worldProvider.initialise(); }