Files
models
src
textures
build.gradle
gradle.properties
settings.gradle
Code
50 2,196
src/main/java/com/kodari/airportfurniture/AirportFurnitureMod.java
package com.kodari.airportfurniture;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.Interaction;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
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.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
public final class AirportFurnitureMod implements ModInitializer {
public static final String MOD_ID = "airportfurniture";
private static final String SEAT_TAG = MOD_ID + ":seat";
private static final String[] FURNITURE = {
"barrier",
"bin",
"chair_single",
"chair_third",
"conveyor_belt",
"conveyor_belt_corner",
"flight_signs",
"luggage",
"metal_detector",
"packing_box",
"packing_box_blank",
"sign_main_gate",
"signpost",
"table_with_computer",
"ticket_machine",
"ticket_machine_02",
"time_zone",
"trolley_1",
"trolley_blank",
"trolley_with_bag",
"trolley_with_bag_2",
"xray"
};
@Override
public void onInitialize() {
List<BlockItem> furnitureItems = new ArrayList<>();
for (String id : FURNITURE) {
Identifier identifier = Identifier.fromNamespaceAndPath(MOD_ID, id);
ResourceKey<Block> blockKey = ResourceKey.create(Registries.BLOCK, identifier);
BlockBehaviour.Properties properties = BlockBehaviour.Properties.of().strength(1.5F, 3.0F).setId(blockKey);
Block block = id.startsWith("chair_") ? new ChairBlock(properties) : new FurnitureBlock(properties);
Registry.register(BuiltInRegistries.BLOCK, identifier, block);
ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM, identifier);
BlockItem blockItem = new BlockItem(block, new Item.Properties().setId(itemKey));
Registry.register(BuiltInRegistries.ITEM, identifier, blockItem);
furnitureItems.add(blockItem);
}
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "building_blocks")
)).register(output -> furnitureItems.forEach(output::prepend));
ServerTickEvents.END_LEVEL_TICK.register(level -> {
for (Entity entity : level.getAllEntities()) {
if (entity instanceof Interaction && entity.entityTags().contains(SEAT_TAG) && entity.getPassengers().isEmpty()) {
entity.discard();
}
}
});
}
private static class FurnitureBlock extends Block {
private FurnitureBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected List<ItemStack> getDrops(BlockState state, LootParams.Builder params) {
return List.of(new ItemStack(this));
}
}
private static final class ChairBlock extends FurnitureBlock {
private static final VoxelShape SEAT_SHAPE = Block.box(0, 0, 0, 16, 8, 16);
private ChairBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SEAT_SHAPE;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
return sit(level, pos, player);
}
@Override
protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
return sit(level, pos, player);
}
private InteractionResult sit(Level level, BlockPos pos, Player player) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
Interaction seat = EntityTypes.INTERACTION.create(level, EntitySpawnReason.TRIGGERED);
if (seat != null) {
seat.addTag(SEAT_TAG);
seat.setPos(pos.getX() + 0.5, pos.getY() + 1.1, pos.getZ() + 0.5);
if (level.addFreshEntity(seat)) {
if (!player.startRiding(seat)) {
seat.discard();
}
} else {
seat.discard();
}
}
return InteractionResult.SUCCESS_SERVER;
}
}
}