Skip to content

Commit

Permalink
rebase: fix error branch
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 6, 2024
1 parent 963db23 commit 4764499
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "ll/core/LeviLamina.h"
#include "magic_enum.hpp"
#include "mc/util/Random.h"
#include "mc/util/WorldChangeTransaction.h"
#include "mc/world/level/BlockPos.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/block/Block.h"
#include "mc/world/level/levelgen/structure/StructureStart.h"


LL_AUTO_TYPED_INSTANCE_HOOK(
StructureStartHook,
ll::memory::HookPriority::Normal,
Expand All @@ -21,4 +23,29 @@ LL_AUTO_TYPED_INSTANCE_HOOK(
ll::logger.info("StructureStart:");
ll::logger.info("mChunkX={}", this->mChunkX);
ll::logger.info("mChunkZ={}", this->mChunkZ);
unhook();
}

LL_AUTO_TYPED_INSTANCE_HOOK(
WorldChangeTransactionHook,
ll::memory::HookPriority::Normal,
WorldChangeTransaction,
&WorldChangeTransaction::apply,
bool
) {
bool rtv = origin();

ll::logger.info("{}<-calculateBoundingBox", rtv);
ll::logger.info("WorldChangeTransaction:");
for (auto& v : this->mData->mChanges) {
ll::logger.info(
"mData->mChanges[{},{{{},{},{}}}]",
v.first.toString(),
v.second.mUpdateFlags,
v.second.mOldBlock->getName().getString(),
v.second.mNewBlock->getName().getString()
);
}

return rtv;
}
18 changes: 18 additions & 0 deletions src/mc/util/WorldChangeTransaction.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/world/level/BlockPos.h"
#include "mc/world/level/IBlockWorldGenAPI.h"
#include "mc/world/level/block/Block.h"

class WorldChangeTransaction {
public:
struct BlockChange {
int mUpdateFlags;
Block const* mOldBlock;
Block const* mNewBlock;
};

struct Data {
std::unordered_map<BlockPos, BlockChange> mChanges;
};

public:
IBlockWorldGenAPI& mTarget; // this+0x0
std::unique_ptr<Data> mData; // this+0x8

public:
// prevent constructor by default
WorldChangeTransaction& operator=(WorldChangeTransaction const&);
Expand Down
88 changes: 17 additions & 71 deletions src/mc/world/Container.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "mc/_HeaderOutputPredefine.h"
#include "mc/world/ContainerIterator.h"

// auto generated inclusion list
#include "mc/deps/core/data/BidirectionalUnorderedMap.h"
Expand Down Expand Up @@ -32,82 +33,27 @@ class Container {

[[nodiscard]] ItemStack const& operator[](size_t index) const { return this->getItem(static_cast<int>(index)); }


public:
// prevent constructor by default
Container& operator=(Container const&);
Container();

public:
class ConstIterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = ItemStack;
using pointer = ItemStack*;
using reference = ItemStack&;
using iterator_category = std::random_access_iterator_tag;

[[nodiscard]] constexpr ConstIterator(const Container* container, int position)
: mContainer(container),
mPosition(position) {}
[[nodiscard]] constexpr bool operator==(const ConstIterator& other) const {
return mContainer == other.mContainer && mPosition == other.mPosition;
}
[[nodiscard]] constexpr ItemStack const& operator*() const { return mContainer->getItem(mPosition); }
[[nodiscard]] constexpr ItemStack const* operator->() const { return &mContainer->getItem(mPosition); }

constexpr ConstIterator& operator++() {
++mPosition;
return *this;
}
constexpr ConstIterator& operator--() {
--mPosition;
return *this;
}

protected:
const Container* mContainer;
int mPosition;
};

class Iterator : public ConstIterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = ItemStack;
using pointer = ItemStack*;
using reference = ItemStack&;
using iterator_category = std::random_access_iterator_tag;

[[nodiscard]] constexpr Iterator(Container* container, int position) : ConstIterator(container, position) {}
[[nodiscard]] constexpr Container& getContainer() const { return *const_cast<Container*>(mContainer); }
[[nodiscard]] constexpr ItemStack& operator*() const { return getContainer().getItemNonConst(mPosition); }
[[nodiscard]] constexpr ItemStack* operator->() const { return getContainer().getItemNonConst(mPosition); }

constexpr Iterator& operator++() {
++mPosition;
return *this;
}
constexpr Iterator& operator--() {
--mPosition;
return *this;
}
};
using Iterator = ContainerIterator<Container, false>;
using ConstIterator = ContainerIterator<Container, true>;

using ReverseIterator = std::reverse_iterator<Iterator>;
using ConstReverseIterator = std::reverse_iterator<ConstIterator>;

[[nodiscard]] constexpr Iterator begin() noexcept { return {this, 0}; }
[[nodiscard]] constexpr Iterator end() noexcept { return {this, getContainerSize()}; }
[[nodiscard]] constexpr ReverseIterator rbegin() noexcept { return ReverseIterator(end()); }
[[nodiscard]] constexpr ReverseIterator rend() noexcept { return ReverseIterator(begin()); }
[[nodiscard]] constexpr ConstIterator begin() const noexcept { return {this, 0}; }
[[nodiscard]] constexpr ConstIterator end() const noexcept { return {this, getContainerSize()}; }
[[nodiscard]] constexpr ConstIterator cbegin() const noexcept { return {this, 0}; }
[[nodiscard]] constexpr ConstIterator cend() const noexcept { return {this, getContainerSize()}; }
[[nodiscard]] constexpr ConstReverseIterator rbegin() const noexcept { return ConstReverseIterator(end()); }
[[nodiscard]] constexpr ConstReverseIterator rend() const noexcept { return ConstReverseIterator(begin()); }
[[nodiscard]] constexpr ConstReverseIterator crbegin() const noexcept { return ConstReverseIterator(cend()); }
[[nodiscard]] constexpr ConstReverseIterator crend() const noexcept { return ConstReverseIterator(cbegin()); }
[[nodiscard]] constexpr Iterator begin() noexcept { return {this, 0}; }
[[nodiscard]] constexpr ConstIterator cbegin() const noexcept { return {this, 0}; }
[[nodiscard]] constexpr Iterator end() noexcept { return {this, getContainerSize()}; }
[[nodiscard]] constexpr ConstIterator cend() const noexcept { return {this, getContainerSize()}; }

[[nodiscard]] constexpr ReverseIterator rbegin() noexcept { return ReverseIterator{end()}; }
[[nodiscard]] constexpr ConstReverseIterator crbegin() const noexcept { return ConstReverseIterator{cend()}; }
[[nodiscard]] constexpr ReverseIterator rend() noexcept { return ReverseIterator{begin()}; }
[[nodiscard]] constexpr ConstReverseIterator crend() const noexcept { return ConstReverseIterator{cbegin()}; }

public:
// prevent constructor by default
Container& operator=(Container const&);
Container();

public:
// NOLINTBEGIN
Expand Down
56 changes: 56 additions & 0 deletions src/mc/world/ContainerIterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "mc/_HeaderOutputPredefine.h"
#include "mc/world/item/registry/ItemStack.h"


template <typename T, bool IsConst>
class ContainerIterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = std::conditional_t<IsConst, ItemStack const, ItemStack>;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::random_access_iterator_tag;

[[nodiscard]] constexpr ContainerIterator(const T* container, int position)
: mContainer(const_cast<T*>(container)),
mSlot(position) {}

[[nodiscard]] constexpr bool operator==(const ContainerIterator& other) const {
return mSlot == other.mSlot && mContainer == other.mContainer;
}
[[nodiscard]] constexpr std::strong_ordering operator<=>(const ContainerIterator& other) const {
return mSlot <=> other.mSlot;
}

[[nodiscard]] constexpr reference operator*() { return mContainer->getItemNonConst(mSlot); }
[[nodiscard]] constexpr reference operator*() const { return mContainer->getItem(mSlot); }
[[nodiscard]] constexpr pointer operator->() { return mContainer->getItemNonConst(mSlot); }
[[nodiscard]] constexpr pointer operator->() const { return &mContainer->getItem(mSlot); }

constexpr ContainerIterator& operator++() {
++mSlot;
return *this;
}
constexpr ContainerIterator& operator--() {
--mSlot;
return *this;
}

constexpr ContainerIterator operator+(difference_type n) const { return ContainerIterator(mContainer, mSlot + n); }
constexpr ContainerIterator operator-(difference_type n) const { return ContainerIterator(mContainer, mSlot - n); }

constexpr ContainerIterator& operator+=(difference_type n) {
mSlot += n;
return *this;
}
constexpr ContainerIterator& operator-=(difference_type n) {
mSlot -= n;
return *this;
}

[[nodiscard]] constexpr reference operator[](difference_type n) const { return *(*this + n); }

private:
T* mContainer;
int mSlot;
};
3 changes: 1 addition & 2 deletions src/mc/world/actor/player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ bool Player::addAndRefresh(class ItemStack& item) {


optional_ref<EnderChestContainer> Player::getEnderChestContainer() {
std::vector<int> ids;
return ll::memory::dAccess<EnderChestContainer*>(this, 3304);
// ida: Player::Player : EnderChestContainer::EnderChestContainer
}

optional_ref<EnderChestContainer> Player::getEnderChestContainer() const {
optional_ref<EnderChestContainer const> Player::getEnderChestContainer() const {
return ll::memory::dAccess<EnderChestContainer*>(this, 3304);
}
15 changes: 14 additions & 1 deletion src/mc/world/actor/player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,38 @@ class Player : public ::Mob {
*/
LLNDAPI std::string getRealName() const;

/**
* @brief Get the name of the player's langtext.
* @return The name of the player's langtext.
* @warning This is not the player's in-game nickname.
*/
LLNDAPI std::string getLocaleName() const;

LLNDAPI std::optional<NetworkPeer::NetworkStatus> getNetworkStatus() const;

/**
* @brief Disconnect player's client
*/
LLAPI void disconnect(std::string_view reason) const;

/**
* @brief Send a message to player
*/
LLAPI void sendMessage(std::string_view msg) const;

LLAPI void setAbility(::AbilitiesIndex index, bool value);

/**
* @brief Give player item and refresh.
* @return Whether it is a complete success (e.g. returning false if the quantity of items given to the player does not match the expectation).
* @warning The return value does not take into account whether the refresh was successful.
* You can use Player::add, but it will not refresh the item, which may lead to potential issues.
*/
LLAPI bool addAndRefresh(class ItemStack& item);

LLNDAPI optional_ref<EnderChestContainer> getEnderChestContainer();

LLNDAPI optional_ref<EnderChestContainer> getEnderChestContainer() const;
LLNDAPI optional_ref<EnderChestContainer const> getEnderChestContainer() const;

// prevent constructor by default
Player& operator=(Player const&);
Expand Down

0 comments on commit 4764499

Please sign in to comment.