Structure Generation!
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-07-20 22:39:30 -04:00
parent 0b53380226
commit 2b9becda18
17 changed files with 346 additions and 74 deletions

View File

@ -13,6 +13,13 @@ group = project.maven_group
minecraft { minecraft {
} }
repositories {
maven {
name = 'Earthcomputer Mods'
url = 'https://dl.bintray.com/earthcomputer/mods'
}
}
dependencies { dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}" minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.minecraft_version}+build.${project.yarn_build}:v2" mappings "net.fabricmc:yarn:${project.minecraft_version}+build.${project.yarn_build}:v2"
@ -27,6 +34,9 @@ dependencies {
modImplementation "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}" modImplementation "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}"
include "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}" include "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}"
modImplementation "net.earthcomputer:libstructure:${project.libstructure_version}"
include "net.earthcomputer:libstructure:${project.libstructure_version}"
} }
processResources { processResources {

View File

@ -17,3 +17,4 @@ org.gradle.jvmargs = -Xmx1G
modmenu_version = 1.14.5+build.30 modmenu_version = 1.14.5+build.30
cloth_config_version = 4.6.0 cloth_config_version = 4.6.0
autoconfig_version = 3.2.0-unstable autoconfig_version = 3.2.0-unstable
libstructure_version = 1.3

View File

@ -1,7 +1,7 @@
package com.thebrokenrail.energonrelics; package com.thebrokenrail.energonrelics;
import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock; import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock;
import com.thebrokenrail.energonrelics.block.TestBlock; import com.thebrokenrail.energonrelics.block.structure.ResearchComplexGeneratorBlock;
import com.thebrokenrail.energonrelics.block.ThermalGlassBlock; import com.thebrokenrail.energonrelics.block.ThermalGlassBlock;
import com.thebrokenrail.energonrelics.block.battery.ActiveBatteryControllerBlock; import com.thebrokenrail.energonrelics.block.battery.ActiveBatteryControllerBlock;
import com.thebrokenrail.energonrelics.block.reactor.ReactorControllerBlock; import com.thebrokenrail.energonrelics.block.reactor.ReactorControllerBlock;
@ -14,11 +14,11 @@ import com.thebrokenrail.energonrelics.block.EnergonLightBlock;
import com.thebrokenrail.energonrelics.block.SolarPanelBlock; import com.thebrokenrail.energonrelics.block.SolarPanelBlock;
import com.thebrokenrail.energonrelics.block.SwitchBlock; import com.thebrokenrail.energonrelics.block.SwitchBlock;
import com.thebrokenrail.energonrelics.block.util.SimpleBlock; import com.thebrokenrail.energonrelics.block.util.SimpleBlock;
import com.thebrokenrail.energonrelics.client.config.UserConfig;
import com.thebrokenrail.energonrelics.item.MultimeterItem; import com.thebrokenrail.energonrelics.item.MultimeterItem;
import com.thebrokenrail.energonrelics.item.NetworkChipItem; import com.thebrokenrail.energonrelics.item.NetworkChipItem;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import com.thebrokenrail.energonrelics.structure.feature.ResearchComplexFeature;
import me.sargunvohra.mcmods.autoconfig1u.serializer.GsonConfigSerializer; import com.thebrokenrail.energonrelics.structure.feature.ResearchComplexGenerator;
import net.earthcomputer.libstructure.LibStructure;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -28,12 +28,19 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.structure.StructurePieceType;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries; import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biomes;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.chunk.StructureConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature; import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import net.minecraft.world.gen.feature.Feature; import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.FeatureConfig;
import net.minecraft.world.gen.feature.OreFeatureConfig; import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraft.world.gen.feature.StructureFeature;
public class EnergonRelics implements ModInitializer { public class EnergonRelics implements ModInitializer {
public static final String NAMESPACE = "energonrelics"; public static final String NAMESPACE = "energonrelics";
@ -68,6 +75,11 @@ public class EnergonRelics implements ModInitializer {
public static final Item DEFENSIVE_LASER_CORE_ITEM = new Item(new Item.Settings().group(ITEM_GROUP)); public static final Item DEFENSIVE_LASER_CORE_ITEM = new Item(new Item.Settings().group(ITEM_GROUP));
public static final DefensiveLaserBlock DEFENSIVE_LASER_BLOCK = new DefensiveLaserBlock(); public static final DefensiveLaserBlock DEFENSIVE_LASER_BLOCK = new DefensiveLaserBlock();
public static final ResearchComplexGeneratorBlock RESEARCH_COMPLEX_GENERATOR_BLOCK = new ResearchComplexGeneratorBlock();
public static StructureFeature<DefaultFeatureConfig> RESEARCH_COMPLEX_STRUCTURE_FEATURE = new ResearchComplexFeature(DefaultFeatureConfig.CODEC);
public static StructurePieceType RESEARCH_COMPLEX_STRUCTURE_PIECE = (structureManager, tag) -> new ResearchComplexGenerator.Piece(tag);
@Override @Override
public void onInitialize() { public void onInitialize() {
NETWORK_CHIP_ITEM = Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "network_chip"), new NetworkChipItem()); NETWORK_CHIP_ITEM = Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "network_chip"), new NetworkChipItem());
@ -97,6 +109,10 @@ public class EnergonRelics implements ModInitializer {
Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "defensive_laser_core"), DEFENSIVE_LASER_CORE_ITEM); Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "defensive_laser_core"), DEFENSIVE_LASER_CORE_ITEM);
DEFENSIVE_LASER_BLOCK.register("defensive_laser"); DEFENSIVE_LASER_BLOCK.register("defensive_laser");
new TestBlock().register("test_block"); RESEARCH_COMPLEX_GENERATOR_BLOCK.register("research_complex_generator");
Registry.register(Registry.STRUCTURE_PIECE, new Identifier(NAMESPACE, "research_complex_piece"), RESEARCH_COMPLEX_STRUCTURE_PIECE);
Biomes.PLAINS.addStructureFeature(RESEARCH_COMPLEX_STRUCTURE_FEATURE.configure(FeatureConfig.DEFAULT));
LibStructure.registerStructure(new Identifier(NAMESPACE, "research_complex"), RESEARCH_COMPLEX_STRUCTURE_FEATURE, GenerationStep.Feature.UNDERGROUND_STRUCTURES, new StructureConfig(32, 8, 14357618), RESEARCH_COMPLEX_STRUCTURE_FEATURE.configure(FeatureConfig.DEFAULT));
} }
} }

View File

@ -1,28 +0,0 @@
package com.thebrokenrail.energonrelics.block;
import com.thebrokenrail.energonrelics.block.util.SimpleBlock;
import com.thebrokenrail.energonrelics.structure.StructurePlacer;
import com.thebrokenrail.energonrelics.structure.researchcomplex.ResearchComplexStartPart;
import com.thebrokenrail.energonrelics.structure.researchcomplex.ResearchComplexState;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Collections;
public class TestBlock extends SimpleBlock {
public TestBlock() {
super(FabricBlockSettings.of(Material.STONE));
}
@SuppressWarnings("deprecation")
@Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
super.onBlockAdded(state, world, pos, oldState, notify);
if (!world.isClient()) {
new StructurePlacer(new ResearchComplexStartPart(new ResearchComplexState(world, world.random), Collections.emptyList())).place(world, pos);
}
}
}

View File

@ -0,0 +1,52 @@
package com.thebrokenrail.energonrelics.block.entity.structure;
import com.thebrokenrail.energonrelics.block.structure.ResearchComplexGeneratorBlock;
import com.thebrokenrail.energonrelics.structure.StructurePart;
import com.thebrokenrail.energonrelics.structure.StructurePlacer;
import com.thebrokenrail.energonrelics.structure.researchcomplex.ResearchComplexStartPart;
import com.thebrokenrail.energonrelics.structure.researchcomplex.ResearchComplexState;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.Direction;
import java.util.Collections;
import java.util.Objects;
import java.util.Random;
public class ResearchComplexGeneratorBlockEntity extends BlockEntity implements Tickable {
private long seed = 0;
public ResearchComplexGeneratorBlockEntity(BlockEntityType<?> type) {
super(type);
}
public void setSeed(long seed) {
this.seed = seed;
}
@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
tag.putLong("Seed", seed);
return tag;
}
@Override
public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
seed = tag.getLong("Seed");
}
@Override
public void tick() {
if (hasWorld() && !Objects.requireNonNull(getWorld()).isClient()) {
Direction facing = getCachedState().get(ResearchComplexGeneratorBlock.HORIZONTAL_FACING);
getWorld().setBlockState(getPos(), Blocks.STONE.getDefaultState());
new StructurePlacer(new ResearchComplexStartPart(new ResearchComplexState(getWorld(), new Random(seed)), Collections.singletonList(StructurePart.getTransformationFromDirection(facing)))).place(getWorld(), getPos());
}
}
}

View File

@ -0,0 +1,41 @@
package com.thebrokenrail.energonrelics.block.structure;
import com.thebrokenrail.energonrelics.block.entity.structure.ResearchComplexGeneratorBlockEntity;
import com.thebrokenrail.energonrelics.block.util.SimpleBlockWithEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.Direction;
import java.util.function.Function;
public class ResearchComplexGeneratorBlock extends SimpleBlockWithEntity {
public static DirectionProperty HORIZONTAL_FACING = Properties.HORIZONTAL_FACING;
public ResearchComplexGeneratorBlock() {
super(FabricBlockSettings.of(Material.STONE).strength(-1.0F, 3600000.0F).dropsNothing());
setDefaultState(getDefaultState().with(HORIZONTAL_FACING, Direction.NORTH));
}
@Override
protected boolean registerItem() {
return false;
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(HORIZONTAL_FACING);
}
@Override
protected Function<BlockEntityType<BlockEntity>, BlockEntity> getFactory() {
return ResearchComplexGeneratorBlockEntity::new;
}
}

View File

@ -2,7 +2,6 @@ package com.thebrokenrail.energonrelics.block.util;
import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.client.block.entity.render.HighlightBlockEntityRenderer; import com.thebrokenrail.energonrelics.client.block.entity.render.HighlightBlockEntityRenderer;
import com.thebrokenrail.energonrelics.component.NetworkComponent;
import com.thebrokenrail.energonrelics.energy.core.EnergyProviderBlockEntity; import com.thebrokenrail.energonrelics.energy.core.EnergyProviderBlockEntity;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
@ -13,7 +12,6 @@ import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer; import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.BlockHitResult;
@ -58,7 +56,7 @@ public abstract class EnergyProviderBlock extends SimpleBlockWithEntity {
if (stack.getItem() == EnergonRelics.NETWORK_CHIP_ITEM) { if (stack.getItem() == EnergonRelics.NETWORK_CHIP_ITEM) {
if (!((EnergyProviderBlockEntity) entity).hasStack()) { if (!((EnergyProviderBlockEntity) entity).hasStack()) {
if (!world.isClient()) { if (!world.isClient()) {
((EnergyProviderBlockEntity) entity).placeStack(stack); ((EnergyProviderBlockEntity) entity).placeStack(stack, world);
stack.setCount(0); stack.setCount(0);
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
@ -68,7 +66,7 @@ public abstract class EnergyProviderBlock extends SimpleBlockWithEntity {
} else if (stack.isEmpty() && hand == Hand.MAIN_HAND) { } else if (stack.isEmpty() && hand == Hand.MAIN_HAND) {
if (((EnergyProviderBlockEntity) entity).hasStack()) { if (((EnergyProviderBlockEntity) entity).hasStack()) {
if (!world.isClient()) { if (!world.isClient()) {
ItemStack newStack = ((EnergyProviderBlockEntity) entity).takeStack(); ItemStack newStack = ((EnergyProviderBlockEntity) entity).takeStack(world);
player.setStackInHand(hand, newStack); player.setStackInHand(hand, newStack);
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;

View File

@ -14,6 +14,12 @@ public class SimpleBlock extends Block {
public void register(String name) { public void register(String name) {
Registry.register(Registry.BLOCK, new Identifier(EnergonRelics.NAMESPACE, name), this); Registry.register(Registry.BLOCK, new Identifier(EnergonRelics.NAMESPACE, name), this);
Registry.register(Registry.ITEM, new Identifier(EnergonRelics.NAMESPACE, name), new BlockItem(this, new Item.Settings().group(EnergonRelics.ITEM_GROUP))); if (registerItem()) {
Registry.register(Registry.ITEM, new Identifier(EnergonRelics.NAMESPACE, name), new BlockItem(this, new Item.Settings().group(EnergonRelics.ITEM_GROUP)));
}
}
protected boolean registerItem() {
return true;
} }
} }

View File

@ -2,12 +2,16 @@ package com.thebrokenrail.energonrelics.component;
import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.energy.core.util.EnergyProvider; import com.thebrokenrail.energonrelics.energy.core.util.EnergyProvider;
import com.thebrokenrail.energonrelics.util.BlockPosWithDimension;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.datafixer.NbtOps;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -19,7 +23,7 @@ import java.util.Objects;
public class NetworkComponent extends PersistentState { public class NetworkComponent extends PersistentState {
private static class Entry { private static class Entry {
private final List<BlockPos> sources = new ArrayList<>(); private final List<BlockPosWithDimension> sources = new ArrayList<>();
private int id = 0; private int id = 0;
} }
@ -52,7 +56,8 @@ public class NetworkComponent extends PersistentState {
int x = sources.getCompound(i).getInt("X"); int x = sources.getCompound(i).getInt("X");
int y = sources.getCompound(i).getInt("Y"); int y = sources.getCompound(i).getInt("Y");
int z = sources.getCompound(i).getInt("Z"); int z = sources.getCompound(i).getInt("Z");
entry.sources.add(new BlockPos(x, y, z)); RegistryKey<World> dimension = World.CODEC.parse(NbtOps.INSTANCE, sources.getCompound(i).get("Dimension")).result().orElse(World.OVERWORLD);
entry.sources.add(new BlockPosWithDimension(new BlockPos(x, y, z), dimension));
} }
} }
return entry; return entry;
@ -62,11 +67,12 @@ public class NetworkComponent extends PersistentState {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
tag.putInt("ID", entry.id); tag.putInt("ID", entry.id);
ListTag sources = new ListTag(); ListTag sources = new ListTag();
for (BlockPos pos : entry.sources) { for (BlockPosWithDimension pos : entry.sources) {
CompoundTag posTag = new CompoundTag(); CompoundTag posTag = new CompoundTag();
posTag.putInt("X", pos.getX()); posTag.putInt("X", pos.pos.getX());
posTag.putInt("Y", pos.getY()); posTag.putInt("Y", pos.pos.getY());
posTag.putInt("Z", pos.getZ()); posTag.putInt("Z", pos.pos.getZ());
Identifier.CODEC.encodeStart(NbtOps.INSTANCE, pos.dimension.getValue()).result().ifPresent(tags -> posTag.put("Dimension", tags));
sources.add(posTag); sources.add(posTag);
} }
tag.put("Sources", sources); tag.put("Sources", sources);
@ -101,21 +107,21 @@ public class NetworkComponent extends PersistentState {
return Objects.requireNonNull(world.getServer().getWorld(World.OVERWORLD)).getPersistentStateManager().getOrCreate(NetworkComponent::new, EnergonRelics.NAMESPACE); return Objects.requireNonNull(world.getServer().getWorld(World.OVERWORLD)).getPersistentStateManager().getOrCreate(NetworkComponent::new, EnergonRelics.NAMESPACE);
} }
public List<BlockPos> getSourcePos(int id) { public List<BlockPosWithDimension> getSourcePos(int id) {
Entry entry = getOrCreate(id); Entry entry = getOrCreate(id);
return entry.sources; return entry.sources;
} }
public List<EnergyProvider> getSource(World world, int id) { public List<EnergyProvider> getSource(World world, int id) {
List<BlockPos> sources = getSourcePos(id); List<BlockPosWithDimension> sources = getSourcePos(id);
List<BlockPos> valid = new ArrayList<>(); List<BlockPosWithDimension> valid = new ArrayList<>();
Iterator<BlockPos> iterator = sources.iterator(); Iterator<BlockPosWithDimension> iterator = sources.iterator();
List<EnergyProvider> providers = new ArrayList<>(); List<EnergyProvider> providers = new ArrayList<>();
boolean dirty = false; boolean dirty = false;
while (iterator.hasNext()) { while (iterator.hasNext()) {
BlockPos pos = iterator.next(); BlockPosWithDimension pos = iterator.next();
if (!valid.contains(pos)) { if (!valid.contains(pos)) {
BlockEntity entity = world.getBlockEntity(pos); BlockEntity entity = Objects.requireNonNull(Objects.requireNonNull(world.getServer()).getWorld(pos.dimension)).getBlockEntity(pos.pos);
if (entity instanceof EnergyProvider && ((EnergyProvider) entity).isNetwork(id)) { if (entity instanceof EnergyProvider && ((EnergyProvider) entity).isNetwork(id)) {
providers.add((EnergyProvider) entity); providers.add((EnergyProvider) entity);
} else { } else {
@ -135,7 +141,7 @@ public class NetworkComponent extends PersistentState {
return providers; return providers;
} }
public void addSource(int id, BlockPos pos) { public void addSource(int id, BlockPosWithDimension pos) {
Entry entry = getOrCreate(id); Entry entry = getOrCreate(id);
if (!entry.sources.contains(pos)) { if (!entry.sources.contains(pos)) {
entry.sources.add(pos); entry.sources.add(pos);
@ -143,7 +149,7 @@ public class NetworkComponent extends PersistentState {
markDirty(); markDirty();
} }
public void removeSource(int id, BlockPos pos) { public void removeSource(int id, BlockPosWithDimension pos) {
Entry entry = getOrCreate(id); Entry entry = getOrCreate(id);
entry.sources.remove(pos); entry.sources.remove(pos);
markDirty(); markDirty();
@ -160,7 +166,7 @@ public class NetworkComponent extends PersistentState {
@Override @Override
public void markDirty() { public void markDirty() {
List<Integer> ids = new ArrayList<>(); List<Integer> ids = new ArrayList<>();
List<BlockPos> positions = new ArrayList<>(); List<BlockPosWithDimension> positions = new ArrayList<>();
Iterator<Entry> iterator = networks.iterator(); Iterator<Entry> iterator = networks.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entry entry = iterator.next(); Entry entry = iterator.next();

View File

@ -5,6 +5,7 @@ import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.component.NetworkComponent; import com.thebrokenrail.energonrelics.component.NetworkComponent;
import com.thebrokenrail.energonrelics.energy.core.util.Action; import com.thebrokenrail.energonrelics.energy.core.util.Action;
import com.thebrokenrail.energonrelics.energy.core.util.EnergyProvider; import com.thebrokenrail.energonrelics.energy.core.util.EnergyProvider;
import com.thebrokenrail.energonrelics.util.BlockPosWithDimension;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -16,6 +17,7 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable; import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.ArrayList; import java.util.ArrayList;
@ -59,6 +61,11 @@ public class EnergyProviderBlockEntity extends BlockEntity implements EnergyProv
return getPos().isWithinDistance(pos, HardcodedConfig.POWER_RANGE); return getPos().isWithinDistance(pos, HardcodedConfig.POWER_RANGE);
} }
@Override
public RegistryKey<World> getRegistryKey() {
return Objects.requireNonNull(getWorld()).getRegistryKey();
}
private ItemStack stack = ItemStack.EMPTY; private ItemStack stack = ItemStack.EMPTY;
@Override @Override
@ -99,9 +106,9 @@ public class EnergyProviderBlockEntity extends BlockEntity implements EnergyProv
} }
NetworkComponent component = NetworkComponent.getInstance((ServerWorld) Objects.requireNonNull(getWorld())); NetworkComponent component = NetworkComponent.getInstance((ServerWorld) Objects.requireNonNull(getWorld()));
List<BlockPos> sources = component.getSourcePos(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack)); List<BlockPosWithDimension> sources = component.getSourcePos(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack));
if (!sources.contains(getPos())) { if (!sources.contains(new BlockPosWithDimension(getPos(), getWorld().getRegistryKey()))) {
takeStack(); takeStack(getWorld());
} }
} }
pendingPropagatedActions.clear(); pendingPropagatedActions.clear();
@ -121,23 +128,24 @@ public class EnergyProviderBlockEntity extends BlockEntity implements EnergyProv
private static void setEnergyProviderSource(World world, BlockPos pos, ItemStack stack, boolean remove) { private static void setEnergyProviderSource(World world, BlockPos pos, ItemStack stack, boolean remove) {
ServerWorld serverWorld = (ServerWorld) world; ServerWorld serverWorld = (ServerWorld) world;
NetworkComponent component = NetworkComponent.getInstance(serverWorld); NetworkComponent component = NetworkComponent.getInstance(serverWorld);
BlockPosWithDimension newPos = new BlockPosWithDimension(pos, world.getRegistryKey());
if (remove) { if (remove) {
component.removeSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), pos); component.removeSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), newPos);
} else { } else {
component.addSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), pos); component.addSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), newPos);
} }
} }
public ItemStack takeStack() { public ItemStack takeStack(World world) {
ItemStack newStack = stack.copy(); ItemStack newStack = stack.copy();
setEnergyProviderSource(getWorld(), getPos(), newStack, true); setEnergyProviderSource(world, getPos(), newStack, true);
stack = ItemStack.EMPTY; stack = ItemStack.EMPTY;
markDirty(); markDirty();
return newStack; return newStack;
} }
public void placeStack(ItemStack newStack) { public void placeStack(ItemStack newStack, World world) {
setEnergyProviderSource(getWorld(), getPos(), newStack, false); setEnergyProviderSource(world, getPos(), newStack, false);
stack = newStack.copy(); stack = newStack.copy();
markDirty(); markDirty();
} }
@ -159,6 +167,8 @@ public class EnergyProviderBlockEntity extends BlockEntity implements EnergyProv
@Override @Override
public void markDirty() { public void markDirty() {
super.markDirty(); super.markDirty();
sync(); if (hasWorld()) {
sync();
}
} }
} }

View File

@ -10,6 +10,8 @@ import net.minecraft.nbt.IntArrayTag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable; import net.minecraft.util.Tickable;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -32,6 +34,11 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
public boolean isWithinDistance(Vec3d pos) { public boolean isWithinDistance(Vec3d pos) {
return true; return true;
} }
@Override
public RegistryKey<World> getRegistryKey() {
return Objects.requireNonNull(getWorld()).getRegistryKey();
}
} }
public EnergyReceiverBlockEntity(BlockEntityType<?> type) { public EnergyReceiverBlockEntity(BlockEntityType<?> type) {
@ -74,9 +81,11 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
for (int network : networks) { for (int network : networks) {
List<EnergyProvider> potentialProviders = component.getSource(world, network); List<EnergyProvider> potentialProviders = component.getSource(world, network);
for (EnergyProvider provider : potentialProviders) { for (EnergyProvider provider : potentialProviders) {
Vec3d pos = new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d); if (provider.getRegistryKey().equals(getWorld().getRegistryKey())) {
if (provider.isWithinDistance(pos)) { Vec3d pos = new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d);
providers.add(provider); if (provider.isWithinDistance(pos)) {
providers.add(provider);
}
} }
} }
} }

View File

@ -1,9 +1,12 @@
package com.thebrokenrail.energonrelics.energy.core.util; package com.thebrokenrail.energonrelics.energy.core.util;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
public interface EnergyProvider { public interface EnergyProvider {
void addPropagatedAction(Action.PropagatedAction action); void addPropagatedAction(Action.PropagatedAction action);
boolean isNetwork(int network); boolean isNetwork(int network);
boolean isWithinDistance(Vec3d pos); boolean isWithinDistance(Vec3d pos);
RegistryKey<World> getRegistryKey();
} }

View File

@ -7,6 +7,7 @@ import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockMirror; import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation; import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.ArrayList; import java.util.ArrayList;
@ -152,22 +153,22 @@ public abstract class StructurePart<T> {
protected abstract void handleBlockPlace(World world, BlockPos pos, BlockState state); protected abstract void handleBlockPlace(World world, BlockPos pos, BlockState state);
public static Transformation getTransformationFromBlockRotation(BlockRotation rotation) { public static Transformation getTransformationFromDirection(Direction direction) {
switch (rotation) { switch (direction) {
case NONE: { case NORTH: {
return NONE; return NONE;
} }
case CLOCKWISE_90: { case EAST: {
return CLOCKWISE_90; return CLOCKWISE_90;
} }
case CLOCKWISE_180: { case SOUTH: {
return CLOCKWISE_180; return CLOCKWISE_180;
} }
case COUNTERCLOCKWISE_90: { case WEST: {
return COUNTERCLOCKWISE_90; return COUNTERCLOCKWISE_90;
} }
default: { default: {
throw new MissingCaseException(rotation); throw new MissingCaseException(direction);
} }
} }
} }

View File

@ -0,0 +1,39 @@
package com.thebrokenrail.energonrelics.structure.feature;
import com.mojang.serialization.Codec;
import net.minecraft.structure.StructureManager;
import net.minecraft.structure.StructureStart;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.DynamicRegistryManager;
import net.minecraft.world.Heightmap;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import net.minecraft.world.gen.feature.StructureFeature;
public class ResearchComplexFeature extends StructureFeature<DefaultFeatureConfig> {
public ResearchComplexFeature(Codec<DefaultFeatureConfig> codec) {
super(codec);
}
@Override
public StructureStartFactory<DefaultFeatureConfig> getStructureStartFactory() {
return Start::new;
}
public static class Start extends StructureStart<DefaultFeatureConfig> {
public Start(StructureFeature<DefaultFeatureConfig> structureFeature, int i, int j, BlockBox blockBox, int k, long l) {
super(structureFeature, i, j, blockBox, k, l);
}
@Override
public void init(DynamicRegistryManager dynamicRegistryManager, ChunkGenerator chunkGenerator, StructureManager structureManager, int x, int z, Biome biome, DefaultFeatureConfig defaultFeatureConfig) {
BlockPos blockPos = new BlockPos(x * 16, chunkGenerator.getHeight(x, z, Heightmap.Type.WORLD_SURFACE) / 2, z * 16);
BlockRotation blockRotation = BlockRotation.random(random);
ResearchComplexGenerator.addPieces(blockPos, blockRotation, children);
setBoundingBoxFromChildren();
}
}
}

View File

@ -0,0 +1,82 @@
package com.thebrokenrail.energonrelics.structure.feature;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.entity.structure.ResearchComplexGeneratorBlockEntity;
import com.thebrokenrail.energonrelics.block.structure.ResearchComplexGeneratorBlock;
import com.thebrokenrail.energonrelics.util.MissingCaseException;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.structure.StructurePiece;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import java.util.List;
import java.util.Random;
public class ResearchComplexGenerator {
static void addPieces(BlockPos pos, BlockRotation rotation, List<StructurePiece> pieces) {
pieces.add(new Piece(rotation, pos));
}
public static class Piece extends StructurePiece {
private final BlockRotation rotation;
private final BlockPos pos;
private Piece(BlockRotation rotation, BlockPos pos) {
super(EnergonRelics.RESEARCH_COMPLEX_STRUCTURE_PIECE, 0);
this.rotation = rotation;
this.pos = pos;
boundingBox = BlockBox.create(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
public Piece(CompoundTag tag) {
super(EnergonRelics.RESEARCH_COMPLEX_STRUCTURE_PIECE, tag);
rotation = BlockRotation.valueOf(tag.getString("Rot"));
pos = new BlockPos(tag.getInt("X"), tag.getInt("Y"), tag.getInt("Z"));
}
@Override
protected void toNbt(CompoundTag tag) {
tag.putString("Rot", rotation.name());
tag.putInt("X", pos.getX());
tag.putInt("Y", pos.getY());
tag.putInt("Z", pos.getZ());
}
@Override
public boolean generate(ServerWorldAccess world, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox boundingBox, ChunkPos chunkPos, BlockPos blockPos) {
world.setBlockState(pos, EnergonRelics.RESEARCH_COMPLEX_GENERATOR_BLOCK.getDefaultState().with(ResearchComplexGeneratorBlock.HORIZONTAL_FACING, blockRotationToDirection(rotation)), 4);
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof ResearchComplexGeneratorBlockEntity) {
((ResearchComplexGeneratorBlockEntity) entity).setSeed(random.nextLong());
}
return true;
}
private static Direction blockRotationToDirection(BlockRotation rotation) {
switch (rotation) {
case NONE: {
return Direction.NORTH;
}
case CLOCKWISE_90: {
return Direction.EAST;
}
case CLOCKWISE_180: {
return Direction.SOUTH;
}
case COUNTERCLOCKWISE_90: {
return Direction.WEST;
}
default: {
throw new MissingCaseException(rotation);
}
}
}
}
}

View File

@ -108,7 +108,7 @@ public class ResearchComplexStartPart extends BaseResearchComplexPart {
if (state.getBlock() == EnergonRelics.REACTOR_CONTROLLER_BLOCK) { if (state.getBlock() == EnergonRelics.REACTOR_CONTROLLER_BLOCK) {
BlockEntity entity = world.getBlockEntity(pos); BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof ReactorControllerBlockEntity) { if (entity instanceof ReactorControllerBlockEntity) {
((ReactorControllerBlockEntity) entity).placeStack(EnergonRelics.NETWORK_CHIP_ITEM.create(getState().getMainNetwork())); ((ReactorControllerBlockEntity) entity).placeStack(EnergonRelics.NETWORK_CHIP_ITEM.create(getState().getMainNetwork()), world);
} }
} }
} }

View File

@ -0,0 +1,26 @@
package com.thebrokenrail.energonrelics.util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
public class BlockPosWithDimension {
public final BlockPos pos;
public final RegistryKey<World> dimension;
public BlockPosWithDimension(BlockPos pos, RegistryKey<World> dimension) {
this.pos = pos;
this.dimension = dimension;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof BlockPosWithDimension) {
return pos.equals(((BlockPosWithDimension) obj).pos) && dimension.equals(((BlockPosWithDimension) obj).dimension);
} else {
return false;
}
}
}