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

Move various settings from NonVanillaCustomItemData to CustomItemData and allow use in JSON mappings #4655

Open
wants to merge 26 commits into
base: api/2.4.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e6c9c51
Move some functionality from non vanilla custom items to vanilla cust…
eclipseisoffline May 10, 2024
9cd847e
Update implementations
eclipseisoffline May 10, 2024
97e1beb
Implement logic for new vanilla custom item components in custom item…
eclipseisoffline May 10, 2024
965d11c
Move armor type to super class and implement protection value and arm…
eclipseisoffline May 10, 2024
72da5a5
Move translation string back to non vanilla and move hat to vanilla
eclipseisoffline May 11, 2024
86518d5
Merge remote-tracking branch 'upstream/master' into customitemapi
eclipseisoffline May 11, 2024
22f5ad1
Add overrides for backwards compatibility (testing needed) and reorde…
eclipseisoffline May 11, 2024
0a7a06a
Add new vanilla custom item properties to JSON reader, needs testing
eclipseisoffline May 11, 2024
d31e94a
Fix edible animation
eclipseisoffline May 11, 2024
e8266ff
Move tool property back to non vanilla custom item
eclipseisoffline May 11, 2024
17c3b78
Merge branch 'master' into customitemapi
eclipseisoffline May 17, 2024
ac4d543
Merge remote-tracking branch 'refs/remotes/upstream/master' into cust…
eclipseisoffline Jun 18, 2024
6819d2b
Update to recent Geyser
eclipseisoffline Jun 18, 2024
52b9921
Merge remote-tracking branch 'origin/customitemapi' into customitemapi
eclipseisoffline Jun 18, 2024
e5f037e
Move tool type and tool tier back to non vanilla items
eclipseisoffline Jul 21, 2024
9bdf05a
Adjust documentation
eclipseisoffline Jul 21, 2024
aa59f40
Whoops
eclipseisoffline Jul 21, 2024
e55eaa7
Simplify documentation of armor type property
eclipseisoffline Jul 21, 2024
7881c46
Merge remote-tracking branch 'refs/remotes/upstream/master' into cust…
eclipseisoffline Jul 23, 2024
7d49af1
Implement stack size and max damage restrictions
eclipseisoffline Aug 1, 2024
30df334
Improve documentation and add more proper checks to stack size and ma…
eclipseisoffline Aug 2, 2024
7394cf5
Improve documentation slightly again
eclipseisoffline Aug 2, 2024
7ebbf53
Improve exception messages for stackSize and maxDamage builder method…
eclipseisoffline Aug 11, 2024
c890f06
Add more annotations and proper validating of attack damage and prote…
eclipseisoffline Aug 13, 2024
db92184
Add validation for armor type
eclipseisoffline Aug 13, 2024
5b864ab
Return -1 if not set for attack damage and protection value and imple…
eclipseisoffline Aug 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public interface CustomItemData {
*
* @return the stack size of the item
*/
@NonNegative
int stackSize();
@NonNegative int stackSize();

/**
* Gets the max damage of the item.
Expand All @@ -143,7 +142,7 @@ public interface CustomItemData {
* Gets the attack damage of the item.
* This is purely visual, and only applied to tools
*
* <p>Returns 0 if not set. When 0, Geyser takes the Java item attack damage when based on a vanilla item, or uses 0 when porting a modded item.</p>
* <p>Returns -1 if not set. When not set, Geyser takes the Java item attack damage when based on a vanilla item, or uses 0 when porting a modded item.</p>
*
* @return the attack damage of the item
*/
Expand All @@ -162,7 +161,9 @@ public interface CustomItemData {
/**
* Gets the armor protection value of the item.
*
* <p>Only has a function when {@link CustomItemData#armorType} is set.</p>
* <p>Only has a function when {@link CustomItemData#armorType} is set, or when the Java item is an armor item (when based on a vanilla item).</p>
*
* <p>Returns -1 if not set. When not set, Geyser takes the Java item protection value when based on a vanilla item, or uses 0 when porting a modded item.</p>
*
* @return the armor protection value of the item
*/
Expand Down Expand Up @@ -231,11 +232,11 @@ interface Builder {

Builder maxDamage(@NonNegative int maxDamage);

Builder attackDamage(int attackDamage);
Builder attackDamage(@NonNegative int attackDamage);

Builder armorType(@Nullable String armorType);

Builder protectionValue(int protectionValue);
Builder protectionValue(@NonNegative int protectionValue);

Builder hat(boolean isHat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ interface Builder extends CustomItemData.Builder {
Builder maxDamage(@NonNegative int maxDamage);

@Override
Builder attackDamage(int attackDamage);
Builder attackDamage(@NonNegative int attackDamage);

@Override
Builder armorType(@Nullable String armorType);

@Override
Builder protectionValue(int protectionValue);
Builder protectionValue(@NonNegative int protectionValue);

@Override
Builder hat(boolean isHat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

package org.geysermc.geyser.item;

import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.item.custom.CustomItemData;
Expand All @@ -41,6 +44,8 @@
@EqualsAndHashCode
@ToString
public class GeyserCustomItemData implements CustomItemData {
private static final List<String> VALID_ARMOR_TYPES = List.of("boots", "leggings", "chestplate", "helmet");

private final String name;
private final CustomItemOptions customItemOptions;
private final String displayName;
Expand Down Expand Up @@ -199,9 +204,9 @@ public static class Builder implements CustomItemData.Builder {
protected Set<String> tags = new HashSet<>();
private int stackSize = 0;
private int maxDamage = -1;
private int attackDamage = 0;
private int attackDamage = -1;
private String armorType = null;
private int protectionValue = 0;
private int protectionValue = -1;
private boolean hat = false;
private boolean foil = false;
private boolean edible = false;
Expand Down Expand Up @@ -274,7 +279,7 @@ public Builder tags(@Nullable Set<String> tags) {
}

@Override
public Builder stackSize(int stackSize) {
public Builder stackSize(@Positive int stackSize) {
if (stackSize < 1) {
throw new IllegalArgumentException("Stack size cannot be below 1 (" + stackSize + " was given)");
} else if (stackSize > 1) {
Expand All @@ -290,7 +295,7 @@ public Builder stackSize(int stackSize) {
}

@Override
public Builder maxDamage(int maxDamage) {
public Builder maxDamage(@NonNegative int maxDamage) {
if (maxDamage < 0) {
throw new IllegalArgumentException("Max damage cannot be below 0 (" + maxDamage + " was given)");
} else if (maxDamage > 0) {
Expand All @@ -306,19 +311,28 @@ public Builder maxDamage(int maxDamage) {
}

@Override
public Builder attackDamage(int attackDamage) {
public Builder attackDamage(@NonNegative int attackDamage) {
if (attackDamage < 0) {
throw new IllegalArgumentException("Protection value cannot be below 0 (" + attackDamage + " was given)");
}
this.attackDamage = attackDamage;
return this;
}

@Override
public Builder armorType(@Nullable String armorType) {
if (!VALID_ARMOR_TYPES.contains(armorType)) {
throw new IllegalArgumentException("Invalid armor type " + armorType + "! Can be \"boots\", \"leggings\", \"chestplate\", or \"helmet\"");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to force it to lowercase?

this.armorType = armorType;
return this;
}

@Override
public Builder protectionValue(int protectionValue) {
public Builder protectionValue(@NonNegative int protectionValue) {
if (protectionValue < 0) {
throw new IllegalArgumentException("Protection value cannot be below 0 (" + protectionValue + " was given)");
}
this.protectionValue = protectionValue;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.item.custom.CustomItemOptions;
Expand Down Expand Up @@ -167,17 +169,17 @@ public Builder tags(@Nullable Set<String> tags) {
}

@Override
public Builder stackSize(int stackSize) {
public Builder stackSize(@Positive int stackSize) {
return (Builder) super.stackSize(stackSize);
}

@Override
public Builder maxDamage(int maxDamage) {
public Builder maxDamage(@NonNegative int maxDamage) {
return (Builder) super.maxDamage(maxDamage);
}

@Override
public Builder attackDamage(int attackDamage) {
public Builder attackDamage(@NonNegative int attackDamage) {
return (Builder) super.attackDamage(attackDamage);
}

Expand All @@ -187,7 +189,7 @@ public Builder armorType(@Nullable String armorType) {
}

@Override
public Builder protectionValue(int protectionValue) {
public Builder protectionValue(@NonNegative int protectionValue) {
return (Builder) super.protectionValue(protectionValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ private static NbtMapBuilder createComponentNbt(CustomItemData customItemData, I
int protectionValue = 0;
if (mapping.getArmorType() != null) {
armorType = mapping.getArmorType();
protectionValue = mapping.getProtectionValue();
protectionValue = customItemData.protectionValue() == -1 ? mapping.getProtectionValue() : customItemData.protectionValue();
} else if (customItemData.armorType() != null) {
armorType = customItemData.armorType();
protectionValue = customItemData.protectionValue();
// Using 0 as fallback here because the Java item doesn't have an armor type - so its protection value would be 0
protectionValue = customItemData.protectionValue() == -1 ? 0 : customItemData.protectionValue();
}

if (armorType != null) {
Expand Down Expand Up @@ -244,13 +245,13 @@ private static NbtMapBuilder createComponentNbt(NonVanillaCustomItemData customI

boolean canDestroyInCreative = true;
if (customItemData.toolType() != null) { // This is not using the isTool boolean because it is not just a render type here.
canDestroyInCreative = computeToolProperties(Objects.requireNonNull(customItemData.toolType()), itemProperties, componentBuilder, customItemData.attackDamage());
canDestroyInCreative = computeToolProperties(Objects.requireNonNull(customItemData.toolType()), itemProperties, componentBuilder, Math.max(0, customItemData.attackDamage()));
}
itemProperties.putBoolean("can_destroy_in_creative", canDestroyInCreative);

String armorType = customItemData.armorType();
if (armorType != null) {
computeArmorProperties(armorType, customItemData.protectionValue(), itemProperties, componentBuilder);
computeArmorProperties(armorType, Math.max(0, customItemData.protectionValue()), itemProperties, componentBuilder);
}

if (customItemData.isEdible()) {
Expand Down