Wejście do Block-ów
Informacje podstawowe
Bloki są najczęściej identyfikowane i pozyskiwane przez ich Location. Ta lokacja odnosi się do określonych współrzędnych wewnątrz World. W większości przypadków ServerWorld będzie służył jako World.
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.api.ResourceKey;
public ServerLocation getBlockAt(ResourceKey worldKey, int posX, int posY, int posZ) {
ServerWorld world = Sponge.server().worldManager().world(worldKey).get();
ServerLocation blockLocation = world.location(posX, posY, posZ);
return blockLoc;
}
Ostrzeżenie
Należy zauważyć, że powyższy przykład nie sprawdza, czy istnieje świat. world(worldKey).get()
nie powiedzie się jeśli świat o takiej nazwie nie jest załadowany.
Z pomocą takiego obiektu ServerLocation
, możesz posiąść kolejne informacje na temat bloku. Poniższy kod sprawdza, czy wskazany blok jest banerem poprzez sprawdzenie typu bloku.
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.tag.BlockTypeTag;
import org.spongepowered.api.world.server.ServerLocation;
public boolean isBanner(ServerLocation blockLoc) {
Tag<BlockType> bannerTag = BlockTypeTags.BANNERS.get();
BlockType blockType = blockLoc.blockType();
return bannerTag.contains(blockType);
}
Wskazówka
Operacji ==
można użyć zamiast equals()
, kiedy sprawdzamy dwa obkiety BlockType gdyż istnieje ich określona ilość każdego bloku. Aczkolwiek, generalnie rekomendowanie jest używanie equals()
.
Manipulatory Danych w Block-ach
The data of a block is held as a DataManipulator, similar to other parts of the API. This is the container that holds information on components of our block such as the orientation, light level, and so on. Checking the values of these manipulators is easy, you just need to check the direction Key.
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.data.Keys;
public boolean isFacingNorth(ServerLocation blockLoc) {
Optional<Direction> optionalBlockDirection = blockLoc.get(Keys.DIRECTION).get();
if(!optionalBlockDirection.isPresent()){
return false;
}
Direction blockDirection = optionalBlockDirection.get();
if(blockDirection.equals(Direction.NORTH)){
return true;
}
return false;
}
First, we need to know which DataManipulator
Key
we need. We can then pass the get(Key)
method of
ServerLocation
which will return an Optional
. We then have to check if our DataManipulator
actually
exists for our block by checking ifPresent()
. If it exists, then we can use it.
More on DataManipulator
s can be found in the data documentation.
Wskazówka
If a block will never stop supporting a particular DataManipulator
, such as DirectionalData
with stairs,
then there is no need to check for isPresent()
.
Just remove the optional around the DataManipulator
’s value output and fetch the non-optional data
by adding .get()
to the end of the statement. Note, that this will cause a NoSuchElementException
if a block ever stops supporting a particular DataManipulator
.
Statusy Block-ów
A BlockState contains a BlockType, any DataManipulator
s and properties that are applied to
the block, and any StatePropertys for a block. It stores all immutable values for a particular block. One
use of this is getting an DataManipulator.Immutable, as shown below:
import org.spongepowered.api.block.BlockState;
public boolean isWet(ServerLocation blockLoc) {
BlockState sponge = blockLoc.getBlock();
if (!sponge.getType().equals(BlockTypes.SPONGE.get())) {
return false;
}
Optional<Boolean> wetness = sponge.get(Keys.IS_WET);
if (wetness.isPresent()){
return wetness.get();
}
return false;
}
More information on mutable and immutable DataManipulator
s can be found in the data documentation.
Block State Properties
A block state property is a certain value on the current state of a block. A block may or may not contain
properties depending on the type of block. For example, a bed has a BooleanStateProperty called
BED_OCCUPIED
. As a boolean can only have two values, true and false, the BED_OCCUPIED
property can only be
true or false. Checking this value is simple, just call the BlockState#stateProperty(StateProperty<T>)
method. An example of this with a bed is shown below:
import org.spongepowered.api.state.BooleanStateProperties;
public boolean isBlueBedOccupied(ServerLocation blockLoc) {
if(blockLoc.state().type().equals(BlockTypes.BLUE_BED.get())) {
return blockLoc.state().stateProperty(BooleanStateProperties.BLUE_BED_OCCUPIED).get();
}
return false;
}
Ostrzeżenie
If possible, it is recommended to use DataManipulator
s in place of StateProperty
s where possible as
they are only to be meant as a fallback for modded compatibility.