use of org.spongepowered.api.block.BlockState in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callNeighborNotifyEvent.
public static NotifyNeighborBlockEvent callNeighborNotifyEvent(Event event) {
NotifyNeighborBlockEvent spongeEvent = (NotifyNeighborBlockEvent) event;
LocatableBlock locatableBlock = spongeEvent.getCause().first(LocatableBlock.class).orElse(null);
TileEntity tileEntitySource = spongeEvent.getCause().first(TileEntity.class).orElse(null);
Location<World> sourceLocation = null;
IBlockState state = null;
if (locatableBlock != null) {
Location<World> location = locatableBlock.getLocation();
sourceLocation = location;
state = (IBlockState) locatableBlock.getBlockState();
} else if (tileEntitySource != null) {
sourceLocation = tileEntitySource.getLocation();
state = (IBlockState) sourceLocation.getBlock();
} else {
// should never happen but just in case it does
return spongeEvent;
}
EnumSet<EnumFacing> facings = EnumSet.noneOf(EnumFacing.class);
for (Map.Entry<Direction, BlockState> mapEntry : spongeEvent.getNeighbors().entrySet()) {
if (mapEntry.getKey() != Direction.NONE) {
facings.add(DirectionFacingProvider.getInstance().get(mapEntry.getKey()).get());
}
}
if (facings.isEmpty()) {
return spongeEvent;
}
BlockPos pos = ((IMixinLocation) (Object) sourceLocation).getBlockPos();
net.minecraft.world.World world = (net.minecraft.world.World) sourceLocation.getExtent();
// TODO - the boolean forced redstone bit needs to be set properly
final NeighborNotifyEvent forgeEvent = new NeighborNotifyEvent(world, pos, state, facings, false);
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
if (forgeEvent.isCanceled()) {
spongeEvent.setCancelled(true);
}
return spongeEvent;
}
use of org.spongepowered.api.block.BlockState in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createChangeBlockEventPlace.
public static ChangeBlockEvent.Place createChangeBlockEventPlace(BlockEvent.PlaceEvent forgeEvent) {
final BlockPos pos = forgeEvent.getPos();
final net.minecraft.world.World world = forgeEvent.getWorld();
if (world.isRemote) {
return null;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockSnapshot originalSnapshot = ((IMixinBlockSnapshot) forgeEvent.getBlockSnapshot()).createSpongeBlockSnapshot();
BlockSnapshot finalSnapshot = ((BlockState) forgeEvent.getPlacedBlock()).snapshotFor(new Location<>((World) world, VecHelper.toVector3d(pos)));
ImmutableList<Transaction<BlockSnapshot>> blockSnapshots = new ImmutableList.Builder<Transaction<BlockSnapshot>>().add(new Transaction<>(originalSnapshot, finalSnapshot)).build();
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
EntityPlayer player = forgeEvent.getPlayer();
if (SpongeImplHooks.isFakePlayer(player)) {
Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
} else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(owner);
}
} else {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_PLACE, (World) world);
return SpongeEventFactory.createChangeBlockEventPlace(Sponge.getCauseStackManager().getCurrentCause(), blockSnapshots);
}
use of org.spongepowered.api.block.BlockState in project SpongeForge by SpongePowered.
the class ForgeLightEmissionPropertyStore method getFor.
@Override
public Optional<LightEmissionProperty> getFor(Location<World> location) {
net.minecraft.world.World world = (net.minecraft.world.World) location.getExtent();
Block block = (Block) location.getBlockType();
final BlockState blockState = location.getBlock();
return Optional.of(new LightEmissionProperty(block.getLightValue(BlockUtil.toNative(blockState), world, VecHelper.toBlockPos(location))));
}
use of org.spongepowered.api.block.BlockState 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.BlockState in project RedProtect by FabioZumbi12.
the class RPGlobalListener method onFireSpread.
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(NotifyNeighborBlockEvent e, @Root BlockSnapshot source) {
Map<Direction, BlockState> dirs = e.getNeighbors();
for (Direction dir : dirs.keySet()) {
BlockSnapshot b = source.getLocation().get().getRelative(dir).createSnapshot();
BlockState bstate = source.getState();
Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get());
if (r != null) {
return;
}
if ((bstate.getType().equals(BlockTypes.FIRE) || bstate.getType().getName().contains("lava")) && !RedProtect.get().cfgs.getGlobalFlag(b.getLocation().get().getExtent().getName(), "fire-spread")) {
e.setCancelled(true);
return;
}
}
}
Aggregations