Skip to content
LeviMC
English
AutoLightDark
LeviLamina
Loading...
Searching...
No Matches
BlockComponentStorage.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4
5// auto generated inclusion list
6#include "mc/deps/core/utility/typeid_t.h"
7#include "mc/platform/brstd/flat_map.h"
8#include "mc/platform/brstd/flat_set.h"
9
10// auto generated forward declare list
11// clang-format off
24class OnFallOnTrigger;
26class OnPlacedTrigger;
30class OnStepOnTrigger;
67// clang-format on
68
70public:
71 // BlockComponentStorage inner types declare
72 // clang-format off
73 struct ComponentBase;
74 template<typename T0> struct ComponentInstance;
75 // clang-format on
76
77 // BlockComponentStorage inner types define
79 public:
80 // virtual functions
81 // NOLINTBEGIN
82 virtual ~ComponentBase() = default;
83 // NOLINTEND
84 };
85
88 template <typename T0>
89 struct ComponentInstance : ComponentBase {
90 template <class... A>
91 explicit ComponentInstance(A&&... a) : mValue(::std::forward<A>(a)...) {}
92
93 T0 mValue;
94 };
95
96public:
97 // member variables
98 // NOLINTBEGIN
99 ::ll::TypedStorage<
100 8,
101 48,
104 ::std::unique_ptr<::BlockComponentStorage::ComponentBase>,
105 ::std::less<::Bedrock::typeid_t<void>>,
106 ::std::vector<::Bedrock::typeid_t<void>>,
107 ::std::vector<::std::unique_ptr<::BlockComponentStorage::ComponentBase>>>>
108 mComponents;
109 ::ll::TypedStorage<
110 8,
111 24,
114 ::std::less<::Bedrock::typeid_t<void>>,
115 ::std::vector<::Bedrock::typeid_t<void>>>>
116 mStatelessComponents;
117 ::ll::TypedStorage<1, 1, bool> mAllowModifyingComponents;
118 ::ll::TypedStorage<1, 1, bool> mAllowComponentReplacement;
119 ::ll::TypedStorage<1, 1, bool> mAllowTryGetComponentBeforeFinalization;
120 ::ll::TypedStorage<8, 16, ::std::weak_ptr<::BlockComponentEventSubscriber>> mBlockComponentEventSubscriber;
121 // NOLINTEND
122
123public:
124 // member functions
125 // NOLINTBEGIN
126 MCAPI ::BlockComponentStorage::ComponentBase* _findComponentBase(::Bedrock::typeid_t<void> componentIdToFind) const;
127
129 // NOLINTEND
130
131public:
132 template <class T>
133 [[nodiscard]] bool hasComponent() const {
134 // The game also reads mAllowModifyingComponents at the top of this function and discards
135 // the result; that read has no observable effect, so it is omitted here.
136 auto const& components = mComponents.get();
137 return components.find(::Bedrock::type_id<void, T>()) != components.end();
138 }
139
141 template <class T>
142 [[nodiscard]] T* _findComponentTyped() const {
143 auto* componentBase = _findComponentBase(::Bedrock::type_id<void, T>());
144 return componentBase != nullptr ? reinterpret_cast<T*>(&componentBase[1]) : nullptr;
145 }
146
149 template <class TComponent, class... Args>
150 TComponent* _addStatefulComponent(::Bedrock::typeid_t<void> typeId, Args&&... args) {
151 if (auto* existing = _findComponentBase(typeId)) {
152 // The payload sits immediately after ComponentBase's vtable pointer.
153 auto* component = reinterpret_cast<TComponent*>(&existing[1]);
154 if (mAllowComponentReplacement) {
155 *component = TComponent{::std::forward<Args>(args)...};
156 }
157 return component;
158 }
159
160 auto* instance = new ComponentInstance<TComponent>(::std::forward<Args>(args)...);
161 auto* component = &instance->mValue;
162
163 auto const& keys = mComponents->keys();
164 auto const at =
165 ::std::lower_bound(keys.cbegin(), keys.cend(), typeId, ::std::less<::Bedrock::typeid_t<void>>{});
166 mComponents->emplace_hint(at, typeId, ::std::unique_ptr<ComponentBase>{instance});
167
168 // The game locks the event subscriber here and drops the result; refcounting it is all its
169 // inlined body does.
170 (void)mBlockComponentEventSubscriber.lock();
171
172 return component;
173 }
174
175 inline void allowComponentReplacement() { mAllowComponentReplacement = true; }
176
177public:
178 // destructor thunk
179 // NOLINTBEGIN
180 MCNAPI void $dtor();
181 // NOLINTEND
182};
Definition typeid_t.h:36
Definition BlockComponentEventSubscriber.h:10
Definition BlockComponentStorage.h:69
MCAPI ~BlockComponentStorage()
TComponent * _addStatefulComponent(::Bedrock::typeid_t< void > typeId, Args &&... args)
Constructs a component of type TComponent in place and inserts it, or overwrites the existing one whe...
Definition BlockComponentStorage.h:150
T * _findComponentTyped() const
The payload sits immediately after ComponentBase's vtable pointer.
Definition BlockComponentStorage.h:142
Definition BlockConnectionComponent.h:17
Definition BlockCustomComponentsComponent.h:21
Definition BlockDeprecatedR16EventListenerComponent.h:17
Definition BlockEntityFallOnConfigurationComponent.h:5
Definition BlockMultiBlockComponent.h:24
Definition BlockNeighborChangeConfigurationComponent.h:5
Definition BlockRandomOffsetComponent.h:14
Definition BlockRedstoneConsumerComponent.h:16
Definition BlockTickConfigurationComponent.h:5
Definition BlockTransformationComponent.h:9
Definition BlockVibrationPropertiesComponent.h:5
Definition OnFallOnTrigger.h:14
Definition OnInteractTrigger.h:14
Definition OnPlacedTrigger.h:14
Definition OnPlayerDestroyedTrigger.h:15
Definition OnPlayerPlacingTrigger.h:14
Definition OnStepOffTrigger.h:14
Definition OnStepOnTrigger.h:14
Definition flat_map.h:15
Definition flat_set.h:10
Definition BlockAmbientSoundComponent.h:14
Definition BlockBlockEntityComponent.h:5
Definition BlockChestObstructionComponent.h:8
Definition BlockCollisionBoxComponent.h:14
Definition BlockComponentStorage.h:78
Definition BlockComponentStorage.h:89
Definition BlockConnectionRuleComponent.h:10
Definition BlockContainerComponent.h:5
Definition BlockCraftingTableComponent.h:5
Definition BlockDestructibleByExplosionComponent.h:5
Definition BlockDestructibleByMiningComponent.h:8
Definition BlockDestructionParticlesComponent.h:13
Definition BlockDisplayNameComponent.h:5
Definition BlockEmbeddedVisualComponent.h:11
Definition BlockFlammableComponent.h:10
Definition BlockFlowerPottableComponent.h:5
Definition BlockFrictionComponent.h:5
Definition BlockGeometryComponent.h:18
Definition BlockInstrumentComponent.h:8
Definition BlockItemVisualComponent.h:11
Definition BlockLeashableComponent.h:9
Definition BlockLightDampeningComponent.h:8
Definition BlockLightEmissionComponent.h:8
Definition BlockLiquidDetectionComponent.h:9
Definition BlockLootComponent.h:5
Definition BlockMapColorComponent.h:9
Definition BlockMaterialInstancesComponent.h:14
Definition BlockMovableComponent.h:9
Definition BlockPlacementFilterComponent.h:12
Definition BlockPrecipitationInteractionsComponent.h:8
Definition BlockQueuedTickingComponent.h:9
Definition BlockRandomTickingComponent.h:9
Definition BlockRedstoneComponent.h:5
Definition BlockRedstoneProducerComponent.h:19
Definition BlockSelectionBoxComponent.h:8
Definition BlockSoundComponent.h:10
Definition BlockSupportComponent.h:13
Definition BlockVoxelShapesCullingComponent.h:10