generated from TropheusJ/fabric-example-mod
-
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.
- Loading branch information
Showing
8 changed files
with
288 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.tropheusj.krill; | ||
|
||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry; | ||
import net.minecraft.client.renderer.entity.NoopRenderer; | ||
import net.minecraft.client.renderer.item.ItemProperties; | ||
import net.minecraft.core.component.DataComponents; | ||
import net.minecraft.world.item.Items; | ||
import net.minecraft.world.item.component.ChargedProjectiles; | ||
|
||
public class KrillClient implements ClientModInitializer { | ||
@Override | ||
public void onInitializeClient() { | ||
ItemProperties.register(Items.CROSSBOW, Krill.id("krill"), (stack, level, entity, i) -> { | ||
ChargedProjectiles projectiles = stack.get(DataComponents.CHARGED_PROJECTILES); | ||
return projectiles != null && projectiles.contains(Items.FIREWORK_ROCKET) ? 1 : 0; | ||
}); | ||
|
||
EntityRendererRegistry.register(Krill.KRILL_TYPE, NoopRenderer::new); | ||
EntityRendererRegistry.register(Krill.KRILL_ARROW_TYPE, NoopRenderer::new); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/io/github/tropheusj/krill/arrow/KrillArrowEntity.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,56 @@ | ||
package io.github.tropheusj.krill.arrow; | ||
|
||
import io.github.tropheusj.krill.Krill; | ||
import io.github.tropheusj.krill.entity.KrillEntity; | ||
import io.github.tropheusj.krill.entity.Krilling; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.entity.projectile.AbstractArrow; | ||
import net.minecraft.world.entity.projectile.Arrow; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.Items; | ||
import net.minecraft.world.level.Level; | ||
|
||
import net.minecraft.world.phys.BlockHitResult; | ||
|
||
import net.minecraft.world.phys.EntityHitResult; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class KrillArrowEntity extends AbstractArrow { | ||
public KrillArrowEntity(EntityType<? extends KrillArrowEntity> entityType, Level level) { | ||
super(entityType, level); | ||
} | ||
|
||
@Override | ||
protected void onHitBlock(BlockHitResult hit) { | ||
super.onHitBlock(hit); | ||
// free him | ||
BlockPos offset = hit.getBlockPos().relative(hit.getDirection()); | ||
KrillEntity krill = Krill.KRILL_TYPE.create(this.level()); | ||
if (krill != null) { | ||
krill.moveTo(offset, 0, 0); | ||
this.level().addFreshEntity(krill); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onHitEntity(EntityHitResult hit) { | ||
super.onHitEntity(hit); | ||
if (this.level().isClientSide) | ||
return; | ||
if (this.ownedBy(hit.getEntity())) { | ||
// krilled self | ||
} | ||
if (hit.getEntity() instanceof LivingEntity living) | ||
Krilling.transform(living); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
protected ItemStack getDefaultPickupItem() { | ||
return new ItemStack(Items.ARROW); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/io/github/tropheusj/krill/command/KrillCommand.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,52 @@ | ||
package io.github.tropheusj.krill.command; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterators; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
|
||
import io.github.tropheusj.krill.Krill; | ||
import io.github.tropheusj.krill.entity.Krilling; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.commands.arguments.EntityArgument; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.LivingEntity; | ||
|
||
import java.util.Collection; | ||
|
||
import static net.minecraft.commands.Commands.argument; | ||
import static net.minecraft.commands.Commands.literal; | ||
|
||
public class KrillCommand { | ||
public static LiteralArgumentBuilder<CommandSourceStack> build() { | ||
return literal("kill") | ||
.requires(source -> source.hasPermission(2)) | ||
.executes(ctx -> krill(ctx.getSource(), ImmutableList.of(ctx.getSource().getEntityOrException()))) | ||
.then(argument("targets", EntityArgument.entities()) | ||
.executes(ctx -> krill(ctx.getSource(), EntityArgument.getEntities(ctx, "targets"))) | ||
); | ||
} | ||
|
||
private static int krill(CommandSourceStack source, Collection<? extends Entity> collection) { | ||
for (Entity entity : collection) { | ||
if (entity instanceof LivingEntity living) | ||
Krilling.transform(living); | ||
} | ||
|
||
Component feedback = getFeedback(collection); | ||
source.sendSuccess(() -> feedback, true); | ||
return collection.size(); | ||
} | ||
|
||
private static Component getFeedback(Collection<? extends Entity> entities) { | ||
int count = entities.size(); | ||
if (count == 1) { | ||
Entity entity = entities.iterator().next(); | ||
Component name = entity.getDisplayName(); | ||
return Component.translatable("commands.krill.success.single", name); | ||
} else { | ||
return Component.translatable("commands.krill.success.multiple", count); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/tropheusj/krill/entity/KrillEntity.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,45 @@ | ||
package io.github.tropheusj.krill.entity; | ||
|
||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.AgeableMob; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.ExperienceOrb; | ||
import net.minecraft.world.entity.npc.AbstractVillager; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.Items; | ||
import net.minecraft.world.item.trading.ItemCost; | ||
import net.minecraft.world.item.trading.MerchantOffer; | ||
import net.minecraft.world.level.Level; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class KrillEntity extends AbstractVillager { | ||
public KrillEntity(EntityType<? extends KrillEntity> type, Level level) { | ||
super(type, level); | ||
} | ||
|
||
@Override | ||
protected void rewardTradeXp(MerchantOffer merchantOffer) { | ||
if (merchantOffer.shouldRewardExp()) { | ||
int i = 3 + this.random.nextInt(4); | ||
this.level().addFreshEntity(new ExperienceOrb( | ||
this.level(), this.getX(), this.getY() + 0.5, this.getZ(), i | ||
)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void updateTrades() { | ||
this.getOffers().add(new MerchantOffer( | ||
new ItemCost(Items.AMBER_GEM), | ||
new ItemStack(Items.POISONOUS_POTATO_FRIES), | ||
7, 1, 1 | ||
)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public AgeableMob getBreedOffspring(ServerLevel serverLevel, AgeableMob ageableMob) { | ||
return null; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/io/github/tropheusj/krill/entity/Krilling.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,63 @@ | ||
package io.github.tropheusj.krill.entity; | ||
|
||
import io.github.tropheusj.krill.Krill; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.particles.ParticleTypes; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.world.effect.MobEffect; | ||
import net.minecraft.world.effect.MobEffectInstance; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
public class Krilling { | ||
public static void transform(LivingEntity entity) { | ||
if (!(entity.level() instanceof ServerLevel level)) | ||
throw new IllegalStateException(); | ||
|
||
// particles | ||
AABB bounds = entity.getBoundingBox(); | ||
BlockPos.betweenClosedStream(bounds).forEach(pos -> { | ||
Vec3 center = Vec3.atCenterOf(pos); | ||
level.sendParticles( | ||
ParticleTypes.POOF, | ||
center.x, center.y, center.z, | ||
7, // count | ||
0.5, 0.5, 0.5, // offset | ||
0.1 // max speed | ||
); | ||
}); | ||
|
||
// sound | ||
level.playSound( | ||
null, entity.blockPosition(), | ||
SoundEvents.ZOMBIE_VILLAGER_CURE, | ||
entity.getSoundSource(), | ||
1 + entity.getRandom().nextFloat(), | ||
(float) (entity.getRandom().nextFloat() * 0.7 + 0.3) | ||
); | ||
|
||
if (entity instanceof ServerPlayer player) { | ||
transformPlayer(player); | ||
return; | ||
} | ||
|
||
// replace existing entity | ||
KrillEntity krill = Krill.KRILL_TYPE.create(level); | ||
if (krill != null) { | ||
krill.setPos(entity.position()); | ||
level.addFreshEntity(krill); | ||
entity.discard(); | ||
} | ||
} | ||
|
||
private static void transformPlayer(ServerPlayer player) { | ||
// apply status effect | ||
Holder<MobEffect> holder = BuiltInRegistries.MOB_EFFECT.wrapAsHolder(Krill.KRILLED); | ||
player.addEffect(new MobEffectInstance(holder, 30 * 20)); | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"commands.krill.success.single": "Krilled %s", | ||
"commands.krill.success.multiple": "Krilled %s entities" | ||
} |
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