Skip to content

Commit

Permalink
feat: durability support for tool items
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 23, 2024
1 parent 3d3df47 commit f3c8fb4
Show file tree
Hide file tree
Showing 34 changed files with 437 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ default ItemStack getItemInHand() {
return getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand();
}

default void setItemInHand(ItemStack itemStack) {
getContainer(FullContainerType.PLAYER_INVENTORY).setItemInHand(itemStack);
}

default void sendItemInHandUpdate() {
var inv = getContainer(FullContainerType.PLAYER_INVENTORY);
var itemStack = inv.getItemInHand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ItemAttributes {
protected String itemColorName = "";
@Builder.Default
protected int itemColorRGB = 0;
// TODO: 当前此数据代表的不是最大耐久值,怀疑导出器有问题,需要修复
@Builder.Default
protected int maxDamage = 0;
@Builder.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ public interface ItemBaseComponent extends ItemComponent {

int getDurability();

boolean isBroken();

void setDurability(int durability);

default void increaseDurability(int addition) {
setDurability(getDurability() + addition);
}

boolean willDamageItem();

String getCustomName();

void setCustomName(String customName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.item.component.tool;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemAxeComponent extends ItemToolComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.item.component.tool;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemHoeComponent extends ItemToolComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.item.component.tool;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemPickaxeComponent extends ItemToolComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.item.component.tool;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemShovelComponent extends ItemToolComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.item.component.tool;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemSwordComponent extends ItemToolComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.allaymc.api.item.component.tool;

import org.allaymc.api.item.component.ItemComponent;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public interface ItemToolComponent extends ItemComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemDiamondPickaxeStack extends ItemStack {
public interface ItemDiamondPickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemGoldenPickaxeStack extends ItemStack {
public interface ItemGoldenPickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemIronPickaxeStack extends ItemStack {
public interface ItemIronPickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemNetheritePickaxeStack extends ItemStack {
public interface ItemNetheritePickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemStonePickaxeStack extends ItemStack {
public interface ItemStonePickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.pickaxe;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemPickaxeComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemWoodenPickaxeStack extends ItemStack {
public interface ItemWoodenPickaxeStack extends ItemStack, ItemPickaxeComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemDiamondSwordStack extends ItemStack {
public interface ItemDiamondSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemGoldenSwordStack extends ItemStack {
public interface ItemGoldenSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemIronSwordStack extends ItemStack {
public interface ItemIronSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemNetheriteSwordStack extends ItemStack {
public interface ItemNetheriteSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemStoneSwordStack extends ItemStack {
public interface ItemStoneSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.allaymc.api.item.interfaces.sword;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.tool.ItemSwordComponent;

/**
* @author daoge_cmd <br>
* Allay Project <br>
*/
public interface ItemWoodenSwordStack extends ItemStack {
public interface ItemWoodenSwordStack extends ItemStack, ItemSwordComponent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import org.allaymc.api.container.FullContainerType;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.type.EnchantmentUnbreakingType;
import org.allaymc.api.item.init.ItemStackInitInfo;
import org.allaymc.api.world.Dimension;
import org.allaymc.server.item.component.common.ItemBaseComponentImpl;
import org.joml.Vector3fc;
import org.joml.Vector3ic;

import java.util.concurrent.ThreadLocalRandom;

/**
* Allay Project 2024/1/27
*
Expand All @@ -34,6 +37,14 @@ public boolean useItemInAir(EntityPlayer player) {
return true;
}

@Override
public boolean willDamageItem() {
float level = getEnchantmentLevel(EnchantmentUnbreakingType.UNBREAKING_TYPE);
if (level == 0) return true;
float possibility = 0.6f + 0.4f / (level + 1f);
return ThreadLocalRandom.current().nextFloat() <= possibility;
}

protected void equipArmor(EntityPlayer player, ItemStack itemStack) {
var armorType = getArmorType();
var armorContainer = player.getContainer(FullContainerType.ARMOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.allaymc.api.entity.Entity;
import org.allaymc.api.item.component.event.*;
import org.allaymc.api.item.enchantment.SimpleEnchantmentInstance;
import org.allaymc.api.item.enchantment.type.EnchantmentUnbreakingType;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.component.annotation.ComponentIdentifier;
import org.allaymc.api.component.annotation.ComponentedObject;
Expand All @@ -39,6 +40,7 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

import static org.allaymc.api.item.ItemHelper.*;

Expand Down Expand Up @@ -158,6 +160,14 @@ public void setDurability(int durability) {
this.durability = durability;
}

@Override
public boolean willDamageItem() {
float level = getEnchantmentLevel(EnchantmentUnbreakingType.UNBREAKING_TYPE);
if (level == 0) return true;
float possibility = 1f / (level + 1f);
return ThreadLocalRandom.current().nextFloat() <= possibility;
}

@Override
public String getCustomName() {
return customName;
Expand Down Expand Up @@ -386,6 +396,16 @@ public void onAttackEntity(Entity attacker, Entity victim) {
manager.callEvent(new ItemAttackEntityEvent(attacker, victim));
}

@Override
public boolean isBroken() {
var maxDamage = attributeComponent.getItemAttributes().maxDamage();
if (maxDamage == 0) {
// This item does not support durability
return false;
}
return durability >= maxDamage;
}

// 记录那些对工具品质有要求的方块的正确工具集合
private static final EnumMap<VanillaBlockId, VanillaItemId[]> CORRECT_TOOL_SPECIAL_MAP = new EnumMap<>(VanillaBlockId.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.allaymc.server.item.component.tool;

import org.allaymc.api.item.component.tool.ItemAxeComponent;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public class ItemAxeComponentImpl extends ItemBlockBreakingToolComponent implements ItemAxeComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.allaymc.server.item.component.tool;

import org.allaymc.api.component.annotation.Dependency;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.item.component.common.ItemBaseComponent;
import org.allaymc.api.item.component.event.ItemAttackEntityEvent;
import org.allaymc.api.item.component.event.ItemBreakBlockEvent;
import org.allaymc.api.item.component.tool.ItemToolComponent;
import org.cloudburstmc.protocol.bedrock.data.GameType;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public abstract class ItemBlockBreakingToolComponent implements ItemToolComponent {
@Dependency
protected ItemBaseComponent baseComponent;

@EventHandler
protected void onBreakBlock(ItemBreakBlockEvent event) {
var entity = event.getBreaker();
if (entity instanceof EntityPlayer player && player.getGameType() == GameType.CREATIVE) return;
var hardness = event.getBlock().getBlockAttributes().hardness();
if (hardness == 0) return;
if (!baseComponent.willDamageItem()) return;
baseComponent.increaseDurability(1);
}

@EventHandler
protected void onAttackEntity(ItemAttackEntityEvent event) {
if (!baseComponent.willDamageItem()) return;
baseComponent.increaseDurability(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.allaymc.server.item.component.tool;

import org.allaymc.api.item.component.tool.ItemHoeComponent;

/**
* Allay Project 2024/6/23
*
* @author daoge_cmd
*/
public class ItemHoeComponentImpl extends ItemBlockBreakingToolComponent implements ItemHoeComponent {

}
Loading

0 comments on commit f3c8fb4

Please sign in to comment.