Skip to content

Commit 250ab85

Browse files
committed
registerBlock
1 parent 739ef0c commit 250ab85

5 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/main/java/survivalblock/atmosphere/atmospheric_api/not_mixin/registrant/ItemRegistrant.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public <S extends Item.Properties> BlockItem register(Block block, S settings) {
4848
}
4949

5050
public <T extends Item, S extends Item.Properties> T register(Block block, Function<S, T> itemFunction, S settings) {
51-
return this.register(block.builtInRegistryHolder().key().location().getPath(), itemFunction, settings);
51+
T item = this.register(block.builtInRegistryHolder().key().location().getPath(), itemFunction, settings);
52+
if (item instanceof BlockItem blockItem) {
53+
blockItem.registerBlocks(Item.BY_BLOCK, blockItem);
54+
}
55+
return item;
5256
}
5357

5458
@Override

src/main/java/survivalblock/atmosphere/atmospheric_api/not_mixin/registrant/delayed/DelayedItemRegistrant.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public <S extends Item.Properties> BlockItem register(Block block, S settings) {
4848
}
4949

5050
public <T extends Item, S extends Item.Properties> T register(Block block, Function<S, T> itemFunction, S settings) {
51-
return this.register(block.builtInRegistryHolder().getRegisteredName(), itemFunction, settings);
51+
T item = this.register(block.builtInRegistryHolder().key().location().getPath(), itemFunction, settings);
52+
if (item instanceof BlockItem blockItem) {
53+
blockItem.registerBlocks(Item.BY_BLOCK, blockItem);
54+
}
55+
return item;
5256
}
5357

5458
@Override

src/main/java/survivalblock/atmosphere/atmospheric_api/not_mixin/registrant/dynamic/DamageTypeRegistrant.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.minecraft.resources.ResourceLocation;
1212
import net.minecraft.world.damagesource.DamageType;
1313

14+
import java.util.Objects;
1415
import java.util.function.Function;
1516

1617
@SuppressWarnings("unused")
@@ -30,4 +31,25 @@ public DamageTypeRegistrant(String modId) {
3031
public DamageTypeRegistrant(Function<String, ResourceLocation> idFunction) {
3132
this(idFunction, Registries.DAMAGE_TYPE);
3233
}
34+
35+
public ResourceKey<DamageType> register(String path, String message) {
36+
return this.register(path, message, 0.1F);
37+
}
38+
39+
public ResourceKey<DamageType> register(String path, String message, float exhaustion) {
40+
return this.register(path, new DamageType(message, exhaustion));
41+
}
42+
43+
@Override
44+
public String getTranslationKey(ResourceKey<DamageType> key) {
45+
return "death.attack." + Objects.requireNonNull(this.registered.get(key)).msgId();
46+
}
47+
48+
public String getTranslationKeyPlayer(ResourceKey<DamageType> key) {
49+
return this.getTranslationKeyWithSuffix(key, "player");
50+
}
51+
52+
public String getTranslationKeyItem(ResourceKey<DamageType> key) {
53+
return this.getTranslationKeyWithSuffix(key, "item");
54+
}
3355
}

src/main/java/survivalblock/atmosphere/atmospheric_api/not_mixin/registrant/dynamic/DynamicRegistrant.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package survivalblock.atmosphere.atmospheric_api.not_mixin.registrant.dynamic;
77

88
import net.fabricmc.fabric.impl.datagen.FabricDataGenHelper;
9+
import net.minecraft.Util;
910
import net.minecraft.core.HolderGetter;
1011
import net.minecraft.core.Registry;
1112
import net.minecraft.core.RegistrySetBuilder;
@@ -16,7 +17,6 @@
1617

1718
import java.util.HashMap;
1819
import java.util.Map;
19-
import java.util.function.Consumer;
2020
import java.util.function.Function;
2121

2222
/**
@@ -29,6 +29,7 @@ public class DynamicRegistrant<T> {
2929
protected final ResourceKey<? extends Registry<T>> registry;
3030

3131
protected final Map<ResourceKey<T>, Function<BootstrapContext<T>, T>> toRegister = new HashMap<>();
32+
protected final Map<ResourceKey<T>, T> registered = new HashMap<>();
3233

3334
public DynamicRegistrant(String modId, ResourceKey<? extends Registry<T>> registry) {
3435
this(path -> ResourceLocation.fromNamespaceAndPath(modId, path), registry);
@@ -67,11 +68,23 @@ protected boolean runningDatagen() {
6768
@SuppressWarnings("unused")
6869
public void bootstrap(BootstrapContext<T> registerable) {
6970
for (Map.Entry<ResourceKey<T>, Function<BootstrapContext<T>, T>> entry : this.toRegister.entrySet()) {
70-
registerable.register(entry.getKey(), entry.getValue().apply(registerable));
71+
ResourceKey<T> key = entry.getKey();
72+
T value = entry.getValue().apply(registerable);
73+
74+
registerable.register(key, value);
75+
this.registered.put(key, value);
7176
}
7277
this.toRegister.clear();
7378
}
7479

80+
public String getTranslationKey(ResourceKey<T> key) {
81+
return Util.makeDescriptionId(key.registry().getPath(), key.location());
82+
}
83+
84+
protected String getTranslationKeyWithSuffix(ResourceKey<T> key, String suffix) {
85+
return this.getTranslationKey(key) + "." + suffix;
86+
}
87+
7588
public interface Creator<T> {
7689
BootstrapContext<T> registerable();
7790

src/main/java/survivalblock/atmosphere/atmospheric_api/not_mixin/registrant/dynamic/EnchantmentRegistrant.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public ResourceKey<Enchantment> register(String path, Consumer<EnchantmentCreato
5454
return key;
5555
}
5656

57+
public String getDescriptionKey(ResourceKey<Enchantment> key) {
58+
return this.getTranslationKeyWithSuffix(key, "desc");
59+
}
60+
5761
@SuppressWarnings("UnusedReturnValue")
5862
public interface EnchantmentCreator extends Creator<Enchantment> {
5963

0 commit comments

Comments
 (0)