Skip to content

Instantly share code, notes, and snippets.

@mackthehobbit
Last active November 23, 2015 22:07
Show Gist options
  • Save mackthehobbit/bb56d7e8f57f0828bd85 to your computer and use it in GitHub Desktop.
Save mackthehobbit/bb56d7e8f57f0828bd85 to your computer and use it in GitHub Desktop.
Basic idea of a redstone-activated block in 0.13 (definitely works). Won't compile as-is, obviously.
// however you're adding blocks
initBlocks hook {
// create & register a MyBlock instance etc
}
#pragma once
#include <memory>
class BlockSource;
struct BlockPos;
class LevelChunk;
class BaseCircuitComponent;
class ConsumerComponent;
class PoweredBlockComponent;
class ProducerComponent;
class BaseRailTransporter;
#include "../../../CommonTypes.h"
class CircuitSystem {
public:
char filler[88];
CircuitSystem();
void cacheValues();
void checkForAssociatedNewPoweredBlocks(BlockSource&, BlockPos const&);
template <class ComponentT> ComponentT* create(BlockPos const&, BlockSource*, signed char);
void createComponent(BlockPos const&, signed char, std::unique_ptr<BaseCircuitComponent>);
void evaluate(BlockSource*);
void evaluateComponents(BlockSource*, bool);
char getDirection(BlockPos const&);
unsigned char getStrength(BlockPos const&);
bool hasDirectPower(BlockPos const&);
bool hasNeighborComponent(BlockPos const&, int);
bool isAvailableAt(BlockPos const&);
void lockGraph(bool);
void onChunkDiscarded(LevelChunk&);
void refresh(BlockSource*);
void removeComponents(BlockPos const&);
void updateDependencies();
void updateLevelChunk(LevelChunk&);
bool validPowerBlock(FullBlock const&, FullBlock const&, BlockPos const&);
};
#include "MyBlock.h"
// necessary includes eg. Level, BlockSource, BlockPos etc
#include "minecraftpe/world/level/circuit/CircuitSystem.h"
void FloodgateBlock::onRedstoneUpdate(BlockSource& source, const BlockPos& pos, int strength, bool b) {
// react to change in redstone here
// eg.
if(strength) {
source.removeBlock(pos); // remove this block (set to air)
// source.getLevel()->explode ???
// do something else cool
}
// or do anything else really
}
void FloodgateBlock::onPlace(BlockSource& source, const BlockPos& pos) {
Block::onPlace(source, pos);
// also call code to add the circuit component
// needs to be done on world load & on block placement
this->onLoaded(source, pos);
}
void FloodgateBlock::onLoaded(BlockSource& source, const BlockPos& pos) {
// only perform server-side
if(source.getLevel()->isClientSide()) return;
// add component to circuit system
CircuitSystem *system = source.getDimension()->getCircuitSystem();
system->create<ConsumerComponent>(pos, &source, 0);
}
#pragma once
#include "minecraftpe/world/level/block/Block.h"
class MyBlock : public Block {
public:
/* add constructors, other methods etc here */
virtual void onRedstoneUpdate(BlockSource&, const BlockPos&, int, bool);
virtual void onPlace(BlockSource&, const BlockPos&);
virtual void onLoaded(BlockSource&, const BlockPos&);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment