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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class BlockEvents {
* <pre>
* {@code
* BlockEvents.REGISTER_BLOCKS.register(() -> {
* BlockRegistry.register(99, NamespacedIdentifiers.from("example", "cookie"), new CookieBlock());
* BlockRegistry.register(NamespacedIdentifiers.from("example", "cookie"), new CookieBlock());
* });
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

import net.ornithemc.osl.blocks.impl.BlockRegistryImpl;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.registries.api.registry.DefaultedRegistry;
import net.ornithemc.osl.registries.api.registry.ResourceKey;

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

public static final DefaultedRegistry<Block> REGISTRY = BlockRegistryImpl.REGISTRY;

/**
* @return the numerical ID assigned to the given block.
*/
Expand All @@ -22,7 +26,14 @@ public static int getId(Block block) {
/**
* @return the namespaced ID assigned to the given block.
*/
public static NamespacedIdentifier getKey(Block block) {
public static NamespacedIdentifier getIdentifier(Block block) {
return BlockRegistryImpl.getIdentifier(block);
}

/**
* @return the resource key assigned to the given block.
*/
public static ResourceKey<Block> getKey(Block block) {
return BlockRegistryImpl.getKey(block);
}

Expand All @@ -36,24 +47,62 @@ public static Block getBlock(int id) {
/**
* @return the block mapped to the given namespaced ID.
*/
public static Block getBlock(NamespacedIdentifier key) {
public static Block getBlock(NamespacedIdentifier identifier) {
return BlockRegistryImpl.getBlock(identifier);
}

/**
* @return the block mapped to the given resource key.
*/
public static Block getBlock(ResourceKey<Block> key) {
return BlockRegistryImpl.getBlock(key);
}

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

/**
* @return a set containing all resource keys in the registry.
*/
public static Set<ResourceKey<Block>> keySet() {
return BlockRegistryImpl.keySet();
}

/**
* @param <T> the block type.
* @param identifier the namespaced ID of the block.
* @param block the block to register.
* @return the registered block.
*/
public static <T extends Block> T register(NamespacedIdentifier identifier, T block) {
return BlockRegistryImpl.register(identifier, block);
}

/**
* @param <T> the block type.
* @param key the resource key of the block.
* @param block the block to register.
* @return the registered block.
*/
public static <T extends Block> T register(ResourceKey<Block> key, T block) {
return BlockRegistryImpl.register(key, block);
}

/**
* @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.
*
* @deprecated use {@linkplain #register(NamespacedIdentifier, Block)}
* or {@linkplain #register(ResourceKey, Block)} instead.
*/
@Deprecated
public static <T extends Block> T register(int id, NamespacedIdentifier key, T block) {
return BlockRegistryImpl.register(id, key, block);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,90 @@
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;
import net.ornithemc.osl.registries.api.registry.DefaultedRegistry;
import net.ornithemc.osl.registries.api.registry.Registry;
import net.ornithemc.osl.registries.api.registry.RegistryKeys;
import net.ornithemc.osl.registries.api.registry.ResourceKey;
import net.ornithemc.osl.registries.api.registry.SyncedRegistries;
import net.ornithemc.osl.registries.impl.registry.VanillaRegistries;

public final class BlockRegistryImpl {

public static final DefaultedRegistry<Block> REGISTRY = VanillaRegistries.registerDefaulted(RegistryKeys.BLOCK, Block.REGISTRY);

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

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

public static NamespacedIdentifier getIdentifier(Block block) {
return REGISTRY.getIdentifier(block);
}

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

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

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

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

public static <T extends Block> T register(int id, NamespacedIdentifier key, T block) {
public static Set<NamespacedIdentifier> identifierSet() {
return REGISTRY.identifierSet();
}

public static Set<ResourceKey<Block>> keySet() {
return REGISTRY.keySet();
}

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

return block;
public static <T extends Block> T register(ResourceKey<Block> key, T block) {
if (locked) {
throw new IllegalStateException("register called too early: registry locked!");
} else {
return Registry.register(REGISTRY, key, block);
}
}

public static void lock() {
if (!initialized) {
throw new IllegalStateException("cannot lock block registry unless it's been initialized!");
@Deprecated
public static <T extends Block> T register(int id, NamespacedIdentifier key, T block) {
if (locked) {
throw new IllegalStateException("register called too early: registry locked!");
} else {
return Registry.register(REGISTRY, id, key, block);
}
}

locked = true;
public static void init() {
SyncedRegistries.register(RegistryKeys.BLOCK);
}

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!");
}

public static void registerBlocks() {
BlockEvents.REGISTER_BLOCKS.invoker().run();
initialized = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public AirBlock() {
super(Material.AIR);
}

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

@Override
public int getRenderType() {
return -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.ornithemc.osl.blocks.impl.block;

public interface BlockPostInit {

void osl$blocks$postInit();

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package net.ornithemc.osl.blocks.impl.mixin.common;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.block.Block;
import net.minecraft.entity.living.mob.monster.EndermanEntity;

import net.ornithemc.osl.blocks.api.BlockRegistry;
import net.ornithemc.osl.blocks.api.block.BlockExtension;
import net.ornithemc.osl.blocks.impl.BlockRegistryImpl;
import net.ornithemc.osl.blocks.impl.BlocksMixinPlugin;
import net.ornithemc.osl.blocks.impl.block.AirBlock;
import net.ornithemc.osl.blocks.impl.block.BlockPostInit;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.core.impl.util.Util;
import net.ornithemc.osl.registries.api.registry.RegistryKeys;
import net.ornithemc.osl.registries.api.registry.SyncedRegistries;
import net.ornithemc.osl.registries.api.registry.sync.BooleanArrayMapper;

@Mixin(Block.class)
public class BlockMixin implements BlockExtension {

@Shadow
private String key;

@Inject(
method = "init",
at = @At(
Expand All @@ -36,14 +50,47 @@ public class BlockMixin implements BlockExtension {
target = "Lnet/minecraft/util/registry/IdRegistry;iterator()Ljava/util/Iterator;"
)
)
private static void osl$blocks$initAndLockBlockRegistry(CallbackInfo ci) {
BlockRegistryImpl.init();
BlockRegistryImpl.lock();
private static void osl$blocks$registerBlocks(CallbackInfo ci) {
BlockRegistryImpl.registerBlocks();

for (Block block : Block.REGISTRY) {
if (block instanceof BlockPostInit) {
((BlockPostInit) block).osl$blocks$postInit();
}
}
}

@Inject(
method = "init",
at = @At(
value = "TAIL"
)
)
private static void osl$blocks$registerArrayMappers(CallbackInfo ci) {
SyncedRegistries.registerMapper(RegistryKeys.BLOCK, NamespacedIdentifiers.from("block/enderman_holdable"), BooleanArrayMapper.of(EndermanEntity.HOLDABLE_BLOCKS));
}

@Inject(
method = "getTranslationKey",
at = @At(
value = "HEAD"
)
)
private void osl$blocks$autoAssignTranslationKey(CallbackInfoReturnable<String> cir) {
if (this.key == null) {
NamespacedIdentifier identifier = BlockRegistry.getIdentifier((Block) (Object) this);

if (identifier == null) {
this.key = "unknown";
} else {
this.key = Util.makeTranslationKey(identifier);
}
}
}

@Override
public String toString() {
return "Block{" + BlockRegistryImpl.getKey((Block) (Object) this) + "}";
return "Block{" + BlockRegistryImpl.getIdentifier((Block) (Object) this) + "}";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.ornithemc.osl.blocks.impl.mixin.common;

import org.objectweb.asm.Opcodes;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.Block;
import net.minecraft.entity.living.mob.monster.EndermanEntity;

import net.ornithemc.osl.registries.api.registry.sync.DynamicArrays;

@Mixin(EndermanEntity.class)
public class EndermanEntityMixin {

// what the fuck, mojang?
@Shadow @Final @Mutable
private static boolean[] HOLDABLE_BLOCKS;

@Inject(
method = "<clinit>",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/entity/living/mob/monster/EndermanEntity;HOLDABLE_BLOCKS:[Z",
opcode = Opcodes.PUTSTATIC,
shift = Shift.AFTER
)
)
private static void osl$blocks$growArray(CallbackInfo ci) {
int capacity = HOLDABLE_BLOCKS.length;

for (Block block : Block.REGISTRY) {
int required = Block.getId(block) + 1;

if (required > capacity) {
capacity = required;
}
}

HOLDABLE_BLOCKS = DynamicArrays.grow(HOLDABLE_BLOCKS, capacity);
}
}
Loading
Loading