Skip to content

Commit

Permalink
feat: implement tnt (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd authored Jan 16, 2025
1 parent 3b4f7e0 commit ef6afe1
Show file tree
Hide file tree
Showing 41 changed files with 1,079 additions and 109 deletions.
20 changes: 18 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ Unless otherwise specified, any version comparison below is the comparison of se
history for more details.
- (API) Implemented picking block with block entity data. The following methods are added: `ItemBaseComponent#getBlockEntityNBT`,
`ItemBaseComponent#setBlockEntityNBT`, `ItemBaseComponent#clearBlockEntityNBT` and `ItemBaseComponent#hasBlockEntityNBT`.
- (API) Implemented TNT. There is now a new class called `Explosion` which can be used by plugin to make custom explosion.
- (API) Introduced a number of overloads of `Dimension#addSound`.
- (API) Introduced method `EntityAttributeComponent#supportAttribute` to check if the entity support specified attribute type.
- (API) Introduced methods `DamageContainer#blockExplosion` and `DamageContainer#entityExplosion` to create explosion related damage.
- (API) Introduced methods `EntityBaseComponent#getDragFactorOnGround` and `EntityBaseComponent#getDragFactorInAir`, which can be used to
customize the drag factor of an entity.
- (API) Introduced event `EntityExplodeEvent` which will be called when tnt or creeper(WIP) is about to explode.
- (API) Introduced method `EntityBaseComponent#isTouchingWater` to check if an entity is touching water.
- (API) Implemented TNT entity and block. Several related interfaces are added to api module.
- Implemented trapdoor except redstone feature (Redstone feature requires the implementation of redstone system).
- Implemented sponge and wet sponge.
- Implemented farmland and hoe.
Expand All @@ -66,23 +74,31 @@ Unless otherwise specified, any version comparison below is the comparison of se
- (API) Renamed `Structure#pickStructure` to `Structure#pick`.
- (API) Renamed `ItemItemStorableComponentImpl` to `ItemStuffStorableComponentImpl`, and now `ItemShulkerBoxStack`
extends `ItemStuffStorableComponent`.
- (API) Removed methods `EntityAttributeComponent#supportHealth` and `EntityAttributeComponent#supportAbsorption`.
Consider using new method `EntityAttributeComponent#supportAttribute`.
- (API) Renamed method `EntityBaseComponent#getBaseOffset` to `EntityBaseComponent#getNetworkOffset` for better
understanding.
- (API) Removed method `Dimension#setBlockStates`. This method is considered to be unsafe as it will only set the block state,
block entity won't be created if the block has block entity. Further research is currently needed.
- Removed useless class `PackageClassLoaderUtils`, dependency `org.reflections.reflections` is also removed.
- Added `-dev` suffix to api version in development build.
- Changed `ContainerActionProcessorHolder` to a final class instead of an interface, because this abstraction is
meaningless.
- Changed `enableGui` to `enable-gui` in `server-settings.yml`
- Disabled packet limit only in dev build.
- Optimized the performance of physics calculation when there are a lot of entities.

### Fixed

- (API) `BlockHangingSignBehavior` now extends `BlockEntityHolderComponent<BlockEntityHangingSign>` which was forgotten
to be added.
- Fixed several bugs that can led falling block keep existing even if it is already on ground or can't move.
- Fixed the `ClassCastException` when breaking shulker box.
- Fixed the bug that interacting with door doesn't have any sound.
- Waxing copper-made block using honeycomb won't call `BlockFadeEvent` now.
- Fixed the bug that player can still open enchant table even if he is sneaking.
- Fixed the bug that brewing stand fast brew and lagging brew animation.
- Fixed translation for potion mix loading.
- Fixed NaN motion caused by liquid in some very special cases.
- Fixed the bug that entity will still get ticked after called `removeEntity()`.

## [0.1.2](https://github.com/AllayMC/Allay/releases/tag/0.1.2) (API 0.3.0) - 2024-12-31

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.allaymc.api.block.component;

import org.allaymc.api.block.dto.BlockStateWithPos;

/**
* @author daoge_cmd
*/
public interface BlockTntBaseComponent extends BlockBaseComponent {

/**
* @see #prime(BlockStateWithPos, int)
*/
default void prime(BlockStateWithPos blockStateWithPos) {
this.prime(blockStateWithPos, 80);
}

/**
* Prime the TNT at specified pos.
*
* @param blockStateWithPos the tnt to prime.
* @param fuse the fuse of the tnt.
*/
void prime(BlockStateWithPos blockStateWithPos, int fuse);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockTntBaseComponent;

public interface BlockTntBehavior extends BlockBehavior {
public interface BlockTntBehavior extends BlockBehavior, BlockTntBaseComponent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,14 @@ default boolean enableHeadYaw() {
}

/**
* Get the base offset of this entity.
* Get the network offset of this entity.
* <p>
* The network offset is the additional offset in y coordinate when sent over network.
* This is mostly the case for older entities such as players and TNT.
*
* @return the base offset of this entity.
*/
default float getBaseOffset() {
default float getNetworkOffset() {
return 0f;
}

Expand Down Expand Up @@ -612,6 +615,31 @@ default float getGravity() {
return 0.08f;
}

/**
* Get the drag factor when on ground of this entity.
* <p>
* This factor will be multiplied to the motion along x and z axis every tick.
* The bigger the factor is, the quicker the entity will stop in x-axis and z-axis.
*
* @return the drag factor when on ground of this entity.
*/
default float getDragFactorOnGround() {
return 0.1f;
}

/**
* Get the drag factor when in air of this entity.
* <p>
* This value is similar to {@link #getDragFactorOnGround}, however this value
* will be used to reduce motion along x-axis and z-axis when the entity is not on ground.
* This value will always be used for motion along y-axis.
*
* @return the drag factor when in air of this entity.
*/
default float getDragFactorInAir() {
return 0.02f;
}

/**
* Check if the entity has gravity.
*
Expand Down Expand Up @@ -725,32 +753,35 @@ default BlockFace getHorizontalFace() {
}

/**
* Knockback the entity.
*
* @param source the source of the knockback.
* @see #knockback(Vector3fc, float, boolean, float)
*/
default void knockback(Vector3fc source) {
knockback(source, DEFAULT_KNOCKBACK);
}

/**
* Knockback the entity.
*
* @param source the source of the knockback.
* @param kb the knockback strength to apply.
* @see #knockback(Vector3fc, float, boolean, float)
*/
default void knockback(Vector3fc source, float kb) {
knockback(source, kb, false);
}

/**
* Knockback the entity.
* @see #knockback(Vector3fc, float, boolean, float)
*/
default void knockback(Vector3fc source, float kb, boolean ignoreKnockbackResistance) {
knockback(source, kb, ignoreKnockbackResistance, kb);
}

/**
* Knockback the entity with specified kb value.
*
* @param source the source of the knockback.
* @param kb the knockback strength to apply.
* @param ignoreKnockbackResistance {@code true} if the knockback resistance should be ignored.
* @param kby the knockback strength in y-axis.
*/
void knockback(Vector3fc source, float kb, boolean ignoreKnockbackResistance);
void knockback(Vector3fc source, float kb, boolean ignoreKnockbackResistance, float kby);

/**
* Apply the entity event to the entity.
Expand Down Expand Up @@ -836,7 +867,7 @@ default boolean canCriticalAttack() {
/**
* Check if the entity's eyes is in water.
*
* @return {@code true} if the entity is in water, otherwise {@code false}.
* @return {@code true} if the entity's eyes is in water, otherwise {@code false}.
*/
default boolean isEyesInWater() {
var dim = getDimension();
Expand All @@ -847,6 +878,20 @@ default boolean isEyesInWater() {
eyesBlockState.getBlockStateData().computeOffsetShape(MathUtils.floor(eyeLoc)).intersectsPoint(eyeLoc);
}

/**
* Check if the entity is touching water.
*
* @return {@code true} if the entity is touching water, otherwise {@code false}.
*/
default boolean isTouchingWater() {
var dim = getDimension();
var loc = getLocation();
var blockState = dim.getBlockState(loc);

return blockState.getBlockType().hasBlockTag(BlockTags.WATER) &&
blockState.getBlockStateData().computeOffsetShape(MathUtils.floor(loc)).intersectsPoint(loc);
}

/**
* Check if the entity can breathe.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.allaymc.api.entity.component;

/**
* @author daoge_cmd
*/
public interface EntityTntBaseComponent extends EntityBaseComponent {
/**
* Get the fuse of the tnt entity.
*
* @return the fuse of the tnt entity.
*/
int getFuse();

/**
* Set the fuse of the tnt entity.
*
* @param fuse the fuse of the tnt entity.
*/
void setFuse(int fuse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ static AttributeType[] basicEntityAttributes() {
void setAttributeValue(AttributeType attributeType, float value);

/**
* Check if entity supports health attribute.
* Check if entity support specified attribute type.
*
* @return {@code true} if entity supports health attribute, {@code false} otherwise.
* @param attributeType the attribute type to check.
*
* @return {@code true} if entity supports the specified attribute type, {@code false} otherwise.
*/
default boolean supportHealth() {
return getAttribute(AttributeType.HEALTH) != null;
default boolean supportAttribute(AttributeType attributeType) {
return getAttribute(attributeType) != null;
}

/**
Expand Down Expand Up @@ -138,15 +140,6 @@ default void kill() {
setHealth(0);
}

/**
* Check if entity supports absorption attribute.
*
* @return {@code true} if entity supports absorption attribute, {@code false} otherwise.
*/
default boolean supportAbsorption() {
return getAttribute(AttributeType.ABSORPTION) != null;
}

/**
* Get entity absorption.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ default long getItemUsingInAirTime() {
* @return The base offset of the player.
*/
@Override
default float getBaseOffset() {
default float getNetworkOffset() {
return 1.62f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,40 @@ public static DamageContainer fireTick(float sourceDamage) {
return new DamageContainer(null, FIRE_TICK, sourceDamage);
}

/**
* Create a lava damage container.
*
* @param sourceDamage the source damage.
*
* @return the damage container.
*/
public static DamageContainer lava(float sourceDamage) {
return new DamageContainer(null, LAVA, sourceDamage);
}

/**
* Create a block explosion damage container.
*
* @param sourceDamage the source damage.
*
* @return the damage container.
*/
public static DamageContainer blockExplosion(float sourceDamage) {
return new DamageContainer(null, BLOCK_EXPLOSION, sourceDamage);
}

/**
* Create an entity explosion damage container.
*
* @param attacker the entity that exploded.
* @param sourceDamage the source damage.
*
* @return the damage container.
*/
public static DamageContainer entityExplosion(Entity attacker, float sourceDamage) {
return new DamageContainer(attacker, ENTITY_EXPLOSION, sourceDamage);
}

/**
* Get the attacker.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.api.entity.effect.type;

import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.component.attribute.AttributeType;
import org.allaymc.api.entity.component.attribute.EntityAttributeComponent;
import org.allaymc.api.entity.effect.AbstractEffectType;
import org.allaymc.api.entity.effect.EffectInstance;
Expand All @@ -16,14 +17,14 @@ public EffectAbsorptionType() {

@Override
public void onAdd(Entity entity, EffectInstance effectInstance) {
if (entity instanceof EntityAttributeComponent attributeComponent && attributeComponent.supportAbsorption()) {
if (entity instanceof EntityAttributeComponent attributeComponent && attributeComponent.supportAttribute(AttributeType.ABSORPTION)) {
attributeComponent.setAbsorption(effectInstance.getLevel() * 4);
}
}

@Override
public void onRemove(Entity entity, EffectInstance effectInstance) {
if (entity instanceof EntityAttributeComponent attributeComponent && attributeComponent.supportAbsorption()) {
if (entity instanceof EntityAttributeComponent attributeComponent && attributeComponent.supportAttribute(AttributeType.ABSORPTION)) {
attributeComponent.setAbsorption(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.api.entity.effect.type;

import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.component.attribute.AttributeType;
import org.allaymc.api.entity.component.attribute.EntityAttributeComponent;
import org.allaymc.api.entity.effect.AbstractEffectType;
import org.allaymc.api.entity.effect.EffectInstance;
Expand All @@ -16,14 +17,16 @@ public EffectHealthBoostType() {

@Override
public void onAdd(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityAttributeComponent component) || !component.supportHealth()) return;
if (!(entity instanceof EntityAttributeComponent component) || !component.supportAttribute(AttributeType.HEALTH))
return;
var level = effectInstance.getLevel();
component.setMaxHealth(component.getMaxHealth() + (level * 4));
}

@Override
public void onRemove(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityAttributeComponent component) || !component.supportHealth()) return;
if (!(entity instanceof EntityAttributeComponent component) || !component.supportAttribute(AttributeType.HEALTH))
return;
var level = effectInstance.getLevel();
component.setMaxHealth(component.getMaxHealth() - (level * 4));
if (component.getHealth() > component.getMaxHealth()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.allaymc.api.entity.interfaces;

import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.component.EntityTntBaseComponent;

public interface EntityTnt extends Entity {
public interface EntityTnt extends Entity, EntityTntBaseComponent {

}
Loading

0 comments on commit ef6afe1

Please sign in to comment.