use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class VolumeTransformationTest method testTransformationsOfPositions.
@MethodSource("testTransformationsOfPositions")
@ParameterizedTest
void testTransformationsOfPositions(final Vector3i min, final Vector3i max, final Vector3i origin, final Vector3i testForRoundTrip, final int rotationCount, final StubRotations wanted) {
final SpongeArchetypeVolume volume = VolumeTransformationTest.fillVolume(min, max, origin);
final Vector3i size = volume.size();
final Vector3i relativeMin = volume.min();
final Vector3d center = volume.logicalCenter();
ArchetypeVolume intermediary = volume;
for (int i = 0; i < rotationCount; i++) {
intermediary = intermediary.transform(Transformation.builder().origin(center).rotate(wanted).build());
}
Rotation expected = Rotations.NONE.get();
for (int i = 0; i < rotationCount; i++) {
expected = expected.and(wanted);
}
final Transformation expectedTransform = Transformation.builder().origin(center).rotate(expected).build();
final Transformation inverse = expectedTransform.inverse();
final ArchetypeVolume rotated = intermediary;
if (rotationCount > 0) {
final Vector3d preliminaryTransformed = expectedTransform.transformPosition(testForRoundTrip.toDouble());
Vector3i unTransformed = preliminaryTransformed.round().toInt();
for (int i = 0; i < rotationCount; i++) {
unTransformed = ((AbstractReferentArchetypeVolume) rotated).inverseTransform(unTransformed.x(), unTransformed.y(), unTransformed.z());
}
Assertions.assertEquals(testForRoundTrip, unTransformed);
}
for (int x = 0; x < size.x(); x++) {
for (int y = 0; y < size.y(); y++) {
for (int z = 0; z < size.z(); z++) {
final int relativeX = x + relativeMin.x();
final int relativeY = y + relativeMin.y();
final int relativeZ = z + relativeMin.z();
final Vector3d rawRelativePosition = new Vector3d(relativeX, relativeY, relativeZ);
final BlockState untransformedState = volume.block(relativeX, relativeY, relativeZ);
final Vector3i transformedPosition = expectedTransform.transformPosition(rawRelativePosition).toInt();
final BlockState transformedState = rotated.block(transformedPosition.x(), transformedPosition.y(), transformedPosition.z());
Assertions.assertEquals(untransformedState, transformedState, () -> String.format("Block Check Failed!\nOriginal(%d, %d, %d): %s\nTransformed(%d, %d, %d): %s\n", relativeX, relativeY, relativeZ, untransformedState, transformedPosition.x(), transformedPosition.y(), transformedPosition.z(), transformedState));
}
}
}
if (rotationCount < 0) {
return;
}
// At this point, we should have an abstract referent volume at least
rotated.blockStateStream(rotated.min(), rotated.max(), StreamOptions.lazily()).forEach((rotatedRef, type, x, y, z) -> {
final Vector3d transformedPos = new Vector3d(x, y, z);
// We have this offset in the stream, so we have to undo it here.
final Vector3d invertedTransformedPos = inverse.transformPosition(transformedPos.add(VolumePositionTranslators.BLOCK_OFFSET)).sub(VolumePositionTranslators.BLOCK_OFFSET);
final Vector3i invertedBlockPos = invertedTransformedPos.toInt();
final Vector3i expectedPos;
Assertions.assertTrue(type instanceof StubState, () -> String.format("expected state to be a stub state for pos: [%f, %f, %f] but got %s", x, y, z, type));
Assertions.assertNotEquals(((StubState) type).deducedPos, VolumeTransformationTest.INVALID_STUB_POSITION, () -> String.format("expected to have a positioned stub state: [%f, %f, %f] but got %s", x, y, z, type));
expectedPos = ((StubState) type).deducedPos;
Assertions.assertEquals(expectedPos, invertedBlockPos, () -> String.format("expected untransformed position %s for state %s does not match reverse transformed position: %s", expectedPos, type, invertedBlockPos));
final BlockState block = volume.block(expectedPos.x(), expectedPos.y(), expectedPos.z());
Assertions.assertEquals(type, block, () -> String.format("Expected deduced state to be equal from the original target volume but had a mismatch: Original target %s does not match %s", block, type));
});
}
use of org.spongepowered.api.block.BlockState in project Skree by Skelril.
the class GoldRushInstance method randomizeLevers.
private void randomizeLevers() {
if (System.currentTimeMillis() - lastLeverSwitch >= TimeUnit.SECONDS.toMillis(14)) {
for (Location<World> entry : leverBlocks.keySet()) {
BlockState state = entry.getBlock();
entry.getExtent().setBlock(entry.getBlockPosition(), state.withTrait(BooleanTraits.LEVER_POWERED, false).orElse(state), Cause.source(SkreePlugin.container()).build());
leverBlocks.put(entry, !Probability.getChance(3));
}
lastLeverSwitch = System.currentTimeMillis();
randomizeLevers();
} else if (System.currentTimeMillis() - lastLeverSwitch == 0) {
for (Location<World> entry : leverBlocks.keySet()) {
Location<World> targLoc = entry.add(0, -2, 0);
targLoc.getExtent().setBlockType(targLoc.getBlockPosition(), BlockTypes.STONEBRICK, Cause.source(SkreePlugin.container()).build());
}
Task.builder().execute(() -> {
for (Map.Entry<Location<World>, Boolean> entry : leverBlocks.entrySet()) {
Location<World> targLoc = entry.getKey().add(0, -2, 0);
targLoc.getExtent().setBlockType(targLoc.getBlockPosition(), entry.getValue() ? BlockTypes.REDSTONE_BLOCK : BlockTypes.STONEBRICK, Cause.source(SkreePlugin.container()).build());
}
Task.builder().execute(this::randomizeLevers).delayTicks(15).submit(SkreePlugin.inst());
}).delayTicks(15).submit(SkreePlugin.inst());
} else {
for (Location<World> entry : leverBlocks.keySet()) {
Location<World> targLoc = entry.add(0, -2, 0);
targLoc.getExtent().setBlockType(targLoc.getBlockPosition(), BlockTypes.STONEBRICK, Cause.source(SkreePlugin.container()).build());
}
}
}
use of org.spongepowered.api.block.BlockState in project Skree by Skelril.
the class GoldRushInstance method findChestAndKeys.
private void findChestAndKeys() {
keyRoom.forAll((pt) -> {
BlockState block = getRegion().getExtent().getBlock(pt);
if (block.getType() == BlockTypes.CHEST) {
// TODO Sponge port
Optional<TileEntity> optTileEnt = getRegion().getExtent().getTileEntity(pt);
if (optTileEnt.isPresent() && optTileEnt.get() instanceof IInventory) {
((IInventory) optTileEnt.get()).clear();
}
chestBlocks.add(new Location<>(getRegion().getExtent(), pt));
} else if (block.getType() == BlockTypes.WALL_SIGN) {
Optional<org.spongepowered.api.block.tileentity.TileEntity> optTileEnt = getRegion().getExtent().getTileEntity(pt);
if (!optTileEnt.isPresent()) {
return;
}
locks.add(new Location<>(getRegion().getExtent(), pt));
}
});
}
use of org.spongepowered.api.block.BlockState in project Skree by Skelril.
the class JungleRaidInstance method smartStart.
public void smartStart() {
List<Player> ffaList = new ArrayList<>();
List<Player> redList = new ArrayList<>();
List<Player> blueList = new ArrayList<>();
Collection<Player> containedPlayers = getPlayers(PARTICIPANT);
if (containedPlayers.size() <= 1) {
return;
}
for (Player player : containedPlayers) {
BlockState state = player.getLocation().add(0, -1, 0).getBlock();
if (state.getType() != BlockTypes.WOOL) {
return;
}
Optional<?> optColor = state.getTraitValue(EnumTraits.WOOL_COLOR);
if (optColor.isPresent()) {
DyeColor color = (DyeColor) optColor.get();
if (color == DyeColors.RED) {
redList.add(player);
} else if (color == DyeColors.BLUE) {
blueList.add(player);
} else if (color == DyeColors.WHITE) {
ffaList.add(player);
} else {
return;
}
}
}
if (getWinner(ffaList, blueList, redList).isPresent()) {
getPlayerMessageChannel(SPECTATOR).send(Text.of(TextColors.RED, "All players are on one team, the game will not start."));
return;
}
ffaList.stream().forEach(p -> addFFAPlayer(p, classMap.getOrDefault(p, JungleRaidClass.BALANCED)));
redList.stream().forEach(p -> addRedPlayer(p, classMap.getOrDefault(p, JungleRaidClass.BALANCED)));
blueList.stream().forEach(p -> addBluePlayer(p, classMap.getOrDefault(p, JungleRaidClass.BALANCED)));
state = JungleRaidState.INITIALIZE;
startTime = System.currentTimeMillis();
}
use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.
the class TrackingUtil method updateTickBlock.
public static void updateTickBlock(IMixinWorldServer mixinWorld, Block block, BlockPos pos, IBlockState state, Random random) {
final WorldServer minecraftWorld = mixinWorld.asMinecraftWorld();
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(minecraftWorld);
if (ShouldFire.TICK_BLOCK_EVENT) {
BlockSnapshot snapshot = mixinWorld.createSpongeBlockSnapshot(state, state, pos, BlockChangeFlags.NONE);
final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(Sponge.getCauseStackManager().getCurrentCause(), snapshot);
SpongeImpl.postEvent(event);
if (event.isCancelled()) {
return;
}
}
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>(mixinWorld.asSpongeWorld(), pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
Sponge.getCauseStackManager().pushCause(locatable);
IPhaseState<BlockTickContext> phase = ((IMixinBlock) block).requiresBlockCapture() ? TickPhase.Tick.BLOCK : TickPhase.Tick.NO_CAPTURE_BLOCK;
final BlockTickContext phaseContext = phase.createPhaseContext().source(locatable);
checkAndAssignBlockTickConfig(block, minecraftWorld, phaseContext);
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
// We have to associate any notifiers in case of scheduled block updates from other sources
final PhaseData current = phaseTracker.getCurrentPhaseData();
final IPhaseState<?> currentState = current.state;
((IPhaseState) currentState).appendNotifierPreBlockTick(mixinWorld, pos, current.context, phaseContext);
try (PhaseContext<?> context = phaseContext.buildAndSwitch()) {
block.updateTick(minecraftWorld, pos, state, random);
} catch (Exception | NoClassDefFoundError e) {
phaseTracker.printExceptionFromPhase(e, phaseContext);
}
}
}
Aggregations