Skip to content

Commit

Permalink
feat: add wither jar behavior (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarva authored Jul 21, 2024
1 parent 1464c3f commit 17019c8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ public void applyForRange(Level level, Vec3 atPos, double distanceFrom, Function
posMap.get(key).remove(pos);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.hollingsworth.arsnouveau.common.mob_jar;

import com.hollingsworth.arsnouveau.ArsNouveau;
import com.hollingsworth.arsnouveau.api.entity.IDispellable;
import com.hollingsworth.arsnouveau.api.entity.ISummon;
import com.hollingsworth.arsnouveau.api.mob_jar.JarBehavior;
import com.hollingsworth.arsnouveau.api.util.LevelPosMap;
import com.hollingsworth.arsnouveau.common.block.tile.MobJarTile;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = ArsNouveau.MODID)
public class WitherBehavior extends JarBehavior<WitherBoss> {
public static LevelPosMap WITHER_MAP = new LevelPosMap(
(level, pos) -> !(level.getBlockEntity(pos) instanceof MobJarTile mobJarTile) || !(mobJarTile.getEntity() instanceof WitherBoss)
);

@Override
public void tick(MobJarTile tile) {
Level level = tile.getLevel();
if (level == null) return;

if(level.getGameTime() % 20 == 0){
WITHER_MAP.addPosition(level, tile.getBlockPos());
}
}

@Override
public void onRedstonePower(MobJarTile tile) {
destroyBlocks(tile);
}

private void destroyBlocks(MobJarTile tile) {
BlockPos pos = tile.getBlockPos();
WitherBoss entity = entityFromJar(tile);
if (!ForgeEventFactory.getMobGriefingEvent(tile.getLevel(), entity)) return;
for (BlockPos block : BlockPos.betweenClosed(pos.offset(-1, -1, -1), pos.offset(1, 1, 1))) {
if (block.equals(pos)) continue;
BlockState bs = tile.getLevel().getBlockState(block);
if (bs.canEntityDestroy(tile.getLevel(), block, entity) && ForgeEventFactory.onEntityDestroyBlock(entity, block, bs)) {
tile.getLevel().destroyBlock(block, true, entity);
}
}
}

@SubscribeEvent
public static void livingDeath(LivingDeathEvent event) {
Entity entity = event.getEntity();
Level level = entity.level();
if (level.isClientSide() || entity instanceof IDispellable || entity instanceof ISummon)
return;

WITHER_MAP.applyForRange(level, entity.blockPosition(), 4, (pos) ->{
if(level.getBlockEntity(pos) instanceof MobJarTile tile && tile.getEntity() instanceof WitherBoss){
ItemStack rose = new ItemStack(Items.WITHER_ROSE);
BlockPos blockPos = entity.blockPosition();
level.addFreshEntity(new ItemEntity(level, blockPos.getX(), blockPos.getY(), blockPos.getZ(), rose));
return true;
}
return false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public void use(BlockState state, Level world, BlockPos pos, Player player, Inte
}
}
});
JarBehaviorRegistry.register(EntityType.WITHER, new WitherBehavior());
}

public static void registerFamiliar(AbstractFamiliarHolder familiar) {
Expand Down

0 comments on commit 17019c8

Please sign in to comment.