Skip to content

Commit

Permalink
Implement logic for new vanilla custom item components in custom item…
Browse files Browse the repository at this point in the history
… registry
  • Loading branch information
eclipseisoffline committed May 10, 2024
1 parent 9cd847e commit 97e1beb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public interface CustomItemData {
/**
* Gets the stack size of the item.
*
* Returns 0 if not set. When not set (or 0), takes the Java item stack count when based of a vanilla item, or uses 64 when porting a modded item.
*
* @return the stack size of the item
*/
@NonNegative
Expand All @@ -126,6 +128,8 @@ public interface CustomItemData {
/**
* Gets the max damage of the item.
*
* Returns -1 if not set. When not set (or below 0), takes the Java item max damage when based of a vanilla item, or uses 0 when porting a modded item.
*
* @return the max damage of the item
*/
int maxDamage();
Expand All @@ -134,6 +138,8 @@ public interface CustomItemData {
* Gets the attack damage of the item.
* This is purely visual, and only applied to tools
*
* Returns 0 if not set. When 0, takes the Java item attack damage when based of a vanilla item, or uses 0 when porting a modded item.
*
* @return the attack damage of the item
*/
int attackDamage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public static class Builder implements CustomItemData.Builder {
protected int textureSize = 16;
protected CustomRenderOffsets renderOffsets = null;
protected Set<String> tags = new HashSet<>();
private int stackSize = 64;
private int maxDamage = 0;
private int stackSize = 0;
private int maxDamage = -1;
private int attackDamage = 0;
private String toolType = null;
private String toolTier = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public static NonVanillaItemRegistration registerCustomItem(NonVanillaCustomItem
Set<String> repairMaterials = customItemData.repairMaterials();

Item.Builder itemBuilder = Item.builder()
.stackSize(customItemData.stackSize())
.maxDamage(customItemData.maxDamage());
.stackSize(customItemData.stackSize() == 0 ? 64 : customItemData.stackSize())
.maxDamage(Math.max(customItemData.maxDamage(), 0));
Item item = new Item(customIdentifier, itemBuilder) {
@Override
public boolean isValidRepairItem(Item other) {
Expand Down Expand Up @@ -168,12 +168,24 @@ private static NbtMapBuilder createComponentNbt(CustomItemData customItemData, I
NbtMapBuilder itemProperties = NbtMap.builder();
NbtMapBuilder componentBuilder = NbtMap.builder();

setupBasicItemInfo(javaItem.maxDamage(), javaItem.maxStackSize(), mapping.getToolType() != null || customItemData.displayHandheld(), customItemData, itemProperties, componentBuilder, protocolVersion);
setupBasicItemInfo(customItemData.maxDamage() < 0 ? javaItem.maxDamage() : customItemData.maxDamage(),
customItemData.stackSize() == 0 ? javaItem.maxStackSize() : customItemData.stackSize(),
mapping.getToolType() != null || customItemData.displayHandheld(),
customItemData, itemProperties, componentBuilder, protocolVersion);

boolean canDestroyInCreative = true;
if (mapping.getToolType() != null) { // This is not using the isTool boolean because it is not just a render type here.
canDestroyInCreative = computeToolProperties(mapping.getToolType(), itemProperties, componentBuilder, javaItem.attackDamage());
String toolType = null;
if (mapping.getToolType() != null) {
toolType = mapping.getToolType();
} else if (customItemData.toolType() != null) {
toolType = customItemData.toolType();
}

if (toolType != null) {
canDestroyInCreative = computeToolProperties(toolType, itemProperties, componentBuilder,
customItemData.attackDamage() == 0 ? javaItem.attackDamage() : customItemData.attackDamage());
}

itemProperties.putBoolean("can_destroy_in_creative", canDestroyInCreative);

if (mapping.getArmorType() != null) {
Expand All @@ -184,14 +196,18 @@ private static NbtMapBuilder createComponentNbt(CustomItemData customItemData, I
computeBlockItemProperties(mapping.getBedrockIdentifier(), componentBuilder);
}

if (mapping.isEdible()) {
computeConsumableProperties(itemProperties, componentBuilder, 1, false);
if (mapping.isEdible() || customItemData.isEdible()) {
computeConsumableProperties(itemProperties, componentBuilder, 1, customItemData.canAlwaysEat());
}

if (mapping.isEntityPlacer()) {
computeEntityPlacerProperties(componentBuilder);
}

if (customItemData.isFoil()) {
itemProperties.putBoolean("foil", true);
}

switch (mapping.getBedrockIdentifier()) {
case "minecraft:fire_charge", "minecraft:flint_and_steel" -> computeBlockItemProperties("minecraft:fire", componentBuilder);
case "minecraft:bow", "minecraft:crossbow", "minecraft:trident" -> computeChargeableProperties(itemProperties, componentBuilder, mapping.getBedrockIdentifier(), protocolVersion);
Expand All @@ -217,7 +233,8 @@ private static NbtMapBuilder createComponentNbt(NonVanillaCustomItemData customI
NbtMapBuilder itemProperties = NbtMap.builder();
NbtMapBuilder componentBuilder = NbtMap.builder();

setupBasicItemInfo(customItemData.maxDamage(), customItemData.stackSize(), displayHandheld, customItemData, itemProperties, componentBuilder, protocolVersion);
setupBasicItemInfo(Math.max(customItemData.maxDamage(), 0), customItemData.stackSize() == 0 ? 64 : customItemData.stackSize(),
displayHandheld, customItemData, itemProperties, componentBuilder, protocolVersion);

boolean canDestroyInCreative = true;
if (customItemData.toolType() != null) { // This is not using the isTool boolean because it is not just a render type here.
Expand Down

0 comments on commit 97e1beb

Please sign in to comment.