Skip to content

Commit

Permalink
feat: add BlockSpreadEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 17, 2024
1 parent 4dd2f2a commit 13d6e71
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class BlockBreakEvent extends BlockEvent implements CancellableEvent {
*/
protected EntityPlayer player;

public BlockBreakEvent(BlockStateWithPos block, ItemStack usedItem, EntityPlayer player) {
super(block);
public BlockBreakEvent(BlockStateWithPos blockState, ItemStack usedItem, EntityPlayer player) {
super(blockState);
this.usedItem = usedItem;
this.player = player;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.allaymc.api.eventbus.event.block;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.allaymc.api.block.dto.BlockStateWithPos;
import org.allaymc.api.eventbus.event.Event;

/**
* @author daoge_cmd
*/
@AllArgsConstructor
@Getter
public abstract class BlockEvent extends Event {
protected BlockStateWithPos block;
protected BlockStateWithPos blockState;

public BlockEvent(BlockStateWithPos blockState) {
this.blockState = blockState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
public class BlockFadeEvent extends BlockEvent implements CancellableEvent {
protected BlockState newBlockState;

public BlockFadeEvent(BlockStateWithPos block, BlockState newBlockState) {
super(block);
public BlockFadeEvent(BlockStateWithPos blockState, BlockState newBlockState) {
super(blockState);
this.newBlockState = newBlockState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author daoge_cmd
*/
public class BlockFallEvent extends BlockEvent {
public BlockFallEvent(BlockStateWithPos block) {
super(block);
public BlockFallEvent(BlockStateWithPos blockState) {
super(blockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class BlockNeighborUpdateEvent extends BlockEvent implements CancellableE
protected BlockStateWithPos neighborBlock;
protected BlockFace blockFace;

public BlockNeighborUpdateEvent(BlockStateWithPos block, BlockStateWithPos neighborBlock, BlockFace blockFace) {
super(block);
this.neighborBlock = neighborBlock;
public BlockNeighborUpdateEvent(BlockStateWithPos blockState, BlockStateWithPos neighborBlockState, BlockFace blockFace) {
super(blockState);
this.neighborBlock = neighborBlockState;
this.blockFace = blockFace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Getter
public class BlockPlaceEvent extends BlockEvent implements CancellableEvent {

protected BlockState blockReplaced;
protected BlockState blockStateReplaced;
/**
* can be null
*/
Expand All @@ -28,9 +28,9 @@ public class BlockPlaceEvent extends BlockEvent implements CancellableEvent {
*/
protected PlayerInteractInfo interactInfo;

public BlockPlaceEvent(BlockStateWithPos blockPlaced, BlockState blockReplaced, ItemStack itemUsed, EntityPlayer player, PlayerInteractInfo interactInfo) {
super(blockPlaced);
this.blockReplaced = blockReplaced;
public BlockPlaceEvent(BlockStateWithPos blockStatePlaced, BlockState blockStateReplaced, ItemStack itemUsed, EntityPlayer player, PlayerInteractInfo interactInfo) {
super(blockStatePlaced);
this.blockStateReplaced = blockStateReplaced;
this.itemUsed = itemUsed;
this.player = player;
this.interactInfo = interactInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author daoge_cmd
*/
public class BlockRandomUpdateEvent extends BlockEvent implements CancellableEvent {
public BlockRandomUpdateEvent(BlockStateWithPos block) {
super(block);
public BlockRandomUpdateEvent(BlockStateWithPos blockState) {
super(blockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author daoge_cmd
*/
public class BlockScheduleUpdateEvent extends BlockEvent implements CancellableEvent {
public BlockScheduleUpdateEvent(BlockStateWithPos block) {
super(block);
public BlockScheduleUpdateEvent(BlockStateWithPos blockState) {
super(blockState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.allaymc.api.eventbus.event.block;

import lombok.Getter;
import lombok.Setter;
import org.allaymc.api.block.dto.BlockStateWithPos;
import org.allaymc.api.eventbus.event.CancellableEvent;

/**
* @author daoge_cmd
*/
@Getter
@Setter
public class BlockSpreadEvent extends BlockEvent implements CancellableEvent {

protected BlockStateWithPos spreadBlockState;

public BlockSpreadEvent(BlockStateWithPos blockState, BlockStateWithPos spreadBlockState) {
super(blockState);
this.spreadBlockState = spreadBlockState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class SignTextChangeEvent extends BlockEvent implements CancellableEvent
protected String[] text;
protected EntityPlayer editor;

public SignTextChangeEvent(BlockStateWithPos block, String[] text, EntityPlayer editor) {
super(block);
public SignTextChangeEvent(BlockStateWithPos blockState, String[] text, EntityPlayer editor) {
super(blockState);
setText(text);
this.editor = editor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class SignWaxEvent extends BlockEvent implements CancellableEvent {
protected EntityPlayer player;

public SignWaxEvent(BlockStateWithPos block, EntityPlayer player) {
super(block);
public SignWaxEvent(BlockStateWithPos blockState, EntityPlayer player) {
super(blockState);
this.player = player;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.eventbus.event.block.BlockFadeEvent;
import org.allaymc.api.eventbus.event.block.BlockSpreadEvent;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.type.ItemTypes;
import org.allaymc.api.math.position.Position3i;

import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -61,13 +63,16 @@ public void onRandomUpdate(BlockStateWithPos blockStateWithPos) {
int y = random.nextInt(pos.y() - 3, pos.y() + 1 + 1);
int z = random.nextInt(pos.z() - 1, pos.z() + 1 + 1);
var blockState = dimension.getBlockState(x, y, z);
if (blockState.getBlockType() == BlockTypes.DIRT
if (blockState.getBlockType() == BlockTypes.DIRT &&
// The dirt block must have a light level of at least 4 above it.
&& dimension.getLightService().getInternalLight(x, y + 1, z) >= 4
dimension.getLightService().getInternalLight(x, y + 1, z) >= 4 &&
// Any block directly above the dirt block must not reduce light by 2 levels or more.
&& dimension.getBlockState(x, y + 1, z).getBlockStateData().lightDampening() < 2) {
// TODO: BlockSpreadEvent
dimension.setBlockState(x, y, z, BlockTypes.GRASS_BLOCK.getDefaultState());
dimension.getBlockState(x, y + 1, z).getBlockStateData().lightDampening() < 2) {
var spreadBlockState = new BlockStateWithPos(BlockTypes.GRASS_BLOCK.getDefaultState(), new Position3i(x, y, z, dimension), 0);
var event = new BlockSpreadEvent(blockStateWithPos, spreadBlockState);
if (event.call()) {
dimension.setBlockState(x, y, z, event.getSpreadBlockState().blockState());
}
}
}
}
Expand Down

0 comments on commit 13d6e71

Please sign in to comment.