Skip to content

Commit

Permalink
created ItemBuilder example
Browse files Browse the repository at this point in the history
  • Loading branch information
UselessBullets committed May 8, 2024
1 parent 16f7429 commit 3c51a3f
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 20 deletions.
10 changes: 1 addition & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ repositories {
}
metadataSources { artifact() }
}
ivy {
url = "https://github.com/Turnip-Labs"
patternLayout {
artifact "[organisation]/releases/download/[revision]/[module]-bta-[revision].jar"
m2compatible = true
}
metadataSources { artifact() }
}
ivy {
url = "https://piston-data.mojang.com"
patternLayout {
Expand Down Expand Up @@ -75,7 +67,7 @@ dependencies {

implementation 'com.google.guava:guava:30.0-jre'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
var log4jVersion = "2.20.0"
var log4jVersion = '2.23.1'
implementation("org.apache.logging.log4j:log4j-core:${log4jVersion}")
implementation("org.apache.logging.log4j:log4j-api:${log4jVersion}")
implementation("org.apache.logging.log4j:log4j-1.2-api:${log4jVersion}")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bta_version=7.2-pre1
loader_version=0.15.6-babric.6-bta

# HalpLibe
halplibe_version=4.0.4
halplibe_version=4.0.5
modmenu_version=2.0.6

# Mod
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/turniplabs/examplemod/CustomItemModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package turniplabs.examplemod;

import net.minecraft.client.render.item.model.ItemModelStandard;
import net.minecraft.core.item.Item;
import net.minecraft.core.item.ItemStack;

import java.awt.*;

public class CustomItemModel extends ItemModelStandard {
public CustomItemModel(Item item, String namespace) {
super(item, namespace);
}
@Override
public int getColor(ItemStack stack) {
// Slowly shift its color as time increases
return Color.HSBtoRGB((System.currentTimeMillis()%10000)/10000f, 1f, 1f);
}
}
65 changes: 55 additions & 10 deletions src/main/java/turniplabs/examplemod/ExampleMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
import net.minecraft.core.item.ItemFood;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.examplemod.item.ItemCarrotStick;
import turniplabs.halplibe.helper.ItemBuilder;
import turniplabs.halplibe.helper.RecipeBuilder;
import turniplabs.halplibe.util.GameStartEntrypoint;
import turniplabs.halplibe.util.RecipeEntrypoint;


public class ExampleMod implements ModInitializer, GameStartEntrypoint {
public class ExampleMod implements ModInitializer, GameStartEntrypoint, RecipeEntrypoint {
public static final String MOD_ID = "examplemod";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static Item bamboo;
public static Item potato;
public static Item carrot;
public static Item potatoBaked;
public static Item carrotStick;
public static Item rgbChestplate;
@Override
public void onInitialize() {
LOGGER.info("ExampleMod initialized.");
Expand All @@ -33,21 +38,61 @@ public void beforeGameStart() {
// is to make your ids user configurable
int startingItemId = 20000;

bamboo = new ItemBuilder(MOD_ID).build(new Item("bamboo", startingItemId++));
potato = new ItemBuilder(MOD_ID).build(new ItemFood("potato", startingItemId++, 2, 10, false, 64));
potatoBaked = new ItemBuilder(MOD_ID).build(new ItemFood("baked.potato", startingItemId++, 6, 24, false, 8));
// A very basic item with the translation key set to "bamboo" which will automatically get "item.<your mod id>." appended to the front turning this
// specific translation key to "item.examplemod.bamboo", you can then set the name and description of the item inside of "/lang/<modid>/en_US.lang"
// in this example we set the name to "Bamboo" by setting "item.examplemod.bamboo.name=Bamboo" in the lang file
// By default the texture for the item uses its given key and our modId in this case since we set the key to "bamboo" and our modId is "examplemod"
// it uses the texture "/assets/examplemod/textures/item/bamboo.png" for its icon
bamboo = new ItemBuilder(MOD_ID)
.build(new Item("bamboo", startingItemId++));

// A custom items using the ItemFood food class making it edible, we can change the input parameters in order to change how much it heals
potato = new ItemBuilder(MOD_ID)
.build(new ItemFood("potato", startingItemId++, 2, 72, false, 64));
carrot = new ItemBuilder(MOD_ID)
.build(new ItemFood("carrot", startingItemId++, 2, 40, false, 64));
potatoBaked = new ItemBuilder(MOD_ID)
.build(new ItemFood("baked.potato", startingItemId++, 6, 30, false, 8));

// A custom item with custom properties
carrotStick = new ItemBuilder(MOD_ID)
.setItemModel(item -> {
ItemModelStandard model = new ItemModelStandard(item, null);
model.icon = TextureRegistry.getTexture("examplemod:item/carrot_on_a_stick");
return model;
})
.build(new Item("stick.carrot", startingItemId++));
.setItemModel(item -> new ItemModelStandard(item, null).setRotateWhenRendering().setFull3D()) // This sets the item to render likes tools do, where its rotated in the player's hand
.setIcon("examplemod:item/carrot_on_a_stick") // This sets a specific texture to use for the icon of them item, in this case it loads "/assets/examplemod/textures/item/carrot_on_a_stick.png"
.setStackSize(1) // Sets the max stack size to be 1
.build(new ItemCarrotStick("stick.carrot", startingItemId++)); // We assign our custom item class to it to give it custom behavior

// A custom item with a custom ItemModel
rgbChestplate = new ItemBuilder(MOD_ID)
.setItemModel(item -> new CustomItemModel(item, null)) // We set a custom item model to the item, this model just changes the item's color through time
.setIcon("examplemod:item/leather_chestplate")
.build(new Item("rgb.chest", startingItemId++));
}

@Override
public void afterGameStart() {

}

@Override
public void onRecipesReady() {
// For more on adding recipes see the dedicated RecipeBuilder example mod here: https://github.com/Turnip-Labs/halplibe-examples-repo/tree/recipebuilder

// Make the carrot on a stick craftable by crafting a carrot and fishing rod
RecipeBuilder.Shapeless(MOD_ID)
.addInput(Item.toolFishingrod)
.addInput(carrot)
.create("carrot_on_a_stick", carrotStick.getDefaultStack());

// Recipe to turn two bamboo into a stick
RecipeBuilder.Shapeless(MOD_ID)
.addInput(bamboo)
.addInput(bamboo)
.create("bamboo_to_stick", Item.stick.getDefaultStack());

}

@Override
public void initNamespaces() {
RecipeBuilder.initNameSpace(MOD_ID);
}
}
29 changes: 29 additions & 0 deletions src/main/java/turniplabs/examplemod/item/ItemCarrotStick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package turniplabs.examplemod.item;

import net.minecraft.core.entity.Entity;
import net.minecraft.core.entity.animal.EntityPig;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.Item;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.util.phys.Vec3d;
import net.minecraft.core.world.World;

public class ItemCarrotStick extends Item {
public ItemCarrotStick(String name, int id) {
super(name, id);
}
@Override
public void inventoryTick(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
if (!(entity instanceof EntityPlayer)) return;
if (((EntityPlayer) entity).getHeldItem() == null) return;
if (((EntityPlayer) entity).getHeldItem().getItem() != this) return;
if (!(entity.vehicle instanceof EntityPig)) return;
EntityPig pig = (EntityPig) entity.vehicle;
Vec3d looking = entity.getLookAngle();
int x = (int) (entity.x + looking.xCoord * 5);
int y = (int) (entity.y + looking.yCoord * 5);
int z = (int) (entity.z + looking.zCoord * 5);
pig.setPathToEntity(world.getEntityPathToXYZ(pig, x, y, z, 20));
pig.yRot = entity.yRot;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
],
"beforeGameStart": [
"turniplabs.examplemod.ExampleMod"
],
"recipesReady": [
"turniplabs.examplemod.ExampleMod"
]
},
"mixins": [
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/lang/examplemod/en_US.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
item.examplemod.bamboo.name=Bamboo
item.examplemod.bamboo.desc=Its bamboo, half stick half green.
item.examplemod.potato.name=Potato
item.examplemod.potato.desc=Edible... but just barely.
item.examplemod.carrot.name=Carrot
item.examplemod.carrot.desc=Orange.
item.examplemod.baked.potato.name=Baked Potato
item.examplemod.baked.potato.desc=Ball of heated starch.
item.examplemod.stick.carrot.name=Carrot on a Stick
item.examplemod.stick.carrot.desc=Pigs may be interested in this.
item.examplemod.rgb.chest.name=RGB Chestplate
item.examplemod.rgb.chest.desc=Made by gamers for gamers

0 comments on commit 3c51a3f

Please sign in to comment.