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/enchantments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Enchantments API

The Enchantments API provides events and utilities for registering and working with enchantments.

## Registering Custom Enchantments

Enchantment registration should be done in a listener to the `REGISTER_ENCHANTMENTS` event. The `EnchantmentRegistry` class provides utility methods for registering enchantments.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.enchantments.api.EnchantmentEvents;
import net.ornithemc.osl.entrypoints.api.ModInitializer;

public class ExampleInitializer implements ModInitializer {

@Override
public void init() {
EnchantmentEvents.REGISTER_ENCHANTMENTS.register(ExampleEnchantments::init);
}
}
```

```java
package com.example;

import net.minecraft.enchantment.Enchantment;

import net.ornithemc.osl.core.util.NamespacedIdentifiers;
import net.ornithemc.osl.enchantments.api.EnchantmentRegistry;

public final class ExampleEnchantments {

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

public static void init() {
}
}
```
1 change: 1 addition & 0 deletions libraries/enchantments/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpLibrary(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
min_mc_version = 14w28a
max_mc_version = 1.8.9

minecraft_version = 1.8.9
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.ornithemc.osl.enchantments.api;

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

/**
* Events related to the enchantments lifecycle.
*/
public final class EnchantmentEvents {

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

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

import java.util.Set;

import net.minecraft.enchantment.Enchantment;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.enchantments.impl.EnchantmentRegistryImpl;
import net.ornithemc.osl.registries.api.registry.Registry;
import net.ornithemc.osl.registries.api.registry.ResourceKey;

/**
* Public access to the Enchantments registry.
*/
public final class EnchantmentRegistry {

public static final Registry<Enchantment> REGISTRY = EnchantmentRegistryImpl.REGISTRY;

/**
* @return the numerical ID assigned to the given enchantment.
*/
public static int getId(Enchantment enchantment) {
return EnchantmentRegistryImpl.getId(enchantment);
}

/**
* @return the namespaced ID assigned to the given enchantment.
*/
public static NamespacedIdentifier getIdentifier(Enchantment enchantment) {
return EnchantmentRegistryImpl.getIdentifier(enchantment);
}

/**
* @return the resource key assigned to the given enchantment.
*/
public static ResourceKey<Enchantment> getKey(Enchantment enchantment) {
return EnchantmentRegistryImpl.getKey(enchantment);
}

/**
* @return the enchantment mapped to the given numerical ID.
*/
public static Enchantment getEnchantment(int id) {
return EnchantmentRegistryImpl.getEnchantment(id);
}

/**
* @return the enchantment mapped to the given namespaced ID.
*/
public static Enchantment getEnchantment(NamespacedIdentifier identifier) {
return EnchantmentRegistryImpl.getEnchantment(identifier);
}

/**
* @return the enchantment mapped to the given resource key.
*/
public static Enchantment getEnchantment(ResourceKey<Enchantment> key) {
return EnchantmentRegistryImpl.getEnchantment(key);
}

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

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

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

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

/**
* @param <T> the enchantment type.
* @param id the numerical ID of the enchantment.
* @param key the namespaced ID of the enchantment.
* @param enchantment the enchantment to register.
* @return the registered enchantment.
*
* @deprecated use {@linkplain #register(NamespacedIdentifier, Enchantment)}
* or {@linkplain #register(ResourceKey, Enchantment)} instead.
*/
@Deprecated
public static <T extends Enchantment> T register(int id, NamespacedIdentifier key, T enchantment) {
return EnchantmentRegistryImpl.register(id, key, enchantment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.ornithemc.osl.enchantments.api.enchantment;

import net.minecraft.enchantment.Enchantment;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.enchantments.impl.EnchantmentRegistryImpl;
import net.ornithemc.osl.registries.api.registry.Registry;

public interface EnchantmentExtension {

Registry<Enchantment> REGISTRY = EnchantmentRegistryImpl.REGISTRY;
int AUTO_ASSIGN_ID = -172;
NamespacedIdentifier PLACEHOLDER_NAMESPACED_ID = NamespacedIdentifiers.from("osl", "placeholder");

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

import java.util.Set;

import net.minecraft.enchantment.Enchantment;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.enchantments.api.EnchantmentEvents;
import net.ornithemc.osl.enchantments.impl.enchantment.EnchantmentIdFixer;
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.RegistriesImpl;

public final class EnchantmentRegistryImpl {

public static final Registry<Enchantment> REGISTRY = RegistriesImpl.register(RegistryKeys.ENCHANTMENT, new WrappedEnchantmentRegistry(RegistryKeys.ENCHANTMENT.identifier()), () -> Enchantment.BY_ID.getClass());

private static boolean locked = true;

public static int getId(Enchantment enchantment) {
return REGISTRY.getId(enchantment);
}

public static NamespacedIdentifier getIdentifier(Enchantment enchantment) {
return REGISTRY.getIdentifier(enchantment);
}

public static ResourceKey<Enchantment> getKey(Enchantment enchantment) {
return REGISTRY.getKey(enchantment);
}

public static Enchantment getEnchantment(int id) {
return REGISTRY.get(id);
}

public static Enchantment getEnchantment(NamespacedIdentifier identifier) {
return REGISTRY.get(identifier);
}

public static Enchantment getEnchantment(ResourceKey<Enchantment> key) {
return REGISTRY.get(key);
}

public static Set<NamespacedIdentifier> identifierSet() {
return REGISTRY.identifierSet();
}

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

@SuppressWarnings("deprecation")
public static <T extends Enchantment> T register(NamespacedIdentifier identifier, T enchantment) {
if (locked) {
throw new IllegalStateException("register called too early: registry locked!");
} else {
return Registry.register(REGISTRY, enchantment.id, identifier, enchantment);
}
}

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

@Deprecated
public static <T extends Enchantment> T register(int id, NamespacedIdentifier key, T enchantment) {
if (locked) {
throw new IllegalStateException("register called too early: registry locked!");
} else {
if (enchantment.id != id) {
throw new IllegalArgumentException("ID " + id + " does not match enchantment ID " + enchantment.id + " for " + key);
}

return Registry.register(REGISTRY, id, key, enchantment);
}
}

public static void init() {
SyncedRegistries.register(RegistryKeys.ENCHANTMENT);
SyncedRegistries.registerFixer(RegistryKeys.ENCHANTMENT, NamespacedIdentifiers.from("enchantment/id"), new EnchantmentIdFixer());
}

public static void unlock() {
locked = false;
}

public static void registerEnchantments() {
VanillaEnchantments.init();
EnchantmentEvents.REGISTER_ENCHANTMENTS.invoker().run();
}

public static void registerUnknownEnchantments() {
for (Enchantment enchantment : Enchantment.BY_ID) {
if (enchantment != null && REGISTRY.getIdentifier(enchantment) == null) {
NamespacedIdentifier identifier = NamespacedIdentifiers.from("osl", "enchantment_" + enchantment.id);

RegistriesImpl.LOGGER.warn("Enchantment {}/{}", enchantment.id, enchantment.getClass().getSimpleName() + " was not registered to OSL's Enchantment registry! Adding it as '" + identifier + "'...");
EnchantmentRegistryImpl.register(identifier, enchantment);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.ornithemc.osl.enchantments.impl;

import net.ornithemc.osl.registries.api.RegistryEvents;

public final class EnchantmentsInit {

public static void init() {
RegistryEvents.INIT_REGISTRIES.register(EnchantmentRegistryImpl::init);
}
}
Loading
Loading