Skip to content

Commit

Permalink
More advancement stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 21, 2023
1 parent 3833db0 commit 3babbdf
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/fusionflux/portalcubed/PortalCubed.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fusionflux.gravity_api.util.GravityChannel;
import com.fusionflux.gravity_api.util.RotationUtil;
import com.fusionflux.portalcubed.accessor.CalledValues;
import com.fusionflux.portalcubed.advancements.PortalCubedAdvancements;
import com.fusionflux.portalcubed.blocks.PortalBlocksLoader;
import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.blocks.TallButtonVariant;
Expand All @@ -25,7 +26,6 @@
import com.fusionflux.portalcubed.items.PortalCubedItems;
import com.fusionflux.portalcubed.packet.PortalCubedServerPackets;
import com.fusionflux.portalcubed.particle.PortalCubedParticleTypes;
import com.fusionflux.portalcubed.predicates.PortalCubedConditions;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import com.fusionflux.portalcubed.util.IPQuaternion;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -273,7 +273,7 @@ public void onInitialize(ModContainer mod) {
PortalCubedSounds.registerSounds();
PortalCubedGameRules.register();
PortalCubedParticleTypes.register();
PortalCubedConditions.register();
PortalCubedAdvancements.register();

PortalTabsLoader.load(mod);
BlockContentRegistries.FLAMMABLE.put(PortalCubedBlocks.NEUROTOXIN_BLOCK, new FlammableBlockEntry(10000, 10000));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fusionflux.portalcubed.advancements;

import com.fusionflux.portalcubed.advancements.conditions.PortalCubedConditions;
import com.fusionflux.portalcubed.advancements.predicates.PortalCubedPredicates;
import com.fusionflux.portalcubed.advancements.triggers.PortalCubedTriggers;

public class PortalCubedAdvancements {
public static void register() {
PortalCubedConditions.register();
PortalCubedPredicates.register();
PortalCubedTriggers.register();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fusionflux.portalcubed.predicates;
package com.fusionflux.portalcubed.advancements.conditions;

import com.fusionflux.portalcubed.accessor.EntityExt;
import com.google.common.collect.ImmutableSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fusionflux.portalcubed.predicates;
package com.fusionflux.portalcubed.advancements.conditions;

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.fusionflux.portalcubed.advancements.predicates;

import com.fusionflux.portalcubed.entity.EnergyPellet;
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.EntitySubPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class EnergyPelletPredicate implements EntitySubPredicate {
private final MinMaxBounds.Ints bounces;
private final EntityPredicate thrower;

public EnergyPelletPredicate(MinMaxBounds.Ints bounces, EntityPredicate thrower) {
this.bounces = bounces;
this.thrower = thrower;
}

public static EnergyPelletPredicate fromJson(JsonObject json) {
return new EnergyPelletPredicate(
MinMaxBounds.Ints.fromJson(json.get("bounces")),
EntityPredicate.fromJson(json.get("thrower"))
);
}

@Override
public boolean matches(Entity entity, ServerLevel level, @Nullable Vec3 pos) {
if (!(entity instanceof EnergyPellet energyPellet)) {
return false;
}
if (!bounces.matches(energyPellet.getBounces())) {
return false;
}
//noinspection RedundantIfStatement
if (!thrower.matches(level, pos, energyPellet.getThrower())) {
return false;
}
return true;
}

@NotNull
@Override
public JsonObject serializeCustomData() {
final JsonObject result = new JsonObject();
if (!bounces.isAny()) {
result.add("bounces", bounces.serializeToJson());
}
if (thrower != EntityPredicate.ANY) {
result.add("thrower", thrower.serializeToJson());
}
return result;
}

@NotNull
@Override
public Type type() {
return PortalCubedPredicates.ENERGY_PELLET;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fusionflux.portalcubed.advancements.predicates;

import net.minecraft.advancements.critereon.EntitySubPredicate;

public class PortalCubedPredicates {
public static final EntitySubPredicate.Type ENERGY_PELLET = EnergyPelletPredicate::fromJson;

public static void register() {
EntitySubPredicate.Types.TYPES.put("portalcubed:energy_pellet", ENERGY_PELLET);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fusionflux.portalcubed.advancements.triggers;

import com.fusionflux.portalcubed.entity.EnergyPellet;
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.fusionflux.portalcubed.PortalCubed.id;

public class BounceTrigger extends SimpleCriterionTrigger<BounceTrigger.TriggerInstance> {
public static final ResourceLocation ID = id("bounce");

@NotNull
@Override
public ResourceLocation getId() {
return ID;
}

@NotNull
@Override
protected TriggerInstance createInstance(JsonObject json, ContextAwarePredicate predicate, DeserializationContext deserializationContext) {
return new TriggerInstance(predicate, EntityPredicate.fromJson(json.get("pellet")));
}

public void trigger(ServerPlayer player, EnergyPellet pellet) {
trigger(player, conditions -> conditions.matches(player.serverLevel(), player.position(), pellet));
}

public static class TriggerInstance extends AbstractCriterionTriggerInstance {
private final EntityPredicate pellet;

public TriggerInstance(ContextAwarePredicate player, EntityPredicate pellet) {
super(ID, player);
this.pellet = pellet;
}

public boolean matches(ServerLevel level, @Nullable Vec3 position, EnergyPellet pellet) {
return this.pellet.matches(level, position, pellet);
}

@NotNull
@Override
public JsonObject serializeToJson(SerializationContext context) {
final JsonObject result = super.serializeToJson(context);
result.add("pellet", pellet.serializeToJson());
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fusionflux.portalcubed.advancements.triggers;

import net.minecraft.advancements.CriteriaTriggers;

public class PortalCubedTriggers {
public static final BounceTrigger BOUNCE = new BounceTrigger();

public static void register() {
CriteriaTriggers.register(BOUNCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fusionflux.portalcubed.accessor.BlockCollisionTrigger;
import com.fusionflux.portalcubed.accessor.EntityExt;
import com.fusionflux.portalcubed.blocks.blockentities.CatapultBlockEntity;
import com.fusionflux.portalcubed.entity.EnergyPelletEntity;
import com.fusionflux.portalcubed.entity.EnergyPellet;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand Down Expand Up @@ -46,7 +46,7 @@ public VoxelShape getTriggerShape(BlockState state, BlockGetter world, BlockPos

@Override
public void onEntityEnter(BlockState state, Level world, BlockPos pos, Entity entity) {
if (entity instanceof EntityExt ext && !(entity instanceof EnergyPelletEntity)) {
if (entity instanceof EntityExt ext && !(entity instanceof EnergyPellet)) {
world.getBlockEntity(pos, PortalCubedBlocks.CATAPULT_BLOCK_ENTITY).ifPresent(ext::collidedWithCatapult);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.fusionflux.portalcubed.client.render.entity;

import com.fusionflux.portalcubed.entity.EnergyPelletEntity;
import com.fusionflux.portalcubed.entity.EnergyPellet;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.ThrownItemRenderer;
import net.minecraft.util.Mth;

public class EnergyPelletRenderer extends ThrownItemRenderer<EnergyPelletEntity> {
public class EnergyPelletRenderer extends ThrownItemRenderer<EnergyPellet> {
public static Float pelletAlpha = null;

public EnergyPelletRenderer(EntityRendererProvider.Context ctx) {
super(ctx, 1f, true);
}

@Override
public void render(EnergyPelletEntity entity, float yaw, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light) {
public void render(EnergyPellet entity, float yaw, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light) {
if (entity.getStartingLife() > 0) {
pelletAlpha = Mth.clamp(Mth.lerp((float)entity.getLife() / entity.getStartingLife(), 0.25f, 1f), 0f, 1f);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import com.fusionflux.portalcubed.PortalCubedConfig;
import com.fusionflux.portalcubed.accessor.EntityExt;
import com.fusionflux.portalcubed.accessor.LevelExt;
import com.fusionflux.portalcubed.advancements.triggers.PortalCubedTriggers;
import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.items.PortalCubedItems;
import com.fusionflux.portalcubed.listeners.WentThroughPortalListener;
import com.fusionflux.portalcubed.particle.DecalParticleOption;
import com.fusionflux.portalcubed.particle.PortalCubedParticleTypes;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import net.minecraft.Util;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
Expand All @@ -32,13 +36,18 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

import static com.fusionflux.portalcubed.mechanics.PortalCubedDamageSources.pcSources;

public class EnergyPelletEntity extends Entity implements ItemSupplier, WentThroughPortalListener {
private static final EntityDataAccessor<Integer> STARTING_LIFE = SynchedEntityData.defineId(EnergyPelletEntity.class, EntityDataSerializers.INT);
private static final EntityDataAccessor<Integer> LIFE = SynchedEntityData.defineId(EnergyPelletEntity.class, EntityDataSerializers.INT);
public class EnergyPellet extends Entity implements ItemSupplier, WentThroughPortalListener {
private static final EntityDataAccessor<Integer> STARTING_LIFE = SynchedEntityData.defineId(EnergyPellet.class, EntityDataSerializers.INT);
private static final EntityDataAccessor<Integer> LIFE = SynchedEntityData.defineId(EnergyPellet.class, EntityDataSerializers.INT);

private int bounces;
private UUID thrower = Util.NIL_UUID;

public EnergyPelletEntity(EntityType<?> type, Level world) {
public EnergyPellet(EntityType<?> type, Level world) {
super(type, world);
}

Expand All @@ -51,11 +60,15 @@ protected void defineSynchedData() {
@Override
protected void readAdditionalSaveData(CompoundTag nbt) {
setLife(nbt.getInt("Life"));
bounces = nbt.getInt("Bounces");
thrower = nbt.getUUID("Thrower");
}

@Override
protected void addAdditionalSaveData(CompoundTag nbt) {
nbt.putInt("Life", getLife());
nbt.putInt("Bounces", bounces);
nbt.putUUID("Thrower", thrower);
}

public int getStartingLife() {
Expand Down Expand Up @@ -123,7 +136,7 @@ public void tick() {
}
setDeltaMovement(vel);
if (bouncedDir != null) {
level().playSound(null, this, PortalCubedSounds.PELLET_BOUNCE_EVENT, SoundSource.HOSTILE, 0.4f, 1f);
bounced();
if (level() instanceof ServerLevel serverLevel) {
final Vec3 spawnPos = serverLevel.clip(new ClipContext(
position(),
Expand Down Expand Up @@ -184,11 +197,23 @@ private void bounceOrKill(LivingEntity entity) {
final double mag = vel.length();
setDeltaMovement(Math.cos(newAngle) * mag, vel.y, Math.sin(newAngle) * mag);
level().playSound(null, this, PortalCubedSounds.PELLET_BOUNCE_EVENT, SoundSource.HOSTILE, 0.4f, 1f);
bounced();
} else {
kill(entity);
}
}

private void bounced() {
level().playSound(null, this, PortalCubedSounds.PELLET_BOUNCE_EVENT, SoundSource.HOSTILE, 0.4f, 1f);
bounces++;
if (level() instanceof ServerLevel serverLevel && thrower != Util.NIL_UUID) {
final ServerPlayer player = (ServerPlayer)serverLevel.getPlayerByUUID(thrower);
if (player != null) {
PortalCubedTriggers.BOUNCE.trigger(player, this);
}
}
}

private void kill(@Nullable LivingEntity entity) {
level().playSound(null, position().x, position().y, position().z, PortalCubedSounds.PELLET_EXPLODE_EVENT, SoundSource.HOSTILE, 0.8f, 1f);
if (level() instanceof ServerLevel serverLevel) {
Expand All @@ -198,7 +223,7 @@ private void kill(@Nullable LivingEntity entity) {
);
}
if (entity != null) {
entity.hurt(pcSources(level()).vaporization(), PortalCubedConfig.pelletDamage);
entity.hurt(pcSources(level()).vaporization(this, getThrower()), PortalCubedConfig.pelletDamage);
}
kill();
}
Expand All @@ -218,4 +243,17 @@ public ItemStack getItem() {
public void wentThroughPortal(Portal portal) {
setLife(getStartingLife());
}

public int getBounces() {
return bounces;
}

public UUID getThrowerUUID() {
return thrower;
}

@Nullable
public Entity getThrower() {
return ((LevelExt)level()).getEntityByUuid(thrower);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fusionflux.portalcubed.blocks.BaseGel;
import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.entity.beams.ExcursionFunnelEntity;

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
Expand Down Expand Up @@ -127,7 +126,7 @@ PortalCubedBlocks.REFLECTION_GEL, id("textures/block/reflection_gel.png")
.setDimensions(EntityDimensions.scalable(0.1875f, 0.1875f))
.build();

public static final EntityType<EnergyPelletEntity> ENERGY_PELLET = QuiltEntityTypeBuilder.create(MobCategory.MISC, EnergyPelletEntity::new)
public static final EntityType<EnergyPellet> ENERGY_PELLET = QuiltEntityTypeBuilder.create(MobCategory.MISC, EnergyPellet::new)
.setDimensions(EntityDimensions.scalable(0.25f, 0.25f))
.build();

Expand Down
Loading

0 comments on commit 3babbdf

Please sign in to comment.