Skip to content

Commit

Permalink
Improved keycard reader and unique keycard system
Browse files Browse the repository at this point in the history
  • Loading branch information
andersmmg committed May 22, 2024
1 parent 6b76b25 commit 017847f
Show file tree
Hide file tree
Showing 24 changed files with 1,407 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variants": {
"facing=east": {
"model": "lockandblock:block/keycard_cloner",
"y": 90
},
"facing=north": {
"model": "lockandblock:block/keycard_cloner"
},
"facing=south": {
"model": "lockandblock:block/keycard_cloner",
"y": 180
},
"facing=west": {
"model": "lockandblock:block/keycard_cloner",
"y": 270
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variants": {
"facing=east": {
"model": "lockandblock:block/keycard_writer",
"y": 90
},
"facing=north": {
"model": "lockandblock:block/keycard_writer"
},
"facing=south": {
"model": "lockandblock:block/keycard_writer",
"y": 180
},
"facing=west": {
"model": "lockandblock:block/keycard_writer",
"y": 270
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "lockandblock:block/keycard_cloner"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "lockandblock:block/keycard_writer"
}
16 changes: 15 additions & 1 deletion src/main/java/com/andersmmg/lockandblock/LockAndBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,38 @@
import com.andersmmg.lockandblock.block.entity.ModBlockEntities;
import com.andersmmg.lockandblock.item.ModItemGroups;
import com.andersmmg.lockandblock.item.ModItems;
import com.andersmmg.lockandblock.sounds.ModSounds;
import net.fabricmc.api.ModInitializer;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LockAndBlock implements ModInitializer {
public static final String MOD_ID = "lockandblock";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
// public static final ModConfig CONFIG = ModConfig.createAndLoad();
// public static final ModConfig CONFIG = ModConfig.createAndLoad();
public static final String CARD_UUID_KEY = "card_uuid";

public static Identifier id(String path) {
return new Identifier(MOD_ID, path);
}

public static MutableText langText(String key) {
return langText(key, "text");
}

public static MutableText langText(String key, String type) {
return Text.translatable(type + "." + LockAndBlock.MOD_ID + "." + key);
}

@Override
public void onInitialize() {
ModItems.registerModItems();
ModItemGroups.registerItemGroups();
ModBlocks.registerModBlocks();
ModBlockEntities.registerBlockEntities();
ModSounds.registerSounds();
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/andersmmg/lockandblock/block/ModBlocks.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.andersmmg.lockandblock.block;

import com.andersmmg.lockandblock.LockAndBlock;
import com.andersmmg.lockandblock.block.custom.KeycardClonerBlock;
import com.andersmmg.lockandblock.block.custom.KeycardReaderBlock;
import com.andersmmg.lockandblock.block.custom.KeycardWriterBlock;
import com.andersmmg.lockandblock.item.ModItemGroups;
import io.wispforest.owo.itemgroup.OwoItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
Expand All @@ -15,6 +17,10 @@
public class ModBlocks {
public static final Block KEYCARD_READER = registerBlock("keycard_reader",
new KeycardReaderBlock(FabricBlockSettings.copyOf(Blocks.WHITE_CONCRETE).nonOpaque()));
public static final Block KEYCARD_WRITER = registerBlock("keycard_writer",
new KeycardWriterBlock(FabricBlockSettings.copyOf(Blocks.WHITE_CONCRETE).nonOpaque()));
public static final Block KEYCARD_CLONER = registerBlock("keycard_cloner",
new KeycardClonerBlock(FabricBlockSettings.copyOf(Blocks.WHITE_CONCRETE).nonOpaque()));

private static Block registerBlock(String name, Block block) {
registerBlockItem(name, block);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.andersmmg.lockandblock.block.custom;

import com.andersmmg.lockandblock.LockAndBlock;
import com.andersmmg.lockandblock.block.entity.KeycardClonerBlockEntity;
import com.andersmmg.lockandblock.item.ModItems;
import com.andersmmg.lockandblock.item.custom.KeycardItem;
import com.andersmmg.lockandblock.util.VoxelUtils;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Hand;
import net.minecraft.util.function.BooleanBiFunction;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.stream.Stream;

public class KeycardClonerBlock extends BlockWithEntity {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
private static final VoxelShape VOXEL_SHAPE = Stream.of(
Block.createCuboidShape(3, 0, 3, 13, 2, 13),
Block.createCuboidShape(3, 2, 3, 4, 4, 13),
Block.createCuboidShape(5, 2, 3, 6, 4, 13)
).reduce((v1, v2) -> VoxelShapes.combineAndSimplify(v1, v2, BooleanBiFunction.OR)).get();
;

public KeycardClonerBlock(Settings settings) {
super(settings);
this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH));
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack stack = player.getStackInHand(hand);
if (stack.isOf(ModItems.KEYCARD)) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof KeycardClonerBlockEntity keycardClonerBlockEntity) {
if (keycardClonerBlockEntity.hasUuid()) {
if (KeycardItem.hasUuid(stack)) {
if (!world.isClient)
player.sendMessage(LockAndBlock.langText("card_not_blank"), true);
return ActionResult.FAIL;
} else {
if (!world.isClient) {
KeycardItem.setUuid(keycardClonerBlockEntity.getUuid(), stack);
player.sendMessage(LockAndBlock.langText("card_copied"), true);
keycardClonerBlockEntity.clearUuid();
}
}
return ActionResult.SUCCESS;
} else {
if (KeycardItem.hasUuid(stack)) {
if (!world.isClient) {
keycardClonerBlockEntity.setUuid(KeycardItem.getUuid(stack));
player.sendMessage(LockAndBlock.langText("card_saved"), true);
}
} else {
if (!world.isClient)
player.sendMessage(LockAndBlock.langText("card_blank"), true);
}
}
}
return ActionResult.SUCCESS;
} else {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof KeycardClonerBlockEntity keycardClonerBlockEntity && keycardClonerBlockEntity.hasUuid()) {
if (!world.isClient) {
keycardClonerBlockEntity.clearUuid();
player.sendMessage(LockAndBlock.langText("card_cleared"), true);
}
return ActionResult.SUCCESS;
}
}
return ActionResult.FAIL;
}

protected static Direction getDirection(BlockState state) {
return state.get(FACING);
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(FACING, rotation.rotate(state.get(FACING)));
}

@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return state.rotate(mirror.getRotation(state.get(FACING)));
}

@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return VoxelUtils.rotateShape(getDirection(state), VOXEL_SHAPE);
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
}

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return this.getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing().getOpposite());
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new KeycardClonerBlockEntity(pos, state);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.andersmmg.lockandblock.block.custom;

import com.andersmmg.lockandblock.LockAndBlock;
import com.andersmmg.lockandblock.block.entity.KeycardReaderBlockEntity;
import com.andersmmg.lockandblock.item.ModItems;
import com.andersmmg.lockandblock.item.custom.KeycardItem;
import com.andersmmg.lockandblock.sounds.ModSounds;
import com.andersmmg.lockandblock.util.VoxelUtils;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.DirectionProperty;
Expand Down Expand Up @@ -36,22 +41,68 @@ public KeycardReaderBlock(Settings settings) {
this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH).with(POWERED, false));
}

protected static Direction getDirection(BlockState state) {
return state.get(FACING);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.getStackInHand(hand).isOf(ModItems.KEYCARD)) {
if (!state.get(POWERED)) {
if (!world.isClient()) {
world.setBlockState(pos, state.with(POWERED, true), 3);
world.scheduleBlockTick(pos, this, 20, TickPriority.NORMAL);
this.updateNeighbors(state, (ServerWorld) world, pos);
ItemStack stack = player.getStackInHand(hand);
if (state.get(POWERED)) {
return ActionResult.FAIL;
}
if (stack.isOf(ModItems.KEYCARD)) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof KeycardReaderBlockEntity keycardReaderBlockEntity) {
if (keycardReaderBlockEntity.hasUuid()) {
if (KeycardItem.hasUuid(stack)) {
if (keycardReaderBlockEntity.getUuid().equals(KeycardItem.getUuid(stack))) {
return this.activate(state, world, pos);
} else {
if (!world.isClient) {
world.playSound(null, pos, ModSounds.BEEP_ERROR, SoundCategory.BLOCKS, 1.0F, 1.0F);
player.sendMessage(LockAndBlock.langText("wrong_keycard"), true);
}
}
} else {
if (!world.isClient) {
world.playSound(null, pos, ModSounds.BEEP_ERROR, SoundCategory.BLOCKS, 1.0F, 1.0F);
player.sendMessage(LockAndBlock.langText("blank_keycard"), true);
}
}
} else {
if (KeycardItem.hasUuid(stack)) {
if (!world.isClient) {
keycardReaderBlockEntity.setUuid(KeycardItem.getUuid(stack));
player.sendMessage(LockAndBlock.langText("reader_programmed"), true);
return this.activate(state, world, pos);
}
} else {
if (!world.isClient) {
world.playSound(null, pos, ModSounds.BEEP_ERROR, SoundCategory.BLOCKS, 1.0F, 1.0F);
player.sendMessage(LockAndBlock.langText("blank_keycard"), true);
}
}
}
return ActionResult.SUCCESS;
}
return ActionResult.SUCCESS;
}
return ActionResult.FAIL;
}

private ActionResult activate(BlockState state, World world, BlockPos pos) {
if (!state.get(POWERED)) {
if (!world.isClient()) {
world.playSound(null, pos, ModSounds.BEEP_SUCCESS, SoundCategory.BLOCKS, 1.0F, 1.0F);
world.setBlockState(pos, state.with(POWERED, true), 3);
world.scheduleBlockTick(pos, this, 20, TickPriority.NORMAL);
this.updateNeighbors(state, (ServerWorld) world, pos);
}
return ActionResult.SUCCESS;
}
return ActionResult.CONSUME;
}

private void updateNeighbors(BlockState state, ServerWorld world, BlockPos pos) {
world.updateNeighborsAlways(pos, this);
world.updateNeighborsAlways(pos.offset(getDirection(state).getOpposite()), this);
Expand All @@ -69,10 +120,6 @@ public boolean emitsRedstonePower(BlockState state) {
return true;
}

protected static Direction getDirection(BlockState state) {
return state.get(FACING);
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
Expand Down
Loading

0 comments on commit 017847f

Please sign in to comment.