This commit is contained in:
Bruno Rybársky 2024-07-24 13:28:59 +02:00
parent 97c2285f77
commit 5ea34e9f16
20 changed files with 347 additions and 50 deletions

@ -3,13 +3,25 @@ package systems.brn.plasticgun;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.world.World;
import systems.brn.plasticgun.bullets.BulletEntity;
import systems.brn.plasticgun.bullets.BulletItem;
import systems.brn.plasticgun.guns.Gun;
import systems.brn.plasticgun.lib.EventHandler;
import java.util.ArrayList;
@ -54,7 +66,6 @@ public class PlasticGun implements ModInitializer {
guns.add(new Gun("tokarev_tt_33", 0.6, 2, 8, 42, 762));
BULLET_ENTITY_TYPE = Registry.register(
Registries.ENTITY_TYPE,
id("bullet"),
@ -62,6 +73,13 @@ public class PlasticGun implements ModInitializer {
);
PolymerEntityUtils.registerType(BULLET_ENTITY_TYPE);
// Detect item use
UseItemCallback.EVENT.register(EventHandler::onItemUse);
// Use a custom method to detect general hand swings
// This is done by checking the player's actions in the tick event
ServerTickEvents.END_WORLD_TICK.register(EventHandler::onWorldTick);
PolymerResourcePackUtils.addModAssets(MOD_ID);
PolymerResourcePackUtils.markAsRequired();
}

@ -1,12 +1,18 @@
package systems.brn.plasticgun.bullets;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
@ -18,13 +24,15 @@ import static systems.brn.plasticgun.PlasticGun.bullets;
public class BulletEntity extends PersistentProjectileEntity implements PolymerEntity {
private final Gun gun;
public BulletEntity(Vec3d pos, ServerPlayerEntity player, ItemStack stack, ItemStack weapon, Gun gun, double damage, int speed) {
super(BULLET_ENTITY_TYPE, pos.x, pos.y + 1.5d, pos.z, player.getEntityWorld(), stack, weapon);
this.setOwner(player);
this.setVelocity(player, player.getPitch(), player.getYaw(), 0.0F, speed, 0);
this.pickupType = PickupPermission.CREATIVE_ONLY;
player.setPitch(player.getPitch() + 5);
this.setDamage(damage);
this.setSound(SoundEvents.ENTITY_GENERIC_EXPLODE.value());
this.setSilent(true);
this.gun = gun;
}
@ -35,7 +43,7 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
@Override
protected ItemStack getDefaultItemStack() {
if (gun != null){
if (gun != null) {
return gun.ammo.getFirst().getDefaultStack();
} else {
return bullets.getFirst().getDefaultStack();
@ -47,6 +55,18 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
return EntityType.ARROW;
}
@Override
protected void onBlockHit(BlockHitResult blockHitResult) {
if (blockHitResult.getType() == HitResult.Type.BLOCK) {
BlockState block = this.getWorld().getBlockState(blockHitResult.getBlockPos());
SoundEvent soundEvent = block.getSoundGroup().getHitSound();
setSilent(false);
playSound(soundEvent, 4.0F, 1.0F);
setSilent(true);
}
super.onBlockHit(blockHitResult);
}
@Override
protected void onEntityHit(EntityHitResult entityHitResult) {
Vec3d pos = entityHitResult.getPos();
@ -57,6 +77,9 @@ public class BulletEntity extends PersistentProjectileEntity implements PolymerE
if (entityHitResult.getEntity() instanceof PlayerEntity && height >= 1.75 && height <= 2) {
this.setDamage(2);
}
setSilent(false);
playSound(SoundEvents.BLOCK_BAMBOO_HIT, 4.0F, 1.0F);
setSilent(true);
super.onEntityHit(entityHitResult);
}

@ -3,6 +3,7 @@ package systems.brn.plasticgun.bullets;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import systems.brn.plasticgun.lib.SimpleItem;
@ -13,7 +14,7 @@ public class BulletItem extends SimpleItem {
public final double damageCoefficient;
public final int caliber;
public BulletItem(String path, double damageCoefficient, int caliber) {
super(new Settings().maxCount(99), id(path));
super(new Settings().maxCount(99), id(path), Items.STICK);
this.damageCoefficient = damageCoefficient;
this.caliber = caliber;
Item item = Registry.register(Registries.ITEM, this.identifier, this);

@ -2,15 +2,21 @@ package systems.brn.plasticgun.guns;
import eu.pb4.polymer.core.api.item.PolymerItem;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.StackReference;
import net.minecraft.item.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ClickType;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
@ -35,7 +41,13 @@ public class Gun extends SimpleItem implements PolymerItem {
public final int caliber;
public Gun(String path, double damage, int reloadCount, int clipSize, int speed, int caliber) {
super(new Settings().maxCount(1).component(GUN_AMMO_COMPONENT, ItemStack.EMPTY).maxDamage(clipSize + 1), id(path));
super(
new Settings()
.maxCount(1)
.component(GUN_AMMO_COMPONENT, ItemStack.EMPTY)
.maxDamage(clipSize + 1)
, id(path), Items.WOODEN_HOE
);
Item item = Registry.register(Registries.ITEM, id(path), this);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT).register(content -> content.add(item));
this.damage = damage;
@ -44,7 +56,7 @@ public class Gun extends SimpleItem implements PolymerItem {
this.speed = speed;
ArrayList<Item> ammo = new ArrayList<>();
for (BulletItem bullet : bullets) {
if(bullet.caliber == caliber){
if (bullet.caliber == caliber) {
ammo.add(bullet);
}
}
@ -52,52 +64,85 @@ public class Gun extends SimpleItem implements PolymerItem {
this.caliber = caliber;
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if (context.getPlayer() instanceof ServerPlayerEntity player) {
use(context.getWorld(), player, context.getHand());
public void reload(World world, PlayerEntity user, Hand hand) {
if (user instanceof ServerPlayerEntity player) {
ItemStack stack = user.getStackInHand(hand);
ItemStack bulletStack = findBulletStack(ammo, player);
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY).copy();
int bulletsInChamber = chamber.getCount();
int currentReload = stack.getOrDefault(GUN_LOADING_COMPONENT, 1);
if (bulletStack != null && !bulletStack.isEmpty()) { //we have ammo
if (currentReload < reloadCount) { //still reloading
stack.set(GUN_LOADING_COMPONENT, currentReload + 1);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.BLOCK_COMPARATOR_CLICK, SoundCategory.PLAYERS, 0.1f, 1.8f);
} else if (currentReload == reloadCount) { //now reload
int addedBullets = Math.min(bulletStack.getCount(), clipSize - bulletsInChamber); //how many
if (chamber.isEmpty() || chamber.getItem() == bulletStack.getItem()) {
if (chamber.isEmpty()) {
chamber = bulletStack.copy();
}
chamber.setCount(bulletsInChamber + addedBullets);
bulletStack.decrement(addedBullets);
if (chamber.isEmpty()) {
stack.set(GUN_AMMO_COMPONENT, ItemStack.EMPTY);
} else {
stack.set(GUN_AMMO_COMPONENT, chamber);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.PLAYERS, 0.5f, 1.0f);
}
stack.set(GUN_LOADING_COMPONENT, 1);
} else {
if (canInsertItemIntoInventory(player.getInventory(), chamber.copy()) == chamber.getCount()) { //can take out chamber
insertStackIntoInventory(player.getInventory(), chamber.copy());
chamber.setCount(0); //empty
int targetCount = Math.min(bulletStack.getCount(), clipSize);
chamber = bulletStack.copy();
chamber.setCount(targetCount);
if (chamber.isEmpty()) {
stack.set(GUN_AMMO_COMPONENT, ItemStack.EMPTY);
} else {
stack.set(GUN_AMMO_COMPONENT, chamber);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.PLAYERS, 1f, 2.5f);
}
bulletStack.decrement(targetCount);
stack.set(GUN_LOADING_COMPONENT, 1);
}
}
}
}
if (player.isCreative()) {
stack.set(GUN_AMMO_COMPONENT, new ItemStack(ammo.getFirst(), clipSize)); // Ensure ammo.get(0) is a valid item
}
updateDamage(stack);
}
return super.useOnBlock(context);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
public void updateDamage(ItemStack stack) {
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY).copy();
int numBullets = chamber.getCount();
stack.setDamage(clipSize - numBullets + 1);
}
public void shoot(World world, PlayerEntity user, Hand hand) {
if (user instanceof ServerPlayerEntity player) {
ItemStack stack = user.getStackInHand(hand);
int currentReload = stack.getOrDefault(GUN_LOADING_COMPONENT, 1);
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY).copy();
int numBullets = chamber.getCount();
if (numBullets > 0) {
ItemStack chamber = stack.getOrDefault(GUN_AMMO_COMPONENT, ItemStack.EMPTY);
if (!chamber.isEmpty() && currentReload == 1) {
BulletEntity bulletEntity = new BulletEntity(user.getPos(), player, chamber, user.getStackInHand(hand), this, damage, speed);
world.spawnEntity(bulletEntity);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_GENERIC_EXPLODE.value(), SoundCategory.PLAYERS, 0.1f, 1.2f);
chamber.decrement(1);
stack.set(GUN_AMMO_COMPONENT, chamber);
if (chamber.isEmpty()) {
stack.set(GUN_AMMO_COMPONENT, ItemStack.EMPTY);
} else {
stack.set(GUN_AMMO_COMPONENT, chamber);
}
} else {
ItemStack bulletStack = findBulletStack(ammo, player);
if (bulletStack != null && !bulletStack.isEmpty() && !isCreative(player)) {
if (currentReload < reloadCount) {
stack.set(GUN_LOADING_COMPONENT, currentReload + 1);
} else if (currentReload == reloadCount) {
int addedBullets = Math.min(bulletStack.getCount(), clipSize);
bulletStack.decrement(addedBullets);
ItemStack clipStack = bulletStack.copy();
clipStack.setCount(Math.min(clipStack.getCount(), clipSize));
stack.set(GUN_AMMO_COMPONENT, clipStack);
stack.set(GUN_LOADING_COMPONENT, 1);
}
}
if (player.isCreative()) {
stack.set(GUN_AMMO_COMPONENT, new ItemStack(ammo.getFirst(), clipSize));
}
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.PLAYERS, 1.0f, 2.0f);
}
getDefaultStack().setDamage(clipSize - numBullets);
updateDamage(stack);
}
return super.use(world, user, hand);
}
private static boolean isCreative(ServerPlayerEntity player) {
return player.isCreative();
}
}

@ -0,0 +1,50 @@
package systems.brn.plasticgun.lib;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import systems.brn.plasticgun.guns.Gun;
import static systems.brn.plasticgun.PlasticGun.guns;
public class EventHandler {
public static TypedActionResult<ItemStack> onItemUse(PlayerEntity playerEntity, World world, Hand hand) {
ItemStack stack = playerEntity.getStackInHand(hand);
if (!world.isClient) {
Item stackInHand = playerEntity.getStackInHand(hand).getItem();
for (Gun gun : guns) {
if (gun == stackInHand) {
gun.reload(world, playerEntity, hand);
break;
}
}
}
return TypedActionResult.pass(stack);
}
public static void onWorldTick(World world) {
// Iterate through all players to detect hand swings or item interactions
for (PlayerEntity player : world.getPlayers()) {
if (!world.isClient && player.handSwinging && player.handSwingTicks == 1) {
Hand hand = player.getActiveHand();
Item stackInHand = player.getStackInHand(hand).getItem();
for (Gun gun : guns) {
if (gun == stackInHand) {
gun.shoot(world, player, hand);
break;
}
}
}
}
}
}

@ -13,10 +13,10 @@ public abstract class SimpleItem extends SimplePolymerItem implements PolymerIte
private final PolymerModelData polymerModel;
protected final Identifier identifier;
public SimpleItem(Settings settings, Identifier identifier) {
super(settings, Items.BARRIER);
public SimpleItem(Settings settings, Identifier identifier, Item replacement) {
super(settings, replacement);
this.identifier = identifier;
this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier.withPath("item/" + identifier.getPath()));
this.polymerModel = PolymerResourcePackUtils.requestModel(replacement, identifier.withPath("item/" + identifier.getPath()));
}
@Override

@ -1,5 +1,7 @@
package systems.brn.plasticgun.lib;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
@ -28,4 +30,71 @@ public class Util {
}
return ItemStack.EMPTY;
}
public static int canInsertItemIntoInventory(Inventory inventory, ItemStack itemStack) {
// Get the player's inventory
int maxInsert = 0;
if (inventory instanceof PlayerInventory playerInventory) {
// Iterate through the slots in the player's inventory
for (int i = 0; i < playerInventory.main.size(); i++) {
ItemStack slotStack = playerInventory.main.get(i);
maxInsert = canInsertToStack(slotStack, itemStack, maxInsert);
}
} else {
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
maxInsert = canInsertToStack(slotStack, itemStack, maxInsert);
}
}
return maxInsert; // Return the maximum insertion count
}
public static int canInsertToStack(ItemStack stack1, ItemStack stack2, int maxInsert) {
if (stack1.isEmpty() || ItemStack.areItemsEqual(stack1, stack2)) {
int remainingSpace = stack1.isEmpty() ? stack2.getMaxCount() : stack1.getMaxCount() - stack1.getCount();
maxInsert += remainingSpace;
// If the maximum insertion count is greater than or equal to the item count, return the item count
if (maxInsert >= stack2.getCount()) {
return stack2.getCount();
}
}
return maxInsert;
}
public static boolean canCombine(ItemStack stack1, ItemStack stack2) {
return !stack1.isEmpty() && stack1.getItem() == stack2.getItem() && ItemStack.areItemsAndComponentsEqual(stack1, stack2);
}
public static ItemStack insertStackIntoInventory(Inventory inventory, ItemStack stack) {
// First, try to merge with existing stacks
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
if (canCombine(slotStack, stack)) {
int transferAmount = Math.min(stack.getCount(), slotStack.getMaxCount() - slotStack.getCount());
if (transferAmount > 0) {
slotStack.increment(transferAmount);
stack.decrement(transferAmount);
inventory.markDirty();
if (stack.isEmpty()) {
return ItemStack.EMPTY;
}
}
}
}
// Next, try to find an empty slot
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
if (slotStack.isEmpty()) {
inventory.setStack(i, stack.copy());
stack.setCount(0);
inventory.markDirty();
return ItemStack.EMPTY;
}
}
return stack;
}
}

@ -1,3 +1,22 @@
{
"item.plasticgun.arrowhead": "Central Processing Unit"
"item.bullet.357_magnum": "357 Magnum",
"item.bullet.32_acp_high_velocity": "32 ACP High Velocity",
"item.bullet.45_acp_hollow_point": "45 ACP Hollow Point",
"item.bullet.9mm_jhp": "9mm JHP",
"item.bullet.38_special_p": "38 Special +P",
"item.bullet.762_tokarev_ap": "7.62 Tokarev AP",
"item.bullet.357_standard": "357 Standard",
"item.bullet.32_acp": "32 ACP",
"item.bullet.45_acp": "45 ACP",
"item.bullet.9mm_parabellum": "9mm Parabellum",
"item.bullet.38_special": "38 Special",
"item.bullet.762_tokarev": "7.62 Tokarev",
"item.gun.357_revolver": "357 Revolver",
"item.gun.colt_1903": "Colt 1903",
"item.gun.colt_45": "Colt 45",
"item.gun.colt_peacemaker": "Colt Peacemaker",
"item.gun.p2022": "P2022",
"item.gun.snub_nosed_revolver": "Snub Nosed Revolver",
"item.gun.tokarev_tt_33": "Tokarev TT-33"
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/32_acp"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/32_acp_high_velocity"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/357_magnum"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/357_standard"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/38_special"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/38_special_p"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/45_acp"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/45_acp_hollow_point"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/762_tokarev"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/762_tokarev_ap"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/9mm_jhp"
}
}

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "plasticgun:item/9mm_parabellum"
}
}