Skip to content

Commit

Permalink
make castingimplement function based on component
Browse files Browse the repository at this point in the history
  • Loading branch information
iron431 committed Oct 17, 2024
1 parent 05c40ba commit 855dc0f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@ private static HitResult internalRaycastForEntity(Level level, Entity originEnti
List<? extends Entity> entities = level.getEntities(originEntity, range, filter);
for (Entity target : entities) {
HitResult hit = checkEntityIntersecting(target, start, end, bbInflation);
if (hit.getType() != HitResult.Type.MISS)
if (hit.getType() != HitResult.Type.MISS) {
hits.add(hit);
}
}

if (!hits.isEmpty()) {
Expand Down
54 changes: 3 additions & 51 deletions src/main/java/io/redspace/ironsspellbooks/item/CastingItem.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
package io.redspace.ironsspellbooks.item;

import io.redspace.ironsspellbooks.api.magic.SpellSelectionManager;
import io.redspace.ironsspellbooks.api.spells.SpellData;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.item.weapons.IMultihandWeapon;
import io.redspace.ironsspellbooks.player.ClientMagicData;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.util.Unit;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.Level;

import java.util.List;
import static io.redspace.ironsspellbooks.registries.ComponentRegistry.CASTING_IMPLEMENT;

public class CastingItem extends Item implements IMultihandWeapon {
public CastingItem(Properties pProperties) {
super(pProperties);
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
ItemStack itemStack = player.getItemInHand(hand);
SpellSelectionManager spellSelectionManager = new SpellSelectionManager(player);
SpellSelectionManager.SelectionOption selectionOption = spellSelectionManager.getSelection();
if (selectionOption == null || selectionOption.spellData.equals(SpellData.EMPTY)) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.1 {} {}", level.isClientSide, hand);
return InteractionResultHolder.pass(itemStack);
}
SpellData spellData = selectionOption.spellData;
int spellLevel = spellData.getSpell().getLevelFor(spellData.getLevel(), player);
if (level.isClientSide()) {
if (ClientMagicData.isCasting()) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.2 {} {}", level.isClientSide, hand);
return InteractionResultHolder.consume(itemStack);
} else if (ClientMagicData.getPlayerMana() < spellData.getSpell().getManaCost(spellLevel)
|| ClientMagicData.getCooldowns().isOnCooldown(spellData.getSpell())
|| !ClientMagicData.getSyncedSpellData(player).isSpellLearned(spellData.getSpell())) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.3 {} {}", level.isClientSide, hand);
//todo: initiate start using item on the client side to prevent the aternos bug?
return InteractionResultHolder.pass(itemStack);
} else {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.4 {} {}", level.isClientSide, hand);
return InteractionResultHolder.consume(itemStack);
}
}

var castingSlot = hand.ordinal() == 0 ? SpellSelectionManager.MAINHAND : SpellSelectionManager.OFFHAND;

if (spellData.getSpell().attemptInitiateCast(itemStack, spellLevel, level, player, selectionOption.getCastSource(), true, castingSlot)) {
return InteractionResultHolder.consume(itemStack);
} else {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.6 {} {}", level.isClientSide, hand);
return InteractionResultHolder.fail(itemStack);
}
super(pProperties.component(CASTING_IMPLEMENT, Unit.INSTANCE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import io.redspace.ironsspellbooks.effect.CustomDescriptionMobEffect;
import io.redspace.ironsspellbooks.effect.guiding_bolt.GuidingBoltManager;
import io.redspace.ironsspellbooks.entity.mobs.dead_king_boss.DeadKingMusicManager;
import io.redspace.ironsspellbooks.item.CastingItem;
import io.redspace.ironsspellbooks.item.Scroll;
import io.redspace.ironsspellbooks.item.SpellBook;
import io.redspace.ironsspellbooks.item.weapons.IMultihandWeapon;
import io.redspace.ironsspellbooks.network.casting.CancelCastPacket;
import io.redspace.ironsspellbooks.registries.ComponentRegistry;
import io.redspace.ironsspellbooks.registries.MobEffectRegistry;
import io.redspace.ironsspellbooks.render.SpellRenderingHelper;
import io.redspace.ironsspellbooks.spells.blood.RayOfSiphoningSpell;
Expand Down Expand Up @@ -190,7 +190,7 @@ public static void imbuedWeaponTooltips(ItemTooltipEvent event) {
var lines = event.getToolTip();
boolean advanced = event.getFlags().isAdvanced();
// Active Spell Tooltip
if (stack.getItem() instanceof CastingItem) {
if (stack.has(ComponentRegistry.CASTING_IMPLEMENT)) {
handleCastingImplementTooltip(stack, player, lines, advanced);
}
// Imbued Spell Tooltip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.redspace.ironsspellbooks.api.events.SpellTeleportEvent;
import io.redspace.ironsspellbooks.api.item.UpgradeData;
import io.redspace.ironsspellbooks.api.magic.MagicData;
import io.redspace.ironsspellbooks.api.magic.SpellSelectionManager;
import io.redspace.ironsspellbooks.api.registry.AttributeRegistry;
import io.redspace.ironsspellbooks.api.spells.CastSource;
import io.redspace.ironsspellbooks.api.spells.CastType;
import io.redspace.ironsspellbooks.api.spells.ISpellContainer;
import io.redspace.ironsspellbooks.api.spells.SpellData;
import io.redspace.ironsspellbooks.api.util.CameraShakeManager;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.block.BloodCauldronBlock;
Expand All @@ -29,6 +31,7 @@
import io.redspace.ironsspellbooks.network.EquipmentChangedPacket;
import io.redspace.ironsspellbooks.network.SyncManaPacket;
import io.redspace.ironsspellbooks.registries.BlockRegistry;
import io.redspace.ironsspellbooks.registries.ComponentRegistry;
import io.redspace.ironsspellbooks.registries.ItemRegistry;
import io.redspace.ironsspellbooks.registries.MobEffectRegistry;
import io.redspace.ironsspellbooks.util.MinecraftInstanceHelper;
Expand All @@ -46,6 +49,7 @@
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.tags.EntityTypeTags;
import net.minecraft.util.StringUtil;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.*;
Expand Down Expand Up @@ -97,6 +101,65 @@ public class ServerPlayerEvents {
// }
// }


@SubscribeEvent
public static void onUseItem(PlayerInteractEvent.RightClickItem event) {
var player = event.getEntity();
if (player.level.isClientSide) {
MinecraftInstanceHelper.ifPlayerPresent(localPlayer -> {
if (ClientMagicData.isCasting() && player.getUUID().equals(localPlayer.getUUID())) {
event.setCanceled(true);
}
});
} else {
var magicData = MagicData.getPlayerMagicData(player);
if (magicData.isCasting() && event.getItemStack() != magicData.getPlayerCastingItem()) {
event.setCanceled(true);
}
}
if (event.isCanceled()) {
return;
}

var level = player.level;
var hand = event.getHand();
ItemStack itemStack = player.getItemInHand(hand);
if (itemStack.has(ComponentRegistry.CASTING_IMPLEMENT)) {
SpellSelectionManager spellSelectionManager = new SpellSelectionManager(player);
SpellSelectionManager.SelectionOption selectionOption = spellSelectionManager.getSelection();
if (selectionOption == null || selectionOption.spellData.equals(SpellData.EMPTY)) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.1 {} {}", level.isClientSide, hand);
return;
}
SpellData spellData = selectionOption.spellData;
int spellLevel = spellData.getSpell().getLevelFor(spellData.getLevel(), player);
if (level.isClientSide()) {
if (ClientMagicData.isCasting()) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.2 {} {}", level.isClientSide, hand);
event.setCancellationResult(InteractionResult.CONSUME);
} else if (ClientMagicData.getPlayerMana() < spellData.getSpell().getManaCost(spellLevel)
|| ClientMagicData.getCooldowns().isOnCooldown(spellData.getSpell())
|| !ClientMagicData.getSyncedSpellData(player).isSpellLearned(spellData.getSpell())) {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.3 {} {}", level.isClientSide, hand);
return;
} else {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.4 {} {}", level.isClientSide, hand);
event.setCancellationResult(InteractionResult.CONSUME);
}
}

var castingSlot = hand.ordinal() == 0 ? SpellSelectionManager.MAINHAND : SpellSelectionManager.OFFHAND;

if (spellData.getSpell().attemptInitiateCast(itemStack, spellLevel, level, player, selectionOption.getCastSource(), true, castingSlot)) {
event.setCancellationResult(InteractionResult.CONSUME);
} else {
//IronsSpellbooks.LOGGER.debug("CastingItem.Use.6 {} {}", level.isClientSide, hand);
event.setCancellationResult(InteractionResult.FAIL);
}
event.setCanceled(true);
}
}

@SubscribeEvent
public static void onPlayerDropItem(ItemTossEvent event) {
var itemStack = event.getEntity().getItem();
Expand Down Expand Up @@ -464,23 +527,6 @@ public static void useOnEntityEvent(PlayerInteractEvent.EntityInteractSpecific e
}
}

@SubscribeEvent
public static void useItemEvent(PlayerInteractEvent.RightClickItem event) {
var entity = event.getEntity();
if (entity.level.isClientSide) {
MinecraftInstanceHelper.ifPlayerPresent(localPlayer -> {
if (ClientMagicData.isCasting() && entity.getUUID().equals(localPlayer.getUUID())) {
event.setCanceled(true);
}
});
} else {
var magicData = MagicData.getPlayerMagicData(entity);
if (magicData.isCasting() && event.getItemStack() != magicData.getPlayerCastingItem()) {
event.setCanceled(true);
}
}
}

@SubscribeEvent
public static void handleResistanceAttributesOnSpawn(FinalizeSpawnEvent event) {
var mob = event.getEntity();
Expand Down Expand Up @@ -566,7 +612,7 @@ public static void changeDigSpeed(PlayerEvent.BreakSpeed event) {
}

@SubscribeEvent
public static void changeBreedOutcome(BabyEntitySpawnEvent event){
public static void changeBreedOutcome(BabyEntitySpawnEvent event) {
if (ServerConfigs.HOGLIN_OFFSPRING_PROTECTION.get()) {
if (event.getChild() instanceof Hoglin baby && event.getParentA() instanceof Hoglin parent1 && event.getParentB() instanceof Hoglin parent2) {
double i = (parent1.isImmuneToZombification() ? 0.5 : 0) + (parent2.isImmuneToZombification() ? 0.5 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.redspace.ironsspellbooks.item.weapons.AutoloaderCrossbow;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.Unit;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
Expand All @@ -33,4 +35,5 @@ private static <T> DeferredHolder<DataComponentType<?>, DataComponentType<T>> re
public static final DeferredHolder<DataComponentType<?>, DataComponentType<ISpellContainer>> SPELL_CONTAINER = register("spell_container", (builder) -> builder.persistent(SpellContainer.CODEC).networkSynchronized(SpellContainer.STREAM_CODEC).cacheEncoding());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<WaywardCompassData>> WAYWARD_COMPASS = register("wayward_compass", (builder) -> builder.persistent(WaywardCompassData.CODEC).networkSynchronized(WaywardCompassData.STREAM_CODEC).cacheEncoding());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<AutoloaderCrossbow.LoadStateComponent>> CROSSBOW_LOAD_STATE = register("crossbow_load_state", (builder) -> builder.persistent(AutoloaderCrossbow.LoadStateComponent.CODEC).networkSynchronized(AutoloaderCrossbow.LoadStateComponent.STREAM_CODEC).cacheEncoding());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> CASTING_IMPLEMENT = register("casting_implement", (builder) -> builder.persistent(Unit.CODEC).networkSynchronized(StreamCodec.unit(Unit.INSTANCE)).cacheEncoding());
}

0 comments on commit 855dc0f

Please sign in to comment.