Skip to content

Commit

Permalink
Add ability to create advancements for iron button and lever
Browse files Browse the repository at this point in the history
  • Loading branch information
legobmw99 committed Jan 13, 2024
1 parent d807cf1 commit c75c716
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.legobmw99.allomancy.Allomancy;
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.extras.advancement.AllomanticallyActivatedBlockTrigger;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnEntityTrigger;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnPlayerTrigger;
import com.legobmw99.allomancy.modules.extras.block.IronButtonBlock;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class ExtrasSetup {
private static final DeferredRegister<CriterionTrigger<?>> CT = DeferredRegister.create(Registries.TRIGGER_TYPE, Allomancy.MODID);
public static final Supplier<MetalUsedOnEntityTrigger> METAL_USED_ON_ENTITY_TRIGGER = CT.register("metal_used_on_entity", MetalUsedOnEntityTrigger::new);
public static final Supplier<MetalUsedOnPlayerTrigger> METAL_USED_ON_PLAYER_TRIGGER = CT.register("metal_used_on_player", MetalUsedOnPlayerTrigger::new);
public static final Supplier<AllomanticallyActivatedBlockTrigger> ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER = CT.register("activated_allomancy_block",
AllomanticallyActivatedBlockTrigger::new);

public static void register(IEventBus bus) {
BLOCKS.register(bus);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.legobmw99.allomancy.modules.extras.advancement;

import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.SimpleCriterionTrigger;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;

import java.util.Arrays;
import java.util.Optional;

public class AllomanticallyActivatedBlockTrigger extends SimpleCriterionTrigger<AllomanticallyActivatedBlockTrigger.TriggerInstance> {

public void trigger(ServerPlayer player, BlockPos blockPos, boolean isPush) {
ServerLevel serverlevel = player.serverLevel();
BlockState blockstate = serverlevel.getBlockState(blockPos);
LootParams lootparams = new LootParams.Builder(serverlevel)
.withParameter(LootContextParams.ORIGIN, blockPos.getCenter())
.withParameter(LootContextParams.THIS_ENTITY, player)
.withParameter(LootContextParams.BLOCK_STATE, blockstate)
.withParameter(LootContextParams.TOOL, ItemStack.EMPTY)
.create(LootContextParamSets.ADVANCEMENT_LOCATION);
LootContext lootcontext = new LootContext.Builder(lootparams).create(Optional.empty());
this.trigger(player, p_286596_ -> p_286596_.matches(lootcontext, isPush));
}

@Override
public Codec<TriggerInstance> codec() {
return TriggerInstance.CODEC;
}

public record TriggerInstance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> location, Optional<Boolean> isPush) implements SimpleInstance {
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(builder -> builder
.group(ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player),
ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "entity").forGetter(TriggerInstance::location),
Codec.BOOL.optionalFieldOf("is_push").forGetter(TriggerInstance::isPush))
.apply(builder, TriggerInstance::new));

public static Criterion<TriggerInstance> activatedBlock(Block block) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(LootItemBlockStatePropertyCondition.hasBlockStateProperties(block).build());
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.empty()));
}

public static Criterion<TriggerInstance> activatedBlock(LootItemCondition.Builder... p_301013_) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(
Arrays.stream(p_301013_).map(LootItemCondition.Builder::build).toArray(LootItemCondition[]::new));
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.empty()));
}

public static Criterion<TriggerInstance> pushBlock(Block block) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(LootItemBlockStatePropertyCondition.hasBlockStateProperties(block).build());
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.of(true)));
}

public static Criterion<TriggerInstance> pushBlock(LootItemCondition.Builder... p_301013_) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(
Arrays.stream(p_301013_).map(LootItemCondition.Builder::build).toArray(LootItemCondition[]::new));
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.of(true)));
}

public static Criterion<TriggerInstance> pullBlock(Block block) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(LootItemBlockStatePropertyCondition.hasBlockStateProperties(block).build());
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.of(false)));
}

public static Criterion<TriggerInstance> pullBlock(LootItemCondition.Builder... p_301013_) {
ContextAwarePredicate contextawarepredicate = ContextAwarePredicate.create(
Arrays.stream(p_301013_).map(LootItemCondition.Builder::build).toArray(LootItemCondition[]::new));
return ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER
.get()
.createCriterion(new TriggerInstance(Optional.empty(), Optional.of(contextawarepredicate), Optional.of(false)));
}

public boolean matches(LootContext ctx, boolean is_push) {
return (this.isPush.isEmpty() || this.isPush.get() == is_push) && (this.location.isEmpty() || this.location.get().matches(ctx));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.legobmw99.allomancy.modules.extras.block;

import com.legobmw99.allomancy.api.block.IAllomanticallyUsableBlock;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.legobmw99.allomancy.util.ItemDisplay;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
Expand All @@ -30,6 +32,10 @@ public IronButtonBlock() {

@Override
public boolean useAllomantically(BlockState state, Level level, BlockPos pos, Player player, boolean isPush) {
if (player instanceof ServerPlayer sp) {
ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER.get().trigger(sp, pos, isPush);
}

if (state.getValue(POWERED) || level.isClientSide) {
return true;
} else if (isPush) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.legobmw99.allomancy.modules.extras.block;

import com.legobmw99.allomancy.api.block.IAllomanticallyUsableBlock;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.legobmw99.allomancy.util.ItemDisplay;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
Expand All @@ -29,7 +31,11 @@ public IronLeverBlock() {
}

@Override
public boolean useAllomantically(BlockState state, Level world, BlockPos pos, Player playerIn, boolean isPush) {
public boolean useAllomantically(BlockState state, Level world, BlockPos pos, Player player, boolean isPush) {
if (player instanceof ServerPlayer sp) {
ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER.get().trigger(sp, pos, isPush);
}

state = state.cycle(POWERED);
if (world.isClientSide) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;

import java.util.Arrays;
Expand Down Expand Up @@ -53,18 +53,22 @@ public static void handleEmotionChange(final EmotionPayload data, final PlayPayl

public static void tryPushPullBlock(final BlockPushPullPayload data, final PlayPayloadContext ctx) {
ctx.workHandler().execute(() -> {
Player player = ctx.player().get();
ServerPlayer player = (ServerPlayer) ctx.player().get();
Level level = ctx.level().get();
BlockPos pos = data.block();
// Sanity check to make sure the block is loaded in the server
if (level.isLoaded(pos)) {
// activate blocks
if (level.getBlockState(pos).getBlock() instanceof IAllomanticallyUsableBlock block) {
block.useAllomantically(level.getBlockState(pos), level, pos, player, data.isPush());
} else if (PowerUtils.isBlockStateMetal(player.level().getBlockState(pos)) // Check whitelist on server
BlockState blockState = level.getBlockState(pos);
if (blockState.getBlock() instanceof IAllomanticallyUsableBlock block) {
block.useAllomantically(blockState, level, pos, player, data.isPush());
} else if (PowerUtils.isBlockStateMetal(blockState) // Check whitelist on server
|| (player.getMainHandItem().getItem() == CombatSetup.COIN_BAG.get() // check coin bag
&& (!player.getProjectile(player.getMainHandItem()).isEmpty()) && data.isPush())) {
PowerUtils.move(data.direction(), player, pos);
} else {
Allomancy.LOGGER.warn("Illegal use of iron/steel by player: {}!", player);
ctx.packetHandler().disconnect(Component.translatable("allomancy.networking.kicked", "Tried to push or pull against an non-metallic block!"));
}
} else {
Allomancy.LOGGER.warn("Illegal use of iron/steel by player: {}!", player);
Expand Down Expand Up @@ -139,7 +143,7 @@ public static void toggleBurnRequest(final ToggleBurnPayload payload, final Play
public static void updateEnhanced(final EnhanceTimePayload payload, final PlayPayloadContext ctx) {
ctx.workHandler().submitAsync(() -> {
ServerPlayer source = (ServerPlayer) ctx.player().get();
var data = source.getData(AllomancerAttachment.ALLOMANCY_DATA);
IAllomancerData data = source.getData(AllomancerAttachment.ALLOMANCY_DATA);
if (!data.isBurning(Metal.NICROSIL)) {
Allomancy.LOGGER.warn("Illegal use of Nicrosil by player: {}!", source);
ctx.packetHandler().disconnect(Component.translatable("allomancy.networking.kicked", "Tried to mark other player as enhanced while not burning Nicrosil!"));
Expand All @@ -148,13 +152,12 @@ public static void updateEnhanced(final EnhanceTimePayload payload, final PlayPa

Entity e = ctx.level().get().getEntity(payload.entityID());
ExtrasSetup.METAL_USED_ON_ENTITY_TRIGGER.get().trigger(source, e, Metal.NICROSIL, data.isEnhanced());
if (e instanceof ServerPlayer player) {
ExtrasSetup.METAL_USED_ON_PLAYER_TRIGGER.get().trigger(player, Metal.NICROSIL, data.isEnhanced());
if (!PowerUtils.hasTinFoilHat(player)) {
var target_data = player.getData(AllomancerAttachment.ALLOMANCY_DATA);
target_data.setEnhanced(payload.enhanceTime());
if (e instanceof ServerPlayer target) {
ExtrasSetup.METAL_USED_ON_PLAYER_TRIGGER.get().trigger(target, Metal.NICROSIL, data.isEnhanced());
if (!PowerUtils.hasTinFoilHat(target)) {
target.getData(AllomancerAttachment.ALLOMANCY_DATA).setEnhanced(payload.enhanceTime());
// broadcast back to player and tracking
Network.sync(payload, player);
Network.sync(payload, target);

}
}
Expand Down

0 comments on commit c75c716

Please sign in to comment.