Skip to content

Commit

Permalink
More menu work
Browse files Browse the repository at this point in the history
  • Loading branch information
Fureniku committed Jan 21, 2024
1 parent 7d10a43 commit c2a4906
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
43 changes: 20 additions & 23 deletions src/main/java/com/fureniku/metropolis/blocks/MetroBlockBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
Expand Down Expand Up @@ -125,23 +127,7 @@ public float getMaxVerticalOffset() {
* @param pos The position of the block
* @param player The player who right-clicked it
*/
protected void onRightClick(BlockState state, Level level, BlockPos pos, Player player) {}

/**
* Called when a block is right-clicked - only on the client. Useful for localized messaging or other things that don't change the worldstate.
* @param state The blockstate
* @param pos The position of the block
* @param player The player who right-clicked it
*/
protected void onRightClickLocal(BlockState state, BlockPos pos, Player player) {}

/**
* Called when a block is right-clicked - only on the server. Useful for anything logic-based. Vast majority of cases will probably be in here.
* @param state The blockstate
* @param pos The position of the block
* @param player The player who right-clicked it
*/
protected void onRightClickRemote(BlockState state, BlockPos pos, Player player) {}
protected InteractionResult onRightClick(BlockState state, Level level, BlockPos pos, Player player) { return InteractionResult.PASS; }

/**
* Called when a neighbouring block changes.
Expand Down Expand Up @@ -185,6 +171,17 @@ public void generateBlockState(RegistryObject<Block> blockRegistryObject, MetroB
blockStateProvider.simpleBlockWithItem(blockRegistryObject.get());
}

/**
* Get a menu to open
* @param state The blockstate
* @param level The current level
* @param pos The position of the block
* @return The menu instance to open
*/
public MenuProvider getMenu(BlockState state, Level level, BlockPos pos) {
return null;
}

/*
* FORGE START
* Everything below here is a Minecraft/Forge function, which will call MY functions above.
Expand All @@ -207,12 +204,7 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult result) {
if (hand == InteractionHand.MAIN_HAND) {
onRightClick(state, level, pos, player);
if (level.isClientSide) {
onRightClickLocal(state, pos, player);
} else {
onRightClickRemote(state, pos, player);
}
return onRightClick(state, level, pos, player);
}
return onUse(state, level, pos, player, hand, result);
}
Expand All @@ -226,4 +218,9 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block block, BlockPos neighborPos, boolean p_55671_) {
onNeighbourChanged(state, level, pos, state.getBlock(), neighborPos);
}

@Override
public MenuProvider getMenuProvider(BlockState state, Level level, BlockPos pos) {
return getMenu(state, level, pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fureniku.metropolis.utils.SimpleUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
Expand Down Expand Up @@ -205,10 +206,11 @@ public void onNeighbourChanged(BlockState state, Level level, BlockPos pos, Bloc
}

@Override
protected void onRightClickRemote(BlockState state, BlockPos pos, Player player) {
protected InteractionResult onRightClick(BlockState state, Level level, BlockPos pos, Player player) {
if (_toggleHelper != null) {
_toggleHelper.rightClick(state, pos, player, this);
}
return InteractionResult.PASS;
}

@Override
Expand Down

0 comments on commit c2a4906

Please sign in to comment.