Block, BlockEntity test

This commit is contained in:
DBotThePony 2021-08-08 14:29:29 +07:00
parent 8e5e451d4c
commit 27ee58f193
Signed by: DBot
GPG Key ID: DCC23B5715498507
6 changed files with 151 additions and 24 deletions

View File

@ -9,9 +9,7 @@ import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
@ -27,6 +25,7 @@ import net.minecraftforge.fmlserverevents.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.dbotthepony.mc.otm.block.BlockAndroidStation;
import ru.dbotthepony.mc.otm.capability.AndroidCapability;
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
@ -39,8 +38,10 @@ public class OverdriveThatMatters {
public static final String MOD_ID = "overdrive_that_matters";
public static final Logger LOGGER = LogManager.getLogger();
public static Block ANDROID_STATION = new Block(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.STONE).requiresCorrectToolForDrops().strength(3F, 6.0F)).setRegistryName(OverdriveThatMatters.MOD_ID, "android_station");
public static Item ANDROID_STATION_ITEM = new BlockItem(ANDROID_STATION, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)).setRegistryName(OverdriveThatMatters.MOD_ID, "android_station");
public static Block ANDROID_STATION = new BlockAndroidStation();
public static BlockEntityType.Builder<BlockAndroidStation.BlockAndroidStationEntity> ANDROID_STATION_BUILDER = BlockEntityType.Builder.of(BlockAndroidStation.BlockAndroidStationEntity::new, ANDROID_STATION);
public static BlockEntityType<BlockAndroidStation.BlockAndroidStationEntity> ANDROID_STATION_FACTORY = null;
public static Item ANDROID_STATION_ITEM = new BlockItem(ANDROID_STATION, new Item.Properties().stacksTo(64).tab(CreativeModeTab.TAB_MISC)).setRegistryName(BlockAndroidStation.REGISTRY_NAME);
// public static final Block STONE = register("stone", new Block(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.STONE).requiresCorrectToolForDrops().strength(1.5F, 6.0F)));
@ -94,19 +95,16 @@ public class OverdriveThatMatters {
@SubscribeEvent
public void onLivingTick(LivingEvent.LivingUpdateEvent event) {
if (!(event.getEntity() instanceof ServerPlayer)) {
return;
if (event.getEntity() instanceof ServerPlayer ply) {
LazyOptional<IAndroidCapability> capability = ply.getCapability(AndroidCapability.CAPABILITY);
if (!capability.isPresent()) {
return;
}
IAndroidCapability android = capability.resolve().get();
android.tick();
}
ServerPlayer ply = (ServerPlayer) event.getEntityLiving();
LazyOptional<IAndroidCapability> capability = ply.getCapability(AndroidCapability.CAPABILITY);
if (!capability.isPresent()) {
return;
}
IAndroidCapability android = capability.resolve().get();
android.tick();
}
// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
@ -115,16 +113,26 @@ public class OverdriveThatMatters {
public static class RegistryEvents {
@SubscribeEvent
public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
// register a new block here
// регистрация блоков
LOGGER.info("HELLO from Register Block");
blockRegistryEvent.getRegistry().register(ANDROID_STATION);
}
@SubscribeEvent
public static void onItemRegistry(final RegistryEvent.Register<Item> blockRegistryEvent) {
// register a new block here
// регистрация предметов
LOGGER.info("HELLO from Register Item");
blockRegistryEvent.getRegistry().register(ANDROID_STATION_ITEM);
}
@SubscribeEvent
public static void onBlockEntityRegistry(final RegistryEvent.Register<BlockEntityType<?>> blockRegistryEvent) {
// регистрация tile entity
LOGGER.info("HELLO from Register BlockEntity");
// build(data_fixer)
ANDROID_STATION_FACTORY = ANDROID_STATION_BUILDER.build(null);
ANDROID_STATION_FACTORY.setRegistryName(BlockAndroidStation.REGISTRY_NAME);
blockRegistryEvent.getRegistry().register(ANDROID_STATION_FACTORY);
}
}
}

View File

@ -0,0 +1,88 @@
package ru.dbotthepony.mc.otm.block;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
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.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import org.apache.logging.log4j.core.jmx.Server;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class BlockAndroidStation extends Block implements EntityBlock {
public static final ResourceLocation REGISTRY_NAME = new ResourceLocation(OverdriveThatMatters.MOD_ID, "android_station");
private final VoxelShape SHAPE = Shapes.box(0, 0, 0, 1, 0.5, 1);
public BlockAndroidStation() {
super(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.STONE).requiresCorrectToolForDrops().strength(3F, 6.0F));
this.setRegistryName(REGISTRY_NAME);
}
@Override
public VoxelShape getShape(BlockState p_151964_, BlockGetter p_151965_, BlockPos p_151966_, CollisionContext p_151967_) {
return SHAPE;
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new BlockAndroidStationEntity(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return level.isClientSide || type != OverdriveThatMatters.ANDROID_STATION_FACTORY ? null : BlockAndroidStationEntity::tick;
}
public static class BlockAndroidStationEntity extends BlockEntity {
public BlockAndroidStationEntity(BlockPos p_155229_, BlockState p_155230_) {
super(OverdriveThatMatters.ANDROID_STATION_FACTORY, p_155229_, p_155230_);
}
@Nonnull
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return super.getCapability(cap, side);
}
public void load(CompoundTag nbt) {
super.load(nbt);
}
void tick() {
if (!hasLevel())
return;
BlockPos pos = getBlockPos();
List<ServerPlayer> players = getLevel().getEntitiesOfClass(ServerPlayer.class, new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1));
OverdriveThatMatters.LOGGER.info("Players standing above {}", players);
}
public static <T extends BlockEntity> void tick(Level level, BlockPos blockPos, BlockState blockState, T t) {
if (t instanceof BlockAndroidStationEntity tile) {
tile.tick();
}
}
}
}

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.capability;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.LongTag;
@ -7,6 +8,8 @@ import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
@ -16,6 +19,7 @@ import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Function;
public class AndroidCapability implements ICapabilityProvider, IAndroidCapability, INBTSerializable<Tag> {
private final LivingEntity ent;
@ -39,12 +43,12 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
@Override
public void deserializeNBT(Tag nbt) {
if (nbt instanceof CompoundTag) {
if (((CompoundTag) nbt).get("energy_stored") != null)
((CompoundTag) nbt).getLong("energy_stored");
if (nbt instanceof CompoundTag compound) {
if (compound.contains("energy_stored"))
energy_stored = compound.getLong("energy_stored");
if (((CompoundTag) nbt).get("energy_stored_max") != null)
((CompoundTag) nbt).getLong("energy_stored_max");
if (compound.contains("energy_stored_max"))
energy_stored_max = compound.getLong("energy_stored_max");
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "overdrive_that_matters:block/android_station"
}
}
}

View File

@ -0,0 +1,17 @@
{
"parent": "block/block",
"elements": [
{
"from": [ 0, 0, 0 ],
"to": [ 16, 8, 16 ],
"faces": {
"down": { "texture": "#down", "cullface": "down" },
"up": { "texture": "#up", "cullface": "up" },
"north": { "texture": "#north", "cullface": "north" },
"south": { "texture": "#south", "cullface": "south" },
"west": { "texture": "#west", "cullface": "west" },
"east": { "texture": "#east", "cullface": "east" }
}
}
]
}

View File

@ -0,0 +1,3 @@
{
"parent": "overdrive_that_matters:block/android_station"
}