Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static void init() {
}

VanillaBlocks.init();

BlockEvents.REGISTER_BLOCKS.invoker().run();
initialized = true;
}
Expand Down
41 changes: 41 additions & 0 deletions libraries/items/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Items API

The Items API provides events and utilities for registering and working with items.

## Registering Custom Items

Item registration should be done in a listener to the `REGISTER_ITEMS` event. The `ItemRegistry` class provides utility methods for registering items.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.entrypoints.api.ModInitializer;
import net.ornithemc.osl.items.api.ItemEvents;

public class ExampleInitializer implements ModInitializer {

@Override
public void init() {
ItemEvents.REGISTER_ITEMS.register(ExampleItems::init);
}
}
```

```java
package com.example;

import net.minecraft.item.Item;

import net.ornithemc.osl.core.util.NamespacedIdentifiers;
import net.ornithemc.osl.items.api.ItemRegistry;

public final class ExampleItems {

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

public static void init() {
}
}
```
1 change: 1 addition & 0 deletions libraries/items/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpLibrary(project)
6 changes: 6 additions & 0 deletions libraries/items/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library_id = items
library_name = Items
library_description = Items API and events.
library_version = 0.1.0-alpha.1

osl_dependencies = core:>=0.7.0,blocks:>=0.1.0-
1 change: 1 addition & 0 deletions libraries/items/items-mc13w36a-mc14w25b/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
8 changes: 8 additions & 0 deletions libraries/items/items-mc13w36a-mc14w25b/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
min_mc_version = 13w36a
max_mc_version = 14w25b
minecraft_dependency = >=1.7-alpha.13.36.a <=1.8-alpha.14.25.b

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.items.api;

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

/**
* Events related to the items lifecycle.
*/
public final class ItemEvents {

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

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

import java.util.Set;

import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.items.impl.ItemRegistryImpl;

/**
* Public access to the Items registry.
*/
public final class ItemRegistry {

/**
* @return the numerical ID assigned to the given item.
*/
public static int getId(Item item) {
return ItemRegistryImpl.getId(item);
}

/**
* @return the namespaced ID assigned to the given item.
*/
public static NamespacedIdentifier getKey(Item item) {
return ItemRegistryImpl.getKey(item);
}

/**
* @return the item mapped to the given numerical ID.
*/
public static Item getItem(int id) {
return ItemRegistryImpl.getItem(id);
}

/**
* @return the item mapped to the given namespaced ID.
*/
public static Item getItem(NamespacedIdentifier key) {
return ItemRegistryImpl.getItem(key);
}

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

/**
* @param block the block item to register.
* @return the registered block item.
*/
public static BlockItem register(Block block) {
return ItemRegistryImpl.register(block);
}

/**
* @param <T> the item type.
* @param item the item to register.
* @return the registered block item.
*/
public static <T extends BlockItem> T register(T item) {
return ItemRegistryImpl.register(item);
}

/**
* @param <T> the item type.
* @param block the block placed by the item.
* @param item the item to register.
* @return the registered item.
*/
public static <T extends Item> T register(Block block, T item) {
return ItemRegistryImpl.register(block, item);
}

/**
* @param <T> the item type.
* @param id the numerical ID of the item.
* @param key the namespaced ID of the item.
* @param item the item to register.
* @return the registered item.
*/
public static <T extends Item> T register(int id, NamespacedIdentifier key, T item) {
return ItemRegistryImpl.register(id, key, item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package net.ornithemc.osl.items.impl;

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

import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;

import net.ornithemc.osl.blocks.api.BlockRegistry;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.items.api.ItemEvents;
import net.ornithemc.osl.items.impl.mixin.common.BlockItemAccess;

public final class ItemRegistryImpl {

public static final Map<Block, Item> BLOCK_ITEMS = new HashMap<>();

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

public static int getId(Item item) {
return Item.REGISTRY.getId(item);
}

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

public static Item getItem(int id) {
return Item.REGISTRY.get(id);
}

public static Item getItem(NamespacedIdentifier key) {
return Item.REGISTRY.get(key.toString());
}

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

public static BlockItem register(Block block) {
return register(block, new BlockItem(block));
}

public static <T extends BlockItem> T register(T item) {
return register(((BlockItemAccess) item).accessBlock(), item);
}

public static <T extends Item> T register(Block block, T item) {
if (!locked) {
BLOCK_ITEMS.put(block, item);
}

return register(BlockRegistry.getId(block), BlockRegistry.getKey(block), item);
}

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

return item;
}

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

locked = true;
}

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

locked = false;
}

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

ItemEvents.REGISTER_ITEMS.invoker().run();
initialized = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.ornithemc.osl.items.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 ItemsMixinPlugin implements IMixinConfigPlugin {

public static final boolean SPECIAL_BLOCK_ITEM_HANDLING = MinecraftVersion.resolve().compareTo("13w37a") >= 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.items.impl.mixin.common.ItemMixinNew".equals(mixinClassName)) {
return SPECIAL_BLOCK_ITEM_HANDLING;
}
if ("net.ornithemc.osl.items.impl.mixin.common.ItemMixinOld".equals(mixinClassName)) {
return !SPECIAL_BLOCK_ITEM_HANDLING;
}

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,15 @@
package net.ornithemc.osl.items.impl.mixin.common;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;

@Mixin(BlockItem.class)
public interface BlockItemAccess {

@Accessor("block")
Block accessBlock();

}
Loading
Loading