Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.20.4: Creative mode tabs and test classes #22

Merged
merged 1 commit into from
Jun 29, 2024
Merged
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
24 changes: 24 additions & 0 deletions 1.20.4/common/src/main/java/org/TestSystem/TestMod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.TestSystem;

import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import org.TestSystem.world.Blocks.TestBlocks;
import org.TestSystem.world.Items.CreativeModeTabs;
import org.TestSystem.world.Items.TestItems;
import org.Vrglab.Utils.Utils;

import java.util.function.Supplier;

public class TestMod {
public static final String MODID = "vrglabslib";

public static void init(){
CreativeModeTabs.init();
TestItems.init();
TestBlocks.init();
}

public static Item.Settings basicItemSettings() {
return new Item.Settings();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.TestSystem.world.Blocks;

import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos;
import org.Vrglab.Utils.Utils;

public class TestBlockEntity extends BlockEntity {
public TestBlockEntity(BlockPos pos, BlockState state) {
super(Utils.convertToMcSafeType(TestBlocks.TEST_ENTITY_BLOCK_TYPE), pos, state);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.TestSystem.world.Blocks;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import org.TestSystem.TestMod;
import org.Vrglab.Modloader.Registration.Registry;

import java.util.ArrayList;
import java.util.List;

public class TestBlocks {

public static Object TEST_BLOCK = Registry.RegisterBlock("test_block", TestMod.MODID, ()->new Block(AbstractBlock.Settings.create()), ()->TestMod.basicItemSettings());
public static Object TEST_ITEMLESS_BLOCK = Registry.RegisterItemlessBlock("test_itemless_block", TestMod.MODID, ()->new Block(AbstractBlock.Settings.create()));

public static Object TEST_POI_BLOCK = Registry.RegisterBlock("test_poi_block", TestMod.MODID, ()->new Block(AbstractBlock.Settings.create()), ()->TestMod.basicItemSettings());
public static Object TEST_ENTITY_BLOCK = Registry.RegisterBlock("test_entity_block", TestMod.MODID, ()->new TestEntityBlockHolder(AbstractBlock.Settings.create()), ()->TestMod.basicItemSettings());

public static Object TEST_ENTITY_BLOCK_TYPE = Registry.RegisterBlockEntityType("test_entity_block_type", TestMod.MODID, TestBlockEntity::new, TEST_ENTITY_BLOCK);

public static Object[] getAll(){
List<Object> objectList = new ArrayList<>();
objectList.add(TEST_BLOCK);
objectList.add(TEST_POI_BLOCK);
objectList.add(TEST_ENTITY_BLOCK);
return objectList.toArray();
}

public static void init() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.TestSystem.world.Blocks;

import com.mojang.serialization.MapCodec;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

public class TestEntityBlockHolder extends BlockWithEntity implements BlockEntityProvider {

protected TestEntityBlockHolder(Settings settings) {
super(settings);
}

@Override
protected MapCodec<? extends BlockWithEntity> getCodec() {
return null;
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new TestBlockEntity(pos, state);
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.TestSystem.world.Items;

import org.TestSystem.TestMod;
import org.TestSystem.world.Blocks.TestBlocks;
import org.Vrglab.Helpers.CreativeModeTabBuilder;
import org.Vrglab.Modloader.Registration.Registry;

import java.util.ArrayList;

public class CreativeModeTabs {

public static Object TEST_GROUP = Registry.RegisterCreativeModeTab("test_tab", TestMod.MODID,
CreativeModeTabBuilder.create()
.setDisplayText("test_tab")
.setIcon(TestItems.TEST_ITEM)
.setEntries(TestItems.getAll(), TestBlocks.getAll())
.build()
);

public static void init(){
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.TestSystem.world.Items;

import net.minecraft.item.Item;
import org.TestSystem.TestMod;
import org.Vrglab.Modloader.Registration.Registry;

import java.util.ArrayList;
import java.util.List;

public class TestItems {

public static Object TEST_ITEM = Registry.RegisterItem("test_item", TestMod.MODID, ()->new Item(TestMod.basicItemSettings()));


public static Object[] getAll(){
List<Object> objectList = new ArrayList<>();
objectList.add(TEST_ITEM);
return objectList.toArray();
}


public static void init(){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.Vrglab.Helpers;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import org.Vrglab.Utils.Utils;

public class CreativeModeTabBuilder {
private ItemGroup.Builder builder;

public static CreativeModeTabBuilder create(){
var instance = new CreativeModeTabBuilder();
instance.builder = ItemGroup.create(null, -1);
return instance;
}

public CreativeModeTabBuilder setDisplayText(String txt) {
builder.displayName(Text.translatable(txt));
return this;
}

public CreativeModeTabBuilder setIcon(Object item) {
builder.icon(()->new ItemStack((Item)Utils.convertToMcSafeType(item)));
return this;
}

public CreativeModeTabBuilder setEntries(Object[] itementeries, Object[] blockenteries) {
builder.entries(((displayContext, entries1) -> {
for (var data: itementeries) {
entries1.add((Item)Utils.convertToMcSafeType(data));
}
for (var data: blockenteries) {
entries1.add((Block)Utils.convertToMcSafeType(data));
}
}));
return this;
}

public ItemGroup build(){
return this.builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.sound.SoundEvent;
Expand Down Expand Up @@ -154,7 +155,7 @@ public static Object RegisterItem(String name, String Modid, Supplier aNew) {
* @author Arad Bozorgmehr
* @since 1.0.0
*/
public static Object RegisterBlock(String name, String Modid, Supplier aNew, Item.Settings settings) {
public static Object RegisterBlock(String name, String Modid, Supplier aNew, Supplier<Item.Settings> settings) {
return SimpleRegister(RegistryTypes.BLOCK, Modid, name, aNew, settings);
}

Expand Down Expand Up @@ -303,6 +304,20 @@ public static Object RegisterRecipeType(String name, String Modid, RecipeType ty
}


/**
* Register's a new Creative mode tab
*
* @param name The tab name (aka ID)
* @param Modid The Mod Id of the Registerar
* @param tab The instance to the tab
*
* @author Arad Bozorgmehr
* @since 1.0.0-mc1.20.4
*/
public static <T extends ItemGroup> Object RegisterCreativeModeTab(String name, String Modid, T tab) {
return SimpleRegister(RegistryTypes.CREATIVE_MODE_TAB, Modid, name, tab);
}

/**
* Sends data to the modloader for something to be registered for MC
* @param type The type to use for registeration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum RegistryTypes implements IRegistryType {
SCREEN_HANDLER_TYPE(UUID.randomUUID()),
HANDLED_SCREEN(UUID.randomUUID()),
RECIPE_SERIALIZER(UUID.randomUUID()),
RECIPE_TYPE(UUID.randomUUID())
RECIPE_TYPE(UUID.randomUUID()),
CREATIVE_MODE_TAB(UUID.randomUUID())
;


Expand Down
2 changes: 2 additions & 0 deletions 1.20.4/common/src/main/java/org/Vrglab/VrglabsLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.Vrglab;

import org.TestSystem.TestMod;

public final class VrglabsLib {

public static void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Object accept(Object... args) {
@Override
public Object accept(Object... args) {
Block b = net.minecraft.registry.Registry.register(Registries.BLOCK, new Identifier(modid, args[0].toString()), ((Supplier<Block>)args[1]).get());
net.minecraft.registry.Registry.register(Registries.ITEM, new Identifier(modid, args[0].toString()), new BlockItem(b, (Item.Settings) args[2]));
net.minecraft.registry.Registry.register(Registries.ITEM, new Identifier(modid, args[0].toString()), new BlockItem(b, ((Supplier<Item.Settings>) args[2]).get()));
return b;
}
};
Expand Down Expand Up @@ -157,6 +157,14 @@ public Object accept(Object... args) {
}
};

ICallBack ItemGroupRegistryCallBack = new ICallBack() {
@Override
public Object accept(Object... args) {
return net.minecraft.registry.Registry.register(Registries.ITEM_GROUP, new Identifier(modid, args[0].toString()), (ItemGroup)args[1]);
}
};

Registry.initRegistry(ItemGroupRegistryCallBack, RegistryTypes.CREATIVE_MODE_TAB, modid);
Registry.initRegistry(ItemRegistryCallBack, RegistryTypes.ITEM, modid);
Registry.initRegistry(ItemlessBlockRegistryCallBack, RegistryTypes.ITEMLESS_BLOCK, modid);
Registry.initRegistry(BlockRegistryCallBack, RegistryTypes.BLOCK, modid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.*;
Expand Down Expand Up @@ -42,6 +43,7 @@
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.Vrglab.Modloader.CreationHelpers.OreGenFeatCreationHelper;
import org.Vrglab.Modloader.CreationHelpers.PlacementModifierCreationHelper;
import org.Vrglab.Modloader.CreationHelpers.TypeTransformer;
import org.Vrglab.Modloader.Registration.Bootstrapper;
import org.Vrglab.Modloader.Registration.Registry;
import org.Vrglab.Modloader.Types.IScreenHandledCreationFunction;
Expand Down Expand Up @@ -74,6 +76,9 @@ public Object accept(Object... args) {


public static void Create(IEventBus eventBus, String modid) {
DeferredRegister<ItemGroup> ITEM_GROUP_REGISTRY = DeferredRegister.create(Registries.ITEM_GROUP.getKey(), modid);
ITEM_GROUP_REGISTRY.register(eventBus);

DeferredRegister.Items ITEM_REGISTRY = DeferredRegister.createItems(modid);
ITEM_REGISTRY.register(eventBus);

Expand All @@ -92,6 +97,12 @@ public static void Create(IEventBus eventBus, String modid) {
DeferredRegister<RecipeType<?>> RECIPE_TYPE_REGISTRY = DeferredRegister.create(Registries.RECIPE_TYPE.getKey(), modid);
RECIPE_TYPE_REGISTRY.register(eventBus);

TypeTransformer.ObjectToType = new ICallBack() {
@Override
public Object accept(Object... args) {
return ((DeferredHolder)args[0]).get();
}
};

OreGenFeatCreationHelper.ObjectBlockToStateConverted = new ICallBack() {
@Override
Expand All @@ -118,7 +129,7 @@ public Object accept(Object... args) {
@Override
public Object accept(Object... args) {
DeferredHolder<Block, ?> b = BLOCK_REGISTRY.register(args[0].toString(), (Supplier<? extends Block>) args[1]);
ITEM_REGISTRY.register(args[0].toString(), ()->new BlockItem(b.get(), (Item.Settings) args[2]));
ITEM_REGISTRY.register(args[0].toString(), ()->new BlockItem(b.get(), ((Supplier<Item.Settings>) args[2]).get()));
return b;
}
};
Expand Down Expand Up @@ -179,6 +190,14 @@ public Object accept(Object... args) {
}
};

ICallBack CreativeModeTabcallback = new ICallBack() {
@Override
public Object accept(Object... args) {
return ITEM_GROUP_REGISTRY.register(args[0].toString(), ()->(ItemGroup)args[1]);
}
};

Registry.initRegistry(CreativeModeTabcallback, RegistryTypes.CREATIVE_MODE_TAB, modid);
Registry.initRegistry(Itemcallback, RegistryTypes.ITEM, modid);
Registry.initRegistry(ItemlessBlockcallback, RegistryTypes.ITEMLESS_BLOCK, modid);
Registry.initRegistry(Blockcallback, RegistryTypes.BLOCK, modid);
Expand Down