Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions libraries/blocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Blocks API

The Blocks API provides events and utilities for registering and working with blocks.

## Registering Custom Blocks

Block registration should be done in a listener to the `REGISTER_BLOCKS` event. The `BlockRegistry` class provides utility methods for registering blocks.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.blocks.api.BlockEvents;
import net.ornithemc.osl.entrypoints.api.ModInitializer;

public class ExampleInitializer implements ModInitializer {

@Override
public void init() {
BlockEvents.REGISTER_BLOCKS.register(ExampleBlocks::init);
}
}
```

```java
package com.example;

import net.minecraft.block.Block;

import net.ornithemc.osl.blocks.api.BlockRegistry;
import net.ornithemc.osl.core.util.NamespacedIdentifiers;

public final class ExampleBlocks {

public static final CookieBlock COOKIE = BlockRegistry.register(NamespacedIdentifiers.from("example", "cookie"), new CookieBlock());

public static void init() {
}
}
```
1 change: 1 addition & 0 deletions libraries/blocks/blocks-mc13w36a-mc14w26c/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
8 changes: 8 additions & 0 deletions libraries/blocks/blocks-mc13w36a-mc14w26c/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
min_mc_version = 13w36a
max_mc_version = 14w26c
minecraft_dependency = >=1.7-alpha.13.36.a <=1.8-alpha.14.26.c

minecraft_version = 1.7.2
raven_build = 1
sparrow_build = 1
nests_build = 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.ornithemc.osl.blocks.api;

import net.ornithemc.osl.core.api.events.Event;

/**
* Events related to the blocks lifecycle.
*/
public final class BlockEvents {

/**
* This event is invoked upon game start-up, after Vanilla block registration is finished.
*
* <p>
* Custom block registration should be done in a listener of this event.
* Helper methods for registering blocks can be found in the {@linkplain
* BlockRegistry} class.
*
* <p>
* Listeners to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* BlockEvents.REGISTER_BLOCKS.register(() -> {
* BlockRegistry.register(99, NamespacedIdentifiers.from("example", "cookie"), new CookieBlock());
* });
* }
* </pre>
*
* @see BlockRegistry
*/
public static final Event<Runnable> REGISTER_BLOCKS = Event.runnable();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.ornithemc.osl.blocks.api;

import java.util.Set;

import net.minecraft.block.Block;

import net.ornithemc.osl.blocks.impl.BlockRegistryImpl;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;

/**
* Public access to the Blocks registry.
*/
public final class BlockRegistry {

/**
* @return the numerical ID assigned to the given block.
*/
public static int getId(Block block) {
return BlockRegistryImpl.getId(block);
}

/**
* @return the namespaced ID assigned to the given block.
*/
public static NamespacedIdentifier getKey(Block block) {
return BlockRegistryImpl.getKey(block);
}

/**
* @return the block mapped to the given numerical ID.
*/
public static Block getBlock(int id) {
return BlockRegistryImpl.getBlock(id);
}

/**
* @return the block mapped to the given namespaced ID.
*/
public static Block getBlock(NamespacedIdentifier key) {
return BlockRegistryImpl.getBlock(key);
}

/**
* @return a set containing all namespaced IDs in the registry.
*/
public static Set<NamespacedIdentifier> keySet() {
return BlockRegistryImpl.keySet();
}

/**
* @param <T> the block type.
* @param id the numerical ID of the block.
* @param key the namespaced ID of the block.
* @param block the block to register.
* @return the registered block.
*/
public static <T extends Block> T register(int id, NamespacedIdentifier key, T block) {
return BlockRegistryImpl.register(id, key, block);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.ornithemc.osl.blocks.api.block;

public interface BlockExtension {

/**
* @return whether this block is air.
*/
boolean isAir();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ornithemc.osl.blocks.api.block;

import net.minecraft.block.Block;

public interface BlocksExtension {

Block AIR = Block.REGISTRY.get("air");

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.ornithemc.osl.blocks.impl;

import java.util.Set;
import java.util.stream.Collectors;

import net.minecraft.block.Block;

import net.ornithemc.osl.blocks.api.BlockEvents;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;

public final class BlockRegistryImpl {

private static boolean locked = true;
private static boolean initialized = false;

public static int getId(Block block) {
return Block.REGISTRY.getId(block);
}

public static NamespacedIdentifier getKey(Block block) {
String key = Block.REGISTRY.getKey(block);
return key == null ? null : NamespacedIdentifiers.parse(key);
}

public static Block getBlock(int id) {
return Block.REGISTRY.get(id);
}

public static Block getBlock(NamespacedIdentifier key) {
return Block.REGISTRY.get(key.toString());
}

public static Set<NamespacedIdentifier> keySet() {
return Block.REGISTRY.keySet().stream().map(NamespacedIdentifiers::parse).collect(Collectors.toSet());
}

public static <T extends Block> T register(int id, NamespacedIdentifier key, T block) {
if (locked) {
throw new IllegalStateException("register called too " + (initialized ? "late" : "early") + ": registry locked!");
} else {
Block.REGISTRY.register(id, key.toString(), block);
}

return block;
}

public static void lock() {
if (!initialized) {
throw new IllegalStateException("cannot lock block registry unless it's been initialized!");
}

locked = true;
}

public static void unlock() {
if (initialized) {
throw new IllegalStateException("cannot unlock block registry once it's been initialized!");
}

locked = false;
}

public static void init() {
if (locked) {
throw new IllegalStateException("cannot initialize block registry when it's locked!");
}

BlockEvents.REGISTER_BLOCKS.invoker().run();
initialized = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.ornithemc.osl.blocks.impl;

import java.util.List;
import java.util.Set;

import org.objectweb.asm.tree.ClassNode;

import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import net.ornithemc.osl.core.impl.util.MinecraftVersion;

public class BlocksMixinPlugin implements IMixinConfigPlugin {

public static final boolean AIR_BLOCK_EXISTS = MinecraftVersion.resolve().compareTo("13w38b") >= 0;

@Override
public void onLoad(String mixinPackage) {
}

@Override
public String getRefMapperConfig() {
return null;
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if ("net.ornithemc.osl.blocks.impl.mixin.common.AirBlockMixin".equals(mixinClassName)) {
return AIR_BLOCK_EXISTS;
}
if ("net.ornithemc.osl.blocks.impl.mixin.common.BlocksMixin".equals(mixinClassName)) {
return !AIR_BLOCK_EXISTS;
}

return true;
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
}

@Override
public List<String> getMixins() {
return null;
}

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.ornithemc.osl.blocks.impl.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;

public class AirBlock extends Block {

public AirBlock() {
super(Material.AIR);
}

@Override
public int getRenderType() {
return -1;
}

@Override
public Box getCollisionShape(World world, int x, int y, int z) {
return null;
}

@Override
public boolean isSolidRender() {
return false;
}

@Override
public boolean canRayTrace(int metadata, boolean allowLiquids) {
return false;
}

@Override
public void dropItems(World world, int x, int y, int z, int metadata, float luck, int fortuneLevel) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.ornithemc.osl.blocks.impl.block;

import net.ornithemc.osl.blocks.api.block.BlockExtension;

public interface BlockExtensionImpl extends BlockExtension {

@Override
default boolean isAir() {
throw new AbstractMethodError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.ornithemc.osl.blocks.impl.block;

import net.ornithemc.osl.blocks.api.block.BlocksExtension;

public interface BlocksExtensionImpl extends BlocksExtension {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.ornithemc.osl.blocks.impl.mixin.common;

import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.block.AirBlock;

import net.ornithemc.osl.blocks.api.block.BlockExtension;

@Mixin(AirBlock.class)
public class AirBlockMixin implements BlockExtension {

@Override
public boolean isAir() {
return true;
}
}
Loading
Loading