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

This commit is contained in:
TheBrokenRail 2020-07-20 18:40:56 -04:00
parent 58b97a8d80
commit 0b53380226
4 changed files with 114 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package com.thebrokenrail.energonrelics.structure;
import com.thebrokenrail.energonrelics.util.MissingCaseException; import com.thebrokenrail.energonrelics.util.MissingCaseException;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
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;
@ -86,7 +87,11 @@ public abstract class StructurePart<T> {
@Override @Override
public BlockState transform(BlockState state) { public BlockState transform(BlockState state) {
return state.mirror(BlockMirror.LEFT_RIGHT); state = state.mirror(BlockMirror.LEFT_RIGHT);
if (state.contains(Properties.CHEST_TYPE)) {
state = state.with(Properties.CHEST_TYPE, state.get(Properties.CHEST_TYPE).getOpposite());
}
return state;
} }
}; };

View File

@ -3,11 +3,16 @@ package com.thebrokenrail.energonrelics.structure.researchcomplex;
import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.config.HardcodedConfig; import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.structure.StructureContext; import com.thebrokenrail.energonrelics.structure.StructureContext;
import com.thebrokenrail.energonrelics.util.WeightedList;
import net.minecraft.block.BedBlock;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.DoorBlock; import net.minecraft.block.DoorBlock;
import net.minecraft.block.entity.BarrelBlockEntity;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.block.enums.BedPart;
import net.minecraft.block.enums.ChestType;
import net.minecraft.block.enums.DoubleBlockHalf; import net.minecraft.block.enums.DoubleBlockHalf;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -78,16 +83,17 @@ public abstract class AbstractResearchComplexRoomPart extends BaseResearchComple
abstract int getWidth(); abstract int getWidth();
static AbstractResearchComplexRoomPart get(ResearchComplexState state, List<Transformation> transformations) { @Override
if (state.random.nextFloat() <= Barrel.CHANCE) { protected void handleBlockPlace(World world, BlockPos pos, BlockState state) {
return new Barrel(state, transformations); super.handleBlockPlace(world, pos, state);
} else { BlockEntity entity = world.getBlockEntity(pos);
return new Simple(state, transformations); if (entity instanceof LootableContainerBlockEntity) {
((LootableContainerBlockEntity) entity).setLootTable(new Identifier(EnergonRelics.NAMESPACE, "chests/research_complex"), getState().random.nextLong());
} }
} }
private static class Simple extends AbstractResearchComplexRoomPart { private static class BedRoom extends AbstractResearchComplexRoomPart {
private Simple(ResearchComplexState state, List<Transformation> list) { private BedRoom(ResearchComplexState state, List<Transformation> list) {
super(state, list); super(state, list);
} }
@ -98,14 +104,49 @@ public abstract class AbstractResearchComplexRoomPart extends BaseResearchComple
@Override @Override
protected void buildInterior() { protected void buildInterior() {
BlockState chest = Blocks.CHEST.getDefaultState();
BlockState bed = Blocks.RED_BED.getDefaultState();
BlockState bedFoot = bed.with(BedBlock.PART, BedPart.FOOT);
BlockState bedHead = bed.with(BedBlock.PART, BedPart.HEAD);
set(new BlockPos(-1, 0, 2), bedHead);
set(new BlockPos(-1, 0, 3), bedFoot);
set(new BlockPos(-1, 0, 4), chest);
set(new BlockPos(1, 0, 2), bedHead);
set(new BlockPos(1, 0, 3), bedFoot);
set(new BlockPos(1, 0, 4), chest);
} }
} }
private static class Barrel extends AbstractResearchComplexRoomPart { private static class CraftingRoom extends AbstractResearchComplexRoomPart {
private static final float CHANCE = 0.05f; private CraftingRoom(ResearchComplexState state, List<Transformation> list) {
super(state, list);
}
private Barrel(ResearchComplexState state, List<Transformation> list) { @Override
int getWidth() {
return 2;
}
@Override
protected void buildInterior() {
BlockState chest = Blocks.CHEST.getDefaultState().with(ChestBlock.FACING, Direction.SOUTH);
set(new BlockPos(2, 0, 2), chest.with(ChestBlock.CHEST_TYPE, ChestType.RIGHT));
set(new BlockPos(3, 0, 2), chest.with(ChestBlock.CHEST_TYPE, ChestType.LEFT));
set(new BlockPos(4, 0, 4), Blocks.ANVIL.getDefaultState());
set(new BlockPos(3, 0, 4), Blocks.BLAST_FURNACE.getDefaultState());
set(new BlockPos(2, 0, 4), Blocks.SMOKER.getDefaultState());
set(new BlockPos(1, 0, 4), Blocks.FURNACE.getDefaultState());
set(new BlockPos(0, 0, 4), Blocks.STONECUTTER.getDefaultState());
set(new BlockPos(-1, 0, 4), Blocks.CRAFTING_TABLE.getDefaultState());
}
}
private static class StorageRoom extends AbstractResearchComplexRoomPart {
private StorageRoom(ResearchComplexState state, List<Transformation> list) {
super(state, list); super(state, list);
} }
@ -122,14 +163,21 @@ public abstract class AbstractResearchComplexRoomPart extends BaseResearchComple
rect(new BlockPos(4, 2, 3), 1, 1, 2, barrel); rect(new BlockPos(4, 2, 3), 1, 1, 2, barrel);
set(new BlockPos(1, 0, 4), barrel); set(new BlockPos(1, 0, 4), barrel);
} }
}
@Override private interface RoomFactory {
protected void handleBlockPlace(World world, BlockPos pos, BlockState state) { AbstractResearchComplexRoomPart create(ResearchComplexState state, List<Transformation> transformations);
super.handleBlockPlace(world, pos, state); }
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof BarrelBlockEntity) { private static WeightedList<RoomFactory> createWeightedList() {
((BarrelBlockEntity) entity).setLootTable(new Identifier(EnergonRelics.NAMESPACE, "chests/research_complex_barrel"), getState().random.nextLong()); WeightedList<RoomFactory> weightedList = new WeightedList<>();
} weightedList.add(1, StorageRoom::new);
} weightedList.add(4, CraftingRoom::new);
weightedList.add(4, BedRoom::new);
return weightedList;
}
static AbstractResearchComplexRoomPart get(ResearchComplexState state, List<Transformation> transformations) {
return createWeightedList().pick(state.random).create(state, transformations);
} }
} }

View File

@ -0,0 +1,29 @@
package com.thebrokenrail.energonrelics.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class WeightedList<T> {
private final Map<T, Integer> map = new HashMap<>();
public void add(int weight, T obj) {
map.put(obj, weight);
}
public T pick(Random random) {
int totalWeight = 0;
for (Map.Entry<T, Integer> entry : map.entrySet()) {
totalWeight += entry.getValue();
}
int i = random.nextInt(totalWeight + 1);
int countWeight = 0;
for (Map.Entry<T, Integer> entry : map.entrySet()) {
countWeight += entry.getValue();
if (countWeight >= i) {
return entry.getKey();
}
}
throw new UnsupportedOperationException();
}
}

View File

@ -56,7 +56,11 @@
"functions": [ "functions": [
{ {
"function": "minecraft:set_count", "function": "minecraft:set_count",
"count": 1 "count": {
"type": "minecraft:uniform",
"min": 1,
"max": 2
}
} }
] ]
} }
@ -68,7 +72,7 @@
{ {
"type": "minecraft:item", "type": "minecraft:item",
"name": "energonrelics:circuit_board", "name": "energonrelics:circuit_board",
"weight": 12, "weight": 10,
"functions": [ "functions": [
{ {
"function": "minecraft:set_count", "function": "minecraft:set_count",
@ -83,11 +87,15 @@
{ {
"type": "minecraft:item", "type": "minecraft:item",
"name": "energonrelics:veridium_ingot", "name": "energonrelics:veridium_ingot",
"weight": 2, "weight": 3,
"functions": [ "functions": [
{ {
"function": "minecraft:set_count", "function": "minecraft:set_count",
"count": 1 "count": {
"type": "minecraft:uniform",
"min": 1,
"max": 2
}
} }
] ]
} }