mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-04-24 00:23:44 +00:00
111 lines
3.9 KiB
C++
111 lines
3.9 KiB
C++
#include "../../Platform/stdafx.h"
|
|
#include "../../Headers/net.minecraft.world.entity.ai.attributes.h"
|
|
#include "../../Util/BasicTypeContainers.h"
|
|
#include "SharedMonsterAttributes.h"
|
|
|
|
Attribute* SharedMonsterAttributes::MAX_HEALTH =
|
|
(new RangedAttribute(eAttributeId_GENERIC_MAXHEALTH, 20, 0,
|
|
Double::MAX_VALUE))
|
|
->setSyncable(true);
|
|
Attribute* SharedMonsterAttributes::FOLLOW_RANGE =
|
|
(new RangedAttribute(eAttributeId_GENERIC_FOLLOWRANGE, 32, 0, 2048));
|
|
Attribute* SharedMonsterAttributes::KNOCKBACK_RESISTANCE =
|
|
(new RangedAttribute(eAttributeId_GENERIC_KNOCKBACKRESISTANCE, 0, 0, 1));
|
|
Attribute* SharedMonsterAttributes::MOVEMENT_SPEED =
|
|
(new RangedAttribute(eAttributeId_GENERIC_MOVEMENTSPEED, 0.7f, 0,
|
|
Double::MAX_VALUE))
|
|
->setSyncable(true);
|
|
Attribute* SharedMonsterAttributes::ATTACK_DAMAGE = new RangedAttribute(
|
|
eAttributeId_GENERIC_ATTACKDAMAGE, 2, 0, Double::MAX_VALUE);
|
|
|
|
ListTag<CompoundTag>* SharedMonsterAttributes::saveAttributes(
|
|
BaseAttributeMap* attributes) {
|
|
ListTag<CompoundTag>* list = new ListTag<CompoundTag>();
|
|
|
|
std::vector<AttributeInstance*> atts;
|
|
attributes->getAttributes(atts);
|
|
for (AUTO_VAR(it, atts.begin()); it != atts.end(); ++it) {
|
|
AttributeInstance* attribute = *it;
|
|
list->add(saveAttribute(attribute));
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
CompoundTag* SharedMonsterAttributes::saveAttribute(
|
|
AttributeInstance* instance) {
|
|
CompoundTag* tag = new CompoundTag();
|
|
Attribute* attribute = instance->getAttribute();
|
|
|
|
tag->putInt(L"ID", attribute->getId());
|
|
tag->putDouble(L"Base", instance->getBaseValue());
|
|
|
|
std::unordered_set<AttributeModifier*> modifiers;
|
|
instance->getModifiers(modifiers);
|
|
|
|
if (!modifiers.empty()) {
|
|
ListTag<CompoundTag>* list = new ListTag<CompoundTag>();
|
|
|
|
for (AUTO_VAR(it, modifiers.begin()); it != modifiers.end(); ++it) {
|
|
AttributeModifier* modifier = *it;
|
|
if (modifier->isSerializable()) {
|
|
list->add(saveAttributeModifier(modifier));
|
|
}
|
|
}
|
|
|
|
tag->put(L"Modifiers", list);
|
|
}
|
|
|
|
return tag;
|
|
}
|
|
|
|
CompoundTag* SharedMonsterAttributes::saveAttributeModifier(
|
|
AttributeModifier* modifier) {
|
|
CompoundTag* tag = new CompoundTag();
|
|
|
|
tag->putDouble(L"Amount", modifier->getAmount());
|
|
tag->putInt(L"Operation", modifier->getOperation());
|
|
tag->putInt(L"UUID", modifier->getId());
|
|
|
|
return tag;
|
|
}
|
|
|
|
void SharedMonsterAttributes::loadAttributes(BaseAttributeMap* attributes,
|
|
ListTag<CompoundTag>* list) {
|
|
for (int i = 0; i < list->size(); i++) {
|
|
CompoundTag* tag = list->get(i);
|
|
AttributeInstance* instance = attributes->getInstance(
|
|
static_cast<eATTRIBUTE_ID>(tag->getInt(L"ID")));
|
|
|
|
if (instance != NULL) {
|
|
loadAttribute(instance, tag);
|
|
} else {
|
|
app.DebugPrintf("Ignoring unknown attribute '%d'",
|
|
tag->getInt(L"ID"));
|
|
}
|
|
}
|
|
}
|
|
|
|
void SharedMonsterAttributes::loadAttribute(AttributeInstance* instance,
|
|
CompoundTag* tag) {
|
|
instance->setBaseValue(tag->getDouble(L"Base"));
|
|
|
|
if (tag->contains(L"Modifiers")) {
|
|
ListTag<CompoundTag>* list =
|
|
(ListTag<CompoundTag>*)tag->getList(L"Modifiers");
|
|
|
|
for (int i = 0; i < list->size(); i++) {
|
|
AttributeModifier* modifier = loadAttributeModifier(list->get(i));
|
|
AttributeModifier* old = instance->getModifier(modifier->getId());
|
|
if (old != NULL) instance->removeModifier(old);
|
|
instance->addModifier(modifier);
|
|
}
|
|
}
|
|
}
|
|
|
|
AttributeModifier* SharedMonsterAttributes::loadAttributeModifier(
|
|
CompoundTag* tag) {
|
|
eMODIFIER_ID id = (eMODIFIER_ID)tag->getInt(L"UUID");
|
|
return new AttributeModifier(id, tag->getDouble(L"Amount"),
|
|
tag->getInt(L"Operation"));
|
|
} |