Skip to content

Commit

Permalink
Got most things resolved - NeoForge fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
GirafiStudios committed Dec 11, 2023
1 parent 12bba51 commit ecfbb2e
Show file tree
Hide file tree
Showing 18 changed files with 492 additions and 58 deletions.
5 changes: 5 additions & 0 deletions common/src/main/java/com/girafi/waddles/CommonClass.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.girafi.waddles;

import com.girafi.waddles.init.PenguinRegistry;
import com.girafi.waddles.init.WaddlesSounds;

public class CommonClass {

public static void init() {
WaddlesSounds.load();
PenguinRegistry.load();
}
}
26 changes: 15 additions & 11 deletions common/src/main/java/com/girafi/waddles/init/PenguinRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.girafi.waddles.Constants;
import com.girafi.waddles.entity.AdeliePenguinEntity;
import com.girafi.waddles.registration.RegistrationProvider;
import com.girafi.waddles.registration.RegistryObject;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
Expand All @@ -15,23 +18,24 @@

public class PenguinRegistry {
public static final Collection<Supplier<Item>> SPAWN_EGGS = new ArrayList<>();
public static final HashMap<String, Supplier<EntityType<? extends AdeliePenguinEntity>>> PENGUINS = new HashMap<>();
public static final HashMap<Supplier<EntityType<? extends AdeliePenguinEntity>>, Float> PENGUIN_WIDTHS = new HashMap<>();
public static final HashMap<Supplier<EntityType<? extends AdeliePenguinEntity>>, Float> PENGUIN_HEIGHTS = new HashMap<>();
public static final HashMap<Supplier<EntityType<? extends AdeliePenguinEntity>>, Integer> PENGUIN_EGG_PRIMARY = new HashMap<>();
public static final HashMap<Supplier<EntityType<? extends AdeliePenguinEntity>>, Integer> PENGUIN_EGG_SECONDARY = new HashMap<>();
public static final RegistrationProvider<EntityType<?>> ENTITIES = RegistrationProvider.get(BuiltInRegistries.ENTITY_TYPE, Constants.MOD_ID);
public static final HashMap<RegistryObject<EntityType<? extends AdeliePenguinEntity>>, String> PENGUINS = new HashMap<>();
public static final HashMap<RegistryObject<EntityType<? extends AdeliePenguinEntity>>, Integer> PENGUIN_EGG_PRIMARY = new HashMap<>();
public static final HashMap<RegistryObject<EntityType<? extends AdeliePenguinEntity>>, Integer> PENGUIN_EGG_SECONDARY = new HashMap<>();

public static final Supplier<EntityType<? extends AdeliePenguinEntity>> ADELIE_PENGUIN = registerPenguin("adelie_penguin", () -> AdeliePenguinEntity::new, 0.4F, 0.95F, 0x000000, 0xFFFFFF);
public static final RegistryObject<EntityType<? extends AdeliePenguinEntity>> ADELIE_PENGUIN = registerPenguin("adelie_penguin", () -> AdeliePenguinEntity::new, 0.4F, 0.95F, 0x000000, 0xFFFFFF);

private static <T extends AdeliePenguinEntity> Supplier<EntityType<? extends AdeliePenguinEntity>> registerPenguin(String name, Supplier<EntityType.EntityFactory<T>> factory, float width, float height, int eggPrimary, int eggSecondary) {
private static <T extends AdeliePenguinEntity> RegistryObject<EntityType<? extends AdeliePenguinEntity>> registerPenguin(String name, Supplier<EntityType.EntityFactory<T>> factory, float width, float height, int eggPrimary, int eggSecondary) {
ResourceLocation location = new ResourceLocation(Constants.MOD_ID, name);
Supplier<EntityType<? extends AdeliePenguinEntity>> entityType = () -> EntityType.Builder.of(factory.get(), MobCategory.CREATURE).sized(width, height).clientTrackingRange(64).updateInterval(1).build(location.toString());
RegistryObject<EntityType<? extends AdeliePenguinEntity>> entityType = ENTITIES.register(name, () -> EntityType.Builder.of(factory.get(), MobCategory.CREATURE).sized(width, height).clientTrackingRange(64).updateInterval(1).build(location.toString()));

PENGUINS.put(name, entityType);
PENGUIN_WIDTHS.put(entityType, width);
PENGUIN_HEIGHTS.put(entityType, height);
PENGUINS.put(entityType, name);
PENGUIN_EGG_PRIMARY.put(entityType, eggPrimary);
PENGUIN_EGG_SECONDARY.put(entityType, eggSecondary);

return entityType;
}

//Needed to statically initialize fields
public static void load() {}
}
23 changes: 14 additions & 9 deletions common/src/main/java/com/girafi/waddles/init/WaddlesSounds.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.girafi.waddles.init;

import com.girafi.waddles.Constants;
import com.girafi.waddles.registration.RegistrationProvider;
import com.girafi.waddles.registration.RegistryObject;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.EntityType;

import java.util.HashMap;
import java.util.function.Supplier;

public class WaddlesSounds {
public static final HashMap<String, Supplier<SoundEvent>> SOUND_MAP = new HashMap<>();
public static final Supplier<SoundEvent> ADELIE_AMBIENT = createSound("adelie.ambient");
public static final Supplier<SoundEvent> ADELIE_BABY_AMBIENT = createSound("adelie.baby.ambient");
public static final Supplier<SoundEvent> ADELIE_DEATH = createSound("adelie.death");
public static final Supplier<SoundEvent> ADELIE_HURT = createSound("adelie.hurt");
public static final RegistrationProvider<SoundEvent> SOUNDS = RegistrationProvider.get(BuiltInRegistries.SOUND_EVENT, Constants.MOD_ID);
public static final RegistryObject<SoundEvent> ADELIE_AMBIENT = createSound("adelie.ambient");
public static final RegistryObject<SoundEvent> ADELIE_BABY_AMBIENT = createSound("adelie.baby.ambient");
public static final RegistryObject<SoundEvent> ADELIE_DEATH = createSound("adelie.death");
public static final RegistryObject<SoundEvent> ADELIE_HURT = createSound("adelie.hurt");

private static Supplier<SoundEvent> createSound(String name) {
private static RegistryObject<SoundEvent> createSound(String name) {
ResourceLocation resourceLocation = new ResourceLocation(Constants.MOD_ID, name);
Supplier<SoundEvent> soundEvent = () -> SoundEvent.createVariableRangeEvent(resourceLocation);
SOUND_MAP.put(name, soundEvent);
return soundEvent;
return SOUNDS.register(name, () -> SoundEvent.createVariableRangeEvent(resourceLocation));
}

//Needed to statically initialize fields
public static void load() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.girafi.waddles.registration;

import com.girafi.waddles.platform.Services;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

import java.util.Collection;
import java.util.function.Supplier;

/**
* Utility class for multiloader registration.
* <p>
* Example usage:
* <pre>{@code
* public static final RegistrationProvider<Test> PROVIDER = RegistrationProvider.get(Test.REGISTRY, "modid");
* public static final RegistryObject<Test> OBJECT = PROVIDER.register("object", () -> new Test());
*
* // The purpose of this method is to be called in the mod's constructor, in order to assure that the class is loaded, and that objects can be registered.
* public static void loadClass(){}
* }</pre>
*
* @param <T> the type of the objects that this class registers
*/
public interface RegistrationProvider<T> {

/**
* Gets a provider for specified {@code modId} and {@code resourceKey}. <br>
* It is <i>recommended</i> to store the resulted provider in a {@code static final} field to
* the {@link Factory#INSTANCE factory} creating multiple providers for the same resource key and mod id.
*
* @param resourceKey the {@link ResourceKey} of the registry of the provider
* @param modId the mod id that the provider will register objects for
* @param <T> the type of the provider
* @return the provider
*/
static <T> RegistrationProvider<T> get(ResourceKey<? extends Registry<T>> resourceKey, String modId) {
return Factory.INSTANCE.create(resourceKey, modId);
}

/**
* Gets a provider for specified {@code modId} and {@code registry}. <br>
* It is <i>recommended</i> to store the resulted provider in a {@code static final} field to
* the {@link Factory#INSTANCE factory} creating multiple providers for the same resource key and mod id.
*
* @param registry the {@link Registry} of the provider
* @param modId the mod id that the provider will register objects for
* @param <T> the type of the provider
* @return the provider
*/
static <T> RegistrationProvider<T> get(Registry<T> registry, String modId) {
return Factory.INSTANCE.create(registry, modId);
}

/**
* Registers an object.
*
* @param name the name of the object
* @param supplier a supplier of the object to register
* @param <I> the type of the object
* @return a wrapper containing the lazy registered object. <strong>Calling {@link RegistryObject#get() get} too early
* on the wrapper might result in crashes!</strong>
*/
<I extends T> RegistryObject<I> register(String name, Supplier<? extends I> supplier);

/**
* Gets all the objects currently registered.
*
* @return an <strong>immutable</strong> view of all the objects currently registered
*/
Collection<RegistryObject<T>> getEntries();

/**
* Gets the mod id that this provider registers objects for.
*
* @return the mod id
*/
String getModId();

/**
* Factory class for {@link RegistrationProvider registration providers}. <br>
* This class is loaded using {@link java.util.ServiceLoader Service Loaders}, and only one
* should exist per mod loader.
*/
interface Factory {

/**
* The singleton instance of the {@link Factory}. This is different on each loader.
*/
Factory INSTANCE = Services.load(Factory.class);

/**
* Creates a {@link RegistrationProvider}.
*
* @param resourceKey the {@link ResourceKey} of the registry to create this provider for
* @param modId the mod id for which the provider will register objects
* @param <T> the type of the provider
* @return the provider
*/
<T> RegistrationProvider<T> create(ResourceKey<? extends Registry<T>> resourceKey, String modId);

/**
* Creates a {@link RegistrationProvider}.
*
* @param registry the {@link Registry} to create this provider for
* @param modId the mod id for which the provider will register objects
* @param <T> the type of the provider
* @return the provider
*/
default <T> RegistrationProvider<T> create(Registry<T> registry, String modId) {
return create(registry.key(), modId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.girafi.waddles.registration;

import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Supplier;

/**
* Represents a lazy wrapper for registry object.
*
* @param <T> the type of the object
*/
public interface RegistryObject<T> extends Supplier<T> {

/**
* Gets the {@link ResourceKey} of the registry of the object wrapped.
*
* @return the {@link ResourceKey} of the registry
*/
ResourceKey<T> getResourceKey();

/**
* Gets the id of the object.
*
* @return the id of the object
*/
ResourceLocation getId();

/**
* Gets the object behind this wrapper. Calling this method too early
* might result in crashes.
*
* @return the object behind this wrapper
*/
@Override
T get();

/**
* Gets this object wrapped in a vanilla {@link Holder}.
*
* @return the holder
*/
Holder<T> asHolder();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"values": [
"#forge:is_snowy"
"#forge:is_snowy",
{
"id": "#c:snowy",
"required": false
}
]
}
2 changes: 1 addition & 1 deletion fabric/src/main/java/com/girafi/waddles/Waddles.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Waddles implements ModInitializer {

@Override
public void onInitialize() {
ForgeConfigRegistry.INSTANCE.register(Constants.MOD_ID, ModConfig.Type.COMMON, ConfigurationHandler.spec);
CommonClass.init();
ForgeConfigRegistry.INSTANCE.register(Constants.MOD_ID, ModConfig.Type.COMMON, ConfigurationHandler.spec);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.girafi.waddles.platform;

import com.girafi.waddles.registration.RegistrationProvider;
import com.girafi.waddles.registration.RegistryObject;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

public class FabricRegistrationFactory implements RegistrationProvider.Factory {

@Override
public <T> RegistrationProvider<T> create(ResourceKey<? extends Registry<T>> resourceKey, String modId) {
return new Provider<>(modId, resourceKey);
}

@Override
public <T> RegistrationProvider<T> create(Registry<T> registry, String modId) {
return new Provider<>(modId, registry);
}

private static class Provider<T> implements RegistrationProvider<T> {
private final String modId;
private final Registry<T> registry;

private final Set<RegistryObject<T>> entries = new HashSet<>();
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries);

@SuppressWarnings({"unchecked"})
private Provider(String modId, ResourceKey<? extends Registry<T>> key) {
this.modId = modId;

final var reg = BuiltInRegistries.REGISTRY.get(key.location());
if (reg == null) {
throw new RuntimeException("Registry with name " + key.location() + " was not found!");
}
registry = (Registry<T>) reg;
}

private Provider(String modId, Registry<T> registry) {
this.modId = modId;
this.registry = registry;
}

@Override
@SuppressWarnings("unchecked")
public <I extends T> RegistryObject<I> register(String name, Supplier<? extends I> supplier) {
final var rl = new ResourceLocation(modId, name);
final var obj = Registry.register(registry, rl, supplier.get());
final var ro = new RegistryObject<I>() {
final ResourceKey<I> key = ResourceKey.create((ResourceKey<? extends Registry<I>>) registry.key(), rl);

@Override
public ResourceKey<I> getResourceKey() {
return key;
}

@Override
public ResourceLocation getId() {
return rl;
}

@Override
public I get() {
return obj;
}

@Override
public Holder<I> asHolder() {
return (Holder<I>) registry.getHolderOrThrow((ResourceKey<T>) this.key);
}
};
entries.add((RegistryObject<T>) ro);
return ro;
}

@Override
public Collection<RegistryObject<T>> getEntries() {
return entriesView;
}

@Override
public String getModId() {
return modId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.girafi.waddles.platform.FabricRegistrationFactory

0 comments on commit ecfbb2e

Please sign in to comment.