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

The Effects API provides events and utilities for registering and working with status effects.

## Registering Custom Status Effects

Effect registration should be done in a listener to the `REGISTER_STATUS_EFFECTS` event. The `StatusEffectRegistry` class provides utility methods for registering effects.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.effects.api.StatusEffectEvents;
import net.ornithemc.osl.entrypoints.api.ModInitializer;

public class ExampleInitializer implements ModInitializer {

@Override
public void init() {
StatusEffectEvents.REGISTER_STATUS_EFFECTS.register(ExampleStatusEffects::init);
}
}
```

```java
package com.example;

import net.minecraft.entity.living.effect.StatusEffect;

import net.ornithemc.osl.core.util.NamespacedIdentifiers;
import net.ornithemc.osl.effects.api.StatusEffectRegistry;

public final class ExampleStatusEffects {

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

public static void init() {
}
}
```
1 change: 1 addition & 0 deletions libraries/effects/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpLibrary(project)
1 change: 1 addition & 0 deletions libraries/effects/effects-mc14w28a-mc1.8.9/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
4 changes: 4 additions & 0 deletions libraries/effects/effects-mc14w28a-mc1.8.9/gradle.properties
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.effects.api;

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

/**
* Events related to the status effects lifecycle.
*/
public final class StatusEffectEvents {

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

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

import java.util.Set;

import net.minecraft.entity.living.effect.StatusEffect;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.effects.impl.StatusEffectRegistryImpl;
import net.ornithemc.osl.registries.api.registry.Registry;
import net.ornithemc.osl.registries.api.registry.ResourceKey;

/**
* Public access to the StatusEffects registry.
*/
public final class StatusEffectRegistry {

public static final Registry<StatusEffect> REGISTRY = StatusEffectRegistryImpl.REGISTRY;

/**
* @return the numerical ID assigned to the given effect.
*/
public static int getId(StatusEffect effect) {
return StatusEffectRegistryImpl.getId(effect);
}

/**
* @return the namespaced ID assigned to the given effect.
*/
public static NamespacedIdentifier getIdentifier(StatusEffect effect) {
return StatusEffectRegistryImpl.getIdentifier(effect);
}

/**
* @return the resource key assigned to the given effect.
*/
public static ResourceKey<StatusEffect> getKey(StatusEffect effect) {
return StatusEffectRegistryImpl.getKey(effect);
}

/**
* @return the effect mapped to the given numerical ID.
*/
public static StatusEffect getStatusEffect(int id) {
return StatusEffectRegistryImpl.getStatusEffect(id);
}

/**
* @return the effect mapped to the given namespaced ID.
*/
public static StatusEffect getStatusEffect(NamespacedIdentifier identifier) {
return StatusEffectRegistryImpl.getStatusEffect(identifier);
}

/**
* @return the effect mapped to the given resource key.
*/
public static StatusEffect getStatusEffect(ResourceKey<StatusEffect> key) {
return StatusEffectRegistryImpl.getStatusEffect(key);
}

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

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

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

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

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

import net.minecraft.entity.living.effect.StatusEffect;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.effects.impl.StatusEffectRegistryImpl;
import net.ornithemc.osl.registries.api.registry.Registry;

public interface StatusEffectExtension {

Registry<StatusEffect> REGISTRY = StatusEffectRegistryImpl.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,10 @@
package net.ornithemc.osl.effects.impl;

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

public final class EffectsInit {

public static void init() {
RegistryEvents.INIT_REGISTRIES.register(StatusEffectRegistryImpl::init);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package net.ornithemc.osl.effects.impl;

import java.util.Set;

import net.minecraft.entity.living.effect.StatusEffect;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.core.api.util.NamespacedIdentifiers;
import net.ornithemc.osl.effects.api.StatusEffectEvents;
import net.ornithemc.osl.effects.impl.effect.StatusEffectIdFixer;
import net.ornithemc.osl.registries.api.registry.Registries;
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 StatusEffectRegistryImpl {

public static final Registry<StatusEffect> REGISTRY = Registries.registerSimple(RegistryKeys.STATUS_EFFECT, () -> StatusEffect.BY_ID.getClass());

private static boolean locked = true;

public static int getId(StatusEffect effect) {
return REGISTRY.getId(effect);
}

public static NamespacedIdentifier getIdentifier(StatusEffect effect) {
return REGISTRY.getIdentifier(effect);
}

public static ResourceKey<StatusEffect> getKey(StatusEffect effect) {
return REGISTRY.getKey(effect);
}

public static StatusEffect getStatusEffect(int id) {
return REGISTRY.get(id);
}

public static StatusEffect getStatusEffect(NamespacedIdentifier identifier) {
return REGISTRY.get(identifier);
}

public static StatusEffect getStatusEffect(ResourceKey<StatusEffect> key) {
return REGISTRY.get(key);
}

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

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

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

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

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

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

public static void init() {
SyncedRegistries.register(RegistryKeys.STATUS_EFFECT);
SyncedRegistries.registerFixer(RegistryKeys.STATUS_EFFECT, NamespacedIdentifiers.from("mob_effect/id"), new StatusEffectIdFixer());
}

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

public static void registerStatusEffects() {
VanillaStatusEffects.init();
StatusEffectEvents.REGISTER_STATUS_EFFECTS.invoker().run();
}

public static void registerUnknownStatusEffects() {
for (StatusEffect effect : StatusEffect.BY_ID) {
if (effect != null && REGISTRY.getIdentifier(effect) == null) {
NamespacedIdentifier identifier = NamespacedIdentifiers.from("osl", "mob_effect_" + effect.id);

RegistriesImpl.LOGGER.warn("StatusEffect {}/{}", effect.id, effect.getClass().getSimpleName() + " was not registered to OSL's StatusEffect registry! Adding it as '" + identifier + "'...");
StatusEffectRegistryImpl.register(identifier, effect);
}
}
}
}
Loading
Loading