From 27ee58f1931e62012eb22985d8289a3f846dda4a Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 8 Aug 2021 14:29:29 +0700 Subject: [PATCH] Block, BlockEntity test --- .../mc/otm/OverdriveThatMatters.java | 46 ++++++---- .../mc/otm/block/BlockAndroidStation.java | 88 +++++++++++++++++++ .../mc/otm/capability/AndroidCapability.java | 14 +-- .../blockstates/android_station.json | 7 ++ .../models/block/android_station.json | 17 ++++ .../models/item/android_station.json | 3 + 6 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java create mode 100644 src/main/resources/assets/overdrive_that_matters/blockstates/android_station.json create mode 100644 src/main/resources/assets/overdrive_that_matters/models/block/android_station.json create mode 100644 src/main/resources/assets/overdrive_that_matters/models/item/android_station.json diff --git a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java index c35d89cc8..1ddf1485c 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java +++ b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java @@ -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 ANDROID_STATION_BUILDER = BlockEntityType.Builder.of(BlockAndroidStation.BlockAndroidStationEntity::new, ANDROID_STATION); + public static BlockEntityType 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 capability = ply.getCapability(AndroidCapability.CAPABILITY); + + if (!capability.isPresent()) { + return; + } + + IAndroidCapability android = capability.resolve().get(); + android.tick(); } - - ServerPlayer ply = (ServerPlayer) event.getEntityLiving(); - LazyOptional 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 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 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> 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); + } } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java new file mode 100644 index 000000000..f4383402b --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockAndroidStation.java @@ -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 BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType 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 LazyOptional getCapability(@Nonnull Capability 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 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 void tick(Level level, BlockPos blockPos, BlockState blockState, T t) { + if (t instanceof BlockAndroidStationEntity tile) { + tile.tick(); + } + } + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapability.java b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapability.java index ff124279b..5ce5b0500 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapability.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapability.java @@ -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 { 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"); } } diff --git a/src/main/resources/assets/overdrive_that_matters/blockstates/android_station.json b/src/main/resources/assets/overdrive_that_matters/blockstates/android_station.json new file mode 100644 index 000000000..6f3c0262f --- /dev/null +++ b/src/main/resources/assets/overdrive_that_matters/blockstates/android_station.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "overdrive_that_matters:block/android_station" + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/overdrive_that_matters/models/block/android_station.json b/src/main/resources/assets/overdrive_that_matters/models/block/android_station.json new file mode 100644 index 000000000..bfbdd98a4 --- /dev/null +++ b/src/main/resources/assets/overdrive_that_matters/models/block/android_station.json @@ -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" } + } + } + ] +} diff --git a/src/main/resources/assets/overdrive_that_matters/models/item/android_station.json b/src/main/resources/assets/overdrive_that_matters/models/item/android_station.json new file mode 100644 index 000000000..5643fca3c --- /dev/null +++ b/src/main/resources/assets/overdrive_that_matters/models/item/android_station.json @@ -0,0 +1,3 @@ +{ + "parent": "overdrive_that_matters:block/android_station" +} \ No newline at end of file