Skip to content

Commit 77d5344

Browse files
committed
feat: Add ranged_attack for skeleton
1 parent 63eeb3a commit 77d5344

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/java-entity.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ class Entity::Impl {
631631
E(sheep, C(Animal, AgeableA(u8"minecraft:sheep"), Colorable(u8"sheep"), Definitions({u8"+minecraft:sheep_dyeable", u8"+minecraft:rideable_wooly", u8"+minecraft:loot_wooly"}), Sheep));
632632
E(shulker, C(Monster, Shulker));
633633
M(silverfish);
634-
M(skeleton); // lefty skeleton does not exist in Bedrock?
634+
E(skeleton, C(Monster, RangedAttack)); // lefty skeleton does not exist in Bedrock?
635635

636636
E(skeleton_horse, C(Animal, Definitions({u8"+minecraft:skeleton_horse_adult"}), SkeletonHorse));
637637
E(slime, C(Monster, Slime));
@@ -693,7 +693,7 @@ class Entity::Impl {
693693
E(interaction, Null);
694694

695695
E(armadillo, C(Animal, Definitions({u8"+minecraft:armadillo"}), AgeableG, Armadillo));
696-
E(bogged, C(Monster, Definitions({u8"+minecraft:bogged", u8"+minecraft:ranged_attack"}), Bogged));
696+
E(bogged, C(Monster, Definitions({u8"+minecraft:bogged"}), RangedAttack, Bogged));
697697
E(breeze, C(Monster, Definitions({u8"+minecraft:breeze"})));
698698

699699
E(creaking, C(Monster, Definitions({u8"+minecraft:creaking"}), Creaking));
@@ -1013,7 +1013,7 @@ class Entity::Impl {
10131013
bool melee = true;
10141014
if (auto handItems = tag.listTag(u8"HandItems"); handItems && !handItems->fValue.empty()) {
10151015
if (auto item = handItems->fValue[0]->asCompound(); item) {
1016-
if (Item::Count(*item, 0) > 0 && item->string(u8"id") == u8"minecraft:trident") {
1016+
if (Item::Count(*item, 0) > 0 && Item::IsMeleeWeapon(item->string(u8"id", u8""))) {
10171017
melee = false;
10181018
}
10191019
}
@@ -1444,10 +1444,10 @@ class Entity::Impl {
14441444
}
14451445
if (itemInHand) {
14461446
b.set(u8"ItemInHand", itemInHand);
1447-
auto name = itemInHand->string(u8"Name");
1448-
if (name == u8"minecraft:crossbow") {
1447+
auto name = itemInHand->string(u8"Name", u8"");
1448+
if (Item::IsRangedWeapon(name)) {
14491449
AddDefinition(b, u8"+ranged_unit");
1450-
} else if (name == u8"minecraft:golden_sword") {
1450+
} else if (Item::IsMeleeWeapon(name)) {
14511451
AddDefinition(b, u8"+melee_unit");
14521452
}
14531453
}
@@ -2021,6 +2021,24 @@ class Entity::Impl {
20212021
}
20222022
}
20232023

2024+
static void RangedAttack(CompoundTag &b, CompoundTag const &j, ConverterContext &) {
2025+
auto mainHandB = b.listTag(u8"Mainhand");
2026+
if (!mainHandB) {
2027+
return;
2028+
}
2029+
if (mainHandB->size() < 1) {
2030+
return;
2031+
}
2032+
auto itemB = mainHandB->fValue[0]->asCompound();
2033+
if (!itemB) {
2034+
return;
2035+
}
2036+
auto name = itemB->string(u8"Name", u8"");
2037+
if (Item::IsRangedWeapon(name)) {
2038+
AddDefinition(b, u8"+minecraft:ranged_attack");
2039+
}
2040+
}
2041+
20242042
static void Sittable(CompoundTag &c, CompoundTag const &tag, ConverterContext &) {
20252043
CopyBoolValues(tag, c, {{u8"Sitting"}});
20262044
}

src/java-item.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ class Item::Impl {
7070
return armor;
7171
}
7272

73+
static bool IsRangedWeapon(std::u8string const &name) {
74+
static std::unordered_set<std::u8string> const sWeapons = {u8"bow", u8"crossbow", u8"trident"};
75+
return sWeapons.find(Namespace::Remove(name)) != sWeapons.end();
76+
}
77+
78+
static bool IsMeleeWeapon(std::u8string const &name) {
79+
if (name.ends_with(u8"_sword")) {
80+
return true;
81+
}
82+
if (name.ends_with(u8"_axe")) {
83+
return true;
84+
}
85+
return false;
86+
}
87+
7388
private:
7489
static CompoundTagPtr Convert(CompoundTagPtr const &in, Context &ctx, DataVersion const &dataVersion) {
7590
using namespace std;
@@ -2036,4 +2051,11 @@ CompoundTagPtr Item::Empty() {
20362051
return Impl::Empty();
20372052
}
20382053

2054+
bool Item::IsRangedWeapon(std::u8string const &name) {
2055+
return Impl::IsRangedWeapon(name);
2056+
}
2057+
2058+
bool Item::IsMeleeWeapon(std::u8string const &name) {
2059+
return Impl::IsMeleeWeapon(name);
2060+
}
20392061
} // namespace je2be::java

src/java/_item.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Item {
1919
static CompoundTagPtr From(CompoundTagPtr const &item, Context &ctx, DataVersion const &dataVersion);
2020
static i8 GetSkullTypeFromBlockName(std::u8string_view const &name);
2121
static CompoundTagPtr Empty();
22+
static bool IsRangedWeapon(std::u8string const &name);
23+
static bool IsMeleeWeapon(std::u8string const &name);
2224

2325
static std::optional<i32> Count(CompoundTag const &c) {
2426
if (auto count = c.int32(u8"count"); count) {

0 commit comments

Comments
 (0)