From 3f88795d68c501b3aa29e1509133c67c1902d9c4 Mon Sep 17 00:00:00 2001 From: AnAwesomGuy <79700396+AnAwesomGuy@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:04:30 +0800 Subject: [PATCH 1/5] multitools! --- .../kubejs/item/custom/MultitoolItemJS.java | 179 ++++++++++++++++++ .../fabric/BuiltinKubeJSFabricPlugin.java | 8 + .../forge/BuiltinKubeJSForgePlugin.java | 8 + .../forge/custom/MultitoolItemJSForge.java | 33 ++++ 4 files changed, 228 insertions(+) create mode 100644 common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java create mode 100644 forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java new file mode 100644 index 000000000..79013e205 --- /dev/null +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java @@ -0,0 +1,179 @@ +package dev.latvian.mods.kubejs.item.custom; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import dev.latvian.mods.kubejs.item.MutableToolTier; +import dev.latvian.mods.kubejs.registry.KubeJSRegistries; +import dev.latvian.mods.kubejs.typings.Info; +import dev.latvian.mods.kubejs.typings.Param; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.BlockTags; +import net.minecraft.tags.TagKey; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.ai.attributes.Attribute; +import net.minecraft.world.entity.ai.attributes.AttributeModifier; +import net.minecraft.world.item.DiggerItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.context.UseOnContext; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.Nullable; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static java.math.RoundingMode.CEILING; + +public class MultitoolItemJS extends DiggerItem { + public static class Builder extends HandheldItemBuilder { + protected boolean isAxe = false, isHoe = false, isPickaxe = false, isShovel = false; + protected final List speedValues = new ArrayList<>(5); + protected final List attackValues = new ArrayList<>(5); + protected final Set> mineableTags = new HashSet<>(); + public Builder(ResourceLocation i) { + super(i, 0, 0); + } + @Info(value = """ + Adds a tool to the multi-tool. + + Valid tool types include: 'axe', 'hoe', 'pickaxe', and 'shovel'. + """, params = + @Param(name = "tool", value = "The name of the tool to add to the multi-tool. Will error if it is an unknown tool type.")) + public Builder tool(String tool) { + // maybe swords and shears (after #673 is merged)? + return switch (tool) { + default -> throw new IllegalArgumentException("Unknown tool type '" + tool + + "', valid tool types are: 'axe', 'hoe', 'pickaxe', and 'shovel'!"); + case "axe" -> { + isAxe = true; + yield addValues(6, -3.1f, BlockTags.MINEABLE_WITH_AXE); + } + case "hoe" -> { + isHoe = true; + yield addValues(-2, -1, BlockTags.MINEABLE_WITH_HOE); + } + case "pickaxe" -> { + isPickaxe = true; + yield addValues(1, -2.8f, BlockTags.MINEABLE_WITH_PICKAXE); + } + case "shovel" -> { + isShovel = true; + yield addValues(1.5f, -3, BlockTags.MINEABLE_WITH_SHOVEL); + } + }; + } + protected Builder addValues(float attack, float speed, @Nullable TagKey tag) { + attackValues.add(attack); + speedValues.add(speed); + if (tag != null) + mineableTags.add(tag); + return this; + } + @Override + public Builder attackDamageBaseline(float f) { + attackValues.clear(); + attackValues.add(f); + return this; + } + @Override + public Builder speedBaseline(float f) { + speedValues.clear(); + speedValues.add(f); + return this; + } + protected void setValues() { + // TOD0: find a way to make this better / more efficient + // i hate floating-point jank + BigDecimal attack = new BigDecimal("0"), speed = attack; + for (final float f : attackValues) { + attack = attack.add(new BigDecimal(Float.toString(f))); + } + attack = attack.divide(new BigDecimal(Integer.toString(attackValues.size())), 3, CEILING); + attackDamageBaseline = attack.add(attack.multiply(BigDecimal.valueOf(.08))) + .setScale(1, CEILING).floatValue(); + + for (final float f : speedValues) { + speed = speed.add(new BigDecimal(Float.toString(f))); + } + speed = speed.divide(new BigDecimal(Integer.toString(speedValues.size())), 3, CEILING); + speedBaseline = speed.add(speed.multiply(BigDecimal.valueOf(.05))) + .setScale(1, CEILING).floatValue(); + } + @Override + public Item createObject() { + setValues(); + return new MultitoolItemJS(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); + } + } + public final Set> mineableTags; + protected boolean isAxe, isHoe, isPickaxe, isShovel; + private final Multimap attributes; + private boolean modified = false; + public MultitoolItemJS(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { + super(attack, speed, tier, null, properties); + defaultModifiers = ArrayListMultimap.create(defaultModifiers); + mineableTags = Set.copyOf(builder.mineableTags); + isAxe = builder.isAxe; + isHoe = builder.isHoe; + isPickaxe = builder.isPickaxe; + isShovel = builder.isShovel; + attributes = builder.attributes; + } + public boolean isAxe() { + return isAxe; + } + public boolean isHoe() { + return isHoe; + } + public boolean isPickaxe() { + return isPickaxe; + } + public boolean isShovel() { + return isShovel; + } + @Override + public float getDestroySpeed(ItemStack itemStack, BlockState blockState) { + return isInMineables(blockState) ? speed : 1f; + } + @Override + public Multimap getDefaultAttributeModifiers(EquipmentSlot equipmentSlot) { + if (!modified) { + modified = true; + attributes.forEach((r, m) -> defaultModifiers.put(KubeJSRegistries.attributes().get(r), m)); + } + return super.getDefaultAttributeModifiers(equipmentSlot); + } + @Override + // right-clicking on dirt will till, unless sneaking, then it will turn it into a path + public InteractionResult useOn(UseOnContext ctx) { + return (isAxe && Items.IRON_AXE.useOn(ctx) != InteractionResult.PASS) || + (isShovel && (!isHoe || Optional.ofNullable(ctx.getPlayer()) + .map(Entity::isCrouching).orElse(false)) && // player sneaking check + Items.IRON_SHOVEL.useOn(ctx) != InteractionResult.PASS) || + (isHoe && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) || + (isPickaxe && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) ? + InteractionResult.sidedSuccess(ctx.getLevel().isClientSide) : InteractionResult.PASS; + } + @Override + public boolean isCorrectToolForDrops(BlockState state) { + final int i = tier.getLevel(); + if ((i < 3 && state.is(BlockTags.NEEDS_DIAMOND_TOOL)) || + (i < 2 && state.is(BlockTags.NEEDS_IRON_TOOL)) || + (i < 1 && state.is(BlockTags.NEEDS_STONE_TOOL))) return false; + + return isInMineables(state); + } + private boolean isInMineables(BlockState state) { + for (TagKey tag : mineableTags) + if (state.is(tag)) return true; + return false; + } +} \ No newline at end of file diff --git a/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java b/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java index a470ae28f..60bef9f67 100644 --- a/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java +++ b/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java @@ -1,10 +1,18 @@ package dev.latvian.mods.kubejs.fabric; import dev.latvian.mods.kubejs.BuiltinKubeJSPlugin; +import dev.latvian.mods.kubejs.item.custom.MultitoolItemJS; +import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.util.ClassFilter; public class BuiltinKubeJSFabricPlugin extends BuiltinKubeJSPlugin { + @Override + public void init() { + super.init(); + + RegistryInfo.ITEM.addType("multitool", MultitoolItemJS.Builder.class, MultitoolItemJS.Builder::new); + } @Override public void registerClasses(ScriptType type, ClassFilter filter) { super.registerClasses(type, filter); diff --git a/forge/src/main/java/dev/latvian/mods/kubejs/forge/BuiltinKubeJSForgePlugin.java b/forge/src/main/java/dev/latvian/mods/kubejs/forge/BuiltinKubeJSForgePlugin.java index 376e026f4..829c76eaf 100644 --- a/forge/src/main/java/dev/latvian/mods/kubejs/forge/BuiltinKubeJSForgePlugin.java +++ b/forge/src/main/java/dev/latvian/mods/kubejs/forge/BuiltinKubeJSForgePlugin.java @@ -3,6 +3,8 @@ import dev.latvian.mods.kubejs.BuiltinKubeJSPlugin; import dev.latvian.mods.kubejs.fluid.FluidStackJS; import dev.latvian.mods.kubejs.integration.forge.jei.JEIEvents; +import dev.latvian.mods.kubejs.item.forge.custom.MultitoolItemJSForge; +import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.BindingsEvent; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.util.ClassFilter; @@ -14,6 +16,12 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; public class BuiltinKubeJSForgePlugin extends BuiltinKubeJSPlugin { + @Override + public void init() { + super.init(); + + RegistryInfo.ITEM.addType("multitool", MultitoolItemJSForge.Builder.class, MultitoolItemJSForge.Builder::new); + } @Override public void registerEvents() { super.registerEvents(); diff --git a/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java new file mode 100644 index 000000000..58d122c20 --- /dev/null +++ b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java @@ -0,0 +1,33 @@ +package dev.latvian.mods.kubejs.item.forge.custom; + +import dev.latvian.mods.kubejs.item.MutableToolTier; +import dev.latvian.mods.kubejs.item.custom.MultitoolItemJS; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.ToolAction; +import net.minecraftforge.common.ToolActions; + +public class MultitoolItemJSForge extends MultitoolItemJS { + public static class Builder extends MultitoolItemJS.Builder { + public Builder(ResourceLocation i) { + super(i); + } + @Override + public Item createObject() { + setValues(); + return new MultitoolItemJSForge(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); + } + } + public MultitoolItemJSForge(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { + super(attack, speed, tier, properties, builder); + } + @Override + public boolean canPerformAction(ItemStack stack, ToolAction toolAction) { + return super.canPerformAction(stack, toolAction) || + ((isAxe && ToolActions.DEFAULT_AXE_ACTIONS.contains(toolAction)) || + (isHoe && ToolActions.DEFAULT_HOE_ACTIONS.contains(toolAction)) || + (isPickaxe && ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(toolAction)) || + (isShovel && ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(toolAction))); + } +} From cd3d11b5e602dfd26e7251119d9f192492ff1ef9 Mon Sep 17 00:00:00 2001 From: AnAwesomGuy <79700396+AnAwesomGuy@users.noreply.github.com> Date: Fri, 4 Aug 2023 13:03:45 +0800 Subject: [PATCH 2/5] cleanup to (hopefully) save max and lat from cleaning up my work 5 times --- .../kubejs/item/custom/MultitoolItemJS.java | 66 ++++++++++++------- .../forge/custom/MultitoolItemJSForge.java | 11 ++-- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java index 79013e205..50c81bf1b 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java @@ -35,12 +35,17 @@ public class MultitoolItemJS extends DiggerItem { public static class Builder extends HandheldItemBuilder { protected boolean isAxe = false, isHoe = false, isPickaxe = false, isShovel = false; + protected final List speedValues = new ArrayList<>(5); + protected final List attackValues = new ArrayList<>(5); + protected final Set> mineableTags = new HashSet<>(); + public Builder(ResourceLocation i) { super(i, 0, 0); } + @Info(value = """ Adds a tool to the multi-tool. @@ -48,7 +53,7 @@ public Builder(ResourceLocation i) { """, params = @Param(name = "tool", value = "The name of the tool to add to the multi-tool. Will error if it is an unknown tool type.")) public Builder tool(String tool) { - // maybe swords and shears (after #673 is merged)? + // maybe swords and shears? (i would need some help with that, as neither extends diggeritem) return switch (tool) { default -> throw new IllegalArgumentException("Unknown tool type '" + tool + "', valid tool types are: 'axe', 'hoe', 'pickaxe', and 'shovel'!"); @@ -70,6 +75,7 @@ public Builder tool(String tool) { } }; } + protected Builder addValues(float attack, float speed, @Nullable TagKey tag) { attackValues.add(attack); speedValues.add(speed); @@ -77,91 +83,104 @@ protected Builder addValues(float attack, float speed, @Nullable TagKey t mineableTags.add(tag); return this; } + @Override public Builder attackDamageBaseline(float f) { attackValues.clear(); attackValues.add(f); return this; } + @Override public Builder speedBaseline(float f) { speedValues.clear(); speedValues.add(f); return this; } + protected void setValues() { // TOD0: find a way to make this better / more efficient // i hate floating-point jank - BigDecimal attack = new BigDecimal("0"), speed = attack; - for (final float f : attackValues) { + var attack = new BigDecimal("0"); + for (final var f : attackValues) { attack = attack.add(new BigDecimal(Float.toString(f))); } attack = attack.divide(new BigDecimal(Integer.toString(attackValues.size())), 3, CEILING); attackDamageBaseline = attack.add(attack.multiply(BigDecimal.valueOf(.08))) .setScale(1, CEILING).floatValue(); - - for (final float f : speedValues) { + var speed = new BigDecimal("0"); + for (final var f : speedValues) { speed = speed.add(new BigDecimal(Float.toString(f))); } speed = speed.divide(new BigDecimal(Integer.toString(speedValues.size())), 3, CEILING); speedBaseline = speed.add(speed.multiply(BigDecimal.valueOf(.05))) .setScale(1, CEILING).floatValue(); } + @Override public Item createObject() { setValues(); return new MultitoolItemJS(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); } } + { + defaultModifiers = ArrayListMultimap.create(defaultModifiers); + } + + public final Builder builder; + public final Set> mineableTags; - protected boolean isAxe, isHoe, isPickaxe, isShovel; - private final Multimap attributes; + private boolean modified = false; + public MultitoolItemJS(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { super(attack, speed, tier, null, properties); - defaultModifiers = ArrayListMultimap.create(defaultModifiers); + this.builder = builder; mineableTags = Set.copyOf(builder.mineableTags); - isAxe = builder.isAxe; - isHoe = builder.isHoe; - isPickaxe = builder.isPickaxe; - isShovel = builder.isShovel; - attributes = builder.attributes; } + public boolean isAxe() { - return isAxe; + return builder.isAxe; } + public boolean isHoe() { - return isHoe; + return builder.isHoe; } + public boolean isPickaxe() { - return isPickaxe; + return builder.isPickaxe; } + public boolean isShovel() { - return isShovel; + return builder.isShovel; } + @Override public float getDestroySpeed(ItemStack itemStack, BlockState blockState) { return isInMineables(blockState) ? speed : 1f; } + @Override public Multimap getDefaultAttributeModifiers(EquipmentSlot equipmentSlot) { if (!modified) { modified = true; - attributes.forEach((r, m) -> defaultModifiers.put(KubeJSRegistries.attributes().get(r), m)); + builder.attributes.forEach((r, m) -> defaultModifiers.put(KubeJSRegistries.attributes().get(r), m)); } return super.getDefaultAttributeModifiers(equipmentSlot); } + @Override // right-clicking on dirt will till, unless sneaking, then it will turn it into a path public InteractionResult useOn(UseOnContext ctx) { - return (isAxe && Items.IRON_AXE.useOn(ctx) != InteractionResult.PASS) || - (isShovel && (!isHoe || Optional.ofNullable(ctx.getPlayer()) + return (isAxe() && Items.IRON_AXE.useOn(ctx) != InteractionResult.PASS) || + (isShovel() && (!isHoe() || Optional.ofNullable(ctx.getPlayer()) .map(Entity::isCrouching).orElse(false)) && // player sneaking check Items.IRON_SHOVEL.useOn(ctx) != InteractionResult.PASS) || - (isHoe && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) || - (isPickaxe && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) ? + (isHoe() && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) || + (isPickaxe() && Items.IRON_HOE.useOn(ctx) != InteractionResult.PASS) ? InteractionResult.sidedSuccess(ctx.getLevel().isClientSide) : InteractionResult.PASS; } + @Override public boolean isCorrectToolForDrops(BlockState state) { final int i = tier.getLevel(); @@ -171,8 +190,9 @@ public boolean isCorrectToolForDrops(BlockState state) { return isInMineables(state); } + private boolean isInMineables(BlockState state) { - for (TagKey tag : mineableTags) + for (final var tag : mineableTags) if (state.is(tag)) return true; return false; } diff --git a/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java index 58d122c20..bd6f1f7fb 100644 --- a/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java +++ b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java @@ -13,21 +13,24 @@ public static class Builder extends MultitoolItemJS.Builder { public Builder(ResourceLocation i) { super(i); } + @Override public Item createObject() { setValues(); return new MultitoolItemJSForge(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); } } + public MultitoolItemJSForge(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { super(attack, speed, tier, properties, builder); } + @Override public boolean canPerformAction(ItemStack stack, ToolAction toolAction) { return super.canPerformAction(stack, toolAction) || - ((isAxe && ToolActions.DEFAULT_AXE_ACTIONS.contains(toolAction)) || - (isHoe && ToolActions.DEFAULT_HOE_ACTIONS.contains(toolAction)) || - (isPickaxe && ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(toolAction)) || - (isShovel && ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(toolAction))); + ((isAxe() && ToolActions.DEFAULT_AXE_ACTIONS.contains(toolAction)) || + (isHoe() && ToolActions.DEFAULT_HOE_ACTIONS.contains(toolAction)) || + (isPickaxe() && ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(toolAction)) || + (isShovel() && ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(toolAction))); } } From 7e73daa8b242879384178d78a18d5a9c1be0b924 Mon Sep 17 00:00:00 2001 From: AnAwesomGuy <79700396+AnAwesomGuy@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:22:48 +0800 Subject: [PATCH 3/5] fixed some stuff --- .../kubejs/item/custom/MultitoolItemJS.java | 85 +++++++++---------- .../forge/custom/MultitoolItemJSForge.java | 12 +++ 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java index 50c81bf1b..3f38ccdb8 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java @@ -1,6 +1,7 @@ package dev.latvian.mods.kubejs.item.custom; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import dev.latvian.mods.kubejs.item.MutableToolTier; import dev.latvian.mods.kubejs.registry.KubeJSRegistries; @@ -25,7 +26,7 @@ import java.math.BigDecimal; import java.util.ArrayList; -import java.util.HashSet; +import java.util.EnumSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -34,13 +35,9 @@ public class MultitoolItemJS extends DiggerItem { public static class Builder extends HandheldItemBuilder { - protected boolean isAxe = false, isHoe = false, isPickaxe = false, isShovel = false; - protected final List speedValues = new ArrayList<>(5); - protected final List attackValues = new ArrayList<>(5); - - protected final Set> mineableTags = new HashSet<>(); + protected final EnumSet toolTypes = EnumSet.noneOf(ToolTypes.class); public Builder(ResourceLocation i) { super(i, 0, 0); @@ -52,35 +49,10 @@ public Builder(ResourceLocation i) { Valid tool types include: 'axe', 'hoe', 'pickaxe', and 'shovel'. """, params = @Param(name = "tool", value = "The name of the tool to add to the multi-tool. Will error if it is an unknown tool type.")) - public Builder tool(String tool) { - // maybe swords and shears? (i would need some help with that, as neither extends diggeritem) - return switch (tool) { - default -> throw new IllegalArgumentException("Unknown tool type '" + tool + - "', valid tool types are: 'axe', 'hoe', 'pickaxe', and 'shovel'!"); - case "axe" -> { - isAxe = true; - yield addValues(6, -3.1f, BlockTags.MINEABLE_WITH_AXE); - } - case "hoe" -> { - isHoe = true; - yield addValues(-2, -1, BlockTags.MINEABLE_WITH_HOE); - } - case "pickaxe" -> { - isPickaxe = true; - yield addValues(1, -2.8f, BlockTags.MINEABLE_WITH_PICKAXE); - } - case "shovel" -> { - isShovel = true; - yield addValues(1.5f, -3, BlockTags.MINEABLE_WITH_SHOVEL); - } - }; - } - - protected Builder addValues(float attack, float speed, @Nullable TagKey tag) { - attackValues.add(attack); - speedValues.add(speed); - if (tag != null) - mineableTags.add(tag); + public Builder tool(ToolTypes tool) { + toolTypes.add(tool); + attackValues.add(tool.attack); + speedValues.add(tool.speed); return this; } @@ -101,6 +73,10 @@ public Builder speedBaseline(float f) { protected void setValues() { // TOD0: find a way to make this better / more efficient // i hate floating-point jank + + // this finds the average of all the floats in attack/speedValues, + // then does average += average * 0.8 or 0.5, and rounds it up to the + // tenths place. the resulting value is the attackDamage/speedBaseline. var attack = new BigDecimal("0"); for (final var f : attackValues) { attack = attack.add(new BigDecimal(Float.toString(f))); @@ -108,6 +84,7 @@ protected void setValues() { attack = attack.divide(new BigDecimal(Integer.toString(attackValues.size())), 3, CEILING); attackDamageBaseline = attack.add(attack.multiply(BigDecimal.valueOf(.08))) .setScale(1, CEILING).floatValue(); + var speed = new BigDecimal("0"); for (final var f : speedValues) { speed = speed.add(new BigDecimal(Float.toString(f))); @@ -123,36 +100,58 @@ public Item createObject() { return new MultitoolItemJS(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); } } + + public enum ToolTypes { + // maybe swords and shears? (i would need some help with that, as neither extends diggeritem) + AXE(6, -3.1F, BlockTags.MINEABLE_WITH_AXE), + HOE(-2, -1, BlockTags.MINEABLE_WITH_HOE), + PICKAXE(1, -2.8F, BlockTags.MINEABLE_WITH_PICKAXE), + SHOVEL(1.5F, -3, BlockTags.MINEABLE_WITH_SHOVEL); + + public final float attack, speed; + @Nullable + public final TagKey tag; + + ToolTypes(float attack, float speed, @Nullable TagKey tag) { + this.attack = attack; + this.speed = speed; + this.tag = tag; + } + } + { defaultModifiers = ArrayListMultimap.create(defaultModifiers); } public final Builder builder; - public final Set> mineableTags; - private boolean modified = false; public MultitoolItemJS(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { super(attack, speed, tier, null, properties); this.builder = builder; - mineableTags = Set.copyOf(builder.mineableTags); + + var mineables = new ImmutableSet.Builder>(); + for (var toolType : builder.toolTypes) + if (toolType.tag != null) + mineables.add(toolType.tag); + mineableTags = mineables.build(); } public boolean isAxe() { - return builder.isAxe; + return builder.toolTypes.contains(ToolTypes.AXE); } public boolean isHoe() { - return builder.isHoe; + return builder.toolTypes.contains(ToolTypes.HOE); } public boolean isPickaxe() { - return builder.isPickaxe; + return builder.toolTypes.contains(ToolTypes.PICKAXE); } public boolean isShovel() { - return builder.isShovel; + return builder.toolTypes.contains(ToolTypes.SHOVEL); } @Override @@ -191,7 +190,7 @@ public boolean isCorrectToolForDrops(BlockState state) { return isInMineables(state); } - private boolean isInMineables(BlockState state) { + protected boolean isInMineables(BlockState state) { for (final var tag : mineableTags) if (state.is(tag)) return true; return false; diff --git a/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java index bd6f1f7fb..cecb52e65 100644 --- a/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java +++ b/forge/src/main/java/dev/latvian/mods/kubejs/item/forge/custom/MultitoolItemJSForge.java @@ -5,6 +5,8 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.common.TierSortingRegistry; import net.minecraftforge.common.ToolAction; import net.minecraftforge.common.ToolActions; @@ -33,4 +35,14 @@ public boolean canPerformAction(ItemStack stack, ToolAction toolAction) { (isPickaxe() && ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(toolAction)) || (isShovel() && ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(toolAction))); } + + @Override + public boolean isCorrectToolForDrops(BlockState state) { + return isInMineables(state) && TierSortingRegistry.isCorrectTierForDrops(tier, state); + } + + @Override + public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) { + return isCorrectToolForDrops(state); + } } From 84e9971df22d9bf5ac959375f0c46c4ed25703ad Mon Sep 17 00:00:00 2001 From: AnAwesomGuy <79700396+AnAwesomGuy@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:13:01 +0800 Subject: [PATCH 4/5] fixed some stuff on fabric --- .../kubejs/item/custom/MultitoolItemJS.java | 8 ++--- .../fabric/BuiltinKubeJSFabricPlugin.java | 4 +-- .../fabric/custom/MultitoolItemJSFabric.java | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 fabric/src/main/java/dev/latvian/mods/kubejs/item/fabric/custom/MultitoolItemJSFabric.java diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java index 3f38ccdb8..301450ff8 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java @@ -183,11 +183,11 @@ public InteractionResult useOn(UseOnContext ctx) { @Override public boolean isCorrectToolForDrops(BlockState state) { final int i = tier.getLevel(); - if ((i < 3 && state.is(BlockTags.NEEDS_DIAMOND_TOOL)) || - (i < 2 && state.is(BlockTags.NEEDS_IRON_TOOL)) || - (i < 1 && state.is(BlockTags.NEEDS_STONE_TOOL))) return false; - return isInMineables(state); + return (i >= 3 || !state.is(BlockTags.NEEDS_DIAMOND_TOOL)) && + (i >= 2 || !state.is(BlockTags.NEEDS_IRON_TOOL)) && + (i >= 1 || !state.is(BlockTags.NEEDS_STONE_TOOL)) && + isInMineables(state); } protected boolean isInMineables(BlockState state) { diff --git a/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java b/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java index 60bef9f67..01df6de9a 100644 --- a/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java +++ b/fabric/src/main/java/dev/latvian/mods/kubejs/fabric/BuiltinKubeJSFabricPlugin.java @@ -1,7 +1,7 @@ package dev.latvian.mods.kubejs.fabric; import dev.latvian.mods.kubejs.BuiltinKubeJSPlugin; -import dev.latvian.mods.kubejs.item.custom.MultitoolItemJS; +import dev.latvian.mods.kubejs.item.fabric.custom.MultitoolItemJSFabric; import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.util.ClassFilter; @@ -11,7 +11,7 @@ public class BuiltinKubeJSFabricPlugin extends BuiltinKubeJSPlugin { public void init() { super.init(); - RegistryInfo.ITEM.addType("multitool", MultitoolItemJS.Builder.class, MultitoolItemJS.Builder::new); + RegistryInfo.ITEM.addType("multitool", MultitoolItemJSFabric.Builder.class, MultitoolItemJSFabric.Builder::new); } @Override public void registerClasses(ScriptType type, ClassFilter filter) { diff --git a/fabric/src/main/java/dev/latvian/mods/kubejs/item/fabric/custom/MultitoolItemJSFabric.java b/fabric/src/main/java/dev/latvian/mods/kubejs/item/fabric/custom/MultitoolItemJSFabric.java new file mode 100644 index 000000000..e29268699 --- /dev/null +++ b/fabric/src/main/java/dev/latvian/mods/kubejs/item/fabric/custom/MultitoolItemJSFabric.java @@ -0,0 +1,30 @@ +package dev.latvian.mods.kubejs.item.fabric.custom; + +import dev.latvian.mods.kubejs.item.MutableToolTier; +import dev.latvian.mods.kubejs.item.custom.MultitoolItemJS; +import net.fabricmc.fabric.api.mininglevel.v1.MiningLevelManager; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.block.state.BlockState; + +public class MultitoolItemJSFabric extends MultitoolItemJS { + public static class Builder extends MultitoolItemJS.Builder { + public Builder(ResourceLocation i) { + super(i); + } + + @Override + public Item createObject() { + setValues(); + return new MultitoolItemJSFabric(attackDamageBaseline, speedBaseline, toolTier, createItemProperties(), this); + } + } + public MultitoolItemJSFabric(float attack, float speed, MutableToolTier tier, Properties properties, Builder builder) { + super(attack, speed, tier, properties, builder); + } + + @Override + public boolean isCorrectToolForDrops(BlockState state) { + return tier.getLevel() >= MiningLevelManager.getRequiredMiningLevel(state) && isInMineables(state); + } +} From 143b8aa5aedb5a5968d6cd412257f730e1b9b8fe Mon Sep 17 00:00:00 2001 From: AnAwesomGuy <79700396+AnAwesomGuy@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:16:26 +0800 Subject: [PATCH 5/5] no more BigDecimals! --- .../kubejs/item/custom/MultitoolItemJS.java | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java index 301450ff8..8a825fd32 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/custom/MultitoolItemJS.java @@ -24,15 +24,12 @@ import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.Nullable; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Optional; import java.util.Set; -import static java.math.RoundingMode.CEILING; - public class MultitoolItemJS extends DiggerItem { public static class Builder extends HandheldItemBuilder { protected final List speedValues = new ArrayList<>(5); @@ -71,27 +68,18 @@ public Builder speedBaseline(float f) { } protected void setValues() { - // TOD0: find a way to make this better / more efficient - // i hate floating-point jank - // this finds the average of all the floats in attack/speedValues, - // then does average += average * 0.8 or 0.5, and rounds it up to the + // then does average += average * 0.08 or 0.05, and rounds it up to the // tenths place. the resulting value is the attackDamage/speedBaseline. - var attack = new BigDecimal("0"); for (final var f : attackValues) { - attack = attack.add(new BigDecimal(Float.toString(f))); + attackDamageBaseline += f; } - attack = attack.divide(new BigDecimal(Integer.toString(attackValues.size())), 3, CEILING); - attackDamageBaseline = attack.add(attack.multiply(BigDecimal.valueOf(.08))) - .setScale(1, CEILING).floatValue(); + attackDamageBaseline = (float)(Math.ceil(attackDamageBaseline * 11.2 / attackValues.size()) / 10); - var speed = new BigDecimal("0"); for (final var f : speedValues) { - speed = speed.add(new BigDecimal(Float.toString(f))); + speedBaseline += f; } - speed = speed.divide(new BigDecimal(Integer.toString(speedValues.size())), 3, CEILING); - speedBaseline = speed.add(speed.multiply(BigDecimal.valueOf(.05))) - .setScale(1, CEILING).floatValue(); + speedBaseline = (float)(Math.ceil(speedBaseline * 11.2 / speedValues.size()) / 10); } @Override