Skip to content

Commit

Permalink
Update how iron lever/buttons work to use capability, add bell functi…
Browse files Browse the repository at this point in the history
…onality
  • Loading branch information
legobmw99 committed Jun 22, 2024
1 parent 9181ecf commit 221583b
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 86 deletions.
2 changes: 2 additions & 0 deletions src/generated/resources/assets/allomancy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"advancements.local_metallurgist.title": "Local Metallurgist!",
"advancements.metallic_collector.desc": "Collect every single metallic flake, even the useless ones",
"advancements.metallic_collector.title": "Metallic Collector",
"advancements.noisey.desc": "Allomancy can grant great stealth, unless you do that!",
"advancements.noisey.title": "Going Loud",
"advancements.tin_foil_hat.desc": "Protect yourself, and be a bit paranoid too",
"advancements.tin_foil_hat.title": "Tin foil hat",
"allomancy.aluminum.black": "Black Aluminum Symbol",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"parent": "allomancy:main/coinshot",
"criteria": {
"allomantically_activate_bell": {
"conditions": {
"entity": [
{
"block": "minecraft:bell",
"condition": "minecraft:block_state_property"
}
]
},
"trigger": "allomancy:activated_allomancy_block"
}
},
"display": {
"announce_to_chat": false,
"description": {
"translate": "advancements.noisey.desc"
},
"hidden": true,
"icon": {
"count": 1,
"id": "minecraft:bell"
},
"title": {
"translate": "advancements.noisey.title"
}
},
"requirements": [
[
"allomantically_activate_bell"
]
],
"sends_telemetry_event": true
}
1 change: 1 addition & 0 deletions src/main/java/com/legobmw99/allomancy/Allomancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Allomancy(IEventBus bus, ModContainer container, Dist dist) {
bus.addListener(Network::registerPayloads);

ExtrasSetup.register(bus);
bus.addListener(ExtrasSetup::registerCapabilities);
NeoForge.EVENT_BUS.addListener(ExtrasSetup::registerCommands);

CombatSetup.register(bus);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package com.legobmw99.allomancy.api.block;

import com.legobmw99.allomancy.modules.extras.block.IronLeverBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

/**
* This interface can be used to signify if a block should react to being pushed
* or pulled, rather than moving the pusher
*
* @author legobmw99
* @see IronLeverBlock
* @see com.legobmw99.allomancy.modules.extras.ExtrasSetup#ALLOMANTICALLY_USABLE_BLOCK
*/
public interface IAllomanticallyUsableBlock {
public interface IAllomanticallyUsable {

/**
* Called when the block is steelpushed or ironpulled
*
* @param isPush whether the activation is Steel
* @return whether the block was activated
*/
boolean useAllomantically(BlockState state, Level world, BlockPos pos, Player playerIn, boolean isPush);
boolean useAllomantically(Player player, boolean isPush);
}
14 changes: 14 additions & 0 deletions src/main/java/com/legobmw99/allomancy/datagen/Advancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.combat.CombatSetup;
import com.legobmw99.allomancy.modules.consumables.ConsumeSetup;
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.materials.MaterialsSetup;
Expand Down Expand Up @@ -148,5 +149,18 @@ public void generate(HolderLookup.Provider registries,
Metal.IRON))
.requirements(AdvancementRequirements.Strategy.OR)
.save(saver, "allomancy:main/consequences");


Advancement.Builder
.advancement()
.parent(Advancement.Builder
.advancement()
.build(ResourceLocation.fromNamespaceAndPath(Allomancy.MODID, "main/coinshot")))
.display(Blocks.BELL, Component.translatable("advancements.noisey.title"),
Component.translatable("advancements.noisey.desc"), null, AdvancementType.TASK, true, true,
true)
.addCriterion("allomantically_activate_bell",
AllomanticallyActivatedBlockTrigger.TriggerInstance.activatedBlock(Blocks.BELL))
.save(saver, "allomancy:main/noisey");
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/legobmw99/allomancy/datagen/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ protected void addTranslations() {
add("advancements.tin_foil_hat.desc", "Protect yourself, and be a bit paranoid too");
add("advancements.consequences.title", "Consequences, Vin");
add("advancements.consequences.desc", "Learn what happens when you push on a much heavier target.");
add("advancements.noisey.title", "Going Loud");
add("advancements.noisey.desc", "Allomancy can grant great stealth, unless you do that!");

add("key.categories.allomancy", "Allomancy");
add("key.burn", "Burn Metals");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.legobmw99.allomancy.modules.extras;

import com.legobmw99.allomancy.Allomancy;
import com.legobmw99.allomancy.api.block.IAllomanticallyUsable;
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.extras.advancement.AllomanticallyActivatedBlockTrigger;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnEntityTrigger;
Expand All @@ -17,12 +18,19 @@
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BannerPatternItem;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.BellBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BannerPattern;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.registries.DeferredBlock;
import net.neoforged.neoforge.registries.DeferredItem;
Expand All @@ -36,6 +44,12 @@ public class ExtrasSetup {
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Allomancy.MODID);
private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Allomancy.MODID);

public static final BlockCapability<IAllomanticallyUsable, Void> ALLOMANTICALLY_USABLE_BLOCK =
BlockCapability.createVoid(
// Provide a name to uniquely identify the capability.
ResourceLocation.fromNamespaceAndPath(Allomancy.MODID, "allomantically_usable_block"),
// Provide the queried type. Here, we want to look up `IItemHandler` instances.
IAllomanticallyUsable.class);

public static final DeferredBlock<IronButtonBlock> IRON_BUTTON =
BLOCKS.register("iron_button", () -> new IronButtonBlock(true));
Expand Down Expand Up @@ -108,6 +122,32 @@ public static void bootstrapBanners(BootstrapContext<BannerPattern> bootstrapCon
}
}

public static void registerCapabilities(RegisterCapabilitiesEvent event) {
event.registerBlock(ALLOMANTICALLY_USABLE_BLOCK, new IronLeverBlock.AllomanticUseCapabilityProvider(),
IRON_LEVER.get());
event.registerBlock(ALLOMANTICALLY_USABLE_BLOCK, new IronButtonBlock.AllomanticUseCapabilityProvider(),
IRON_BUTTON.get(), INVERTED_IRON_BUTTON.get());

event.registerBlock(ALLOMANTICALLY_USABLE_BLOCK,
((level, pos, state, blockEntity, context) -> ((player, isPush) -> {
if (player instanceof ServerPlayer sp) {
ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER.get().trigger(sp, pos, isPush);
}
if (level.isClientSide()) {
return true;
}
var direction = player.getNearestViewDirection();
if (isPush) {
direction = direction.getOpposite();
}
return ((BellBlock) state.getBlock()).onHit(level, state,
new BlockHitResult(Vec3.atCenterOf(pos),
direction, pos, false),
player, true);

})), Blocks.BELL);

}

public static void registerCommands(final RegisterCommandsEvent e) {
AllomancyPowerCommand.register(e.getDispatcher());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.legobmw99.allomancy.modules.extras.block;

import com.legobmw99.allomancy.api.block.IAllomanticallyUsableBlock;
import com.legobmw99.allomancy.api.block.IAllomanticallyUsable;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.legobmw99.allomancy.util.ItemDisplay;
import net.minecraft.ChatFormatting;
Expand All @@ -18,17 +18,19 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.ButtonBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockSetType;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.capabilities.IBlockCapabilityProvider;

import java.util.List;
import java.util.function.BiConsumer;

public class IronButtonBlock extends ButtonBlock implements IAllomanticallyUsableBlock {
public class IronButtonBlock extends ButtonBlock {


private final boolean activatedOnPush;
Expand All @@ -38,24 +40,6 @@ public IronButtonBlock(boolean activatedOnPush) {
this.activatedOnPush = activatedOnPush;
}

@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 == this.activatedOnPush) {
this.press(state, level, pos, player);
this.playSound(null, level, pos, true);
level.gameEvent(player, GameEvent.BLOCK_ACTIVATE, pos);
return true;
} else {
return false;
}
}

public boolean activatedOnPush() {
return this.activatedOnPush;
}
Expand Down Expand Up @@ -96,4 +80,37 @@ public void appendHoverText(ItemStack stack,
ItemDisplay.addColorToText("block.allomancy.iron_activation.lore", ChatFormatting.GRAY);
tooltip.add(lore);
}


public static class AllomanticUseCapabilityProvider implements IBlockCapabilityProvider<IAllomanticallyUsable,
Void> {

@Override
public IAllomanticallyUsable getCapability(Level level,
BlockPos pos,
BlockState state,
BlockEntity blockEntity,
Void context) {
return ((player, isPush) -> {
if (player instanceof ServerPlayer sp) {
ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER.get().trigger(sp, pos, isPush);
}

if (state.getValue(POWERED) || level.isClientSide()) {
return true;
}

IronButtonBlock block = (IronButtonBlock) state.getBlock();
if (isPush == block.activatedOnPush()) {
block.press(state, level, pos, player);
block.playSound(player, level, pos, false);
level.gameEvent(player, GameEvent.BLOCK_ACTIVATE, pos);
return true;
} else {
return false;
}
});
}
}

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

import com.legobmw99.allomancy.api.block.IAllomanticallyUsableBlock;
import com.legobmw99.allomancy.api.block.IAllomanticallyUsable;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.legobmw99.allomancy.util.ItemDisplay;
import net.minecraft.ChatFormatting;
Expand All @@ -18,39 +18,21 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.LeverBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.capabilities.IBlockCapabilityProvider;

import java.util.List;
import java.util.function.BiConsumer;

public class IronLeverBlock extends LeverBlock implements IAllomanticallyUsableBlock {
public class IronLeverBlock extends LeverBlock {

public IronLeverBlock() {
super(Block.Properties.of().noCollission().strength(1.0F));
}

@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 (level.isClientSide()) {
return true;
}
if (isPush == state.getValue(POWERED)) {
this.pull(state, level, pos, player);
float f = state.getValue(POWERED) ? 0.6F : 0.5F;
level.playSound(null, pos, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3F, f);
level.gameEvent(player, state.getValue(POWERED) ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE,
pos);
return true;

}
return false;
}

@Override
protected InteractionResult useWithoutItem(BlockState pState,
Level pLevel,
Expand Down Expand Up @@ -84,4 +66,35 @@ private void updateNeighbors(BlockState state, Level world, BlockPos pos) {
world.updateNeighborsAt(pos.relative(getConnectedDirection(state).getOpposite()), this);
}

public static class AllomanticUseCapabilityProvider implements IBlockCapabilityProvider<IAllomanticallyUsable,
Void> {

@Override
public IAllomanticallyUsable getCapability(Level level,
BlockPos pos,
BlockState state,
BlockEntity blockEntity,
Void context) {
return ((player, isPush) -> {
if (player instanceof ServerPlayer sp) {
ExtrasSetup.ALLOMANTICALLY_ACTIVATED_BLOCK_TRIGGER.get().trigger(sp, pos, isPush);
}
if (level.isClientSide()) {
return true;
}
if (isPush == state.getValue(POWERED)) {
((LeverBlock) state.getBlock()).pull(state, level, pos, player);
float f = state.getValue(POWERED) ? 0.6F : 0.5F;
level.playSound(null, pos, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3F, f);
level.gameEvent(player,
state.getValue(POWERED) ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE,
pos);
return true;

}
return false;
});
}
}

}
Loading

0 comments on commit 221583b

Please sign in to comment.