Skip to content

Commit

Permalink
feat: More new blocks (#361)
Browse files Browse the repository at this point in the history
* feat: add stonecutter

No functionality, just block at the moment!!!

* runtime states for lightning rod & powder snow

* feat: powder snow freezing
  • Loading branch information
KoshakMineDEV authored Jan 1, 2025
1 parent af47c03 commit 43e8fac
Show file tree
Hide file tree
Showing 31 changed files with 183 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,7 @@ protected void checkBlockCollision() {
boolean portal = false;
boolean endPortal = false;
boolean scaffolding = false;
boolean powderSnow = false;

for (Block block : this.getCollisionBlocks()) {
switch (block.getId()) {
Expand All @@ -1802,6 +1803,9 @@ protected void checkBlockCollision() {
case Block.SCAFFOLDING:
scaffolding = true;
break;
case Block.POWDER_SNOW:
powderSnow = true;
break;
}

block.onEntityCollide(this);
Expand Down Expand Up @@ -1905,6 +1909,26 @@ public void onRun() {
}
}
}

if(this.getFreezingTicks() < 140 && powderSnow) {
if (getFreezingTicks() == 0) {
this.setSprinting(false);
}
this.addFreezingTicks(1);
EntityFreezeEvent event = new EntityFreezeEvent(this);
this.server.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
this.setMovementSpeed((float) Math.max(0.05, getMovementSpeed() - 3.58e-4));
}
}
if(!powderSnow && this.getFreezingTicks() > 0) {
this.addFreezingTicks(-1);
this.setMovementSpeed((float) Math.min(Player.DEFAULT_SPEED, getMovementSpeed() + 3.58e-4));//This magic number is to change the player's 0.05 speed within 140tick
}

if (this.getFreezingTicks() == 140 && this.getServer().getTick() % 40 == 0) {
this.attack(new EntityDamageEvent(this, EntityDamageEvent.DamageCause.FREEZING, getFrostbiteInjury()));
}
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/cn/nukkit/block/BlockStonecutterBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cn.nukkit.block;

import cn.nukkit.Player;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.item.ItemTool;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.Faceable;

public class BlockStonecutterBlock extends BlockSolidMeta implements Faceable {

public BlockStonecutterBlock() {
this(0);
}

public BlockStonecutterBlock(int meta) {
super(meta);
}

@Override
public String getName() {
return "Stonecutter Block";
}

@Override
public int getId() {
return STONECUTTER_BLOCK;
}

@Override
public double getHardness() {
return 3.5;
}

@Override
public double getResistance() {
return 17.5;
}

@Override
public boolean canHarvestWithHand() {
return false;
}

@Override
public int getToolType() {
return ItemTool.TYPE_PICKAXE;
}

@Override
public int getToolTier() {
return ItemTool.TIER_WOODEN;
}

@Override
public Item[] getDrops(Item item) {
return new Item[]{toItem()};
}

@Override
public Item toItem() {
return new ItemBlock(new BlockStonecutterBlock());
}

@Override
public double getMaxY() {
return y + 0.5625;
}

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
this.setDamage(Block.FACES2534[player != null ? player.getDirection().getHorizontalIndex() : 0]);
return super.place(item, block, target, face, fx, fy, fz, player);
}

//TODO: Add Stonecutter functionality
/*@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Item item, Player player) {
if (player != null) {
player.addWindow(new StonecutterInventory(player.getUIInventory(), this), Player.STONECUTTER_WINDOW_ID);
}
return true;
}*/

@Override
public BlockFace getBlockFace() {
return BlockFace.fromHorizontalIndex(this.getDamage() & 0x7);
}
}
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/block/Blocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public class Blocks {
list[LECTERN] = BlockLectern.class; //449
list[GRINDSTONE] = BlockGrindstone.class; //450
list[BLAST_FURNACE] = BlockBlastFurnace.class; //451
list[STONECUTTER_BLOCK] = BlockStonecutterBlock.class; //452
list[SMOKER] = BlockSmoker.class; //453
list[LIT_SMOKER] = BlockSmokerLit.class; //454
list[CARTOGRAPHY_TABLE] = BlockCartographyTable.class; //455
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public abstract class Entity extends Location implements Metadatable {
public int lastUpdate;
public int fireTicks = 0;
public int inPortalTicks = 0;
public int freezingTicks = 0;//0 - 140
public int inEndPortalTicks = 0;
public Position portalPos = null;

Expand Down Expand Up @@ -534,6 +535,10 @@ protected float getBaseOffset() {
return 0;
}

public int getFrostbiteInjury() {
return 1;
}

public Entity(FullChunk chunk, CompoundTag nbt) {
this.isPlayer = this instanceof Player;
if (this.isPlayer) {
Expand Down Expand Up @@ -2599,13 +2604,19 @@ protected void checkBlockCollision() {

Vector3 vector = new Vector3(0, 0, 0);
boolean portal = false;
boolean powderSnow = false;

for (Block block : this.getCollisionBlocks()) {
if (block.getId() == Block.NETHER_PORTAL) {
portal = true;
continue;
}

if (block.getId() == Block.POWDER_SNOW) {
portal = true;
continue;
}

block.onEntityCollide(this);
block.getLevelBlockAtLayer(1).onEntityCollide(this);
block.addVelocityToEntity(this, vector);
Expand All @@ -2616,7 +2627,7 @@ protected void checkBlockCollision() {
} else {
this.inPortalTicks = 0;
}

if (vector.lengthSquared() > 0) {
vector = vector.normalize();
double d = 0.014d;
Expand Down Expand Up @@ -3210,6 +3221,34 @@ public List<StringTag> getAllTags() {
return this.namedTag.getList("Tags", StringTag.class).getAll();
}

public float getFreezingEffectStrength() {
return getDataPropertyFloat(DATA_FREEZING_EFFECT_STRENGTH);
}

public void setFreezingEffectStrength(float strength) {
if (strength < 0 || strength > 1)
throw new IllegalArgumentException("Freezing Effect Strength must be between 0 and 1");
this.setDataProperty(new FloatEntityData(DATA_FREEZING_EFFECT_STRENGTH, strength));
}

public int getFreezingTicks() {
return this.freezingTicks;
}

public void setFreezingTicks(int ticks) {
if (ticks < 0) this.freezingTicks = 0;
else if (ticks > 140) this.freezingTicks = 140;
else this.freezingTicks = ticks;
setFreezingEffectStrength(ticks / 140f);
}

public void addFreezingTicks(int increments) {
if (freezingTicks + increments < 0) this.freezingTicks = 0;
else if (freezingTicks + increments > 140) this.freezingTicks = 140;
else this.freezingTicks += increments;
setFreezingEffectStrength(this.freezingTicks / 140f);
}

private boolean validateAndSetIntProperty(String identifier, int value) {
if(!intProperties.containsKey(identifier)) {
return false;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/nukkit/event/entity/EntityDamageEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ public enum DamageCause {
/**
* Damage caused by standing on BlockMagma
*/
HOT_FLOOR
HOT_FLOOR,
/**
* Damage caused by temperature
*/
FREEZING
}

private static class DamageModifierFloatEnumMap extends EnumMap<DamageModifier, Float> {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/cn/nukkit/event/entity/EntityFreezeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.nukkit.event.entity;

import cn.nukkit.entity.Entity;
import cn.nukkit.event.Cancellable;
import cn.nukkit.event.HandlerList;

public class EntityFreezeEvent extends EntityEvent implements Cancellable {
private final Entity entity;

private static final HandlerList handlers = new HandlerList();

public static HandlerList getHandlers() {
return handlers;
}

public EntityFreezeEvent(Entity human) {
this.entity = human;
}
}
Binary file modified src/main/resources/runtime_block_states_440.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_448.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_465.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_471.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_486.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_503.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_527.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_544.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_560.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_567.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_575.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_582.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_589.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_594.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_618.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_622.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_630.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_649.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_662.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_671.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_685.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_712.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_729.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_748.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_766.dat
Binary file not shown.

0 comments on commit 43e8fac

Please sign in to comment.