Skip to content

Commit

Permalink
Migrate instruments to Fabric, leave out looper
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Oct 10, 2023
1 parent 351ad40 commit 6bba75a
Show file tree
Hide file tree
Showing 45 changed files with 2,718 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dependencies {
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modImplementation "maven.modrinth:genshin-instruments:${project.genshinstrument_version}"
modCompileOnly "curse.maven:forge-config-api-port-fabric-547434:${project.forge_config_api_port_version}"
modImplementation "curse.maven:forge-config-api-port-fabric-547434:${project.forge_config_api_port_version}"

modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cstav.evenmoreinstruments;

import com.cstav.evenmoreinstruments.item.ModItems;

import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;

public abstract class EMIModCreativeModeTabs {

public static final CreativeModeTab INSTRUMENT_ACCESSORY_TAB = FabricItemGroup.builder()
.icon(() -> new ItemStack(ModItems.KEYBOARD_STAND))
.title(Component.translatable("itemGroup.evenmoreinstruments.instrument_accessories"))
.build();

public static void regsiter() {
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB,
new ResourceLocation(Main.MODID, "instruments_tab"), INSTRUMENT_ACCESSORY_TAB
);
}

}
44 changes: 36 additions & 8 deletions src/main/java/com/cstav/evenmoreinstruments/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
package com.cstav.evenmoreinstruments;

import net.fabricmc.api.ModInitializer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cstav.evenmoreinstruments.block.ModBlocks;
import com.cstav.evenmoreinstruments.block.blockentity.ModBlockEntities;
import com.cstav.evenmoreinstruments.gamerule.ModGameRules;
import com.cstav.evenmoreinstruments.item.ModItems;
import com.cstav.evenmoreinstruments.networking.ModPacketHandler;
import com.cstav.evenmoreinstruments.sound.ModSounds;

import net.fabricmc.api.ModInitializer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;

public class Main implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("evenmoreinstruments");
public static final String MODID = "evenmoreinstruments";
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);

public static CompoundTag modTag(final ItemStack item) {
return item.getOrCreateTagElement(MODID);
}
//TODO implement getPersistentData for BlockEntities
// public static CompoundTag modTag(final BlockEntity be) {
// return CommonUtil.getOrCreateElementTag(be.getPersistentData(), MODID);
// }

@Override
public void onInitialize() {
ModPacketHandler.registerServerPackets();
EMIModCreativeModeTabs.regsiter();

ModSounds.load();
ModGameRules.load();


ModBlocks.load();
ModBlockEntities.load();

@Override
public void onInitialize() {
LOGGER.info("Hello Fabric world!");
}
}
ModItems.load();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cstav.evenmoreinstruments.block;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public interface IDoubleBlock {
BlockPos getOtherBlock(final BlockState state, final BlockPos blockPos, final Level level);
}
153 changes: 153 additions & 0 deletions src/main/java/com/cstav/evenmoreinstruments/block/KeyboardBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.cstav.evenmoreinstruments.block;

import com.cstav.evenmoreinstruments.block.blockentity.ModInstrumentBlockEntity;
import com.cstav.evenmoreinstruments.networking.ModPacketHandler;
import com.cstav.evenmoreinstruments.networking.packet.ModOpenInstrumentPacket;
import com.cstav.evenmoreinstruments.util.CommonUtil;
import com.cstav.genshinstrument.block.partial.AbstractInstrumentBlock;
import com.cstav.genshinstrument.block.partial.InstrumentBlockEntity;
import com.cstav.genshinstrument.networking.OpenInstrumentPacketSender;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class KeyboardBlock extends AbstractInstrumentBlock implements IDoubleBlock {
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final EnumProperty<KeyboardBlock.KeyboardPart> PART = EnumProperty.create("part", KeyboardPart.class);

public static final VoxelShape
SHAPE_LEFT_SOUTH = Block.box(0.0D, 0.0D, 4.0D, 15.65D, 4.4D, 12.8D),
SHAPE_RIGHT_SOUTH = Block.box(0.3D, 0.0D, 4D, 16.0D, 4.4D, 12.8D),

SHAPE_LEFT_EAST = Block.box(3.5D, 0.0D, 0.0D, 12D, 4.4D, 15.65D),
SHAPE_RIGHT_EAST = Block.box(3.5D, 0.0D, 0.3D, 12D, 4.4D, 16D);
;


public KeyboardBlock(Properties pProperties) {
super(pProperties);
registerDefaultState(defaultBlockState()
.setValue(FACING, Direction.NORTH)
.setValue(PART, KeyboardPart.LEFT)
);
}

@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
final Direction facing = pState.getValue(FACING);
final boolean southOrNorth = (facing == Direction.SOUTH) || (facing == Direction.NORTH);

return (pState.getValue(PART) == KeyboardPart.LEFT)
? (southOrNorth ? SHAPE_LEFT_SOUTH : SHAPE_LEFT_EAST)
: (southOrNorth ? SHAPE_RIGHT_SOUTH : SHAPE_RIGHT_EAST);
}

public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}


@Override
protected OpenInstrumentPacketSender instrumentPacketSender() {
return (player, hand) -> ModPacketHandler.sendToClient(new ModOpenInstrumentPacket("keyboard", hand), player);
}

@Override
public InstrumentBlockEntity newBlockEntity(BlockPos arg0, BlockState arg1) {
return new ModInstrumentBlockEntity(arg0, arg1);
}


// Handle 2 blocks
@Override
public void setPlacedBy(Level pLevel, BlockPos pPos, BlockState pState, LivingEntity pPlacer, ItemStack pStack) {
super.setPlacedBy(pLevel, pPos, pState, pPlacer, pStack);
if (pLevel.isClientSide)
return;

final BlockPos sidePos = pPos.relative(CommonUtil.getRight(pState.getValue(FACING)));

pLevel.setBlock(sidePos,
pState.setValue(PART, KeyboardPart.RIGHT)
, 3);
pLevel.blockUpdated(pPos, Blocks.AIR);
pLevel.blockUpdated(sidePos, Blocks.AIR);
pState.updateNeighbourShapes(pLevel, pPos, 3);
}

@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) {
if (pLevel.isClientSide)
return;

final BlockPos sideBlock = getOtherBlock(pState, pPos, pLevel);
if (sideBlock == null)
return;

pLevel.setBlock(sideBlock,
Blocks.AIR.defaultBlockState()
, 1|2|4);
pLevel.blockUpdated(pPos, Blocks.AIR);
pState.updateNeighbourShapes(pLevel, pPos, 1|2|4);
}


@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
final Direction direction = pContext.getHorizontalDirection();
final BlockPos pos = pContext.getClickedPos();
final BlockPos sidePos = pos.relative(CommonUtil.getLeft(direction));

final Level level = pContext.getLevel();

return (
level.getBlockState(sidePos).canBeReplaced(pContext) && level.getWorldBorder().isWithinBounds(sidePos)
&& (level.getBlockState(pos.below(1)).canOcclude() || level.getBlockState(sidePos.below(1)).canOcclude())
)
? defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite())
: null;
}

@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING, PART);
}


public static enum KeyboardPart implements StringRepresentable {
LEFT, RIGHT;

@Override
public String getSerializedName() {
return toString().toLowerCase();
}
}


@Override
public BlockPos getOtherBlock(final BlockState state, BlockPos blockPos, Level level) {
final BlockPos sideBlock = blockPos.relative((state.getValue(PART) == KeyboardPart.LEFT)
? CommonUtil.getRight(state.getValue(FACING))
: CommonUtil.getLeft(state.getValue(FACING))
);

return (!level.getBlockState(sideBlock).is(ModBlocks.KEYBOARD)) ? null : sideBlock;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.cstav.evenmoreinstruments.block;

import com.cstav.evenmoreinstruments.block.blockentity.ModInstrumentBlockEntity;
import com.cstav.evenmoreinstruments.item.ModItems;
import com.cstav.evenmoreinstruments.networking.ModPacketHandler;
import com.cstav.evenmoreinstruments.networking.packet.ModOpenInstrumentPacket;
import com.cstav.genshinstrument.block.partial.AbstractInstrumentBlock;
import com.cstav.genshinstrument.block.partial.InstrumentBlockEntity;
import com.cstav.genshinstrument.networking.OpenInstrumentPacketSender;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class KeyboardStandBlock extends AbstractInstrumentBlock {
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final BooleanProperty HAS_KEYBOARD = BooleanProperty.create("has_keyboard");


public static final VoxelShape BASE_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 9.0D, 16.0D),
WITH_KEYBOARD_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D);

@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return pState.getValue(HAS_KEYBOARD) ? WITH_KEYBOARD_SHAPE : BASE_SHAPE;
}

public KeyboardStandBlock(Properties pProperties) {
super(pProperties);
registerDefaultState(defaultBlockState()
.setValue(FACING, Direction.NORTH)
.setValue(HAS_KEYBOARD, false)
);
}

@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand,
BlockHitResult pHit) {
if (!pState.getValue(HAS_KEYBOARD))
return InteractionResult.FAIL;

return super.use(pState, pLevel, pPos, pPlayer, pHand, pHit);
}

@Override
public void onRemove(BlockState arg0, Level arg1, BlockPos arg2, BlockState arg3, boolean arg4) {
//NOTE: Does not check for creative, because no context is given
if (!arg1.isClientSide && arg0.getValue(HAS_KEYBOARD))
arg1.addFreshEntity(
new ItemEntity(arg1, arg2.getX(), arg2.getY(), arg2.getZ(), new ItemStack(ModItems.KEYBOARD))
);

super.onRemove(arg0, arg1, arg2, arg3, arg4);
}


@Override
protected OpenInstrumentPacketSender instrumentPacketSender() {
return (player, hand) -> ModPacketHandler.sendToClient(new ModOpenInstrumentPacket("keyboard", hand), player);
}

@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}


@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite());
}

@Override
public InstrumentBlockEntity newBlockEntity(BlockPos arg0, BlockState arg1) {
return new ModInstrumentBlockEntity(arg0, arg1);
}

@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING, HAS_KEYBOARD);
}

}
Loading

0 comments on commit 6bba75a

Please sign in to comment.