fix dispense behaviour

This commit is contained in:
Jenny 2025-02-12 16:18:09 +01:00
parent 301d8b192a
commit fc33eea2e5
Signed by: Jenny
GPG Key ID: 2072A14E40940632

View File

@ -1,12 +1,23 @@
package com.jenny.advancedarrows.items;
import net.minecraft.core.dispenser.ProjectileDispenseBehavior;
import com.jenny.advancedarrows.entities.baseArrow;
import com.jenny.advancedarrows.entities.entities;
import net.minecraft.core.Direction;
import net.minecraft.core.Position;
import net.minecraft.core.dispenser.BlockSource;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import static com.jenny.advancedarrows.advancedArrows.MODID;
@ -28,13 +39,46 @@ public class items {
public static void registerDispenser() {
DispenserBlock.registerBehavior(ARROW_INCENDIARY.get(), new ProjectileDispenseBehavior(items.ARROW_INCENDIARY.get()));
/*DispenserBlock.registerBehavior(ARROW_RICOCHET.get(), new ArrowDispenseBehaviour(entities.ARROW_RICOCHET.get()));
DispenserBlock.registerBehavior(ARROW_KINETIC.get(), new ArrowDispenseBehaviour(entities.ARROW_KINETIC.get()));
DispenserBlock.registerBehavior(ARROW_SHARPENED.get(), new ArrowDispenseBehaviour(entities.ARROW_SHARPENED.get()));
DispenserBlock.registerBehavior(ARROW_SWITCH.get(), new ArrowDispenseBehaviour(entities.ARROW_SWITCH.get()));
DispenserBlock.registerBehavior(ARROW_BREACHING.get(), new ArrowDispenseBehaviour(entities.ARROW_BREACHING.get()));
DispenserBlock.registerBehavior(ARROW_HOMING.get(), new ArrowDispenseBehaviour(entities.ARROW_HOMING.get()));
DispenserBlock.registerBehavior(ARROW_ENDER.get(), new ArrowDispenseBehaviour(entities.ARROW_ENDER.get()));*/
class ArrowDispenseBehaviour extends DefaultDispenseItemBehavior {
private final ArrowAbstract.DispenseConfig dispenseConfig;
private final EntityType<? extends baseArrow> arrowType;
public ArrowDispenseBehaviour(@NotNull ArrowAbstract arrowItem, EntityType<? extends baseArrow> arrow) {
this.dispenseConfig = arrowItem.createDispenseConfig();
this.arrowType = arrow;
}
@Override
public @NotNull ItemStack execute(@NotNull BlockSource pBlockSource, @NotNull ItemStack pItem) {
Level level = pBlockSource.level();
Direction dir = pBlockSource.state().getValue(DispenserBlock.FACING);
Position pos = this.dispenseConfig.positionFunction().getDispensePosition(pBlockSource, dir);
AbstractArrow arrowEntity = arrowType.create(level);
assert arrowEntity != null;
arrowEntity.setPos(new Vec3(pos.x(), pos.y(), pos.z()));
arrowEntity.setDeltaMovement(dir.getStepX(), dir.getStepY(), dir.getStepZ());
level.addFreshEntity(arrowEntity);
pItem.shrink(1);
return pItem;
}
}
DispenserBlock.registerBehavior(ARROW_INCENDIARY.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_INCENDIARY.get(), entities.ARROW_INCENDIARY.get()));
DispenserBlock.registerBehavior(ARROW_RICOCHET.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_RICOCHET.get(), entities.ARROW_RICOCHET.get()));
DispenserBlock.registerBehavior(ARROW_KINETIC.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_KINETIC.get(), entities.ARROW_KINETIC.get()));
DispenserBlock.registerBehavior(ARROW_SHARPENED.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_SHARPENED.get(), entities.ARROW_SHARPENED.get()));
DispenserBlock.registerBehavior(ARROW_SWITCH.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_SWITCH.get(), entities.ARROW_SWITCH.get()));
DispenserBlock.registerBehavior(ARROW_BREACHING.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_BREACHING.get(), entities.ARROW_BREACHING.get()));
DispenserBlock.registerBehavior(ARROW_HOMING.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_HOMING.get(), entities.ARROW_HOMING.get()));
DispenserBlock.registerBehavior(ARROW_ENDER.get(), new ArrowDispenseBehaviour(
(ArrowAbstract) items.ARROW_ENDER.get(), entities.ARROW_ENDER.get()));
}
}