-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rotate piranha based on attached block side
- Loading branch information
Showing
6 changed files
with
184 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/wenxin2/marioverse/items/BetterSpawnEggItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.wenxin2.marioverse.items; | ||
|
||
import com.wenxin2.marioverse.entities.PiranhaPlantEntity; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.InteractionResult; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.Mob; | ||
import net.minecraft.world.entity.MobSpawnType; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.context.UseOnContext; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.Spawner; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.gameevent.GameEvent; | ||
import net.neoforged.neoforge.common.DeferredSpawnEggItem; | ||
|
||
public class BetterSpawnEggItem extends DeferredSpawnEggItem { | ||
public BetterSpawnEggItem(Supplier<? extends EntityType<? extends Mob>> entityType, | ||
int primaryColor, int secondaryColor, Properties properties) { | ||
super(entityType, primaryColor, secondaryColor, properties); | ||
} | ||
|
||
@Override | ||
public InteractionResult useOn(UseOnContext context) { | ||
Level world = context.getLevel(); | ||
if (!(world instanceof ServerLevel)) { | ||
return InteractionResult.SUCCESS; | ||
} else { | ||
ItemStack stack = context.getItemInHand(); | ||
BlockPos pos = context.getClickedPos(); | ||
Direction direction = context.getClickedFace(); | ||
BlockState state = world.getBlockState(pos); | ||
|
||
if (world.getBlockEntity(pos) instanceof Spawner spawner) { | ||
EntityType<?> entityType1 = this.getType(stack); | ||
spawner.setEntityId(entityType1, world.getRandom()); | ||
world.sendBlockUpdated(pos, state, state, 3); | ||
world.gameEvent(context.getPlayer(), GameEvent.BLOCK_CHANGE, pos); | ||
stack.shrink(1); | ||
return InteractionResult.CONSUME; | ||
} else { | ||
|
||
EntityType<?> entityType = this.getType(stack); | ||
BlockPos spawnPos = context.getClickedFace().getOpposite() == Direction.DOWN | ||
? (new BlockPos(pos.getX(), (int) (pos.getY() - entityType.getHeight()), pos.getZ())) : pos.relative(context.getClickedFace()); | ||
BlockPos pos1; | ||
if (state.getCollisionShape(world, spawnPos).isEmpty()) | ||
pos1 = spawnPos; | ||
else pos1 = spawnPos.relative(direction); | ||
|
||
Entity entity = entityType.spawn((ServerLevel) world, stack, context.getPlayer(), pos1, MobSpawnType.SPAWN_EGG, true, | ||
!Objects.equals(spawnPos, pos1) && direction == Direction.UP); | ||
if (entity != null) { | ||
stack.shrink(1); | ||
world.gameEvent(context.getPlayer(), GameEvent.ENTITY_PLACE, pos); | ||
|
||
if (entity instanceof PiranhaPlantEntity piranhaPlant) | ||
piranhaPlant.setAttachedSide(context.getClickedFace().getOpposite()); | ||
} | ||
|
||
return InteractionResult.CONSUME; | ||
} | ||
} | ||
} | ||
} |