use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.
the class MixinBlockLeaves method getStateWithData.
@Override
public Optional<BlockState> getStateWithData(IBlockState blockState, ImmutableDataManipulator<?, ?> manipulator) {
if (manipulator instanceof ImmutableTreeData) {
final TreeType treeType = ((ImmutableTreeData) manipulator).type().get();
final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
if (blockState.getBlock() instanceof BlockOldLeaf) {
if (treeType.equals(TreeTypes.OAK) || treeType.equals(TreeTypes.BIRCH) || treeType.equals(TreeTypes.SPRUCE) || treeType.equals(TreeTypes.JUNGLE)) {
return Optional.of((BlockState) blockState.withProperty(BlockOldLeaf.VARIANT, type));
}
} else if (blockState.getBlock() instanceof BlockNewLeaf) {
if (treeType.equals(TreeTypes.ACACIA) || treeType.equals(TreeTypes.DARK_OAK)) {
return Optional.of((BlockState) blockState.withProperty(BlockNewLeaf.VARIANT, type));
}
}
return Optional.empty();
}
if (manipulator instanceof ImmutableDecayableData) {
final boolean decayable = ((ImmutableDecayableData) manipulator).decayable().get();
return Optional.of((BlockState) blockState.withProperty(BlockLeaves.DECAYABLE, decayable));
}
return super.getStateWithData(blockState, manipulator);
}
use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.
the class MixinBlockLog method getStateWithData.
@Override
public Optional<BlockState> getStateWithData(IBlockState blockState, ImmutableDataManipulator<?, ?> manipulator) {
if (manipulator instanceof ImmutableTreeData) {
final TreeType treeType = ((ImmutableTreeData) manipulator).type().get();
final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
return processLogType(blockState, type, treeType);
} else if (manipulator instanceof ImmutableLogAxisData) {
final LogAxis logAxis = ((ImmutableLogAxisData) manipulator).type().get();
return Optional.of((BlockState) blockState.withProperty(BlockLog.LOG_AXIS, (BlockLog.EnumAxis) (Object) logAxis));
}
return super.getStateWithData(blockState, manipulator);
}
use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.
the class MixinBlockLog method getTreeData.
protected ImmutableTreeData getTreeData(IBlockState blockState) {
BlockPlanks.EnumType type;
if (blockState.getBlock() instanceof BlockOldLog) {
type = blockState.getValue(BlockOldLog.VARIANT);
} else if (blockState.getBlock() instanceof BlockNewLog) {
type = blockState.getValue(BlockNewLog.VARIANT);
} else {
type = BlockPlanks.EnumType.OAK;
}
final TreeType treeType = TreeTypeResolver.getFor(type);
return ImmutableDataCachingUtil.getManipulator(ImmutableSpongeTreeData.class, treeType);
}
use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.
the class MixinBlockLog method getStateWithValue.
@Override
public <E> Optional<BlockState> getStateWithValue(IBlockState blockState, Key<? extends BaseValue<E>> key, E value) {
if (key.equals(Keys.TREE_TYPE)) {
final TreeType treeType = (TreeType) value;
final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
return processLogType(blockState, type, treeType);
} else if (key.equals(Keys.LOG_AXIS)) {
return Optional.of((BlockState) blockState.withProperty(BlockLog.LOG_AXIS, (BlockLog.EnumAxis) value));
}
return super.getStateWithValue(blockState, key, value);
}
use of org.spongepowered.api.data.type.TreeType 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;
}
}
}
Aggregations