use of org.spongepowered.api.block.BlockSnapshot in project modules-extra by CubeEngine.
the class ChopListener method onChop.
@Listener
public void onChop(final ChangeBlockEvent.Break event, @First Player player) {
if (!player.getItemInHand(HandTypes.MAIN_HAND).isPresent() || event.getCause().getContext().containsKey(EventContextKeys.PLAYER_SIMULATED)) {
return;
}
ItemStack axe = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null);
if (axe == null || axe.getType() != DIAMOND_AXE || axe.get(Keys.ITEM_DURABILITY).orElse(0) <= 0 || !axe.get(Keys.ITEM_ENCHANTMENTS).orElse(emptyList()).contains(Enchantment.builder().type(EnchantmentTypes.PUNCH).level(5).build())) {
return;
}
if (!module.use.check(player)) {
return;
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_SIMULATED, player.getProfile());
for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
BlockType type = transaction.getOriginal().getState().getType();
Location<World> orig = transaction.getOriginal().getLocation().get();
BlockType belowType = orig.getBlockRelative(DOWN).getBlockType();
if (isLog(type) && isSoil(belowType)) {
TreeType treeType = transaction.getOriginal().get(TREE_TYPE).get();
Set<Location<World>> treeBlocks = findTreeBlocks(orig, treeType);
if (treeBlocks.isEmpty()) {
return;
}
int logs = 0;
int leaves = 0;
Set<Location> saplings = new HashSet<>();
for (Location<World> block : treeBlocks) {
if (isLog(block.getBlockType())) {
if (!block.equals(orig)) {
block.getExtent().playSound(SoundTypes.BLOCK_WOOD_STEP, block.getPosition(), 1);
}
logs++;
block.setBlockType(BlockTypes.AIR);
BlockType belowTyp = block.getBlockRelative(DOWN).getBlockType();
if (isSoil(belowTyp)) {
saplings.add(block);
}
}
if (block.getBlockType() == LEAVES || block.getBlockType() == LEAVES2) {
block.setBlockType(BlockTypes.AIR);
// TODO leaves sound?
block.getExtent().playSound(SoundTypes.BLOCK_GRASS_STEP, block.getPosition(), 1);
leaves++;
}
}
ItemStack log = ItemStack.builder().itemType(type.getItem().get()).quantity(logs).build();
log.offer(TREE_TYPE, treeType);
int apples = 0;
if (treeType == JUNGLE) {
leaves = leaves / 40;
} else {
if (treeType == DARK_OAK || treeType == OAK) {
apples = leaves / 200;
}
leaves = leaves / 20;
}
if (leaves == 0) {
leaves = 1;
}
BlockState sapState = BlockTypes.SAPLING.getDefaultState().with(TREE_TYPE, treeType).get();
if (this.module.autoplant.check(player)) {
leaves -= saplings.size();
leaves = Math.max(0, leaves);
transaction.setCustom(sapState.snapshotFor(transaction.getOriginal().getLocation().get()));
saplings.forEach(l -> l.setBlock(sapState));
}
final int uses = axe.get(Keys.ITEM_DURABILITY).get() - logs;
axe.offer(Keys.ITEM_DURABILITY, uses);
player.setItemInHand(HandTypes.MAIN_HAND, axe);
World world = player.getWorld();
Entity itemEntity;
Sponge.getCauseStackManager().removeContext(EventContextKeys.PLAYER_SIMULATED);
Sponge.getCauseStackManager().pushCause(player);
if (apples > 0) {
ItemStack apple = ItemStack.builder().itemType(APPLE).quantity(apples).build();
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, apple.createSnapshot());
world.spawnEntity(itemEntity);
}
if (leaves > 0) {
ItemStack sap = ItemStack.builder().fromBlockState(sapState).build();
sap.setQuantity(leaves);
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, sap.createSnapshot());
world.spawnEntity(itemEntity);
}
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, log.createSnapshot());
world.spawnEntity(itemEntity);
return;
}
}
}
use of org.spongepowered.api.block.BlockSnapshot in project SpongeVanilla by SpongePowered.
the class MixinEntityPlayer method wakeUpPlayer.
/**
* @author Minecrell
* @reason Implements the post sleeping events.
*/
@Overwrite
public void wakeUpPlayer(boolean immediately, boolean updateWorldFlag, boolean setSpawn) {
// Sponge start (Set size after event call)
// this.setSize(0.6F, 1.8F);
Transform<org.spongepowered.api.world.World> newLocation = null;
// Sponge end
IBlockState iblockstate = this.world.getBlockState(this.bedLocation);
if (this.bedLocation != null && iblockstate.getBlock() == Blocks.BED) {
// Sponge start (Change block state after event call)
// this.world.setBlockState(this.playerLocation, iblockstate.withProperty(BlockBed.OCCUPIED, Boolean.valueOf(false)), 4);
// Sponge end
BlockPos blockpos = BlockBed.getSafeExitLocation(this.world, this.bedLocation, 0);
if (blockpos == null) {
blockpos = this.bedLocation.up();
}
// Sponge start (Store position for later)
/*this.setPosition((double) ((float) blockpos.getX() + 0.5F), (double) ((float) blockpos.getY() + 0.1F),
(double) ((float) blockpos.getZ() + 0.5F));*/
newLocation = getTransform().setPosition(new Vector3d(blockpos.getX() + 0.5F, blockpos.getY() + 0.1F, blockpos.getZ() + 0.5F));
// Sponge end
}
// Sponge start
BlockSnapshot bed = getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation));
try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(this);
SleepingEvent.Post event = SpongeEventFactory.createSleepingEventPost(Sponge.getCauseStackManager().getCurrentCause(), bed, Optional.ofNullable(newLocation), this, setSpawn);
if (SpongeImpl.postEvent(event)) {
return;
}
// Moved from above
this.setSize(0.6F, 1.8F);
if (newLocation != null) {
// Set property only if bed still existsthis.world.setBlockState(this.bedLocation, iblockstate.withProperty(BlockBed.OCCUPIED, false), 4);}
// Teleport player
event.getSpawnTransform().ifPresent(this::setLocationAndAngles);
// Sponge end
this.sleeping = false;
if (!this.world.isRemote && updateWorldFlag) {
this.world.updateAllPlayersSleepingFlag();
}
this.sleepTimer = immediately ? 0 : 100;
if (setSpawn) {
this.setSpawnPoint(this.bedLocation, false);
}
// Sponge start
SpongeImpl.postEvent(SpongeEventFactory.createSleepingEventFinish(Sponge.getCauseStackManager().getCurrentCause(), bed, this));
}
// Sponge end
}
}
use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.
the class RPContainer method canOpen.
public boolean canOpen(BlockSnapshot b, Player p) {
if (!RedProtect.get().cfgs.getBool("private.use")) {
return true;
}
List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST);
String blocktype = b.getState().getType().getName();
Location<World> loc = b.getLocation().get();
World w = loc.getExtent();
List<String> blocks = RedProtect.get().cfgs.getStringList("private.allowed-blocks");
if (blocks.contains(blocktype)) {
for (Direction dir : dirs) {
Location<World> loc1 = getRelative(loc, dir);
if (isSign(loc1.getBlockType())) {
BlockSnapshot sign1 = w.createSnapshot(loc1.getBlockPosition());
if (!validateOpenBlock(sign1, p)) {
return false;
}
}
if (blocks.contains(loc1.getBlockType().getName()) && loc1.getBlockType().equals(b.getState().getType())) {
for (Direction dir2 : dirs) {
Location<World> loc3 = getRelative(loc1, dir2);
if (isSign(loc3.getBlockType())) {
BlockSnapshot sign2 = w.createSnapshot(loc3.getBlockPosition());
if (!validateOpenBlock(sign2, p)) {
return false;
}
}
}
}
}
}
return true;
}
use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.
the class RPContainer method canWorldBreak.
public boolean canWorldBreak(BlockSnapshot b) {
if (!RedProtect.get().cfgs.getBool("private.use")) {
return true;
}
Region reg = RedProtect.get().rm.getTopRegion(b.getLocation().get());
if (reg == null && !RedProtect.get().cfgs.getBool("private.allow-outside")) {
return true;
}
List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.UP, Direction.DOWN);
String blocktype = b.getState().getType().getName();
Location<World> loc = b.getLocation().get();
World w = loc.getExtent();
List<String> blocks = RedProtect.get().cfgs.getStringList("private.allowed-blocks");
if (isSign(loc.getBlockType())) {
BlockSnapshot sign1 = w.createSnapshot(loc.getBlockPosition());
if (!validatePrivateSign(sign1)) {
return false;
}
}
if (blocks.contains(blocktype)) {
for (Direction dir : dirs) {
Location<World> loc1 = getRelative(loc, dir);
if (isSign(loc1.getBlockType())) {
BlockSnapshot sign1 = w.createSnapshot(loc1.getBlockPosition());
if (!validatePrivateSign(sign1)) {
return false;
}
}
if (blocks.contains(loc1.getBlockType().getName()) && loc1.getBlockType().equals(b.getState().getType())) {
for (Direction dir2 : dirs) {
Location<World> loc3 = getRelative(loc1, dir2);
if (isSign(loc3.getBlockType())) {
BlockSnapshot sign2 = w.createSnapshot(loc3.getBlockPosition());
if (!validatePrivateSign(sign2)) {
return false;
}
}
}
}
}
}
return true;
}
use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.
the class RPDoor method ChangeDoor.
public static void ChangeDoor(BlockSnapshot b, Region r) {
if ((!RedProtect.get().cfgs.isFlagEnabled("smart-door") && !RedProtect.get().cfgs.getBool("flags.smart-door")) || !r.getFlagBool("smart-door")) {
return;
}
Location<World> loc = b.getLocation().get();
World w = loc.getExtent();
if (isDoor(b)) {
changeDoorState(b);
if (getDoorState(b)) {
w.playSound(SoundTypes.BLOCK_CHEST_CLOSE, loc.getPosition(), 1);
} else {
w.playSound(SoundTypes.BLOCK_CHEST_OPEN, loc.getPosition(), 1);
}
}
// check side block if is door
BlockSnapshot[] block = new BlockSnapshot[4];
block[0] = w.createSnapshot(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ());
block[1] = w.createSnapshot(loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ());
block[2] = w.createSnapshot(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() + 1);
block[3] = w.createSnapshot(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() - 1);
for (BlockSnapshot b2 : block) {
if (b.getExtendedState().getType().getName().equals(b2.getExtendedState().getType().getName())) {
// b.getWorld().playEffect(b.getLocation(), Effect.DOOR_TOGGLE, 0);
changeDoorState(b);
break;
}
}
}
Aggregations