Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.bitbucket.transitauthority.transitmod.multiblock;
- import com.google.common.base.Optional;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import eu.tetrabyte.contentmod.tileentity.TileEntityStructureDummy;
- import eu.tetrabyte.contentmod.util.mathsbumpf.Shroud;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockStructureVoid;
- import net.minecraft.block.ITileEntityProvider;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.client.renderer.texture.TextureUtil;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import net.minecraft.world.gen.structure.template.PlacementSettings;
- import org.apache.commons.lang3.builder.StandardToStringStyle;
- import org.apache.commons.lang3.builder.ToStringBuilder;
- import org.apache.logging.log4j.Level;
- import org.bitbucket.transitauthority.transitmod.TransitMod;
- import org.bitbucket.transitauthority.transitmod.util.StringHelper;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- public class StructureTemplate {
- public static final StandardToStringStyle STYLE = new StandardToStringStyle();
- public final ResourceLocation ID;
- public List<BlockInfo> template = Lists.newArrayList();
- public EnumFacing facing = EnumFacing.NORTH;
- public BlockPos size = BlockPos.ORIGIN;
- public BlockPos placementPoint = BlockPos.ORIGIN;
- public PlacementSettings placement = new PlacementSettings();
- static {
- STYLE.setUseShortClassName(true);
- STYLE.setUseIdentityHashCode(false);
- STYLE.setContentStart(":[{");
- STYLE.setContentEnd("}]");
- STYLE.setFieldNameValueSeparator(": ");
- STYLE.setFieldSeparator(", ");
- STYLE.setNullText("---");
- }
- private StructureTemplate(ResourceLocation ID, BlockPos size, EnumFacing facing, BlockPos placementPoint) {
- this.ID = ID;
- this.size = size;
- this.facing = facing;
- this.placementPoint = placementPoint;
- }
- //parse png file
- //save png file (needed?)
- /* build, destroy, activate, check */
- public boolean buildTemplateInWorld(World world, BlockPos worldPos, EnumFacing facing, boolean force) {
- this.placement.setRotation(StructureUtils.getRotationFromDefault(this.facing, facing));
- // BlockPos placementPointRotated = StructureUtils.getRotatedSize(this.placementPoint, this.placement.getRotation());
- // BlockPos placementPointRotated = StructureUtils.getTransformedIndex(this.placementPoint, this.size, this.placement);
- BlockPos placementPointRotated = Shroud.simpleRotate(this.placementPoint, this.placement.getRotation());
- TransitMod.logger.printf(Level.INFO, "place point: %s, rotated: %s, world: %s, world - rotated: %s", StringHelper.get(this.placementPoint), StringHelper.get(placementPointRotated), StringHelper.get(worldPos), StringHelper.get(worldPos.subtract(placementPointRotated)));
- StructureUtils.setRotatedBoundingBox(this.placement, this.size, worldPos.subtract(placementPointRotated));
- BlockPos min = StructureUtils.getCorner(this.placement.getBoundingBox(), false);
- for (BlockInfo info : this.template) {
- if (info == null || info.checkedOnly || info.isWildcard) continue;
- TransitMod.logger.printf(Level.INFO, "info: %s", info.toString(false));
- // BlockPos index = StructureUtils.getTransformedPos(info.pos, this.placement);
- // BlockPos index = StructureUtils.getTransformedIndex(info.pos, this.size, this.placement);
- BlockPos index = Shroud.simpleRotate(info.pos, this.placement.getRotation());
- BlockPos indexWorld = index.add(min);
- TransitMod.logger.printf(Level.INFO, "index after rotation: %s, index world pos after rotation: %s", StringHelper.get(index), StringHelper.get(indexWorld));
- TransitMod.logger.printf(Level.INFO, "rotated place point - index: %s", StringHelper.get(placementPointRotated.subtract(index)));
- //TODO: indexWorld fails isVecInside check but works math-wise
- // if (this.placement.getBoundingBox().isVecInside(indexWorld)) {
- if (force || world.getBlockState(indexWorld).getBlock().isReplaceable(world, indexWorld) || world.isAirBlock(indexWorld)) {
- IBlockState rotated = info.state.withRotation(this.placement.getRotation());
- if (world.setBlockState(indexWorld, rotated, 3)) {
- TileEntity tile = world.getTileEntity(indexWorld);
- if (tile != null) {
- if (info.tileData != null) {
- info.tileData.setInteger("x", indexWorld.getX());
- info.tileData.setInteger("y", indexWorld.getY());
- info.tileData.setInteger("z", indexWorld.getZ());
- tile.readFromNBT(info.tileData);
- tile.func_189667_a(this.placement.getRotation());
- tile.func_189668_a(this.placement.getMirror());
- }
- if (tile instanceof TileEntityStructureDummy) {
- ((TileEntityStructureDummy) tile).structureId = this.ID;
- ((TileEntityStructureDummy) tile).placePointOffset = placementPointRotated.subtract(index);
- }
- }
- }
- }
- }
- // }
- return true;
- }
- public boolean removeBlocksFromWorld(World world, BlockPos sourceWorldPos, boolean breakWildcards) {
- for (BlockInfo info : this.template) {
- if (info.checkedOnly || (!breakWildcards && info.isWildcard)) continue;
- BlockPos worldPos = StructureUtils.getTransformedPos(info.pos, this.placement).add(StructureUtils.getCorner(this.placement.getBoundingBox(), false));
- if (worldPos.equals(sourceWorldPos)) continue;
- //TODO: worldPos fails isVecInside but works fine math-wise
- // else if (this.placement.getBoundingBox().isVecInside(worldPos)) {
- else {
- world.setBlockToAir(worldPos);
- world.removeTileEntity(worldPos);
- }
- }
- return true;
- }
- public boolean checkStructureCompleteness(World world, BlockPos worldPos, EnumFacing facing) {
- this.placement.setRotation(StructureUtils.getRotationFromDefault(this.facing, facing));
- StructureUtils.setRotatedBoundingBox(this.placement, this.size, worldPos.subtract(this.placementPoint));
- BlockPos min = StructureUtils.getCorner(this.placement.getBoundingBox(), false);
- for (BlockInfo info : this.template) {
- BlockPos idx = StructureUtils.getTransformedPos(info.pos, this.placement).add(min);
- if (info.state.getBlock() instanceof BlockStructureVoid) continue;
- IBlockState worldState = world.getBlockState(idx);
- if (!info.state.equals(worldState)) return false;
- }
- return true;
- }
- public boolean activateStructure(World world, BlockPos masterWorldPos, EntityPlayer player, EnumHand hand, ItemStack stack, EnumFacing side, float hitX, float hitY, float hitZ) {
- return world.getBlockState(masterWorldPos).getBlock().onBlockActivated(world, masterWorldPos, world.getBlockState(masterWorldPos), player, hand, stack, side, hitX, hitY, hitZ);
- }
- @Override
- public String toString() {
- return this.toString(false);
- }
- public String toString(boolean full) {
- return this.appendData(new ToStringBuilder(this, STYLE), full).toString();
- }
- public ToStringBuilder appendData(ToStringBuilder builder, boolean full) {
- builder.append("ID", this.ID);
- builder.append("length", this.template.size());
- builder.append("size", StringHelper.get(this.size));
- builder.append("facing", this.facing);
- builder.append("point", StringHelper.get(this.placementPoint));
- builder.append("box", StringHelper.get(this.placement.getBoundingBox()));
- builder.append("rot", this.placement.getRotation());
- builder.append("mir", this.placement.getMirror());
- if (full) {
- this.template.forEach(builder::append);
- builder.appendToString(String.format("%n"));
- }
- return builder;
- }
- public static class BlockInfo {
- public final BlockPos pos;
- public final IBlockState state;
- public final NBTTagCompound tileData;
- public final boolean isWildcard;
- public boolean checkedOnly = false;
- public BlockInfo(BlockPos pos, IBlockState state, NBTTagCompound tileData, boolean checkedOnly) {
- this.pos = pos;
- this.state = state;
- this.tileData = tileData;
- this.isWildcard = state.getBlock() instanceof BlockStructureVoid;
- this.checkedOnly = checkedOnly;
- }
- @Override
- public String toString() {
- return this.toString(false);
- }
- public String toString(boolean startWithNewline) {
- StringBuilder builder = new StringBuilder(startWithNewline ? "%n " : "");
- builder.append(StringHelper.get(this.pos));
- builder.append(String.format("%s, %s, ", this.state, this.tileData != null ? this.tileData : "---"));
- builder.append(String.format("wild: %b, checked only: %b", this.isWildcard, this.checkedOnly));
- return builder.toString();
- }
- }
- public static class Builder {
- public ResourceLocation id;
- public EnumFacing facing;
- public BlockPos size = BlockPos.ORIGIN;
- public BlockPos placementPoint = BlockPos.ORIGIN;
- public Map<Integer, Map<String, String>> colorMap = Maps.newHashMap();
- private List<BlockInfo> normalBlocks = Lists.newArrayList();
- private List<BlockInfo> tileBlocks = Lists.newArrayList();
- private List<BlockInfo> otherBlocks = Lists.newArrayList();
- private List<BlockInfo> template = Lists.newArrayList();
- public Builder(ResourceLocation id, BlockPos size, EnumFacing facing, BlockPos placementPoint) {
- this.id = id;
- this.size = size;
- this.facing = facing;
- this.placementPoint = placementPoint;
- }
- public StructureTemplate processImage(File pngFile) {
- try {
- BufferedImage bufferedImage = TextureUtil.readBufferedImage(new FileInputStream(pngFile));
- if (bufferedImage.getHeight() % this.size.getY() != 0) return null;
- Map<Integer, Map<String, IProperty>> colorToPropertiesMap = Maps.newHashMap();
- Map<String, IProperty> propertyMap = Maps.newHashMap();
- Map<Integer, ResourceLocation> colorToIdMap = Maps.newHashMap();
- this.colorMap.forEach((integer, map) -> colorToIdMap.put(integer, new ResourceLocation(map.remove("block"))));
- IBlockState state;
- for (int y = 0; y < this.size.getY(); y++) {
- for (int x = 0; x < this.size.getX(); x++) {
- for (int z = 0; z < this.size.getZ(); z++) {
- int color = bufferedImage.getRGB(x, z + (y * this.size.getZ()));
- TransitMod.logger.printf(Level.INFO, "(%s, %s, %s)|(%s, %s): color: %s", x, y, z, x, z + (y * this.size.getZ()), Integer.toHexString(bufferedImage.getRGB(x, z + (y * this.size.getZ()))).toUpperCase());
- if ((color & 0xFF000000) == 0) {
- this.addToCorrectList(new BlockInfo(new BlockPos(x, y, z), Blocks.STRUCTURE_VOID.getDefaultState(), null, false));
- continue;
- } else state = Block.REGISTRY.getObject(colorToIdMap.get(color)).getDefaultState();
- if (!colorToPropertiesMap.containsKey(color)) {
- propertyMap.clear();
- state.getPropertyNames().forEach(iProperty -> propertyMap.put(iProperty.getName(), iProperty));
- colorToPropertiesMap.put(color, propertyMap);
- }
- for (Map.Entry<String, String> entry : this.colorMap.get(color).entrySet()) {
- Optional optional = colorToPropertiesMap.get(color).get(entry.getKey()).parseValue(entry.getValue());
- if (optional.isPresent()) state = state.withProperty(colorToPropertiesMap.get(color).get(entry.getKey()), (Comparable) optional.get());
- }
- this.addToCorrectList(new BlockInfo(new BlockPos(x, y, z), state, null, false));
- }
- }
- }
- this.template.clear();
- this.template.addAll(this.normalBlocks);
- this.template.addAll(this.tileBlocks);
- this.template.addAll(this.otherBlocks);
- StructureTemplate ret = new StructureTemplate(this.id, this.size, this.facing, this.placementPoint);
- ret.template = Lists.newArrayList(this.template);
- return ret;
- } catch (IOException e) {
- TransitMod.logger.printf(Level.ERROR, "Failed to parse png file '%s'.", pngFile.getAbsolutePath());
- return null;
- }
- }
- private void addToCorrectList(BlockInfo info) {
- if (info.state.getBlock().hasTileEntity(info.state) || info.state.getBlock() instanceof ITileEntityProvider) this.tileBlocks.add(info);
- else if (!info.state.isFullBlock() && !info.state.isFullCube()) this.otherBlocks.add(info);
- else this.normalBlocks.add(info);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment