Skip to content

Instantly share code, notes, and snippets.

@gabizou
Last active December 31, 2015 23:53
Show Gist options
  • Save gabizou/b5d66805ea2ef798ec7c to your computer and use it in GitHub Desktop.
Save gabizou/b5d66805ea2ef798ec7c to your computer and use it in GitHub Desktop.
package org.spongepowered.cookbook.plugin;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableSet;
import org.spongepowered.api.registry.AdditionalCatalogRegistryModule;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class FooRegistryModule implements AdditionalCatalogRegistryModule<FooType> {
private final Map<String, FooType> fooTypeMap = new HashMap<>();
@Override
public void registerDefaults() {
this.fooTypeMap.put("foo", FooTypes.FOO);
this.fooTypeMap.put("bar", FooTypes.BAR);
this.fooTypeMap.put("baz", FooTypes.BAZ);
}
@Override
public void registerAdditionalCatalog(FooType extraCatalog) {
checkArgument(!this.fooTypeMap.containsKey(checkNotNull(extraCatalog).getId().toLowerCase()));
this.fooTypeMap.put(extraCatalog.getId().toLowerCase(), extraCatalog);
}
@Override
public Optional<FooType> getById(String id) {
return Optional.ofNullable(this.fooTypeMap.get(checkNotNull(id).toLowerCase()));
}
@Override
public Collection<FooType> getAll() {
return ImmutableSet.copyOf(this.fooTypeMap.values());
}
}
package org.spongepowered.cookbook.plugin;
import static com.google.common.base.Preconditions.checkNotNull;
import org.spongepowered.api.CatalogType;
import org.spongepowered.api.util.annotation.CatalogedBy;
@CatalogedBy(FooTypes.class)
public class FooType implements CatalogType {
private final String id;
private final String name;
public FooType(String id, String name) {
this.id = checkNotNull(id);
this.name = checkNotNull(name);
}
@Override
public String getId() {
return this.id;
}
@Override
public String getName() {
return this.name;
}
}
package org.spongepowered.cookbook.plugin;
public final class FooTypes {
public static final FooType FOO = new FooType("foo", "Foo");
public static final FooType BAR = new FooType("bar", "Bar");
public static final FooType BAZ = new FooType("baz", "Baz");
private FooTypes() {
}
}
package org.spongepowered.cookbook.plugin;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import javax.annotation.Resource;
@Plugin(id = "spongetest", name = "SpongeTest", version = "0.1")
@Resource
public class WorldsTest {
@Listener
public void onPreInit(GamePreInitializationEvent event) {
Sponge.getRegistry().registerModule(FooType.class, new FooRegistryModule());
}
@Listener
public void onInit(GameInitializationEvent event) {
Sponge.getRegistry().register(FooType.class, new FooType("derp", "Derp"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment