TerraForged/src/main/java/com/terraforged/material/Materials.java

124 lines
4.3 KiB
Java
Raw Normal View History

2020-02-28 22:25:38 +00:00
/*
*
* MIT License
*
* Copyright (c) 2020 TerraForged
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2020-05-14 18:56:57 +00:00
package com.terraforged.material;
2020-02-28 22:25:38 +00:00
import com.terraforged.api.material.WGTags;
2020-02-28 22:25:38 +00:00
import com.terraforged.api.material.layer.LayerManager;
2020-05-13 19:31:45 +00:00
import com.terraforged.api.material.state.States;
import com.terraforged.core.concurrent.Resource;
2020-05-14 18:56:57 +00:00
import com.terraforged.util.DummyBlockReader;
2020-05-13 19:31:45 +00:00
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import net.minecraft.block.AirBlock;
2020-02-28 22:25:38 +00:00
import net.minecraft.block.Block;
2020-05-13 19:31:45 +00:00
import net.minecraft.block.BlockState;
import net.minecraft.block.ConcretePowderBlock;
import net.minecraft.block.GrassBlock;
import net.minecraft.block.MyceliumBlock;
import net.minecraft.tags.BlockTags;
2020-02-28 22:25:38 +00:00
import net.minecraft.tags.Tag;
2020-05-13 19:31:45 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.registries.IForgeRegistryEntry;
2020-02-28 22:25:38 +00:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
2020-02-28 22:25:38 +00:00
import java.util.Set;
public class Materials {
2020-05-13 19:31:45 +00:00
public final LayerManager layerManager = new LayerManager();
public final Set<Block> stone = create(WGTags.STONE, States.STONE.getBlock());
public final Set<Block> dirt = create(WGTags.DIRT, States.DIRT.getBlock());
public final Set<Block> clay = create(WGTags.CLAY, States.CLAY.getBlock());
public final Set<Block> sediment = create(WGTags.SEDIMENT, States.GRAVEL.getBlock());
public final Set<Block> erodible = create(WGTags.ERODIBLE, null);
2020-02-28 22:25:38 +00:00
public LayerManager getLayerManager() {
return layerManager;
}
public boolean isStone(Block block) {
return stone.contains(block);
}
public boolean isEarth(Block block) {
return dirt.contains(block);
}
public boolean isClay(Block block) {
return clay.contains(block);
}
public boolean isSediment(Block block) {
return sediment.contains(block);
}
public boolean isErodible(Block block) {
return erodible.contains(block);
}
2020-05-13 19:31:45 +00:00
public boolean isAir(Block block) {
return block instanceof AirBlock;
2020-02-28 22:25:38 +00:00
}
2020-05-13 19:31:45 +00:00
public boolean isGrass(Block block) {
return block instanceof GrassBlock || block instanceof MyceliumBlock;
}
public boolean isSand(Block block) {
return BlockTags.SAND.contains(block) && !(block instanceof ConcretePowderBlock);
}
private static Set<Block> create(Tag<Block> tag, Block def) {
ObjectOpenHashSet<Block> set = new ObjectOpenHashSet<>(tag.getAllElements());
if (set.isEmpty() && def != null) {
set.add(def);
}
return ObjectSets.unmodifiable(set);
2020-02-28 22:25:38 +00:00
}
2020-05-13 19:31:45 +00:00
public static float getHardness(BlockState state) {
try (Resource<DummyBlockReader> reader = DummyBlockReader.pooled()) {
reader.get().set(state);
return state.getBlockHardness(reader.get(), BlockPos.ZERO);
2020-02-28 22:25:38 +00:00
}
}
public static <T extends IForgeRegistryEntry<T>> List<T> toList(Collection<T> collection) {
List<T> list = new ArrayList<>(collection);
list.sort(Materials::compare);
return Collections.unmodifiableList(list);
}
public static int compare(IForgeRegistryEntry<?> a, IForgeRegistryEntry<?> b) {
String as = a.getRegistryName() + "";
String bs = b.getRegistryName() + "";
return as.compareTo(bs);
}
2020-02-28 22:25:38 +00:00
}