Skip to content

Commit

Permalink
Add slingshot
Browse files Browse the repository at this point in the history
  • Loading branch information
KaptainWutax committed Jan 14, 2025
1 parent f4e68b6 commit b052605
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package abeshutt.staracademy.entity;

import abeshutt.staracademy.init.ModEntities;
import com.cobblemon.mod.common.entity.pokemon.PokemonEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.projectile.thrown.ThrownItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;

public class SlingshotEntity extends ThrownItemEntity {

public SlingshotEntity(EntityType<SlingshotEntity> type, World world) {
super(type, world);
}

public SlingshotEntity(World world, LivingEntity thrower, ItemStack stack) {
super(ModEntities.SLINGSHOT.get(), thrower, world);
this.getDataTracker().set(ITEM, stack);
}

@Override
protected Item getDefaultItem() {
return Items.AIR;
}

protected void onEntityHit(EntityHitResult result) {
super.onEntityHit(result);

if(result.getEntity() instanceof PokemonEntity entity) {
entity.takeKnockback(2.0D, -this.getVelocity().x, -this.getVelocity().z);
this.getWorld().playSound(null, entity.getX(), entity.getY(), entity.getZ(),
SoundEvents.ENTITY_PLAYER_ATTACK_KNOCKBACK, SoundCategory.PLAYERS, 1.0F, 1.2F);
entity.kill();
}
}

protected void onCollision(HitResult result) {
super.onCollision(result);
this.discard();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ModEntities extends ModRegistries {

public static RegistrySupplier<EntityType<StarBadgeEntity>> STAR_BADGE;
public static RegistrySupplier<EntityType<DuelingGloveEntity>> DUELING_GLOVE;
public static RegistrySupplier<EntityType<SlingshotEntity>> SLINGSHOT;
public static RegistrySupplier<EntityType<PartnerNPCEntity>> PARTNER_NPC;
public static RegistrySupplier<EntityType<SafariNPCEntity>> SAFARI_NPC;
public static RegistrySupplier<EntityType<NurseNPCEntity>> NURSE_NPC;
Expand All @@ -25,6 +26,9 @@ public static void register() {
DUELING_GLOVE = register("dueling_glove", DuelingGloveEntity::new, SpawnGroup.MISC,
builder -> builder.setDimensions(0.98F, 0.7F).maxTrackingRange(128));

SLINGSHOT = register("slingshot", SlingshotEntity::new, SpawnGroup.MISC,
builder -> builder.setDimensions(0.98F, 0.7F).maxTrackingRange(128));

PARTNER_NPC = register("partner_npc", PartnerNPCEntity::new, SpawnGroup.MISC,
builder -> builder.setDimensions(0.6F, 1.8F).maxTrackingRange(128));

Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/abeshutt/staracademy/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public class ModItems extends ModRegistries {
public static RegistrySupplier<DuelingGloveItem> DUELING_GLOVE;
public static RegistrySupplier<OutfitItem> OUTFIT;
public static RegistrySupplier<SafariTicketItem> SAFARI_TICKET;
public static RegistrySupplier<SlingshotItem> SLINGSHOT;

public static void register() {
STAR_BADGE = register("star_badge", StarBadgeItem::new);
HUNT = register("hunt", HuntItem::new);
DUELING_GLOVE = register("dueling_glove", DuelingGloveItem::new);
OUTFIT = register("outfit", OutfitItem::new);
SAFARI_TICKET = register("safari_ticket", SafariTicketItem::new);
SLINGSHOT = register("slingshot", SlingshotItem::new);
}

public static <V extends Item> RegistrySupplier<V> register(Identifier id, Supplier<V> item) {
Expand Down
14 changes: 13 additions & 1 deletion common/src/main/java/abeshutt/staracademy/init/ModRenderers.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@ public static void register() {
ProxyModelPredicateProviderRegistry.register(ModItems.DUELING_GLOVE.get(), new Identifier("pulling"), (stack, world, entity, seed) -> {
return entity != null && entity.isUsingItem() && entity.getActiveItem() == stack ? 1.0F : 0.0F;
});
}

ProxyModelPredicateProviderRegistry.register(ModItems.SLINGSHOT.get(), new Identifier("pull"), (stack, world, entity, seed) -> {
if(entity == null) {
return 0.0F;
}

return entity.getActiveItem() != stack ? 0.0F : (float)(stack.getMaxUseTime() - entity.getItemUseTimeLeft()) / 20.0F;
});

ProxyModelPredicateProviderRegistry.register(ModItems.SLINGSHOT.get(), new Identifier("pulling"), (stack, world, entity, seed) -> {
return entity != null && entity.isUsingItem() && entity.getActiveItem() == stack ? 1.0F : 0.0F;
});
}
}

public static class Entities extends ModRenderers {
public static void register() {
ClientLifecycleEvent.CLIENT_SETUP.register(minecraft -> {
EntityRenderers.register(ModEntities.STAR_BADGE.get(), FlyingItemEntityRenderer::new);
EntityRenderers.register(ModEntities.DUELING_GLOVE.get(), FlyingItemEntityRenderer::new);
EntityRenderers.register(ModEntities.SLINGSHOT.get(), FlyingItemEntityRenderer::new);
EntityRenderers.register(ModEntities.PARTNER_NPC.get(), ctx -> new HumanEntityRenderer<>(ctx, false));
EntityRenderers.register(ModEntities.SAFARI_NPC.get(), ctx -> new HumanEntityRenderer<>(ctx, false));
EntityRenderers.register(ModEntities.NURSE_NPC.get(), ctx -> new HumanEntityRenderer<>(ctx, false));
Expand Down
84 changes: 84 additions & 0 deletions common/src/main/java/abeshutt/staracademy/item/SlingshotItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package abeshutt.staracademy.item;

import abeshutt.staracademy.entity.SlingshotEntity;
import abeshutt.staracademy.init.ModItems;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.RangedWeaponItem;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.world.World;

import java.util.function.Predicate;

public class SlingshotItem extends RangedWeaponItem {

public SlingshotItem() {
super(new Settings());
}

@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
if(user instanceof PlayerEntity player) {
float progress = getPullProgress(this.getMaxUseTime(stack) - remainingUseTicks) / 2.0F;

if(progress < 0.1D) {
return;
}

if(!world.isClient) {
SlingshotEntity entity = new SlingshotEntity(world, player, new ItemStack(Items.FIRE_CHARGE));
entity.setVelocity(entity, player.getPitch(), player.getYaw(), 0.0F, progress, 1.0F);
world.spawnEntity(entity);
}

world.playSound(null, player.getX(), player.getY(), player.getZ(),
SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F,
1.0F / (world.getRandom().nextFloat() * 0.4F + 1.2F) + progress * 0.5F);
player.incrementStat(Stats.USED.getOrCreateStat(this));
}
}

public static float getPullProgress(int useTicks) {
float f = (float)useTicks / 20.0F;
f = (f * f + f * 2.0F) / 3.0F;
if (f > 1.0F) {
f = 1.0F;
}

return f;
}

@Override
public int getMaxUseTime(ItemStack stack) {
return 72000;
}

@Override
public UseAction getUseAction(ItemStack stack) {
return UseAction.BOW;
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
user.setCurrentHand(hand);
return TypedActionResult.consume(user.getStackInHand(hand));
}

@Override
public Predicate<ItemStack> getProjectiles() {
return ItemStack::isEmpty;
}

@Override
public int getRange() {
return 15;
}

}
1 change: 1 addition & 0 deletions common/src/main/resources/assets/academy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"item.academy.hunt": "Hunt",
"item.academy.dueling_glove": "Dueling Glove",
"item.academy.safari_ticket": "Safari Ticket",
"item.academy.slingshot": "Slingshot",
"item.academy.outfit": "Outfit",
"item.academy.outfit.classy1_hat": "Classy Hat I",
"item.academy.outfit.classy1_shirt": "Classy Shirt I",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"parent": "item/generated",
"textures": {
"layer0": "academy:item/slingshot"
},
"display": {
"thirdperson_righthand": {
"rotation": [ -80, 260, -40 ],
"translation": [ -1, -2, 2.5 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"thirdperson_lefthand": {
"rotation": [ -80, -280, 40 ],
"translation": [ -1, -2, 2.5 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"firstperson_righthand": {
"rotation": [ 0, -90, 25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
},
"firstperson_lefthand": {
"rotation": [ 0, 90, -25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
}
},
"overrides": [
{
"predicate": {
"pulling": 1
},
"model": "academy:item/slingshot_pulling_0"
},
{
"predicate": {
"pulling": 1,
"pull": 0.65
},
"model": "academy:item/slingshot_pulling_1"
},
{
"predicate": {
"pulling": 1,
"pull": 0.9
},
"model": "academy:item/slingshot_pulling_2"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "academy:item/dueling_glove",
"textures": {
"layer0": "academy:item/slingshot_pulling_0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "academy:item/dueling_glove",
"textures": {
"layer0": "academy:item/slingshot_pulling_1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "academy:item/dueling_glove",
"textures": {
"layer0": "academy:item/slingshot_pulling_2"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b052605

Please sign in to comment.