Skip to content

Revelation API

DaFuqs edited this page Mar 23, 2023 · 4 revisions

Registering a revealable block or item

If you are a creating a mod you want to use revelations in, you can either use the data pack format, or use Revelationary's API. When using the API you are given a few more possibilities on how to interact with Revelations. For example you are able to query if a block/item is visible to a player and are able to implement reactions when your blocks/items get revealed or hidden.

You find basic Blocks and Items that include the RevelationAware interface in the API package, ready to be used:

If you want to implement more detailed functionality just implement the RevelationAware Interface yourself. This single interface can be used for both blocks and items.

Example: Custom Log

public class MyCloakedLogBlock extends PillarBlock implements RevelationAware {
	
	public MyCloakedLogBlock(Settings settings) {
		super(settings);
		RevelationAware.register(this);
	}
	
	// return the identifier of the advancement a player must have to be able to see this block
	@Override
	public Identifier getCloakAdvancementIdentifier() {
		return new Identifier(MOD_ID, "my_advancement");
	}
	
	// returns a BlockState=>BlockState mapping for when this block is cloaked
	// In case of this custom log, it returns the BlockState of vanilla oak logs
	// that is turned in the same direction
	@Override
	public Map<BlockState, BlockState> getBlockStateCloaks() {
		Map<BlockState, BlockState> cloaks = new Hashtable<>();
		for (Direction.Axis axis : PillarBlock.AXIS.getValues()) {
			cloaks.put(this.getDefaultState().with(PillarBlock.AXIS, axis), Blocks.OAK_LOG.getDefaultState().with(PillarBlock.AXIS, axis));
		}
		return cloaks;
	}
	
	// How the item of this log should look to the player when cloaked
	// returns a item mapping from this logs item to the vanilla log item
	@Override
	public Pair<Item, Item> getItemCloak() {
		return new Pair<>(this.asItem(), Blocks.OAK_LOG.asItem());
	}
	
	// Implement custom logic when the block is getting hidden from the player
	// Optional
	@Override
	public void onCloak() {
		
	}
	
	// Implement custom logic when the block is made visible to the player
	// Optional
	@Override
	public void onUncloak() {
		// spawn particles
	}

	/**
	 * Optionally return a mapping of a revelation aware item and the text that should be used as translation
	 * If you return null (the default) it's name will be scattered unreadable instead
	 * @return the matching of the item and the text it will use when not revealed
	 */
	@Nullable default Pair<Item, MutableText> getCloakedItemTranslation() {
		return new Pair<>(this.asItem(), TranslatableText.of("block.demo.weird_log"));
	}
	
	/**
	 * Optionally return a mapping of a revelation aware block and the text that should be used as translation
	 * If you return null (the default) it's name will be scattered unreadable instead
	 * @return the matching of the block and the text it will use when not revealed
	 */
	@Nullable default Pair<Block, MutableText> getCloakedBlockTranslation() {
		return new Pair<>(this, TranslatableText.of("block.demo.weird_log"));
	}
	
	
}

Revelation Callback

If you want to receive notifications when revelations happen you can create your own RevealingCallback. This will provide you with a method that get's called every time blocks or items will get revealed and will include all blocks and items revealed, not just those from your mod. The flag "isJoinPacket" is set to true the first time a player joins a world and get's synched their advancements, right when logging in and all their advancements from previous play sessions get synched. Every time while the player is actively playing and revelations happen that boolean will be false.

You have to register your callback using RevealingCallback.register(this)

public class MyModClientClient implements ClientModInitializer, RevealingCallback {
	
	@Override
	public void onInitializeClient() {
		// [...]
		RevealingCallback.register(this);
	}
	
	@Override
	public void trigger(Set<Identifier> advancements, Set<Block> blocks, Set<Item> items, boolean isJoinPacket) {
		if(!isJoinPacket) {
			for (Block block : blocks) {
				if (Registry.BLOCK.getId(block).getNamespace().equals(SpectrumCommon.MOD_ID)) {
					// Show a chat message "You now can find new blocks of my mod"
					break;
				}
			}
		}
	}
	
}