Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabric's Projectile Events + TestSkill #23

Merged
merged 11 commits into from
Jan 3, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ gradle-app.setting

# Common working directory
run/
runServer/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public boolean isMastered(ManasSkillInstance instance, LivingEntity entity) {
public void addMasteryPoint(ManasSkillInstance instance, LivingEntity entity) {
if (isMastered(instance, entity)) return;
instance.setMastery(instance.getMastery() + 1);
if (isMastered(instance, entity)) onSkillMastered(instance, entity);
if (isMastered(instance, entity)) instance.onSkillMastered(entity);
}

/**
Expand Down Expand Up @@ -197,7 +197,7 @@ public void onTick(ManasSkillInstance instance, LivingEntity living) {
* @param instance Affected {@link ManasSkillInstance}
* @param entity Affected {@link LivingEntity} owning this Skill.
*/
public void onPressed(ManasSkillInstance instance, LivingEntity entity) {
public void onPressed(ManasSkillInstance instance, LivingEntity entity, int keyNumber) {
// Override this method to add your own logic
}

Expand All @@ -219,7 +219,7 @@ public boolean onHeld(ManasSkillInstance instance, LivingEntity living, int held
* @param instance Affected {@link ManasSkillInstance}
* @param entity Affected {@link LivingEntity} owning this Skill.
*/
public void onRelease(ManasSkillInstance instance, LivingEntity entity, int keyNumber, int heldTicks) {
public void onRelease(ManasSkillInstance instance, LivingEntity entity, int heldTicks, int keyNumber) {
// Override this method to add your own logic
}

Expand Down Expand Up @@ -266,9 +266,9 @@ public void onRightClickBlock(ManasSkillInstance instance, Player player, Intera
/**
* Called when the {@link LivingEntity} owning this Skill starts to be targeted by a mob.
*
* @see ManasSkillInstance#onBeingTargeted(Changeable)
* @see ManasSkillInstance#onBeingTargeted(Changeable, LivingEntity)
*/
public boolean onBeingTargeted(ManasSkillInstance instance, Changeable<LivingEntity> target) {
public boolean onBeingTargeted(ManasSkillInstance instance, Changeable<LivingEntity> target, LivingEntity owner) {
// Override this method to add your own logic
return true;
}
Expand All @@ -286,19 +286,19 @@ public boolean onBeingDamaged(ManasSkillInstance instance, LivingEntity entity,
/**
* Called when the {@link LivingEntity} owning this Skill damage another {@link LivingEntity}.
*
* @see ManasSkillInstance#onDamageEntity(LivingEntity, DamageSource, Changeable)
* @see ManasSkillInstance#onDamageEntity(LivingEntity, LivingEntity, DamageSource, Changeable)
*/
public boolean onDamageEntity(ManasSkillInstance instance, LivingEntity owner, DamageSource source, Changeable<Float> amount) {
public boolean onDamageEntity(ManasSkillInstance instance, LivingEntity owner, LivingEntity target, DamageSource source, Changeable<Float> amount) {
// Override this method to add your own logic
return true;
}

/**
* Called when the {@link LivingEntity} owning this Skill damage another {@link LivingEntity},
*
* @see ManasSkillInstance#onTouchEntity(LivingEntity, DamageSource, Changeable)
* @see ManasSkillInstance#onTouchEntity(LivingEntity, LivingEntity, DamageSource, Changeable)
*/
public boolean onTouchEntity(ManasSkillInstance instance, LivingEntity entity, DamageSource source, Changeable<Float> amount) {
public boolean onTouchEntity(ManasSkillInstance instance, LivingEntity owner, LivingEntity target, DamageSource source, Changeable<Float> amount) {
// Override this method to add your own logic
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ManasSkillInstance {
private boolean dirty = false;
protected final RegistrySupplier<ManasSkill> skillRegistryObject;

public ManasSkillInstance(ManasSkill skill) {
protected ManasSkillInstance(ManasSkill skill) {
this.skillRegistryObject = SkillAPI.getSkillRegistry().delegate(SkillAPI.getSkillRegistry().getId(skill));
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public void deserialize(CompoundTag tag) {
* <p>
* The {@link CompoundTag} has to be created though {@link ManasSkillInstance#toNBT()}
*/
public static ManasSkillInstance fromNBT(CompoundTag tag) {
public static ManasSkillInstance fromNBT(CompoundTag tag) throws NullPointerException {
ResourceLocation skillLocation = ResourceLocation.tryParse(tag.getString("skill"));
ManasSkillInstance instance = Objects.requireNonNull(SkillAPI.getSkillRegistry().get(skillLocation)).createDefaultInstance();
instance.deserialize(tag);
Expand Down Expand Up @@ -388,8 +388,8 @@ public void onTick(LivingEntity living) {
*
* @param entity Affected {@link LivingEntity} owning this instance.
*/
public void onPressed(LivingEntity entity) {
this.getSkill().onPressed(this, entity);
public void onPressed(LivingEntity entity, int keyNumber) {
this.getSkill().onPressed(this, entity, keyNumber);
}

/**
Expand All @@ -408,8 +408,8 @@ public boolean onHeld(LivingEntity entity, int heldTicks) {
* @param entity Affected {@link LivingEntity} owning this instance.
* @param heldTicks - the number of ticks the skill activation button is held down.
*/
public void onRelease(LivingEntity entity, int keyNumber, int heldTicks) {
this.getSkill().onRelease(this, entity, keyNumber, heldTicks);
public void onRelease(LivingEntity entity, int heldTicks, int keyNumber) {
this.getSkill().onRelease(this, entity, heldTicks, keyNumber);
}

/**
Expand Down Expand Up @@ -454,8 +454,8 @@ public void onRightClickBlock(Player player, InteractionHand hand, BlockPos pos,
*
* @return false will stop the mob from targeting the owner.
*/
public boolean onBeingTargeted(Changeable<LivingEntity> target) {
return this.getSkill().onBeingTargeted(this, target);
public boolean onBeingTargeted(Changeable<LivingEntity> owner, LivingEntity mob) {
return this.getSkill().onBeingTargeted(this, owner, mob);
}

/**
Expand All @@ -470,27 +470,27 @@ public boolean onBeingDamaged(LivingEntity entity, DamageSource source, float am
}

/**
* Called when the {@link LivingEntity} owning this instance gets hurt.
* Called when the {@link LivingEntity} owning this instance starts attacking another {@link LivingEntity}.
* <p>
* Gets executed after {@link ManasSkillInstance#onBeingDamaged}<br>
* Gets executed before {@link ManasSkillInstance#onTouchEntity}
*
* @return false will prevent the owner from taking damage.
* @return false will prevent the owner from dealing damage
*/
public boolean onDamageEntity(LivingEntity owner, DamageSource source, Changeable<Float> amount) {
return this.getSkill().onDamageEntity(this, owner, source, amount);
public boolean onDamageEntity(LivingEntity owner, LivingEntity target, DamageSource source, Changeable<Float> amount) {
return this.getSkill().onDamageEntity(this, owner, target, source, amount);
}

/**
* Called when the {@link LivingEntity} owning this instance gets hurt (after effects like Barriers are consumed the damage amount).
* Called when the {@link LivingEntity} owning this instance hurts another {@link LivingEntity} (after effects like Barriers are consumed the damage amount).
* <p>
* Gets executed after {@link ManasSkillInstance#onDamageEntity}
* Gets executed before {@link ManasSkillInstance#onTakenDamage}
*
* @return false will prevent the owner from taking damage.
* @return false will prevent the owner from dealing damage.
*/
public boolean onTouchEntity(LivingEntity owner, DamageSource source, Changeable<Float> amount) {
return this.getSkill().onTouchEntity(this, owner, source, amount);
public boolean onTouchEntity(LivingEntity owner, LivingEntity target, DamageSource source, Changeable<Float> amount) {
return this.getSkill().onTouchEntity(this, owner, target, source, amount);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.manasmods.manascore.skill.SkillStorage;
import lombok.NonNull;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Optional;
Expand All @@ -15,15 +16,31 @@ public interface Skills {

void updateSkill(ManasSkillInstance updatedInstance, boolean sync);

default boolean learnSkill(@NotNull ResourceLocation skillId) {
return learnSkill(SkillAPI.getSkillRegistry().get(skillId).createDefaultInstance());
}

default boolean learnSkill(@NonNull ManasSkill skill) {
return learnSkill(skill.createDefaultInstance());
}

boolean learnSkill(ManasSkillInstance instance);

Optional<ManasSkillInstance> getSkill(@NonNull ResourceLocation skillId);
Optional<ManasSkillInstance> getSkill(@NotNull ResourceLocation skillId);

default Optional<ManasSkillInstance> getSkill(@NonNull ManasSkill skill) {
return getSkill(skill.getRegistryName());
}

void forgetSkill(ManasSkillInstance instance);
void forgetSkill(@NotNull ResourceLocation skillId);

default void forgetSkill(@NonNull ManasSkill skill) {
forgetSkill(skill.getRegistryName());
}

default void forgetSkill(@NonNull ManasSkillInstance instance) {
forgetSkill(instance.getSkillId());
}

void forEachSkill(BiConsumer<SkillStorage, ManasSkillInstance> skillInstanceConsumer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface ProjectileHitEvent {
}

enum ProjectileHitResult {
DEFUALT, // Hit, damage + possibly continue
DEFAULT, // Hit, damage + possibly continue
HIT, // Hit + damage
HIT_NO_DAMAGE, // Hit
PASS // Pass through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void handle(Supplier<PacketContext> contextSupplier) {
storage.getSkill(skillId).ifPresent(skill -> {
if(!skill.canInteractSkill(player)) return;
if (skill.onCoolDown() && !skill.canIgnoreCoolDown(player)) return;
skill.onPressed(player);
skill.onPressed(player, keyNumber);
storage.markDirty();

SkillStorage.tickingSkills.put(player.getUUID(), new TickingSkill(skill.getSkill()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void handle(Supplier<PacketContext> contextSupplier) {
context.queue(() -> {
Player player = context.getPlayer();
if (player == null) return;
StorageManager.getStorage(player, SkillStorage.getKey()).handleSkillRelease(skillList, keyNumber, heldTick);
StorageManager.getStorage(player, SkillStorage.getKey()).handleSkillRelease(skillList, heldTick, keyNumber);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public static void init() {
EntityEvents.LIVING_CHANGE_TARGET.register((entity, changeableTarget) -> {
if (!changeableTarget.isPresent()) return EventResult.pass();

for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(changeableTarget.get()).getLearnedSkills()) {
if (!instance.canInteractSkill(entity)) continue;
if (!instance.onBeingTargeted(changeableTarget)) return EventResult.interruptFalse();
LivingEntity owner = changeableTarget.get();
for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) {
if (!instance.canInteractSkill(owner)) continue;
if (!instance.onBeingTargeted(changeableTarget, entity)) return EventResult.interruptFalse();
}

return EventResult.pass();
Expand All @@ -55,19 +56,23 @@ public static void init() {
return EventResult.pass();
});

SkillEvents.SKILL_DAMAGE_PRE_CALCULATION.register((storage, entity, source, amount) -> {
for (ManasSkillInstance instance : storage.getLearnedSkills()) {
if (!instance.canInteractSkill(entity)) continue;
if (!instance.onDamageEntity(entity, source, amount)) return EventResult.interruptFalse();
SkillEvents.SKILL_DAMAGE_PRE_CALCULATION.register((storage, target, source, amount) -> {
if (!(source.getEntity() instanceof LivingEntity owner)) return EventResult.pass();

for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) {
if (!instance.canInteractSkill(owner)) continue;
if (!instance.onDamageEntity(owner, target, source, amount)) return EventResult.interruptFalse();
}

return EventResult.pass();
});

SkillEvents.SKILL_DAMAGE_POST_CALCULATION.register((storage, entity, source, amount) -> {
for (ManasSkillInstance instance : storage.getLearnedSkills()) {
if (!instance.canInteractSkill(entity)) continue;
if (!instance.onTouchEntity(entity, source, amount)) return EventResult.interruptFalse();
SkillEvents.SKILL_DAMAGE_POST_CALCULATION.register((storage, target, source, amount) -> {
if (!(source.getEntity() instanceof LivingEntity owner)) return EventResult.pass();

for (ManasSkillInstance instance : SkillAPI.getSkillsFrom(owner).getLearnedSkills()) {
if (!instance.canInteractSkill(owner)) continue;
if (!instance.onTouchEntity(owner, target, source, amount)) return EventResult.interruptFalse();
}

return EventResult.pass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -56,6 +57,7 @@ public static void init() {

private static void handleSkillTick(LivingEntity entity, Level level, Skills storage) {
MinecraftServer server = level.getServer();
if (server == null) return;

boolean shouldPassiveConsume = server.getTickCount() % INSTANCE_UPDATE == 0;
if (!shouldPassiveConsume) return;
Expand Down Expand Up @@ -147,8 +149,9 @@ public Optional<ManasSkillInstance> getSkill(@NonNull ResourceLocation skillId)
return Optional.ofNullable(this.skillInstances.get(skillId));
}

public void forgetSkill(ManasSkillInstance instance) {
if (!this.skillInstances.containsKey(instance.getSkillId())) return;
public void forgetSkill(@NotNull ResourceLocation skillId) {
if (!this.skillInstances.containsKey(skillId)) return;
ManasSkillInstance instance = this.skillInstances.get(skillId);

EventResult result = SkillEvents.REMOVE_SKILL.invoker().removeSkill(instance, getOwner());
if (result.isFalse()) return;
Expand All @@ -164,12 +167,12 @@ public void forEachSkill(BiConsumer<SkillStorage, ManasSkillInstance> skillInsta
markDirty();
}

public void handleSkillRelease(List<ResourceLocation> skillList, int keyNumber, int heldTick) {
public void handleSkillRelease(List<ResourceLocation> skillList, int heldTick, int keyNumber) {
for (final ResourceLocation skillId : skillList) {
getSkill(skillId).ifPresent(skill -> {
if (!skill.canInteractSkill(getOwner())) return;
if (skill.onCoolDown() && !skill.canIgnoreCoolDown(getOwner())) return;
skill.onRelease(getOwner(), keyNumber, heldTick);
skill.onRelease(getOwner(), heldTick, keyNumber);
if (skill.isDirty()) markDirty();

UUID ownerID = getOwner().getUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.manasmods.manascore.api.client.keybinding.KeybindingCategory;
import com.github.manasmods.manascore.api.client.keybinding.KeybindingEvents;
import com.github.manasmods.manascore.api.client.keybinding.ManasKeybinding;
import com.github.manasmods.manascore.api.skill.SkillAPI;

public class KeybindingTest {
public static void init() {
Expand All @@ -17,6 +18,11 @@ public static void init() {

registry.create(new ManasKeybinding("manascore.keybinding.test_press", category, () -> ManasCore.Logger.info("Pressed")));

registry.create(new ManasKeybinding("manascore.keybinding.skill", category,
() -> SkillAPI.skillActivationPacket(0),
duration -> SkillAPI.skillReleasePacket(0, (int) (duration / 50))
));
registry.create(new ManasKeybinding("manascore.keybinding.skill_toggle", category, SkillAPI::skillTogglePacket));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RegisterTest {
private static final RegistrySupplier<BlockEntityType<TestBlockEntity>> TEST_BLOCK_ENTITY = REGISTER.blockEntity("test_block_entity", TestBlockEntity::new)
.withValidBlocks(TEST_BLOCK)
.end();
public static final RegistrySupplier<TestSkill> TEST_SKILL = REGISTER.skill("test_skill", TestSkill::new).end();

public static void init() {
ManasCore.Logger.info("Registered test content!");
Expand Down
Loading
Loading